Restored UI generated by QTDesigner
[ipypbx] / src / ipypbx / controllers.py
1 # Copyright (c) Stas Shtin, 2010
2
3 # This file is part of IPyPBX.
4
5 # IPyPBX is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9
10 # IPyPBX is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with IPyPBX.  If not, see <http://www.gnu.org/licenses/>.
17
18 #from ipypbx import models
19 from PyQt4 import QtCore, QtGui, QtSql
20
21
22 class BaseController(QtCore.QObject):
23     """
24     Base class for other controllers.
25
26     Doesn't do anything useful on its own.
27     """
28     fields = ()
29     view_list_fields = ()
30     view_display_fields = ()
31     
32     def __init__(self, model=None, view_list=None, view_display=None, parent=None, views=None):
33         super(BaseController, self).__init__(parent=parent)
34
35         self.views = views
36         
37         # Find out base name.
38         classname = self.__class__.__name__
39         self.basename = (
40             classname[:-10] if classname.endswith('Controller')
41             else classname)
42         self.basename = self.basename[0].lower() + self.basename[1:]
43
44         if model:
45             # We're given an existing model.
46             self.model = model
47         else:
48             # Initialize a new model.
49             self.model = QtSql.QSqlTableModel(parent)
50             self.model.setTable(self.basename + 's')
51
52             # Create model header from fields list.
53             for i, field in enumerate(self.fields):
54                 self.model.setHeaderData(
55                     i, QtCore.Qt.Horizontal, QtCore.QVariant(field))
56
57             # Fetch model data.
58             self.model.select()
59
60         if view_list:
61             # We're given an existing view list.
62             self.view_list = view_list
63         else:
64             # Get view list from the parent.
65             self.view_list = getattr(views, self.basename + 'ViewList')
66             self.view_list.setModel(self.model)
67
68             # Hide fields not meant for display.
69             for i, field in enumerate(self.fields):
70                 if field not in self.view_list_fields:
71                     self.view_list.setColumnHidden(i, True)
72
73             # Stretch headers to fill all available width.
74             self.view_list.setSelectionMode(QtGui.QTableView.SingleSelection)
75             self.view_list.setSelectionBehavior(QtGui.QTableView.SelectRows)
76             self.view_list.resizeColumnsToContents()
77             self.view_list.resizeRowsToContents()
78             self.view_list.horizontalHeader().setStretchLastSection(True)
79
80         if view_display:
81             # We're given an existing view display.
82             self.view_display = view_display
83         else:
84             # Get view display from the parent.
85             self.view_display = QtGui.QDataWidgetMapper(parent)
86             #getattr(parent, self.basename + 'ViewDisplay')
87             self.view_display.setModel(self.model)
88
89             # If view_display_fields is not send, display all fields except
90             # the first one that is usually the ID.
91             display_fields = self.view_display_fields or self.fields[1:]
92             for i, field in enumerate(self.fields):
93                 if field in display_fields:
94                     field_name = self.basename + ''.join(
95                         word.capitalize() for word in field.split(' '))
96                     self.view_display.addMapping(
97                         getattr(views, field_name), i)
98
99         # Register signals for this controller.
100         for sender, signal, receiver in self.getSignalsData():
101             QtCore.QObject.connect(sender, QtCore.SIGNAL(signal), receiver)        
102
103     def getSignalsData(self):
104         """
105         Default signals built from controller's base name.
106         """
107         return (
108             (getattr(self.views, self.basename + 'Add'), 'clicked()', self.add),
109             (getattr(self.views, self.basename + 'ViewList'),
110              'currentRowChanged(int)', self.select),
111             (getattr(self.views, self.basename + 'Save'), 'clicked()',
112              self.save),
113             )
114
115     def add(self):
116         """
117         Add new connection.
118         """
119         #name_template = 'New connection'
120
121         num_rows = self.model.rowCount()
122         self.model.insertRows(num_rows, 1)
123         self.view_list.selectRow(num_rows)
124         getattr(self.parent(), self.basename + 'Add').setEnabled(False)
125             
126         #self.parent.ui.connectionName.setText('New connection')
127         #self.parent.ui.connectionName.setFocus()
128         #self.parent.ui.connectionName.selectAll()
129         #self.parent.ui.connectionLocalIpAddress.clear()
130         #self.parent.ui.connectionLocalPort.clear()
131         #self.parent.ui.connectionFreeswitchIpAddress.clear()
132         #self.parent.ui.connectionFreeswitchPort.clear()
133     
134     def save(self):
135         """
136         TODO: Default implementation.
137         """
138         return NotImplemented
139
140     def add(self):
141         """
142         TODO: Default implementation.
143         """
144         return NotImplemented
145
146
147 class ConnectionController(BaseController):
148     """
149     Connections handler.
150     """
151     fields = (
152         'ID', 'Name', 'Local IP Address', 'Local Port',
153         'Freeswitch IP Address', 'Freeswitch Port')
154     view_list_fields = ('Name', 'Freeswitch IP Address', 'Freeswitch Port')
155     
156     def select(self, row):
157         """
158         Select another connection as current.
159         """
160         self.currentConnection = self.connections[row]
161
162         # Fill in form based on selection.
163         self.parent.ui.connectionName.setText(self.currentConnection.name)
164         self.parent.ui.connectionLocalIpAddress.setText(
165             self.currentConnection.local_ip_address)
166         self.parent.ui.connectionLocalPort.setText(
167             unicode(self.currentConnection.local_port))
168         self.parent.ui.connectionFreeswitchIpAddress.setText(
169             self.currentConnection.freeswitch_ip_address)
170         self.parent.ui.connectionFreeswitchPort.setText(
171             unicode(self.currentConnection.freeswitch_port))
172
173     def clone(self):
174         """
175         TODO: Clone an existing connection.
176
177         This creates a new connection with bound data copied from another one.
178         """
179
180     def save(self):
181         """
182         Save new or existing connection.
183         """
184         name = unicode(self.parent.ui.connectionName.text())
185
186         # Add to connection list if we've created it.
187         if self.currentConnection is None:            
188             #self.currentConnection = models.Connection(store=state.store)            
189             self.connections.append(self.currentConnection)
190             self.parent.ui.connectionList.addItem(name)
191
192         self.currentConnection.name = name
193         self.currentConnection.local_ip_address = unicode(
194             self.parent.ui.connectionLocalIpAddress.text())
195         self.currentConnection.local_port = int(
196             self.parent.ui.connectionLocalPort.text())
197         self.currentConnection.freeswitch_ip_address = unicode(
198             self.parent.ui.connectionFreeswitchIpAddress.text())
199         self.currentConnection.freeswitch_port = int(
200             self.parent.ui.connectionFreeswitchPort.text())
201
202         self.currentConnection.checkpoint()