summary refs log tree commit diff
path: root/src/RoomInfoListItem.cc
blob: f898994802d6bf69231b5fbd87389033ab761d52 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * nheko Copyright (C) 2017  Konstantinos Sideris <siderisk@auth.gr>
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include <QDebug>
#include <QMouseEvent>
#include <QPainter>

#include <variant.hpp>

#include "Config.h"
#include "Menu.h"
#include "Ripple.h"
#include "RippleOverlay.h"
#include "RoomInfoListItem.h"
#include "RoomSettings.h"
#include "Theme.h"

constexpr int Padding   = 7;
constexpr int IconSize  = 48;
constexpr int MaxHeight = IconSize + 2 * Padding;

constexpr int InviteBtnX = IconSize + 2 * Padding;
constexpr int InviteBtnY = IconSize / 2 + Padding;

void
RoomInfoListItem::init(QWidget *parent)
{
        setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        setMouseTracking(true);
        setAttribute(Qt::WA_Hover);

        setFixedHeight(MaxHeight);

        QPainterPath path;
        path.addRect(0, 0, parent->width(), height());

        ripple_overlay_ = new RippleOverlay(this);
        ripple_overlay_->setClipPath(path);
        ripple_overlay_->setClipping(true);
}

RoomInfoListItem::RoomInfoListItem(QString room_id,
                                   mtx::responses::InvitedRoom room,
                                   QWidget *parent)
  : QWidget(parent)
  , roomType_{RoomType::Invited}
  , invitedRoom_{std::move(room)}
  , roomId_{std::move(room_id)}
{
        init(parent);

        roomAvatar_ = QString::fromStdString(invitedRoom_.avatar());
        roomName_   = QString::fromStdString(invitedRoom_.name());
}

RoomInfoListItem::RoomInfoListItem(QSharedPointer<RoomSettings> settings,
                                   RoomState state,
                                   QString room_id,
                                   QWidget *parent)
  : QWidget(parent)
  , state_(state)
  , roomId_(room_id)
  , roomSettings_{settings}
  , isPressed_(false)
  , unreadMsgCount_(0)
{
        init(parent);

        menu_ = new Menu(this);

        toggleNotifications_ = new QAction(notificationText(), this);
        connect(toggleNotifications_, &QAction::triggered, this, [=]() {
                roomSettings_->toggleNotifications();
        });

        leaveRoom_ = new QAction(tr("Leave room"), this);
        connect(leaveRoom_, &QAction::triggered, this, [=]() { emit leaveRoom(room_id); });

        menu_->addAction(toggleNotifications_);
        menu_->addAction(leaveRoom_);
}

QString
RoomInfoListItem::notificationText()
{
        if (roomSettings_.isNull() || roomSettings_->isNotificationsEnabled())
                return QString(tr("Disable notifications"));

        return tr("Enable notifications");
}

void
RoomInfoListItem::resizeEvent(QResizeEvent *)
{
        // Update ripple's clipping path.
        QPainterPath path;
        path.addRect(0, 0, width(), height());

        ripple_overlay_->setClipPath(path);
        ripple_overlay_->setClipping(true);
}

