Merge pull request #2281 from PhenomRetroShare/Add_RSTreeWidgetSortMenu

Add RsTreeWidget Sort Context Menu
This commit is contained in:
defnax 2021-02-17 19:44:41 +01:00 committed by GitHub
commit 5ad2342922
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 286 additions and 326 deletions

View File

@ -31,23 +31,21 @@
#include "util/QtVersion.h"
#include "util/DateTime.h"
#include <QDesktopWidget>
#include <QMenu>
#include <QToolButton>
#include <stdint.h>
Q_GUI_EXPORT int qt_defaultDpi();
#define ROLE_ID Qt::UserRole
#define ROLE_NAME Qt::UserRole + 1
#define ROLE_DESCRIPTION Qt::UserRole + 2
#define ROLE_POPULARITY Qt::UserRole + 3
#define ROLE_LASTPOST Qt::UserRole + 4
#define ROLE_POSTS Qt::UserRole + 5
#define ROLE_UNREAD Qt::UserRole + 6
#define ROLE_SEARCH_SCORE Qt::UserRole + 7
#define ROLE_SUBSCRIBE_FLAGS Qt::UserRole + 8
#define ROLE_COLOR Qt::UserRole + 9
#define ROLE_SAVED_ICON Qt::UserRole + 10
#define ROLE_SEARCH_STRING Qt::UserRole + 11
#define ROLE_REQUEST_ID Qt::UserRole + 12
#define ROLE_SUBSCRIBE_FLAGS Qt::UserRole + 3
#define ROLE_COLOR Qt::UserRole + 4
#define ROLE_REQUEST_ID Qt::UserRole + 5
#define ROLE_SORT Qt::UserRole + 6
#define FILTER_NAME_INDEX 0
#define FILTER_DESC_INDEX 1
@ -57,18 +55,6 @@ GroupTreeWidget::GroupTreeWidget(QWidget *parent) :
{
ui->setupUi(this);
displayMenu = NULL;
actionSortAscending = NULL;
// actionSortDescending = NULL;
actionSortByName = NULL;
actionSortByPopularity = NULL;
actionSortByLastPost = NULL;
actionSortByPosts = NULL;
actionSortByUnread = NULL;
compareRole = new RSTreeWidgetItemCompareRole;
compareRole->setRole(GTW_COLUMN_DATA, ROLE_NAME);
/* Connect signals */
connect(ui->filterLineEdit, SIGNAL(textChanged(QString)), this, SLOT(filterChanged()));
connect(ui->filterLineEdit, SIGNAL(filterChanged(int)), this, SLOT(filterChanged()));
@ -86,8 +72,18 @@ GroupTreeWidget::GroupTreeWidget(QWidget *parent) :
itemDelegate->setSpacing(QSize(0, 2));
ui->treeWidget->setItemDelegate(itemDelegate);
/* Set compare role for each column */
compareRole = new RSTreeWidgetItemCompareRole(QMap<int, QList<int>>({ {GTW_COLUMN_UNREAD, {ROLE_SORT}}
, {GTW_COLUMN_POSTS, {ROLE_SORT}}
, {GTW_COLUMN_POPULARITY, {ROLE_SORT}}
, {GTW_COLUMN_LAST_POST, {ROLE_SORT}}
, {GTW_COLUMN_SEARCH_SCORE,{ROLE_SORT}}
}));
/* Initialize tree widget */
ui->treeWidget->setColumnCount(GTW_COLUMN_COUNT);
ui->treeWidget->setSortingEnabled(true);
ui->treeWidget->header()->setSortIndicator(GTW_COLUMN_NAME,Qt::AscendingOrder);
ui->treeWidget->enableColumnCustomize(true);
ui->treeWidget->setColumnCustomizable(GTW_COLUMN_NAME, false);
@ -101,11 +97,20 @@ GroupTreeWidget::GroupTreeWidget(QWidget *parent) :
headerItem->setText(GTW_COLUMN_POSTS, "");
headerItem->setText(GTW_COLUMN_POPULARITY, "");
headerItem->setText(GTW_COLUMN_LAST_POST, "");
headerItem->setToolTip(GTW_COLUMN_NAME, tr("Group Name"));
headerItem->setText(GTW_COLUMN_SEARCH_SCORE, "");
headerItem->setText(GTW_COLUMN_DESCRIPTION, tr("Description"));
headerItem->setToolTip(GTW_COLUMN_NAME, tr("Name"));
headerItem->setToolTip(GTW_COLUMN_UNREAD, tr("Number of Unread message"));
headerItem->setToolTip(GTW_COLUMN_POSTS, tr("Friend's Posts"));
headerItem->setToolTip(GTW_COLUMN_POPULARITY, tr("Popularity"));
headerItem->setToolTip(GTW_COLUMN_LAST_POST, tr("Last Post"));
headerItem->setToolTip(GTW_COLUMN_SEARCH_SCORE, tr("Search Score"));
headerItem->setToolTip(GTW_COLUMN_DESCRIPTION, tr("Description"));
headerItem->setIcon(GTW_COLUMN_UNREAD, FilesDefs::getIconFromQtResourcePath(":/images/message-state-header.png"));
headerItem->setIcon(GTW_COLUMN_POSTS, FilesDefs::getIconFromQtResourcePath(":/images/filetype-association.png"));
headerItem->setIcon(GTW_COLUMN_POPULARITY, FilesDefs::getIconFromQtResourcePath(":/images/hot_5.png"));
headerItem->setIcon(GTW_COLUMN_LAST_POST, FilesDefs::getIconFromQtResourcePath(":/images/kalarm.png"));
headerItem->setIcon(GTW_COLUMN_SEARCH_SCORE, FilesDefs::getIconFromQtResourcePath(":/images/find.png"));
/* Set header resize modes and initial section sizes */
QHeaderView *header = ui->treeWidget->header ();
@ -122,6 +127,12 @@ GroupTreeWidget::GroupTreeWidget(QWidget *parent) :
QHeaderView_setSectionResizeModeColumn(header, GTW_COLUMN_LAST_POST, QHeaderView::Interactive);
header->resizeSection(GTW_COLUMN_LAST_POST, D+4) ;
header->setSectionHidden(GTW_COLUMN_LAST_POST, true);
QHeaderView_setSectionResizeModeColumn(header, GTW_COLUMN_SEARCH_SCORE, QHeaderView::Interactive);
header->resizeSection(GTW_COLUMN_SEARCH_SCORE, 3*W+4) ;
header->setSectionHidden(GTW_COLUMN_SEARCH_SCORE, true);
QHeaderView_setSectionResizeModeColumn(header, GTW_COLUMN_DESCRIPTION, QHeaderView::Interactive);
header->resizeSection(GTW_COLUMN_DESCRIPTION, 40*W+4) ;
header->setSectionHidden(GTW_COLUMN_DESCRIPTION, true);
/* add filter actions */
ui->filterLineEdit->addFilter(QIcon(), tr("Title"), FILTER_NAME_INDEX , tr("Search Title"));
@ -132,9 +143,6 @@ GroupTreeWidget::GroupTreeWidget(QWidget *parent) :
connect(ui->distantSearchLineEdit,SIGNAL(returnPressed()),this,SLOT(distantSearch())) ;
/* Initialize display button */
initDisplayMenu(ui->displayButton);
ui->treeWidget->setIconSize(QSize(S*1.8,S*1.8));
}
@ -166,9 +174,13 @@ void GroupTreeWidget::addToolButton(QToolButton *toolButton)
}
/* Initialize button */
int i = qt_defaultDpi();
auto desktopWidget = QApplication::desktop();
auto y = desktopWidget->logicalDpiY();
toolButton->setAutoRaise(true);
toolButton->setIconSize(ui->displayButton->iconSize());
toolButton->setFocusPolicy(ui->displayButton->focusPolicy());
toolButton->setIconSize(QSize(24*y/i,24*y/i));
toolButton->setFocusPolicy(Qt::NoFocus);
ui->toolBarFrame->layout()->addWidget(toolButton);
}
@ -180,113 +192,8 @@ void GroupTreeWidget::processSettings(bool load)
return;
}
const int SORTBY_NAME = 1;
const int SORTBY_POPULARITY = 2;
const int SORTBY_LASTPOST = 3;
const int SORTBY_POSTS = 4;
const int SORTBY_UNREAD = 5;
ui->treeWidget->setSettingsVersion(1);//Change it when modifing column properties
ui->treeWidget->setSettingsVersion(2);//Change it when modifing column properties
ui->treeWidget->processSettings(load);
if (load) {
// load Settings
// state of order
bool ascSort = Settings->value("GroupAscSort", true).toBool();
actionSortAscending->setChecked(ascSort);
actionSortDescending->setChecked(!ascSort);
// state of sort
int sortby = Settings->value("GroupSortBy").toInt();
switch (sortby) {
case SORTBY_NAME:
if (actionSortByName) {
actionSortByName->setChecked(true);
}
break;
case SORTBY_POPULARITY:
if (actionSortByPopularity) {
actionSortByPopularity->setChecked(true);
}
break;
case SORTBY_LASTPOST:
if (actionSortByLastPost) {
actionSortByLastPost->setChecked(true);
}
break;
case SORTBY_POSTS:
if (actionSortByPosts) {
actionSortByPosts->setChecked(true);
}
break;
case SORTBY_UNREAD:
if (actionSortByUnread) {
actionSortByUnread->setChecked(true);
}
break;
}
} else {
// save Settings
// state of order
Settings->setValue("GroupAscSort", !(actionSortDescending && actionSortDescending->isChecked())); //True by default
// state of sort
int sortby = SORTBY_NAME;
if (actionSortByName && actionSortByName->isChecked()) {
sortby = SORTBY_NAME;
} else if (actionSortByPopularity && actionSortByPopularity->isChecked()) {
sortby = SORTBY_POPULARITY;
} else if (actionSortByLastPost && actionSortByLastPost->isChecked()) {
sortby = SORTBY_LASTPOST;
} else if (actionSortByPosts && actionSortByPosts->isChecked()) {
sortby = SORTBY_POSTS;
} else if (actionSortByUnread && actionSortByUnread->isChecked()) {
sortby = SORTBY_UNREAD;
}
Settings->setValue("GroupSortBy", sortby);
}
}
void GroupTreeWidget::initDisplayMenu(QToolButton *toolButton)
{
displayMenu = new QMenu();
QActionGroup *actionGroupAsc = new QActionGroup(displayMenu);
actionSortDescending = displayMenu->addAction(FilesDefs::getIconFromQtResourcePath(":/images/sort_decrease.png"), tr("Sort Descending Order"), this, SLOT(sort()));
actionSortDescending->setCheckable(true);
actionSortDescending->setActionGroup(actionGroupAsc);
actionSortAscending = displayMenu->addAction(FilesDefs::getIconFromQtResourcePath(":/images/sort_incr.png"), tr("Sort Ascending Order"), this, SLOT(sort()));
actionSortAscending->setCheckable(true);
actionSortAscending->setActionGroup(actionGroupAsc);
displayMenu->addSeparator();
QActionGroup *actionGroup = new QActionGroup(displayMenu);
actionSortByName = displayMenu->addAction(QIcon(), tr("Sort by Name"), this, SLOT(sort()));
actionSortByName->setCheckable(true);
actionSortByName->setChecked(true); // set standard to sort by name
actionSortByName->setActionGroup(actionGroup);
actionSortByPopularity = displayMenu->addAction(QIcon(), tr("Sort by Popularity"), this, SLOT(sort()));
actionSortByPopularity->setCheckable(true);
actionSortByPopularity->setActionGroup(actionGroup);
actionSortByLastPost = displayMenu->addAction(QIcon(), tr("Sort by Last Post"), this, SLOT(sort()));
actionSortByLastPost->setCheckable(true);
actionSortByLastPost->setActionGroup(actionGroup);
actionSortByPosts = displayMenu->addAction(QIcon(), tr("Sort by Number of Friend's Posts"), this, SLOT(sort()));
actionSortByPosts->setCheckable(true);
actionSortByPosts->setActionGroup(actionGroup);
actionSortByUnread = displayMenu->addAction(QIcon(), tr("Sort by Unread"), this, SLOT(sort()));
actionSortByUnread->setCheckable(true);
actionSortByUnread->setActionGroup(actionGroup);
toolButton->setMenu(displayMenu);
}
void GroupTreeWidget::updateColors()
@ -337,10 +244,10 @@ void GroupTreeWidget::itemActivated(QTreeWidgetItem *item, int column)
emit treeItemActivated(id);
}
QTreeWidgetItem *GroupTreeWidget::addCategoryItem(const QString &name, const QIcon &icon, bool expand)
QTreeWidgetItem *GroupTreeWidget::addCategoryItem(const QString &name, const QIcon &icon, bool expand, int sortOrder /*= -1*/)
{
QFont font;
QTreeWidgetItem *item = new QTreeWidgetItem();
RSTreeWidgetItem *item = new RSTreeWidgetItem();
ui->treeWidget->addTopLevelItem(item);
// To get StyleSheet for Items
ui->treeWidget->style()->unpolish(ui->treeWidget);
@ -361,6 +268,12 @@ QTreeWidgetItem *GroupTreeWidget::addCategoryItem(const QString &name, const QIc
item->setExpanded(expand);
item->setNoOrder(true, ROLE_SORT);
if (sortOrder>=0)
item->setData(0, ROLE_SORT, sortOrder);
else
item->setData(0, ROLE_SORT, ui->treeWidget->topLevelItemCount());
return item;
}
@ -371,12 +284,11 @@ void GroupTreeWidget::removeSearchItem(QTreeWidgetItem *item)
QTreeWidgetItem *GroupTreeWidget::addSearchItem(const QString& search_string, uint32_t id, const QIcon& icon)
{
QTreeWidgetItem *item = addCategoryItem(search_string,icon,true);
QTreeWidgetItem *item = addCategoryItem(search_string,icon,true);
item->setData(GTW_COLUMN_DATA,ROLE_SEARCH_STRING,search_string) ;
item->setData(GTW_COLUMN_DATA,ROLE_REQUEST_ID ,id) ;
item->setData(GTW_COLUMN_DATA,ROLE_REQUEST_ID ,id) ;
return item;
return item;
}
void GroupTreeWidget::setDistSearchVisible(bool visible)
@ -473,11 +385,13 @@ void GroupTreeWidget::fillGroupItems(QTreeWidgetItem *categoryItem, const QList<
if (item == NULL) {
item = new RSTreeWidgetItem(compareRole);
item->setData(GTW_COLUMN_DATA, ROLE_ID, itemInfo.id);
//static_cast<RSTreeWidgetItem*>(item)->setNoDataAsLast(true); //Uncomment this to sort data with QVariant() always at end.
categoryItem->addChild(item);
}
item->setText(GTW_COLUMN_NAME, itemInfo.name);
item->setData(GTW_COLUMN_DATA, ROLE_NAME, itemInfo.name);
item->setText(GTW_COLUMN_DESCRIPTION, itemInfo.description);
item->setData(GTW_COLUMN_DATA, ROLE_DESCRIPTION, itemInfo.description);
// Add children for context strings. This happens in the search.
@ -492,17 +406,21 @@ void GroupTreeWidget::fillGroupItems(QTreeWidgetItem *categoryItem, const QList<
}
/* Set last post */
qlonglong lastPost = itemInfo.lastpost.toTime_t();
item->setData(GTW_COLUMN_DATA, ROLE_LASTPOST, -lastPost); // negative for correct sorting
if(itemInfo.lastpost == QDateTime::fromTime_t(0))
{
item->setText(GTW_COLUMN_LAST_POST, tr("Never"));
item->setData(GTW_COLUMN_LAST_POST, ROLE_SORT, QVariant());// To allow them not be sorted with ->setNoDataAsLast(true)
}
else
{
item->setText(GTW_COLUMN_LAST_POST, itemInfo.lastpost.toString(Qt::ISODate).replace("T"," "));
item->setData(GTW_COLUMN_LAST_POST, ROLE_SORT, itemInfo.lastpost.toTime_t());
}
/* Set visible posts */
item->setText(GTW_COLUMN_POSTS, QString::number(itemInfo.max_visible_posts));
item->setData(GTW_COLUMN_DATA, ROLE_POSTS, -itemInfo.max_visible_posts);// negative for correct sorting
item->setData(GTW_COLUMN_POSTS, ROLE_SORT, itemInfo.max_visible_posts);
/* Set icon */
item->setIcon(GTW_COLUMN_NAME, itemInfo.icon);
@ -511,7 +429,7 @@ void GroupTreeWidget::fillGroupItems(QTreeWidgetItem *categoryItem, const QList<
QString tooltip = PopularityDefs::tooltip(itemInfo.popularity);
item->setIcon(GTW_COLUMN_POPULARITY, PopularityDefs::icon(itemInfo.popularity));
item->setData(GTW_COLUMN_DATA, ROLE_POPULARITY, -itemInfo.popularity); // negative for correct sorting
item->setData(GTW_COLUMN_POPULARITY, ROLE_SORT, itemInfo.popularity);
/* Set tooltip */
if (itemInfo.adminKey)
@ -580,8 +498,6 @@ void GroupTreeWidget::fillGroupItems(QTreeWidgetItem *categoryItem, const QList<
++child;
}
}
resort(categoryItem);
}
void GroupTreeWidget::setUnreadCount(QTreeWidgetItem *item, int unreadCount)
@ -593,13 +509,13 @@ void GroupTreeWidget::setUnreadCount(QTreeWidgetItem *item, int unreadCount)
QFont font = item->font(GTW_COLUMN_NAME);
if (unreadCount) {
item->setData(GTW_COLUMN_DATA, ROLE_UNREAD, unreadCount);
item->setText(GTW_COLUMN_UNREAD, QString::number(unreadCount));
font.setBold(true);
} else {
item->setText(GTW_COLUMN_UNREAD, "");
font.setBold(false);
}
item->setData(GTW_COLUMN_UNREAD, ROLE_SORT, unreadCount);
item->setFont(GTW_COLUMN_NAME, font);
}
@ -707,7 +623,8 @@ void GroupTreeWidget::calculateScore(QTreeWidgetItem *item, const QString &filte
}
}
item->setData(GTW_COLUMN_DATA, ROLE_SEARCH_SCORE, -score); // negative for correct sorting
item->setText(GTW_COLUMN_SEARCH_SCORE, QString::number(score));
item->setData(GTW_COLUMN_SEARCH_SCORE, ROLE_SORT, score);
return;
}
@ -730,47 +647,17 @@ void GroupTreeWidget::filterChanged()
{
/* Recalculate score */
calculateScore(NULL, ui->filterLineEdit->text());
resort(NULL);
}
void GroupTreeWidget::resort(QTreeWidgetItem *categoryItem)
{
Qt::SortOrder order = (actionSortAscending == NULL || actionSortAscending->isChecked()) ? Qt::AscendingOrder : Qt::DescendingOrder;
if (ui->filterLineEdit->text().isEmpty() == false) {
compareRole->setRole(GTW_COLUMN_DATA, ROLE_SEARCH_SCORE);
compareRole->addRole(GTW_COLUMN_DATA, ROLE_LASTPOST);
} else if (actionSortByName && actionSortByName->isChecked()) {
compareRole->setRole(GTW_COLUMN_DATA, ROLE_NAME);
} else if (actionSortByPopularity && actionSortByPopularity->isChecked()) {
compareRole->setRole(GTW_COLUMN_DATA, ROLE_POPULARITY);
} else if (actionSortByLastPost && actionSortByLastPost->isChecked()) {
compareRole->setRole(GTW_COLUMN_DATA, ROLE_LASTPOST);
} else if (actionSortByPosts && actionSortByPosts->isChecked()) {
compareRole->setRole(GTW_COLUMN_DATA, ROLE_POSTS);
} else if (actionSortByUnread && actionSortByUnread->isChecked()) {
compareRole->setRole(GTW_COLUMN_DATA, ROLE_UNREAD);
}
if (categoryItem) {
categoryItem->sortChildren(GTW_COLUMN_DATA, order);
} else {
int count = ui->treeWidget->topLevelItemCount();
for (int child = 0; child < count; ++child) {
ui->treeWidget->topLevelItem(child)->sortChildren(GTW_COLUMN_DATA, order);
}
}
}
void GroupTreeWidget::distantSearch()
{
emit distantSearchRequested(ui->distantSearchLineEdit->text());
emit distantSearchRequested(ui->distantSearchLineEdit->text());
ui->distantSearchLineEdit->clear();
ui->distantSearchLineEdit->clear();
}
void GroupTreeWidget::sort()
{
resort(NULL);
ui->treeWidget->resort();
}

