aboutsummaryrefslogtreecommitdiff
path: root/src/views/mcompleterview.cpp
blob: 546094e768d053ce77d84453065207305bf5a4f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include "mcompleter.h"
#include "mcompleterview.h"
#include "mcompleterview_p.h"

#include "mtextedit.h"
#include "moverlay.h"
#include "mlabel.h"
#include "mbutton.h"
#include "mpopuplist.h"
#include "mapplication.h"
#include "mapplicationwindow.h"
#include "mscenemanager.h"

#include <QGraphicsSceneMouseEvent>
#include <QGraphicsLinearLayout>
#include <QTextDocumentFragment>

namespace
{
    //! default height for completer
    const int DefaultCompleterHeight = 60;
    //!default object names
    const QString CompleterCandidatesLabelObjectName("MCompleterCandidatesLabel");
    const QString CompleterTotalButtonObjectName("MCompleterTotalButton");
    //! default maximum hits is 10
    const int DefaultMaximumHits = 10;
}

MCompleterViewPrivate::MCompleterViewPrivate(MCompleter *controller, MCompleterView *q)
    : controller(controller),
      q_ptr(q),
      completionLabel(0),
      completionsButton(0),
      layout(0),
      popup(0),
      overLaypreferredSize(QSizeF(0, 0))
{
    completionLabel = new MLabel(controller);
    completionLabel->setObjectName(CompleterCandidatesLabelObjectName);
    completionLabel->setTextElide(true);
    completionLabel->setWordWrap(false);
    completionLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    completionsButton = new MButton(controller);
    completionsButton->setObjectName(CompleterTotalButtonObjectName);

    layout = new QGraphicsLinearLayout(Qt::Horizontal, controller);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);
    layout->addItem(completionLabel);
    layout->setAlignment(completionLabel, Qt::AlignCenter);
    layout->addItem(completionsButton);
    layout->setAlignment(completionsButton, Qt::AlignCenter);

    controller->setLayout(layout);

    //click focus, and eat focusout for the textedit widget
    controller->setFocusPolicy(Qt::ClickFocus);
    controller->setManagedManually(true);
}

MCompleterViewPrivate::~MCompleterViewPrivate()
{
    delete completionLabel;
    completionLabel = 0;
    delete completionsButton;
    completionsButton = 0;

    delete popup;
    popup  = 0;
}


void MCompleterViewPrivate::init()
{
    connect(completionsButton, SIGNAL(clicked()), this, SLOT(showPopup()));
    connect(controller, SIGNAL(shown()), this, SLOT(createContents()));
    connect(controller, SIGNAL(hidden()), this, SLOT(clear()));
    if (controller->widget()) {
        connect(controller->widget()->sceneManager(),
                SIGNAL(orientationChangeFinished(const M::Orientation &)),
                this, SLOT(organizeContents()));
    }
}

void MCompleterViewPrivate::organizeContents()
{
    Q_Q(MCompleterView);

    if (!controller->widget() || !controller->isActive())
        return;
    QSize screenSize = MApplication::activeWindow()->visibleSceneSize();
    int textWidgetWidth = 0;

    //mcompleter has by default the same width than the according Text-field that it is attached to.
    //default position is aligning left bottom with text widget
    const QRect textWidgetRect = controller->widget()->mapToScene(
                                     controller->widget()->boundingRect()).boundingRect().toRect();
    textWidgetWidth = textWidgetRect.width();

    int width = 0;
    int height = DefaultCompleterHeight;

    if (q->style()->height() > 0)
        height = q->style()->height();

    int textWidth = 0;
    //using the font information from the label to caculate the width (of the plain label text)
    //TODO: use the MLabel::preferredSize() instead of calculate by ourselves.
    textWidth = QFontMetrics(completionLabel->font()).width(
                    QTextDocumentFragment::fromHtml(completionLabel->text()).toPlainText());

    const int labelMargin = q->style()->labelMargin();
    const int buttonWidth = q->style()->buttonWidth();
    const int displayBorder = q->style()->displayBorder();

    width = textWidth + 2 * labelMargin;
    bool buttonVisible = (!completionsButton->text().isEmpty()) && completionsButton->isVisible();
    if (buttonVisible) {
        const int buttonMargin = q->style()->buttonMargin();
        width += buttonWidth + buttonMargin;
    }
    // Input suggestions that do not fit into the available space force the completer to grow.
    // First the completer grows to the right until it reaches the displayBorder.
    // If then continues growing to the left until it reaches the left displayBorder.
    // This means that the maximum size of the completer is the width of the display minus 2*displayBorder
    if (width < textWidgetWidth)
        width = textWidgetWidth;
    if (width > (screenSize.width() - 2 * displayBorder)) {
        width = screenSize.width() - 2 * displayBorder;
    }
    overLaypreferredSize = QSizeF(width, height);

    QPoint completerPos = locatePosition(textWidgetRect);
    if ((completerPos.x() + width) > (screenSize.width() - displayBorder)) {
        completerPos.setX(screenSize.width() - displayBorder - width);
    }
    //add y position offset
    const int  yPositionOffset = q->style()->yPositionOffset();
    if (yPositionOffset > 0) {
        completerPos.setY(completerPos.y() - yPositionOffset);
    }

    //adjust position, to avoid the completer to be beyond the screen edge
    //completer's position is fixed, aligning left bottom with text widget
    //judge completion should stay at text widget below or upper
    if (height > (screenSize.height() - completerPos.y())) {
        completerPos.setY(completerPos.y() - controller->widget()->boundingRect().height()
                          - height + yPositionOffset);
    }
    if (completerPos != controller->pos())
        controller->setPos(completerPos);
    q->updateGeometry();
}

