Add stub of sources merge functionality
[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
48 class BEditingDialog(QDialog):
49
50     def __init__(self, parent=None):
51         QDialog.__init__(self, parent)
52         self.setupUi()
53         self.connectSignals()
54         self.module_page.reloadData()
55     
56     def setupUi(self):
57         layout = QVBoxLayout()
58         self.module_page = BModulePage.BModulePage()
59         layout.addWidget(self.module_page)
60         button_layout = QHBoxLayout()
61         self.advanced_button = QToolButton()
62         self.setupMenu()
63         self.advanced_button.setMenu(self.menu)
64         self.advanced_button.setPopupMode(QToolButton.InstantPopup)
65         self.advanced_button.setText(self.tr("Advanced"))
66         button_layout.addWidget(self.advanced_button)
67         button_layout.addStretch()
68         self.cancel_button = QPushButton(self.tr("Cancel"))
69         button_layout.addWidget(self.cancel_button)
70         self.apply_button = QPushButton(self.tr("Apply"))
71         button_layout.addWidget(self.apply_button)
72         layout.addLayout(button_layout)
73         self.setLayout(layout)
74
75     def setupMenu(self):
76         self.menu = QMenu(self.tr("Advanced options"))
77         self.change_toolchain = QAction(self.tr("Change toolchain"), self)
78         self.change_bertos_version = QAction(self.tr("Change BeRTOS version"), self)
79         self.menu.addAction(self.change_toolchain)
80         self.menu.addAction(self.change_bertos_version)
81
82     def connectSignals(self):
83         self.connect(self.change_toolchain, SIGNAL("triggered(bool)"), self.changeToolchain)
84         self.connect(self.change_bertos_version, SIGNAL("triggered(bool)"), self.changeBertosVersion)
85         self.connect(self.apply_button, SIGNAL("clicked()"), self.apply)
86         self.connect(self.cancel_button, SIGNAL("clicked()"), self.reject)
87
88     def changeToolchain(self):
89         dialog = QDialog()
90         layout = QVBoxLayout()
91         toolchain_page = BToolchainPage()
92         toolchain_page.reloadData()
93         layout.addWidget(toolchain_page)
94         button_layout = QHBoxLayout()
95         button_layout.addStretch()
96         cancel_button = QPushButton(self.tr("Cancel")) 
97         button_layout.addWidget(cancel_button)
98         ok_button = QPushButton(self.tr("Ok"))
99         button_layout.addWidget(ok_button)
100         dialog.connect(cancel_button, SIGNAL("clicked()"), dialog.reject)
101         layout.addLayout(button_layout)
102         dialog.setLayout(layout)
103         dialog.connect(ok_button, SIGNAL("clicked()"), dialog.accept)
104         if dialog.exec_():
105             toolchain = qvariant_converter.getStringDict(toolchain_page.currentItem().data(Qt.UserRole))
106             toolchain_page.setProjectInfo("TOOLCHAIN", toolchain)
107     
108     def changeBertosVersion(self):
109         dialog = QDialog()
110         layout = QVBoxLayout()
111         version_page = BVersionPage()
112         version_page.reloadData()
113         layout.addWidget(version_page)
114         button_layout = QHBoxLayout()
115         button_layout.addStretch()
116         cancel_button = QPushButton(self.tr("Cancel")) 
117         button_layout.addWidget(cancel_button)
118         ok_button = QPushButton(self.tr("Ok"))
119         button_layout.addWidget(ok_button)
120         dialog.connect(cancel_button, SIGNAL("clicked()"), dialog.reject)
121         layout.addLayout(button_layout)
122         dialog.setLayout(layout)
123         dialog.connect(ok_button, SIGNAL("clicked()"), dialog.accept)
124         current_version = version_page.projectInfo("SOURCES_PATH")
125         if dialog.exec_():
126             version = qvariant_converter.getString(version_page.currentItem().data(Qt.UserRole))
127             if version != current_version:
128                 if QMessageBox.question(
129                     version_page,
130                     self.tr("BeRTOS version update"),
131                     self.tr("Changing the BeRTOS version will destroy all the modification done on the BeRTOS sources"),
132                     QMessageBox.Ok | QMessageBox.Cancel
133                 ) == QMessageBox.Ok:
134                     version_page.setProjectInfo("SOURCES_PATH", version)
135                     version_page.setProjectInfo("OLD_SOURCES_PATH", current_version)
136
137     def apply(self):
138         createBertosProject(self.module_page.project(), edit=True)
139         self.accept()
140
141     def toolchains(self):
142         return self.module_page.toolchains()
143     
144     def currentToolchain(self):
145         return self.module_page.projectInfo("TOOLCHAIN")
146     
147     def setCurrentToolchain(self, toolchain):
148         self.module_page.setProjectInfo("TOOLCHAIN", toolchain)
149
150     def versions(self):
151         return self.module_page.versions()
152
153     def currentVersion(self):
154         return self.module_page.projectInfo("SOURCES_PATH")
155     
156     def setCurrentVersion(self, version):
157         self.module_page.setProjectInfo("SOURCES_PATH", version)
158
159
160
161 def main():
162     if len(sys.argv) > 1:
163         project_file = sys.argv[1]
164     else:
165         print "Invalid usage: use <command> project_file"
166         sys.exit()
167     app = QApplication([])
168     app.project = loadBertosProject(project_file)
169     app.settings = QSettings("Develer", "Bertos Configurator")
170     dialog = BEditingDialog()
171     dialog.show()
172     sys.exit(app.exec_())
173
174 if __name__ == "__main__":
175     main()