Move loadSourceTree and findDefinitions function into BProject class (to avoid useles...
[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         self.setWindowIcon(QIcon(":/images/appicon.png"))
88
89     def setupMenu(self):
90         self.menu = QMenu(self.tr("Advanced options"))
91         self.change_toolchain = QAction(self.tr("Change toolchain"), self)
92         self.change_bertos_version = QAction(self.tr("Change BeRTOS version"), self)
93         self.menu.addAction(self.change_toolchain)
94         self.menu.addAction(self.change_bertos_version)
95
96     def connectSignals(self):
97         self.connect(self.change_toolchain, SIGNAL("triggered(bool)"), self.changeToolchain)
98         self.connect(self.change_bertos_version, SIGNAL("triggered(bool)"), self.changeBertosVersion)
99         self.connect(self.apply_button, SIGNAL("clicked()"), self.apply)
100         self.connect(self.cancel_button, SIGNAL("clicked()"), self.reject)
101         self.connect(self.cpu_frequency_spinbox, SIGNAL("valueChanged(double)"), self.frequencyChanged)
102     
103     def setFrequency(self):
104         frequency = long(self.module_page.projectInfo("SELECTED_FREQ"))
105         self.cpu_frequency_spinbox.setValue(frequency)
106
107     def frequencyChanged(self, frequency):
108         frequency = unicode(long(frequency))
109         self.module_page.setProjectInfo("SELECTED_FREQ", frequency)
110
111     def changeToolchain(self):
112         dialog = BToolchainDialog()
113         if dialog.exec_():
114             toolchain = qvariant_converter.getStringDict(dialog.toolchain_page.currentItem().data(Qt.UserRole))
115             dialog.toolchain_page.setProjectInfo("TOOLCHAIN", toolchain)
116     
117     def changeBertosVersion(self):
118         current_version = self.module_page.projectInfo("SOURCES_PATH")
119         dialog = BVersionDialog()
120         if dialog.exec_():
121             version = qvariant_converter.getString(dialog.version_page.currentItem().data(Qt.UserRole))
122             if QMessageBox.question(
123                 dialog.version_page,
124                 self.tr("BeRTOS version update"),
125                 self.tr("Changing the BeRTOS version will destroy all the modification done on the BeRTOS sources"),
126                 QMessageBox.Ok | QMessageBox.Cancel
127             ) == QMessageBox.Ok:
128                 qApp.setOverrideCursor(QCursor(Qt.WaitCursor))
129                 dialog.version_page.setProjectInfo("SOURCES_PATH", version)
130                 dialog.version_page.setProjectInfo("OLD_SOURCES_PATH", current_version)
131                 enabled_modules = bertos_utils.enabledModules(dialog.version_page.project())
132                 old_configuration = dialog.version_page.projectInfo("CONFIGURATIONS")
133                 dialog.version_page.project().loadSourceTree()
134                 bertos_utils.loadModuleData(dialog.version_page.project())
135                 new_configuration = dialog.version_page.projectInfo("CONFIGURATIONS")
136                 merged_configuration = {}
137                 for conf in new_configuration:
138                     if conf in old_configuration:
139                         configuration = bertos_utils.updateConfigurationValues(new_configuration[conf], old_configuration[conf])
140                     else:
141                         configuration = new_configuration[conf]
142                     merged_configuration[conf] = configuration
143                 dialog.version_page.setProjectInfo("CONFIGURATIONS", merged_configuration)
144                 bertos_utils.setEnabledModules(dialog.version_page.project(), enabled_modules)
145                 self.module_page.fillModuleTree()
146                 qApp.restoreOverrideCursor()
147             else:
148                 # Rollback version to the previous selected one.
149                 dialog.version_page.setProjectInfo("SOURCES_PATH", current_version)
150
151     def apply(self):
152         qApp.setOverrideCursor(QCursor(Qt.WaitCursor))
153         def foo():
154             print "qui"
155             createBertosProject(self.module_page.project(), edit=True)
156         import cProfile
157         print "quo"
158         cProfile.runctx("foo()", globals(), locals(), sort=1)
159         qApp.restoreOverrideCursor()
160         self.accept()
161
162     def toolchains(self):
163         return self.module_page.toolchains()
164     
165     def currentToolchain(self):
166         return self.module_page.projectInfo("TOOLCHAIN")
167     
168     def setCurrentToolchain(self, toolchain):
169         self.module_page.setProjectInfo("TOOLCHAIN", toolchain)
170
171     def versions(self):
172         return self.module_page.versions()
173
174     def currentVersion(self):
175         return self.module_page.projectInfo("SOURCES_PATH")
176     
177     def setCurrentVersion(self, version):
178         self.module_page.setProjectInfo("SOURCES_PATH", version)
179
180 class BToolchainDialog(QDialog):
181     def __init__(self):
182         QDialog.__init__(self)
183         self.setWindowIcon(QIcon(":/images/appicon.png"))
184         layout = QVBoxLayout()
185         toolchain_page = BToolchainPage()
186         current_toolchain = toolchain_page.projectInfo("TOOLCHAIN")
187         toolchain_page.reloadData()
188         # TODO: to be moved in BToolchainPage
189         for toolchain_row in range(toolchain_page.pageContent.toolchainList.count()):
190             toolchain = qvariant_converter.getStringDict(toolchain_page.pageContent.toolchainList.item(toolchain_row).data(Qt.UserRole))
191             if current_toolchain and toolchain["path"] == current_toolchain["path"]:
192                 toolchain_page.pageContent.toolchainList.setCurrentRow(toolchain_row)
193                 toolchain_page.selectionChanged()
194                 break
195         self.toolchain_page = toolchain_page
196         layout.addWidget(toolchain_page)
197         button_layout = QHBoxLayout()
198         button_layout.addStretch()
199         cancel_button = QPushButton(self.tr("Cancel")) 
200         button_layout.addWidget(cancel_button)
201         ok_button = QPushButton(self.tr("Ok"))
202         button_layout.addWidget(ok_button)
203         self.connect(cancel_button, SIGNAL("clicked()"), self.reject)
204         layout.addLayout(button_layout)
205         self.setLayout(layout)
206         self.connect(ok_button, SIGNAL("clicked()"), self.accept)
207         self.setWindowTitle(self.tr("Change toolchain"))
208
209 class BVersionDialog(QDialog):
210     def __init__(self):
211         QDialog.__init__(self)
212         self.setWindowIcon(QIcon(":/images/appicon.png"))
213         layout = QVBoxLayout()
214         version_page = BVersionPage(edit=True)
215         version_page.reloadData()
216         self.version_page = version_page
217         layout.addWidget(version_page)
218         button_layout = QHBoxLayout()
219         button_layout.addStretch()
220         cancel_button = QPushButton(self.tr("Cancel")) 
221         button_layout.addWidget(cancel_button)
222         ok_button = QPushButton(self.tr("Ok"))
223         button_layout.addWidget(ok_button)
224         self.connect(cancel_button, SIGNAL("clicked()"), self.reject)
225         layout.addLayout(button_layout)
226         self.setLayout(layout)
227         self.connect(ok_button, SIGNAL("clicked()"), self.accept)
228         current_version = version_page.projectInfo("SOURCES_PATH")
229         self.setWindowTitle(self.tr("Change BeRTOS version"))
230
231
232 def main():
233     if len(sys.argv) > 1:
234         project_file = sys.argv[1]
235     else:
236         print "Invalid usage: use <command> project_file"
237         sys.exit()
238     app = QApplication([])
239     app.project = loadBertosProject(project_file)
240     app.settings = QSettings("Develer", "Bertos Configurator")
241     dialog = BEditingDialog()
242     dialog.show()
243     sys.exit(app.exec_())
244
245 if __name__ == "__main__":
246     main()