View File

@ -36,13 +36,15 @@ class RSTreeWidget;
#define GROUPTREEWIDGET_COLOR_PRIVATEKEY 1
#define GROUPTREEWIDGET_COLOR_COUNT 2
#define GTW_COLUMN_NAME 0
#define GTW_COLUMN_UNREAD 1
#define GTW_COLUMN_POSTS 2
#define GTW_COLUMN_POPULARITY 3
#define GTW_COLUMN_LAST_POST 4
#define GTW_COLUMN_COUNT 5
#define GTW_COLUMN_DATA GTW_COLUMN_NAME
#define GTW_COLUMN_NAME 0
#define GTW_COLUMN_UNREAD 1
#define GTW_COLUMN_POSTS 2
#define GTW_COLUMN_POPULARITY 3
#define GTW_COLUMN_LAST_POST 4
#define GTW_COLUMN_SEARCH_SCORE 5
#define GTW_COLUMN_DESCRIPTION 6
#define GTW_COLUMN_COUNT 7
#define GTW_COLUMN_DATA GTW_COLUMN_NAME
namespace Ui {
class GroupTreeWidget;
@ -88,8 +90,16 @@ public:
// Load and save settings (group must be started from the caller)
void processSettings(bool load);
// Add a new category item
QTreeWidgetItem *addCategoryItem(const QString &name, const QIcon &icon, bool expand);
///
/// \brief addCategoryItem: Add a new category item
/// \param name: Name shown on item
/// \param icon: Icon used for item
/// \param expand: If it is expanded by default
/// \param sortOrder: To asc sort them in tree
/// \return
///
QTreeWidgetItem *addCategoryItem(const QString &name, const QIcon &icon, bool expand, int sortOrder = -1);
// Add a new search item
void setDistSearchVisible(bool) ; // shows/hides distant search UI parts.
QTreeWidgetItem *addSearchItem(const QString& search_string, uint32_t id, const QIcon &icon) ;
@ -143,27 +153,17 @@ private slots:
void sort();
private:
// Initialize the display menu for sorting
void initDisplayMenu(QToolButton *toolButton);
void calculateScore(QTreeWidgetItem *item, const QString &filterText);
void resort(QTreeWidgetItem *categoryItem);
void updateColors();
private:
QMenu *displayMenu;
QAction *actionSortAscending;
QAction *actionSortDescending;
QAction *actionSortByName;
QAction *actionSortByPopularity;
QAction *actionSortByLastPost;
QAction *actionSortByPosts;
QAction *actionSortByUnread;
RSTreeWidgetItemCompareRole *compareRole;
/* Color definitions (for standard see qss.default) */
QColor mTextColor[GROUPTREEWIDGET_COLOR_COUNT];
// Compare role used for each column
RSTreeWidgetItemCompareRole *compareRole;
Ui::GroupTreeWidget *ui;
};

