Added customize of columns to RSTreeWidget.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@8582 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2015-07-09 20:23:52 +00:00
parent 46a4273668
commit e8e02b9c79
2 changed files with 89 additions and 0 deletions

View File

@ -21,11 +21,15 @@
#include <QPainter>
#include <QMouseEvent>
#include <QHeaderView>
#include <QMenu>
#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<QAction*>(sender());
if (!action) {
return;
}
int column = action->data().toInt();
bool visible = action->isChecked();
setColumnHidden(column, !visible);
emit columnVisibleChanged(column, visible);
}

View File

@ -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