We changed enough of dhcpcd-qt in 2017 to warrant a copyright bump.
[dhcpcd-ui] / src / dhcpcd-qt / dhcpcd-ssidmenuwidget.cpp
index db23494550d215d43551c821d5ffdf5f37116095..b2ab91d153016971b33b0266da74cecc2c452ef7 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * dhcpcd-qt
- * Copyright 2014 Roy Marples <roy@marples.name>
+ * Copyright 2014-2017 Roy Marples <roy@marples.name>
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -37,6 +37,8 @@
 #include "dhcpcd-qt.h"
 #include "dhcpcd-ssidmenuwidget.h"
 
+#define ICON_SIZE      16
+
 DhcpcdSsidMenuWidget::DhcpcdSsidMenuWidget(QWidget *parent,
     DhcpcdWi *wi, DHCPCD_WI_SCAN *scan)
     : QFrame(parent)
@@ -44,13 +46,17 @@ DhcpcdSsidMenuWidget::DhcpcdSsidMenuWidget(QWidget *parent,
 
        this->wi = wi;
        this->scan = scan;
+       this->active = true;
 
        QHBoxLayout *layout = new QHBoxLayout(this);
        layout->setContentsMargins(1, 1, 1, 1);
        selicon = new QLabel(this);
+       selicon->setMinimumSize(ICON_SIZE, ICON_SIZE);
        layout->addWidget(selicon);
 
        ssid = new QLabel(this);
+       ssid->setSizePolicy(QSizePolicy::MinimumExpanding,
+           QSizePolicy::MinimumExpanding);
        layout->addWidget(ssid);
 
        encicon = new QLabel(this);
@@ -77,67 +83,117 @@ DHCPCD_WI_SCAN *DhcpcdSsidMenuWidget::getScan()
        return scan;
 }
 
+bool DhcpcdSsidMenuWidget::isAssociated()
+{
+
+       return associated;
+}
+
+bool DhcpcdSsidMenuWidget::isActive()
+{
+
+       return active;
+}
+
+void DhcpcdSsidMenuWidget::setActive(bool active)
+{
+
+       if (this->active != active) {
+               this->ssid->setEnabled(active);
+               this->active = active;
+       }
+}
+
 void DhcpcdSsidMenuWidget::setScan(DHCPCD_WI_SCAN *scan)
 {
        DHCPCD_WPA *wpa;
        DHCPCD_IF *i;
        QIcon icon;
        QPixmap picon;
+       bool active;
 
        this->scan = scan;
        wpa = wi->getWpa();
        i = dhcpcd_wpa_if(wpa);
+       associated = dhcpcd_wi_associated(i, scan);
+
+       /*
+        * Icons are rescaled because they are not always the size requested
+        * due to a bug in Qt-4.8.
+        * https://qt.gitorious.org/qt/qt/merge_requests/2566
+        */
 
-       if (i->up && i->ssid && strcmp(scan->ssid, i->ssid) == 0) {
+       if (associated) {
                icon = DhcpcdQt::getIcon("actions", "dialog-ok-apply");
-               picon = icon.pixmap(16, 16);
+               picon = icon.pixmap(ICON_SIZE);
+               if (picon.height() != ICON_SIZE)
+                       picon = picon.scaledToHeight(ICON_SIZE,
+                           Qt::SmoothTransformation);
                selicon->setPixmap(picon);
+               ssid->setStyleSheet("font:bold;");
+       } else {
+               selicon->setPixmap(QPixmap());
+               ssid->setStyleSheet(NULL);
        }
        ssid->setText(scan->ssid);
-       if (scan->flags[0] == '\0')
+       active = true;
+       if (scan->flags & WSF_SECURE) {
+               if (scan->flags & WSF_PSK)
+                       icon = DhcpcdQt::getIcon("status",
+                           "network-wireless-encrypted");
+               else {
+                       icon = DhcpcdQt::getIcon("status",
+                           "network-error");
+                       active = false;
+               }
+       } else
                icon = DhcpcdQt::getIcon("status", "dialog-warning");
-       else
-               icon = DhcpcdQt::getIcon("status",
-                   "network-wireless-encrypted");
-       picon = icon.pixmap(16, 16);
+       picon = icon.pixmap(ICON_SIZE);
+       if (picon.height() != ICON_SIZE)
+               picon = picon.scaledToHeight(ICON_SIZE, Qt::SmoothTransformation);
        encicon->setPixmap(picon);
 
-       if (scan->strength.value > 80)
-               icon = DhcpcdQt::getIcon("status",
-                   "network-wireless-connected-100");
-       else if (scan->strength.value > 55)
-               icon = DhcpcdQt::getIcon("status",
-                   "network-wireless-connected-75");
-       else if (scan->strength.value > 30)
-               icon = DhcpcdQt::getIcon("status",
-                   "network-wireless-connected-50");
-       else if (scan->strength.value > 5)
-               icon = DhcpcdQt::getIcon("status",
-                   "network-wireless-connected-25");
-       else
-               icon = DhcpcdQt::getIcon("status",
-                   "network-wireless-connected-00");
-       picon = icon.pixmap(16, 16);
+       icon = DhcpcdQt::getIcon("status",
+           DhcpcdQt::signalStrengthIcon(scan));
+       picon = icon.pixmap(ICON_SIZE);
+       if (picon.height() != ICON_SIZE)
+               picon = picon.scaledToHeight(ICON_SIZE, Qt::SmoothTransformation);
        stricon->setPixmap(picon);
+
+       setActive(active);
 }
 
 bool DhcpcdSsidMenuWidget::eventFilter(QObject *, QEvent *event)
 {
 
-       if (event->type() == QEvent::MouseButtonPress)
-               emit triggered();
+       if (event->type() == QEvent::MouseButtonPress) {
+               if (active)
+                       emit triggered();
+       }
        return false;
 }
 
 void DhcpcdSsidMenuWidget::enterEvent(QEvent *)
 {
 
-       ssid->setStyleSheet("color:palette(highlighted-text);");
+       if (!active)
+               return;
+
+       if (associated)
+               ssid->setStyleSheet("color:palette(highlighted-text); font:bold;");
+       else
+               ssid->setStyleSheet("color:palette(highlighted-text);");
        emit hovered();
 }
 
 void DhcpcdSsidMenuWidget::leaveEvent(QEvent *)
 {
 
-       ssid->setStyleSheet("color:palette(text);");
+       if (!active)
+               return;
+
+       if (associated)
+               ssid->setStyleSheet("color:palette(text); font:bold;");
+       else
+               ssid->setStyleSheet("color:palette(text);");
 }