QPoint MCompleterViewPrivate::locatePosition(const QRect &r) const
{
    QSize screenSize = MApplication::activeWindow()->visibleSceneSize();
    QPoint p;
    switch (MApplication::activeWindow()->orientationAngle()) {
    case M:: Angle90:
        p = r.topLeft();
        p = QPoint(p.y(), (screenSize.height() - p.x()));
        break;
    case M:: Angle180:
        p = r.topRight();
        p = QPoint((screenSize.width() - p.x()), (screenSize.height() - p.y()));
        break;
    case M:: Angle270:
        p = r.bottomRight();
        p = QPoint((screenSize.width() - p.y()), p.x());
        break;
    case M:: Angle0:
    default:
        p = r.bottomLeft();
        break;
    }
    return p;
}

void MCompleterViewPrivate::createContents()
{
    Q_Q(MCompleterView);

    if (controller->widget() && q->model()->matchedModel()) {
        //add controller to scence when first time to show, it is neccessary for later setFocusProxy
        if (!controller->widget()->scene()->items().contains(controller))
            controller->widget()->scene()->addItem(controller);

        QString text;
        if (q->model()->matchedModel()->rowCount() > q->model()->matchedIndex()) {
            QVariant var = q->model()->matchedModel()->data(
                               q->model()->matchedModel()->index(q->model()->matchedIndex(), 0));
            if (var.isValid())
                text += var.toString();
        }
        if (text.isEmpty())
            return;

        //highlight the completionPrefix
        QString prefix = q->model()->completionPrefix();
        text = text.replace('<', "&lt;");
        text = text.replace('>', "&gt;");
        prefix = prefix.replace('<', "&lt;");
        prefix = prefix.replace('>', "&gt;");
        QRegExp exp(QString("^%1").arg(prefix), Qt::CaseInsensitive);
        int index = exp.indexIn(text);
        if (index == -1) {
            exp.setPattern(QString("\\W%1").arg(prefix));
            index = exp.indexIn(text);
            if (index != -1)
                ++index;
        }

        // only highlight if there is a match in text.
        if (index != -1) {
            QString highlightColor = q->style()->highlightColor().name();
            QString replacedString = text.mid(index, prefix.length());
            text = text.replace(index, replacedString.length(),
                                QString("<font color=%1>" + replacedString + "</font>").arg(highlightColor));
        }
        completionLabel->setText(text);

        //set button's visibility and label
        int total = q->model()->matchedModel()->rowCount();
        if (total > 1) {
            // Workaround for NB#177781: MButton has different alignments for rich text and normal text in its label.
            // Both completionLabel and completionsButton use rich text label to get same alignment
            if (total <= DefaultMaximumHits)
                completionsButton->setText(QString("<b></b>%1").arg(total));
            else
                completionsButton->setText(QString("<b></b>&gt;%1").arg(DefaultMaximumHits));
            completionsButton->setFocusProxy(controller->widget());
            completionsButton->setVisible(true);
            layout->addItem(completionsButton);
            layout->setAlignment(completionsButton, Qt::AlignCenter);
        } else {
            completionsButton->setText("");
            completionsButton->setFocusProxy(0);
            completionsButton->setVisible(false);
            layout->removeItem(completionsButton);
        }

        completionLabel->setFocusProxy(controller->widget());
        controller->setFocusProxy(controller->widget());

        //set the focus back to textwidget
        //even we already set focus proxy for controller, this step is still necessary,
        //otherwise the textwidget can not get focus.
        controller->scene()->setFocusItem(controller->widget());
        organizeContents();
    }
}

