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