Merge pull request #1920 from PhenomRetroShare/Fix_GroupTreeWidgetQSS

Fix GroupTreeWidget with RSElidedItemDelegate for StyleSheet.
This commit is contained in:
defnax 2020-05-09 18:19:34 +02:00 committed by GitHub
commit d2794ed6d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 158 additions and 128 deletions

View File

@ -24,8 +24,8 @@ before_install:
sudo apt-get install -y
build-essential libssl-dev libsqlcipher-dev libbz2-dev libsqlite3-dev
libupnp-dev pkg-config qt5-default libxss-dev qtmultimedia5-dev
libqt5x11extras5-dev libqt5designer5 libxapian-dev qttools5-dev
rapidjson-dev ;
libqt5x11extras5-dev libqt5designer5 libqt5svg5-dev
libxapian-dev qttools5-dev rapidjson-dev ;
fi
- if [ $TRAVIS_OS_NAME == osx ]; then brew update ; fi
- if [ $TRAVIS_OS_NAME == osx ]; then brew install qt5; fi

View File

@ -18,28 +18,20 @@
* *
*******************************************************************************/
#include <iostream>
#include "GroupTreeWidget.h"
#include "ui_GroupTreeWidget.h"
#include <QHBoxLayout>
#include <QHeaderView>
#include <QLabel>
#include <QMenu>
#include <QMovie>
#include <QToolButton>
#include "retroshare/rsgxsflags.h"
#include "PopularityDefs.h"
#include "RSElidedItemDelegate.h"
#include "RSTreeWidgetItem.h"
#include "gui/common/ElidedLabel.h"
#include "gui/settings/rsharesettings.h"
#include "util/QtVersion.h"
#include "util/DateTime.h"
#include <QMenu>
#include <stdint.h>
#define COLUMN_NAME 0
@ -66,9 +58,6 @@
#define FILTER_NAME_INDEX 0
#define FILTER_DESC_INDEX 1
Q_DECLARE_METATYPE(ElidedLabel*)
Q_DECLARE_METATYPE(QLabel*)
GroupTreeWidget::GroupTreeWidget(QWidget *parent) :
QWidget(parent), ui(new Ui::GroupTreeWidget)
{
@ -151,38 +140,6 @@ GroupTreeWidget::~GroupTreeWidget()
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)
{
QWidget::changeEvent(e);
@ -346,11 +303,6 @@ void GroupTreeWidget::updateColors()
}
item->setForeground(COLUMN_NAME, brush);
ElidedLabel *nameLabel = NULL;
QLabel *waitLabel = NULL;
getNameWidget(ui->treeWidget, item, nameLabel, waitLabel);
nameLabel->setTextColor(brush.color());
}
}
@ -391,23 +343,17 @@ QTreeWidgetItem *GroupTreeWidget::addCategoryItem(const QString &name, const QIc
QTreeWidgetItem *item = new QTreeWidgetItem();
ui->treeWidget->addTopLevelItem(item);
ElidedLabel *nameLabel = NULL;
QLabel *waitLabel = NULL;
getNameWidget(ui->treeWidget, item, nameLabel, waitLabel);
nameLabel->setText(name);
item->setText(COLUMN_NAME, name);
item->setData(COLUMN_DATA, ROLE_NAME, name);
font = item->font(COLUMN_NAME);
font.setBold(true);
item->setFont(COLUMN_NAME, font);
nameLabel->setFont(font);
item->setIcon(COLUMN_NAME, icon);
int S = QFontMetricsF(font).height();
item->setSizeHint(COLUMN_NAME, QSize(S*1.9, S*1.9));
item->setForeground(COLUMN_NAME, QBrush(textColorCategory()));
nameLabel->setTextColor(textColorCategory());
item->setData(COLUMN_DATA, ROLE_COLOR, GROUPTREEWIDGET_COLOR_CATEGORY);
item->setExpanded(expand);
@ -437,19 +383,19 @@ void GroupTreeWidget::setDistSearchVisible(bool visible)
bool GroupTreeWidget::isSearchRequestResult(QPoint &point,QString& group_id,uint32_t& search_req_id)
{
QTreeWidgetItem *item = ui->treeWidget->itemAt(point);
QTreeWidgetItem *item = ui->treeWidget->itemAt(point);
if (item == NULL)
return false;
QTreeWidgetItem *parent = item->parent();
QTreeWidgetItem *parent = item->parent();
if(parent == NULL)
return false ;
if(parent == NULL)
return false ;
search_req_id = parent->data(COLUMN_DATA, ROLE_REQUEST_ID).toUInt();
group_id = itemId(item) ;
group_id = itemId(item) ;
return search_req_id > 0;
return search_req_id > 0;
}
bool GroupTreeWidget::isSearchRequestItem(QPoint &point,uint32_t& search_req_id)
@ -514,11 +460,7 @@ void GroupTreeWidget::fillGroupItems(QTreeWidgetItem *categoryItem, const QList<
categoryItem->addChild(item);
}
ElidedLabel *nameLabel = NULL;
QLabel *waitLabel = NULL;
getNameWidget(ui->treeWidget, item, nameLabel, waitLabel);
nameLabel->setText(itemInfo.name);
item->setText(COLUMN_NAME, itemInfo.name);
item->setData(COLUMN_DATA, ROLE_NAME, itemInfo.name);
item->setData(COLUMN_DATA, ROLE_DESCRIPTION, itemInfo.description);
@ -535,12 +477,7 @@ void GroupTreeWidget::fillGroupItems(QTreeWidgetItem *categoryItem, const QList<
item->setData(COLUMN_DATA, ROLE_POSTS, -itemInfo.max_visible_posts);// negative for correct sorting
/* Set icon */
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_NAME, itemInfo.icon);
/* Set popularity */
QString tooltip = PopularityDefs::tooltip(itemInfo.popularity);
@ -582,7 +519,6 @@ void GroupTreeWidget::fillGroupItems(QTreeWidgetItem *categoryItem, const QList<
item->setData(COLUMN_DATA, ROLE_COLOR, GROUPTREEWIDGET_COLOR_STANDARD);
}
item->setForeground(COLUMN_NAME, brush);
nameLabel->setTextColor(brush.color());
/* Calculate score */
calculateScore(item, filterText);
@ -616,11 +552,8 @@ void GroupTreeWidget::setUnreadCount(QTreeWidgetItem *item, int unreadCount)
if (item == NULL) {
return;
}
ElidedLabel *nameLabel = NULL;
QLabel *waitLabel = NULL;
getNameWidget(ui->treeWidget, item, nameLabel, waitLabel);
QFont font = nameLabel->font();
QFont font = item->font(COLUMN_NAME);
if (unreadCount) {
item->setData(COLUMN_DATA, ROLE_UNREAD, unreadCount);
@ -631,7 +564,7 @@ void GroupTreeWidget::setUnreadCount(QTreeWidgetItem *item, int unreadCount)
font.setBold(false);
}
nameLabel->setFont(font);
item->setFont(COLUMN_NAME, font);
}
QTreeWidgetItem *GroupTreeWidget::getItemFromId(const QString &id)
@ -677,40 +610,7 @@ bool GroupTreeWidget::setWaiting(const QString &id, bool wait)
return false;
}
ElidedLabel *nameLabel = NULL;
QLabel *waitLabel = NULL;
getNameWidget(ui->treeWidget, item, nameLabel, waitLabel);
if (wait) {
if (waitLabel->isVisible()) {
/* Allready waiting */
} else {
/* Save icon in role */
QIcon icon = item->icon(COLUMN_NAME);
item->setData(COLUMN_DATA, ROLE_SAVED_ICON, icon);
/* Create empty icon of the same size */
QPixmap pixmap(ui->treeWidget->iconSize());
pixmap.fill(Qt::transparent);
item->setIcon(COLUMN_NAME, QIcon(pixmap));
/* Show waitLabel and hide nameLabel */
nameLabel->setHidden(true);
waitLabel->setVisible(true);
waitLabel->movie()->start();
}
} else {
if (waitLabel->isVisible()) {
/* Show nameLabel and hide waitLabel */
waitLabel->movie()->stop();
waitLabel->setHidden(true);
nameLabel->setVisible(true);
/* Set icon saved in role */
item->setIcon(COLUMN_NAME, item->data(COLUMN_DATA, ROLE_SAVED_ICON).value<QIcon>());
item->setData(COLUMN_DATA, ROLE_SAVED_ICON, QIcon());
}
}
item->setData(COLUMN_NAME, Qt::StatusTipRole, wait ? "waiting" : "");
return true;
}
@ -719,6 +619,25 @@ RSTreeWidget *GroupTreeWidget::treeWidget()
return ui->treeWidget;
}
void GroupTreeWidget::setWaitingSVG(const QString &value)
{
if (ui->treeWidget->itemDelegate())
ui->treeWidget->itemDelegate()->setProperty("waitingSVG",value);
for(int i=0; i<COLUMN_COUNT; i++)
if (ui->treeWidget->itemDelegateForColumn(i))
ui->treeWidget->itemDelegateForColumn(i)->setProperty("waitingSVG",value);
}
void GroupTreeWidget::setWaitingSVG_Over(const bool &value)
{
if (ui->treeWidget->itemDelegate())
ui->treeWidget->itemDelegate()->setProperty("waitingSVG_Over",value);
for(int i=0; i<COLUMN_COUNT; i++)
if (ui->treeWidget->itemDelegateForColumn(i))
ui->treeWidget->itemDelegateForColumn(i)->setProperty("waitingSVG_Over",value);
}
bool GroupTreeWidget::getGroupName(QString id, QString& name)
{
QTreeWidgetItem *item = getItemFromId(id);

View File

@ -21,8 +21,6 @@
#ifndef GROUPTREEWIDGET_H
#define GROUPTREEWIDGET_H
#include <QWidget>
#include <QIcon>
#include <QTreeWidgetItem>
#include <QDateTime>
@ -68,6 +66,8 @@ class GroupTreeWidget : public QWidget
Q_PROPERTY(QColor textColorCategory READ textColorCategory WRITE setTextColorCategory)
Q_PROPERTY(QColor textColorPrivateKey READ textColorPrivateKey WRITE setTextColorPrivateKey)
Q_PROPERTY(QString waitingSVG WRITE setWaitingSVG)
Q_PROPERTY(bool waitingSVG_Over WRITE setWaitingSVG_Over)
public:
GroupTreeWidget(QWidget *parent = 0);
@ -109,7 +109,11 @@ public:
void setTextColorCategory(QColor color) { mTextColor[GROUPTREEWIDGET_COLOR_CATEGORY] = color; }
void setTextColorPrivateKey(QColor color) { mTextColor[GROUPTREEWIDGET_COLOR_PRIVATEKEY] = color; }
bool getGroupName(QString id, QString& name);
void setWaitingSVG(const QString &value);
void setWaitingSVG_Over(const bool &value);
bool getGroupName(QString id, QString& name);
int subscribeFlags(const QString &id);

View File

@ -23,6 +23,7 @@
#include "gui/common/StyledElidedLabel.h"
#include "util/rsdebug.h"
#include <QAbstractItemView>
#include <QApplication>
#include <QPainter>
#include <QTextDocument>
@ -32,7 +33,9 @@
RSElidedItemDelegate::RSElidedItemDelegate(QObject *parent)
: RSStyledItemDelegate(parent)
, mOnlyPlainText(false), mPaintRoundedRect(true)
, mWaitingSVG(":/icons/svg/waiting.svg"), mWaitingSVG_Over(true)
{
mSVG_Renderer = new QSvgRenderer(this);
}
QSize RSElidedItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
@ -124,13 +127,35 @@ void RSElidedItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
}
// draw the icon
{
QIcon::Mode mode = QIcon::Normal;
if (!(ownOption.state & QStyle::State_Enabled))
mode = QIcon::Disabled;
else if (ownOption.state & QStyle::State_Selected)
mode = QIcon::Selected;
QIcon::State state = ownOption.state & QStyle::State_Open ? QIcon::On : QIcon::Off;
ownOption.icon.paint(painter, iconRect, ownOption.decorationAlignment, mode, state);
if (!ownOption.icon.isNull())
{
QString status;
if (index.data(Qt::StatusTipRole).canConvert(QMetaType::QString))
status = index.data(Qt::StatusTipRole).toString();
if (mWaitingSVG_Over || (status.toLower() != "waiting"))
{
QIcon::Mode mode = QIcon::Normal;
if (!(ownOption.state & QStyle::State_Enabled))
mode = QIcon::Disabled;
else if (ownOption.state & QStyle::State_Selected)
mode = QIcon::Selected;
QIcon::State state = ownOption.state & QStyle::State_Open ? QIcon::On : QIcon::Off;
ownOption.icon.paint(painter, iconRect, ownOption.decorationAlignment, mode, state);
}
if (status.toLower() == "waiting")
{
if (mLastSVG_Loaded != mWaitingSVG)
mSVG_Renderer->load(mWaitingSVG);
mLastSVG_Loaded = mWaitingSVG;
const QAbstractItemView* aiv = dynamic_cast<const QAbstractItemView*>(option.widget);
if (aiv)
connect(mSVG_Renderer, SIGNAL(repaintNeeded()), aiv->viewport(),SLOT(update()));
mSVG_Renderer->render(painter, iconRect);
}
}
}
// draw the text
if (!ownOption.text.isEmpty()) {
@ -141,6 +166,7 @@ void RSElidedItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
if (ownOption.state & QStyle::State_Selected) {
painter->setPen(ownOption.palette.color(cg, QPalette::HighlightedText));
} else {
#if QT_VERSION >= QT_VERSION_CHECK(5,6,0)
if (ownOption.state & QStyle::State_MouseOver) {
//TODO: Manage to get palette with HOVER css pseudoclass
// For now this is hidden by Qt: https://code.woboq.org/qt5/qtbase/src/widgets/styles/qstylesheetstyle.cpp.html#6103
@ -227,6 +253,7 @@ void RSElidedItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
painter->setPen(moColor);
}
else
#endif
if (textColor.spec()==QColor::Invalid) {
painter->setPen(ownOption.palette.color(cg, QPalette::Text));
} else { //Only get color from index for unselected(as Qt does)

View File

@ -23,11 +23,18 @@
#include <gui/common/RSItemDelegate.h>
#include <QSvgRenderer>
class RSElidedItemDelegate : public RSStyledItemDelegate
{
Q_OBJECT
// For now, these properties cannot be changed by StyleSheet
// If needed, you can add properties to owner widget then copy them in this delegate.
Q_PROPERTY(bool isOnlyPlainText READ isOnlyPlainText WRITE setOnlyPlainText)
Q_PROPERTY(bool paintRoundedRect READ paintRoundedRect WRITE setPaintRoundedRect)
Q_PROPERTY(QString waitingSVG READ waitingSVG WRITE setWaitingSVG)
Q_PROPERTY(bool waitingSVG_Over READ waitingSVG_Over WRITE setWaitingSVG_Over)
public:
RSElidedItemDelegate(QObject *parent = 0);
@ -39,6 +46,10 @@ public:
void setOnlyPlainText(const bool &value) { mOnlyPlainText = value; }
bool paintRoundedRect() const { return mPaintRoundedRect; }
void setPaintRoundedRect(const bool &value) { mPaintRoundedRect = value; }
QString waitingSVG() const {return mWaitingSVG; }
void setWaitingSVG(const QString &value) {mWaitingSVG = value; }
bool waitingSVG_Over() const {return mWaitingSVG_Over; }
void setWaitingSVG_Over(const bool &value) {mWaitingSVG_Over = value; }
protected:
bool editorEvent(QEvent *event,
@ -49,7 +60,10 @@ protected:
private:
bool mOnlyPlainText;
bool mPaintRoundedRect;
QSvgRenderer* mSVG_Renderer;
mutable QString mLastSVG_Loaded;
QString mWaitingSVG;
bool mWaitingSVG_Over;
};
#endif // RSELIDEDITEMDELEGATE_H

View File

@ -217,6 +217,7 @@
<file>icons/svg/thumbs-up.svg</file>
<file>icons/svg/upload.svg</file>
<file>icons/svg/video.svg</file>
<file>icons/svg/waiting.svg</file>
<file>icons/switch00_128.png</file>
<file>icons/switch01_128.png</file>
<file>icons/switch10_128.png</file>

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
height="128"
width="128"
xml:space="preserve">
<g>
<circle cx="64" cy="64" fill="none"
stroke="#039bd5" stroke-opacity="1"
stroke-width="10" r="32"
stroke-dasharray="40,20,10,15,10,20"
stroke-linecap="round">
<animateTransform attributeName="transform" type="rotate" repeatCount="indefinite" dur="2s" values="0 64 64;360 64 64" keyTimes="0;1"/>
</circle>
<circle cx="64" cy="64" fill="none"
stroke="#ffffff" stroke-opacity="1"
stroke-width="10" r="22"
stroke-dasharray="40,20,10,15,10,20"
stroke-linecap="round">
<animateTransform attributeName="transform" type="rotate" repeatCount="indefinite" dur="2s" values="360 64 64;0 64 64" keyTimes="0;1"/>
</circle>
<circle cx="64" cy="64" fill="none"
stroke="#000000" stroke-opacity="1"
stroke-width="10" r="42"
stroke-dasharray="40,20,10,15,10,20"
stroke-linecap="round">
<animateTransform attributeName="transform" type="rotate" repeatCount="indefinite" dur="2s" values="360 64 64;0 64 64" keyTimes="0;1"/>
</circle>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -2143,3 +2143,10 @@ PostedCardView > QFrame#mainFrame[new=true] {
WireGroupItem QFrame#frame{
background: transparent;
}
GxsChannelDialog GroupTreeWidget#groupTreeWidget{
background-color: #F050F0;
qproperty-waitingSVG_Over: false;
qproperty-waitingSVG: url(%THISPATH%/qdarkstyle/channel_waiting.svg);
}

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
height="128"
width="128"
xml:space="preserve">
<g>
<circle cx="64" cy="64" fill="#039bd5" fill-opacity="1" stroke="none" stroke-width="1.6" r="64"/>
<g>
<rect x="20" y="32" width="84" height="64"
fill="none"
stroke-width="5" stroke="#ffffff" stroke-opacity="1"
rx="16">
<animateTransform attributeName="transform" type="rotate" repeatCount="indefinite" dur="1s" values="0 64 64;360 64 64" keyTimes="0;1"/>
</rect>
<polygon points="55.92,50 80.16,64 55.92,78"
fill="none"
stroke-width="5" stroke="#ffffff" stroke-opacity="1"
stroke-linejoin="round" rx="10">
<animateTransform attributeName="transform" type="rotate" repeatCount="indefinite" dur="1s" values="360 64 64;0 64 64" keyTimes="0;1"/>
</polygon>
</g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 996 B

View File

@ -19,7 +19,7 @@
!include("../../retroshare.pri"): error("Could not include file ../../retroshare.pri")
TEMPLATE = app
QT += network xml
QT += network xml svg
CONFIG += qt gui uic qrc resources idle
CONFIG += console
TARGET = retroshare