author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 29 Apr 2010 15:15:16 +0300 | |
branch | RCL_3 |
changeset 17 | 4b6ee5efea19 |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the Qt Linguist of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "globals.h" |
|
43 |
#include "mainwindow.h" |
|
44 |
#include "messagemodel.h" |
|
45 |
#include "phrase.h" |
|
46 |
#include "phraseview.h" |
|
47 |
#include "phrasemodel.h" |
|
48 |
#include "simtexth.h" |
|
49 |
||
50 |
#include <QHeaderView> |
|
51 |
#include <QKeyEvent> |
|
52 |
#include <QSettings> |
|
53 |
#include <QTreeView> |
|
54 |
#include <QWidget> |
|
55 |
#include <QDebug> |
|
56 |
||
57 |
||
58 |
QT_BEGIN_NAMESPACE |
|
59 |
||
60 |
// Maximum number of guesses to display |
|
61 |
static const int MaxCandidates = 5; |
|
62 |
||
63 |
static QString phraseViewHeaderKey() |
|
64 |
{ |
|
65 |
return settingPath("PhraseViewHeader"); |
|
66 |
} |
|
67 |
||
68 |
PhraseView::PhraseView(MultiDataModel *model, QList<QHash<QString, QList<Phrase *> > > *phraseDict, QWidget *parent) |
|
69 |
: QTreeView(parent), |
|
70 |
m_dataModel(model), |
|
71 |
m_phraseDict(phraseDict), |
|
72 |
m_modelIndex(-1), |
|
73 |
m_doGuesses(true) |
|
74 |
{ |
|
75 |
setObjectName(QLatin1String("phrase list view")); |
|
76 |
||
77 |
m_phraseModel = new PhraseModel(this); |
|
78 |
||
79 |
setModel(m_phraseModel); |
|
80 |
setAlternatingRowColors(true); |
|
81 |
setSelectionBehavior(QAbstractItemView::SelectRows); |
|
82 |
setSelectionMode(QAbstractItemView::SingleSelection); |
|
83 |
setRootIsDecorated(false); |
|
84 |
setItemsExpandable(false); |
|
85 |
||
86 |
for (int i = 0; i < 10; i++) |
|
87 |
(void) new GuessShortcut(i, this, SLOT(guessShortcut(int))); |
|
88 |
||
89 |
header()->setResizeMode(QHeaderView::Interactive); |
|
90 |
header()->setClickable(true); |
|
91 |
header()->restoreState(QSettings().value(phraseViewHeaderKey()).toByteArray()); |
|
92 |
||
93 |
connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(selectPhrase(QModelIndex))); |
|
94 |
} |
|
95 |
||
96 |
PhraseView::~PhraseView() |
|
97 |
{ |
|
98 |
QSettings().setValue(phraseViewHeaderKey(), header()->saveState()); |
|
99 |
deleteGuesses(); |
|
100 |
} |
|
101 |
||
102 |
void PhraseView::toggleGuessing() |
|
103 |
{ |
|
104 |
m_doGuesses = !m_doGuesses; |
|
105 |
update(); |
|
106 |
} |
|
107 |
||
108 |
void PhraseView::update() |
|
109 |
{ |
|
110 |
setSourceText(m_modelIndex, m_sourceText); |
|
111 |
} |
|
112 |
||
113 |
||
114 |
void PhraseView::contextMenuEvent(QContextMenuEvent *event) |
|
115 |
{ |
|
116 |
QModelIndex index = indexAt(event->pos()); |
|
117 |
if (!index.isValid()) |
|
118 |
return; |
|
119 |
||
120 |
QMenu *contextMenu = new QMenu(this); |
|
121 |
||
122 |
QAction *insertAction = new QAction(tr("Insert"), contextMenu); |
|
123 |
connect(insertAction, SIGNAL(triggered()), this, SLOT(selectPhrase())); |
|
124 |
||
125 |
QAction *editAction = new QAction(tr("Edit"), contextMenu); |
|
126 |
connect(editAction, SIGNAL(triggered()), this, SLOT(editPhrase())); |
|
127 |
editAction->setEnabled(model()->flags(index) & Qt::ItemIsEditable); |
|
128 |
||
129 |
contextMenu->addAction(insertAction); |
|
130 |
contextMenu->addAction(editAction); |
|
131 |
||
132 |
contextMenu->exec(event->globalPos()); |
|
133 |
event->accept(); |
|
134 |
} |
|
135 |
||
136 |
void PhraseView::mouseDoubleClickEvent(QMouseEvent *event) |
|
137 |
{ |
|
138 |
QModelIndex index = indexAt(event->pos()); |
|
139 |
if (!index.isValid()) |
|
140 |
return; |
|
141 |
||
142 |
emit phraseSelected(m_modelIndex, m_phraseModel->phrase(index)->target()); |
|
143 |
event->accept(); |
|
144 |
} |
|
145 |
||
146 |
void PhraseView::guessShortcut(int key) |
|
147 |
{ |
|
148 |
foreach (const Phrase *phrase, m_phraseModel->phraseList()) |
|
149 |
if (phrase->shortcut() == key) { |
|
150 |
emit phraseSelected(m_modelIndex, phrase->target()); |
|
151 |
return; |
|
152 |
} |
|
153 |
} |
|
154 |
||
155 |
void PhraseView::selectPhrase(const QModelIndex &index) |
|
156 |
{ |
|
157 |
emit phraseSelected(m_modelIndex, m_phraseModel->phrase(index)->target()); |
|
158 |
} |
|
159 |
||
160 |
void PhraseView::selectPhrase() |
|
161 |
{ |
|
162 |
emit phraseSelected(m_modelIndex, m_phraseModel->phrase(currentIndex())->target()); |
|
163 |
} |
|
164 |
||
165 |
void PhraseView::editPhrase() |
|
166 |
{ |
|
167 |
edit(currentIndex()); |
|
168 |
} |
|
169 |
||
170 |
static CandidateList similarTextHeuristicCandidates(MultiDataModel *model, int mi, |
|
171 |
const char *text, int maxCandidates) |
|
172 |
{ |
|
173 |
QList<int> scores; |
|
174 |
CandidateList candidates; |
|
175 |
||
176 |
StringSimilarityMatcher stringmatcher(QString::fromLatin1(text)); |
|
177 |
||
178 |
for (MultiDataModelIterator it(model, mi); it.isValid(); ++it) { |
|
179 |
MessageItem *m = it.current(); |
|
180 |
if (!m) |
|
181 |
continue; |
|
182 |
||
183 |
TranslatorMessage mtm = m->message(); |
|
184 |
if (mtm.type() == TranslatorMessage::Unfinished |
|
185 |
|| mtm.translation().isEmpty()) |
|
186 |
continue; |
|
187 |
||
188 |
QString s = m->text(); |
|
189 |
||
190 |
int score = stringmatcher.getSimilarityScore(s); |
|
191 |
||
192 |
if (candidates.count() == maxCandidates && score > scores[maxCandidates - 1]) |
|
193 |
candidates.removeLast(); |
|
194 |
if (candidates.count() < maxCandidates && score >= textSimilarityThreshold ) { |
|
195 |
Candidate cand(s, mtm.translation()); |
|
196 |
||
197 |
int i; |
|
198 |
for (i = 0; i < candidates.size(); ++i) { |
|
199 |
if (score >= scores.at(i)) { |
|
200 |
if (score == scores.at(i)) { |
|
201 |
if (candidates.at(i) == cand) |
|
202 |
goto continue_outer_loop; |
|
203 |
} else { |
|
204 |
break; |
|
205 |
} |
|
206 |
} |
|
207 |
} |
|
208 |
scores.insert(i, score); |
|
209 |
candidates.insert(i, cand); |
|
210 |
} |
|
211 |
continue_outer_loop: |
|
212 |
; |
|
213 |
} |
|
214 |
return candidates; |
|
215 |
} |
|
216 |
||
217 |
||
218 |
void PhraseView::setSourceText(int model, const QString &sourceText) |
|
219 |
{ |
|
220 |
m_modelIndex = model; |
|
221 |
m_sourceText = sourceText; |
|
222 |
m_phraseModel->removePhrases(); |
|
223 |
deleteGuesses(); |
|
224 |
||
225 |
if (model < 0) |
|
226 |
return; |
|
227 |
||
228 |
foreach (Phrase *p, getPhrases(model, sourceText)) |
|
229 |
m_phraseModel->addPhrase(p); |
|
230 |
||
231 |
if (!sourceText.isEmpty() && m_doGuesses) { |
|
232 |
CandidateList cl = similarTextHeuristicCandidates(m_dataModel, model, |
|
233 |
sourceText.toLatin1(), MaxCandidates); |
|
234 |
int n = 0; |
|
235 |
foreach (const Candidate &candidate, cl) { |
|
236 |
QString def; |
|
237 |
if (n < 9) |
|
238 |
def = tr("Guess (%1)").arg(QString(QKeySequence(Qt::CTRL | (Qt::Key_0 + (n + 1))))); |
|
239 |
else |
|
240 |
def = tr("Guess"); |
|
241 |
Phrase *guess = new Phrase(candidate.source, candidate.target, def, n); |
|
242 |
m_guesses.append(guess); |
|
243 |
m_phraseModel->addPhrase(guess); |
|
244 |
++n; |
|
245 |
} |
|
246 |
} |
|
247 |
} |
|
248 |
||
249 |
QList<Phrase *> PhraseView::getPhrases(int model, const QString &source) |
|
250 |
{ |
|
251 |
QList<Phrase *> phrases; |
|
252 |
QString f = MainWindow::friendlyString(source); |
|
253 |
QStringList lookupWords = f.split(QLatin1Char(' ')); |
|
254 |
||
255 |
foreach (const QString &s, lookupWords) { |
|
256 |
if (m_phraseDict->at(model).contains(s)) { |
|
257 |
foreach (Phrase *p, m_phraseDict->at(model).value(s)) { |
|
258 |
if (f.contains(MainWindow::friendlyString(p->source()))) |
|
259 |
phrases.append(p); |
|
260 |
} |
|
261 |
} |
|
262 |
} |
|
263 |
return phrases; |
|
264 |
} |
|
265 |
||
266 |
void PhraseView::deleteGuesses() |
|
267 |
{ |
|
268 |
qDeleteAll(m_guesses); |
|
269 |
m_guesses.clear(); |
|
270 |
} |
|
271 |
||
272 |
QT_END_NAMESPACE |