From f18bcd66072c115456d98a8aeb87500b2ca8e642 Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Wed, 15 Oct 2014 15:30:49 +0200 Subject: Dynamic dive trip list column widths Compute the default widths for the columns in the dive trip list from their header and (expected) content length rather than some fixed pixel sizes. Signed-off-by: Giuseppe Bilotta Signed-off-by: Dirk Hohndel --- qt-ui/divelistview.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++----- qt-ui/modeldelegates.cpp | 10 ++++++-- qt-ui/modeldelegates.h | 2 ++ 3 files changed, 63 insertions(+), 8 deletions(-) (limited to 'qt-ui') diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp index 29a2301b9..01d53794b 100644 --- a/qt-ui/divelistview.cpp +++ b/qt-ui/divelistview.cpp @@ -28,6 +28,9 @@ #include #include "../qthelper.h" +// # Date Rtg Dpth Dur Tmp Wght Suit Cyl Gas SAC OTU CNS Loc +static int defaultWidth[] = { 70, 140, 90, 50, 50, 50, 50, 70, 50, 50, 70, 50, 50, 500}; + DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelection(false), sortColumn(0), currentOrder(Qt::DescendingOrder), searchBox(this), dontEmitDiveChangedSignal(false), selectionSaved(false) { @@ -44,11 +47,58 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelec setSortingEnabled(false); setContextMenuPolicy(Qt::DefaultContextMenu); header()->setContextMenuPolicy(Qt::ActionsContextMenu); -#ifdef Q_OS_MAC - // Fixes for the layout needed for mac + const QFontMetrics metrics(defaultModelFont()); - header()->setMinimumHeight(metrics.height() + 10); + int ht = metrics.height(); + int em = metrics.width('m'); + int zw = metrics.width('0'); + + // Fixes for the layout needed for mac +#ifdef Q_OS_MAC + header()->setMinimumHeight(ht + 10); #endif + + // TODO FIXME we need this to get the header names + // can we find a smarter way? + DiveTripModel *tripModel = new DiveTripModel(this); + + // set the default width as a minimum between the hard-coded defaults, + // the header text width and the (assumed) content width, calculated + // based on type + for (int col = DiveTripModel::NR; col < DiveTripModel::COLUMNS; ++col) { + QString header_txt = tripModel->headerData(col, Qt::Horizontal, Qt::DisplayRole).toString(); + int width = metrics.width(header_txt); + int sw = 0; + switch (col) { + case DiveTripModel::NR: + case DiveTripModel::DURATION: + sw = 8*zw; + break; + case DiveTripModel::DATE: + sw = 14*em; + break; + case DiveTripModel::RATING: + sw = static_cast(itemDelegateForColumn(col))->starSize().width(); + break; + case DiveTripModel::SUIT: + case DiveTripModel::SAC: + sw = 7*em; + break; + case DiveTripModel::LOCATION: + sw = 50*em; + break; + default: + sw = 5*em; + } + if (sw > width) + width = sw; + width += zw; // small padding + if (width > defaultWidth[col]) + defaultWidth[col] = width; + } + delete tripModel; + + header()->setStretchLastSection(true); QAction *showSearchBox = new QAction(tr("Show search box"), this); showSearchBox->setShortcut(Qt::CTRL + Qt::Key_F); @@ -61,9 +111,6 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelec // connect(&searchBox, SIGNAL(textChanged(QString)), model, SLOT(setFilterFixedString(QString))); } -// # Date Rtg Dpth Dur Tmp Wght Suit Cyl Gas SAC OTU CNS Loc -static int defaultWidth[] = { 70, 140, 90, 50, 50, 50, 50, 70, 50, 50, 70, 50, 50, 500}; - DiveListView::~DiveListView() { QSettings settings; diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp index 70cce7d68..507a5f197 100644 --- a/qt-ui/modeldelegates.cpp +++ b/qt-ui/modeldelegates.cpp @@ -32,6 +32,8 @@ static bool keyboardFinished = false; StarWidgetsDelegate::StarWidgetsDelegate(QWidget *parent) : QStyledItemDelegate(parent), parentWidget(parent) { + const StarMetrics& metrics = StarWidget::metrics(); + minStarSize = QSize(metrics.size * TOTALSTARS + metrics.spacing * (TOTALSTARS - 1), metrics.size); } void StarWidgetsDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const @@ -61,8 +63,12 @@ void StarWidgetsDelegate::paint(QPainter *painter, const QStyleOptionViewItem &o QSize StarWidgetsDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { - const StarMetrics& metrics = StarWidget::metrics(); - return QSize(metrics.size * TOTALSTARS + metrics.spacing * (TOTALSTARS - 1), metrics.size); + return minStarSize; +} + +const QSize& StarWidgetsDelegate::starSize() const +{ + return minStarSize; } ComboBoxDelegate::ComboBoxDelegate(QAbstractItemModel *model, QObject *parent) : QStyledItemDelegate(parent), model(model) diff --git a/qt-ui/modeldelegates.h b/qt-ui/modeldelegates.h index bc5dd3fcd..94ffe0868 100644 --- a/qt-ui/modeldelegates.h +++ b/qt-ui/modeldelegates.h @@ -20,9 +20,11 @@ public: explicit StarWidgetsDelegate(QWidget *parent = 0); virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const; + const QSize& starSize() const; private: QWidget *parentWidget; + QSize minStarSize; }; class ComboBoxDelegate : public QStyledItemDelegate { -- cgit v1.2.3-70-g09d2