Update copyrights
[dhcpcd-ui] / src / dhcpcd-qt / dhcpcd-ssidmenuwidget.cpp
1 /*
2  * dhcpcd-qt
3  * Copyright 2014-2015 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 #define ICON_SIZE       16
41
42 DhcpcdSsidMenuWidget::DhcpcdSsidMenuWidget(QWidget *parent,
43     DhcpcdWi *wi, DHCPCD_WI_SCAN *scan)
44     : QFrame(parent)
45 {
46
47         this->wi = wi;
48         this->scan = scan;
49
50         QHBoxLayout *layout = new QHBoxLayout(this);
51         layout->setContentsMargins(1, 1, 1, 1);
52         selicon = new QLabel(this);
53         selicon->setMinimumSize(ICON_SIZE, ICON_SIZE);
54         layout->addWidget(selicon);
55
56         ssid = new QLabel(this);
57         ssid->setSizePolicy(QSizePolicy::MinimumExpanding,
58             QSizePolicy::MinimumExpanding);
59         layout->addWidget(ssid);
60
61         encicon = new QLabel(this);
62         layout->addWidget(encicon);
63         layout->setAlignment(encicon, Qt::AlignRight);
64
65         stricon = new QLabel(this);
66         layout->addWidget(stricon);
67         layout->setAlignment(stricon, Qt::AlignRight);
68
69         setScan(scan);
70         installEventFilter(this);
71
72         setStyleSheet("QFrame { border-radius: 4px; } "
73             "QFrame:hover {"
74             "color:palette(highlighted-text);"
75             "background-color:palette(highlight);"
76             "}");
77 }
78
79 DHCPCD_WI_SCAN *DhcpcdSsidMenuWidget::getScan()
80 {
81
82         return scan;
83 }
84
85 bool DhcpcdSsidMenuWidget::isAssociated()
86 {
87
88         return associated;
89 }
90
91 void DhcpcdSsidMenuWidget::setScan(DHCPCD_WI_SCAN *scan)
92 {
93         DHCPCD_WPA *wpa;
94         DHCPCD_IF *i;
95         QIcon icon;
96         QPixmap picon;
97
98         this->scan = scan;
99         wpa = wi->getWpa();
100         i = dhcpcd_wpa_if(wpa);
101         associated = dhcpcd_wi_associated(i, scan);
102
103         /*
104          * Icons are rescaled because they are not always the size requested
105          * due to a bug in Qt-4.8.
106          * https://qt.gitorious.org/qt/qt/merge_requests/2566
107          */
108
109         if (associated) {
110                 icon = DhcpcdQt::getIcon("actions", "dialog-ok-apply");
111                 picon = icon.pixmap(ICON_SIZE);
112                 if (picon.height() != ICON_SIZE)
113                         picon = picon.scaledToHeight(ICON_SIZE,
114                             Qt::SmoothTransformation);
115                 selicon->setPixmap(picon);
116                 ssid->setStyleSheet("font:bold;");
117         } else {
118                 selicon->setPixmap(NULL);
119                 ssid->setStyleSheet(NULL);
120         }
121         ssid->setText(scan->ssid);
122         if (scan->flags & WSF_SECURE)
123                 icon = DhcpcdQt::getIcon("status",
124                     "network-wireless-encrypted");
125         else
126                 icon = DhcpcdQt::getIcon("status", "dialog-warning");
127         picon = icon.pixmap(ICON_SIZE);
128         if (picon.height() != ICON_SIZE)
129                 picon = picon.scaledToHeight(ICON_SIZE, Qt::SmoothTransformation);
130         encicon->setPixmap(picon);
131
132         icon = DhcpcdQt::getIcon("status",
133             DhcpcdQt::signalStrengthIcon(scan));
134         picon = icon.pixmap(ICON_SIZE);
135         if (picon.height() != ICON_SIZE)
136                 picon = picon.scaledToHeight(ICON_SIZE, Qt::SmoothTransformation);
137         stricon->setPixmap(picon);
138 }
139
140 bool DhcpcdSsidMenuWidget::eventFilter(QObject *, QEvent *event)
141 {
142
143         if (event->type() == QEvent::MouseButtonPress)
144                 emit triggered();
145         return false;
146 }
147
148 void DhcpcdSsidMenuWidget::enterEvent(QEvent *)
149 {
150
151         if (associated)
152                 ssid->setStyleSheet("color:palette(highlighted-text); font:bold;");
153         else
154                 ssid->setStyleSheet("color:palette(highlighted-text);");
155         emit hovered();
156 }
157
158 void DhcpcdSsidMenuWidget::leaveEvent(QEvent *)
159 {
160
161         if (associated)
162                 ssid->setStyleSheet("color:palette(text); font:bold;");
163         else
164                 ssid->setStyleSheet("color:palette(text);");
165 }