Merge from trunk.
[bertos.git] / wizard / bertos.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 os
37 import sys
38 from distutils.dep_util import newer
39
40 from PyQt4.QtCore import *
41 from PyQt4.QtGui import *
42
43 import exception_handler
44
45 import BProject
46
47 import BStartPage
48 import BWizard
49
50 from BIntroPage import BIntroPage
51 from BFolderPage import BFolderPage
52 from BOpenPage import BOpenPage
53 from BVersionPage import BVersionPage
54 from BCpuPage import BCpuPage
55 from BToolchainPage import BToolchainPage
56 from BModulePage import BModulePage
57 from BOutputPage import BOutputPage
58 from BCreationPage import BCreationPage
59 from BFinalPage import BFinalPage
60
61 from BEditingDialog import BEditingDialog, BVersionDialog, BToolchainDialog
62
63 import bertos_utils
64 import const
65
66 from LoadException import VersionException, ToolchainException
67
68 def newProject():
69     page_list = [BIntroPage, BFolderPage, BVersionPage, BCpuPage, BToolchainPage, BModulePage, BOutputPage, BCreationPage, BFinalPage]
70     wizard = BWizard.BWizard(page_list)
71     wizard.show()
72     wizard.exec_()
73     project = QApplication.instance().project
74     to_be_opened = project.info("TO_BE_OPENED")
75     command_lines = project.info("COMMAND_LINES")
76     relevant_files = project.info("RELEVANT_FILES")
77     if to_be_opened:
78         for ide in to_be_opened:
79             command_line = command_lines[ide]
80             relevant_file = relevant_files[ide]
81             QProcess.startDetached(command_line, [relevant_file,])
82     sys.exit()
83
84 def editProject(project_file):
85     info_dict = {}
86     while(True):
87         try:
88             QApplication.instance().project = bertos_utils.loadBertosProject(project_file, info_dict)
89         except VersionException:
90             QMessageBox.critical(
91                 None,
92                 QObject().tr("BeRTOS version not found!"),
93                 QObject().tr("The selected BeRTOS version is not found, please select an existing one...")
94             )
95             dialog = BVersionDialog()
96             if dialog.exec_():
97                 version = dialog.version_page.currentVersion()
98                 info_dict["SOURCES_PATH"] = version
99             continue
100         except ToolchainException, exc:
101             QMessageBox.critical(
102                 None,
103                 QObject().tr("Toolchain not found!"),
104                 QObject().tr("The selected toolchain is not found, please select an existing one...")
105             )
106             QApplication.instance().project = exc.partial_project
107             dialog = BToolchainDialog()
108             if dialog.exec_():
109                 toolchain = dialog.toolchain_page.currentToolchain()
110                 info_dict["TOOLCHAIN"] = toolchain
111             continue
112         break
113     dialog = BEditingDialog()
114     dialog.exec_()
115
116 def showStartPage():
117     QApplication.instance().dialog = BStartPage.BStartPage()
118     QApplication.instance().connect(QApplication.instance().dialog, SIGNAL("newProject"), newProject)
119     QApplication.instance().connect(QApplication.instance().dialog, SIGNAL("editProject"), editProject)
120     QApplication.instance().dialog.show()
121
122 def main():
123     app = QApplication(sys.argv)
124     app.settings = QSettings("Develer", "Bertos Configurator")
125     app.project = BProject.BProject()
126     # Development utility lines, to be removed for production
127     datadir = const.DATA_DIR
128     qrc, rcc = os.path.join(datadir, 'bertos.qrc'), os.path.join(datadir, 'bertos.rcc')
129     if not (hasattr(sys, "frozen") and sys.frozen) and newer(qrc, rcc):
130         os.system("rcc -binary %s -o %s" %(qrc, rcc))
131     QResource.registerResource(rcc)
132     if len(sys.argv) == 3 and sys.argv[1] == "--edit":
133         editProject(sys.argv[2])
134     else:
135         newProject()
136
137
138 if __name__ == '__main__':
139     main()
140