void MCompleterViewPrivate::clear()
{
    overLaypreferredSize = QSize(0, 0);
    //clear focus proxy
    completionLabel->setFocusProxy(0);
    completionsButton->setFocusProxy(0);
    controller->setFocusProxy(0);
}


void MCompleterViewPrivate::showPopup()
{
    Q_Q(MCompleterView);
    if (!q->model()->matchedModel() || q->model()->matchedModel()->rowCount() <= 0)
        return;

    if (!popup) {
        popup = new MPopupList();
        popup->setItemModel(q->model()->matchedModel());
        connect(popup, SIGNAL(appearing()), this, SLOT(handlePopupAppearing()));
        connect(popup, SIGNAL(disappeared()), this, SLOT(handlePopupDisappeared()));
    }

    if (popup->currentIndex().row() < 0)
        popup->setCurrentIndex(popup->itemModel()->index(0, 0));

    //if the label of the button is ">10", should query all before showing popup
    if (completionsButton->text() == QString(">%1").arg(DefaultMaximumHits))
        controller->queryAll();
    controller->sceneManager()->appearSceneWindow(popup);
}

void MCompleterViewPrivate::handlePopupAppearing()
{
    Q_Q(MCompleterView);
    q->model()->setPopupActive(true);
    //hide completion widget before showing popup
    controller->hideCompleter();
    controller->widget()->clearFocus();
    connect(controller->widget(), SIGNAL(gainedFocus(Qt::FocusReason)),
            this, SLOT(refocusPopup()), Qt::UniqueConnection);
}

void MCompleterViewPrivate::handlePopupDisappeared()
{
    Q_Q(MCompleterView);
    disconnect(controller->widget(), SIGNAL(gainedFocus(Qt::FocusReason)),
               this, SLOT(refocusPopup()));
    if (popup->result() == MDialog::Accepted) {
        //only confirm when accept
        controller->scene()->setFocusItem(controller->widget());
        q->model()->setMatchedIndex(popup->currentIndex().row());
        controller->confirm();
    } else {
        controller->scene()->setFocusItem(controller->widget());
    }
    q->model()->setPopupActive(false);
}

void MCompleterViewPrivate::refocusPopup()
{
    Q_Q(MCompleterView);
    // if text widget gains focus again when popup list is still
    // visibile, should transfer the focus to popup list.
    if (popup && q->model()->popupActive()) {
        controller->widget()->clearFocus();
        controller->scene()->setFocusItem(popup);
        controller->setActive(true);
    }
}

MCompleterView::MCompleterView(MCompleter *controller)
    : MSceneWindowView(controller),
      d_ptr(new MCompleterViewPrivate(controller, this))
{
    Q_D(MCompleterView);
    d->init();
}


MCompleterView::~MCompleterView()
{
    Q_D(MCompleterView);
    delete d;
}

QSizeF MCompleterView::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
{
    Q_UNUSED(which);
    Q_UNUSED(constraint);
    Q_D(const MCompleterView);

    // return the actual height of mcompleter to avoid twice relocation.
    if (!d->overLaypreferredSize.isEmpty())
        return d->overLaypreferredSize;
    else {
        int height = (style()->height() > 0) ? style()->height() : DefaultCompleterHeight;
        return QSize(MApplication::activeWindow()->visibleSceneSize().width(), height);
    }
}

void MCompleterView::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    style()->pressFeedback().play();
    style().setModePressed();
    applyStyle();
    update();
    event->accept();
}

void MCompleterView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    Q_D(MCompleterView);
    style()->releaseFeedback().play();
    style().setModeDefault();
    applyStyle();
    update();
    if (d->controller && d->controller->isVisible())
        d->controller->confirm();
    event->accept();
}

M_REGISTER_VIEW_NEW(MCompleterView, MCompleter)