source: calamares/trunk/fuentes/src/modules/dummypythonqt/main.py @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 8.6 KB
Line 
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#
4# === This file is part of Calamares - <https://github.com/calamares> ===
5#
6#   Copyright 2016-2017, Teo Mrnjavac <teo@kde.org>
7#   Copyright 2017, Alf Gaida <agaida@siduction.org>
8#
9#   Calamares is free software: you can redistribute it and/or modify
10#   it under the terms of the GNU General Public License as published by
11#   the Free Software Foundation, either version 3 of the License, or
12#   (at your option) any later version.
13#
14#   Calamares is distributed in the hope that it will be useful,
15#   but WITHOUT ANY WARRANTY; without even the implied warranty of
16#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17#   GNU General Public License for more details.
18#
19#   You should have received a copy of the GNU General Public License
20#   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
21
22import platform
23
24from PythonQt.QtGui import *
25import PythonQt.calamares as calamares
26
27# WARNING: the Calamares PythonQt API is considered EXPERIMENTAL as of
28# Calamares 2.5. It comes with no promise or commitment to API stability.
29
30
31# Set up translations.
32# You may skip this if your Calamares module has no user visible strings.
33# DO NOT install _ into the builtin namespace because each module loads
34# its own catalog.
35# DO use the gettext class-based API and manually alias _ as described in:
36# https://docs.python.org/3.5/library/gettext.html#localizing-your-module
37import gettext
38import inspect
39import os
40_filename = inspect.getframeinfo(inspect.currentframe()).filename
41_path = os.path.dirname(os.path.abspath(_filename))
42
43_ = gettext.gettext
44
45# Example Python ViewModule.
46# A Python ViewModule is a Python program which defines a ViewStep class.
47# One UI module ==> one ViewStep.
48# This class must be marked with the @calamares_module decorator. A
49# ViewModule may define other classes, but only one may be decorated with
50# @calamares_module. Such a class must conform to the Calamares ViewStep
51# interface and functions as the entry point of the module.
52# A ViewStep manages one or more "wizard pages" through methods like
53# back/next, and reports its status through isNextEnabled/isBackEnabled/
54# isAtBeginning/isAtEnd. The whole UI, including all the pages, must be
55# exposed as a single QWidget, returned by the widget function.
56#
57# For convenience, both C++ and PythonQt ViewSteps are considered to be
58# implementations of ViewStep.h. Additionally, the Calamares PythonQt API
59# allows Python developers to keep their identifiers more Pythonic on the
60# Python side. Thus, all of the following are considered valid method
61# identifiers in a ViewStep implementation: isNextEnabled, isnextenabled,
62# is_next_enabled.
63
64
65@calamares_module
66class DummyPythonQtViewStep:
67    def __init__(self):
68        # Importing PythonQt.QtGui provides access to most Qt widget classes.
69        self.main_widget = QFrame()
70
71        self.main_widget.setLayout(QVBoxLayout())
72
73        label = QLabel()
74        self.main_widget.layout().addWidget(label)
75
76        accumulator = "\nCalamares+PythonQt running embedded Python " +\
77                      platform.python_version()
78        label.text = accumulator
79
80        btn = QPushButton()
81
82        # Python strings can be used wherever a method wants a QString. Python
83        # gettext translations can be used seamlessly as well.
84        btn.setText(_("Click me!"))
85        self.main_widget.layout().addWidget(btn)
86
87        # The syntax for signal-slot connections is very simple, though
88        # slightly different from the C++ equivalent. There are no SIGNAL and
89        # SLOT macros, and a signal can be connected to any Python method
90        # (without a special "slot" designation).
91        btn.connect("clicked(bool)", self.on_btn_clicked)
92
93    def on_btn_clicked(self):
94        self.main_widget.layout().addWidget(QLabel(_("A new QLabel.")))
95
96    def prettyName(self):
97        return _("Dummy PythonQt ViewStep")
98
99    def isNextEnabled(self):
100        return True  # The "Next" button should be clickable
101
102    def isBackEnabled(self):
103        return True  # The "Back" button should be clickable
104
105    def isAtBeginning(self):
106        # True means the currently shown UI page is the first page of this
107        # module, thus a "Back" button click will not be handled by this
108        # module and will cause a skip to the previous ViewStep instead
109        # (if any). False means that the present ViewStep provides other UI
110        # pages placed logically "before" the current one, thus a "Back" button
111        # click will be handled by this module instead of skipping to another
112        # ViewStep. A module (ViewStep) with only one page will always return
113        # True here.
114        return True
115
116    def isAtEnd(self):
117        # True means the currently shown UI page is the last page of this
118        # module, thus a "Next" button click will not be handled by this
119        # module and will cause a skip to the next ViewStep instead (if any).
120        # False means that the present ViewStep provides other UI pages placed
121        # logically "after" the current one, thus a "Next" button click will
122        # be handled by this module instead of skipping to another ViewStep.
123        # A module (ViewStep) with only one page will always return True here.
124        return True
125
126    def jobs(self):
127        # Returns a list of objects that implement Calamares::Job.
128        return [DummyPQJob("Dummy PythonQt job reporting for duty")]
129
130    def widget(self):
131        # Returns the base QWidget of this module's UI.
132        return self.main_widget
133
134    def retranslate(self, locale_name):
135        # This is where it gets slightly weird. In most desktop applications we
136        # shouldn't need this kind of mechanism, because we could assume that
137        # the operating environment is configured to use a certain language.
138        # Usually the user would change the system-wide language in a settings
139        # UI, restart the application, done.
140        # Alas, Calamares runs on an unconfigured live system, and one of the
141        # core features of Calamares is to allow the user to pick a language.
142        # Unfortunately, strings in the UI do not automatically react to a
143        # runtime language change. To get UI strings in a new language, all
144        # user-visible strings must be retranslated (by calling tr() in C++ or
145        # _() in Python) and reapplied on the relevant widgets.
146        # When the user picks a new UI translation language, Qt raises a QEvent
147        # of type LanguageChange, which propagates through the QObject
148        # hierarchy. By catching and reacting to this event, we can show
149        # user-visible strings in the new language at the right time.
150        # The C++ side of the Calamares PythonQt API catches the LanguageChange
151        # event and calls the present method. It is then up to the module
152        # developer to add here all the needed code to load the module's
153        # translation catalog for the new language (which is separate from the
154        # main Calamares strings catalog) and reapply any user-visible strings.
155        calamares.utils.debug("PythonQt retranslation event "
156                              "for locale name: {}".format(locale_name))
157
158        # First we load the catalog file for the new language...
159        try:
160            global _
161            _t = gettext.translation('dummypythonqt',
162                                     localedir=os.path.join(_path, 'lang'),
163                                     languages=[locale_name])
164            _ = _t.gettext
165        except OSError as e:
166            calamares.utils.debug(e)
167            pass
168
169        # ... and then we can call setText(_("foo")) and similar methods on
170        # the relevant widgets here to reapply the strings.
171
172# An example Job class. Implements Calamares::Job. For method identifiers, the
173# same rules apply as for ViewStep. No decorators are necessary here, because
174# only the ViewStep implementation is the unique entry point, and a module can
175# have any number of jobs.
176
177
178class DummyPQJob:
179    def __init__(self, my_msg):
180        self.my_msg = my_msg
181
182    def pretty_name(self):
183        return _("The Dummy PythonQt Job")
184
185    def pretty_description(self):
186        return _("This is the Dummy PythonQt Job. "
187                 "The dummy job says: {}").format(self.my_msg)
188
189    def pretty_status_message(self):
190        return _("A status message for Dummy PythonQt Job.")
191
192    def exec(self):
193        # As an example, we touch a file in the target root filesystem.
194        rmp = calamares.global_storage['rootMountPoint']
195        os.system("touch {}/calamares_dpqt_was_here".format(rmp))
196        calamares.utils.debug("the dummy job says {}".format(self.my_msg))
197        return {'ok': True}
Note: See TracBrowser for help on using the repository browser.