From e8e02b9c797c8d02a23fd8dda72ed359a044a66f Mon Sep 17 00:00:00 2001 From: thunder2 Date: Thu, 9 Jul 2015 20:23:52 +0000 Subject: [PATCH] Added customize of columns to RSTreeWidget. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@8582 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- .../src/gui/common/RSTreeWidget.cpp | 78 +++++++++++++++++++ retroshare-gui/src/gui/common/RSTreeWidget.h | 11 +++ 2 files changed, 89 insertions(+) diff --git a/retroshare-gui/src/gui/common/RSTreeWidget.cpp b/retroshare-gui/src/gui/common/RSTreeWidget.cpp index c5c4cae45..ef8e225ba 100644 --- a/retroshare-gui/src/gui/common/RSTreeWidget.cpp +++ b/retroshare-gui/src/gui/common/RSTreeWidget.cpp @@ -21,11 +21,15 @@ #include #include +#include +#include #include "RSTreeWidget.h" +#include "gui/settings/rsharesettings.h" RSTreeWidget::RSTreeWidget(QWidget *parent) : QTreeWidget(parent) { + mColumnCustomizable = false; } void RSTreeWidget::setPlaceholderText(const QString &text) @@ -78,6 +82,12 @@ void RSTreeWidget::filterItems(int filterColumn, const QString &text) for (int index = 0; index < count; ++index) { filterItem(topLevelItem(index), filterColumn, text); } + + QTreeWidgetItem *item = currentItem(); + if (item && item->isHidden()) { + // active item is hidden, deselect it + setCurrentItem(NULL); + } } bool RSTreeWidget::filterItem(QTreeWidgetItem *item, int filterColumn, const QString &text) @@ -106,3 +116,71 @@ bool RSTreeWidget::filterItem(QTreeWidgetItem *item, int filterColumn, const QSt return (itemVisible || visibleChildCount); } + +void RSTreeWidget::processSettings(bool load) +{ + if (load) { + // load settings + + // state of tree widget + header()->restoreState(Settings->value(objectName()).toByteArray()); + } else { + // save settings + + // state of tree widget + Settings->setValue(objectName(), header()->saveState()); + } +} + +void RSTreeWidget::setColumnCustomizable(bool customizable) +{ + if (customizable == mColumnCustomizable) { + return; + } + + mColumnCustomizable = customizable; + + QHeaderView *h = header(); + + if (mColumnCustomizable) { + h->setContextMenuPolicy(Qt::CustomContextMenu); + connect(h, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(headerContextMenuRequested(QPoint))); + } else { + h->setContextMenuPolicy(Qt::DefaultContextMenu); + disconnect(h, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(headerContextMenuRequested(QPoint))); + } +} + +void RSTreeWidget::headerContextMenuRequested(const QPoint &pos) +{ + if (!mColumnCustomizable) { + return; + } + + QMenu contextMenu(this); + + QTreeWidgetItem *item = headerItem(); + int columnCount = item->columnCount(); + for (int column = 0; column < columnCount; ++column) { + QAction *action = contextMenu.addAction(QIcon(), item->text(column), this, SLOT(columnVisible())); + action->setCheckable(true); + action->setData(column); + action->setChecked(!isColumnHidden(column)); + } + + contextMenu.exec(mapToGlobal(pos)); +} + +void RSTreeWidget::columnVisible() +{ + QAction *action = dynamic_cast(sender()); + if (!action) { + return; + } + + int column = action->data().toInt(); + bool visible = action->isChecked(); + setColumnHidden(column, !visible); + + emit columnVisibleChanged(column, visible); +} diff --git a/retroshare-gui/src/gui/common/RSTreeWidget.h b/retroshare-gui/src/gui/common/RSTreeWidget.h index 0a06fb307..12c76ddf7 100644 --- a/retroshare-gui/src/gui/common/RSTreeWidget.h +++ b/retroshare-gui/src/gui/common/RSTreeWidget.h @@ -37,17 +37,28 @@ public: void filterItems(int filterColumn, const QString &text); + void processSettings(bool load); + + void setColumnCustomizable(bool customizable); + signals: void signalMouseMiddleButtonClicked(QTreeWidgetItem *item); + void columnVisibleChanged(int column, bool visible); private: bool filterItem(QTreeWidgetItem *item, int filterColumn, const QString &text); +private slots: + void headerContextMenuRequested(const QPoint &pos); + void columnVisible(); + protected: void paintEvent(QPaintEvent *event); virtual void mousePressEvent(QMouseEvent *event); +private: QString mPlaceholderText; + bool mColumnCustomizable; }; #endif