From 3bfd6a5c6798e23c6cb2a4dee830e8cb7db48af2 Mon Sep 17 00:00:00 2001 From: Stas Shtin Date: Fri, 9 Apr 2010 17:55:54 +0400 Subject: [PATCH] Restored UI generated by QTDesigner --- src/ipypbx/controllers.py | 87 +++++++++++++------------- src/ipypbx/main.py | 18 +++++- src/ipypbx/ui.py | 152 ++++++++++++++++++--------------------------- ui/layout.ui | 2 +- 4 files changed, 117 insertions(+), 142 deletions(-) diff --git a/src/ipypbx/controllers.py b/src/ipypbx/controllers.py index 873d98d..bfb7a17 100644 --- a/src/ipypbx/controllers.py +++ b/src/ipypbx/controllers.py @@ -26,18 +26,20 @@ class BaseController(QtCore.QObject): Doesn't do anything useful on its own. """ fields = () - hidden_fields = () - visible_fields = () + view_list_fields = () + view_display_fields = () - def __init__(self, model=None, view_list=None, view_display=None, parent=None): - super(BaseController, self).__init__(parent) + def __init__(self, model=None, view_list=None, view_display=None, parent=None, views=None): + super(BaseController, self).__init__(parent=parent) + + self.views = views # Find out base name. classname = self.__class__.__name__ - self.base_name = ( + self.basename = ( classname[:-10] if classname.endswith('Controller') else classname) - self.base_name = self.base_name[0].lower() + self.base_name[1:] + self.basename = self.basename[0].lower() + self.basename[1:] if model: # We're given an existing model. @@ -45,7 +47,7 @@ class BaseController(QtCore.QObject): else: # Initialize a new model. self.model = QtSql.QSqlTableModel(parent) - self.model.setTable(self.base_name + 's') + self.model.setTable(self.basename + 's') # Create model header from fields list. for i, field in enumerate(self.fields): @@ -60,13 +62,12 @@ class BaseController(QtCore.QObject): self.view_list = view_list else: # Get view list from the parent. - self.view_list = getattr(parent, self.base_name + 'ViewList') + self.view_list = getattr(views, self.basename + 'ViewList') self.view_list.setModel(self.model) # Hide fields not meant for display. for i, field in enumerate(self.fields): - if (field in self.hidden_fields or - field not in self.visible_fields): + if field not in self.view_list_fields: self.view_list.setColumnHidden(i, True) # Stretch headers to fill all available width. @@ -81,9 +82,20 @@ class BaseController(QtCore.QObject): self.view_display = view_display else: # Get view display from the parent. - self.view_display = getattr(parent, self.base_name + 'ViewDisplay') + self.view_display = QtGui.QDataWidgetMapper(parent) + #getattr(parent, self.basename + 'ViewDisplay') self.view_display.setModel(self.model) + # If view_display_fields is not send, display all fields except + # the first one that is usually the ID. + display_fields = self.view_display_fields or self.fields[1:] + for i, field in enumerate(self.fields): + if field in display_fields: + field_name = self.basename + ''.join( + word.capitalize() for word in field.split(' ')) + self.view_display.addMapping( + getattr(views, field_name), i) + # Register signals for this controller. for sender, signal, receiver in self.getSignalsData(): QtCore.QObject.connect(sender, QtCore.SIGNAL(signal), receiver) @@ -92,21 +104,32 @@ class BaseController(QtCore.QObject): """ Default signals built from controller's base name. """ - parent = self.parent() - return ( - (getattr(parent, self.base_name + 'Add'), 'clicked()', self.add), - (getattr(parent, self.base_name + 'ViewList'), + (getattr(self.views, self.basename + 'Add'), 'clicked()', self.add), + (getattr(self.views, self.basename + 'ViewList'), 'currentRowChanged(int)', self.select), - (getattr(parent, self.base_name + 'Save'), 'clicked()', + (getattr(self.views, self.basename + 'Save'), 'clicked()', self.save), ) def add(self): """ - TODO: Default implementation. + Add new connection. """ - return NotImplemented + #name_template = 'New connection' + + num_rows = self.model.rowCount() + self.model.insertRows(num_rows, 1) + self.view_list.selectRow(num_rows) + getattr(self.parent(), self.basename + 'Add').setEnabled(False) + + #self.parent.ui.connectionName.setText('New connection') + #self.parent.ui.connectionName.setFocus() + #self.parent.ui.connectionName.selectAll() + #self.parent.ui.connectionLocalIpAddress.clear() + #self.parent.ui.connectionLocalPort.clear() + #self.parent.ui.connectionFreeswitchIpAddress.clear() + #self.parent.ui.connectionFreeswitchPort.clear() def save(self): """ @@ -128,7 +151,7 @@ class ConnectionController(BaseController): fields = ( 'ID', 'Name', 'Local IP Address', 'Local Port', 'Freeswitch IP Address', 'Freeswitch Port') - visible_fields = ('Name', 'Freeswitch IP Address', 'Freeswitch Port') + view_list_fields = ('Name', 'Freeswitch IP Address', 'Freeswitch Port') def select(self, row): """ @@ -154,32 +177,6 @@ class ConnectionController(BaseController): This creates a new connection with bound data copied from another one. """ - def add(self): - """ - Add new connection. - """ - self.currentConnection = None - - name_template = 'New connection [{0:02}]' - for i in xrange(1, 100): - name = name_template.format(i) - connection_exists = False - for connection in self.connections: - if connection.name == name: - connection_exists = True - break - - if not connection_exists: - break - - self.parent.ui.connectionName.setText('New connection') - self.parent.ui.connectionName.setFocus() - self.parent.ui.connectionName.selectAll() - self.parent.ui.connectionLocalIpAddress.clear() - self.parent.ui.connectionLocalPort.clear() - self.parent.ui.connectionFreeswitchIpAddress.clear() - self.parent.ui.connectionFreeswitchPort.clear() - def save(self): """ Save new or existing connection. diff --git a/src/ipypbx/main.py b/src/ipypbx/main.py index d1d7bb5..626f527 100644 --- a/src/ipypbx/main.py +++ b/src/ipypbx/main.py @@ -55,14 +55,26 @@ def setupDb(prefix=PREFIX, dbname=DB_NAME): None, "Fatal Error", "Database Error: %s" % db.lastError().text()) sys.exit(1) + +def localize(): + locale = QtCore.QLocale.system().name() + translator = QtCore.QTranslator() + + if translator.load("ipypbx_%s" % locale.toLower(), "ipypbx/locale"): + QtGui.QApplication.installTranslator(translator) + if __name__ == '__main__': app = QtGui.QApplication(sys.argv) setupDb() - views = ui.MainWindow() - views.show() + main = QtGui.QMainWindow() + views = ui.Ui_MainWindow() + views.setupUi(main) + localize() + main.show() - connectionController = controllers.ConnectionController(parent=views) + connectionController = controllers.ConnectionController( + parent=main, views=views) app.exec_() # sys.exit() diff --git a/src/ipypbx/ui.py b/src/ipypbx/ui.py index 77c9480..037c40b 100644 --- a/src/ipypbx/ui.py +++ b/src/ipypbx/ui.py @@ -1,49 +1,24 @@ -# Copyright (c) Stas Shtin, 2010 +# -*- coding: utf-8 -*- -# This file is part of IPyPBX. +# Form implementation generated from reading ui file '../ui/layout.ui' +# +# Created: Fri Apr 9 17:29:46 2010 +# by: PyQt4 UI code generator 4.7.2 +# +# WARNING! All changes made in this file will be lost! -# IPyPBX is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. +from PyQt4 import QtCore, QtGui -# IPyPBX is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with IPyPBX. If not, see . - -from PyQt4 import QtCore, QtGui, QtSql - - -class ConnectionDataMapper(QtGui.QDataWidgetMapper): - def add(self): - model = self.model() - model.insertRows(1, model.rowCount()) - - -class MainWindow(QtGui.QMainWindow): - """ - Main GUI window. - """ - def __init__(self, parent=None): - super(MainWindow, self).__init__(parent) - - # Tweak layout. - self.setObjectName("MainWindow") - self.resize(800, 400) - self.centralwidget = QtGui.QWidget(self) +class Ui_MainWindow(object): + def setupUi(self, MainWindow): + MainWindow.setObjectName("MainWindow") + MainWindow.resize(800, 400) + self.centralwidget = QtGui.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") - - # Add tab widget self.tabWidget = QtGui.QTabWidget(self.centralwidget) self.tabWidget.setGeometry(QtCore.QRect(-2, -1, 811, 491)) self.tabWidget.setMinimumSize(QtCore.QSize(800, 480)) self.tabWidget.setObjectName("tabWidget") - - # Connections tab definition. self.connectionsTab = QtGui.QWidget() self.connectionsTab.setObjectName("connectionsTab") self.layoutWidget = QtGui.QWidget(self.connectionsTab) @@ -51,43 +26,42 @@ class MainWindow(QtGui.QMainWindow): self.layoutWidget.setObjectName("layoutWidget") self.gridLayout = QtGui.QGridLayout(self.layoutWidget) self.gridLayout.setObjectName("gridLayout") - self.connectionViewDisplay = ConnectionDataMapper() - #self.formLayout_7 = QtGui.QFormLayout() - #self.formLayout_7.setFieldGrowthPolicy(QtGui.QFormLayout.AllNonFixedFieldsGrow) - #self.formLayout_7.setObjectName("formLayout_7") - #self.label_5 = QtGui.QLabel(self.layoutWidget) - #self.label_5.setObjectName("label_5") - #self.formLayout_7.setWidget(0, QtGui.QFormLayout.LabelRole, self.label_5) - #self.connectionName = QtGui.QLineEdit(self.layoutWidget) - #self.connectionName.setMaxLength(100) - #self.connectionName.setObjectName("connectionName") - #self.formLayout_7.setWidget(0, QtGui.QFormLayout.FieldRole, self.connectionName) - #self.connectionLocalIpAddress = QtGui.QLineEdit(self.layoutWidget) - #self.connectionLocalIpAddress.setInputMethodHints(QtCore.Qt.ImhNone) - #self.connectionLocalIpAddress.setObjectName("connectionLocalIpAddress") - #self.formLayout_7.setWidget(1, QtGui.QFormLayout.FieldRole, self.connectionLocalIpAddress) - #self.label_11 = QtGui.QLabel(self.layoutWidget) - #self.label_11.setObjectName("label_11") - #self.formLayout_7.setWidget(2, QtGui.QFormLayout.LabelRole, self.label_11) - #self.connectionLocalPort = QtGui.QLineEdit(self.layoutWidget) - #self.connectionLocalPort.setObjectName("connectionLocalPort") - #self.formLayout_7.setWidget(2, QtGui.QFormLayout.FieldRole, self.connectionLocalPort) - #self.label_13 = QtGui.QLabel(self.layoutWidget) - #self.label_13.setObjectName("label_13") - #self.formLayout_7.setWidget(4, QtGui.QFormLayout.LabelRole, self.label_13) - #self.connectionFreeswitchPort = QtGui.QLineEdit(self.layoutWidget) - #self.connectionFreeswitchPort.setObjectName("connectionFreeswitchPort") - #self.formLayout_7.setWidget(4, QtGui.QFormLayout.FieldRole, self.connectionFreeswitchPort) - #self.label_12 = QtGui.QLabel(self.layoutWidget) - #self.label_12.setObjectName("label_12") - #self.formLayout_7.setWidget(1, QtGui.QFormLayout.LabelRole, self.label_12) - #self.connectionFreeswitchIpAddress = QtGui.QLineEdit(self.layoutWidget) - #self.connectionFreeswitchIpAddress.setObjectName("connectionFreeswitchIpAddress") - #self.formLayout_7.setWidget(3, QtGui.QFormLayout.FieldRole, self.connectionFreeswitchIpAddress) - #self.label_14 = QtGui.QLabel(self.layoutWidget) - #self.label_14.setObjectName("label_14") - #self.formLayout_7.setWidget(3, QtGui.QFormLayout.LabelRole, self.label_14) - #self.gridLayout.addLayout(self.formLayout_7, 0, 1, 1, 1) + self.formLayout_7 = QtGui.QFormLayout() + self.formLayout_7.setFieldGrowthPolicy(QtGui.QFormLayout.AllNonFixedFieldsGrow) + self.formLayout_7.setObjectName("formLayout_7") + self.label_5 = QtGui.QLabel(self.layoutWidget) + self.label_5.setObjectName("label_5") + self.formLayout_7.setWidget(0, QtGui.QFormLayout.LabelRole, self.label_5) + self.connectionName = QtGui.QLineEdit(self.layoutWidget) + self.connectionName.setMaxLength(100) + self.connectionName.setObjectName("connectionName") + self.formLayout_7.setWidget(0, QtGui.QFormLayout.FieldRole, self.connectionName) + self.connectionLocalIpAddress = QtGui.QLineEdit(self.layoutWidget) + self.connectionLocalIpAddress.setInputMethodHints(QtCore.Qt.ImhNone) + self.connectionLocalIpAddress.setObjectName("connectionLocalIpAddress") + self.formLayout_7.setWidget(1, QtGui.QFormLayout.FieldRole, self.connectionLocalIpAddress) + self.label_11 = QtGui.QLabel(self.layoutWidget) + self.label_11.setObjectName("label_11") + self.formLayout_7.setWidget(2, QtGui.QFormLayout.LabelRole, self.label_11) + self.connectionLocalPort = QtGui.QLineEdit(self.layoutWidget) + self.connectionLocalPort.setObjectName("connectionLocalPort") + self.formLayout_7.setWidget(2, QtGui.QFormLayout.FieldRole, self.connectionLocalPort) + self.label_13 = QtGui.QLabel(self.layoutWidget) + self.label_13.setObjectName("label_13") + self.formLayout_7.setWidget(4, QtGui.QFormLayout.LabelRole, self.label_13) + self.connectionFreeswitchPort = QtGui.QLineEdit(self.layoutWidget) + self.connectionFreeswitchPort.setObjectName("connectionFreeswitchPort") + self.formLayout_7.setWidget(4, QtGui.QFormLayout.FieldRole, self.connectionFreeswitchPort) + self.label_12 = QtGui.QLabel(self.layoutWidget) + self.label_12.setObjectName("label_12") + self.formLayout_7.setWidget(1, QtGui.QFormLayout.LabelRole, self.label_12) + self.connectionFreeswitchIpAddress = QtGui.QLineEdit(self.layoutWidget) + self.connectionFreeswitchIpAddress.setObjectName("connectionFreeswitchIpAddress") + self.formLayout_7.setWidget(3, QtGui.QFormLayout.FieldRole, self.connectionFreeswitchIpAddress) + self.label_14 = QtGui.QLabel(self.layoutWidget) + self.label_14.setObjectName("label_14") + self.formLayout_7.setWidget(3, QtGui.QFormLayout.LabelRole, self.label_14) + self.gridLayout.addLayout(self.formLayout_7, 0, 1, 1, 1) self.connectionAdd = QtGui.QPushButton(self.layoutWidget) self.connectionAdd.setObjectName("connectionAdd") self.gridLayout.addWidget(self.connectionAdd, 1, 0, 1, 1) @@ -384,27 +358,19 @@ class MainWindow(QtGui.QMainWindow): self.formLayout_4.setWidget(3, QtGui.QFormLayout.FieldRole, self.extensionEndpoint) self.gridLayout_4.addLayout(self.formLayout_4, 0, 1, 1, 1) self.tabWidget.addTab(self.extensionsTab, "") - self.setCentralWidget(self.centralwidget) + MainWindow.setCentralWidget(self.centralwidget) - self.retranslateUi() + self.retranslateUi(MainWindow) self.tabWidget.setCurrentIndex(0) - QtCore.QMetaObject.connectSlotsByName(self) - - def retranslateUi(self): - locale = QtCore.QLocale.system().name() - translator = QtCore.QTranslator() - - if translator.load("ipypbx_%s" % locale.toLower(), "ipypbx/locale"): - QtGui.QApplication.installTranslator(translator) + QtCore.QMetaObject.connectSlotsByName(MainWindow) - self.setWindowTitle( - QtGui.QApplication.translate( - "MainWindow", "IPyPBX", None, QtGui.QApplication.UnicodeUTF8)) - #self.label_5.setText(QtGui.QApplication.translate("MainWindow", "Name", None, QtGui.QApplication.UnicodeUTF8)) - #self.label_11.setText(QtGui.QApplication.translate("MainWindow", "Local Port", None, QtGui.QApplication.UnicodeUTF8)) - #self.label_13.setText(QtGui.QApplication.translate("MainWindow", "Freeswitch Port", None, QtGui.QApplication.UnicodeUTF8)) - #self.label_12.setText(QtGui.QApplication.translate("MainWindow", "Local IP address", None, QtGui.QApplication.UnicodeUTF8)) - #self.label_14.setText(QtGui.QApplication.translate("MainWindow", "Freeswitch IP Address", None, QtGui.QApplication.UnicodeUTF8)) + def retranslateUi(self, MainWindow): + MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "IPyPBX", None, QtGui.QApplication.UnicodeUTF8)) + self.label_5.setText(QtGui.QApplication.translate("MainWindow", "Name", None, QtGui.QApplication.UnicodeUTF8)) + self.label_11.setText(QtGui.QApplication.translate("MainWindow", "Local Port", None, QtGui.QApplication.UnicodeUTF8)) + self.label_13.setText(QtGui.QApplication.translate("MainWindow", "Freeswitch Port", None, QtGui.QApplication.UnicodeUTF8)) + self.label_12.setText(QtGui.QApplication.translate("MainWindow", "Local IP address", None, QtGui.QApplication.UnicodeUTF8)) + self.label_14.setText(QtGui.QApplication.translate("MainWindow", "Freeswitch IP Address", None, QtGui.QApplication.UnicodeUTF8)) self.connectionAdd.setText(QtGui.QApplication.translate("MainWindow", "Add", None, QtGui.QApplication.UnicodeUTF8)) self.connectionSave.setText(QtGui.QApplication.translate("MainWindow", "Save", None, QtGui.QApplication.UnicodeUTF8)) self.tabWidget.setTabText(self.tabWidget.indexOf(self.connectionsTab), QtGui.QApplication.translate("MainWindow", "Connections", None, QtGui.QApplication.UnicodeUTF8)) diff --git a/ui/layout.ui b/ui/layout.ui index af8b532..02517df 100644 --- a/ui/layout.ui +++ b/ui/layout.ui @@ -126,7 +126,7 @@ - + -- 1.7.9.5