aboutsummaryrefslogtreecommitdiffstats
path: root/desktop-widgets/tableview.cpp
diff options
context:
space:
mode:
authorGravatar Tomaz Canabrava <tomaz.canabrava@intel.com>2015-09-03 14:20:19 -0300
committerGravatar Dirk Hohndel <dirk@hohndel.org>2015-10-30 10:36:49 -0700
commite49d6213ad129284a45d53c3fcdc03249e84efe2 (patch)
tree2946a666ab38af3375e7bb2b8c5dd887d4a7f9a1 /desktop-widgets/tableview.cpp
parent588abd019fb2ed3f607682f2b6c7fe86a7a5bb90 (diff)
downloadsubsurface-e49d6213ad129284a45d53c3fcdc03249e84efe2.tar.gz
Move qt-ui to desktop-widgets
Since we have now destkop and mobile versions, 'qt-ui' was a very poor name choice for a folder that contains only destkop-enabled widgets. Also, move the graphicsview-common.h/cpp to subsurface-core because it doesn't depend on qgraphicsview, it merely implements all the colors that we use throughout Subsurface, and we will use colors on both desktop and mobile versions Same thing applies for metrics.h/cpp Signed-off-by: Tomaz Canabrava <tomaz.canabrava@intel.com> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'desktop-widgets/tableview.cpp')
-rw-r--r--desktop-widgets/tableview.cpp145
1 files changed, 145 insertions, 0 deletions
diff --git a/desktop-widgets/tableview.cpp b/desktop-widgets/tableview.cpp
new file mode 100644
index 000000000..40d5199ec
--- /dev/null
+++ b/desktop-widgets/tableview.cpp
@@ -0,0 +1,145 @@
+#include "tableview.h"
+#include "modeldelegates.h"
+
+#include <QPushButton>
+#include <QSettings>
+
+TableView::TableView(QWidget *parent) : QGroupBox(parent)
+{
+ ui.setupUi(this);
+ ui.tableView->setItemDelegate(new DiveListDelegate(this));
+
+ QFontMetrics fm(defaultModelFont());
+ int text_ht = fm.height();
+
+ metrics.icon = &defaultIconMetrics();
+
+ metrics.rm_col_width = metrics.icon->sz_small + 2*metrics.icon->spacing;
+ metrics.header_ht = text_ht + 10; // TODO DPI
+
+ /* 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);
+
+ QIcon plusIcon(":plus");
+ plusBtn = new QPushButton(plusIcon, QString(), this);
+ plusBtn->setFlat(true);
+
+ /* 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);
+ connect(plusBtn, SIGNAL(clicked(bool)), this, SIGNAL(addButtonClicked()));
+}
+
+TableView::~TableView()
+{
+ QSettings s;
+ s.beginGroup(objectName());
+ // 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 (ui.tableView->columnWidth(i) == defaultColumnWidth(i))
+ s.remove(QString("colwidth%1").arg(i));
+ else
+ s.setValue(QString("colwidth%1").arg(i), ui.tableView->columnWidth(i));
+ }
+ }
+ s.endGroup();
+}
+
+void TableView::setBtnToolTip(const QString &tooltip)
+{
+ plusBtn->setToolTip(tooltip);
+}
+
+void TableView::setModel(QAbstractItemModel *model)
+{
+ ui.tableView->setModel(model);
+ connect(ui.tableView, SIGNAL(clicked(QModelIndex)), model, SLOT(remove(QModelIndex)));
+
+ QSettings s;
+ s.beginGroup(objectName());
+ const int columnCount = ui.tableView->model()->columnCount();
+ for (int i = 0; i < columnCount; i++) {
+ QVariant width = s.value(QString("colwidth%1").arg(i), defaultColumnWidth(i));
+ ui.tableView->setColumnWidth(i, width.toInt());
+ }
+ s.endGroup();
+
+ ui.tableView->horizontalHeader()->setMinimumHeight(metrics.header_ht);
+}
+
+void TableView::fixPlusPosition()
+{
+ 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());
+}
+
+// We need to manually position the 'plus' on cylinder and weight.
+void TableView::resizeEvent(QResizeEvent *event)
+{
+ fixPlusPosition();
+ QWidget::resizeEvent(event);
+}
+
+void TableView::showEvent(QShowEvent *event)
+{
+ QWidget::showEvent(event);
+ fixPlusPosition();
+}
+
+void TableView::edit(const QModelIndex &index)
+{
+ ui.tableView->edit(index);
+}
+
+int TableView::defaultColumnWidth(int col)
+{
+ QString text = ui.tableView->model()->headerData(col, Qt::Horizontal).toString();
+ return text.isEmpty() ? metrics.rm_col_width : defaultModelFontMetrics().width(text) + 4; // add small margin
+}
+
+QTableView *TableView::view()
+{
+ return ui.tableView;
+}