Added changes from braindead:

- simplified avatarwidget
- prepared property-dependend stylesheets

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@5176 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2012-05-19 22:18:15 +00:00
parent ce22ab5be9
commit b344aebbe6
6 changed files with 107 additions and 148 deletions

View File

@ -16,9 +16,6 @@
<property name="windowTitle"> <property name="windowTitle">
<string notr="true">MainWindow</string> <string notr="true">MainWindow</string>
</property> </property>
<property name="styleSheet">
<string notr="true"/>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3"> <layout class="QVBoxLayout" name="verticalLayout_3">
<property name="margin"> <property name="margin">
<number>0</number> <number>0</number>
@ -116,15 +113,6 @@
<height>16777215</height> <height>16777215</height>
</size> </size>
</property> </property>
<property name="styleSheet">
<string notr="true">border: transparent;</string>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2"> <layout class="QVBoxLayout" name="verticalLayout_2">
<property name="leftMargin"> <property name="leftMargin">
<number>0</number> <number>0</number>
@ -133,7 +121,7 @@
<number>0</number> <number>0</number>
</property> </property>
<item> <item>
<widget class="AvatarWidget" name="avatarWidget" native="true"> <widget class="AvatarWidget" name="avatarWidget">
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>116</width> <width>116</width>
@ -162,7 +150,7 @@
</spacer> </spacer>
</item> </item>
<item> <item>
<widget class="AvatarWidget" name="ownAvatarWidget" native="true"> <widget class="AvatarWidget" name="ownAvatarWidget">
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>116</width> <width>116</width>
@ -215,8 +203,8 @@
</customwidget> </customwidget>
<customwidget> <customwidget>
<class>AvatarWidget</class> <class>AvatarWidget</class>
<extends>QWidget</extends> <extends>QLabel</extends>
<header>gui/common/AvatarWidget.h</header> <header>gui/common/avatarwidget.h</header>
<container>1</container> <container>1</container>
</customwidget> </customwidget>
</customwidgets> </customwidgets>

View File

