931643500e239c4b099f8072efe309acc2a708f5
[dhcpcd-ui] / src / dhcpcd-qt / dhcpcd-ssidmenu.cpp
1 #include <QWidget>
2 #include <QEvent>
3 #include <QHBoxLayout>
4 #include <QRadioButton>
5 #include <QIcon>
6 #include <QLabel>
7 #include <QPixmap>
8 #include <QProgressBar>
9
10 #include "dhcpcd.h"
11 #include "dhcpcd-qt.h"
12 #include "dhcpcd-ssidmenu.h"
13
14 DhcpcdSsidMenu::DhcpcdSsidMenu(QWidget *parent, DHCPCD_IF *ifp, DHCPCD_WI_SCAN *scan)
15     : QWidget(parent, NULL)
16 {
17         int strength;
18         QIcon icon;
19
20         this->ifp = ifp;
21         this->scan = scan;
22
23         QHBoxLayout *layout = new QHBoxLayout(this);
24         button = new QRadioButton(scan->ssid, this);
25         button->setChecked(strcmp(scan->ssid, ifp->ssid) == 0);
26         layout->addWidget(button);
27         if (scan->flags[0] == '\0') {
28                 icon = DhcpcdQt::getIcon("devices", "network-wireless");
29                 setToolTip(scan->bssid);
30         } else {
31                 icon = DhcpcdQt::getIcon("status", "network-wireless-encrypted");
32                 QString tip = QString::fromAscii(scan->bssid);
33                 tip += " " + QString::fromAscii(scan->flags);
34                 setToolTip(tip);
35         }
36         QPixmap picon = icon.pixmap(22, 22);
37         licon = new QLabel(this);
38         licon->setPixmap(picon);
39         layout->addWidget(licon);
40         bar = new QProgressBar(this);
41         bar->setMinimum(0);
42         bar->setMaximum(100);
43         if (scan->quality.value == 0)
44             strength = scan->level.average;
45         else
46             strength = scan->quality.average;
47         bar->setValue(strength < 0 ? 0 : strength > 100 ? 100 : strength);
48         layout->addWidget(bar);
49
50         button->installEventFilter(this);
51         licon->installEventFilter(this);
52         bar->installEventFilter(this);
53 }
54
55 bool DhcpcdSsidMenu::eventFilter(QObject *, QEvent *event)
56 {
57
58         if (event->type() == QEvent::MouseButtonPress)
59                 emit selected(ifp, scan);
60         return false;
61 }