# HG changeset patch # User Dirk Olmes # Date 1412303156 -7200 # Node ID 56b2e8f9846e16ea734cc21660c070be4ba29cc3 # Parent fb7442228526c469c7a9e65b289a6803b470664f 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 diff -r fb7442228526 -r 56b2e8f9846e RemoteViewer/Connection.py --- 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) diff -r fb7442228526 -r 56b2e8f9846e RemoteViewer/ConnectionDialog.py --- 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: diff -r fb7442228526 -r 56b2e8f9846e RemoteViewer/ConnectionDialog.ui --- 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 @@ - + @@ -115,7 +115,7 @@ - + <html><head/><body><p>Password:</p></body></html> @@ -123,6 +123,13 @@ + + + GenericComboBox + QComboBox +
PyQtLib.GenericComboBox
+
+
type hostname