Add a stub of the editing 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 toolchain_validation import validateToolchain
44 import qvariant_converter
45 import BModulePage
46
47 class BEditingDialog(QDialog):
48
49     def __init__(self, parent=None):
50         QDialog.__init__(self, parent)
51         self.setupUi()
52         self.connectSignals()
53         self.module_page.reloadData()
54     
55     def setupUi(self):
56         layout = QVBoxLayout()
57         self.module_page = BModulePage.BModulePage()
58         layout.addWidget(self.module_page)
59         button_layout = QHBoxLayout()
60         self.advanced_button = QToolButton()
61         self.setupMenu()
62         self.advanced_button.setMenu(self.menu)
63         self.advanced_button.setPopupMode(QToolButton.InstantPopup)
64         self.advanced_button.setText(self.tr("Advanced"))
65         button_layout.addWidget(self.advanced_button)
66         button_layout.addStretch()
67         self.cancel_button = QPushButton(self.tr("Cancel"))
68         button_layout.addWidget(self.cancel_button)
69         self.apply_button = QPushButton(self.tr("Apply"))
70         button_layout.addWidget(self.apply_button)
71         layout.addLayout(button_layout)
72         self.setLayout(layout)
73
74     def setupMenu(self):
75         self.menu = QMenu(self.tr("Advanced options"))
76         self.setupToolchainMenu()
77         self.menu.addMenu(self.toolchain_menu)
78         self.setupVersionMenu()
79         self.menu.addMenu(self.version_menu)
80
81     def setupToolchainMenu(self):
82         self.toolchain_menu = QMenu(self.tr("select toolchain"))
83         self.toolchain_actions = []
84         action_group = QActionGroup(self.toolchain_menu)
85         for toolchain in sorted(self.toolchains()):
86             info = validateToolchain(toolchain)
87             if info[0]:
88                 name = getToolchainName(info[1])
89             else:
90                 name = toolchain
91             action = self.toolchain_menu.addAction(name)
92             action_group.addAction(action)
93             action.setCheckable(True)
94             action.setChecked(True if toolchain == self.currentToolchain()["path"] else False)
95             action.setData(qvariant_converter.convertString(toolchain))
96             self.toolchain_actions.append(action)
97
98     def setupVersionMenu(self):
99         self.version_menu = QMenu(self.tr("select BeRTOS version"))
100         self.version_actions = []
101         action_group = QActionGroup(self.version_menu)
102         versions = [(path, bertosVersion(path)) for path in self.versions()]
103         for path, version in versions: 
104             action = self.version_menu.addAction(version)
105             action_group.addAction(action)
106             action.setCheckable(True)
107             action.setChecked(True if path == self.currentVersion() else False)
108             action.setData(qvariant_converter.convertString(path))
109             self.version_actions.append(action)
110
111     def connectSignals(self):
112         for toolchain_action in self.toolchain_actions:
113             self.connect(toolchain_action, SIGNAL("toggled(bool)"), lambda x, toolchain_action=toolchain_action: self.toolchainChanged(
114                 qvariant_converter.getString(toolchain_action.data()),
115                 x
116             ))
117         for version_action in self.version_actions:
118             self.connect(version_action, SIGNAL("toggled(bool)"), lambda x, version_action=version_action: self.versionChanged(
119                 qvariant_converter.getString(version_action.data()),
120                 x
121             ))
122         self.connect(self.apply_button, SIGNAL("clicked()"), self.apply)
123         self.connect(self.cancel_button, SIGNAL("clicked()"), self.reject)
124
125     def toolchainChanged(self, toolchain, activated):
126         if activated:
127             self.setCurrentToolchain(toolchain)
128
129     def versionChanged(self, version, activated):
130         if activated:
131             self.setCurrentVersion(version)
132
133     def apply(self):
134         createBertosProject(self.module_page.project(), edit=True)
135         self.accept()
136
137     def toolchains(self):
138         return self.module_page.toolchains()
139     
140     def currentToolchain(self):
141         return self.module_page.projectInfo("TOOLCHAIN")
142     
143     def setCurrentToolchain(self, toolchain):
144         self.module_page.setProjectInfo("TOOLCHAIN", toolchain)
145
146     def versions(self):
147         return self.module_page.versions()
148
149     def currentVersion(self):
150         return self.module_page.projectInfo("SOURCES_PATH")
151     
152     def setCurrentVersion(self, version):
153         self.module_page.setProjectInfo("SOURCES_PATH", version)
154
155
156
157 def main():
158     if len(sys.argv) > 1:
159         project_file = sys.argv[1]
160     else:
161         print "Invalid usage: use <command> project_file"
162         sys.exit()
163     app = QApplication([])
164     app.project = loadBertosProject(project_file)
165     app.settings = QSettings("Develer", "Bertos Configurator")
166     dialog = BEditingDialog()
167     dialog.show()
168     sys.exit(app.exec_())
169
170 if __name__ == "__main__":
171     main()