2013-09-03 16:57:38 +00:00
|
|
|
#include "tableview.h"
|
2013-12-11 23:44:05 +00:00
|
|
|
#include "modeldelegates.h"
|
2013-09-03 16:57:38 +00:00
|
|
|
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QSettings>
|
|
|
|
|
2014-10-17 19:22:49 +00:00
|
|
|
TableView::TableView(QWidget *parent) : QGroupBox(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));
|
2014-10-15 13:30:48 +00:00
|
|
|
|
|
|
|
QFontMetrics fm(defaultModelFont());
|
|
|
|
int text_ht = fm.height();
|
2014-10-15 13:30:52 +00:00
|
|
|
|
|
|
|
metrics.icon = &defaultIconMetrics();
|
2014-10-15 13:30:48 +00:00
|
|
|
|
2014-10-19 14:15:20 +00:00
|
|
|
metrics.rm_col_width = metrics.icon->sz_small + 2*metrics.icon->spacing;
|
2014-10-15 13:30:48 +00:00
|
|
|
metrics.header_ht = text_ht + 10; // TODO DPI
|
|
|
|
|
2014-10-19 14:15:21 +00:00
|
|
|
/* We want to get rid of the margin around the table, but
|
|
|
|
* we must be careful with some styles (e.g. GTK+) where the top
|
|
|
|
* margin is actually used to hold the label. We thus check the
|
|
|
|
* rectangles for the label and contents to make sure they do not
|
|
|
|
* overlap, and adjust the top contentsMargin accordingly
|
|
|
|
*/
|
|
|
|
|
|
|
|
// start by setting all the margins at zero
|
|
|
|
QMargins margins;
|
|
|
|
|
|
|
|
// grab the label and contents dimensions and positions
|
|
|
|
QStyleOptionGroupBox option;
|
|
|
|
initStyleOption(&option);
|
|
|
|
QRect labelRect = style()->subControlRect(QStyle::CC_GroupBox, &option, QStyle::SC_GroupBoxLabel, this);
|
|
|
|
QRect contentsRect = style()->subControlRect(QStyle::CC_GroupBox, &option, QStyle::SC_GroupBoxContents, this);
|
|
|
|
|
|
|
|
/* we need to ensure that the bottom of the label is higher
|
|
|
|
* than the top of the contents */
|
|
|
|
int delta = contentsRect.top() - labelRect.bottom();
|
|
|
|
const int min_gap = metrics.icon->spacing;
|
|
|
|
if (delta <= min_gap) {
|
|
|
|
margins.setTop(min_gap - delta);
|
|
|
|
}
|
|
|
|
layout()->setContentsMargins(margins);
|
|
|
|
|
2013-09-03 16:57:38 +00:00
|
|
|
QIcon plusIcon(":plus");
|
2014-10-17 19:22:49 +00:00
|
|
|
plusBtn = new QPushButton(plusIcon, QString(), this);
|
2013-09-03 16:57:38 +00:00
|
|
|
plusBtn->setFlat(true);
|
2014-10-19 14:15:22 +00:00
|
|
|
|
|
|
|
/* now determine the icon and button size. Since the button will be
|
|
|
|
* placed in the label, make sure that we do not overflow, as it might
|
|
|
|
* get clipped
|
|
|
|
*/
|
|
|
|
int iconSize = metrics.icon->sz_small;
|
|
|
|
int btnSize = iconSize + 2*min_gap;
|
|
|
|
if (btnSize > labelRect.height()) {
|
|
|
|
btnSize = labelRect.height();
|
|
|
|
iconSize = btnSize - 2*min_gap;
|
|
|
|
}
|
|
|
|
plusBtn->setIconSize(QSize(iconSize, iconSize));
|
|
|
|
plusBtn->resize(btnSize, btnSize);
|
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++) {
|
2014-10-15 13:30:48 +00:00
|
|
|
if (ui.tableView->columnWidth(i) == defaultColumnWidth(i))
|
2014-08-27 00:52:05 +00:00
|
|
|
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-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-10-15 13:30:48 +00:00
|
|
|
QVariant width = s.value(QString("colwidth%1").arg(i), defaultColumnWidth(i));
|
2013-11-22 01:16:19 +00:00
|
|
|
ui.tableView->setColumnWidth(i, width.toInt());
|
2013-09-03 16:57:38 +00:00
|
|
|
}
|
|
|
|
s.endGroup();
|
|
|
|
|
2014-10-15 13:30:48 +00:00
|
|
|
ui.tableView->horizontalHeader()->setMinimumHeight(metrics.header_ht);
|
2013-09-03 16:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TableView::fixPlusPosition()
|
|
|
|
{
|
2014-10-17 19:22:49 +00:00
|
|
|
QStyleOptionGroupBox option;
|
|
|
|
initStyleOption(&option);
|
|
|
|
QRect labelRect = style()->subControlRect(QStyle::CC_GroupBox, &option, QStyle::SC_GroupBoxLabel, this);
|
|
|
|
QRect contentsRect = style()->subControlRect(QStyle::CC_GroupBox, &option, QStyle::QStyle::SC_GroupBoxFrame, this);
|
|
|
|
plusBtn->setGeometry( contentsRect.width() - plusBtn->width(), labelRect.y(), plusBtn->width(), labelRect.height());
|
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-10-15 13:30:48 +00:00
|
|
|
int TableView::defaultColumnWidth(int col)
|
|
|
|
{
|
2015-03-17 20:56:52 +00:00
|
|
|
QString text = ui.tableView->model()->headerData(col, Qt::Horizontal).toString();
|
|
|
|
return text.isEmpty() ? metrics.rm_col_width : defaultModelFontMetrics().width(text) + 4; // add small margin
|
2014-10-15 13:30:48 +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
|
|
|
}
|