Clean up, and reoder the code. Add comments.
[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
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("BERTOS_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("BERTOS_PATH", version)
134                     dialog.version_page.setProjectInfo("OLD_BERTOS_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.reloadCpuInfo()
139                     QApplication.instance().project.loadModuleData()
140                     new_configuration = dialog.version_page.projectInfo("CONFIGURATIONS")
141                     merged_configuration = {}
142                     for conf in new_configuration:
143                         if conf in old_configuration:
144                             configuration = bertos_utils.updateConfigurationValues(new_configuration[conf], old_configuration[conf])
145                         else:
146                             configuration = new_configuration[conf]
147                         merged_configuration[conf] = configuration
148                     dialog.version_page.setProjectInfo("CONFIGURATIONS", merged_configuration)
149                     dialog.version_page.project.setEnabledModules(enabled_modules)
150                     self.module_page.fillModuleTree()
151                 finally:
152                     qApp.restoreOverrideCursor()
153             else:
154                 # Rollback version to the previous selected one.
155                 dialog.version_page.setProjectInfo("BERTOS_PATH", current_version)
156
157     def apply(self):
158         try:
159             qApp.setOverrideCursor(QCursor(Qt.WaitCursor))
160             QApplication.instance().project.createBertosProject()
161         finally:
162             qApp.restoreOverrideCursor()
163         self.accept()
164
165     def toolchains(self):
166         return self.module_page.toolchains()
167     
168     def currentToolchain(self):
169         return self.module_page.projectInfo("TOOLCHAIN")
170     
171     def setCurrentToolchain(self, toolchain):
172         self.module_page.setProjectInfo("TOOLCHAIN", toolchain)
173
174     def versions(self):
175         return self.module_page.versions()
176
177     def currentVersion(self):
178         return self.module_page.projectInfo("BERTOS_PATH")
179     
180     def setCurrentVersion(self, version):
181         self.module_page.setProjectInfo("BERTOS_PATH", version)
182
183 class BToolchainDialog(QDialog):
184     def __init__(self):
185         QDialog.__init__(self)
186         self.setWindowIcon(QIcon(":/images/appicon.png"))
187         layout = QVBoxLayout()
188         toolchain_page = BToolchainPage()
189         current_toolchain = toolchain_page.projectInfo("TOOLCHAIN")
190         toolchain_page.reloadData()
191         # TODO: to be moved in BToolchainPage
192         for toolchain_row in range(toolchain_page.pageContent.toolchainList.count()):
193             toolchain = qvariant_converter.getStringDict(toolchain_page.pageContent.toolchainList.item(toolchain_row).data(Qt.UserRole))
194             if current_toolchain and toolchain["path"] == current_toolchain["path"]:
195                 toolchain_page.pageContent.toolchainList.setCurrentRow(toolchain_row)
196                 toolchain_page.selectionChanged()
197                 break
198         self.toolchain_page = toolchain_page
199         layout.addWidget(toolchain_page)
200         button_layout = QHBoxLayout()
201         button_layout.addStretch()
202         cancel_button = QPushButton(self.tr("Cancel")) 
203         button_layout.addWidget(cancel_button)
204         ok_button = QPushButton(self.tr("Ok"))
205         button_layout.addWidget(ok_button)
206         self.connect(cancel_button, SIGNAL("clicked()"), self.reject)
207         layout.addLayout(button_layout)
208         self.setLayout(layout)
209         self.connect(ok_button, SIGNAL("clicked()"), self.accept)
210         self.setWindowTitle(self.tr("Change toolchain"))
211
212 class BVersionDialog(QDialog):
213     def __init__(self):
214         QDialog.__init__(self)
215         self.setWindowIcon(QIcon(":/images/appicon.png"))
216         layout = QVBoxLayout()
217         version_page = BVersionPage(edit=True)
218         version_page.reloadData()
219         self.version_page = version_page
220         layout.addWidget(version_page)
221         button_layout = QHBoxLayout()
222         button_layout.addStretch()
223         cancel_button = QPushButton(self.tr("Cancel")) 
224         button_layout.addWidget(cancel_button)
225         ok_button = QPushButton(self.tr("Ok"))
226         button_layout.addWidget(ok_button)
227         self.connect(cancel_button, SIGNAL("clicked()"), self.reject)
228         layout.addLayout(button_layout)
229         self.setLayout(layout)
230         self.connect(ok_button, SIGNAL("clicked()"), self.accept)
231         current_version = version_page.projectInfo("BERTOS_PATH")
232         self.setWindowTitle(self.tr("Change BeRTOS version"))
233
234
235 def main():
236     if len(sys.argv) > 1:
237         project_file = sys.argv[1]
238     else:
239         print "Invalid usage: use <command> project_file"
240         sys.exit()
241     app = QApplication([])
242     app.project = BProject(project_file)
243     app.settings = QSettings("Develer", "Bertos Configurator")
244     dialog = BEditingDialog()
245     dialog.show()
246     sys.exit(app.exec_())
247
248 if __name__ == "__main__":
249     main()