View File

@ -56,32 +56,6 @@
<item>
<widget class="LineEditClear" name="filterLineEdit"/>
</item>
<item>
<widget class="QToolButton" name="displayButton">
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
<property name="toolTip">
<string>Display</string>
</property>
<property name="icon">
<iconset resource="../icons.qrc">
<normaloff>:/icons/svg/display_options.svg</normaloff>:/icons/svg/display_options.svg</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<property name="popupMode">
<enum>QToolButton::InstantPopup</enum>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
@ -111,12 +85,6 @@
<property name="allColumnsShowFocus">
<bool>true</bool>
</property>
<attribute name="headerVisible">
<bool>false</bool>
</attribute>
<attribute name="headerStretchLastSection">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string notr="true">1</string>
@ -145,8 +113,6 @@
<header>gui/common/RSTreeWidget.h</header>
</customwidget>
</customwidgets>
<resources>
<include location="../icons.qrc"/>
</resources>
<resources/>
<connections/>
</ui>

View File

@ -206,11 +206,13 @@ void RSTreeWidget::processSettings(bool load)
// Compare version, because Qt can crash in restoreState after column changes
header()->restoreState(Settings->value(objectName()).toByteArray());
}
header()->setHidden(Settings->value(objectName()+"HiddenHeader", false).toBool());
} else {
// Save settings
// state of tree widget
Settings->setValue(objectName(), header()->saveState());
Settings->setValue(objectName()+"HiddenHeader", header()->isHidden());
// Save version
if (mSettingsVersion) {
@ -283,13 +285,57 @@ QMenu *RSTreeWidget::createStandardContextMenu(QMenu *contextMenu)
actShowHeader->setCheckable(true);
actShowHeader->setChecked(!isHeaderHidden());
QMenu *headerMenu = contextMenu->addMenu(QIcon(),tr("Show column..."));
QTreeWidgetItem *item = headerItem();
int columnCount = item->columnCount();
if (isSortingEnabled() && isHeaderHidden())
{
QMenu *headerMenuSort = contextMenu->addMenu(QIcon(),tr("Sort by column …"));
QActionGroup *actionGroupAsc = new QActionGroup(headerMenuSort);
QAction *actionSortDescending = headerMenuSort->addAction(FilesDefs::getIconFromQtResourcePath(":/images/sort_decrease.png")
, tr("Sort Descending Order"), this, SLOT(changeSortOrder()));
actionSortDescending->setData("SortDesc");
actionSortDescending->setCheckable(true);
actionSortDescending->setChecked(header()->sortIndicatorOrder()==Qt::DescendingOrder);
actionSortDescending->setActionGroup(actionGroupAsc);
QAction *actionSortAscending = headerMenuSort->addAction(FilesDefs::getIconFromQtResourcePath(":/images/sort_incr.png")
, tr("Sort Ascending Order"), this, SLOT(changeSortOrder()));
actionSortAscending->setData("SortAsc");
actionSortAscending->setCheckable(true);
actionSortAscending->setChecked(header()->sortIndicatorOrder()==Qt::AscendingOrder);
actionSortAscending->setActionGroup(actionGroupAsc);
headerMenuSort->addSeparator();
QActionGroup *actionGroupSort = new QActionGroup(headerMenuSort);
for (int column = 0; column < columnCount; ++column)
{
QString txt = item->text(column) ;
if(txt == "")
txt = item->data(column,Qt::UserRole).toString() ;
if(txt == "")
txt = item->data(column,Qt::ToolTipRole).toString() ;
if(txt=="")
txt = QString::number(column) + tr(" [no title]") ;
QAction *action = headerMenuSort->addAction(QIcon(), txt, this, SLOT(changeSortColumn()));
action->setData(column);
action->setCheckable(true);
action->setChecked(header()->sortIndicatorSection() == column);
action->setActionGroup(actionGroupSort);
}
}
QMenu *headerMenuShowCol = contextMenu->addMenu(QIcon(),tr("Show column …"));
for (int column = 0; column < columnCount; ++column)
{
QMap<int, bool>::const_iterator it = mColumnCustomizable.find(column);
QMap<int, bool>::iterator it = mColumnCustomizable.find(column);
if (it != mColumnCustomizable.end() && *it == false) {
continue;
}
@ -300,9 +346,9 @@ QMenu *RSTreeWidget::createStandardContextMenu(QMenu *contextMenu)
txt = item->data(column,Qt::ToolTipRole).toString() ;
if(txt=="")
txt = tr("[no title]") ;
txt = QString::number(column) + tr(" [no title]") ;
QAction *action = headerMenu->addAction(QIcon(), txt, this, SLOT(columnVisible()));
QAction *action = headerMenuShowCol->addAction(QIcon(), txt, this, SLOT(columnVisible()));
action->setCheckable(true);
action->setData(column);
action->setChecked(!isColumnHidden(column));
@ -382,3 +428,32 @@ void RSTreeWidget::resort()
sortItems(header()->sortIndicatorSection(), header()->sortIndicatorOrder());
}
}
void RSTreeWidget::changeSortColumn()
{
QAction *action = dynamic_cast<QAction*>(sender());
if (!action) {
return;
}
if (action->data().canConvert<int>())
{
header()->setSortIndicator(action->data().toInt(),header()->sortIndicatorOrder());
}
}
void RSTreeWidget::changeSortOrder()
{
QAction *action = dynamic_cast<QAction*>(sender());
if (!action) {
return;
}
if (action->data().canConvert<QString>())
{
if (action->data().toString() == "SortDesc")
header()->setSortIndicator(header()->sortIndicatorSection(),Qt::DescendingOrder);
else
header()->setSortIndicator(header()->sortIndicatorSection(),Qt::AscendingOrder);
}
}

View File

@ -69,6 +69,8 @@ private slots:
void headerContextMenuRequested(const QPoint &pos);
void headerVisible();
void columnVisible();
void changeSortColumn();
void changeSortOrder();
protected:
void paintEvent(QPaintEvent *event);

View File

@ -20,18 +20,26 @@
#include "RSTreeWidgetItem.h"
#include <QHeaderView>
RSTreeWidgetItemCompareRole::RSTreeWidgetItemCompareRole()
{
}
void RSTreeWidgetItemCompareRole::setRole(int column, int role)
RSTreeWidgetItemCompareRole::RSTreeWidgetItemCompareRole(QMap<int, QList<int>> map)
: QMap<int, QList<int>>(map)
{
}
void RSTreeWidgetItemCompareRole::setRole(const int column, const int role)
{
QList<int> roles;
roles.push_back(role);
insert(column, roles);
}
void RSTreeWidgetItemCompareRole::addRole(int column, int role)
void RSTreeWidgetItemCompareRole::addRole(const int column, const int role)
{
RSTreeWidgetItemCompareRole::iterator it = find(column);
if (it == end()) {
@ -54,49 +62,51 @@ void RSTreeWidgetItemCompareRole::findRoles(const int column, QList<int> &roles)
roles = it.value();
}
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, int type) : QTreeWidgetItem(type)
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, int type)
: QTreeWidgetItem(type), m_compareRole(compareRole), m_noOrder(false), m_useRoleWhenNoOrder(Qt::UserRole), m_noDataAsLast(false)
{
m_compareRole = compareRole;
}
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, const QStringList &strings, int type) : QTreeWidgetItem(strings, type)
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, const QStringList &strings, int type)
: QTreeWidgetItem(strings, type), m_compareRole(compareRole), m_noOrder(false), m_useRoleWhenNoOrder(Qt::UserRole), m_noDataAsLast(false)
{
m_compareRole = compareRole;
}
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidget *view, int type) : QTreeWidgetItem(view, type)
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidget *view, int type)
: QTreeWidgetItem(view, type), m_compareRole(compareRole), m_noOrder(false), m_useRoleWhenNoOrder(Qt::UserRole), m_noDataAsLast(false)
{
m_compareRole = compareRole;
}
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidget *view, const QStringList &strings, int type) : QTreeWidgetItem(view, strings, type)
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidget *view, const QStringList &strings, int type)
: QTreeWidgetItem(view, strings, type), m_compareRole(compareRole), m_noOrder(false), m_useRoleWhenNoOrder(Qt::UserRole), m_noDataAsLast(false)
{
m_compareRole = compareRole;
}
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidget *view, QTreeWidgetItem *after, int type) : QTreeWidgetItem(view, after, type)
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidget *view, QTreeWidgetItem *after, int type)
: QTreeWidgetItem(view, after, type), m_compareRole(compareRole), m_noOrder(false), m_useRoleWhenNoOrder(Qt::UserRole), m_noDataAsLast(false)
{
m_compareRole = compareRole;
}
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidgetItem *parent, int type) : QTreeWidgetItem(parent, type)
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidgetItem *parent, int type)
: QTreeWidgetItem(parent, type), m_compareRole(compareRole), m_noOrder(false), m_useRoleWhenNoOrder(Qt::UserRole), m_noDataAsLast(false)
{
m_compareRole = compareRole;
}
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidgetItem *parent, const QStringList &strings, int type) : QTreeWidgetItem(parent, strings, type)
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidgetItem *parent, const QStringList &strings, int type)
: QTreeWidgetItem(parent, strings, type), m_compareRole(compareRole), m_noOrder(false), m_useRoleWhenNoOrder(Qt::UserRole), m_noDataAsLast(false)
{
m_compareRole = compareRole;
}
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidgetItem *parent, QTreeWidgetItem *after, int type) : QTreeWidgetItem(parent, after, type)
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidgetItem *parent, QTreeWidgetItem *after, int type)
: QTreeWidgetItem(parent, after, type), m_compareRole(compareRole), m_noOrder(false), m_useRoleWhenNoOrder(Qt::UserRole), m_noDataAsLast(false)
{
m_compareRole = compareRole;
}
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, const QTreeWidgetItem &other) : QTreeWidgetItem(other)
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, const QTreeWidgetItem &other)
: QTreeWidgetItem(other), m_compareRole(compareRole), m_noOrder(false), m_useRoleWhenNoOrder(Qt::UserRole), m_noDataAsLast(false)
{
m_compareRole = compareRole;
}
static uint typeOfVariant(const QVariant &value)
@ -125,56 +135,57 @@ static uint typeOfVariant(const QVariant &value)
bool RSTreeWidgetItem::operator<(const QTreeWidgetItem &other) const
{
int column = treeWidget()->sortColumn();
QList<int> roles;
Qt::SortOrder sortOrder = treeWidget() ? treeWidget()->header()->sortIndicatorOrder() : Qt::AscendingOrder;
if (m_noOrder)
{
const QVariant v1 = data(0, m_useRoleWhenNoOrder);
const QVariant v2 = other.data(0, m_useRoleWhenNoOrder);
if (sortOrder == Qt::AscendingOrder)
return v1.toLongLong() < v2.toLongLong();
else
return v1.toLongLong() >= v2.toLongLong();
}
/* Own role for sort defined ? */
if (m_compareRole) {
m_compareRole->findRoles(column, roles);
}
int column = treeWidget() ? treeWidget()->header()->sortIndicatorSection() : 0;
QList<int>::iterator role;
for (role = roles.begin(); role != roles.end(); ++role) {
// taken from "bool QTreeWidgetItem::operator<(const QTreeWidgetItem &other) const"
const QVariant v1 = data(column, *role);
const QVariant v2 = other.data(column, *role);
QList<int> roles;
/* Own role for sort defined ? */
if (m_compareRole) {
m_compareRole->findRoles(column, roles);
}
// taken from "bool QAbstractItemModelPrivate::variantLessThan(const QVariant &v1, const QVariant &v2)"
switch(qMax(typeOfVariant(v1), typeOfVariant(v2)))
{
case 0: //integer type
{
qlonglong value1 = v1.toLongLong();
qlonglong value2 = v2.toLongLong();
for (auto& role : roles)
{
// taken from "bool QTreeWidgetItem::operator<(const QTreeWidgetItem &other) const"
const QVariant v1 = data(column, role);
const QVariant v2 = other.data(column, role);
if (m_noDataAsLast && !v1.isValid() && v2.isValid())
return sortOrder != Qt::AscendingOrder;
if (m_noDataAsLast && v1.isValid() && !v2.isValid())
return sortOrder == Qt::AscendingOrder;
if (value1 != value2) {
return value1 < value2;
}
}
break;
case 1: //floating point
{
double value1 = v1.toDouble();
double value2 = v2.toDouble();
// taken from "bool QAbstractItemModelPrivate::variantLessThan(const QVariant &v1, const QVariant &v2)"
// but using multi roles to sort
switch(qMax(typeOfVariant(v1), typeOfVariant(v2)))
{
case 0: //integer type
if (v1.toLongLong() != v2.toLongLong())
return v1.toLongLong() < v2.toLongLong();
//Fall through
case 1: //floating point
if (v1.toReal() != v2.toReal())
return v1.toReal() < v2.toReal();
//Fall through
default:
if (v1.toString().localeAwareCompare(v2.toString()) != 0)//Need Qt::CaseInsensitive ?
return v1.toString().localeAwareCompare(v2.toString()) < 0;
}
}
// If all roles are equals for this column, compare as string
if (value1 != value2) {
return value1 < value2;
}
}
break;
default:
{
int compare = v1.toString().compare (v2.toString(), Qt::CaseInsensitive);
if (compare) {
return (compare < 0);
}
}
}
}
/* Compare DisplayRole */
const QVariant v1 = data(column, Qt::DisplayRole);
const QVariant v2 = other.data(column, Qt::DisplayRole);
/* Compare DisplayRole */
const QVariant v1 = data(column, Qt::DisplayRole);
const QVariant v2 = other.data(column, Qt::DisplayRole);
return (v1.toString().compare (v2.toString(), Qt::CaseInsensitive) < 0);
return (v1.toString().compare (v2.toString(), Qt::CaseInsensitive) < 0);
}