@ -19,6 +19,7 @@
* Boston, MA 02110-1301, USA. * Boston, MA 02110-1301, USA.
****************************************************************/ ****************************************************************/
#include <rshare.h>
#include <retroshare/rsstatus.h> #include <retroshare/rsstatus.h>
#include <retroshare/rspeers.h> #include <retroshare/rspeers.h>
#include <retroshare/rsmsgs.h> #include <retroshare/rsmsgs.h>
@ -33,7 +34,7 @@
#include <algorithm> #include <algorithm>
AvatarWidget::AvatarWidget(QWidget *parent) : AvatarWidget::AvatarWidget(QWidget *parent) :
QWidget(parent), ui(new Ui::AvatarWidget) QLabel(parent), ui(new Ui::AvatarWidget)
{ {
ui->setupUi(this); ui->setupUi(this);
@ -53,33 +54,30 @@ AvatarWidget::~AvatarWidget()
delete ui; delete ui;
} }
static bool isSmall(const QSize &size) QString AvatarWidget::frameState()
{ {
if (size.width() <= 70 && size.height() <= 70) { switch (mFrameType)
return true; {
case NO_FRAME:
return "NOTHING";
case NORMAL_FRAME:
return "NORMAL";
case STATUS_FRAME:
switch (mPeerState)
{
case RS_STATUS_OFFLINE:
return "OFFLINE";
case RS_STATUS_INACTIVE:
return "INACTIVE";
case RS_STATUS_ONLINE:
return "ONLINE";
case RS_STATUS_AWAY:
return "AWAY";
case RS_STATUS_BUSY:
return "BUSY";
}
} }
return "NOTHING";
return false;
}
void AvatarWidget::resizeEvent(QResizeEvent */*event*/)
{
if (mFrameType == NO_FRAME) {
return;
}
QSize widgetSize = size();
QSize avatarSize;
if (isSmall(widgetSize)) {
avatarSize = QSize(widgetSize.width() * 50 / 70, widgetSize.height() * 50 / 70);
} else {
avatarSize = QSize(widgetSize.width() * 96 / 116, widgetSize.height() * 96 / 116);
}
int x = (widgetSize.width() - avatarSize.width()) / 2;
int y = (widgetSize.height() - avatarSize.height()) / 2;
ui->avatarFrameLayout->setContentsMargins(x, y, x, y);
refreshStatus();
} }
void AvatarWidget::mouseReleaseEvent(QMouseEvent */*event*/) void AvatarWidget::mouseReleaseEvent(QMouseEvent */*event*/)
@ -99,7 +97,6 @@ void AvatarWidget::setFrameType(FrameType type)
switch (mFrameType) { switch (mFrameType) {
case NO_FRAME: case NO_FRAME:
disconnect(NotifyQt::getInstance(), SIGNAL(peerStatusChanged(QString,int)), this, SLOT(updateStatus(const QString&, int))); disconnect(NotifyQt::getInstance(), SIGNAL(peerStatusChanged(QString,int)), this, SLOT(updateStatus(const QString&, int)));
ui->avatarFrameLayout->setContentsMargins(0, 0, 0, 0);
break; break;
case NORMAL_FRAME: case NORMAL_FRAME:
disconnect(NotifyQt::getInstance(), SIGNAL(peerStatusChanged(QString,int)), this, SLOT(updateStatus(const QString&, int))); disconnect(NotifyQt::getInstance(), SIGNAL(peerStatusChanged(QString,int)), this, SLOT(updateStatus(const QString&, int)));
@ -111,6 +108,7 @@ void AvatarWidget::setFrameType(FrameType type)
refreshStatus(); refreshStatus();
updateAvatar(QString::fromStdString(mId)); updateAvatar(QString::fromStdString(mId));
Rshare::refreshStyleSheet(this, false);
} }
void AvatarWidget::setId(const std::string &id, bool isGpg) void AvatarWidget::setId(const std::string &id, bool isGpg)
@ -120,13 +118,13 @@ void AvatarWidget::setId(const std::string &id, bool isGpg)
if (mId == rsPeers->getOwnId()) { if (mId == rsPeers->getOwnId()) {
mFlag.isOwnId = true; mFlag.isOwnId = true;
ui->avatar->setToolTip(tr("Click to change your avatar")); setToolTip(tr("Click to change your avatar"));
} }
ui->avatar->setPixmap(QPixmap()); setPixmap(QPixmap());
if (mId.empty()) { if (mId.empty()) {
ui->avatar->setEnabled(false); setEnabled(false);
} }
refreshStatus(); refreshStatus();
@ -148,62 +146,25 @@ void AvatarWidget::refreshStatus()
{ {
switch (mFrameType) { switch (mFrameType) {
case NO_FRAME: case NO_FRAME:
ui->avatarFrame->setStyleSheet("");
break;
case NORMAL_FRAME: case NORMAL_FRAME:
ui->avatarFrame->setStyleSheet(isSmall(size()) ? "QFrame#avatarFrame{ border-image:url(:/images/avatarstatus-bg-70.png); }" : "QFrame#avatarFrame{ border-image:url(:/images/avatarstatus-bg-116.png); }"); {
Rshare::refreshStyleSheet(this, false);
break; break;
}
case STATUS_FRAME: case STATUS_FRAME:
{ {
StatusInfo statusInfo; StatusInfo statusInfo;
if (mFlag.isOwnId) { if (mFlag.isOwnId) {
rsStatus->getOwnStatus(statusInfo); rsStatus->getOwnStatus(statusInfo);
} else { } else {
// No check of return value. Non existing status info is handled as offline. // No check of return value. Non existing status info is handled as offline.
rsStatus->getStatus(mId, statusInfo); rsStatus->getStatus(mId, statusInfo);
}
updateStatus(QString::fromStdString(statusInfo.id), statusInfo.status);
} }
updateStatus(QString::fromStdString(statusInfo.id), statusInfo.status);
break; break;
} }
}
static QString getStatusFrame(bool small, int status)
{
if (small) {
switch (status) {
case RS_STATUS_OFFLINE:
return "QFrame#avatarFrame{ border-image:url(:/images/avatarstatus-bg-offline-70.png); }";
case RS_STATUS_INACTIVE:
return "QFrame#avatarFrame{ border-image:url(:/images/avatarstatus-bg-idle-70.png); }";
case RS_STATUS_ONLINE:
return "QFrame#avatarFrame{ border-image:url(:/images/avatarstatus-bg-online-70.png); }";
case RS_STATUS_AWAY:
return "QFrame#avatarFrame{ border-image:url(:/images/avatarstatus-bg-away-70.png); }";
case RS_STATUS_BUSY:
return "QFrame#avatarFrame{ border-image:url(:/images/avatarstatus-bg-busy-70.png); }";
}
return "QFrame#avatarFrame{ border-image:url(:/images/avatarstatus-bg-70.png); }";
} else {
switch (status) {
case RS_STATUS_OFFLINE:
return "QFrame#avatarFrame{ border-image:url(:/images/avatarstatus-bg-offline-116.png); }";
case RS_STATUS_INACTIVE:
return "QFrame#avatarFrame{ border-image:url(:/images/avatarstatus-bg-idle-116.png); }";
case RS_STATUS_ONLINE:
return "QFrame#avatarFrame{ border-image:url(:/images/avatarstatus-bg-online-116.png); }";
case RS_STATUS_AWAY:
return "QFrame#avatarFrame{ border-image:url(:/images/avatarstatus-bg-away-116.png); }";
case RS_STATUS_BUSY:
return "QFrame#avatarFrame{ border-image:url(:/images/avatarstatus-bg-busy-116.png); }";
}
return "QFrame#avatarFrame{ border-image:url(:/images/avatarstatus-bg-116.png); }";
} }
return "";
} }
void AvatarWidget::updateStatus(const QString peerId, int status) void AvatarWidget::updateStatus(const QString peerId, int status)
@ -213,36 +174,31 @@ void AvatarWidget::updateStatus(const QString peerId, int status)
} }
if (mId.empty()) { if (mId.empty()) {
ui->avatarFrame->setStyleSheet(getStatusFrame(isSmall(size()), RS_STATUS_OFFLINE)); mPeerState = status;
return; Rshare::refreshStyleSheet(this, false);
} else {
/* set style for status */
if (mId == peerId.toStdString()) {
// the peers status has changed
mPeerState = status;
setEnabled(((uint32_t) status == RS_STATUS_OFFLINE) ? false : true);
Rshare::refreshStyleSheet(this, false);
}
} }
/* set style for status */
if (mId == peerId.toStdString()) {
// the peers status has changed
ui->avatarFrame->setStyleSheet(getStatusFrame(isSmall(size()), status));
ui->avatar->setEnabled(((uint32_t) status == RS_STATUS_OFFLINE) ? false : true);
return;
}
// ignore status change
} }
void AvatarWidget::updateAvatar(const QString &peerId) void AvatarWidget::updateAvatar(const QString &peerId)
{ {
if (mId.empty()) { if (mId.empty()) {
QPixmap avatar(defaultAvatar); QPixmap avatar(defaultAvatar);
ui->avatar->setPixmap(avatar); setPixmap(avatar);
return; return;
} }
if (mFlag.isOwnId) { if (mFlag.isOwnId) {
QPixmap avatar; QPixmap avatar;
AvatarDefs::getOwnAvatar(avatar); AvatarDefs::getOwnAvatar(avatar);
ui->avatar->setPixmap(avatar); setPixmap(avatar);
return; return;
} }
@ -251,7 +207,7 @@ void AvatarWidget::updateAvatar(const QString &peerId)
/* called from AvatarWidget with gpg id */ /* called from AvatarWidget with gpg id */
QPixmap avatar; QPixmap avatar;
AvatarDefs::getAvatarFromGpgId(mId, avatar, defaultAvatar); AvatarDefs::getAvatarFromGpgId(mId, avatar, defaultAvatar);
ui->avatar->setPixmap(avatar); setPixmap(avatar);
return; return;
} }
@ -265,7 +221,7 @@ void AvatarWidget::updateAvatar(const QString &peerId)
/* One of the ssl ids of the gpg id */ /* One of the ssl ids of the gpg id */
QPixmap avatar; QPixmap avatar;
AvatarDefs::getAvatarFromGpgId(mId, avatar, defaultAvatar); AvatarDefs::getAvatarFromGpgId(mId, avatar, defaultAvatar);
ui->avatar->setPixmap(avatar); setPixmap(avatar);
} }
return; return;
@ -274,7 +230,7 @@ void AvatarWidget::updateAvatar(const QString &peerId)
if (mId == peerId.toStdString()) { if (mId == peerId.toStdString()) {
QPixmap avatar; QPixmap avatar;
AvatarDefs::getAvatarFromSslId(mId, avatar, defaultAvatar); AvatarDefs::getAvatarFromSslId(mId, avatar, defaultAvatar);
ui->avatar->setPixmap(avatar); setPixmap(avatar);
return; return;
} }
} }

