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

« back to all changes in this revision

Viewing changes to src/lib/logic/abstractwordengine.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) 2012 Openismus GmbH
 
5
 *
 
6
 * Contact: maliit-discuss@lists.maliit.org
 
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 "abstractwordengine.h"
 
33
 
 
34
namespace MaliitKeyboard {
 
35
namespace Logic {
 
36
 
 
37
//! \class AbstractWordEngine
 
38
//! \brief Provides word candidates based on text model.
 
39
//!
 
40
//! Derived classes need to provide an implementation for
 
41
//! fetchCandidates() and, optionally, addToUserDictionary().
 
42
//! \sa Model::Text, computeCandidates().
 
43
 
 
44
//! \fn void AbstractWordEngine::enabledChanged(bool enabled)
 
45
//! \brief Emitted when word engine toggles word candidate updates on/off.
 
46
//! \param enabled Whether word engine is enabled.
 
47
 
 
48
//! \fn void AbstractWordEngine::candidatesChanged(const WordCandidateList &candidates)
 
49
//! \brief Emitted when new candidates have been computed.
 
50
//! \param candidates The list of updated candidates.
 
51
 
 
52
//! \fn WordCandidateList AbstractWordEngine::fetchCandidates(Model::Text *text)
 
53
//! \brief Returns a list of candidates.
 
54
//! \param text The text model.
 
55
//!
 
56
//! Needs to be implemented by derived classes. Will not be called if engine
 
57
//! is disabled or text model has no preedit.
 
58
 
 
59
//! \property AbstractWordEngine::enabled
 
60
//! \brief Whether the engine provides updates for word candidates.
 
61
 
 
62
class AbstractWordEnginePrivate
 
63
{
 
64
public:
 
65
    bool enabled;
 
66
 
 
67
    explicit AbstractWordEnginePrivate();
 
68
};
 
69
 
 
70
AbstractWordEnginePrivate::AbstractWordEnginePrivate()
 
71
    : enabled(false)
 
72
{}
 
73
 
 
74
 
 
75
//! \brief Constructor.
 
76
//! \param parent The owner of this instance. Can be 0, in case QObject
 
77
//!               ownership is not required.
 
78
AbstractWordEngine::AbstractWordEngine(QObject *parent)
 
79
    : QObject(parent)
 
80
    , d_ptr(new AbstractWordEnginePrivate)
 
81
{}
 
82
 
 
83
//! \brief Destructor.
 
84
//!
 
85
//! Needs to be implemented in derived classes.
 
86
AbstractWordEngine::~AbstractWordEngine()
 
87
{}
 
88
 
 
89
 
 
90
//! \brief Returns whether the word engine is enabled.
 
91
//! \sa AbstractWordEngine::enabled
 
92
bool AbstractWordEngine::isEnabled() const
 
93
{
 
94
    Q_D(const AbstractWordEngine);
 
95
    return d->enabled;
 
96
}
 
97
 
 
98
 
 
99
//! \brief Set whether the engine should be enabled.
 
100
//! \param enabled Setting to true will be ignored if there's no word
 
101
//!                prediction or error correction backend available.
 
102
//! \sa AbstractWordEngine::enabled
 
103
void AbstractWordEngine::setEnabled(bool enabled)
 
104
{
 
105
    Q_D(AbstractWordEngine);
 
106
 
 
107
    if (d->enabled != enabled) {
 
108
        clearCandidates();
 
109
        d->enabled = enabled;
 
110
        Q_EMIT enabledChanged(d->enabled);
 
111
    }
 
112
}
 
113
 
 
114
 
 
115
//! \brief Clears the current candidates.
 
116
//!
 
117
//! Only has an effect when word engine is enabled, in which case
 
118
//! candidatesCanged() is emitted.
 
119
void AbstractWordEngine::clearCandidates()
 
120
{
 
121
    if (isEnabled()) {
 
122
        Q_EMIT candidatesChanged(WordCandidateList());
 
123
    }
 
124
}
 
125
 
 
126
 
 
127
//! \brief Computes new candidates, based on text model.
 
128
//! \param text The text model.
 
129
//!
 
130
//! Can trigger emission of candidatesChanged().
 
131
void AbstractWordEngine::computeCandidates(Model::Text *text)
 
132
{
 
133
    // FIXME: add possiblity to turn off the error correction for
 
134
    // entries that does not need it (like password entries).  Also,
 
135
    // with that we probably will want to turn off preedit styling at
 
136
    // all.
 
137
 
 
138
    if (not isEnabled()
 
139
        || not text
 
140
        || text->preedit().isEmpty()
 
141
        || not text->preedit().at(text->preedit().length() - 1).isLetterOrNumber()) {
 
142
        // FIXME: We should here set some special preedit face in text
 
143
        // model (say Disabled or None) which would be interpreted by
 
144
        // editor to send no formatting informations along with
 
145
        // preedit string. When this is done, preedit-string test
 
146
        // needs to be adapted.
 
147
        return;
 
148
    }
 
149
 
 
150
    Q_EMIT candidatesChanged(fetchCandidates(text));
 
151
}
 
152
 
 
153
//! \brief Adds a word to user dictionary.
 
154
//! \param word A word.
 
155
//!
 
156
//! Needs to be implemented in derived classes. This does nothing.
 
157
void AbstractWordEngine::addToUserDictionary(const QString &word)
 
158
{
 
159
    Q_UNUSED(word);
 
160
}
 
161
 
 
162
}} // namespace MaliitKeyboard, Logic