Added new class StyledElidedLabel and used it for nick name and channel label.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@7757 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2014-12-14 17:05:43 +00:00
parent 2036f379de
commit 78c71ab011
11 changed files with 186 additions and 53 deletions

View file

@ -183,7 +183,7 @@
</widget> </widget>
</item> </item>
<item row="0" column="1" colspan="3"> <item row="0" column="1" colspan="3">
<widget class="StyledLabel" name="nicknameLabel"> <widget class="StyledElidedLabel" name="nicknameLabel">
<property name="text"> <property name="text">
<string notr="true">Nickname (Location)</string> <string notr="true">Nickname (Location)</string>
</property> </property>
@ -830,6 +830,11 @@
<header location="global">gui/chat/ChatTabWidget.h</header> <header location="global">gui/chat/ChatTabWidget.h</header>
<container>1</container> <container>1</container>
</customwidget> </customwidget>
<customwidget>
<class>StyledElidedLabel</class>
<extends>QLabel</extends>
<header>gui/common/StyledElidedLabel.h</header>
</customwidget>
</customwidgets> </customwidgets>
<resources> <resources>
<include location="images.qrc"/> <include location="images.qrc"/>

View file

@ -47,23 +47,28 @@
ElidedLabel::ElidedLabel(const QString &text, QWidget *parent) ElidedLabel::ElidedLabel(const QString &text, QWidget *parent)
: QLabel(parent) : QLabel(parent)
, elided(false) , mElided(false)
, content(text) , mContent(text)
{ {
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
} }
ElidedLabel::ElidedLabel(QWidget *parent) ElidedLabel::ElidedLabel(QWidget *parent)
: QLabel(parent) : QLabel(parent)
, elided(false) , mElided(false)
{ {
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
content="";
} }
void ElidedLabel::setText(const QString &newText) void ElidedLabel::setText(const QString &newText)
{ {
content = newText; mContent = newText;
update();
}
void ElidedLabel::clear()
{
mContent.clear();
update(); update();
} }
@ -80,7 +85,7 @@ void ElidedLabel::paintEvent(QPaintEvent *event)
int lineSpacing = fontMetrics.lineSpacing(); int lineSpacing = fontMetrics.lineSpacing();
int x, y = x =cr.top()+(cr.height()-lineSpacing)/2; int x, y = x =cr.top()+(cr.height()-lineSpacing)/2;
QTextLayout textLayout( content, painter.font()); QTextLayout textLayout(mContent, painter.font());
textLayout.beginLayout(); textLayout.beginLayout();
forever { forever {
QTextLine line = textLayout.createLine(); QTextLine line = textLayout.createLine();
@ -95,7 +100,7 @@ void ElidedLabel::paintEvent(QPaintEvent *event)
line.draw(&painter, QPoint(x, y)); line.draw(&painter, QPoint(x, y));
y = nextLineY; y = nextLineY;
} else { } else {
QString lastLine = content.mid(line.textStart()); QString lastLine = mContent.mid(line.textStart());
QString elidedLastLine = fontMetrics.elidedText(lastLine, Qt::ElideRight, cr.width()); QString elidedLastLine = fontMetrics.elidedText(lastLine, Qt::ElideRight, cr.width());
painter.drawText(QPoint(x, y + fontMetrics.ascent()), elidedLastLine); painter.drawText(QPoint(x, y + fontMetrics.ascent()), elidedLastLine);
line = textLayout.createLine(); line = textLayout.createLine();
@ -105,8 +110,8 @@ void ElidedLabel::paintEvent(QPaintEvent *event)
} }
textLayout.endLayout(); textLayout.endLayout();
if (didElide != elided) { if (didElide != mElided) {
elided = didElide; mElided = didElide;
emit elisionChanged(didElide); emit elisionChanged(didElide);
} }
} }

View file

