2017-04-27 18:26:05 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "desktop-widgets/tableview.h"
|
|
|
|
#include "desktop-widgets/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
|
|
|
|
2019-11-02 21:52:27 +00:00
|
|
|
connect(ui.tableView, &QTableView::clicked, this, &TableView::itemClicked);
|
|
|
|
|
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);
|
|
|
|
|
2017-11-29 09:57:08 +00:00
|
|
|
QIcon plusIcon(":list-add-icon");
|
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));
|
2020-09-13 00:10:19 +00:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
|
|
|
|
// with Qt 5.15, this leads to an inoperable button
|
2014-10-19 14:15:22 +00:00
|
|
|
plusBtn->resize(btnSize, btnSize);
|
2020-09-13 00:10:19 +00:00
|
|
|
#endif
|
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);
|
2017-04-17 10:16:56 +00:00
|
|
|
for (int i = 1; oldDefault && ui.tableView->model() && i < ui.tableView->model()->columnCount(); i++) {
|
2014-08-27 00:52:05 +00:00
|
|
|
if (ui.tableView->columnWidth(i) != 80)
|
|
|
|
oldDefault = false;
|
|
|
|
}
|
|
|
|
if (oldDefault) {
|
|
|
|
s.remove("");
|
2017-04-04 17:50:46 +00:00
|
|
|
} else if (ui.tableView->model()) {
|
2014-08-27 00:52:05 +00:00
|
|
|
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);
|
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)
|
|
|
|
{
|
2017-01-01 05:17:20 +00:00
|
|
|
int width;
|
2015-03-17 20:56:52 +00:00
|
|
|
QString text = ui.tableView->model()->headerData(col, Qt::Horizontal).toString();
|
2020-01-02 01:14:15 +00:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
|
|
|
|
int textSpace = defaultModelFontMetrics().width(text) + 4;
|
|
|
|
#else // QT 5.11 or newer
|
|
|
|
int textSpace = defaultModelFontMetrics().horizontalAdvance(text) + 4;
|
|
|
|
#endif
|
|
|
|
width = text.isEmpty() ? metrics.rm_col_width : textSpace + 4; // add small margin
|
2017-01-01 05:17:20 +00:00
|
|
|
#if defined(Q_OS_MAC)
|
|
|
|
width += 10; // Mac needs more margin
|
|
|
|
#endif
|
|
|
|
return width;
|
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
|
|
|
}
|