qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
genericchatroomwidget.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2014-2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "genericchatroomwidget.h"
21 #include "maskablepixmapwidget.h"
23 #include "src/widget/style.h"
25 #include <QBoxLayout>
26 #include <QMouseEvent>
27 
28 GenericChatroomWidget::GenericChatroomWidget(bool compact, QWidget* parent)
29  : GenericChatItemWidget(compact, parent)
30  , active{false}
31 {
32  // avatar
33  QSize size;
34  if (isCompact())
35  size = QSize(20, 20);
36  else
37  size = QSize(40, 40);
38 
39  avatar = new MaskablePixmapWidget(this, size, ":/img/avatar_mask.svg");
40 
41  // status text
42  statusMessageLabel = new CroppingLabel(this);
43  statusMessageLabel->setTextFormat(Qt::PlainText);
44  statusMessageLabel->setForegroundRole(QPalette::WindowText);
45  statusMessageLabel->setObjectName("statusMessageLabelObj");
46 
47  nameLabel->setForegroundRole(QPalette::WindowText);
48  nameLabel->setObjectName("nameLabelObj");
49 
52 
53  setAutoFillBackground(true);
54  reloadTheme();
55 
56  compactChange(isCompact());
57 }
58 
59 bool GenericChatroomWidget::eventFilter(QObject*, QEvent*)
60 {
61  return true; // Disable all events.
62 }
63 
65 {
66  if (!isCompact())
67  delete textLayout; // has to be first, deleted by layout
68 
69  setCompact(_compact);
70 
71  delete mainLayout;
72 
73  mainLayout = new QHBoxLayout;
74  textLayout = new QVBoxLayout;
75 
76  setLayout(mainLayout);
77  mainLayout->setSpacing(0);
78  mainLayout->setMargin(0);
79  textLayout->setSpacing(0);
80  textLayout->setMargin(0);
81  setLayoutDirection(Qt::LeftToRight); // parent might have set Qt::RightToLeft
82 
83  // avatar
84  if (isCompact()) {
85  delete textLayout; // Not needed
86  setFixedHeight(25);
87  avatar->setSize(QSize(20, 20));
88  mainLayout->addSpacing(18);
89  mainLayout->addWidget(avatar);
90  mainLayout->addSpacing(5);
91  mainLayout->addWidget(nameLabel);
92  mainLayout->addWidget(statusMessageLabel);
93  mainLayout->addSpacing(5);
94  mainLayout->addWidget(&statusPic);
95  mainLayout->addSpacing(5);
96  mainLayout->activate();
99  } else {
100  setFixedHeight(55);
101  avatar->setSize(QSize(40, 40));
102  textLayout->addStretch();
103  textLayout->addWidget(nameLabel);
104  textLayout->addWidget(statusMessageLabel);
105  textLayout->addStretch();
106  mainLayout->addSpacing(20);
107  mainLayout->addWidget(avatar);
108  mainLayout->addSpacing(10);
109  mainLayout->addLayout(textLayout);
110  mainLayout->addSpacing(10);
111  mainLayout->addWidget(&statusPic);
112  mainLayout->addSpacing(10);
113  mainLayout->activate();
116  }
117 }
118 
120 {
121  return active;
122 }
123 
125 {
126  active = _active;
127 
128  setProperty("active", active);
129  nameLabel->setProperty("active", active);
130  statusMessageLabel->setProperty("active", active);
131  Style::repolish(this);
132 }
133 
134 void GenericChatroomWidget::setName(const QString& name)
135 {
136  nameLabel->setText(name);
137 }
138 
139 void GenericChatroomWidget::setStatusMsg(const QString& status)
140 {
141  statusMessageLabel->setText(status);
142 }
143 
145 {
146  return statusMessageLabel->text();
147 }
148 
150 {
151  QString title = getName();
152 
153  if (!getStatusString().isNull())
154  title += QStringLiteral(" (") + getStatusString() + QStringLiteral(")");
155 
156  return title;
157 }
158 
160 {
161  setStyleSheet(Style::getStylesheet("genericChatRoomWidget/genericChatRoomWidget.css"));
162 }
163 
165 {
166  emit chatroomWidgetClicked(this);
167 }
168 
170 {
171  if (event->button() == Qt::LeftButton) {
172  emit chatroomWidgetClicked(this);
173  } else if (event->button() == Qt::MiddleButton) {
174  emit middleMouseClicked();
175  } else {
176  event->ignore();
177  }
178 }
179 
181 {
182  if (!active)
183  setBackgroundRole(QPalette::Highlight);
184 }
185 
187 {
188  if (!active)
189  setBackgroundRole(QPalette::Window);
190  QWidget::leaveEvent(event);
191 }
Style::getFont
static QFont getFont(Font font)
Definition: style.cpp:214
GenericChatroomWidget::avatar
MaskablePixmapWidget * avatar
Definition: genericchatroomwidget.h:83
Settings
Definition: settings.h:51
GenericChatroomWidget::reloadTheme
void reloadTheme() override
Definition: genericchatroomwidget.cpp:159
GenericChatroomWidget::setName
void setName(const QString &name)
Definition: genericchatroomwidget.cpp:134
GenericChatItemWidget::getName
QString getName() const
Definition: genericchatitemwidget.cpp:49
style.h
genericchatroomwidget.h
settings.h
GenericChatroomWidget::isActive
bool isActive()
Definition: genericchatroomwidget.cpp:119
GenericChatroomWidget::mouseReleaseEvent
void mouseReleaseEvent(QMouseEvent *event) override
Definition: genericchatroomwidget.cpp:169
GenericChatroomWidget::mainLayout
QHBoxLayout * mainLayout
Definition: genericchatroomwidget.h:81
CroppingLabel
Definition: croppinglabel.h:26
GenericChatroomWidget::setActive
void setActive(bool active)
Definition: genericchatroomwidget.cpp:124
GenericChatroomWidget::setStatusMsg
void setStatusMsg(const QString &status)
Definition: genericchatroomwidget.cpp:139
CroppingLabel::setText
void setText(const QString &text)
Definition: croppinglabel.cpp:81
GenericChatroomWidget::enterEvent
void enterEvent(QEvent *e) override
Definition: genericchatroomwidget.cpp:180
GenericChatroomWidget::leaveEvent
void leaveEvent(QEvent *e) override
Definition: genericchatroomwidget.cpp:186
GenericChatItemWidget
Definition: genericchatitemwidget.h:27
croppinglabel.h
GenericChatItemWidget::statusPic
QLabel statusPic
Definition: genericchatitemwidget.h:54
MaskablePixmapWidget
Definition: maskablepixmapwidget.h:24
GenericChatItemWidget::nameLabel
CroppingLabel * nameLabel
Definition: genericchatitemwidget.h:53
GenericChatroomWidget::GenericChatroomWidget
GenericChatroomWidget(bool compact, QWidget *parent=nullptr)
Definition: genericchatroomwidget.cpp:28
Style::Big
@ Big
Definition: style.h:57
maskablepixmapwidget.h
Style::repolish
static void repolish(QWidget *w)
Definition: style.cpp:319
GenericChatroomWidget::middleMouseClicked
void middleMouseClicked()
GenericChatItemWidget::setCompact
void setCompact(bool compact)
Definition: genericchatitemwidget.cpp:44
Settings::getInstance
static Settings & getInstance()
Returns the singleton instance.
Definition: settings.cpp:88
GenericChatroomWidget::chatroomWidgetClicked
void chatroomWidgetClicked(GenericChatroomWidget *widget)
GenericChatroomWidget::textLayout
QVBoxLayout * textLayout
Definition: genericchatroomwidget.h:82
Style::getStylesheet
static const QString getStylesheet(const QString &filename, const QFont &baseFont=QFont())
Definition: style.cpp:165
GenericChatroomWidget::eventFilter
bool eventFilter(QObject *, QEvent *) final
Definition: genericchatroomwidget.cpp:59
GenericChatroomWidget::compactChange
void compactChange(bool compact)
Definition: genericchatroomwidget.cpp:64
GenericChatroomWidget::getStatusString
virtual QString getStatusString() const =0
GenericChatroomWidget::active
bool active
Definition: genericchatroomwidget.h:85
GenericChatroomWidget::activate
void activate()
Definition: genericchatroomwidget.cpp:164
Settings::compactLayoutChanged
void compactLayoutChanged(bool enabled)
GenericChatItemWidget::isCompact
bool isCompact() const
Definition: genericchatitemwidget.cpp:39
Style::Small
@ Small
Definition: style.h:61
MaskablePixmapWidget::setSize
void setSize(QSize size)
Definition: maskablepixmapwidget.cpp:73
Style::Medium
@ Medium
Definition: style.h:59
GenericChatroomWidget::statusMessageLabel
CroppingLabel * statusMessageLabel
Definition: genericchatroomwidget.h:84
GenericChatroomWidget::getTitle
QString getTitle() const
Definition: genericchatroomwidget.cpp:149
GenericChatroomWidget::getStatusMsg
QString getStatusMsg() const
Definition: genericchatroomwidget.cpp:144