Merge pull request #536 from PhenomRetroShare/Add_ElidedLabelToGroupTreeWidget

Add ElidedLabel to GroupTreeWidget
This commit is contained in:
Cyril Soler 2016-10-23 23:25:53 +02:00 committed by GitHub
commit c77d0521d0
4 changed files with 195 additions and 121 deletions

View file

@ -52,7 +52,9 @@ ElidedLabel::ElidedLabel(const QString &text, QWidget *parent)
, mElided(false) , mElided(false)
, mOnlyPlainText(false) , mOnlyPlainText(false)
, mContent(text) , mContent(text)
, mTextColor(QColor())
{ {
setStyleSheet("background-color: rgba(0,0,0,0%)");
mRectElision = QRect(); mRectElision = QRect();
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
} }
@ -62,7 +64,9 @@ ElidedLabel::ElidedLabel(QWidget *parent)
, mElided(false) , mElided(false)
, mOnlyPlainText(false) , mOnlyPlainText(false)
, mContent("") , mContent("")
, mTextColor(QColor())
{ {
setStyleSheet("background-color: rgba(0,0,0,0%)");
mRectElision = QRect(); mRectElision = QRect();
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
} }
@ -94,7 +98,7 @@ void ElidedLabel::paintEvent(QPaintEvent *event)
QFontMetrics fontMetrics = painter.fontMetrics(); QFontMetrics fontMetrics = painter.fontMetrics();
QRect cr = contentsRect(); QRect cr = contentsRect();
cr.adjust(margin(), margin(), -margin(), -margin()); cr.adjust(margin(), margin(), -margin(), -margin());
bool didElide = false; bool didElide = false;
QChar ellipsisChar(0x2026);//= "…" QChar ellipsisChar(0x2026);//= "…"
int lineSpacing = fontMetrics.lineSpacing(); int lineSpacing = fontMetrics.lineSpacing();
@ -225,3 +229,11 @@ void ElidedLabel::mousePressEvent(QMouseEvent *ev)
} }
QLabel::mousePressEvent(ev); QLabel::mousePressEvent(ev);
} }
void ElidedLabel::setTextColor(const QColor &color)
{
QPalette tmpPalette = palette();
tmpPalette.setColor(foregroundRole(), color);
setPalette(tmpPalette);
mTextColor = color;
}

View file

@ -53,6 +53,7 @@ class ElidedLabel : public QLabel
Q_PROPERTY(QString text READ text WRITE setText) Q_PROPERTY(QString text READ text WRITE setText)
Q_PROPERTY(bool isElided READ isElided) Q_PROPERTY(bool isElided READ isElided)
Q_PROPERTY(bool isOnlyPlainText READ isOnlyPlainText WRITE setOnlyPlainText) Q_PROPERTY(bool isOnlyPlainText READ isOnlyPlainText WRITE setOnlyPlainText)
Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor)
public: public:
ElidedLabel(const QString &text, QWidget *parent = 0); ElidedLabel(const QString &text, QWidget *parent = 0);
@ -62,6 +63,9 @@ public:
bool isElided() const { return mElided; } bool isElided() const { return mElided; }
bool isOnlyPlainText() const { return mOnlyPlainText; } bool isOnlyPlainText() const { return mOnlyPlainText; }
QColor textColor() const { return mTextColor; }
void setTextColor(const QColor &color);
public slots: public slots:
void setText(const QString &text); void setText(const QString &text);
void setOnlyPlainText(const bool &value); void setOnlyPlainText(const bool &value);
@ -79,6 +83,7 @@ private:
bool mOnlyPlainText; bool mOnlyPlainText;
QString mContent; QString mContent;
QRect mRectElision; QRect mRectElision;
QColor mTextColor;
}; };
#endif // ELIDEDLABEL_H #endif // ELIDEDLABEL_H

View file