View File

@ -22,15 +22,16 @@
#ifndef AVATARWIDGET_H #ifndef AVATARWIDGET_H
#define AVATARWIDGET_H #define AVATARWIDGET_H
#include <QWidget> #include <QLabel>
namespace Ui { namespace Ui {
class AvatarWidget; class AvatarWidget;
} }
class AvatarWidget : public QWidget class AvatarWidget : public QLabel
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString frameState READ frameState)
public: public:
enum FrameType { enum FrameType {
@ -43,13 +44,13 @@ public:
AvatarWidget(QWidget *parent = 0); AvatarWidget(QWidget *parent = 0);
~AvatarWidget(); ~AvatarWidget();
QString frameState();
void setFrameType(FrameType type); void setFrameType(FrameType type);
void setId(const std::string& id, bool isGpg); void setId(const std::string& id, bool isGpg);
void setOwnId(); void setOwnId();
void setDefaultAvatar(const QString &avatar); void setDefaultAvatar(const QString &avatar);
protected: protected:
void resizeEvent(QResizeEvent *event);
void mouseReleaseEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event);
private slots: private slots:
@ -69,6 +70,7 @@ private:
bool isGpg : 1; bool isGpg : 1;
} mFlag; } mFlag;
FrameType mFrameType; FrameType mFrameType;
uint32_t mPeerState;
}; };
#endif // AVATARWIDGET_H #endif // AVATARWIDGET_H

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"> <ui version="4.0">
<class>AvatarWidget</class> <class>AvatarWidget</class>
<widget class="QWidget" name="AvatarWidget"> <widget class="QLabel" name="AvatarWidget">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>
@ -10,36 +10,21 @@
<height>116</height> <height>116</height>
</rect> </rect>
</property> </property>
<layout class="QHBoxLayout" name="horizontalLayout"> <property name="styleSheet">
<property name="margin"> <string notr="true">AvatarWidget{border-width: 10px;}
<number>0</number> AvatarWidget[frameState=&quot;NORMAL&quot;]{border-image:url(:/images/avatarstatus-bg-116.png);}
</property> AvatarWidget[frameState=&quot;OFFLINE&quot;]{border-image:url(:/images/avatarstatus-bg-offline-116.png);}
<item> AvatarWidget[frameState=&quot;INACTIVE&quot;]{border-image:url(:/images/avatarstatus-bg-idle-116.png);}
<widget class="QFrame" name="avatarFrame"> AvatarWidget[frameState=&quot;ONLINE&quot;]{border-image:url(:/images/avatarstatus-bg-online-116.png);}
<property name="styleSheet"> AvatarWidget[frameState=&quot;AWAY&quot;]{border-image:url(:/images/avatarstatus-bg-away-116.png);}
<string notr="true">QFrame#avatarFrame{ border-image:url(:/images/avatarstatus-bg-116.png); }</string> AvatarWidget[frameState=&quot;BUSY&quot;]{border-image:url(:/images/avatarstatus-bg-busy-116.png);}</string>
</property> </property>
<layout class="QHBoxLayout" name="avatarFrameLayout"> <property name="scaledContents">
<property name="margin"> <bool>true</bool>
<number>10</number> </property>
</property> <property name="alignment">
<item> <set>Qt::AlignCenter</set>
<widget class="QLabel" name="avatar"> </property>
<property name="enabled">
<bool>true</bool>
</property>
<property name="scaledContents">
<bool>true</bool>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget> </widget>
<resources/> <resources/>
<connections/> <connections/>