void
RoomInfoListItem::paintEvent(QPaintEvent *event)
{
        Q_UNUSED(event);

        QPainter p(this);
        p.setRenderHint(QPainter::TextAntialiasing);
        p.setRenderHint(QPainter::SmoothPixmapTransform);
        p.setRenderHint(QPainter::Antialiasing);

        QFont font;
        font.setPixelSize(conf::fontSize);
        QFontMetrics metrics(font);

        QPen titlePen(titleColor_);
        QPen subtitlePen(subtitleColor_);

        if (isPressed_) {
                p.fillRect(rect(), highlightedBackgroundColor_);
                titlePen.setColor(highlightedTitleColor_);
                subtitlePen.setColor(highlightedSubtitleColor_);
        } else if (underMouse()) {
                p.fillRect(rect(), hoverBackgroundColor_);
        } else {
                p.fillRect(rect(), backgroundColor_);
        }

        QRect avatarRegion(Padding, Padding, IconSize, IconSize);

        // Description line with the default font.
        int bottom_y = MaxHeight - Padding - Padding / 3 - metrics.ascent() / 2;

        if (width() > ui::sidebar::SmallSize) {
                font.setPixelSize(conf::roomlist::fonts::heading);
                font.setWeight(60);
                p.setFont(font);
                p.setPen(titlePen);

                // Name line.
                QFontMetrics fontNameMetrics(font);
                int top_y = 2 * Padding + fontNameMetrics.ascent() / 2;

                auto name = metrics.elidedText(
                  roomName(), Qt::ElideRight, (width() - IconSize - 2 * Padding) * 0.8);
                p.drawText(QPoint(2 * Padding + IconSize, top_y), name);

                if (roomType_ == RoomType::Joined) {
                        font.setPixelSize(conf::fontSize);
                        p.setFont(font);
                        p.setPen(subtitlePen);

                        auto msgStampWidth = QFontMetrics(font).width(lastMsgInfo_.timestamp) + 5;

                        // The limit is the space between the end of the avatar and the start of the
                        // timestamp.
                        int usernameLimit =
                          std::max(0, width() - 3 * Padding - msgStampWidth - IconSize - 20);
                        auto userName =
                          metrics.elidedText(lastMsgInfo_.username, Qt::ElideRight, usernameLimit);

                        font.setWeight(60);
                        p.setFont(font);
                        p.drawText(QPoint(2 * Padding + IconSize, bottom_y), userName);

                        int nameWidth = QFontMetrics(font).width(userName);

                        font.setBold(false);
                        p.setFont(font);

                        // The limit is the space between the end of the username and the start of
                        // the timestamp.
                        int descriptionLimit = std::max(
                          0, width() - 3 * Padding - msgStampWidth - IconSize - nameWidth - 5);
                        auto description =
                          metrics.elidedText(lastMsgInfo_.body, Qt::ElideRight, descriptionLimit);
                        p.drawText(QPoint(2 * Padding + IconSize + nameWidth, bottom_y),
                                   description);

                        // We either show the bubble or the last message timestamp.
                        if (unreadMsgCount_ == 0) {
                                font.setBold(true);
                                p.drawText(QPoint(width() - Padding - msgStampWidth, bottom_y),
                                           lastMsgInfo_.timestamp);
                        }
                } else {
                        int btnWidth = (width() - IconSize - 6 * Padding) / 2;

                        acceptBtnRegion_ = QRectF(InviteBtnX, InviteBtnY, btnWidth, 20);
                        declineBtnRegion_ =
                          QRectF(InviteBtnX + btnWidth + 2 * Padding, InviteBtnY, btnWidth, 20);

                        QPainterPath acceptPath;
                        acceptPath.addRoundedRect(acceptBtnRegion_, 10, 10);

                        p.setPen(Qt::NoPen);
                        p.fillPath(acceptPath, btnColor_);
                        p.drawPath(acceptPath);

                        QPainterPath declinePath;
                        declinePath.addRoundedRect(declineBtnRegion_, 10, 10);

                        p.setPen(Qt::NoPen);
                        p.fillPath(declinePath, btnColor_);
                        p.drawPath(declinePath);

                        p.setPen(QPen(btnTextColor_));
                        p.setFont(font);
                        p.drawText(acceptBtnRegion_, Qt::AlignCenter, tr("Accept"));
                        p.drawText(declineBtnRegion_, Qt::AlignCenter, tr("Decline"));
                }
        }

        font.setBold(false);
        p.setPen(Qt::NoPen);

        // We using the first letter of room's name.
        if (roomAvatar_.isNull()) {
                QBrush brush;
                brush.setStyle(Qt::SolidPattern);
                brush.setColor("#eee");

                p.setPen(Qt::NoPen);
                p.setBrush(brush);

                p.drawEllipse(avatarRegion.center(), IconSize / 2, IconSize / 2);

                font.setPixelSize(conf::roomlist::fonts::bubble);
                p.setFont(font);
                p.setPen(QColor("#333"));
                p.setBrush(Qt::NoBrush);
                p.drawText(avatarRegion.translated(0, -1), Qt::AlignCenter, QChar(roomName()[0]));
        } else {
                p.save();

                QPainterPath path;
                path.addEllipse(Padding, Padding, IconSize, IconSize);
                p.setClipPath(path);

                p.drawPixmap(avatarRegion, roomAvatar_);
                p.restore();
        }

        if (unreadMsgCount_ > 0) {
                QColor textColor("white");
                QColor backgroundColor("#38A3D8");

                QBrush brush;
                brush.setStyle(Qt::SolidPattern);
                brush.setColor(backgroundColor);

                if (isPressed_)
                        brush.setColor(textColor);

                QFont unreadCountFont;
                unreadCountFont.setPixelSize(conf::roomlist::fonts::badge);
                unreadCountFont.setBold(true);

                p.setBrush(brush);
                p.setPen(Qt::NoPen);
                p.setFont(unreadCountFont);

                int diameter = 20;

                QRectF r(
                  width() - diameter - Padding, bottom_y - diameter / 2 - 5, diameter, diameter);

                if (width() == ui::sidebar::SmallSize)
                        r = QRectF(
                          width() - diameter - 5, height() - diameter - 5, diameter, diameter);

                p.setPen(Qt::NoPen);
                p.drawEllipse(r);

                p.setPen(QPen(textColor));

                if (isPressed_)
                        p.setPen(QPen(backgroundColor));

                p.setBrush(Qt::NoBrush);
                p.drawText(
                  r.translated(0, -0.5), Qt::AlignCenter, QString::number(unreadMsgCount_));
        }
}

