subsurface/qt-ui/tableview.cpp
Tomaz Canabrava 1180b5d2d3 Fix plus icon position based on the theme
I hope this time I got it right. basically, the old code
tried to guess where the plus icon should be with a fairly
bad set of defauults.

This one patch asks for the Qt style where everything is and
uses that knowledge to make it be in a more sane position.

Signed-off-by: Tomaz Canabrava <tomaz.canabrava@intel.com>
Signed-off-by: Giuseppe Bilotta <giuseppe.bilota@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-10-18 15:18:38 -07:00

121 lines
3.3 KiB
C++

#include "tableview.h"
#include "models.h"
#include "modeldelegates.h"
#include <QPushButton>
#include <QLayout>
#include <QFile>
#include <QTextStream>
#include <QSettings>
#include <QStyle>
TableView::TableView(QWidget *parent) : QGroupBox(parent)
{
ui.setupUi(this);
ui.tableView->setItemDelegate(new DiveListDelegate(this));
QFontMetrics fm(defaultModelFont());
int text_ht = fm.height();
int text_em = fm.width('m');
metrics.icon = &defaultIconMetrics();
metrics.col_width = 7*text_em;
metrics.rm_col_width = 3*text_em;
metrics.header_ht = text_ht + 10; // TODO DPI
/* There`s mostly a need for a Mac fix here too. */
if (qApp->style()->objectName() == "gtk+")
layout()->setContentsMargins(0, 9, 0, 0);
else
layout()->setContentsMargins(0, 0, 0, 0);
QIcon plusIcon(":plus");
plusBtn = new QPushButton(plusIcon, QString(), this);
plusBtn->setFlat(true);
plusBtn->setToolTip(tr("Add cylinder"));
plusBtn->setIconSize(QSize(metrics.icon->sz_small, metrics.icon->sz_small));
plusBtn->resize(metrics.icon->sz_med, metrics.icon->sz_med);
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)
{
return col == CylindersModel::REMOVE ? metrics.rm_col_width : metrics.col_width;
}
QTableView *TableView::view()
{
return ui.tableView;
}