We changed enough of dhcpcd-qt in 2017 to warrant a copyright bump.
[dhcpcd-ui] / src / dhcpcd-qt / dhcpcd-ssidmenuwidget.cpp
index 5f62d88a71f9c4ca7410703ad20dfc6f6cb43829..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,11 +46,12 @@ 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(16, 16);
+       selicon->setMinimumSize(ICON_SIZE, ICON_SIZE);
        layout->addWidget(selicon);
 
        ssid = new QLabel(this);
@@ -86,66 +89,96 @@ 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 (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(NULL);
+               selicon->setPixmap(QPixmap());
                ssid->setStyleSheet(NULL);
        }
        ssid->setText(scan->ssid);
-       if (scan->flags & WSF_SECURE)
-               icon = DhcpcdQt::getIcon("status",
-                   "network-wireless-encrypted");
-       else
+       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");
-       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 *)
 {
 
+       if (!active)
+               return;
+
        if (associated)
                ssid->setStyleSheet("color:palette(highlighted-text); font:bold;");
        else
@@ -156,6 +189,9 @@ void DhcpcdSsidMenuWidget::enterEvent(QEvent *)
 void DhcpcdSsidMenuWidget::leaveEvent(QEvent *)
 {
 
+       if (!active)
+               return;
+
        if (associated)
                ssid->setStyleSheet("color:palette(text); font:bold;");
        else