Refactor to use new protocol module and sipo.
[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 #
32 # Author: Lorenzo Berni <duplo@develer.com>
33 #
34
35 import sys
36 import os
37
38 from PyQt4.QtCore import *
39 from PyQt4.QtGui import *
40
41 from bertos_utils import bertosVersion, getToolchainName
42 from BToolchainPage import BToolchainPage
43 from BVersionPage import BVersionPage
44
45 from BProject import BProject
46
47 import qvariant_converter
48 from BModulePage import BModulePage
49 import bertos_utils
50
51 class BEditingDialog(QDialog):
52
53     def __init__(self, parent=None):
54         QDialog.__init__(self, parent)
55         self.setupUi()
56         self.connectSignals()
57         self.module_page.reloadData()
58         self.setFrequency()
59     
60     def setupUi(self):
61         layout = QVBoxLayout()
62         self.module_page = BModulePage()
63         layout.addWidget(self.module_page)
64         frequency_layout = QHBoxLayout()
65         frequency_layout.addWidget(QLabel(self.tr("CPU frequency")))
66         self.cpu_frequency_spinbox = QDoubleSpinBox()
67         self.cpu_frequency_spinbox.setSuffix("Hz")
68         self.cpu_frequency_spinbox.setRange(1, 1000000000)
69         self.cpu_frequency_spinbox.setSingleStep(1000)
70         self.cpu_frequency_spinbox.setDecimals(0)
71         frequency_layout.addWidget(self.cpu_frequency_spinbox)
72         frequency_layout.addStretch()
73         layout.addLayout(frequency_layout)
74         button_layout = QHBoxLayout()
75         self.advanced_button = QToolButton()
76         self.setupMenu()
77         self.advanced_button.setMenu(self.menu)
78         self.advanced_button.setPopupMode(QToolButton.InstantPopup)
79         self.advanced_button.setText(self.tr("Advanced"))
80         button_layout.addWidget(self.advanced_button)
81         button_layout.addStretch()
82         self.cancel_button = QPushButton(self.tr("Cancel"))
83         button_layout.addWidget(self.cancel_button)
84         self.apply_button = QPushButton(self.tr("Apply"))
85         button_layout.addWidget(self.apply_button)
86         layout.addLayout(button_layout)
87         self.setLayout(layout)
88         self.setWindowTitle(self.tr("Edit \"%1\" project").arg(os.path.basename(self.module_page.projectInfo("PROJECT_PATH"))))
89         self.setWindowIcon(QIcon(":/images/appicon.png"))
90
91     def setupMenu(self):
92         self.menu = QMenu(self.tr("Advanced options"))
93         self.change_toolchain = QAction(self.tr("Change toolchain"), self)
94         self.change_bertos_version = QAction(self.tr("Change BeRTOS version"), self)
95         self.menu.addAction(self.change_toolchain)
96         self.menu.addAction(self.change_bertos_version)
97
98     def connectSignals(self):
99         self.connect(self.change_toolchain, SIGNAL("triggered(bool)"), self.changeToolchain)
100         self.connect(self.change_bertos_version, SIGNAL("triggered(bool)"), self.changeBertosVersion)
101         self.connect(self.apply_button, SIGNAL("clicked()"), self.apply)
102         self.connect(self.cancel_button, SIGNAL("clicked()"), self.reject)
103         self.connect(self.cpu_frequency_spinbox, SIGNAL("valueChanged(double)"), self.frequencyChanged)
104     
105     def setFrequency(self):
106         frequency = long(self.module_page.projectInfo("SELECTED_FREQ"))
107         self.cpu_frequency_spinbox.setValue(frequency)
108
109     def frequencyChanged(self, frequency):
110         frequency = unicode(long(frequency))
111         self.module_page.setProjectInfo("SELECTED_FREQ", frequency)
112
113     def changeToolchain(self):
114         dialog = BToolchainDialog()
115         if dialog.exec_():
116             toolchain = qvariant_converter.getStringDict(dialog.toolchain_page.currentItem().data(Qt.UserRole))
117             dialog.toolchain_page.setProjectInfo("TOOLCHAIN", toolchain)
118     
119     def changeBertosVersion(self):
120         current_version = self.module_page.projectInfo("BERTOS_PATH")
121         dialog = BVersionDialog()
122         if dialog.exec_():
123             version = qvariant_converter.getString(dialog.version_page.currentItem().data(Qt.UserRole))
124             if QMessageBox.question(
125                 dialog.version_page,
126                 self.tr("BeRTOS version update"),
127                 self.tr("Changing the BeRTOS version will destroy all the modification done on the BeRTOS sources"),
128                 QMessageBox.Ok | QMessageBox.Cancel
129             ) == QMessageBox.Ok:
130                 try:
131                     qApp.setOverrideCursor(QCursor(Qt.WaitCursor))
132                     dialog.version_page.setProjectInfo("BERTOS_PATH", version)
133                     dialog.version_page.setProjectInfo("OLD_BERTOS_PATH", current_version)
134                     enabled_modules = bertos_utils.enabledModules(dialog.version_page.project)
135                     old_configuration = dialog.version_page.projectInfo("CONFIGURATIONS")
136                     dialog.version_page.project.loadSourceTree()
137                     QApplication.instance().project.reloadCpuInfo()
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                     dialog.version_page.project.setEnabledModules(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("BERTOS_PATH", current_version)
155
156     def apply(self):
157         try:
158             qApp.setOverrideCursor(QCursor(Qt.WaitCursor))
159             QApplication.instance().project.createBertosProject()
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("BERTOS_PATH")
178     
179     def setCurrentVersion(self, version):
180         self.module_page.setProjectInfo("BERTOS_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("BERTOS_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()