source: calamares/trunk/fuentes/src/libcalamaresui/viewpages/PythonQtViewStep.cpp @ 7538

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

sync with github

File size: 6.6 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2016, Teo Mrnjavac <teo@kde.org>
4 *   Copyright 2018, Adriaan de Groot <groot@kde.org>
5 *
6 *   Calamares 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 3 of the License, or
9 *   (at your option) any later version.
10 *
11 *   Calamares 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 Calamares. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "PythonQtViewStep.h"
21#include "utils/Logger.h"
22#include "utils/CalamaresUtilsGui.h"
23#include "utils/PythonQtUtils.h"
24#include "utils/Retranslator.h"
25#include "viewpages/PythonQtJob.h"
26
27#include <gui/PythonQtScriptingConsole.h>
28
29#include <QBoxLayout>
30#include <QWidget>
31
32
33namespace Calamares
34{
35
36PythonQtViewStep::PythonQtViewStep( PythonQtObjectPtr cxt,
37                                    QObject* parent )
38    : ViewStep( parent )
39    , m_widget( new QWidget() )
40    , m_cxt( cxt )
41{
42    PythonQt* pq = PythonQt::self();
43    Q_ASSERT( pq );
44
45    // The @calamares_module decorator should have filled _calamares_module_typename
46    // for us.
47    QString className = m_cxt.getVariable( "_calamares_module_typename" ).toString();
48
49    // Instantiate an object of the class marked with @calamares_module and
50    // store it as _calamares_module.
51    pq->evalScript( m_cxt, QString( "_calamares_module = %1()" )
52                                            .arg( className ) );
53    m_obj = pq->lookupObject( m_cxt, "_calamares_module" );
54
55    Q_ASSERT( !m_obj.isNull() );    // no entry point, no party
56
57    // Prepare the base widget for the module's pages
58    m_widget->setLayout( new QVBoxLayout );
59    CalamaresUtils::unmarginLayout( m_widget->layout() );
60    m_cxt.addObject( "_calamares_module_basewidget", m_widget );
61
62    CALAMARES_RETRANSLATE_WIDGET( m_widget,
63        CalamaresUtils::lookupAndCall( m_obj,
64                                       { "retranslate" },
65                                       { CalamaresUtils::translatorLocaleName() } );
66    )
67}
68
69
70QString
71PythonQtViewStep::prettyName() const
72{
73    return CalamaresUtils::lookupAndCall( m_obj,
74                                          { "prettyName",
75                                            "prettyname",
76                                            "pretty_name" } ).toString();
77}
78
79
80QWidget*
81PythonQtViewStep::widget()
82{
83    if ( m_widget->layout()->count() > 1 )
84        cWarning() << "PythonQtViewStep wrapper widget has more than 1 child. "
85                    "This should never happen.";
86
87    bool nothingChanged = m_cxt.evalScript(
88        "_calamares_module.widget() in _calamares_module_basewidget.children()" ).toBool();
89    if ( nothingChanged )
90        return m_widget;
91
92    // Else, we either don't have a child widget, or we have a child widget that
93    // was previously set and doesn't apply any more since the Python module
94    // set a new one.
95
96    // First we clear the layout, which should only ever have 1 item.
97    // We only remove from the layout and not delete because Python is in charge
98    // of memory management for these widgets.
99    while ( m_widget->layout()->itemAt( 0 ) )
100        m_widget->layout()->takeAt( 0 );
101
102    m_cxt.evalScript(
103        "_calamares_module_basewidget.layout().addWidget(_calamares_module.widget())" );
104
105    return m_widget;
106}
107
108
109void
110PythonQtViewStep::next()
111{
112    CalamaresUtils::lookupAndCall( m_obj, { "next" } );
113}
114
115
116void
117PythonQtViewStep::back()
118{
119    CalamaresUtils::lookupAndCall( m_obj, { "back" } );
120}
121
122
123bool
124PythonQtViewStep::isNextEnabled() const
125{
126    return CalamaresUtils::lookupAndCall( m_obj,
127                                          { "isNextEnabled",
128                                            "isnextenabled",
129                                            "is_next_enabled" } ).toBool();
130}
131
132
133bool
134PythonQtViewStep::isBackEnabled() const
135{
136    return CalamaresUtils::lookupAndCall( m_obj,
137                                          { "isBackEnabled",
138                                            "isbackenabled",
139                                            "is_back_enabled" } ).toBool();
140}
141
142
143bool
144PythonQtViewStep::isAtBeginning() const
145{
146    return CalamaresUtils::lookupAndCall( m_obj,
147                                          { "isAtBeginning",
148                                            "isatbeginning",
149                                            "is_at_beginning" } ).toBool();
150}
151
152
153bool
154PythonQtViewStep::isAtEnd() const
155{
156    return CalamaresUtils::lookupAndCall( m_obj,
157                                          { "isAtEnd",
158                                            "isatend",
159                                            "is_at_end" } ).toBool();
160}
161
162void
163PythonQtViewStep::onActivate() 
164{
165    CalamaresUtils::lookupAndCall( m_obj,
166                                          { "onActivate",
167                                            "onactivate",
168                                            "on_activate" });
169}
170
171void
172PythonQtViewStep::onLeave()
173{
174    CalamaresUtils::lookupAndCall( m_obj,
175                                          { "onLeave",
176                                            "onleave",
177                                            "on_leave" });
178}
179
180
181JobList
182PythonQtViewStep::jobs() const
183{
184    JobList jobs;
185
186    PythonQtObjectPtr jobsCallable = PythonQt::self()->lookupCallable( m_obj, "jobs" );
187    if ( jobsCallable.isNull() )
188        return jobs;
189
190    PythonQtObjectPtr response = PythonQt::self()->callAndReturnPyObject( jobsCallable );
191    if ( response.isNull() )
192        return jobs;
193
194    PythonQtObjectPtr listPopCallable = PythonQt::self()->lookupCallable( response, "pop" );
195    if ( listPopCallable.isNull() )
196        return jobs;
197
198    forever
199    {
200        PythonQtObjectPtr aJob = PythonQt::self()->callAndReturnPyObject( listPopCallable, { 0 } );
201        if ( aJob.isNull() )
202            break;
203
204        jobs.append( Calamares::job_ptr( new PythonQtJob( m_cxt, aJob ) ) );
205    }
206
207    return jobs;
208}
209
210
211void
212PythonQtViewStep::setConfigurationMap( const QVariantMap& configurationMap )
213{
214    m_obj.addVariable( "configuration", configurationMap );
215}
216
217
218QWidget*
219PythonQtViewStep::createScriptingConsole()
220{
221    PythonQtScriptingConsole* console = new PythonQtScriptingConsole( nullptr, m_cxt );
222    console->setProperty( "classname",
223                          m_cxt.getVariable( "_calamares_module_typename" ).toString() );
224    return console;
225}
226
227}
Note: See TracBrowser for help on using the repository browser.