changeset 29:56b2e8f9846e

introduce an object to represent a connection's type. Use this type object to determine if the password checkbox should be hidden in the connection dialog
author Dirk Olmes <dirk@xanthippe.ping.de>
date Fri, 03 Oct 2014 04:25:56 +0200
parents fb7442228526
children 736551d4ad51
files RemoteViewer/Connection.py RemoteViewer/ConnectionDialog.py RemoteViewer/ConnectionDialog.ui
diffstat 3 files changed, 79 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/RemoteViewer/Connection.py	Fri Oct 03 03:04:57 2014 +0200
+++ b/RemoteViewer/Connection.py	Fri Oct 03 04:25:56 2014 +0200
@@ -11,6 +11,7 @@
         return func(object, string)
     return __perform
 
+
 class Connection(object):
     @staticmethod
     def parse(json):
@@ -18,7 +19,10 @@
         for key in KEYS:
             if key in json:
                 value = json[key]
-                setattr(connection, key, value)
+                if key == 'type':
+                    connection.type = Type.get(value)
+                else:
+                    setattr(connection, key, value)
         return connection
 
     def __init__(self):
@@ -28,7 +32,7 @@
         self.lowColor = False
         self.promptForPassword = False
         self.title = None
-        self.type = 'vnc'
+        self.type = VncType()
         self.user = None
 
     def __str__(self):
@@ -87,13 +91,53 @@
             if hasattr(self,  key):
                 value = getattr(self,  key)
                 if value is not None:
+                    if key == 'type':
+                        # just serialize the 'name' attribute of the Type object
+                        value = value.name
                     json[key] = value
         return json
 
     def open(self, parent):
-        if self.type == 'vnc':
-            VncViewer(self).open(parent)
-        elif self.type == 'rdp':
-            FreeRdp(self).open(parent)
+        self.type.open(self, parent)
+
+
+class Type(object):
+    @staticmethod
+    def all():
+        return [RdpType(), VncType()]
+
+    @staticmethod
+    def get(type):
+        if type == 'rdp':
+            return RdpType()
+        elif type == 'vnc':
+            return VncType()
         else:
-            raise ValueError('unknown type: ' + self.type)
+            raise ValueError('invalid type: ' + type)
+
+    def __init__(self, name):
+        self.name = name
+        self.supportsPassword = False
+
+    def __repr__(self):
+        return self.name
+
+    def __eq__(self, other):
+        return self.name == other.name
+
+
+class RdpType(Type):
+    def __init__(self):
+        super(RdpType, self).__init__('rdp')
+        self.supportsPassword = True
+
+    def open(self, connection, parent):
+        FreeRdp(connection).open(parent)
+
+
+class VncType(Type):
+    def __init__(self):
+        super(VncType, self).__init__('vnc')
+
+    def open(self, connection, parent):
+        VncViewer(connection).open(parent)
--- a/RemoteViewer/ConnectionDialog.py	Fri Oct 03 03:04:57 2014 +0200
+++ b/RemoteViewer/ConnectionDialog.py	Fri Oct 03 04:25:56 2014 +0200
@@ -1,6 +1,7 @@
 from PyQt4.QtCore import Qt
 from PyQt4.QtGui import QDialog
 from .Connection import Connection
+from .Connection import Type
 from .Ui_ConnectionDialog import Ui_ConnectionDialog
 
 class ConnectionDialog(QDialog):
@@ -16,18 +17,17 @@
         self.__initTypeDropDown()
         self.__loadConnection()
         self.__connectWidgetSlots()
+        self.__applyType()
 
     def __initTypeDropDown(self):
-        self.ui.type.addItem('rdp')
-        self.ui.type.addItem('vnc')
+        self.types = Type.all()
+        self.ui.type.displayObjects(self.types, 'name')
 
     def __loadConnection(self):
         self.ui.title.setText(self.connection.title)
-        if self.connection.type == 'rdp':
-            index = 0
-        else:
-            index = 1
-        self.ui.type.setCurrentIndex(index)
+        # Do not load the connection type here. This is done in a separate step
+        # after the slots have been connected to ensure proper visibility of the
+        # password checkbox
         self.ui.hostname.setText(self.connection.host)
         self.ui.username.setText(self.connection.user)
         self.__initCheckbox(self.ui.promptForPassword, self.connection.promptForPassword)
@@ -42,13 +42,24 @@
 
     def __connectWidgetSlots(self):
         self.ui.title.textChanged.connect(self.connection.setTitle)
-        self.ui.type.activated[str].connect(self.connection.setType)
+        self.ui.type.selectionChanged.connect(self.typeSelected)
         self.ui.hostname.textChanged.connect(self.connection.setHost)
         self.ui.username.textChanged.connect(self.connection.setUser)
         self.ui.promptForPassword.clicked[bool].connect(self.connection.setPromptForPassword)
         self.ui.geometry.textChanged.connect(self.connection.setGeometry)
         self.ui.lowColor.clicked[bool].connect(self.connection.setLowColor)
 
+    def __applyType(self):
+        index = self.types.index(self.connection.type)
+        print('index:', index)
+        self.ui.type.setCurrentIndex(index)
+
+    def typeSelected(self, type):
+        self.connection.type = type
+        visible = type.supportsPassword
+        self.ui.passwordLabel.setVisible(visible)
+        self.ui.promptForPassword.setVisible(visible)
+
     def accept(self):
         validationErrors = self.connection.validate()
         if validationErrors is None:
--- a/RemoteViewer/ConnectionDialog.ui	Fri Oct 03 03:04:57 2014 +0200
+++ b/RemoteViewer/ConnectionDialog.ui	Fri Oct 03 04:25:56 2014 +0200
@@ -29,7 +29,7 @@
     </widget>
    </item>
    <item row="1" column="1">
-    <widget class="QComboBox" name="type"/>
+    <widget class="GenericComboBox" name="type"/>
    </item>
    <item row="2" column="0">
     <widget class="QLabel" name="label_2">
@@ -115,7 +115,7 @@
     </widget>
    </item>
    <item row="4" column="0">
-    <widget class="QLabel" name="label_7">
+    <widget class="QLabel" name="passwordLabel">
      <property name="text">
       <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Password:&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
      </property>
@@ -123,6 +123,13 @@
    </item>
   </layout>
  </widget>
+ <customwidgets>
+  <customwidget>
+   <class>GenericComboBox</class>
+   <extends>QComboBox</extends>
+   <header>PyQtLib.GenericComboBox</header>
+  </customwidget>
+ </customwidgets>
  <tabstops>
   <tabstop>type</tabstop>
   <tabstop>hostname</tabstop>