RSTreeWidget:

- Added possibility to add own actions to header context menu
- Added resort of items
This commit is contained in:
thunder2 2015-08-21 18:24:56 +02:00
parent 48c3eedcf5
commit 4923c47eb3
2 changed files with 57 additions and 24 deletions

View File

@ -31,6 +31,10 @@ RSTreeWidget::RSTreeWidget(QWidget *parent) : QTreeWidget(parent)
{
mEnableColumnCustomize = false;
mSettingsVersion = 0; // disabled
QHeaderView *h = header();
h->setContextMenuPolicy(Qt::CustomContextMenu);
connect(h, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(headerContextMenuRequested(QPoint)));
}
void RSTreeWidget::setPlaceholderText(const QString &text)
@ -153,16 +157,6 @@ void RSTreeWidget::enableColumnCustomize(bool customizable)
}
mEnableColumnCustomize = customizable;
QHeaderView *h = header();
if (mEnableColumnCustomize) {
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::setColumnCustomizable(int column, bool customizable)
@ -170,25 +164,51 @@ void RSTreeWidget::setColumnCustomizable(int column, bool customizable)
mColumnCustomizable[column] = customizable;
}
void RSTreeWidget::addHeaderContextMenuAction(QAction *action)
{
mHeaderContextMenuActions.push_back(action);
}
void RSTreeWidget::headerContextMenuRequested(const QPoint &pos)
{
if (!mEnableColumnCustomize) {
return;
}
QMenu contextMenu(this);
QTreeWidgetItem *item = headerItem();
int columnCount = item->columnCount();
for (int column = 0; column < columnCount; ++column) {
QMap<int, bool>::const_iterator it = mColumnCustomizable.find(column);
if (it != mColumnCustomizable.end() && *it == false) {
continue;
if (mEnableColumnCustomize) {
QTreeWidgetItem *item = headerItem();
int columnCount = item->columnCount();
for (int column = 0; column < columnCount; ++column) {
QMap<int, bool>::const_iterator it = mColumnCustomizable.find(column);
if (it != mColumnCustomizable.end() && *it == false) {
continue;
}
QAction *action = contextMenu.addAction(QIcon(), item->text(column), this, SLOT(columnVisible()));
action->setCheckable(true);
action->setData(column);
action->setChecked(!isColumnHidden(column));
}
QAction *action = contextMenu.addAction(QIcon(), item->text(column), this, SLOT(columnVisible()));
action->setCheckable(true);
action->setData(column);
action->setChecked(!isColumnHidden(column));
}
if (!mHeaderContextMenuActions.isEmpty()) {
bool addSeparator = false;
if (!contextMenu.isEmpty()) {
// Check for visible action
foreach (QAction *action, mHeaderContextMenuActions) {
if (action->isVisible()) {
addSeparator = true;
break;
}
}
}
if (addSeparator) {
contextMenu.addSeparator();
}
contextMenu.addActions(mHeaderContextMenuActions);
}
if (contextMenu.isEmpty()) {
return;
}
contextMenu.exec(mapToGlobal(pos));
@ -207,3 +227,10 @@ void RSTreeWidget::columnVisible()
emit columnVisibleChanged(column, visible);
}
void RSTreeWidget::resort()
{
if (isSortingEnabled()) {
sortByColumn(header()->sortIndicatorSection(), header()->sortIndicatorOrder());
}
}

View File

@ -43,6 +43,11 @@ public:
void enableColumnCustomize(bool customizable);
void setColumnCustomizable(int column, bool customizable);
void resort();
// Add QAction to context menu (action won't be deleted)
void addHeaderContextMenuAction(QAction *action);
signals:
void signalMouseMiddleButtonClicked(QTreeWidgetItem *item);
void columnVisibleChanged(int column, bool visible);
@ -63,6 +68,7 @@ private:
bool mEnableColumnCustomize;
quint32 mSettingsVersion;
QMap<int, bool> mColumnCustomizable;
QList<QAction*> mHeaderContextMenuActions;
};
#endif