@ -38,38 +38,41 @@
** **
****************************************************************************/ ****************************************************************************/
#ifndef ELIDEDLABEL_H #ifndef ELIDEDLABEL_H
#define ELIDEDLABEL_H #define ELIDEDLABEL_H
#include <QLabel> #include <QLabel>
#include <QRect> #include <QRect>
#include <QResizeEvent> #include <QResizeEvent>
#include <QString> #include <QString>
#include <QWidget> #include <QWidget>
class ElidedLabel : public QLabel class ElidedLabel : public QLabel
{ {
Q_OBJECT Q_OBJECT
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)
public: public:
ElidedLabel(const QString &text, QWidget *parent = 0); ElidedLabel(const QString &text, QWidget *parent = 0);
ElidedLabel(QWidget *parent = 0); ElidedLabel(QWidget *parent = 0);
void setText(const QString &text); const QString & text() const { return mContent; }
const QString & text() const { return content; } bool isElided() const { return mElided; }
bool isElided() const { return elided; }
protected: public slots:
void setText(const QString &text);
void clear();
protected:
void paintEvent(QPaintEvent *event); void paintEvent(QPaintEvent *event);
signals: signals:
void elisionChanged(bool elided); void elisionChanged(bool elided);
private: private:
bool elided; bool mElided;
QString content; QString mContent;
}; };
#endif // TEXTWRAPPINGWIDGET_H #endif // ELIDEDLABEL_H

View file

@ -0,0 +1,43 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2014, RetroShare Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#include <QFont>
#include "StyledElidedLabel.h"
/** Constructor */
StyledElidedLabel::StyledElidedLabel(QWidget *parent)
: ElidedLabel(parent)
{
}
StyledElidedLabel::StyledElidedLabel(const QString &text, QWidget *parent)
: ElidedLabel(text, parent)
{
}
void StyledElidedLabel::setFontSizeFactor(int factor)
{
QFont f = font();
qreal fontSize = factor * f.pointSizeF() / 100;
f.setPointSizeF(fontSize);
setFont(f);
}

View file

@ -0,0 +1,39 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2014, RetroShare Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#ifndef _STYLEDELIDEDLABEL_H
#define _STYLEDELIDEDLABEL_H
#include "ElidedLabel.h"
class StyledElidedLabel : public ElidedLabel
{
Q_OBJECT
Q_PROPERTY(int fontSizeFactor WRITE setFontSizeFactor)
public:
StyledElidedLabel(QWidget *parent = NULL);
StyledElidedLabel(const QString &text, QWidget *parent = NULL);
void setFontSizeFactor(int factor);
};
#endif

View file

