db23494550d215d43551c821d5ffdf5f37116095
[dhcpcd-ui] / src / dhcpcd-qt / dhcpcd-ssidmenuwidget.cpp
1 /*
2  * dhcpcd-qt
3  * Copyright 2014 Roy Marples <roy@marples.name>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <QWidget>
28 #include <QEvent>
29 #include <QHBoxLayout>
30 #include <QRadioButton>
31 #include <QIcon>
32 #include <QLabel>
33 #include <QPixmap>
34 #include <QStyle>
35
36 #include "dhcpcd.h"
37 #include "dhcpcd-qt.h"
38 #include "dhcpcd-ssidmenuwidget.h"
39
40 DhcpcdSsidMenuWidget::DhcpcdSsidMenuWidget(QWidget *parent,
41     DhcpcdWi *wi, DHCPCD_WI_SCAN *scan)
42     : QFrame(parent)
43 {
44
45         this->wi = wi;
46         this->scan = scan;
47
48         QHBoxLayout *layout = new QHBoxLayout(this);
49         layout->setContentsMargins(1, 1, 1, 1);
50         selicon = new QLabel(this);
51         layout->addWidget(selicon);
52
53         ssid = new QLabel(this);
54         layout->addWidget(ssid);
55
56         encicon = new QLabel(this);
57         layout->addWidget(encicon);
58         layout->setAlignment(encicon, Qt::AlignRight);
59
60         stricon = new QLabel(this);
61         layout->addWidget(stricon);
62         layout->setAlignment(stricon, Qt::AlignRight);
63
64         setScan(scan);
65         installEventFilter(this);
66
67         setStyleSheet("QFrame { border-radius: 4px; } "
68             "QFrame:hover {"
69             "color:palette(highlighted-text);"
70             "background-color:palette(highlight);"
71             "}");
72 }
73
74 DHCPCD_WI_SCAN *DhcpcdSsidMenuWidget::getScan()
75 {
76
77         return scan;
78 }
79
80 void DhcpcdSsidMenuWidget::setScan(DHCPCD_WI_SCAN *scan)
81 {
82         DHCPCD_WPA *wpa;
83         DHCPCD_IF *i;
84         QIcon icon;
85         QPixmap picon;
86
87         this->scan = scan;
88         wpa = wi->getWpa();
89         i = dhcpcd_wpa_if(wpa);
90
91         if (i->up && i->ssid && strcmp(scan->ssid, i->ssid) == 0) {
92                 icon = DhcpcdQt::getIcon("actions", "dialog-ok-apply");
93                 picon = icon.pixmap(16, 16);
94                 selicon->setPixmap(picon);
95         }
96         ssid->setText(scan->ssid);
97         if (scan->flags[0] == '\0')
98                 icon = DhcpcdQt::getIcon("status", "dialog-warning");
99         else
100                 icon = DhcpcdQt::getIcon("status",
101                     "network-wireless-encrypted");
102         picon = icon.pixmap(16, 16);
103         encicon->setPixmap(picon);
104
105         if (scan->strength.value > 80)
106                 icon = DhcpcdQt::getIcon("status",
107                     "network-wireless-connected-100");
108         else if (scan->strength.value > 55)
109                 icon = DhcpcdQt::getIcon("status",
110                     "network-wireless-connected-75");
111         else if (scan->strength.value > 30)
112                 icon = DhcpcdQt::getIcon("status",
113                     "network-wireless-connected-50");
114         else if (scan->strength.value > 5)
115                 icon = DhcpcdQt::getIcon("status",
116                     "network-wireless-connected-25");
117         else
118                 icon = DhcpcdQt::getIcon("status",
119                     "network-wireless-connected-00");
120         picon = icon.pixmap(16, 16);
121         stricon->setPixmap(picon);
122 }
123
124 bool DhcpcdSsidMenuWidget::eventFilter(QObject *, QEvent *event)
125 {
126
127         if (event->type() == QEvent::MouseButtonPress)
128                 emit triggered();
129         return false;
130 }
131
132 void DhcpcdSsidMenuWidget::enterEvent(QEvent *)
133 {
134
135         ssid->setStyleSheet("color:palette(highlighted-text);");
136         emit hovered();
137 }
138
139 void DhcpcdSsidMenuWidget::leaveEvent(QEvent *)
140 {
141
142         ssid->setStyleSheet("color:palette(text);");
143 }