Change the settings merge function
[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
76     def setupMenu(self):
77         self.menu = QMenu(self.tr("Advanced options"))
78         self.change_toolchain = QAction(self.tr("Change toolchain"), self)
79         self.change_bertos_version = QAction(self.tr("Change BeRTOS version"), self)
80         self.menu.addAction(self.change_toolchain)
81         self.menu.addAction(self.change_bertos_version)
82
83     def connectSignals(self):
84         self.connect(self.change_toolchain, SIGNAL("triggered(bool)"), self.changeToolchain)
85         self.connect(self.change_bertos_version, SIGNAL("triggered(bool)"), self.changeBertosVersion)
86         self.connect(self.apply_button, SIGNAL("clicked()"), self.apply)
87         self.connect(self.cancel_button, SIGNAL("clicked()"), self.reject)
88
89     def changeToolchain(self):
90         dialog = QDialog()
91         layout = QVBoxLayout()
92         toolchain_page = BToolchainPage()
93         toolchain_page.reloadData()
94         layout.addWidget(toolchain_page)
95         button_layout = QHBoxLayout()
96         button_layout.addStretch()
97         cancel_button = QPushButton(self.tr("Cancel")) 
98         button_layout.addWidget(cancel_button)
99         ok_button = QPushButton(self.tr("Ok"))
100         button_layout.addWidget(ok_button)
101         dialog.connect(cancel_button, SIGNAL("clicked()"), dialog.reject)
102         layout.addLayout(button_layout)
103         dialog.setLayout(layout)
104         dialog.connect(ok_button, SIGNAL("clicked()"), dialog.accept)
105         if dialog.exec_():
106             toolchain = qvariant_converter.getStringDict(toolchain_page.currentItem().data(Qt.UserRole))
107             toolchain_page.setProjectInfo("TOOLCHAIN", toolchain)
108     
109     def changeBertosVersion(self):
110         dialog = QDialog()
111         layout = QVBoxLayout()
112         version_page = BVersionPage()
113         version_page.reloadData()
114         layout.addWidget(version_page)
115         button_layout = QHBoxLayout()
116         button_layout.addStretch()
117         cancel_button = QPushButton(self.tr("Cancel")) 
118         button_layout.addWidget(cancel_button)
119         ok_button = QPushButton(self.tr("Ok"))
120         button_layout.addWidget(ok_button)
121         dialog.connect(cancel_button, SIGNAL("clicked()"), dialog.reject)
122         layout.addLayout(button_layout)
123         dialog.setLayout(layout)
124         dialog.connect(ok_button, SIGNAL("clicked()"), dialog.accept)
125         current_version = version_page.projectInfo("SOURCES_PATH")
126         if dialog.exec_():
127             version = qvariant_converter.getString(version_page.currentItem().data(Qt.UserRole))
128             if version != current_version:
129                 if QMessageBox.question(
130                     version_page,
131                     self.tr("BeRTOS version update"),
132                     self.tr("Changing the BeRTOS version will destroy all the modification done on the BeRTOS sources"),
133                     QMessageBox.Ok | QMessageBox.Cancel
134                 ) == QMessageBox.Ok:
135                     version_page.setProjectInfo("SOURCES_PATH", version)
136                     version_page.setProjectInfo("OLD_SOURCES_PATH", current_version)
137                     project = version_page.project()
138                     modules, lists, configurations, files = project.info("MODULES"), project.info("LISTS"), project.info("CONFIGURATIONS"), project.info("FILES")
139                     bertos_utils.loadSourceTree(version_page.project())
140                     bertos_utils.loadModuleData(version_page.project(), True)
141                     modules_, lists_, configurations_, files_ = project.info("MODULES"), project.info("LISTS"), project.info("CONFIGURATIONS"), project.info("FILES")
142                     self.module_page.fillModuleTree()
143                     print modules == modules_, lists == lists_, configurations == configurations_, files == files_
144
145     def apply(self):
146         createBertosProject(self.module_page.project(), edit=True)
147         self.accept()
148
149     def toolchains(self):
150         return self.module_page.toolchains()
151     
152     def currentToolchain(self):
153         return self.module_page.projectInfo("TOOLCHAIN")
154     
155     def setCurrentToolchain(self, toolchain):
156         self.module_page.setProjectInfo("TOOLCHAIN", toolchain)
157
158     def versions(self):
159         return self.module_page.versions()
160
161     def currentVersion(self):
162         return self.module_page.projectInfo("SOURCES_PATH")
163     
164     def setCurrentVersion(self, version):
165         self.module_page.setProjectInfo("SOURCES_PATH", version)
166
167
168
169 def main():
170     if len(sys.argv) > 1:
171         project_file = sys.argv[1]
172     else:
173         print "Invalid usage: use <command> project_file"
174         sys.exit()
175     app = QApplication([])
176     app.project = loadBertosProject(project_file)
177     app.settings = QSettings("Develer", "Bertos Configurator")
178     dialog = BEditingDialog()
179     dialog.show()
180     sys.exit(app.exec_())
181
182 if __name__ == "__main__":
183     main()