qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
categorywidget.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2015-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 "categorywidget.h"
21 #include "friendlistlayout.h"
22 #include "friendlistwidget.h"
23 #include "friendwidget.h"
24 #include "src/model/status.h"
25 #include "src/widget/style.h"
26 #include "tool/croppinglabel.h"
27 #include <QBoxLayout>
28 #include <QMouseEvent>
29 
30 #include <QApplication>
31 
32 void CategoryWidget::emitChatroomWidget(QLayout* layout, int index)
33 {
34  QWidget* widget = layout->itemAt(index)->widget();
35  GenericChatroomWidget* chatWidget = qobject_cast<GenericChatroomWidget*>(widget);
36  if (chatWidget != nullptr) {
37  emit chatWidget->chatroomWidgetClicked(chatWidget);
38  }
39 }
40 
41 CategoryWidget::CategoryWidget(bool compact, QWidget* parent)
42  : GenericChatItemWidget(compact, parent)
43 {
44  container = new QWidget(this);
45  container->setObjectName("circleWidgetContainer");
46  container->setLayoutDirection(Qt::LeftToRight);
47 
48  statusLabel = new QLabel(this);
49  statusLabel->setObjectName("status");
50  statusLabel->setTextFormat(Qt::PlainText);
51 
52  statusPic.setPixmap(QPixmap(Style::getImagePath("chatArea/scrollBarRightArrow.svg")));
53 
54  fullLayout = new QVBoxLayout(this);
55  fullLayout->setSpacing(0);
56  fullLayout->setMargin(0);
57  fullLayout->addWidget(container);
58 
59  lineFrame = new QFrame(container);
60  lineFrame->setObjectName("line");
61  lineFrame->setFrameShape(QFrame::HLine);
62  lineFrame->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
63  lineFrame->resize(0, 0);
64 
66  listWidget = new QWidget(this);
67  listWidget->setLayout(listLayout);
68  fullLayout->addWidget(listWidget);
69 
70  setAcceptDrops(true);
71 
73 
74  setExpanded(true, false);
75  updateStatus();
76 }
77 
79 {
80  return expanded;
81 }
82 
83 void CategoryWidget::setExpanded(bool isExpanded, bool save)
84 {
85  if (expanded == isExpanded) {
86  return;
87  }
89  setMouseTracking(true);
90 
91  // The listWidget will recieve a enterEvent for some reason if now visible.
92  // Using the following, we prevent that.
93  listWidget->setAttribute(Qt::WA_TransparentForMouseEvents, true);
94  listWidget->setVisible(isExpanded);
95  listWidget->setAttribute(Qt::WA_TransparentForMouseEvents, false);
96 
97  QString pixmapPath;
98  if (isExpanded)
99  pixmapPath = Style::getImagePath("chatArea/scrollBarDownArrow.svg");
100  else
101  pixmapPath = Style::getImagePath("chatArea/scrollBarRightArrow.svg");
102  statusPic.setPixmap(QPixmap(pixmapPath));
103 
104  if (save)
105  onExpand();
106 }
107 
108 void CategoryWidget::leaveEvent(QEvent* event)
109 {
110  event->ignore();
111 }
112 
113 void CategoryWidget::setName(const QString& name, bool save)
114 {
115  nameLabel->setText(name);
116 
117  if (isCompact())
119 
120  if (save)
121  onSetName();
122 }
123 
125 {
126  nameLabel->editBegin();
127  nameLabel->setMaximumWidth(QWIDGETSIZE_MAX);
128 }
129 
131 {
133  updateStatus();
135  w->reloadTheme(); // Otherwise theme will change when moving to another circle.
136 }
137 
139 {
141  updateStatus();
142 }
143 
145 {
146  QString online = QString::number(listLayout->friendOnlineCount());
147  QString offline = QString::number(listLayout->friendTotalCount());
148  QString text = online + QStringLiteral(" / ") + offline;
149  statusLabel->setText(text);
150 }
151 
153 {
154  return listLayout->hasChatrooms();
155 }
156 
157 void CategoryWidget::search(const QString& searchString, bool updateAll, bool hideOnline,
158  bool hideOffline)
159 {
160  if (updateAll) {
161  listLayout->searchChatrooms(searchString, hideOnline, hideOffline);
162  }
163  bool inCategory = searchString.isEmpty() && !(hideOnline && hideOffline);
164  setVisible(inCategory || listLayout->hasChatrooms());
165 }
166 
168 {
169  if (listLayout->friendTotalCount() == 0) {
170  return false;
171  }
172  if (forward) {
173  if (!listLayout->getLayoutOnline()->isEmpty()) {
174  setExpanded(true);
176  return true;
177  } else if (!listLayout->getLayoutOffline()->isEmpty()) {
178  setExpanded(true);
180  return true;
181  }
182  } else {
183  if (!listLayout->getLayoutOffline()->isEmpty()) {
184  setExpanded(true);
186  listLayout->getLayoutOffline()->count() - 1);
187  return true;
188  } else if (!listLayout->getLayoutOnline()->isEmpty()) {
189  setExpanded(true);
191  listLayout->getLayoutOnline()->count() - 1);
192  return true;
193  }
194  }
195  return false;
196 }
197 
198 bool CategoryWidget::cycleContacts(FriendWidget* activeChatroomWidget, bool forward)
199 {
200  int index = -1;
201  QLayout* currentLayout = nullptr;
202 
203  FriendWidget* friendWidget = qobject_cast<FriendWidget*>(activeChatroomWidget);
204  if (friendWidget == nullptr)
205  return false;
206 
207  currentLayout = listLayout->getLayoutOnline();
208  index = listLayout->indexOfFriendWidget(friendWidget, true);
209  if (index == -1) {
210  currentLayout = listLayout->getLayoutOffline();
211  index = listLayout->indexOfFriendWidget(friendWidget, false);
212  }
213 
214  index += forward ? 1 : -1;
215  for (;;) {
216  // Bounds checking.
217  if (index < 0) {
218  if (currentLayout == listLayout->getLayoutOffline())
219  currentLayout = listLayout->getLayoutOnline();
220  else
221  return false;
222 
223  index = currentLayout->count() - 1;
224  continue;
225  } else if (index >= currentLayout->count()) {
226  if (currentLayout == listLayout->getLayoutOnline())
227  currentLayout = listLayout->getLayoutOffline();
228  else
229  return false;
230 
231  index = 0;
232  continue;
233  }
234 
235  GenericChatroomWidget* chatWidget =
236  qobject_cast<GenericChatroomWidget*>(currentLayout->itemAt(index)->widget());
237  if (chatWidget != nullptr)
238  emit chatWidget->chatroomWidgetClicked(chatWidget);
239  return true;
240  }
241 
242  return false;
243 }
244 
246 {
247  delete topLayout;
248  delete mainLayout;
249 
250  topLayout = new QHBoxLayout;
251  topLayout->setSpacing(0);
252  topLayout->setMargin(0);
253 
254  Q_UNUSED(_compact)
255  setCompact(true);
256 
258 
259  mainLayout = nullptr;
260 
261  container->setFixedHeight(25);
262  container->setLayout(topLayout);
263 
264  topLayout->addSpacing(18);
265  topLayout->addWidget(&statusPic);
266  topLayout->addSpacing(5);
267  topLayout->addWidget(nameLabel, 100);
268  topLayout->addWidget(lineFrame, 1);
269  topLayout->addSpacing(5);
270  topLayout->addWidget(statusLabel);
271  topLayout->addSpacing(5);
272  topLayout->activate();
273 
274  Style::repolish(this);
275 }
276 
277 void CategoryWidget::mouseReleaseEvent(QMouseEvent* event)
278 {
279  if (event->button() == Qt::LeftButton)
281 }
282 
283 void CategoryWidget::setContainerAttribute(Qt::WidgetAttribute attribute, bool enabled)
284 {
285  container->setAttribute(attribute, enabled);
287 }
288 
290 {
291  return listLayout->getLayoutOffline();
292 }
293 
295 {
296  return listLayout->getLayoutOnline();
297 }
298 
300 {
301  listLayout->moveFriendWidgets(friendList);
302 }
CategoryWidget::statusLabel
QLabel * statusLabel
Definition: categorywidget.h:82
GenericChatroomWidget::reloadTheme
void reloadTheme() override
Definition: genericchatroomwidget.cpp:159
Status::Status
Status
Definition: status.h:28
style.h
CategoryWidget::mouseReleaseEvent
void mouseReleaseEvent(QMouseEvent *event) final
Definition: categorywidget.cpp:277
CategoryWidget::leaveEvent
void leaveEvent(QEvent *event) final
Definition: categorywidget.cpp:108
CategoryWidget::expanded
bool expanded
Definition: categorywidget.h:85
CategoryWidget::lineFrame
QFrame * lineFrame
Definition: categorywidget.h:84
CategoryWidget::cycleContacts
bool cycleContacts(bool forward)
Definition: categorywidget.cpp:167
FriendListLayout::addFriendWidget
void addFriendWidget(FriendWidget *widget, Status::Status s)
Definition: friendlistlayout.cpp:55
CategoryWidget::container
QWidget * container
Definition: categorywidget.h:83
CategoryWidget::listWidget
QWidget * listWidget
Definition: categorywidget.h:77
FriendListLayout::friendTotalCount
int friendTotalCount() const
Definition: friendlistlayout.cpp:106
CategoryWidget::onSetName
virtual void onSetName()
Definition: categorywidget.h:67
CroppingLabel::setText
void setText(const QString &text)
Definition: croppinglabel.cpp:81
CategoryWidget::addFriendWidget
void addFriendWidget(FriendWidget *w, Status::Status s)
Definition: categorywidget.cpp:130
FriendListLayout::removeFriendWidget
void removeFriendWidget(FriendWidget *widget, Status::Status s)
Definition: friendlistlayout.cpp:68
CategoryWidget::hasChatrooms
bool hasChatrooms() const
Definition: categorywidget.cpp:152
CategoryWidget::CategoryWidget
CategoryWidget(bool compact, QWidget *parent=nullptr)
Definition: categorywidget.cpp:41
Style::getImagePath
static const QString getImagePath(const QString &filename)
Definition: style.cpp:182
CategoryWidget::topLayout
QHBoxLayout * topLayout
Definition: categorywidget.h:81
FriendListLayout::indexOfFriendWidget
int indexOfFriendWidget(GenericChatItemWidget *widget, bool online) const
Definition: friendlistlayout.cpp:76
GenericChatItemWidget
Definition: genericchatitemwidget.h:27
CroppingLabel::editBegin
void editBegin()
Definition: croppinglabel.cpp:60
croppinglabel.h
CategoryWidget::onCompactChanged
void onCompactChanged(bool compact)
Definition: categorywidget.cpp:245
GenericChatItemWidget::statusPic
QLabel statusPic
Definition: genericchatitemwidget.h:54
CategoryWidget::search
void search(const QString &searchString, bool updateAll=false, bool hideOnline=false, bool hideOffline=false)
Definition: categorywidget.cpp:157
FriendListLayout::getLayoutOnline
QLayout * getLayoutOnline() const
Definition: friendlistlayout.cpp:122
GenericChatItemWidget::nameLabel
CroppingLabel * nameLabel
Definition: genericchatitemwidget.h:53
FriendListLayout::searchChatrooms
void searchChatrooms(const QString &searchString, bool hideOnline=false, bool hideOffline=false)
Definition: friendlistlayout.cpp:116
CroppingLabel::minimizeMaximumWidth
void minimizeMaximumWidth()
Definition: croppinglabel.cpp:173
CategoryWidget::friendOfflineLayout
QLayout * friendOfflineLayout() const
Definition: categorywidget.cpp:289
FriendListLayout::moveFriendWidgets
void moveFriendWidgets(FriendListWidget *listWidget)
Definition: friendlistlayout.cpp:83
friendlistwidget.h
FriendListLayout
Definition: friendlistlayout.h:30
CategoryWidget::editName
void editName()
Definition: categorywidget.cpp:124
Style::repolish
static void repolish(QWidget *w)
Definition: style.cpp:319
CategoryWidget::onExpand
virtual void onExpand()
Definition: categorywidget.h:70
CategoryWidget::mainLayout
QVBoxLayout * mainLayout
Definition: categorywidget.h:80
FriendListWidget
Definition: friendlistwidget.h:42
GenericChatItemWidget::setCompact
void setCompact(bool compact)
Definition: genericchatitemwidget.cpp:44
CategoryWidget::fullLayout
QVBoxLayout * fullLayout
Definition: categorywidget.h:79
FriendListLayout::getLayoutOffline
QLayout * getLayoutOffline() const
Definition: friendlistlayout.cpp:127
CategoryWidget::emitChatroomWidget
void emitChatroomWidget(QLayout *layout, int index)
Definition: categorywidget.cpp:32
GenericChatroomWidget::chatroomWidgetClicked
void chatroomWidgetClicked(GenericChatroomWidget *widget)
CategoryWidget::setExpanded
void setExpanded(bool isExpanded, bool save=true)
Definition: categorywidget.cpp:83
CategoryWidget::removeFriendWidget
void removeFriendWidget(FriendWidget *w, Status::Status s)
Definition: categorywidget.cpp:138
FriendListLayout::hasChatrooms
bool hasChatrooms() const
Definition: friendlistlayout.cpp:111
CategoryWidget::updateStatus
void updateStatus()
Definition: categorywidget.cpp:144
CategoryWidget::moveFriendWidgets
void moveFriendWidgets(FriendListWidget *friendList)
Definition: categorywidget.cpp:299
GenericChatItemWidget::isCompact
bool isCompact() const
Definition: genericchatitemwidget.cpp:39
FriendListLayout::friendOnlineCount
int friendOnlineCount() const
Definition: friendlistlayout.cpp:101
GenericChatroomWidget
Definition: genericchatroomwidget.h:32
CategoryWidget::setContainerAttribute
void setContainerAttribute(Qt::WidgetAttribute attribute, bool enabled)
Definition: categorywidget.cpp:283
CategoryWidget::isExpanded
bool isExpanded() const
Definition: categorywidget.cpp:78
categorywidget.h
CategoryWidget::setName
void setName(const QString &name, bool save=true)
Definition: categorywidget.cpp:113
CategoryWidget::listLayout
FriendListLayout * listLayout
Definition: categorywidget.h:78
friendlistlayout.h
friendwidget.h
status.h
FriendWidget
Definition: friendwidget.h:33
CategoryWidget::friendOnlineLayout
QLayout * friendOnlineLayout() const
Definition: categorywidget.cpp:294
CategoryWidget::onAddFriendWidget
virtual void onAddFriendWidget(FriendWidget *)
Definition: categorywidget.h:73