View File

@ -29,30 +29,49 @@
class RSTreeWidgetItemCompareRole : QMap<int, QList<int> >
{
public:
RSTreeWidgetItemCompareRole();
RSTreeWidgetItemCompareRole();
explicit RSTreeWidgetItemCompareRole(QMap<int, QList<int>> map);
void setRole(int column, int role);
void addRole(int column, int role);
void findRoles(const int column, QList<int> &roles) const;
void setRole(const int column, const int role);
void addRole(const int column, const int role);
void findRoles(const int column, QList<int> &roles) const;
};
class RSTreeWidgetItem : public QTreeWidgetItem
{
public:
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole = NULL, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, const QStringList &strings, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidget *view, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidget *view, const QStringList &strings, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidget *view, QTreeWidgetItem *after, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidgetItem *parent, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidgetItem *parent, const QStringList &strings, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidgetItem *parent, QTreeWidgetItem *after, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, const QTreeWidgetItem &other);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole = NULL, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, const QStringList &strings, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidget *view, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidget *view, const QStringList &strings, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidget *view, QTreeWidgetItem *after, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidgetItem *parent, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidgetItem *parent, const QStringList &strings, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidgetItem *parent, QTreeWidgetItem *after, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, const QTreeWidgetItem &other);
bool operator<(const QTreeWidgetItem &other) const;
bool operator<(const QTreeWidgetItem &other) const;
bool noOrder() const {return m_noOrder;}
///
/// \brief setNoOrder: Disabled normal sort for this item. It use column 0 and role defined to sort in ascending in qlonglong format.
/// \param value: True disable normal sort but use defined role.
/// \param useRole: The role to use to ascending sort in first(0) column.
///
void setNoOrder(const bool value, const int useRole = Qt::UserRole) {m_noOrder = value; m_useRoleWhenNoOrder = useRole;}
bool noDataAsLast() const {return m_noDataAsLast;}
///
/// \brief setNoDataAsLast: Item without data will be sort as last. Need a RSTreeWidgetItemCompareRole to get it activated.
/// \param value: True to enable this role.
///
void setNoDataAsLast(const bool value) {m_noDataAsLast = value;}
private:
const RSTreeWidgetItemCompareRole *m_compareRole;
const RSTreeWidgetItemCompareRole *m_compareRole;
bool m_noOrder;
int m_useRoleWhenNoOrder;
bool m_noDataAsLast;
};
#endif