@ -29,6 +29,11 @@ StyledLabel::StyledLabel(QWidget *parent)
{ {
} }
StyledLabel::StyledLabel(const QString &text, QWidget *parent)
: QLabel(text, parent)
{
}
void StyledLabel::setFontSizeFactor(int factor) void StyledLabel::setFontSizeFactor(int factor)
{ {
QFont f = font(); QFont f = font();

View file

@ -23,7 +23,6 @@
#define _STYLEDLABEL_H #define _STYLEDLABEL_H
#include <QLabel> #include <QLabel>
#include <retroshare/rsidentity.h>
class StyledLabel : public QLabel class StyledLabel : public QLabel
{ {
@ -32,6 +31,7 @@ class StyledLabel : public QLabel
public: public:
StyledLabel(QWidget *parent = NULL); StyledLabel(QWidget *parent = NULL);
StyledLabel(const QString &text, QWidget *parent = NULL);
void setFontSizeFactor(int factor); void setFontSizeFactor(int factor);
}; };

View file

@ -27,6 +27,7 @@
#include "UIStateHelper.h" #include "UIStateHelper.h"
#include "RSTreeWidget.h" #include "RSTreeWidget.h"
#include "RSTextBrowser.h" #include "RSTextBrowser.h"
#include "ElidedLabel.h"
class UIStateHelperObject class UIStateHelperObject
{ {
@ -36,6 +37,11 @@ public:
init(); init();
mLabel = widget; mLabel = widget;
} }
UIStateHelperObject(ElidedLabel *widget)
{
init();
mElidedLabel = widget;
}
UIStateHelperObject(QLineEdit *widget) UIStateHelperObject(QLineEdit *widget)
{ {
init(); init();
@ -58,6 +64,10 @@ public:
mLabel->setText(text); mLabel->setText(text);
} }
if (mElidedLabel) {
mElidedLabel->setText(text);
}
if (mLineEdit) { if (mLineEdit) {
if (loading && clear) { if (loading && clear) {
mLineEdit->clear(); mLineEdit->clear();
@ -93,6 +103,10 @@ public:
mLabel->clear(); mLabel->clear();
} }
if (mElidedLabel) {
mElidedLabel->clear();
}
if (mLineEdit) { if (mLineEdit) {
mLineEdit->clear(); mLineEdit->clear();
} }
@ -112,6 +126,10 @@ public:
return mLabel; return mLabel;
} }
if (mElidedLabel) {
return mElidedLabel;
}
if (mLineEdit) { if (mLineEdit) {
return mLineEdit; return mLineEdit;
} }
@ -133,6 +151,10 @@ public:
return true; return true;
} }
if (mElidedLabel == widget) {
return true;
}
if (mLineEdit == widget) { if (mLineEdit == widget) {
return true; return true;
} }
@ -151,6 +173,7 @@ public:
bool operator ==(const UIStateHelperObject &data) const bool operator ==(const UIStateHelperObject &data) const
{ {
if (mLabel == data.mLabel && if (mLabel == data.mLabel &&
mElidedLabel == data.mElidedLabel &&
mLineEdit == data.mLineEdit && mLineEdit == data.mLineEdit &&
mTreeWidget == data.mTreeWidget && mTreeWidget == data.mTreeWidget &&
mRSTextBrowser == data.mRSTextBrowser) { mRSTextBrowser == data.mRSTextBrowser) {
@ -163,6 +186,7 @@ public:
bool operator <(const UIStateHelperObject &data) const bool operator <(const UIStateHelperObject &data) const
{ {
if (mLabel < data.mLabel || if (mLabel < data.mLabel ||
mElidedLabel < data.mElidedLabel ||
mLineEdit < data.mLineEdit || mLineEdit < data.mLineEdit ||
mTreeWidget < data.mTreeWidget || mTreeWidget < data.mTreeWidget ||
mRSTextBrowser < data.mRSTextBrowser) { mRSTextBrowser < data.mRSTextBrowser) {
@ -176,6 +200,7 @@ private:
void init() void init()
{ {
mLabel = NULL; mLabel = NULL;
mElidedLabel = NULL;
mLineEdit = NULL; mLineEdit = NULL;
mTreeWidget = NULL; mTreeWidget = NULL;
mRSTextBrowser = NULL; mRSTextBrowser = NULL;
@ -183,6 +208,7 @@ private:
private: private:
QLabel *mLabel; QLabel *mLabel;
ElidedLabel *mElidedLabel;
QLineEdit *mLineEdit; QLineEdit *mLineEdit;
RSTreeWidget *mTreeWidget; RSTreeWidget *mTreeWidget;
RSTextBrowser *mRSTextBrowser; RSTextBrowser *mRSTextBrowser;
@ -247,6 +273,12 @@ void UIStateHelper::addLoadPlaceholder(int index, QLabel *widget, bool clear, co
data->mLoad.insert(UIStateHelperObject(widget), QPair<QString, bool>(text.isEmpty() ? tr("Loading") : text, clear)); data->mLoad.insert(UIStateHelperObject(widget), QPair<QString, bool>(text.isEmpty() ? tr("Loading") : text, clear));
} }
void UIStateHelper::addLoadPlaceholder(int index, ElidedLabel *widget, bool clear, const QString &text)
{
UIStateHelperData *data = findData(index, true);
data->mLoad.insert(UIStateHelperObject(widget), QPair<QString, bool>(text.isEmpty() ? tr("Loading") : text, clear));
}
void UIStateHelper::addLoadPlaceholder(int index, QLineEdit *widget, bool clear, const QString &text) void UIStateHelper::addLoadPlaceholder(int index, QLineEdit *widget, bool clear, const QString &text)
{ {
UIStateHelperData *data = findData(index, true); UIStateHelperData *data = findData(index, true);

View file

@ -27,6 +27,7 @@
class QWidget; class QWidget;
class QLabel; class QLabel;
class ElidedLabel;
class QLineEdit; class QLineEdit;
class RSTreeWidget; class RSTreeWidget;
class UIStateHelperData; class UIStateHelperData;
@ -59,6 +60,7 @@ public:
/* Add widgets */ /* Add widgets */
void addWidget(int index, QWidget *widget, UIStates states = UISTATE_LOADING_DISABLED | UISTATE_ACTIVE_ENABLED); void addWidget(int index, QWidget *widget, UIStates states = UISTATE_LOADING_DISABLED | UISTATE_ACTIVE_ENABLED);
void addLoadPlaceholder(int index, QLabel *widget, bool clear = true, const QString &text = "" /* ="Loading" */); void addLoadPlaceholder(int index, QLabel *widget, bool clear = true, const QString &text = "" /* ="Loading" */);
void addLoadPlaceholder(int index, ElidedLabel *widget, bool clear = true, const QString &text = "" /* ="Loading" */);
void addLoadPlaceholder(int index, QLineEdit *widget, bool clear = true, const QString &text = "" /* ="Loading" */); void addLoadPlaceholder(int index, QLineEdit *widget, bool clear = true, const QString &text = "" /* ="Loading" */);
void addLoadPlaceholder(int index, RSTreeWidget *widget, bool clear = true, const QString &text = "" /* ="Loading" */); void addLoadPlaceholder(int index, RSTreeWidget *widget, bool clear = true, const QString &text = "" /* ="Loading" */);
void addLoadPlaceholder(int index, RSTextBrowser *widget, bool clear = true, const QString &text = "" /* ="Loading" */); void addLoadPlaceholder(int index, RSTextBrowser *widget, bool clear = true, const QString &text = "" /* ="Loading" */);

View file

@ -73,7 +73,7 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="StyledLabel" name="nameLabel"> <widget class="StyledElidedLabel" name="nameLabel">
<property name="palette"> <property name="palette">
<palette> <palette>
<active/> <active/>
@ -84,9 +84,6 @@
<property name="text"> <property name="text">
<string notr="true">Channel Name</string> <string notr="true">Channel Name</string>
</property> </property>
<property name="scaledContents">
<bool>true</bool>
</property>
</widget> </widget>
</item> </item>
</layout> </layout>
@ -306,6 +303,11 @@
<zorder>fileWidget</zorder> <zorder>fileWidget</zorder>
</widget> </widget>
<customwidgets> <customwidgets>
<customwidget>
<class>StyledElidedLabel</class>
<extends>QLabel</extends>
<header>gui/common/StyledElidedLabel.h</header>
</customwidget>
<customwidget> <customwidget>
<class>LineEditClear</class> <class>LineEditClear</class>
<extends>QLineEdit</extends> <extends>QLineEdit</extends>
@ -328,11 +330,6 @@
<header>gui/gxschannels/GxsChannelFilesWidget.h</header> <header>gui/gxschannels/GxsChannelFilesWidget.h</header>
<container>1</container> <container>1</container>
</customwidget> </customwidget>
<customwidget>
<class>StyledLabel</class>
<extends>QLabel</extends>
<header>gui/common/StyledLabel.h</header>
</customwidget>
</customwidgets> </customwidgets>
<resources> <resources>
<include location="../images.qrc"/> <include location="../images.qrc"/>

View file

@ -480,6 +480,7 @@ HEADERS += rshare.h \
gui/common/FlowLayout.h \ gui/common/FlowLayout.h \
gui/common/PictureFlow.h \ gui/common/PictureFlow.h \
gui/common/StyledLabel.h \ gui/common/StyledLabel.h \
gui/common/StyledElidedLabel.h \
gui/style/RSStyle.h \ gui/style/RSStyle.h \
gui/style/StyleDialog.h \ gui/style/StyleDialog.h \
gui/MessagesDialog.h \ gui/MessagesDialog.h \
@ -778,6 +779,7 @@ SOURCES += main.cpp \
gui/common/FlowLayout.cpp \ gui/common/FlowLayout.cpp \
gui/common/PictureFlow.cpp \ gui/common/PictureFlow.cpp \
gui/common/StyledLabel.cpp \ gui/common/StyledLabel.cpp \
gui/common/StyledElidedLabel.cpp \
gui/style/RSStyle.cpp \ gui/style/RSStyle.cpp \
gui/style/StyleDialog.cpp \ gui/style/StyleDialog.cpp \
gui/settings/rsharesettings.cpp \ gui/settings/rsharesettings.cpp \