void
RoomInfoListItem::updateUnreadMessageCount(int count)
{
        unreadMsgCount_ += count;
        update();
}

void
RoomInfoListItem::clearUnreadMessageCount()
{
        unreadMsgCount_ = 0;
        update();
}

void
RoomInfoListItem::setPressedState(bool state)
{
        if (isPressed_ != state) {
                isPressed_ = state;
                update();
        }
}

void
RoomInfoListItem::setState(const RoomState &new_state)
{
        state_ = new_state;
        update();
}

void
RoomInfoListItem::contextMenuEvent(QContextMenuEvent *event)
{
        Q_UNUSED(event);

        if (roomType_ == RoomType::Invited)
                return;

        toggleNotifications_->setText(notificationText());
        menu_->popup(event->globalPos());
}

void
RoomInfoListItem::mousePressEvent(QMouseEvent *event)
{
        if (event->buttons() == Qt::RightButton) {
                QWidget::mousePressEvent(event);
                return;
        }

        if (roomType_ == RoomType::Invited) {
                const auto point = event->pos();

                if (acceptBtnRegion_.contains(point))
                        emit acceptInvite(roomId_);

                if (declineBtnRegion_.contains(point))
                        emit declineInvite(roomId_);

                return;
        }

        emit clicked(roomId_);

        setPressedState(true);

        // Ripple on mouse position by default.
        QPoint pos           = event->pos();
        qreal radiusEndValue = static_cast<qreal>(width()) / 3;

        Ripple *ripple = new Ripple(pos);

        ripple->setRadiusEndValue(radiusEndValue);
        ripple->setOpacityStartValue(0.15);
        ripple->setColor(QColor("white"));
        ripple->radiusAnimation()->setDuration(200);
        ripple->opacityAnimation()->setDuration(400);

        ripple_overlay_->addRipple(ripple);
}

void
RoomInfoListItem::setAvatar(const QImage &img)
{
        roomAvatar_ = QPixmap::fromImage(
          img.scaled(IconSize, IconSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
        update();
}

void
RoomInfoListItem::setDescriptionMessage(const DescInfo &info)
{
        lastMsgInfo_ = info;
        update();
}

RoomInfoListItem::~RoomInfoListItem() {}