mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-18 05:50:39 -04:00
RSTreeWidget:
- Added possibility to add own actions to header context menu - Added resort of items
This commit is contained in:
parent
48c3eedcf5
commit
4923c47eb3
2 changed files with 57 additions and 24 deletions
|
@ -31,6 +31,10 @@ RSTreeWidget::RSTreeWidget(QWidget *parent) : QTreeWidget(parent)
|
||||||
{
|
{
|
||||||
mEnableColumnCustomize = false;
|
mEnableColumnCustomize = false;
|
||||||
mSettingsVersion = 0; // disabled
|
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)
|
void RSTreeWidget::setPlaceholderText(const QString &text)
|
||||||
|
@ -153,16 +157,6 @@ void RSTreeWidget::enableColumnCustomize(bool customizable)
|
||||||
}
|
}
|
||||||
|
|
||||||
mEnableColumnCustomize = 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)
|
void RSTreeWidget::setColumnCustomizable(int column, bool customizable)
|
||||||
|
@ -170,14 +164,16 @@ void RSTreeWidget::setColumnCustomizable(int column, bool customizable)
|
||||||
mColumnCustomizable[column] = customizable;
|
mColumnCustomizable[column] = customizable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RSTreeWidget::addHeaderContextMenuAction(QAction *action)
|
||||||
|
{
|
||||||
|
mHeaderContextMenuActions.push_back(action);
|
||||||
|
}
|
||||||
|
|
||||||
void RSTreeWidget::headerContextMenuRequested(const QPoint &pos)
|
void RSTreeWidget::headerContextMenuRequested(const QPoint &pos)
|
||||||
{
|
{
|
||||||
if (!mEnableColumnCustomize) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QMenu contextMenu(this);
|
QMenu contextMenu(this);
|
||||||
|
|
||||||
|
if (mEnableColumnCustomize) {
|
||||||
QTreeWidgetItem *item = headerItem();
|
QTreeWidgetItem *item = headerItem();
|
||||||
int columnCount = item->columnCount();
|
int columnCount = item->columnCount();
|
||||||
for (int column = 0; column < columnCount; ++column) {
|
for (int column = 0; column < columnCount; ++column) {
|
||||||
|
@ -190,6 +186,30 @@ void RSTreeWidget::headerContextMenuRequested(const QPoint &pos)
|
||||||
action->setData(column);
|
action->setData(column);
|
||||||
action->setChecked(!isColumnHidden(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));
|
contextMenu.exec(mapToGlobal(pos));
|
||||||
}
|
}
|
||||||
|
@ -207,3 +227,10 @@ void RSTreeWidget::columnVisible()
|
||||||
|
|
||||||
emit columnVisibleChanged(column, visible);
|
emit columnVisibleChanged(column, visible);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RSTreeWidget::resort()
|
||||||
|
{
|
||||||
|
if (isSortingEnabled()) {
|
||||||
|
sortByColumn(header()->sortIndicatorSection(), header()->sortIndicatorOrder());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -43,6 +43,11 @@ public:
|
||||||
void enableColumnCustomize(bool customizable);
|
void enableColumnCustomize(bool customizable);
|
||||||
void setColumnCustomizable(int column, 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:
|
signals:
|
||||||
void signalMouseMiddleButtonClicked(QTreeWidgetItem *item);
|
void signalMouseMiddleButtonClicked(QTreeWidgetItem *item);
|
||||||
void columnVisibleChanged(int column, bool visible);
|
void columnVisibleChanged(int column, bool visible);
|
||||||
|
@ -63,6 +68,7 @@ private:
|
||||||
bool mEnableColumnCustomize;
|
bool mEnableColumnCustomize;
|
||||||
quint32 mSettingsVersion;
|
quint32 mSettingsVersion;
|
||||||
QMap<int, bool> mColumnCustomizable;
|
QMap<int, bool> mColumnCustomizable;
|
||||||
|
QList<QAction*> mHeaderContextMenuActions;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue