~michael-sheldon/ubuntu-keyboard/fix-oxide-dismiss-test

« back to all changes in this revision

Viewing changes to src/viewer/dashboard.cpp

  • Committer: Thomas Moenicke
  • Date: 2013-07-19 12:05:07 UTC
  • Revision ID: thomas.moenicke@canonical.com-20130719120507-lzw5oq50xm567x0j
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of Maliit Plugins
 
3
 *
 
4
 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
 
5
 *
 
6
 * Contact: Mohammad Anwari <Mohammad.Anwari@nokia.com>
 
7
 *
 
8
 * Redistribution and use in source and binary forms, with or without modification,
 
9
 * are permitted provided that the following conditions are met:
 
10
 *
 
11
 * Redistributions of source code must retain the above copyright notice, this list
 
12
 * of conditions and the following disclaimer.
 
13
 * Redistributions in binary form must reproduce the above copyright notice, this list
 
14
 * of conditions and the following disclaimer in the documentation and/or other materials
 
15
 * provided with the distribution.
 
16
 * Neither the name of Nokia Corporation nor the names of its contributors may be
 
17
 * used to endorse or promote products derived from this software without specific
 
18
 * prior written permission.
 
19
 *
 
20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 
21
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 
22
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 
23
 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
25
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
26
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 
27
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
28
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 *
 
30
 */
 
31
 
 
32
#include "models/key.h"
 
33
#include "view/renderer.h"
 
34
#include "view/abstracttexteditor.h"
 
35
#include "logic/languagefeatures.h"
 
36
#include "logic/layout.h"
 
37
#include "logic/keyareaconverter.h"
 
38
#include "logic/wordengine.h"
 
39
#include "dashboard.h"
 
40
 
 
41
#include <QInputMethodEvent>
 
42
#include <QTextCharFormat>
 
43
 
 
44
namespace MaliitKeyboard {
 
45
 
 
46
class DashboardEditor
 
47
    : public AbstractTextEditor
 
48
{
 
49
private:
 
50
    QTextEdit *const m_target;
 
51
 
 
52
public:
 
53
    explicit DashboardEditor(QTextEdit *target,
 
54
                             const EditorOptions &options,
 
55
                             QObject *parent = 0);
 
56
 
 
57
private:
 
58
    //! \reimp
 
59
    virtual void sendPreeditString(const QString &preedit,
 
60
                                   Model::Text::PreeditFace face,
 
61
                                   const Replacement &replacement);
 
62
    virtual void sendCommitString(const QString &commit);
 
63
    virtual void sendKeyEvent(const QKeyEvent &ev);
 
64
    virtual void invokeAction(const QString &command, const QKeySequence &sequence);
 
65
    //! \reimp_end
 
66
};
 
67
 
 
68
DashboardEditor::DashboardEditor(QTextEdit *target,
 
69
                                 const EditorOptions &options,
 
70
                                 QObject *parent)
 
71
    : AbstractTextEditor(options, new Model::Text, new Logic::WordEngine, new Logic::LanguageFeatures, parent)
 
72
    , m_target(target)
 
73
{}
 
74
 
 
75
void DashboardEditor::sendPreeditString(const QString &preedit,
 
76
                                        Model::Text::PreeditFace face,
 
77
                                        const Replacement &replacement)
 
78
{
 
79
    QList<QInputMethodEvent::Attribute> attribute_list;
 
80
    QTextCharFormat char_format;
 
81
    const int start(0);
 
82
    const int length(preedit.length());
 
83
 
 
84
    switch (face) {
 
85
    case Model::Text::PreeditNoCandidates:
 
86
        char_format.setFontUnderline(true);
 
87
        char_format.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline);
 
88
        char_format.setUnderlineColor(Qt::red);
 
89
        break;
 
90
 
 
91
    case Model::Text::PreeditActive:
 
92
        char_format.setForeground(QBrush(QColor(153, 50, 204)));
 
93
        char_format.setFontWeight(QFont::Bold);
 
94
        break;
 
95
 
 
96
    case Model::Text::PreeditDefault:
 
97
    default:
 
98
        char_format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
 
99
        char_format.setUnderlineColor(QColor(0, 0, 0));
 
100
        break;
 
101
    }
 
102
 
 
103
    attribute_list.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat,
 
104
                                                       start,
 
105
                                                       length,
 
106
                                                       char_format));
 
