source: trunk/papywizard/controller/pluginsController.py @ 2192

Revision 2192, 14.2 KB checked in by fma, 4 years ago (diff)

i18 issue in driver combobox

  • Property svn:keywords set to Id
Line 
1# -*- coding: utf-8 -*-
2
3""" Panohead remote control.
4
5License
6=======
7
8 - B{Papywizard} (U{http://www.papywizard.org}) is Copyright:
9  - (C) 2007-2009 Frédéric Mantegazza
10
11This software is governed by the B{CeCILL} license under French law and
12abiding by the rules of distribution of free software.  You can  use,
13modify and/or redistribute the software under the terms of the CeCILL
14license as circulated by CEA, CNRS and INRIA at the following URL
15U{http://www.cecill.info}.
16
17As a counterpart to the access to the source code and  rights to copy,
18modify and redistribute granted by the license, users are provided only
19with a limited warranty  and the software's author,  the holder of the
20economic rights,  and the successive licensors  have only  limited
21liability.
22
23In this respect, the user's attention is drawn to the risks associated
24with loading,  using,  modifying and/or developing or reproducing the
25software by the user in light of its specific status of free software,
26that may mean  that it is complicated to manipulate,  and  that  also
27therefore means  that it is reserved for developers  and  experienced
28professionals having in-depth computer knowledge. Users are therefore
29encouraged to load and test the software's suitability as regards their
30requirements in conditions enabling the security of their systems and/or
31data to be ensured and,  more generally, to use and operate it in the
32same conditions as regards security.
33
34The fact that you are presently reading this means that you have had
35knowledge of the CeCILL license and that you accept its terms.
36
37Module purpose
38==============
39
40Graphical toolkit controller
41
42Implements
43==========
44
45- PreferencesController
46
47@author: Frédéric Mantegazza
48@copyright: (C) 2007-2009 Frédéric Mantegazza
49@license: CeCILL
50
51@todo: check what type(s) of driver(s) a plugin can use
52"""
53
54__revision__ = "$Id$"
55
56from PyQt4 import QtCore, QtGui
57
58from papywizard.common import config
59from papywizard.common.configManager import ConfigManager
60from papywizard.common.loggingServices import Logger
61from papywizard.hardware.bluetoothTransport import BluetoothTransport
62from papywizard.plugins.pluginsManager  import PluginsManager
63from papywizard.controller.abstractController import AbstractModalDialogController
64from papywizard.controller.bluetoothChooserController import BluetoothChooserController
65from papywizard.view.messageDialog import WarningMessageDialog, ExceptionMessageDialog
66
67
68class PluginsController(AbstractModalDialogController):
69    """ Plugins controller object.
70    """
71    def _init(self):
72        self._uiFile = "pluginsDialog.ui"
73
74    def _retreiveWidgets(self):
75        """ Get widgets from widget tree.
76        """
77        AbstractModalDialogController._retreiveWidgets(self)
78
79    def _initWidgets(self):
80        pass
81
82    def _connectSignals(self):
83        AbstractModalDialogController._connectSignals(self)
84        self.connect(self._view.yawAxisComboBox, QtCore.SIGNAL("activated(const QString&)"), self.__onYawAxisComboBoxActivated)
85        self.connect(self._view.pitchAxisComboBox, QtCore.SIGNAL("activated(const QString&)"), self.__onPitchAxisComboBoxActivated)
86        self.connect(self._view.shutterComboBox, QtCore.SIGNAL("activated(const QString&)"), self.__onShutterComboBoxActivated)
87        self.connect(self._view.bluetoothChoosePushButton, QtCore.SIGNAL("clicked()"), self.__onBluetoothChoosePushButtonClicked)
88
89    def _disconnectSignals(self):
90        AbstractModalDialogController._disconnectSignals(self)
91        self.disconnect(self._view.yawAxisComboBox, QtCore.SIGNAL("activated(const QString&)"), self.__onYawAxisComboBoxActivated)
92        self.disconnect(self._view.pitchAxisComboBox, QtCore.SIGNAL("activated(const QString&)"), self.__onPitchAxisComboBoxActivated)
93        self.disconnect(self._view.shutterComboBox, QtCore.SIGNAL("activated(const QString&)"), self.__onShutterComboBoxActivated)
94        self.disconnect(self._view.bluetoothChoosePushButton, QtCore.SIGNAL("clicked()"), self.__onBluetoothChoosePushButtonClicked)
95
96    # Callbacks
97    def _onAccepted(self):
98        """ Ok button has been clicked.
99
100        Save back values to model.
101        """
102        Logger().trace("PluginsController._onAccepted()")
103
104        # Plugins tab
105        previousPluginName = ConfigManager().get('Plugins/PLUGIN_YAW_AXIS')
106        ConfigManager().set('Plugins/PLUGIN_YAW_AXIS', unicode(self._view.yawAxisComboBox.currentText()))
107        newPluginName = ConfigManager().get('Plugins/PLUGIN_YAW_AXIS')
108        newPlugin = PluginsManager ().get('yawAxis', newPluginName)[0]
109        if previousPluginName != newPluginName:
110            previousPlugin = PluginsManager ().get('yawAxis', previousPluginName)[0]
111            previousPlugin.deactivate()
112            newPlugin.activate()
113        if hasattr(newPlugin, '_driver'):
114            newPlugin._config['DRIVER_TYPE'] = config.DRIVER_INDEX[self._view.yawAxisDriverComboBox.currentIndex()]
115        # todo: set config in a callback
116        newPlugin._saveConfig()
117
118        previousPluginName = ConfigManager().get('Plugins/PLUGIN_PITCH_AXIS')
119        ConfigManager().set('Plugins/PLUGIN_PITCH_AXIS', unicode(self._view.pitchAxisComboBox.currentText()))
120        newPluginName = ConfigManager().get('Plugins/PLUGIN_PITCH_AXIS')
121        newPlugin = PluginsManager ().get('pitchAxis', newPluginName)[0]
122        if previousPluginName != newPluginName:
123            previousPlugin = PluginsManager ().get('pitchAxis', previousPluginName)[0]
124            previousPlugin.deactivate()
125            newPlugin.activate()
126        if hasattr(newPlugin, '_driver'):
127            newPlugin._config['DRIVER_TYPE'] = config.DRIVER_INDEX[self._view.pitchAxisDriverComboBox.currentIndex()]
128        # todo: set config in a callback
129        newPlugin._saveConfig()
130
131        previousPluginName = ConfigManager().get('Plugins/PLUGIN_SHUTTER')
132        ConfigManager().set('Plugins/PLUGIN_SHUTTER', unicode(self._view.shutterComboBox.currentText()))
133        newPluginName = ConfigManager().get('Plugins/PLUGIN_SHUTTER')
134        newPlugin = PluginsManager ().get('shutter', newPluginName)[0]
135        if previousPluginName != newPluginName:
136            previousPlugin = PluginsManager ().get('shutter', previousPluginName)[0]
137            previousPlugin.deactivate()
138            newPlugin.activate()
139        if hasattr(newPlugin, '_driver'):
140            newPlugin._config['DRIVER_TYPE'] = config.DRIVER_INDEX[self._view.shutterDriverComboBox.currentIndex()]
141        # todo: set config in a callback
142        newPlugin._saveConfig()
143
144        # Drivers tab
145        ConfigManager().set('Plugins/HARDWARE_BLUETOOTH_DEVICE_ADDRESS', unicode(self._view.bluetoothDeviceAddressLineEdit.text()))
146        ConfigManager().set('Plugins/HARDWARE_SERIAL_PORT', unicode(self._view.serialPortLineEdit.text()))
147        ConfigManager().set('Plugins/HARDWARE_ETHERNET_HOST', unicode(self._view.ethernetHostLineEdit.text()))
148        ConfigManager().setInt('Plugins/HARDWARE_ETHERNET_PORT', self._view.ethernetPortSpinBox.value())
149        ConfigManager().save()
150
151    def __onYawAxisComboBoxActivated(self, pluginName):
152        """ Yaw axis combo box.
153        """
154        Logger().debug("PluginsController.__onYawAxisComboBoxActivated(): plugin=%s" % pluginName)
155        model, controllerClass = PluginsManager ().get('yawAxis', pluginName)
156        if hasattr(model, '_driver'):
157            self._view.yawAxisDriverComboBox.setEnabled(True)
158            self._view.yawAxisDriverComboBox.setCurrentIndex(self._view.yawAxisDriverComboBox.findText(model._config['DRIVER_TYPE']))
159        else:
160            self._view.yawAxisDriverComboBox.setEnabled(False)
161            #self._view.yawAxisDriverComboBox.setCurrentIndex(-1)
162
163    def __onPitchAxisComboBoxActivated(self, pluginName):
164        """ Pitch axis combo box.
165        """
166        Logger().debug("PluginsController.__onPitchAxisComboBoxActivated(): plugin=%s" % pluginName)
167        model, controllerClass = PluginsManager ().get('pitchAxis', pluginName)
168        if hasattr(model, '_driver'):
169            self._view.pitchAxisDriverComboBox.setEnabled(True)
170            self._view.pitchAxisDriverComboBox.setCurrentIndex(self._view.pitchAxisDriverComboBox.findText(model._config['DRIVER_TYPE']))
171        else:
172            self._view.pitchAxisDriverComboBox.setEnabled(False)
173            #self._view.pitchAxisDriverComboBox.setCurrentIndex(-1)
174
175    def __onShutterComboBoxActivated(self, pluginName):
176        """ Shutter combo box.
177        """
178        Logger().debug("PluginsController.__onShutterComboBoxActivated(): plugin=%s" % pluginName)
179        model, controllerClass = PluginsManager ().get('shutter', pluginName)
180        if hasattr(model, '_driver'):
181            self._view.shutterDriverComboBox.setEnabled(True)
182            self._view.shutterDriverComboBox.setCurrentIndex(self._view.shutterDriverComboBox.findText(model._config['DRIVER_TYPE']))
183        else:
184            self._view.shutterDriverComboBox.setEnabled(False)
185            #self._view.shutterDriverComboBox.setCurrentIndex(-1)
186
187    def __onBluetoothChoosePushButtonClicked(self):
188        """ Choose bluetooth button clicked.
189
190        Open the bluetooth chooser dialog.
191        """
192        Logger().trace("PluginsController.__onBluetoothChoosePushButtonClicked()")
193        QtGui.qApp.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))
194        while QtGui.QApplication.hasPendingEvents():
195            QtGui.QApplication.processEvents()  #QtCore.QEventLoop.ExcludeUserInputEvents)
196
197        bluetoothTransport = BluetoothTransport()
198        try:
199            bluetoothDevices = bluetoothTransport.discoverDevices()
200        except Exception, msg:
201            QtGui.qApp.restoreOverrideCursor()
202            Logger().exception("PluginsController.__onBluetoothChoosePushButtonClicked()")
203            Logger().error("Can't scan bluetooth\n%s" % unicode(msg))
204            dialog = ExceptionMessageDialog(self.tr("Can't scan bluetooth"), unicode(msg))
205            dialog.exec_()
206        else:
207            QtGui.qApp.restoreOverrideCursor()
208            controller = BluetoothChooserController(self, model=bluetoothDevices)
209            response = controller.exec_()
210            if response:
211                address, name = controller.getSelectedBluetoothAddress()
212                Logger().debug("PluginsController.__onChooseBluetoothButtonClicked(): address=%s, name=%s" % (address, name))
213                self._view.bluetoothDeviceAddressLineEdit.setText(address)
214            controller.shutdown()
215
216    # Interface
217    def selectTab(self, tabIndex, disable=False):
218        """ Select the specified tab.
219
220        @param tabIndex: page num
221        @type tabIndex: int
222
223        @param disable: if True, disable all other pages
224        @type disable: bool
225        """
226        self._view.tabWidget.setCurrentIndex(tabIndex)
227        for index in xrange(self._view.tabWidget.count()):
228            self._view.tabWidget.setTabEnabled(index, tabIndex == index)
229
230    def refreshView(self):
231
232        # Plugins tab
233        yawAxisPlugins = PluginsManager ().getList('yawAxis')
234        if yawAxisPlugins:
235            for model, controller in yawAxisPlugins:
236                self._view.yawAxisComboBox.addItem(model.name)
237            selectedPluginName = ConfigManager().get('Plugins/PLUGIN_YAW_AXIS')
238            self._view.yawAxisComboBox.setCurrentIndex(self._view.yawAxisComboBox.findText(selectedPluginName))
239            selectedPlugin = PluginsManager ().get('yawAxis', selectedPluginName)[0]
240            if hasattr(selectedPlugin, '_driver'):
241                self._view.yawAxisDriverComboBox.setEnabled(True)
242                driverType = selectedPlugin._config['DRIVER_TYPE']
243                self._view.yawAxisDriverComboBox.setCurrentIndex(config.DRIVER_INDEX[driverType])
244            else:
245                self._view.yawAxisDriverComboBox.setEnabled(False)
246                #self._view.yawAxisDriverComboBox.setCurrentIndex(-1)
247
248        pitchAxisPlugins = PluginsManager ().getList('pitchAxis')
249        if pitchAxisPlugins:
250            for model, controller in pitchAxisPlugins:
251                self._view.pitchAxisComboBox.addItem(model.name)
252            selectedPluginName = ConfigManager().get('Plugins/PLUGIN_PITCH_AXIS')
253            self._view.pitchAxisComboBox.setCurrentIndex(self._view.pitchAxisComboBox.findText(selectedPluginName))
254            selectedPlugin = PluginsManager ().get('pitchAxis', selectedPluginName)[0]
255            if hasattr(selectedPlugin, '_driver'):
256                self._view.pitchAxisDriverComboBox.setEnabled(True)
257                driverType = selectedPlugin._config['DRIVER_TYPE']
258                self._view.pitchAxisDriverComboBox.setCurrentIndex(config.DRIVER_INDEX[driverType])
259            else:
260                self._view.pitchAxisDriverComboBox.setEnabled(False)
261                #self._view.pitchAxisDriverComboBox.setCurrentIndex(-1)
262
263        shutterPlugins = PluginsManager ().getList('shutter')
264        if shutterPlugins:
265            for model, controller in shutterPlugins:
266                self._view.shutterComboBox.addItem(model.name)
267            selectedPluginName = ConfigManager().get('Plugins/PLUGIN_SHUTTER')
268            self._view.shutterComboBox.setCurrentIndex(self._view.shutterComboBox.findText(selectedPluginName))
269            selectedPlugin = PluginsManager ().get('shutter', selectedPluginName)[0]
270            if hasattr(selectedPlugin, '_driver'):
271                self._view.shutterDriverComboBox.setEnabled(True)
272                driverType = selectedPlugin._config['DRIVER_TYPE']
273                self._view.shutterDriverComboBox.setCurrentIndex(config.DRIVER_INDEX[driverType])
274            else:
275                self._view.shutterDriverComboBox.setEnabled(False)
276                #self._view.shutterDriverComboBox.setCurrentIndex(-1)
277
278        # Drivers tab
279        self._view.bluetoothDeviceAddressLineEdit.setText(ConfigManager().get('Plugins/HARDWARE_BLUETOOTH_DEVICE_ADDRESS'))
280        self._view.serialPortLineEdit.setText(ConfigManager().get('Plugins/HARDWARE_SERIAL_PORT'))
281        self._view.ethernetHostLineEdit.setText(ConfigManager().get('Plugins/HARDWARE_ETHERNET_HOST'))
282        self._view.ethernetPortSpinBox.setValue(ConfigManager().getInt('Plugins/HARDWARE_ETHERNET_PORT'))
283
284    def getSelectedTab(self):
285        """ Return the selected tab.
286        """
287        return self._view.tabWidget.currentIndex()
288
289    def setSelectedTab(self, index):
290        """ Set the tab to be selected.
291        """
292        self._view.tabWidget.setCurrentIndex(index)
Note: See TracBrowser for help on using the repository browser.