View File

@ -27,6 +27,7 @@
#include <QTextStream> #include <QTextStream>
#include <QShortcut> #include <QShortcut>
#include <QStyleFactory> #include <QStyleFactory>
#include <QStyle>
#include <gui/common/vmessagebox.h> #include <gui/common/vmessagebox.h>
#include <gui/common/html.h> #include <gui/common/html.h>
#include <util/stringutil.h> #include <util/stringutil.h>
@ -374,6 +375,31 @@ void Rshare::loadStyleSheet(const QString &sheetName)
qApp->setStyleSheet(styleSheet); qApp->setStyleSheet(styleSheet);
} }
void Rshare::refreshStyleSheet(QWidget *widget, bool processChildren)
{
if (widget != NULL) {
// force widget to recalculate valid style
widget->style()->unpolish(widget);
widget->style()->polish(widget);
// widget might need to recalculate properties (like margins) depending on style
QEvent event(QEvent::StyleChange);
QApplication::sendEvent(widget, &event);
// repaint widget
widget->update();
if (processChildren == true) {
// process children recursively
QObjectList childList = widget->children ();
for (QObjectList::Iterator it = childList.begin(); it != childList.end(); it++) {
QWidget *child = qobject_cast<QWidget*>(*it);
if (child != NULL) {
refreshStyleSheet(child, processChildren);
}
}
}
}
}
/** Initialize plugins. */ /** Initialize plugins. */
void Rshare::initPlugins() void Rshare::initPlugins()
{ {

View File

@ -79,6 +79,8 @@ public:
static bool setSheet(QString sheet = QString()); static bool setSheet(QString sheet = QString());
/** Loads stylesheet from external file **/ /** Loads stylesheet from external file **/
static void loadStyleSheet(const QString &sheetName); static void loadStyleSheet(const QString &sheetName);
/** Recalculates matching stylesheet for widget **/
static void refreshStyleSheet(QWidget *widget, bool processChildren);
/** /**
* Update Language, Style and StyleSheet. * Update Language, Style and StyleSheet.