e98edf861ce4329d2bd21c4d5e3c7ff81ea39848
[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 loadBertosProject, bertosVersion, getToolchainName, createBertosProject
43 from BToolchainPage import BToolchainPage
44 from BVersionPage import BVersionPage
45 import qvariant_converter
46 import BModulePage
47 import bertos_utils
48
49 class BEditingDialog(QDialog):
50
51     def __init__(self, parent=None):
52         QDialog.__init__(self, parent)
53         self.setupUi()
54         self.connectSignals()
55         self.module_page.reloadData()
56         self.setFrequency()
57     
58     def setupUi(self):
59         layout = QVBoxLayout()
60         self.module_page = BModulePage.BModulePage()
61         layout.addWidget(self.module_page)
62         frequency_layout = QHBoxLayout()
63         frequency_layout.addWidget(QLabel(self.tr("CPU frequency")))
64         self.cpu_frequency_spinbox = QDoubleSpinBox()
65         self.cpu_frequency_spinbox.setSuffix("Hz")
66         self.cpu_frequency_spinbox.setRange(1, 1000000000)
67         self.cpu_frequency_spinbox.setSingleStep(1000)
68         self.cpu_frequency_spinbox.setDecimals(0)
69         frequency_layout.addWidget(self.cpu_frequency_spinbox)
70         frequency_layout.addStretch()
71         layout.addLayout(frequency_layout)
72         button_layout = QHBoxLayout()
73         self.advanced_button = QToolButton()
74         self.setupMenu()
75         self.advanced_button.setMenu(self.menu)
76         self.advanced_button.setPopupMode(QToolButton.InstantPopup)
77         self.advanced_button.setText(self.tr("Advanced"))
78         button_layout.addWidget(self.advanced_button)
79         button_layout.addStretch()
80         self.cancel_button = QPushButton(self.tr("Cancel"))
81         button_layout.addWidget(self.cancel_button)
82         self.apply_button = QPushButton(self.tr("Apply"))
83         button_layout.addWidget(self.apply_button)
84         layout.addLayout(button_layout)
85         self.setLayout(layout)
86         self.setWindowTitle(self.tr("Edit \"%1\" project").arg(os.path.basename(self.module_page.projectInfo("PROJECT_PATH"))))
87
88     def setupMenu(self):
89         self.menu = QMenu(self.tr("Advanced options"))
90         self.change_toolchain = QAction(self.tr("Change toolchain"), self)
91         self.change_bertos_version = QAction(self.tr("Change BeRTOS version"), self)
92         self.menu.addAction(self.change_toolchain)
93         self.menu.addAction(self.change_bertos_version)
94
95     def connectSignals(self):
96         self.connect(self.change_toolchain, SIGNAL("triggered(bool)"), self.changeToolchain)
97         self.connect(self.change_bertos_version, SIGNAL("triggered(bool)"), self.changeBertosVersion)
98         self.connect(self.apply_button, SIGNAL("clicked()"), self.apply)
99         self.connect(self.cancel_button, SIGNAL("clicked()"), self.reject)
100         self.connect(self.cpu_frequency_spinbox, SIGNAL("valueChanged(double)"), self.frequencyChanged)
101     
102     def setFrequency(self):
103         frequency = long(self.module_page.projectInfo("SELECTED_FREQ"))
104         self.cpu_frequency_spinbox.setValue(frequency)
105
106     def frequencyChanged(self, frequency):
107         frequency = unicode(long(frequency))
108         self.module_page.setProjectInfo("SELECTED_FREQ", frequency)
109
110     def changeToolchain(self):
111         dialog = QDialog()
112         layout = QVBoxLayout()
113         toolchain_page = BToolchainPage()
114         current_toolchain = toolchain_page.projectInfo("TOOLCHAIN")
115         toolchain_page.reloadData()
116         # TODO: to be moved in BToolchainPage
117         for toolchain_row in range(toolchain_page.pageContent.toolchainList.count()):
118             toolchain = qvariant_converter.getStringDict(toolchain_page.pageContent.toolchainList.item(toolchain_row).data(Qt.UserRole))
119             if toolchain["path"] == current_toolchain["path"]:
120                 toolchain_page.pageContent.toolchainList.setCurrentRow(toolchain_row)
121                 toolchain_page.selectionChanged()
122                 break
123         layout.addWidget(toolchain_page)
124         button_layout = QHBoxLayout()
125         button_layout.addStretch()
126         cancel_button = QPushButton(self.tr("Cancel")) 
127         button_layout.addWidget(cancel_button)
128         ok_button = QPushButton(self.tr("Ok"))
129         button_layout.addWidget(ok_button)
130         dialog.connect(cancel_button, SIGNAL("clicked()"), dialog.reject)
131         layout.addLayout(button_layout)
132         dialog.setLayout(layout)
133         dialog.connect(ok_button, SIGNAL("clicked()"), dialog.accept)
134         dialog.setWindowTitle(self.tr("Change toolchain"))
135         if dialog.exec_():
136             toolchain = qvariant_converter.getStringDict(toolchain_page.currentItem().data(Qt.UserRole))
137             toolchain_page.setProjectInfo("TOOLCHAIN", toolchain)
138     
139     def changeBertosVersion(self):
140         dialog = QDialog()
141         layout = QVBoxLayout()
142         version_page = BVersionPage()
143         version_page.reloadData()
144         layout.addWidget(version_page)
145         button_layout = QHBoxLayout()
146         button_layout.addStretch()
147         cancel_button = QPushButton(self.tr("Cancel")) 
148         button_layout.addWidget(cancel_button)
149         ok_button = QPushButton(self.tr("Ok"))
150         button_layout.addWidget(ok_button)
151         dialog.connect(cancel_button, SIGNAL("clicked()"), dialog.reject)
152         layout.addLayout(button_layout)
153         dialog.setLayout(layout)
154         dialog.connect(ok_button, SIGNAL("clicked()"), dialog.accept)
155         current_version = version_page.projectInfo("SOURCES_PATH")
156         dialog.setWindowTitle(self.tr("Change BeRTOS version"))
157         if dialog.exec_():
158             version = qvariant_converter.getString(version_page.currentItem().data(Qt.UserRole))
159             if version != current_version:
160                 if QMessageBox.question(
161                     version_page,
162                     self.tr("BeRTOS version update"),
163                     self.tr("Changing the BeRTOS version will destroy all the modification done on the BeRTOS sources"),
164                     QMessageBox.Ok | QMessageBox.Cancel
165                 ) == QMessageBox.Ok:
166                     version_page.setProjectInfo("SOURCES_PATH", version)
167                     version_page.setProjectInfo("OLD_SOURCES_PATH", current_version)
168                     enabled_modules = bertos_utils.enabledModules(version_page.project())
169                     old_configuration = version_page.projectInfo("CONFIGURATIONS")
170                     bertos_utils.loadSourceTree(version_page.project())
171                     bertos_utils.loadModuleData(version_page.project())
172                     new_configuration = version_page.projectInfo("CONFIGURATIONS")
173                     merged_configuration = {}
174                     for conf in new_configuration:
175                         if conf in old_configuration:
176                             configuration = bertos_utils.updateConfigurationValues(new_configuration[conf], old_configuration[conf])
177                         else:
178                             configuration = new_configuration[conf]
179                         merged_configuration[conf] = configuration
180                     version_page.setProjectInfo("CONFIGURATIONS", merged_configuration)
181                     bertos_utils.setEnabledModules(version_page.project(), enabled_modules)
182                     self.module_page.fillModuleTree()
183                 else:
184                     # Rollback version to the previous selected one.
185                     version_page.setProjectInfo("SOURCES_PATH", current_version)
186
187     def apply(self):
188         createBertosProject(self.module_page.project(), edit=True)
189         self.accept()
190
191     def toolchains(self):
192         return self.module_page.toolchains()
193     
194     def currentToolchain(self):
195         return self.module_page.projectInfo("TOOLCHAIN")
196     
197     def setCurrentToolchain(self, toolchain):
198         self.module_page.setProjectInfo("TOOLCHAIN", toolchain)
199
200     def versions(self):
201         return self.module_page.versions()
202
203     def currentVersion(self):
204         return self.module_page.projectInfo("SOURCES_PATH")
205     
206     def setCurrentVersion(self, version):
207         self.module_page.setProjectInfo("SOURCES_PATH", version)
208
209
210
211 def main():
212     if len(sys.argv) > 1:
213         project_file = sys.argv[1]
214     else:
215         print "Invalid usage: use <command> project_file"
216         sys.exit()
217     app = QApplication([])
218     app.project = loadBertosProject(project_file)
219     app.settings = QSettings("Develer", "Bertos Configurator")
220     dialog = BEditingDialog()
221     dialog.show()
222     sys.exit(app.exec_())
223
224 if __name__ == "__main__":
225     main()