Add Unread column to GroupTreeWidget

This commit is contained in:
Phenom 2016-12-10 17:59:58 +01:00
parent 242338d10c
commit 8f452eefb1
2 changed files with 59 additions and 26 deletions

View File

@ -22,6 +22,7 @@
#include "ui_GroupTreeWidget.h" #include "ui_GroupTreeWidget.h"
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QHeaderView>
#include <QLabel> #include <QLabel>
#include <QMenu> #include <QMenu>
#include <QMovie> #include <QMovie>
@ -39,8 +40,9 @@
#include <stdint.h> #include <stdint.h>
#define COLUMN_NAME 0 #define COLUMN_NAME 0
#define COLUMN_POPULARITY 1 #define COLUMN_UNREAD 1
#define COLUMN_COUNT 2 #define COLUMN_POPULARITY 2
#define COLUMN_COUNT 3
#define COLUMN_DATA COLUMN_NAME #define COLUMN_DATA COLUMN_NAME
#define ROLE_ID Qt::UserRole #define ROLE_ID Qt::UserRole
@ -49,10 +51,11 @@
#define ROLE_POPULARITY Qt::UserRole + 3 #define ROLE_POPULARITY Qt::UserRole + 3
#define ROLE_LASTPOST Qt::UserRole + 4 #define ROLE_LASTPOST Qt::UserRole + 4
#define ROLE_POSTS Qt::UserRole + 5 #define ROLE_POSTS Qt::UserRole + 5
#define ROLE_SEARCH_SCORE Qt::UserRole + 6 #define ROLE_UNREAD Qt::UserRole + 6
#define ROLE_SUBSCRIBE_FLAGS Qt::UserRole + 7 #define ROLE_SEARCH_SCORE Qt::UserRole + 7
#define ROLE_COLOR Qt::UserRole + 8 #define ROLE_SUBSCRIBE_FLAGS Qt::UserRole + 8
#define ROLE_SAVED_ICON Qt::UserRole + 9 #define ROLE_COLOR Qt::UserRole + 9
#define ROLE_SAVED_ICON Qt::UserRole + 10
#define FILTER_NAME_INDEX 0 #define FILTER_NAME_INDEX 0
#define FILTER_DESC_INDEX 1 #define FILTER_DESC_INDEX 1
@ -72,6 +75,7 @@ GroupTreeWidget::GroupTreeWidget(QWidget *parent) :
actionSortByPopularity = NULL; actionSortByPopularity = NULL;
actionSortByLastPost = NULL; actionSortByLastPost = NULL;
actionSortByPosts = NULL; actionSortByPosts = NULL;
actionSortByUnread = NULL;
compareRole = new RSTreeWidgetItemCompareRole; compareRole = new RSTreeWidgetItemCompareRole;
compareRole->setRole(COLUMN_DATA, ROLE_NAME); compareRole->setRole(COLUMN_DATA, ROLE_NAME);
@ -96,14 +100,18 @@ GroupTreeWidget::GroupTreeWidget(QWidget *parent) :
/* Initialize tree widget */ /* Initialize tree widget */
ui->treeWidget->setColumnCount(COLUMN_COUNT); ui->treeWidget->setColumnCount(COLUMN_COUNT);
int S = QFontMetricsF(font()).height(); int S = QFontMetricsF(font()).height() ;
int W = QFontMetricsF(font()).width("999") ;
/* Set header resize modes and initial section sizes */ /* Set header resize modes and initial section sizes */
QHeaderView *header = ui->treeWidget->header (); QHeaderView *header = ui->treeWidget->header ();
header->setStretchLastSection(false);
QHeaderView_setSectionResizeModeColumn(header, COLUMN_NAME, QHeaderView::Stretch); QHeaderView_setSectionResizeModeColumn(header, COLUMN_NAME, QHeaderView::Stretch);
header->resizeSection(COLUMN_NAME, 10*S); header->resizeSection(COLUMN_NAME, 10*S) ;
QHeaderView_setSectionResizeModeColumn(header, COLUMN_UNREAD, QHeaderView::Fixed);
header->resizeSection(COLUMN_UNREAD, W+4) ;
QHeaderView_setSectionResizeModeColumn(header, COLUMN_POPULARITY, QHeaderView::Fixed); QHeaderView_setSectionResizeModeColumn(header, COLUMN_POPULARITY, QHeaderView::Fixed);
header->resizeSection(COLUMN_POPULARITY, 2*S); header->resizeSection(COLUMN_POPULARITY, 2*S) ;
/* add filter actions */ /* add filter actions */
ui->filterLineEdit->addFilter(QIcon(), tr("Title"), FILTER_NAME_INDEX , tr("Search Title")); ui->filterLineEdit->addFilter(QIcon(), tr("Title"), FILTER_NAME_INDEX , tr("Search Title"));
@ -113,7 +121,7 @@ GroupTreeWidget::GroupTreeWidget(QWidget *parent) :
/* Initialize display button */ /* Initialize display button */
initDisplayMenu(ui->displayButton); initDisplayMenu(ui->displayButton);
ui->treeWidget->setIconSize(QSize(S*1.6,S*1.6)) ; ui->treeWidget->setIconSize(QSize(S*1.6,S*1.6));
} }
GroupTreeWidget::~GroupTreeWidget() GroupTreeWidget::~GroupTreeWidget()
@ -193,10 +201,16 @@ void GroupTreeWidget::processSettings(RshareSettings *settings, bool load)
const int SORTBY_POPULRITY = 2; const int SORTBY_POPULRITY = 2;
const int SORTBY_LASTPOST = 3; const int SORTBY_LASTPOST = 3;
const int SORTBY_POSTS = 4; const int SORTBY_POSTS = 4;
const int SORTBY_UNREAD = 5;
if (load) { if (load) {
// load settings // load settings
// state of order
bool ascSort = settings->value("GroupAscSort", true).toBool();
actionSortAscending->setChecked(ascSort);
actionSortDescending->setChecked(!ascSort);
// state of sort // state of sort
int sortby = settings->value("GroupSortBy").toInt(); int sortby = settings->value("GroupSortBy").toInt();
switch (sortby) { switch (sortby) {
@ -220,10 +234,18 @@ void GroupTreeWidget::processSettings(RshareSettings *settings, bool load)
actionSortByPosts->setChecked(true); actionSortByPosts->setChecked(true);
} }
break; break;
case SORTBY_UNREAD:
if (actionSortByUnread) {
actionSortByUnread->setChecked(true);
}
break;
} }
} else { } else {
// save settings // save settings
// state of order
settings->setValue("GroupAscSort", !(actionSortDescending && actionSortDescending->isChecked())); //True by default
// state of sort // state of sort
int sortby = SORTBY_NAME; int sortby = SORTBY_NAME;
if (actionSortByName && actionSortByName->isChecked()) { if (actionSortByName && actionSortByName->isChecked()) {
@ -232,6 +254,10 @@ void GroupTreeWidget::processSettings(RshareSettings *settings, bool load)
sortby = SORTBY_POPULRITY; sortby = SORTBY_POPULRITY;
} else if (actionSortByLastPost && actionSortByLastPost->isChecked()) { } else if (actionSortByLastPost && actionSortByLastPost->isChecked()) {
sortby = SORTBY_LASTPOST; sortby = SORTBY_LASTPOST;
} else if (actionSortByPosts && actionSortByPosts->isChecked()) {
sortby = SORTBY_POSTS;
} else if (actionSortByUnread && actionSortByUnread->isChecked()) {
sortby = SORTBY_UNREAD;
} }
settings->setValue("GroupSortBy", sortby); settings->setValue("GroupSortBy", sortby);
} }
@ -240,18 +266,17 @@ void GroupTreeWidget::processSettings(RshareSettings *settings, bool load)
void GroupTreeWidget::initDisplayMenu(QToolButton *toolButton) void GroupTreeWidget::initDisplayMenu(QToolButton *toolButton)
{ {
displayMenu = new QMenu(); displayMenu = new QMenu();
// QActionGroup *actionGroup = new QActionGroup(displayMenu); QActionGroup *actionGroupAsc = new QActionGroup(displayMenu);
//
// actionSortDescending = displayMenu->addAction(QIcon(":/images/sort_decrease.png"), tr("Sort Descending Order"), this, SLOT(sort())); actionSortDescending = displayMenu->addAction(QIcon(":/images/sort_decrease.png"), tr("Sort Descending Order"), this, SLOT(sort()));
// actionSortDescending->setCheckable(true); actionSortDescending->setCheckable(true);
// actionSortDescending->setActionGroup(actionGroup); actionSortDescending->setActionGroup(actionGroupAsc);
//
// actionSortAscending = displayMenu->addAction(QIcon(":/images/sort_incr.png"), tr("Sort Ascending Order"), this, SLOT(sort())); actionSortAscending = displayMenu->addAction(QIcon(":/images/sort_incr.png"), tr("Sort Ascending Order"), this, SLOT(sort()));
// actionSortAscending->setCheckable(true); actionSortAscending->setCheckable(true);
// actionSortAscending->setChecked(true); // set standard to sort ascending actionSortAscending->setActionGroup(actionGroupAsc);
// actionSortAscending->setActionGroup(actionGroup);
// displayMenu->addSeparator();
// displayMenu->addSeparator();
QActionGroup *actionGroup = new QActionGroup(displayMenu); QActionGroup *actionGroup = new QActionGroup(displayMenu);
actionSortByName = displayMenu->addAction(QIcon(), tr("Sort by Name"), this, SLOT(sort())); actionSortByName = displayMenu->addAction(QIcon(), tr("Sort by Name"), this, SLOT(sort()));
@ -271,6 +296,10 @@ void GroupTreeWidget::initDisplayMenu(QToolButton *toolButton)
actionSortByPosts->setCheckable(true); actionSortByPosts->setCheckable(true);
actionSortByPosts->setActionGroup(actionGroup); actionSortByPosts->setActionGroup(actionGroup);
actionSortByUnread = displayMenu->addAction(QIcon(), tr("Sort by Unread"), this, SLOT(sort()));
actionSortByUnread->setCheckable(true);
actionSortByUnread->setActionGroup(actionGroup);
toolButton->setMenu(displayMenu); toolButton->setMenu(displayMenu);
} }
@ -454,6 +483,7 @@ void GroupTreeWidget::fillGroupItems(QTreeWidgetItem *categoryItem, const QList<
} }
item->setToolTip(COLUMN_NAME, tooltip); item->setToolTip(COLUMN_NAME, tooltip);
item->setToolTip(COLUMN_UNREAD, tooltip);
item->setToolTip(COLUMN_POPULARITY, tooltip); item->setToolTip(COLUMN_POPULARITY, tooltip);
item->setData(COLUMN_DATA, ROLE_SUBSCRIBE_FLAGS, itemInfo.subscribeFlags); item->setData(COLUMN_DATA, ROLE_SUBSCRIBE_FLAGS, itemInfo.subscribeFlags);
@ -506,17 +536,17 @@ void GroupTreeWidget::setUnreadCount(QTreeWidgetItem *item, int unreadCount)
QLabel *waitLabel = NULL; QLabel *waitLabel = NULL;
getNameWidget(ui->treeWidget, item, nameLabel, waitLabel); getNameWidget(ui->treeWidget, item, nameLabel, waitLabel);
QString name = item->data(COLUMN_DATA, ROLE_NAME).toString();
QFont font = nameLabel->font(); QFont font = nameLabel->font();
if (unreadCount) { if (unreadCount) {
name = QString("(%1) ").arg(unreadCount) + name; item->setData(COLUMN_DATA, ROLE_UNREAD, unreadCount);
item->setText(COLUMN_UNREAD, QString::number(unreadCount));
font.setBold(true); font.setBold(true);
} else { } else {
item->setText(COLUMN_UNREAD, "");
font.setBold(false); font.setBold(false);
} }
nameLabel->setText(name);
nameLabel->setFont(font); nameLabel->setFont(font);
} }
@ -698,6 +728,8 @@ void GroupTreeWidget::resort(QTreeWidgetItem *categoryItem)
compareRole->setRole(COLUMN_DATA, ROLE_LASTPOST); compareRole->setRole(COLUMN_DATA, ROLE_LASTPOST);
} else if (actionSortByPosts && actionSortByPosts->isChecked()) { } else if (actionSortByPosts && actionSortByPosts->isChecked()) {
compareRole->setRole(COLUMN_DATA, ROLE_POSTS); compareRole->setRole(COLUMN_DATA, ROLE_POSTS);
} else if (actionSortByUnread && actionSortByUnread->isChecked()) {
compareRole->setRole(COLUMN_DATA, ROLE_UNREAD);
} }
if (categoryItem) { if (categoryItem) {

View File

@ -134,11 +134,12 @@ private:
private: private:
QMenu *displayMenu; QMenu *displayMenu;
QAction *actionSortAscending; QAction *actionSortAscending;
// QAction *actionSortDescending; QAction *actionSortDescending;
QAction *actionSortByName; QAction *actionSortByName;
QAction *actionSortByPopularity; QAction *actionSortByPopularity;
QAction *actionSortByLastPost; QAction *actionSortByLastPost;
QAction *actionSortByPosts; QAction *actionSortByPosts;
QAction *actionSortByUnread;
RSTreeWidgetItemCompareRole *compareRole; RSTreeWidgetItemCompareRole *compareRole;