107
    if (replacement.cursor_position >= 0) {
 
108
        attribute_list << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, replacement.cursor_position, 1, QVariant());
 
109
    }
 
110
 
 
111
    QInputMethodEvent *ev = new QInputMethodEvent(preedit, attribute_list);
 
112
 
 
113
    if (replacement.start || replacement.length) {
 
114
        ev->setCommitString("", replacement.start, replacement.length);
 
115
    }
 
116
    qApp->postEvent(m_target, ev);
 
117
}
 
118
 
 
119
void DashboardEditor::sendCommitString(const QString &commit)
 
120
{
 
121
    QInputMethodEvent *ev = new QInputMethodEvent;
 
122
    ev->setCommitString(commit);
 
123
    qApp->postEvent(m_target, ev);
 
124
}
 
125
 
 
126
void DashboardEditor::sendKeyEvent(const QKeyEvent &ev)
 
127
{
 
128
    QKeyEvent *new_ev = new QKeyEvent(ev.type(), ev.key(), ev.modifiers(), ev.text(), ev.isAutoRepeat(), ev.count());
 
129
    qApp->postEvent(m_target, new_ev);
 
130
}
 
131
 
 
132
void DashboardEditor::invokeAction(const QString &command, const QKeySequence &sequence)
 
133
{
 
134
    Q_UNUSED(command);
 
135
    Q_UNUSED(sequence);
 
136
    // FIXME implement
 
137
}
 
138
 
 
139
class DashboardPrivate
 
140
{
 
141
public:
 
142
    Renderer *renderer;
 
143
    QTextEdit *text_entry;
 
144
    DashboardEditor *editor;
 
145
    QGraphicsProxyWidget *proxy_widget;
 
146
    QVBoxLayout *vbox;
 
147
    QSpacerItem *top;
 
148
    QSpacerItem *bottom;
 
149
    QWidget *buttons;
 
150
    Logic::Layout::Orientation orientation;
 
151
 
 
152
    explicit DashboardPrivate(Dashboard *q)
 
153
        : renderer(0)
 
154
        , text_entry(new QTextEdit)
 
155
        , editor(new DashboardEditor(text_entry, EditorOptions(), q))
 
156
        , proxy_widget(0)
 
157
        , vbox(new QVBoxLayout)
 
158
        , top(new QSpacerItem(0, 0))
 
159
        , bottom(new QSpacerItem(0, 0))
 
160
        , buttons(new QWidget)
 
161
        , orientation(Logic::Layout::Landscape)
 
162
    {}
 
163
};
 
164
 
 
165
Dashboard::Dashboard(QWidget *parent)
 
166
    : QMainWindow(parent)
 
167
    , d_ptr(new DashboardPrivate(this))
 
168
{
 
169
    setWindowTitle("Maliit Keyboard Viewer");
 
170
    resize(854, 480);
 
171
 
 
172
    QGraphicsView *v = new QGraphicsView;
 
173
    v->setScene(new QGraphicsScene(v));
 
174
    v->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 
175
    v->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 
176
    setCentralWidget(v);
 
177
    v->show();
 
178
    QWidget *w = 0;
 
179
    d_ptr->proxy_widget = v->scene()->addWidget(w = new QWidget);
 
180
    d_ptr->proxy_widget->setTransformOriginPoint(d_ptr->proxy_widget->geometry().center());
 
181
    w->resize(size());
 
182
 
 
183
    QVBoxLayout *vbox = d_ptr->vbox;
 
184
    w->setLayout(vbox);
 
185
 
 
186
    QSpacerItem *top_spacer = d_ptr->top;
 
187
    vbox->addItem(top_spacer);
 
188
 
 
189
    QWidget *buttons = d_ptr->buttons;
 
190
    buttons->show();
 
191
    vbox->addWidget(buttons);
 
192
    QHBoxLayout *hbox = new QHBoxLayout;
 
193
    buttons->setLayout(hbox);
 
194
 
 
195
    QPushButton *show = new QPushButton("Show virtual keyboard");
 
196
    connect(show, SIGNAL(clicked()),
 
197
            this, SLOT(onShow()));
 
198
    hbox->addWidget(show);
 
199
    show->show();
 
200
 
 
201
    QPushButton *orientation_changed = new QPushButton("Change orientation");
 
202
    connect(orientation_changed, SIGNAL(clicked()),
 
203
            this, SLOT(onOrientationChangeClicked()));
 
204
    hbox->addWidget(orientation_changed);
 
205
    orientation_changed->show();
 
206
 
 
207
    QPushButton *close = new QPushButton("Close application");
 
208
    connect(close, SIGNAL(clicked()),
 
209
            qApp,  SLOT(quit()));
 
210
    hbox->addWidget(close);
 
211
    close->show();
 
212
 
 
213
    QTextEdit *te = d_ptr->text_entry;
 
214
    vbox->addWidget(te);
 
215
    te->show();
 
216
    te->setFocus();
 
217
 
 
218
    QSpacerItem *bottom_spacer = d_ptr->bottom;
 
219
    vbox->addItem(bottom_spacer);
 
220
 
 
221
    onShow();
 
222
}
 
223
 
 
224
Dashboard::~Dashboard()
 
225
{}
 
226
 
 
227
void Dashboard::setRenderer(Renderer *renderer)
 
228
{
 
229
    Q_D(Dashboard);
 
230
    d->renderer = renderer;
 
231
}
 
232
 
 
233
AbstractTextEditor * Dashboard::editor() const
 
234
{
 
235
    Q_D(const Dashboard);
 
236
    return d->editor;
 
237
}
 
238
 
 
239
void Dashboard::onShow()
 
240
{
 
241
    Q_D(Dashboard);
 
242
 
 
243
    if (d->renderer) {
 
244
        d->renderer->show();
 
245
    }
 
246
 
 
247
    d->top->changeSize(0, d->orientation == Logic::Layout::Landscape ? 50 : 80);
 
248
    d->bottom->changeSize(0, d->orientation == Logic::Layout::Landscape ? 250 : 350);
 
249
    d->vbox->invalidate();
 
250
    d->buttons->hide();
 
251
}
 
252
 
 
253
void Dashboard::onHide()
 
254
{
 
255
    Q_D(Dashboard);
 
256
    d->top->changeSize(0, 0);
 
257
    d->bottom->changeSize(0, 0);
 
258
    d->vbox->invalidate();
 
259
    d->buttons->show();
 
260
}
 
261
 
 
262
void Dashboard::onOrientationChangeClicked()
 
263
{
 
264
    Q_D(Dashboard);
 
265
 
 
266
    d->orientation = (d->orientation == Logic::Layout::Landscape
 
267
                      ? Logic::Layout::Portrait : Logic::Layout::Landscape);
 
268
    static_cast<QGraphicsView *>(centralWidget())->rotate(d->orientation == Logic::Layout::Landscape ? 90 : 270);
 
269
    const QSize &s(centralWidget()->size());
 
270
    d->proxy_widget->resize(d->orientation == Logic::Layout::Landscape ? s : QSize(s.height(), s.width()));
 
271
    onShow();
 
272
    Q_EMIT orientationChanged(d->orientation);
 
273
}
 
274
 
 
275
} // namespace MaliitKeyboard