View list row selection works
authorStas Shtin <antisvin@gmail.com>
Fri, 9 Apr 2010 20:21:11 +0000 (00:21 +0400)
committerStas Shtin <antisvin@gmail.com>
Fri, 9 Apr 2010 20:21:11 +0000 (00:21 +0400)
src/ipypbx/controllers.py
src/ipypbx/sql.py

index 1277d3a..fa6cbbc 100644 (file)
@@ -41,11 +41,11 @@ class BaseController(QtCore.QObject):
             else classname)
         self.basename = self.basename[0].lower() + self.basename[1:]
 
+        # Are we given an existing model?
         if model:
-            # We're given an existing model.
             self.model = model
+        # Otherwise initialize a new model.
         else:
-            # Initialize a new model.
             self.model = QtSql.QSqlTableModel(parent)
             self.model.setTable(self.basename + 's')
 
@@ -57,11 +57,11 @@ class BaseController(QtCore.QObject):
             # Fetch model data.
             self.model.select()
 
+        # Are we given an existing view list?
         if view_list:
-            # We're given an existing view list.
             self.view_list = view_list
+        # Otherwise get view list from the parent.            
         else:
-            # Get view list from the parent.
             self.view_list = getattr(views, self.basename + 'ViewList')
             self.view_list.setModel(self.model)
 
@@ -76,29 +76,55 @@ class BaseController(QtCore.QObject):
             self.view_list.resizeColumnsToContents()
             self.view_list.resizeRowsToContents()
             self.view_list.horizontalHeader().setStretchLastSection(True)
+        self.view_list.selectRow(0)
 
+        # Are we given an existing view display?
         if view_display:
-            # We're given an existing view display.
             self.view_display = view_display
+        # Otherwise get view display from the parent.
         else:
-            # Get view display from the parent.
             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:]
+            display_fields = self.getDisplayFields()
             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)
+                    field_widget = self.getFieldWidget(field)
+                    self.view_display.addMapping(field_widget, i)
 
+        # Select first row in the view list.
+        self.view_display.toFirst()
+        
         # Register signals for this controller.
-        for sender, signal, receiver in self.getSignalsData():
-            QtCore.QObject.connect(sender, QtCore.SIGNAL(signal), receiver)        
+        for data in self.getSignalsData():
+            if len(data) == 3:
+                sender, signal, receiver = data
+                QtCore.QObject.connect(sender, QtCore.SIGNAL(signal), receiver)
+            elif len(data) == 4:
+                sender, signal, receiver, slot = data
+                QtCore.QObject.connect(
+                    sender, QtCore.SIGNAL(signal), receiver, QtCore.SLOT(slot))
+                                       
+
+    def getDisplayFields(self):
+        """
+        Return the list of fields to display.
+
+        If it's not set explicitly, use all defined fields except the first
+        which usually is the primary key.
+        """
+        return self.view_display_fields or self.fields[1:]
+
+    def getFieldWidget(self, field):
+        """
+        Return widget for given field name.
+        """
+        return getattr(
+            self.views,
+            self.basename + ''.join(word.capitalize()
+                                    for word in field.split(' ')))
 
     def getSignalsData(self):
         """
@@ -106,29 +132,51 @@ class BaseController(QtCore.QObject):
         """
         return (
             (getattr(self.views, self.basename + 'Add'), 'clicked()', self.add),
-            (getattr(self.views, self.basename + 'ViewList'),
-             'currentRowChanged(int)', self.select),
+            #connect(ui.bookTable->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
+            # mapper, SLOT(setCurrentModelIndex(QModelIndex)));
+            
+            (self.view_list.selectionModel(), 'currentRowChanged(QModelIndex,QModelIndex)',
+             self.view_display, 'setCurrentModelIndex(QModelIndex)'),
             (getattr(self.views, self.basename + 'Save'), 'clicked()',
              self.save),
             )
 
     def add(self):
         """
-        Add new connection.
+        Add new object.
         """
+        # Add a new row to list view.
         num_rows = self.model.rowCount()
         self.model.insertRows(num_rows, 1)
         self.view_list.selectRow(num_rows)
+
+        # Disable adding more than one row.
         getattr(self.views, 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()
-    
+
+        # Focust to the first displayed field.
+        self.getFieldWidget(self.getDisplayFields()[0]).setFocus()
+
+        # Clear all displayed fields.
+        for field in self.getDisplayFields():
+            self.getFieldWidget(field).clear()
+        
+    def select(self, row):
+        """
+        Select an object from the list.
+        """
+        # Fill in form based on selection.
+        
+        
+        #self.parent.ui.connectionName.setText(self.currentConnection.name)
+        #self.parent.ui.connectionLocalIpAddress.setText(
+        #    self.currentConnection.local_ip_address)
+        #self.parent.ui.connectionLocalPort.setText(
+        #    unicode(self.currentConnection.local_port))
+        #self.parent.ui.connectionFreeswitchIpAddress.setText(
+        #    self.currentConnection.freeswitch_ip_address)
+        #self.parent.ui.connectionFreeswitchPort.setText(
+        #    unicode(self.currentConnection.freeswitch_port))
+
     def save(self):
         """
         TODO: Default implementation.
@@ -145,23 +193,6 @@ class ConnectionController(BaseController):
         'Freeswitch IP Address', 'Freeswitch Port')
     view_list_fields = ('Name', 'Freeswitch IP Address', 'Freeswitch Port')
     
-    def select(self, row):
-        """
-        Select another connection as current.
-        """
-        self.currentConnection = self.connections[row]
-
-        # Fill in form based on selection.
-        self.parent.ui.connectionName.setText(self.currentConnection.name)
-        self.parent.ui.connectionLocalIpAddress.setText(
-            self.currentConnection.local_ip_address)
-        self.parent.ui.connectionLocalPort.setText(
-            unicode(self.currentConnection.local_port))
-        self.parent.ui.connectionFreeswitchIpAddress.setText(
-            self.currentConnection.freeswitch_ip_address)
-        self.parent.ui.connectionFreeswitchPort.setText(
-            unicode(self.currentConnection.freeswitch_port))
-
     def clone(self):
         """
         TODO: Clone an existing connection.
index a467d16..b332d37 100644 (file)
@@ -14,7 +14,4 @@ creation_queries = (
 #    '''
     )
 
-class Connection(object):
-    id, name, local_ip_address, local_port, freeswitch_ip_address, \
-        freeswitch_port = range(6)