Use QApplication.instance() instead of qApp because qApp seems to ignore the attribut...
[bertos.git] / wizard / BEditingDialog.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 # This file is part of BeRTOS.
5 #
6 # Bertos is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 #
20 # As a special exception, you may use this file as part of a free software
21 # library without restriction.  Specifically, if other files instantiate
22 # templates or use macros or inline functions from this file, or you compile
23 # this file and link it with other files to produce an executable, this
24 # file does not by itself cause the resulting executable to be covered by
25 # the GNU General Public License.  This exception does not however
26 # invalidate any other reasons why the executable file might be covered by
27 # the GNU General Public License.
28 #
29 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)
30 #
31 # $Id$
32 #
33 # Author: Lorenzo Berni <duplo@develer.com>
34 #
35
36 import sys
37 import os
38
39 from PyQt4.QtCore import *
40 from PyQt4.QtGui import *
41
42 from bertos_utils import bertosVersion, getToolchainName, createBertosProject
43 from BToolchainPage import BToolchainPage
44 from BVersionPage import BVersionPage
45
46 from BProject import BProject
47
48 import qvariant_converter
49 from BModulePage import BModulePage
50 import bertos_utils
51
52 class BEditingDialog(QDialog):
53
54     def __init__(self, parent=None):
55         QDialog.__init__(self, parent)
56         self.setupUi()
57         self.connectSignals()
58         self.module_page.reloadData()
59         self.setFrequency()
60     
61     def setupUi(self):
62         layout = QVBoxLayout()
63         self.module_page = BModulePage()
64         layout.addWidget(self.module_page)
65         frequency_layout = QHBoxLayout()
66         frequency_layout.addWidget(QLabel(self.tr("CPU frequency")))
67         self.cpu_frequency_spinbox = QDoubleSpinBox()
68         self.cpu_frequency_spinbox.setSuffix("Hz")
69         self.cpu_frequency_spinbox.setRange(1, 1000000000)
70         self.cpu_frequency_spinbox.setSingleStep(1000)
71         self.cpu_frequency_spinbox.setDecimals(0)
72         frequency_layout.addWidget(self.cpu_frequency_spinbox)
73         frequency_layout.addStretch()
74         layout.addLayout(frequency_layout)
75         button_layout = QHBoxLayout()
76         self.advanced_button = QToolButton()
77         self.setupMenu()
78         self.advanced_button.setMenu(self.menu)
79         self.advanced_button.setPopupMode(QToolButton.InstantPopup)
80         self.advanced_button.setText(self.tr("Advanced"))
81         button_layout.addWidget(self.advanced_button)
82         button_layout.addStretch()
83         self.cancel_button = QPushButton(self.tr("Cancel"))
84         button_layout.addWidget(self.cancel_button)
85         self.apply_button = QPushButton(self.tr("Apply"))
86         button_layout.addWidget(self.apply_button)
87         layout.addLayout(button_layout)
88         self.setLayout(layout)
89         self.setWindowTitle(self.tr("Edit \"%1\" project").arg(os.path.basename(self.module_page.projectInfo("PROJECT_PATH"))))
90         self.setWindowIcon(QIcon(":/images/appicon.png"))
91
92     def setupMenu(self):
93         self.menu = QMenu(self.tr("Advanced options"))
94         self.change_toolchain = QAction(self.tr("Change toolchain"), self)
95         self.change_bertos_version = QAction(self.tr("Change BeRTOS version"), self)
96         self.menu.addAction(self.change_toolchain)
97         self.menu.addAction(self.change_bertos_version)
98
99     def connectSignals(self):
100         self.connect(self.change_toolchain, SIGNAL("triggered(bool)"), self.changeToolchain)
101         self.connect(self.change_bertos_version, SIGNAL("triggered(bool)"), self.changeBertosVersion)
102         self.connect(self.apply_button, SIGNAL("clicked()"), self.apply)
103         self.connect(self.cancel_button, SIGNAL("clicked()"), self.reject)
104         self.connect(self.cpu_frequency_spinbox, SIGNAL("valueChanged(double)"), self.frequencyChanged)
105     
106     def setFrequency(self):
107         frequency = long(self.module_page.projectInfo("SELECTED_FREQ"))
108         self.cpu_frequency_spinbox.setValue(frequency)
109
110     def frequencyChanged(self, frequency):
111         frequency = unicode(long(frequency))
112         self.module_page.setProjectInfo("SELECTED_FREQ", frequency)
113
114     def changeToolchain(self):
115         dialog = BToolchainDialog()
116         if dialog.exec_():
117             toolchain = qvariant_converter.getStringDict(dialog.toolchain_page.currentItem().data(Qt.UserRole))
118             dialog.toolchain_page.setProjectInfo("TOOLCHAIN", toolchain)
119     
120     def changeBertosVersion(self):
121         current_version = self.module_page.projectInfo("SOURCES_PATH")
122         dialog = BVersionDialog()
123         if dialog.exec_():
124             version = qvariant_converter.getString(dialog.version_page.currentItem().data(Qt.UserRole))
125             if QMessageBox.question(
126                 dialog.version_page,
127                 self.tr("BeRTOS version update"),
128                 self.tr("Changing the BeRTOS version will destroy all the modification done on the BeRTOS sources"),
129                 QMessageBox.Ok | QMessageBox.Cancel
130             ) == QMessageBox.Ok:
131                 try:
132                     qApp.setOverrideCursor(QCursor(Qt.WaitCursor))
133                     dialog.version_page.setProjectInfo("SOURCES_PATH", version)
134                     dialog.version_page.setProjectInfo("OLD_SOURCES_PATH", current_version)
135                     enabled_modules = bertos_utils.enabledModules(dialog.version_page.project())
136                     old_configuration = dialog.version_page.projectInfo("CONFIGURATIONS")
137                     dialog.version_page.project().loadSourceTree()
138                     QApplication.instance().project.loadModuleData()
139                     new_configuration = dialog.version_page.projectInfo("CONFIGURATIONS")
140                     merged_configuration = {}
141                     for conf in new_configuration:
142                         if conf in old_configuration:
143                             configuration = bertos_utils.updateConfigurationValues(new_configuration[conf], old_configuration[conf])
144                         else:
145                             configuration = new_configuration[conf]
146                         merged_configuration[conf] = configuration
147                     dialog.version_page.setProjectInfo("CONFIGURATIONS", merged_configuration)
148                     bertos_utils.setEnabledModules(dialog.version_page.project(), enabled_modules)
149                     self.module_page.fillModuleTree()
150                 finally:
151                     qApp.restoreOverrideCursor()
152             else:
153                 # Rollback version to the previous selected one.
154                 dialog.version_page.setProjectInfo("SOURCES_PATH", current_version)
155
156     def apply(self):
157         try:
158             qApp.setOverrideCursor(QCursor(Qt.WaitCursor))
159             createBertosProject(self.module_page.project(), edit=True)
160         finally:
161             qApp.restoreOverrideCursor()
162         self.accept()
163
164     def toolchains(self):
165         return self.module_page.toolchains()
166     
167     def currentToolchain(self):
168         return self.module_page.projectInfo("TOOLCHAIN")
169     
170     def setCurrentToolchain(self, toolchain):
171         self.module_page.setProjectInfo("TOOLCHAIN", toolchain)
172
173     def versions(self):
174         return self.module_page.versions()
175
176     def currentVersion(self):
177         return self.module_page.projectInfo("SOURCES_PATH")
178     
179     def setCurrentVersion(self, version):
180         self.module_page.setProjectInfo("SOURCES_PATH", version)
181
182 class BToolchainDialog(QDialog):
183     def __init__(self):
184         QDialog.__init__(self)
185         self.setWindowIcon(QIcon(":/images/appicon.png"))
186         layout = QVBoxLayout()
187         toolchain_page = BToolchainPage()
188         current_toolchain = toolchain_page.projectInfo("TOOLCHAIN")
189         toolchain_page.reloadData()
190         # TODO: to be moved in BToolchainPage
191         for toolchain_row in range(toolchain_page.pageContent.toolchainList.count()):
192             toolchain = qvariant_converter.getStringDict(toolchain_page.pageContent.toolchainList.item(toolchain_row).data(Qt.UserRole))
193             if current_toolchain and toolchain["path"] == current_toolchain["path"]:
194                 toolchain_page.pageContent.toolchainList.setCurrentRow(toolchain_row)
195                 toolchain_page.selectionChanged()
196                 break
197         self.toolchain_page = toolchain_page
198         layout.addWidget(toolchain_page)
199         button_layout = QHBoxLayout()
200         button_layout.addStretch()
201         cancel_button = QPushButton(self.tr("Cancel")) 
202         button_layout.addWidget(cancel_button)
203         ok_button = QPushButton(self.tr("Ok"))
204         button_layout.addWidget(ok_button)
205         self.connect(cancel_button, SIGNAL("clicked()"), self.reject)
206         layout.addLayout(button_layout)
207         self.setLayout(layout)
208         self.connect(ok_button, SIGNAL("clicked()"), self.accept)
209         self.setWindowTitle(self.tr("Change toolchain"))
210
211 class BVersionDialog(QDialog):
212     def __init__(self):
213         QDialog.__init__(self)
214         self.setWindowIcon(QIcon(":/images/appicon.png"))
215         layout = QVBoxLayout()
216         version_page = BVersionPage(edit=True)
217         version_page.reloadData()
218         self.version_page = version_page
219         layout.addWidget(version_page)
220         button_layout = QHBoxLayout()
221         button_layout.addStretch()
222         cancel_button = QPushButton(self.tr("Cancel")) 
223         button_layout.addWidget(cancel_button)
224         ok_button = QPushButton(self.tr("Ok"))
225         button_layout.addWidget(ok_button)
226         self.connect(cancel_button, SIGNAL("clicked()"), self.reject)
227         layout.addLayout(button_layout)
228         self.setLayout(layout)
229         self.connect(ok_button, SIGNAL("clicked()"), self.accept)
230         current_version = version_page.projectInfo("SOURCES_PATH")
231         self.setWindowTitle(self.tr("Change BeRTOS version"))
232
233
234 def main():
235     if len(sys.argv) > 1:
236         project_file = sys.argv[1]
237     else:
238         print "Invalid usage: use <command> project_file"
239         sys.exit()
240     app = QApplication([])
241     app.project = BProject(project_file)
242     app.settings = QSettings("Develer", "Bertos Configurator")
243     dialog = BEditingDialog()
244     dialog.show()
245     sys.exit(app.exec_())
246
247 if __name__ == "__main__":
248     main()