1 /*
2  * Cantata
3  *
4  * Copyright (c) 2011-2020 Craig Drummond <craig.p.drummond@gmail.com>
5  *
6  * ----
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; see the file COPYING.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #include "servicestatuslabel.h"
25 #include <QPalette>
26 #include <QStyle>
27 #include <QApplication>
28 #include <QCursor>
29 
ServiceStatusLabel(QWidget * p)30 ServiceStatusLabel::ServiceStatusLabel(QWidget *p)
31     : QLabel(p)
32     , pressed(false)
33 {
34     QFont f(font());
35     f.setBold(true);
36     setFont(f);
37 }
38 
setText(const QString & txt,const QString & name)39 void ServiceStatusLabel::setText(const QString &txt, const QString &name)
40 {
41     QLabel::setText(txt);
42     onTooltip=tr("Logged into %1").arg(name);
43     offTooltip=tr("<b>NOT</b> logged into %1").arg(name);
44 }
45 
setStatus(bool on)46 void ServiceStatusLabel::setStatus(bool on)
47 {
48     setVisible(true);
49     setToolTip(on ? onTooltip : offTooltip);
50     QString col=on
51             ? palette().highlight().color().name()
52             : palette().color(QPalette::Disabled, QPalette::WindowText).name();
53 
54     int margin=style()->pixelMetric(QStyle::PM_DefaultFrameWidth, nullptr, this);
55     if (margin<2) {
56         margin=2;
57     }
58     setStyleSheet(QString("QLabel { color : %1; border-radius: %4px; border: 2px solid %2; margin: %3px}")
59                   .arg(col).arg(col).arg(margin).arg(margin*2));
60 }
61 
mousePressEvent(QMouseEvent * ev)62 void ServiceStatusLabel::mousePressEvent(QMouseEvent *ev)
63 {
64     QLabel::mousePressEvent(ev);
65     pressed=true;
66 }
67 
mouseReleaseEvent(QMouseEvent * ev)68 void ServiceStatusLabel::mouseReleaseEvent(QMouseEvent *ev)
69 {
70     QLabel::mouseReleaseEvent(ev);
71     if (pressed) {
72         pressed=false;
73         if (this==QApplication::widgetAt(QCursor::pos())) {
74             emit clicked();
75         }
76     }
77 }
78 
79 
80 #include "moc_servicestatuslabel.cpp"
81