summaryrefslogtreecommitdiffstats
path: root/src/TopRoomBar.cc
blob: 4f2a5d831b4e539c82ffedbaf7b49a10f91e0600 (plain)
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
/*
 * 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 <QStyleOption>

#include "Config.h"
#include "TopRoomBar.h"

TopRoomBar::TopRoomBar(QWidget *parent)
  : QWidget(parent)
  , buttonSize_{ 32 }
{
        setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        setMinimumSize(QSize(0, 65));
        setStyleSheet("background-color: #fff; color: #171919;");

        topLayout_ = new QHBoxLayout();
        topLayout_->setSpacing(10);
        topLayout_->setMargin(10);

        avatar_ = new Avatar(this);
        avatar_->setLetter(QChar('?'));
        avatar_->setBackgroundColor(QColor("#d6dde3"));
        avatar_->setTextColor(QColor("#555459"));
        avatar_->setSize(35);

        textLayout_ = new QVBoxLayout();
        textLayout_->setSpacing(0);
        textLayout_->setContentsMargins(0, 0, 0, 0);

        QFont roomFont("Open Sans SemiBold");
        roomFont.setPixelSize(conf::topRoomBar::fonts::roomName);

        nameLabel_ = new QLabel(this);
        nameLabel_->setFont(roomFont);

        QFont descriptionFont("Open Sans");
        descriptionFont.setPixelSize(conf::topRoomBar::fonts::roomDescription);

        topicLabel_ = new QLabel(this);
        topicLabel_->setFont(descriptionFont);
        topicLabel_->setTextFormat(Qt::RichText);
        topicLabel_->setTextInteractionFlags(Qt::TextBrowserInteraction);
        topicLabel_->setOpenExternalLinks(true);

        textLayout_->addWidget(nameLabel_);
        textLayout_->addWidget(topicLabel_);

        settingsBtn_ = new FlatButton(this);
        settingsBtn_->setForegroundColor(QColor("#acc7dc"));
        settingsBtn_->setFixedSize(buttonSize_, buttonSize_);
        settingsBtn_->setCornerRadius(buttonSize_ / 2);

        QIcon settings_icon;
        settings_icon.addFile(
          ":/icons/icons/vertical-ellipsis.png", QSize(), QIcon::Normal, QIcon::Off);
        settingsBtn_->setIcon(settings_icon);
        settingsBtn_->setIconSize(QSize(buttonSize_ / 2, buttonSize_ / 2));

        topLayout_->addWidget(avatar_);
        topLayout_->addLayout(textLayout_);
        topLayout_->addStretch(1);
        topLayout_->addWidget(settingsBtn_);

        menu_ = new Menu(this);

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

        menu_->addAction(toggleNotifications_);

        connect(settingsBtn_, &QPushButton::clicked, this, [=]() {
                if (roomSettings_->isNotificationsEnabled())
                        toggleNotifications_->setText(tr("Disable notifications"));
                else
                        toggleNotifications_->setText(tr("Enable notifications"));

                auto pos = mapToGlobal(settingsBtn_->pos());
                menu_->popup(
                  QPoint(pos.x() + buttonSize_ - menu_->sizeHint().width(), pos.y() + buttonSize_));
        });

        setLayout(topLayout_);
}

void
TopRoomBar::updateRoomAvatarFromName(const QString &name)
{
        QChar letter = '?';

        if (name.size() > 0)
                letter = name[0];

        avatar_->setLetter(letter);
}

void
TopRoomBar::reset()
{
        nameLabel_->setText("");
        topicLabel_->setText("");
        avatar_->setLetter(QChar('?'));
}

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

        QStyleOption option;
        option.initFrom(this);

        QPainter painter(this);
        style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);
}

void
TopRoomBar::setRoomSettings(QSharedPointer<RoomSettings> settings)
{
        roomSettings_ = settings;
}

TopRoomBar::~TopRoomBar()
{
}