blob: 2b47ddb978a67a3842666986df8e7a5a5ae80fa8 [file] [log] [blame]
Isa Nanic601de1d2018-10-23 11:37:26 -04001/***************************************************************************
2 * Copyright (C) 2018 by Savoir-faire Linux *
3 * Author: Isa Nanic <isa.nanic@savoirfairelinux.com> *
4 * Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com> *
5 * *
6 * This program is free 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 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. *
18 **************************************************************************/
19
20#include "currentaccountcombobox.h"
21#include <accountitemdelegate.h>
22#include "pixbufmanipulator.h"
23
24#include "utils.h"
25#include "ringthemeutils.h"
26#include "lrcinstance.h"
27#include <QPixmap>
28
29#include "callwidget.h"
30#include "ui_callwidget.h"
31
32#undef REGISTERED
33
Isa Nanic601de1d2018-10-23 11:37:26 -040034CurrentAccountComboBox::CurrentAccountComboBox(QWidget* parent)
35{
36 Q_UNUSED(parent);
37
Isa Nanic37ccc1c2018-10-26 14:16:28 -040038 setMouseTracking(true);
39 gearLabel_.setMouseTracking(true);
40
Isa Nanic1c20ef12018-10-25 11:49:30 -040041 accountListUpdate();
Andreas Traczyk6ace34f2018-12-14 14:31:23 -050042 accountItemDelegate_ = new AccountItemDelegate(this);
Isa Nanic601de1d2018-10-23 11:37:26 -040043 this->setItemDelegate(accountItemDelegate_);
44
45 // combobox index changed and so must the avatar
46 connect(this, QOverload<int>::of(&QComboBox::currentIndexChanged),
47 [=](const int& index) {
Isa Nanic1c20ef12018-10-25 11:49:30 -040048 setCurrentIndex(index);
Isa Nanic601de1d2018-10-23 11:37:26 -040049 });
50
51 // account added to combobox
52 connect(&LRCInstance::accountModel(),
53 &lrc::api::NewAccountModel::accountAdded,
54 [this](const std::string& accountId) {
55 auto accountList = LRCInstance::accountModel().getAccountList();
56 auto it = std::find(accountList.begin(), accountList.end(), accountId);
57 if (it != accountList.end()) {
58 this->setCurrentIndex(std::distance(accountList.begin(), it));
59 }
60 });
Isa Nanic37ccc1c2018-10-26 14:16:28 -040061
Isa Nanic6e4a39a2018-12-04 14:26:02 -050062 gearLabel_.setPixmap(QPixmap(":/images/icons/round-settings-24px.svg"));
Isa Nanic37ccc1c2018-10-26 14:16:28 -040063 gearLabel_.setParent(this);
64 gearLabel_.setStyleSheet("background: transparent;");
Andreas Traczyk43c08232018-10-31 13:42:09 -040065 setupSettingsButton();
Isa Nanic601de1d2018-10-23 11:37:26 -040066}
67
68CurrentAccountComboBox::~CurrentAccountComboBox()
69{
70 delete accountItemDelegate_;
71}
72
73void
74CurrentAccountComboBox::paintEvent(QPaintEvent* e)
75{
76 Q_UNUSED(e);
77
Isa Nanic37ccc1c2018-10-26 14:16:28 -040078 QPoint p(12, 2);
Isa Nanic601de1d2018-10-23 11:37:26 -040079 QPainter painter(this);
Isa Nanic1c20ef12018-10-25 11:49:30 -040080 painter.setRenderHints((QPainter::Antialiasing | QPainter::TextAntialiasing), true);
Isa Nanic601de1d2018-10-23 11:37:26 -040081
82 QStyleOption opt;
83 opt.init(this);
Andreas Traczyk43c08232018-10-31 13:42:09 -040084 style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter);
Isa Nanic601de1d2018-10-23 11:37:26 -040085
86 // create box in which to draw avatar and presence indicator
87 QRect avatarRect(2, 2, cellHeight_, cellHeight_); // [screen awareness]
Isa Nanic1c20ef12018-10-25 11:49:30 -040088 QRect comboBoxRect(cellHeight_ + p.x() + 2, 4, this->width() - cellHeight_ + p.x(), cellHeight_- 10); // [screen awareness]
Isa Nanic601de1d2018-10-23 11:37:26 -040089
90 // define and set the two fonts
91 QFont fontPrimary = painter.font();
92 QFont fontSecondary = painter.font();
93
94 fontPrimary.setPointSize(11);
95 fontPrimary.setWeight(QFont::ExtraLight);
96 fontSecondary.setPointSize(10);
97 painter.setFont(fontPrimary);
98
99 QFontMetrics fontMetricPrimary(fontPrimary);
100 QFontMetrics fontMetricSecondary(fontSecondary);
101
102 painter.drawPixmap(p, currentAccountAvatarImage_);
103
104 // fill in presence indicator if account is registered
105 auto accountStatus = LRCInstance::getCurrentAccountInfo().status;
106 if (accountStatus == lrc::api::account::Status::REGISTERED) {
107 // paint the presence indicator circle
108 QPainterPath outerCircle, innerCircle;
Isa Nanic37ccc1c2018-10-26 14:16:28 -0400109 QPointF presenceCenter(40.0 + p.x(), 40.0);
Isa Nanic601de1d2018-10-23 11:37:26 -0400110 qreal outerCircleRadius = cellHeight_/6.5;
Andreas Traczyk1c1302c2018-12-31 12:28:16 -0500111 qreal innerCircleRadius = outerCircleRadius * 0.75;
Isa Nanic601de1d2018-10-23 11:37:26 -0400112 outerCircle.addEllipse(presenceCenter, outerCircleRadius, outerCircleRadius);
113 innerCircle.addEllipse(presenceCenter, innerCircleRadius, innerCircleRadius);
114 painter.fillPath(outerCircle, Qt::white);
115 painter.fillPath(innerCircle, RingTheme::presenceGreen_);
116 }
117
118 // write primary and secondary account identifiers to combobox label
Isa Nanic601de1d2018-10-23 11:37:26 -0400119 QString primaryAccountID = QString::fromStdString(Utils::bestNameForAccount(LRCInstance::getCurrentAccountInfo()));
120 painter.setPen(Qt::black);
Andreas Traczykfd0d7c02018-10-30 17:14:06 -0400121 primaryAccountID = fontMetricPrimary.elidedText(primaryAccountID, Qt::ElideRight,
122 comboBoxRect.width() - elidConst - (popupPresent ? 0 : 2 * gearSize_));
Isa Nanic601de1d2018-10-23 11:37:26 -0400123 painter.drawText(comboBoxRect, Qt::AlignLeft, primaryAccountID);
124
125 QString secondaryAccountID = QString::fromStdString(Utils::secondBestNameForAccount(LRCInstance::getCurrentAccountInfo()));
Andreas Traczykfd0d7c02018-10-30 17:14:06 -0400126 secondaryAccountID = fontMetricSecondary.elidedText(secondaryAccountID, Qt::ElideRight,
127 comboBoxRect.width() - elidConst - 2 - (popupPresent ? 0 : 2 * gearSize_)); // [screen awareness]
Isa Nanic601de1d2018-10-23 11:37:26 -0400128
129 if (secondaryAccountID.length()) { // if secondary accound id exists
130 painter.setFont(fontSecondary);
131 painter.setPen(Qt::lightGray);
132 painter.drawText(comboBoxRect, (Qt::AlignBottom | Qt::AlignLeft), secondaryAccountID);
133 }
Andreas Traczyk6ace34f2018-12-14 14:31:23 -0500134
135 if (QWidget* popupWidget = this->findChild<QFrame*>()) {
136 popupWidget->setStyleSheet("QFrame{border: 0px;border-bottom: 1px solid #f0f0f0;}");
137 }
Andreas Traczyk43c08232018-10-31 13:42:09 -0400138}
Isa Nanic601de1d2018-10-23 11:37:26 -0400139
Isa Nanic6e4a39a2018-12-04 14:26:02 -0500140void CurrentAccountComboBox::resizeEvent(QResizeEvent* event)
Andreas Traczyk43c08232018-10-31 13:42:09 -0400141{
Isa Nanic6e4a39a2018-12-04 14:26:02 -0500142 Q_UNUSED(event);
Andreas Traczyk43c08232018-10-31 13:42:09 -0400143 setupSettingsButton();
144}
Andreas Traczykfd0d7c02018-10-30 17:14:06 -0400145
Andreas Traczyk43c08232018-10-31 13:42:09 -0400146void
147CurrentAccountComboBox::setupSettingsButton()
148{
Isa Nanic6e4a39a2018-12-04 14:26:02 -0500149 gearPoint_.setX(this->width() - gearSize_ - 4 * gearBorder_ + 2);
150 gearPoint_.setY(this->height() / 2 - gearLabel_.height() / 2 - 2 * gearBorder_ + 4.5);
Andreas Traczyk43c08232018-10-31 13:42:09 -0400151 gearLabel_.setGeometry(gearPoint_.x() - 3, gearPoint_.y(),
152 gearSize_ + 2 * gearBorder_, gearSize_ + 2 * gearBorder_);
153 gearLabel_.setMargin(gearBorder_);
Isa Nanic601de1d2018-10-23 11:37:26 -0400154}
155
156// import account background account pixmap and scale pixmap to fit in label
157void
Isa Nanic1c20ef12018-10-25 11:49:30 -0400158CurrentAccountComboBox::importLabelPhoto(int index)
Isa Nanic601de1d2018-10-23 11:37:26 -0400159{
Isa Nanic1c20ef12018-10-25 11:49:30 -0400160 currentAccountAvatarImage_ = accountListModel_->data(accountListModel_->index(index, 0), // [efficiency improvement]
Isa Nanic601de1d2018-10-23 11:37:26 -0400161 AccountListModel::Role::Picture).value<QPixmap>().scaledToHeight(cellHeight_ - 4, Qt::SmoothTransformation);
Isa Nanic1c20ef12018-10-25 11:49:30 -0400162}
163
164void
165CurrentAccountComboBox::setCurrentIndex(int index)
166{
Andreas Traczyk6ace34f2018-12-14 14:31:23 -0500167 auto accountListSize = LRCInstance::accountModel().getAccountList().size();
168 if (index == accountListSize) {
169 emit newAccountClicked();
170 } else if (index < accountListSize) {
171 importLabelPhoto(index);
172 QComboBox::setCurrentIndex(index);
173 }
Isa Nanic1c20ef12018-10-25 11:49:30 -0400174}
175
176void
177CurrentAccountComboBox::accountListUpdate()
178{
179 accountListModel_.reset(new AccountListModel());
180 this->setModel(accountListModel_.get());
181}
Isa Nanic37ccc1c2018-10-26 14:16:28 -0400182
Andreas Traczykfd0d7c02018-10-30 17:14:06 -0400183// if gearLabel is clicked
184void
185CurrentAccountComboBox::mousePressEvent(QMouseEvent* mouseEvent)
186{
187 if (!gearLabel_.frameGeometry().contains(mouseEvent->localPos().toPoint())) {
Andreas Traczyk6ace34f2018-12-14 14:31:23 -0500188 QComboBox::mousePressEvent(mouseEvent);
Andreas Traczykfd0d7c02018-10-30 17:14:06 -0400189 } else {
190 emit settingsButtonClicked();
191 }
192}
193
194// if gear label is hovered over
195void
196CurrentAccountComboBox::mouseMoveEvent(QMouseEvent* mouseEvent)
197{
198 if (gearLabel_.frameGeometry().contains(mouseEvent->x(), mouseEvent->y())) {
199 QComboBox::mouseMoveEvent(mouseEvent);
200 gearLabel_.setStyleSheet("background: rgb(237, 237, 237); border-width: 0px; border-radius: 8px;");
201 return;
202 }
203
204 gearLabel_.setStyleSheet("background: transparent;");
205}
206
207void
208CurrentAccountComboBox::showPopup()
209{
Andreas Traczyk43c08232018-10-31 13:42:09 -0400210 gearLabel_.hide();
Andreas Traczykfd0d7c02018-10-30 17:14:06 -0400211 popupPresent = true;
212 QComboBox::showPopup();
213}
214
215void
216CurrentAccountComboBox::hidePopup()
217{
Andreas Traczyk43c08232018-10-31 13:42:09 -0400218 gearLabel_.show();
Andreas Traczykfd0d7c02018-10-30 17:14:06 -0400219 popupPresent = false;
220 QComboBox::hidePopup();
Andreas Traczykfd0d7c02018-10-30 17:14:06 -0400221}
222
223void
224CurrentAccountComboBox::leaveEvent(QEvent* event)
225{
226 gearLabel_.setStyleSheet("background: transparent;");
227 QComboBox::leaveEvent(event);
Isa Nanic6e4a39a2018-12-04 14:26:02 -0500228}
229
230void
231CurrentAccountComboBox::updateComboBoxDisplay()
232{
Andreas Traczyk10523bf2018-12-31 16:49:03 -0500233 auto currAccIndex = LRCInstance::getCurrentAccountIndex();
234 if (currAccIndex != currentIndex())
235 setCurrentIndex(currAccIndex);
236 importLabelPhoto(currAccIndex);
Isa Nanic6e4a39a2018-12-04 14:26:02 -0500237}