mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-10-01 02:35:48 -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
@ -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,25 +164,51 @@ 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);
|
||||||
|
|
||||||
QTreeWidgetItem *item = headerItem();
|
if (mEnableColumnCustomize) {
|
||||||
int columnCount = item->columnCount();
|
QTreeWidgetItem *item = headerItem();
|
||||||
for (int column = 0; column < columnCount; ++column) {
|
int columnCount = item->columnCount();
|
||||||
QMap<int, bool>::const_iterator it = mColumnCustomizable.find(column);
|
for (int column = 0; column < columnCount; ++column) {
|
||||||
if (it != mColumnCustomizable.end() && *it == false) {
|
QMap<int, bool>::const_iterator it = mColumnCustomizable.find(column);
|
||||||
continue;
|
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);
|
if (!mHeaderContextMenuActions.isEmpty()) {
|
||||||
action->setChecked(!isColumnHidden(column));
|
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…
Reference in New Issue
Block a user