qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
notificationscrollarea.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 #include "notificationscrollarea.h"
20 #include "genericchatroomwidget.h"
21 #include "notificationedgewidget.h"
22 #include <QScrollBar>
23 #include <cassert>
24 
26  : AdjustingScrollArea(parent)
27 {
28  connect(verticalScrollBar(), &QAbstractSlider::valueChanged, this,
30  connect(verticalScrollBar(), &QAbstractSlider::rangeChanged, this,
32 }
33 
35 {
36  if (trackedWidgets.find(widget) != trackedWidgets.end())
37  return;
38 
39  Visibility visibility = widgetVisible(widget);
40  if (visibility != Visible) {
41  if (visibility == Above) {
42  if (referencesAbove++ == 0) {
43  assert(topEdge == nullptr);
48  topEdge->show();
49  }
51  } else {
52  if (referencesBelow++ == 0) {
53  assert(bottomEdge == nullptr);
58  bottomEdge->show();
59  }
61  }
62 
63  trackedWidgets.insert(widget, visibility);
64  }
65 }
66 
71 {
72  updateTracking(nullptr);
73 }
74 
80 {
82  while (i != trackedWidgets.end()) {
83  if (i.key() == widget || widgetVisible(i.key()) == Visible) {
84  if (i.value() == Above) {
85  if (--referencesAbove == 0) {
86  topEdge->deleteLater();
87  topEdge = nullptr;
88  } else {
90  }
91  } else {
92  if (--referencesBelow == 0) {
93  bottomEdge->deleteLater();
94  bottomEdge = nullptr;
95  } else {
97  }
98  }
99  i = trackedWidgets.erase(i);
100  continue;
101  }
102  ++i;
103  }
104 }
105 
106 void NotificationScrollArea::resizeEvent(QResizeEvent* event)
107 {
108  if (topEdge != nullptr)
110  if (bottomEdge != nullptr)
112 
114 }
115 
117 {
118  int value = 0;
119  GenericChatroomWidget* next = nullptr;
121 
122  // Find the first next, to avoid nullptr.
123  for (; i != trackedWidgets.end(); ++i) {
124  if (i.value() == Below) {
125  next = i.key();
126  value = next->mapTo(viewport(), QPoint()).y();
127  break;
128  }
129  }
130 
131  // Try finding a closer one.
132  for (; i != trackedWidgets.end(); ++i) {
133  if (i.value() == Below) {
134  int y = i.key()->mapTo(viewport(), QPoint()).y();
135  if (y < value) {
136  next = i.key();
137  value = y;
138  }
139  }
140  }
141 
142  if (next != nullptr)
143  ensureWidgetVisible(next, 0, referencesBelow != 1 ? bottomEdge->height() : 0);
144 }
145 
147 {
148  int value = 0;
149  GenericChatroomWidget* next = nullptr;
151 
152  // Find the first next, to avoid nullptr.
153  for (; i != trackedWidgets.end(); ++i) {
154  if (i.value() == Above) {
155  next = i.key();
156  value = next->mapTo(viewport(), QPoint()).y();
157  break;
158  }
159  }
160 
161  // Try finding a closer one.
162  for (; i != trackedWidgets.end(); ++i) {
163  if (i.value() == Above) {
164  int y = i.key()->mapTo(viewport(), QPoint()).y();
165  if (y > value) {
166  next = i.key();
167  value = y;
168  }
169  }
170  }
171 
172  if (next != nullptr)
173  ensureWidgetVisible(next, 0, referencesAbove != 1 ? topEdge->height() : 0);
174 }
175 
177 {
178  int y = widget->mapTo(viewport(), QPoint()).y();
179 
180  if (y < 0)
181  return Above;
182  else if (y + widget->height() - 1 > viewport()->height())
183  return Below;
184 
185  return Visible;
186 }
187 
189 {
190  topEdge->move(viewport()->pos());
191  topEdge->resize(viewport()->width(), topEdge->height());
192 }
193 
195 {
196  QPoint position = viewport()->pos();
197  position.setY(position.y() + viewport()->height() - bottomEdge->height());
198  bottomEdge->move(position);
199  bottomEdge->resize(viewport()->width(), bottomEdge->height());
200 }
AdjustingScrollArea
Definition: adjustingscrollarea.h:24
NotificationScrollArea::findNextWidget
void findNextWidget()
Definition: notificationscrollarea.cpp:116
NotificationScrollArea::referencesAbove
size_t referencesAbove
Definition: notificationscrollarea.h:59
genericchatroomwidget.h
NotificationScrollArea::recalculateBottomEdge
void recalculateBottomEdge()
Definition: notificationscrollarea.cpp:194
NotificationEdgeWidget
Definition: notificationedgewidget.h:26
NotificationScrollArea::Visible
@ Visible
Definition: notificationscrollarea.h:48
notificationedgewidget.h
NotificationScrollArea::findPreviousWidget
void findPreviousWidget()
Definition: notificationscrollarea.cpp:146
NotificationScrollArea::updateVisualTracking
void updateVisualTracking()
Delete notification bar from visible elements on scroll area.
Definition: notificationscrollarea.cpp:70
QHash
Definition: friendlist.h:27
NotificationScrollArea::Visibility
Visibility
Definition: notificationscrollarea.h:46
AdjustingScrollArea::resizeEvent
void resizeEvent(QResizeEvent *ev) override
Definition: adjustingscrollarea.cpp:32
NotificationEdgeWidget::updateNotificationCount
void updateNotificationCount(int count)
Definition: notificationedgewidget.cpp:52
NotificationScrollArea::widgetVisible
Visibility widgetVisible(QWidget *widget) const
Definition: notificationscrollarea.cpp:176
NotificationScrollArea::topEdge
NotificationEdgeWidget * topEdge
Definition: notificationscrollarea.h:57
NotificationEdgeWidget::Top
@ Top
Definition: notificationedgewidget.h:32
NotificationEdgeWidget::Bottom
@ Bottom
Definition: notificationedgewidget.h:33
NotificationScrollArea::trackedWidgets
QHash< GenericChatroomWidget *, Visibility > trackedWidgets
Definition: notificationscrollarea.h:56
NotificationScrollArea::Above
@ Above
Definition: notificationscrollarea.h:49
NotificationEdgeWidget::clicked
void clicked()
NotificationScrollArea::recalculateTopEdge
void recalculateTopEdge()
Definition: notificationscrollarea.cpp:188
NotificationScrollArea::bottomEdge
NotificationEdgeWidget * bottomEdge
Definition: notificationscrollarea.h:58
NotificationScrollArea::resizeEvent
void resizeEvent(QResizeEvent *event) final
Definition: notificationscrollarea.cpp:106
NotificationScrollArea::Below
@ Below
Definition: notificationscrollarea.h:50
notificationscrollarea.h
GenericChatroomWidget
Definition: genericchatroomwidget.h:32
NotificationScrollArea::trackWidget
void trackWidget(GenericChatroomWidget *widget)
Definition: notificationscrollarea.cpp:34
NotificationScrollArea::updateTracking
void updateTracking(GenericChatroomWidget *widget)
Delete notification bar from visible elements and widget on scroll area.
Definition: notificationscrollarea.cpp:79
NotificationScrollArea::NotificationScrollArea
NotificationScrollArea(QWidget *parent=nullptr)
Definition: notificationscrollarea.cpp:25
NotificationScrollArea::referencesBelow
size_t referencesBelow
Definition: notificationscrollarea.h:60