@ -510,6 +510,7 @@ static void getNameWidget(QTreeWidget *treeWidget, QTreeWidgetItem *item, Elided
if (!widget) { if (!widget) {
widget = new QWidget; widget = new QWidget;
widget->setAttribute(Qt::WA_TranslucentBackground);
nameLabel = new ElidedLabel(widget); nameLabel = new ElidedLabel(widget);
textLabel = new ElidedLabel(widget); textLabel = new ElidedLabel(widget);

View file

@ -18,20 +18,22 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, * Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA. * Boston, MA 02110-1301, USA.
****************************************************************/ ****************************************************************/
#include <QMenu>
#include <QToolButton>
#include <QLabel>
#include <QMovie>
#include "retroshare/rsgxsflags.h"
#include "GroupTreeWidget.h" #include "GroupTreeWidget.h"
#include "ui_GroupTreeWidget.h" #include "ui_GroupTreeWidget.h"
#include "RSItemDelegate.h" #include <QHBoxLayout>
#include <QLabel>
#include <QMenu>
#include <QMovie>
#include <QToolButton>
#include "retroshare/rsgxsflags.h"
#include "PopularityDefs.h" #include "PopularityDefs.h"
#include "gui/settings/rsharesettings.h" #include "RSItemDelegate.h"
#include "RSTreeWidgetItem.h" #include "RSTreeWidgetItem.h"
#include "gui/common/ElidedLabel.h"
#include "gui/settings/rsharesettings.h"
#include "util/QtVersion.h" #include "util/QtVersion.h"
#include <stdint.h> #include <stdint.h>
@ -55,6 +57,9 @@
#define FILTER_NAME_INDEX 0 #define FILTER_NAME_INDEX 0
#define FILTER_DESC_INDEX 1 #define FILTER_DESC_INDEX 1
Q_DECLARE_METATYPE(ElidedLabel*)
Q_DECLARE_METATYPE(QLabel*)
GroupTreeWidget::GroupTreeWidget(QWidget *parent) : GroupTreeWidget::GroupTreeWidget(QWidget *parent) :
QWidget(parent), ui(new Ui::GroupTreeWidget) QWidget(parent), ui(new Ui::GroupTreeWidget)
{ {
@ -116,6 +121,38 @@ GroupTreeWidget::~GroupTreeWidget()
delete ui; delete ui;
} }
static void getNameWidget(QTreeWidget *treeWidget, QTreeWidgetItem *item, ElidedLabel *&nameLabel, QLabel *&waitLabel)
{
QWidget *widget = treeWidget->itemWidget(item, COLUMN_NAME);
if (!widget) {
widget = new QWidget;
widget->setAttribute(Qt::WA_TranslucentBackground);
nameLabel = new ElidedLabel(widget);
waitLabel = new QLabel(widget);
QMovie *movie = new QMovie(":/images/loader/circleball-16.gif");
waitLabel->setMovie(movie);
waitLabel->setHidden(true);
widget->setProperty("nameLabel", qVariantFromValue(nameLabel));
widget->setProperty("waitLabel", qVariantFromValue(waitLabel));
QHBoxLayout *layout = new QHBoxLayout;
layout->setSpacing(0);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(nameLabel);
layout->addWidget(waitLabel);
widget->setLayout(layout);
treeWidget->setItemWidget(item, COLUMN_NAME, widget);
} else {
nameLabel = widget->property("nameLabel").value<ElidedLabel*>();
waitLabel = widget->property("waitLabel").value<QLabel*>();
}
}
void GroupTreeWidget::changeEvent(QEvent *e) void GroupTreeWidget::changeEvent(QEvent *e)
{ {
QWidget::changeEvent(e); QWidget::changeEvent(e);
@ -229,7 +266,7 @@ void GroupTreeWidget::initDisplayMenu(QToolButton *toolButton)
actionSortByLastPost = displayMenu->addAction(QIcon(), tr("Sort by Last Post"), this, SLOT(sort())); actionSortByLastPost = displayMenu->addAction(QIcon(), tr("Sort by Last Post"), this, SLOT(sort()));
actionSortByLastPost->setCheckable(true); actionSortByLastPost->setCheckable(true);
actionSortByLastPost->setActionGroup(actionGroup); actionSortByLastPost->setActionGroup(actionGroup);
actionSortByPosts = displayMenu->addAction(QIcon(), tr("Sort by Posts"), this, SLOT(sort())); actionSortByPosts = displayMenu->addAction(QIcon(), tr("Sort by Posts"), this, SLOT(sort()));
actionSortByPosts->setCheckable(true); actionSortByPosts->setCheckable(true);
actionSortByPosts->setActionGroup(actionGroup); actionSortByPosts->setActionGroup(actionGroup);
@ -255,6 +292,11 @@ void GroupTreeWidget::updateColors()
} }
item->setForeground(COLUMN_NAME, brush); item->setForeground(COLUMN_NAME, brush);
ElidedLabel *nameLabel = NULL;
QLabel *waitLabel = NULL;
getNameWidget(ui->treeWidget, item, nameLabel, waitLabel);
nameLabel->setTextColor(brush.color());
} }
} }
@ -293,21 +335,27 @@ QTreeWidgetItem *GroupTreeWidget::addCategoryItem(const QString &name, const QIc
{ {
QFont font; QFont font;
QTreeWidgetItem *item = new QTreeWidgetItem(); QTreeWidgetItem *item = new QTreeWidgetItem();
ui->treeWidget->addTopLevelItem(item);
ElidedLabel *nameLabel = NULL;
QLabel *waitLabel = NULL;
getNameWidget(ui->treeWidget, item, nameLabel, waitLabel);
nameLabel->setText(name);
item->setData(COLUMN_DATA, ROLE_NAME, name);
font = item->font(COLUMN_NAME); font = item->font(COLUMN_NAME);
font.setBold(true); font.setBold(true);
item->setText(COLUMN_NAME, name);
item->setData(COLUMN_DATA, ROLE_NAME, name);
item->setFont(COLUMN_NAME, font); item->setFont(COLUMN_NAME, font);
nameLabel->setFont(font);
item->setIcon(COLUMN_NAME, icon); item->setIcon(COLUMN_NAME, icon);
int S = QFontMetricsF(font).height(); int S = QFontMetricsF(font).height();
item->setSizeHint(COLUMN_NAME, QSize(S*1.1, S*1.1)); item->setSizeHint(COLUMN_NAME, QSize(S*1.1, S*1.1));
item->setForeground(COLUMN_NAME, QBrush(textColorCategory())); item->setForeground(COLUMN_NAME, QBrush(textColorCategory()));
nameLabel->setTextColor(textColorCategory());
item->setData(COLUMN_DATA, ROLE_COLOR, GROUPTREEWIDGET_COLOR_CATEGORY); item->setData(COLUMN_DATA, ROLE_COLOR, GROUPTREEWIDGET_COLOR_CATEGORY);
ui->treeWidget->addTopLevelItem(item);
item->setExpanded(expand); item->setExpanded(expand);
return item; return item;
@ -334,114 +382,119 @@ QString GroupTreeWidget::itemIdAt(QPoint &point)
void GroupTreeWidget::fillGroupItems(QTreeWidgetItem *categoryItem, const QList<GroupItemInfo> &itemList) void GroupTreeWidget::fillGroupItems(QTreeWidgetItem *categoryItem, const QList<GroupItemInfo> &itemList)
{ {
if (categoryItem == NULL) { if (categoryItem == NULL) {
return; return;
} }
QString filterText = ui->filterLineEdit->text(); QString filterText = ui->filterLineEdit->text();
/* Iterate all items */ /* Iterate all items */
QList<GroupItemInfo>::const_iterator it; QList<GroupItemInfo>::const_iterator it;
for (it = itemList.begin(); it != itemList.end(); ++it) { for (it = itemList.begin(); it != itemList.end(); ++it) {
const GroupItemInfo &itemInfo = *it; const GroupItemInfo &itemInfo = *it;
QTreeWidgetItem *item = NULL; QTreeWidgetItem *item = NULL;
/* Search exisiting item */ /* Search exisiting item */
int childCount = categoryItem->childCount(); int childCount = categoryItem->childCount();
for (int child = 0; child < childCount; ++child) { for (int child = 0; child < childCount; ++child) {
QTreeWidgetItem *childItem = categoryItem->child(child); QTreeWidgetItem *childItem = categoryItem->child(child);
if (childItem->data(COLUMN_DATA, ROLE_ID).toString() == itemInfo.id) { if (childItem->data(COLUMN_DATA, ROLE_ID).toString() == itemInfo.id) {
/* Found child */ /* Found child */
item = childItem; item = childItem;
break; break;
} }
} }
if (item == NULL) { if (item == NULL) {
item = new RSTreeWidgetItem(compareRole); item = new RSTreeWidgetItem(compareRole);
item->setData(COLUMN_DATA, ROLE_ID, itemInfo.id); item->setData(COLUMN_DATA, ROLE_ID, itemInfo.id);
categoryItem->addChild(item); categoryItem->addChild(item);
} }
item->setText(COLUMN_NAME, itemInfo.name); ElidedLabel *nameLabel = NULL;
item->setData(COLUMN_DATA, ROLE_NAME, itemInfo.name); QLabel *waitLabel = NULL;
item->setData(COLUMN_DATA, ROLE_DESCRIPTION, itemInfo.description); getNameWidget(ui->treeWidget, item, nameLabel, waitLabel);
/* Set last post */ nameLabel->setText(itemInfo.name);
qlonglong lastPost = itemInfo.lastpost.toTime_t(); item->setData(COLUMN_DATA, ROLE_NAME, itemInfo.name);
item->setData(COLUMN_DATA, ROLE_LASTPOST, -lastPost); // negative for correct sorting item->setData(COLUMN_DATA, ROLE_DESCRIPTION, itemInfo.description);
/* Set visible posts */ /* Set last post */
item->setData(COLUMN_DATA, ROLE_POSTS, -itemInfo.max_visible_posts);// negative for correct sorting qlonglong lastPost = itemInfo.lastpost.toTime_t();
item->setData(COLUMN_DATA, ROLE_LASTPOST, -lastPost); // negative for correct sorting
/* Set icon */ /* Set visible posts */
if (ui->treeWidget->itemWidget(item, COLUMN_NAME)) { item->setData(COLUMN_DATA, ROLE_POSTS, -itemInfo.max_visible_posts);// negative for correct sorting
/* Item is waiting, save icon in role */
item->setData(COLUMN_DATA, ROLE_SAVED_ICON, itemInfo.icon);
} else {
item->setIcon(COLUMN_NAME, itemInfo.icon);
}
/* Set popularity */ /* Set icon */
QString tooltip = PopularityDefs::tooltip(itemInfo.popularity); if (waitLabel->isVisible()) {
/* Item is waiting, save icon in role */
item->setData(COLUMN_DATA, ROLE_SAVED_ICON, itemInfo.icon);
} else {
item->setIcon(COLUMN_NAME, itemInfo.icon);
}
item->setIcon(COLUMN_POPULARITY, PopularityDefs::icon(itemInfo.popularity)); /* Set popularity */
item->setData(COLUMN_DATA, ROLE_POPULARITY, -itemInfo.popularity); // negative for correct sorting QString tooltip = PopularityDefs::tooltip(itemInfo.popularity);
/* Set tooltip */ item->setIcon(COLUMN_POPULARITY, PopularityDefs::icon(itemInfo.popularity));
if (itemInfo.adminKey) item->setData(COLUMN_DATA, ROLE_POPULARITY, -itemInfo.popularity); // negative for correct sorting
tooltip += "\n" + tr("You are admin (modify names and description using Edit menu)");
else if (itemInfo.publishKey)
tooltip += "\n" + tr("You have been granted as publisher (you can post here!)");
if(!IS_GROUP_SUBSCRIBED(itemInfo.subscribeFlags)) /* Set tooltip */
{ if (itemInfo.adminKey)
tooltip += "\n" + QString::number(itemInfo.max_visible_posts) + " messages available" ; tooltip += "\n" + tr("You are admin (modify names and description using Edit menu)");
tooltip += "\n" + tr("Subscribe to download and read messages") ; else if (itemInfo.publishKey)
} tooltip += "\n" + tr("You have been granted as publisher (you can post here!)");
item->setToolTip(COLUMN_NAME, tooltip); if(!IS_GROUP_SUBSCRIBED(itemInfo.subscribeFlags))
item->setToolTip(COLUMN_POPULARITY, tooltip); {
tooltip += "\n" + QString::number(itemInfo.max_visible_posts) + " messages available" ;
tooltip += "\n" + tr("Subscribe to download and read messages") ;
}
item->setData(COLUMN_DATA, ROLE_SUBSCRIBE_FLAGS, itemInfo.subscribeFlags); item->setToolTip(COLUMN_NAME, tooltip);
item->setToolTip(COLUMN_POPULARITY, tooltip);
/* Set color */ item->setData(COLUMN_DATA, ROLE_SUBSCRIBE_FLAGS, itemInfo.subscribeFlags);
QBrush brush;
if (itemInfo.publishKey) {
brush = QBrush(textColorPrivateKey());
item->setData(COLUMN_DATA, ROLE_COLOR, GROUPTREEWIDGET_COLOR_PRIVATEKEY);
} else {
brush = ui->treeWidget->palette().color(QPalette::Text);
item->setData(COLUMN_DATA, ROLE_COLOR, GROUPTREEWIDGET_COLOR_STANDARD);
}
item->setForeground(COLUMN_NAME, brush);
/* Calculate score */ /* Set color */
calculateScore(item, filterText); QBrush brush;
} if (itemInfo.publishKey) {
brush = QBrush(textColorPrivateKey());
item->setData(COLUMN_DATA, ROLE_COLOR, GROUPTREEWIDGET_COLOR_PRIVATEKEY);
} else {
brush = ui->treeWidget->palette().color(QPalette::Text);
item->setData(COLUMN_DATA, ROLE_COLOR, GROUPTREEWIDGET_COLOR_STANDARD);
}
item->setForeground(COLUMN_NAME, brush);
nameLabel->setTextColor(brush.color());
/* Remove all items not in list */ /* Calculate score */
int child = 0; calculateScore(item, filterText);
int childCount = categoryItem->childCount(); }
while (child < childCount) {
QString id = categoryItem->child(child)->data(COLUMN_DATA, ROLE_ID).toString();
for (it = itemList.begin(); it != itemList.end(); ++it) { /* Remove all items not in list */
if (it->id == id) { int child = 0;
break; int childCount = categoryItem->childCount();
} while (child < childCount) {
} QString id = categoryItem->child(child)->data(COLUMN_DATA, ROLE_ID).toString();
if (it == itemList.end()) { for (it = itemList.begin(); it != itemList.end(); ++it) {
delete(categoryItem->takeChild(child)); if (it->id == id) {
childCount = categoryItem->childCount(); break;
} else { }
++child; }
}
}
resort(categoryItem); if (it == itemList.end()) {
delete(categoryItem->takeChild(child));
childCount = categoryItem->childCount();
} else {
++child;
}
}
resort(categoryItem);
} }
void GroupTreeWidget::setUnreadCount(QTreeWidgetItem *item, int unreadCount) void GroupTreeWidget::setUnreadCount(QTreeWidgetItem *item, int unreadCount)
@ -449,19 +502,22 @@ void GroupTreeWidget::setUnreadCount(QTreeWidgetItem *item, int unreadCount)
if (item == NULL) { if (item == NULL) {
return; return;
} }
ElidedLabel *nameLabel = NULL;
QLabel *waitLabel = NULL;
getNameWidget(ui->treeWidget, item, nameLabel, waitLabel);
QString name = item->data(COLUMN_DATA, ROLE_NAME).toString(); QString name = item->data(COLUMN_DATA, ROLE_NAME).toString();
QFont font = item->font(COLUMN_NAME); QFont font = nameLabel->font();
if (unreadCount) { if (unreadCount) {
name += QString(" (%1)").arg(unreadCount); name = QString("(%1) ").arg(unreadCount) + name;
font.setBold(true); font.setBold(true);
} else { } else {
font.setBold(false); font.setBold(false);
} }
item->setText(COLUMN_NAME, name); nameLabel->setText(name);
item->setFont(COLUMN_NAME, font); nameLabel->setFont(font);
} }
QTreeWidgetItem *GroupTreeWidget::getItemFromId(const QString &id) QTreeWidgetItem *GroupTreeWidget::getItemFromId(const QString &id)
@ -507,9 +563,11 @@ bool GroupTreeWidget::setWaiting(const QString &id, bool wait)
return false; return false;
} }
QWidget *w = ui->treeWidget->itemWidget(item, COLUMN_NAME); ElidedLabel *nameLabel = NULL;
QLabel *waitLabel = NULL;
getNameWidget(ui->treeWidget, item, nameLabel, waitLabel);
if (wait) { if (wait) {
if (w) { if (waitLabel->isVisible()) {
/* Allready waiting */ /* Allready waiting */
} else { } else {
/* Save icon in role */ /* Save icon in role */
@ -519,21 +577,19 @@ bool GroupTreeWidget::setWaiting(const QString &id, bool wait)
/* Create empty icon of the same size */ /* Create empty icon of the same size */
QPixmap pixmap(ui->treeWidget->iconSize()); QPixmap pixmap(ui->treeWidget->iconSize());
pixmap.fill(Qt::transparent); pixmap.fill(Qt::transparent);
item->setIcon(COLUMN_NAME, QIcon(pixmap)); item->setIcon(COLUMN_NAME, QIcon(pixmap));
QLabel *label = new QLabel(this); /* Show waitLabel and hide nameLabel */
QMovie *movie = new QMovie(":/images/loader/circleball-16.gif"); nameLabel->setHidden(true);
label->setMovie(movie); waitLabel->setVisible(true);
waitLabel->movie()->start();
ui->treeWidget->setItemWidget(item, COLUMN_NAME, label);
movie->start();
} }
} else { } else {
if (w) { if (waitLabel->isVisible()) {
ui->treeWidget->setItemWidget(item, COLUMN_NAME, NULL); /* Show nameLabel and hide waitLabel */
delete(w); waitLabel->movie()->stop();
waitLabel->setHidden(true);
nameLabel->setVisible(true);
/* Set icon saved in role */ /* Set icon saved in role */
item->setIcon(COLUMN_NAME, item->data(COLUMN_DATA, ROLE_SAVED_ICON).value<QIcon>()); item->setIcon(COLUMN_NAME, item->data(COLUMN_DATA, ROLE_SAVED_ICON).value<QIcon>());