~timo-jyrinki/ubuntu/trusty/maliit-framework/fix_qt52

« back to all changes in this revision

Viewing changes to examples/apps/settings/mainwindow.cpp

  • Committer: Package Import Robot
  • Author(s): Ricardo Salveti de Araujo, Sergio Schvezov, Ricardo Salveti de Araujo
  • Date: 2013-07-23 19:47:04 UTC
  • mfrom: (1.1.2) (1.2.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130723194704-1lsy1kmlda069cea
Tags: 0.99.0+git20130615+97e8335-0ubuntu1
[ Sergio Schvezov ]
* New build from HEAD 97e8335.
* Packaging import from lp:phablet-extras/maliit-framework.

[ Ricardo Salveti de Araujo ]
* debian/control: adding vcs and fixing dependencies
* General package cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This file is part of Maliit framework
2
 
 *
3
 
 * Copyright (C) 2012 Mattia Barbon <mattia@develer.com>
4
 
 * Copyright (C) 2012 Openismus GmbH
5
 
 *
6
 
 * Contact: maliit-discuss@lists.maliit.org
7
 
 *
8
 
 * This library is free software; you can redistribute it and/or
9
 
 * modify it under the terms of the GNU Lesser General Public
10
 
 * License as published by the Free Software Foundation; either
11
 
 * version 2.1 of the licence, or (at your option) any later version.
12
 
 *
13
 
 * This library is distributed in the hope that it will be useful,
14
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 
 * Lesser General Public License for more details.
17
 
 *
18
 
 * You should have received a copy of the GNU Lesser General Public
19
 
 * License along with this library; if not, write to the
20
 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21
 
 * Boston, MA 02111-1307, USA.
22
 
 */
23
 
 
24
 
#include "mainwindow.h"
25
 
#include "pluginsettings.h"
26
 
#include "stringentryedit.h"
27
 
#include "selectentrycombobox.h"
28
 
#include "boolentrycheckbox.h"
29
 
 
30
 
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
31
 
#include <QtWidgets/QVBoxLayout>
32
 
#include <QtWidgets/QLabel>
33
 
#include <QtGui/QInputMethod>
34
 
#endif
35
 
 
36
 
namespace
37
 
{
38
 
 
39
 
void
40
 
removeAllTabs (QTabWidget& tabs)
41
 
{
42
 
    for (int iter(tabs.count() - 1); iter >= 0; ++iter) {
43
 
        tabs.removeTab(iter);
44
 
    }
45
 
}
46
 
 
47
 
}
48
 
 
49
 
MainWindow::MainWindow() :
50
 
    maliit_settings(Maliit::SettingsManager::create(this)),
51
 
    language_entry(),
52
 
    enabled_entry(),
53
 
    language_selector(0),
54
 
    enable_all(0),
55
 
    im_testing_entry(new QTextEdit),
56
 
    tabs()
57
 
{
58
 
    QVBoxLayout *l = new QVBoxLayout(this);
59
 
    l->addWidget(new QLabel("Text entry for testing"));
60
 
 
61
 
    im_testing_entry->setFocusPolicy(Qt::StrongFocus);
62
 
    im_testing_entry->installEventFilter(this);
63
 
    l->addWidget(im_testing_entry);
64
 
 
65
 
    // Steals focus from im_testing_entry and therefore dismisses input
66
 
    // method. Other widgets should set im_testing_entry as focus proxy.
67
 
    QPushButton *dismiss_im = new QPushButton("Dismiss input method");
68
 
    l->addWidget(dismiss_im);
69
 
    l->addWidget(&tabs);
70
 
 
71
 
    connect(maliit_settings, SIGNAL(pluginSettingsReceived(QList<QSharedPointer<Maliit::PluginSettings> >)),
72
 
            this, SLOT(pluginSettingsReceived(QList<QSharedPointer<Maliit::PluginSettings> >)));
73
 
    connect(maliit_settings, SIGNAL(connected()),
74
 
            this, SLOT(connected()));
75
 
}
76
 
 
77
 
bool MainWindow::eventFilter(QObject *watched,
78
 
                             QEvent *event)
79
 
{
80
 
    // Let the input method show up on focus-in, not on second click:
81
 
    if (watched == im_testing_entry
82
 
        && event->type() == QFocusEvent::FocusIn) {
83
 
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
84
 
        qApp->inputMethod()->show();
85
 
#else
86
 
        if (QInputContext *ic = qApp->inputContext()) {
87
 
            QEvent im_request(QEvent::RequestSoftwareInputPanel);
88
 
            ic->filterEvent(&im_request);
89
 
        }
90
 
#endif
91
 
    }
92
 
 
93
 
    return false;
94
 
}
95
 
 
96
 
void MainWindow::connected()
97
 
{
98
 
    qDebug() << "SettingsWidget::connected()";
99
 
 
100
 
    maliit_settings->loadPluginSettings();
101
 
}
102
 
 
103
 
void MainWindow::pluginSettingsReceived(const QList<QSharedPointer<Maliit::PluginSettings> > &settings)
104
 
{
105
 
    qDebug() << "SettingsWidget::pluginSettingsReceived()";
106
 
 
107
 
    const int current_index(tabs.count() > 0 ? tabs.currentIndex() : 0);
108
 
 
109
 
    if (language_selector) {
110
 
        disconnect(language_selector, SIGNAL(currentIndexChanged(int)),
111
 
                   this, SLOT(setLanguage(int)));
112
 
        language_selector = 0;
113
 
    }
114
 
    if (enable_all) {
115
 
        disconnect(enable_all, SIGNAL(clicked()),
116
 
                   this, SLOT(enableAllLayouts()));
117
 
        enable_all = 0;
118
 
    }
119
 
    removeAllTabs(tabs);
120
 
 
121
 
    Q_FOREACH (const QSharedPointer<Maliit::PluginSettings> &setting, settings) {
122
 
        const QString plugin_name(setting->pluginName());
123
 
        QWidget *page(new QWidget);
124
 
        QGridLayout *layout(new QGridLayout(page));
125
 
 
126
 
        int row = 0;
127
 
 
128
 
        if (plugin_name == "server") {
129
 
            language_selector = new QComboBox;
130
 
            language_selector->setFocusProxy(im_testing_entry);
131
 
 
132
 
            enable_all = new QPushButton("Enable all keyboard layouts");
133
 
            enable_all->setFocusProxy(im_testing_entry);
134
 
 
135
 
            Q_FOREACH (const QSharedPointer<Maliit::SettingsEntry> &entry, setting->configurationEntries()) {
136
 
                if (not entry) {
137
 
                    continue;
138
 
                }
139
 
 
140
 
                if (entry->key() == "/maliit/onscreen/enabled") {
141
 
                    enabled_entry = entry;
142
 
                } else if (entry->key() == "/maliit/onscreen/active") {
143
 
                    QStringList values = entry->attributes()[Maliit::SettingEntryAttributes::valueDomain].toStringList();
144
 
                    QStringList descriptions = entry->attributes()[Maliit::SettingEntryAttributes::valueDomainDescriptions].toStringList();
145
 
 
146
 
                    language_entry = entry;
147
 
                    language_selector->addItem("Select language");
148
 
                    for (int i = 0; i < values.count(); ++i) {
149
 
                        language_selector->addItem(descriptions[i], values[i]);
150
 
                    }
151
 
 
152
 
                    connect(language_entry.data(), SIGNAL(valueChanged()),
153
 
                            this, SLOT(languageChanged()));
154
 
                }
155
 
            }
156
 
 
157
 
            connect(language_selector, SIGNAL(currentIndexChanged(int)),
158
 
                    this, SLOT(setLanguage(int)));
159
 
            connect(enable_all, SIGNAL(clicked()),
160
 
                    this, SLOT(enableAllLayouts()));
161
 
 
162
 
            layout->setColumnStretch(1, 1);
163
 
            layout->addWidget(new QLabel("Layout"), row, 0);
164
 
            layout->addWidget(language_selector, row, 1);
165
 
            ++row;
166
 
            layout->addWidget(enable_all, row, 1);
167
 
            ++row;
168
 
 
169
 
            language_selector->setCurrentIndex(language_selector->findData(language_entry->value()));
170
 
            tabs.insertTab(0, page, plugin_name);
171
 
        } else {
172
 
            typedef QPair<QWidget*, QWidget*> WPair;
173
 
            typedef QList<WPair> WPairList;
174
 
 
175
 
            WPairList wlist;
176
 
            wlist.append(WPair(new QLabel("Plugin desc:"), new QLabel(setting->pluginDescription())));
177
 
 
178
 
            Q_FOREACH (const QSharedPointer<Maliit::SettingsEntry> &entry, setting->configurationEntries()) {
179
 
                if (not entry) {
180
 
                    continue;
181
 
                }
182
 
 
183
 
                wlist.append(WPair(new QLabel("Key:"), new QLabel(entry->key())));
184
 
                wlist.append(WPair(new QLabel("Entry desc:"), new QLabel(entry->description())));
185
 
 
186
 
                QWidget *second_widget(0);
187
 
                const QVariant value(entry->value());
188
 
 
189
 
                switch (entry->type()) {
190
 
                case Maliit::StringType: {
191
 
                    const QStringList values = entry->attributes()[Maliit::SettingEntryAttributes::valueDomain].toStringList();
192
 
 
193
 
                    if (values.isEmpty()) {
194
 
                        second_widget = new StringEntryEdit(entry);
195
 
                    } else {
196
 
                        second_widget = new SelectEntryComboBox(entry);
197
 
                    }
198
 
                } break;
199
 
                case Maliit::IntType:
200
 
                    second_widget = new QLabel(QString::number(value.toInt()));
201
 
                    break;
202
 
                case Maliit::BoolType:
203
 
                    second_widget = new BoolEntryCheckBox(entry);
204
 
                    break;
205
 
                case Maliit::StringListType:
206
 
                    second_widget = new QLabel(value.toStringList().join(", "));
207
 
                    break;
208
 
                case Maliit::IntListType: {
209
 
                    const QVariantList values(value.toList());
210
 
                    QStringList strings;
211
 
 
212
 
                    Q_FOREACH (const QVariant &v, values) {
213
 
                        strings << QString::number(v.toInt());
214
 
                    }
215
 
 
216
 
                    second_widget = new QLabel(strings.join(", "));
217
 
                } break;
218
 
 
219
 
                }
220
 
 
221
 
                wlist.append(WPair(new QLabel("Value:"), second_widget));
222
 
                second_widget->setFocusProxy(im_testing_entry);
223
 
 
224
 
                Q_FOREACH(const WPair& pair, wlist) {
225
 
                    layout->addWidget(pair.first, row, 0);
226
 
                    layout->addWidget(pair.second, row, 1);
227
 
                    ++row;
228
 
                }
229
 
            }
230
 
            tabs.addTab(page, plugin_name);
231
 
        }
232
 
 
233
 
        layout->setRowStretch(row, 10);
234
 
    }
235
 
 
236
 
    const int tabs_count(tabs.count());
237
 
 
238
 
    if (tabs_count > 0) {
239
 
        if (tabs_count > current_index) {
240
 
            tabs.setCurrentIndex(current_index);
241
 
        } else {
242
 
            tabs.setCurrentIndex(0);
243
 
        }
244
 
    }
245
 
}
246
 
 
247
 
void MainWindow::setLanguage(int index)
248
 
{
249
 
    if (index == 0 or not language_selector) {
250
 
        return;
251
 
    }
252
 
 
253
 
    qDebug() << "Setting layout" << language_selector->itemData(index);
254
 
 
255
 
    if (language_entry && enabled_entry) {
256
 
        const QString layout = language_selector->itemData(index).toString();
257
 
 
258
 
        // Make sure the new layout is enabled
259
 
        QStringList list = enabled_entry->value().toStringList();
260
 
        if (!list.contains(layout)) {
261
 
            qDebug() << "Enabling layout first";
262
 
            list.append(layout);
263
 
            enabled_entry->set(list);
264
 
        }
265
 
 
266
 
        // Now activate the new layout
267
 
        language_entry->set(layout);
268
 
    } else {
269
 
        qCritical() << "Language entry is NULL";
270
 
    }
271
 
}
272
 
 
273
 
void MainWindow::enableAllLayouts()
274
 
{
275
 
    if (enabled_entry) {
276
 
        enabled_entry->set(enabled_entry->attributes()[Maliit::SettingEntryAttributes::valueDomain]);
277
 
    } else {
278
 
        qCritical() << "Enabled entry is NULL";
279
 
    }
280
 
}
281
 
 
282
 
void MainWindow::languageChanged()
283
 
{
284
 
    if (not language_selector) {
285
 
        return;
286
 
    }
287
 
 
288
 
    if (language_entry) {
289
 
        qDebug() << "New language is" << language_entry->value();
290
 
 
291
 
        language_selector->setCurrentIndex(language_selector->findData(language_entry->value()));
292
 
    } else {
293
 
        qDebug() << "No language is selected";
294
 
    }
295
 
}