View File

@ -1019,7 +1019,7 @@ void GxsGroupFrameDialog::insertGroupsData(const std::list<RsGxsGenericGroupData
ui->groupTreeWidget->fillGroupItems(mYourGroups, adminList);
mYourGroups->setText(GTW_COLUMN_POPULARITY, QString::number(mYourGroups->childCount()));
ui->groupTreeWidget->fillGroupItems(mSubscribedGroups, subList);
mSubscribedGroups->setText(GTW_COLUMN_POPULARITY, QString::number(mSubscribedGroups->childCount())); // 1 COLUMN_UNREAD 2 COLUMN_POPULARITY
mSubscribedGroups->setText(GTW_COLUMN_POPULARITY, QString::number(mSubscribedGroups->childCount()));
ui->groupTreeWidget->fillGroupItems(mPopularGroups, popList);
mPopularGroups->setText(GTW_COLUMN_POPULARITY, QString::number(mPopularGroups->childCount()));
ui->groupTreeWidget->fillGroupItems(mOtherGroups, otherList);

View File

@ -329,7 +329,7 @@ GxsForumThreadWidget::GxsForumThreadWidget(const RsGxsGroupId &forumId, QWidget
ttheader->resizeSection (RsGxsForumModel::COLUMN_THREAD_AUTHOR, 150*f);
ui->threadTreeWidget->resizeColumnToContents(RsGxsForumModel::COLUMN_THREAD_DISTRIBUTION);
ui->threadTreeWidget->resizeColumnToContents(RsGxsForumModel::COLUMN_THREAD_READ);
//ui->threadTreeWidget->resizeColumnToContents(RsGxsForumModel::COLUMN_THREAD_READ);
QHeaderView_setSectionResizeModeColumn(ttheader, RsGxsForumModel::COLUMN_THREAD_TITLE, QHeaderView::Interactive);
QHeaderView_setSectionResizeModeColumn(ttheader, RsGxsForumModel::COLUMN_THREAD_DATE, QHeaderView::Interactive);