[rkward] rkward/dialogs: Make the category headers look and behave like in other accordion-like controls in RKWard.
Thomas Friedrichsmeier
thomas.friedrichsmeier at ruhr-uni-bochum.de
Sat Nov 14 21:06:17 UTC 2015
Git commit a6c87137afea58a181905347bcb65b5a28ad61c8 by Thomas Friedrichsmeier.
Committed on 14/11/2015 at 21:03.
Pushed by tfry into branch 'master'.
Make the category headers look and behave like in other accordion-like controls in RKWard.
M +55 -9 rkward/dialogs/rkloadlibsdialog.cpp
M +1 -0 rkward/dialogs/rkloadlibsdialog.h
http://commits.kde.org/rkward/a6c87137afea58a181905347bcb65b5a28ad61c8
diff --git a/rkward/dialogs/rkloadlibsdialog.cpp b/rkward/dialogs/rkloadlibsdialog.cpp
index 25b4f34..2c33d70 100644
--- a/rkward/dialogs/rkloadlibsdialog.cpp
+++ b/rkward/dialogs/rkloadlibsdialog.cpp
@@ -631,6 +631,35 @@ void LoadUnloadWidget::cancel () {
}
/////////////////////// InstallPackagesWidget //////////////////////////
+#include <QHeaderView>
+#include <QStyledItemDelegate>
+
+/** Responsible for drawing the "category" items */
+class InstallPackagesDelegate : public QStyledItemDelegate {
+public:
+ InstallPackagesDelegate (QTreeView* parent) : QStyledItemDelegate (parent) {
+ table = parent;
+ expanded = RKStandardIcons::getIcon (RKStandardIcons::ActionCollapseUp);
+ collapsed = RKStandardIcons::getIcon (RKStandardIcons::ActionExpandDown);
+ }
+ void initStyleOption (QStyleOptionViewItem* option, const QModelIndex& index) const {
+ QStyledItemDelegate::initStyleOption (option, index);
+ if (!index.parent ().isValid ()) {
+ QStyleOptionViewItemV4 *v4 = qstyleoption_cast<QStyleOptionViewItemV4 *> (option);
+ if (!v4) {
+ RK_ASSERT (false);
+ return;
+ }
+ v4->icon = table->isExpanded (index) ? expanded : collapsed;
+ v4->features |= QStyleOptionViewItemV2::HasDecoration;
+ v4->font.setBold (true);
+ v4->backgroundBrush = table->palette ().mid ();
+ }
+ }
+ QTreeView* table;
+ QIcon expanded;
+ QIcon collapsed;
+};
InstallPackagesWidget::InstallPackagesWidget (RKLoadLibsDialog *dialog) : QWidget (0) {
RK_TRACE (DIALOGS);
@@ -652,6 +681,13 @@ InstallPackagesWidget::InstallPackagesWidget (RKLoadLibsDialog *dialog) : QWidge
model->setFilterCaseSensitivity (Qt::CaseInsensitive);
model->setSortCaseSensitivity (Qt::CaseInsensitive);
packages_view->setModel (model);
+ packages_view->setItemDelegateForColumn (0, new InstallPackagesDelegate (packages_view));
+ for (int i = 0; i < model->rowCount (); ++i) { // the root level captions
+ packages_view->setFirstColumnSpanned (i, QModelIndex (), true);
+ }
+ connect (packages_view, SIGNAL(clicked(QModelIndex)), this, SLOT(rowClicked(QModelIndex)));
+ packages_view->setRootIsDecorated (false);
+ packages_view->setIndentation (0);
packages_view->setEnabled (false);
packages_view->setMinimumHeight (packages_view->sizeHintForRow (0) * 15); // force a decent height
packages_view->setMinimumWidth (packages_view->fontMetrics ().width ("This is to force a sensible min width for the packages view (empty on construction)"));
@@ -709,12 +745,23 @@ void InstallPackagesWidget::initialize () {
packages_status->initialize (parent->chain);
packages_view->setEnabled (true);
- for (int i = 0; i <= RKRPackageInstallationStatus::PackageName; ++i) {
+ // Force a good width for the icon column, particularly for MacOS X.
+ packages_view->header ()->resizeSection (0, packages_view->sizeHintForIndex (model->index (0, 0, model->index (RKRPackageInstallationStatus::NewPackages, 0, QModelIndex ()))).width () + packages_view->indentation ());
+ for (int i = 1; i <= RKRPackageInstallationStatus::PackageName; ++i) {
packages_view->resizeColumnToContents (i);
-#ifdef Q_WS_MAC
- // HACK: Without this, the column is just too narrow on MacOS X, and the icon is hidden
- if (i == RKRPackageInstallationStatus::EnhancesRKWard) packages_view->setColumnWidth (i, packages_view->columnWidth (i) + 16);
-#endif
+ }
+ // For whatever reason, we have to re-set these, here.
+ for (int i = 0; i < model->rowCount (); ++i) {
+ packages_view->setFirstColumnSpanned (i, QModelIndex (), true);
+ }
+}
+
+void InstallPackagesWidget::rowClicked (const QModelIndex& row) {
+ RK_TRACE (DIALOGS);
+
+ if (!row.parent ().isValid ()) {
+ QModelIndex fixed_row = model->index (row.row (), 0, row.parent ());
+ packages_view->setExpanded (fixed_row, !packages_view->isExpanded (fixed_row));
}
}
@@ -996,16 +1043,15 @@ QVariant RKRPackageInstallationStatus::data (const QModelIndex &index, int role)
if (!index.parent ().isValid ()) { // top level item
int row = index.row ();
if (row == UpdateablePackages) {
- if ((role == Qt::DisplayRole) && (index.column () == PackageName)) return QVariant (i18n ("Updateable Packages"));
+ if (role == Qt::DisplayRole) return QVariant (i18n ("Updateable Packages"));
if (role == Qt::ToolTipRole) return QVariant (i18n ("Packages for which an update is available. This may include packages which were merely built against a newer version of R."));
} else if (row == NewPackages) {
- if ((role == Qt::DisplayRole) && (index.column () == PackageName)) return QVariant (i18n ("New Packages"));
+ if (role == Qt::DisplayRole) return QVariant (i18n ("New Packages"));
if (role == Qt::ToolTipRole) return QVariant (i18n ("Packages which are available for installation, but which are not currently installed."));
} else if (row == InstalledPackages) {
- if ((role == Qt::DisplayRole) && (index.column () == PackageName)) return QVariant (i18n ("Installed Packages"));
+ if (role == Qt::DisplayRole) return QVariant (i18n ("Installed Packages"));
if (role == Qt::ToolTipRole) return QVariant (i18n ("Packages which are installed locally. Note that updates may be available for these packages."));
}
- if (role == Qt::BackgroundColorRole) return QVariant (QColor (200, 200, 200));
} else if (!index.parent ().parent ().isValid ()) { // model has exactly two levels
const int col = index.column ();
const int prow = index.parent ().row ();
diff --git a/rkward/dialogs/rkloadlibsdialog.h b/rkward/dialogs/rkloadlibsdialog.h
index f4a5d55..9952e16 100644
--- a/rkward/dialogs/rkloadlibsdialog.h
+++ b/rkward/dialogs/rkloadlibsdialog.h
@@ -252,6 +252,7 @@ public slots:
void activated ();
void markAllUpdates ();
void configureRepositories ();
+ void rowClicked (const QModelIndex& row);
private:
void doInstall (bool refresh);
QTreeView *packages_view;
More information about the rkward-tracker
mailing list