2013-09-03 16:57:38 +00:00
|
|
|
#include "tableview.h"
|
|
|
|
#include "models.h"
|
2013-12-11 23:44:05 +00:00
|
|
|
#include "modeldelegates.h"
|
2013-09-03 16:57:38 +00:00
|
|
|
|
|
|
|
#include <QPushButton>
|
2013-09-26 23:35:30 +00:00
|
|
|
#include <QLayout>
|
2013-09-03 16:57:38 +00:00
|
|
|
#include <QFile>
|
|
|
|
#include <QTextStream>
|
|
|
|
#include <QSettings>
|
|
|
|
|
2013-11-12 19:57:33 +00:00
|
|
|
TableView::TableView(QWidget *parent) : QWidget(parent)
|
2013-10-03 18:54:25 +00:00
|
|
|
{
|
|
|
|
ui.setupUi(this);
|
2014-01-15 17:52:42 +00:00
|
|
|
ui.tableView->setItemDelegate(new DiveListDelegate(this));
|
2013-09-26 23:35:30 +00:00
|
|
|
/* There`s mostly a need for a Mac fix here too. */
|
|
|
|
if (qApp->style()->objectName() == "gtk+")
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.groupBox->layout()->setContentsMargins(0, 9, 0, 0);
|
2013-09-26 23:35:30 +00:00
|
|
|
else
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.groupBox->layout()->setContentsMargins(0, 0, 0, 0);
|
2013-09-03 16:57:38 +00:00
|
|
|
QIcon plusIcon(":plus");
|
2013-10-03 18:54:25 +00:00
|
|
|
plusBtn = new QPushButton(plusIcon, QString(), ui.groupBox);
|
2013-09-03 16:57:38 +00:00
|
|
|
plusBtn->setFlat(true);
|
2014-07-11 17:39:03 +00:00
|
|
|
plusBtn->setToolTip(tr("Add cylinder"));
|
2014-02-28 04:09:57 +00:00
|
|
|
plusBtn->setIconSize(QSize(16, 16));
|
2013-09-03 16:57:38 +00:00
|
|
|
connect(plusBtn, SIGNAL(clicked(bool)), this, SIGNAL(addButtonClicked()));
|
|
|
|
}
|
|
|
|
|
|
|
|
TableView::~TableView()
|
|
|
|
{
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(objectName());
|
2014-08-27 00:52:05 +00:00
|
|
|
// remove the old default
|
|
|
|
bool oldDefault = (ui.tableView->columnWidth(0) == 30);
|
|
|
|
for (int i = 1; oldDefault && i < ui.tableView->model()->columnCount(); i++) {
|
|
|
|
if (ui.tableView->columnWidth(i) != 80)
|
|
|
|
oldDefault = false;
|
|
|
|
}
|
|
|
|
if (oldDefault) {
|
|
|
|
s.remove("");
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < ui.tableView->model()->columnCount(); i++) {
|
|
|
|
if ((i == CylindersModel::REMOVE && ui.tableView->columnWidth(i) == 30) ||
|
|
|
|
ui.tableView->columnWidth(i) == 70)
|
|
|
|
s.remove(QString("colwidth%1").arg(i));
|
|
|
|
else
|
|
|
|
s.setValue(QString("colwidth%1").arg(i), ui.tableView->columnWidth(i));
|
|
|
|
}
|
2013-09-03 16:57:38 +00:00
|
|
|
}
|
|
|
|
s.endGroup();
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void TableView::setBtnToolTip(const QString &tooltip)
|
2013-09-03 16:57:38 +00:00
|
|
|
{
|
|
|
|
plusBtn->setToolTip(tooltip);
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void TableView::setTitle(const QString &title)
|
2013-09-03 16:57:38 +00:00
|
|
|
{
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.groupBox->setTitle(title);
|
2013-09-03 16:57:38 +00:00
|
|
|
}
|
|
|
|
|
2014-01-16 04:50:56 +00:00
|
|
|
void TableView::setModel(QAbstractItemModel *model)
|
|
|
|
{
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.tableView->setModel(model);
|
|
|
|
connect(ui.tableView, SIGNAL(clicked(QModelIndex)), model, SLOT(remove(QModelIndex)));
|
2013-09-03 16:57:38 +00:00
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(objectName());
|
2013-11-22 01:16:19 +00:00
|
|
|
const int columnCount = ui.tableView->model()->columnCount();
|
|
|
|
for (int i = 0; i < columnCount; i++) {
|
2014-08-27 00:52:05 +00:00
|
|
|
QVariant width = s.value(QString("colwidth%1").arg(i), i == CylindersModel::REMOVE ? 30 : 70);
|
2013-11-22 01:16:19 +00:00
|
|
|
ui.tableView->setColumnWidth(i, width.toInt());
|
2013-09-03 16:57:38 +00:00
|
|
|
}
|
|
|
|
s.endGroup();
|
|
|
|
|
|
|
|
QFontMetrics metrics(defaultModelFont());
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.tableView->horizontalHeader()->setMinimumHeight(metrics.height() + 10);
|
2013-09-03 16:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TableView::fixPlusPosition()
|
|
|
|
{
|
2014-02-28 04:09:57 +00:00
|
|
|
plusBtn->setGeometry(ui.groupBox->contentsRect().width() - 30, 2, 24, 24);
|
2013-09-03 16:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We need to manually position the 'plus' on cylinder and weight.
|
2014-02-28 04:09:57 +00:00
|
|
|
void TableView::resizeEvent(QResizeEvent *event)
|
2013-09-03 16:57:38 +00:00
|
|
|
{
|
|
|
|
fixPlusPosition();
|
|
|
|
QWidget::resizeEvent(event);
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void TableView::showEvent(QShowEvent *event)
|
2013-09-03 16:57:38 +00:00
|
|
|
{
|
|
|
|
QWidget::showEvent(event);
|
|
|
|
fixPlusPosition();
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void TableView::edit(const QModelIndex &index)
|
2014-01-16 04:50:56 +00:00
|
|
|
{
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.tableView->edit(index);
|
2013-09-03 16:57:38 +00:00
|
|
|
}
|
|
|
|
|
2014-01-16 04:50:56 +00:00
|
|
|
QTableView *TableView::view()
|
|
|
|
{
|
2013-10-03 18:54:25 +00:00
|
|
|
return ui.tableView;
|
2013-09-03 16:57:38 +00:00
|
|
|
}
|