1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
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 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
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 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at qt-info@nokia.com.
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "phrasemodel.h"
|
---|
43 |
|
---|
44 | QT_BEGIN_NAMESPACE
|
---|
45 |
|
---|
46 | void PhraseModel::removePhrases()
|
---|
47 | {
|
---|
48 | int r = plist.count();
|
---|
49 | if (r > 0) {
|
---|
50 | plist.clear();
|
---|
51 | reset();
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | Phrase *PhraseModel::phrase(const QModelIndex &index) const
|
---|
56 | {
|
---|
57 | return plist.at(index.row());
|
---|
58 | }
|
---|
59 |
|
---|
60 | void PhraseModel::setPhrase(const QModelIndex &indx, Phrase *ph)
|
---|
61 | {
|
---|
62 | int r = indx.row();
|
---|
63 |
|
---|
64 | plist[r] = ph;
|
---|
65 |
|
---|
66 | // update item in view
|
---|
67 | const QModelIndex &si = index(r, 0);
|
---|
68 | const QModelIndex &ei = index(r, 2);
|
---|
69 | emit dataChanged(si, ei);
|
---|
70 | }
|
---|
71 |
|
---|
72 | QModelIndex PhraseModel::addPhrase(Phrase *p)
|
---|
73 | {
|
---|
74 | int r = plist.count();
|
---|
75 |
|
---|
76 | plist.append(p);
|
---|
77 |
|
---|
78 | // update phrases as we add them
|
---|
79 | beginInsertRows(QModelIndex(), r, r);
|
---|
80 | QModelIndex i = index(r, 0);
|
---|
81 | endInsertRows();
|
---|
82 | return i;
|
---|
83 | }
|
---|
84 |
|
---|
85 | void PhraseModel::removePhrase(const QModelIndex &index)
|
---|
86 | {
|
---|
87 | int r = index.row();
|
---|
88 | beginRemoveRows(QModelIndex(), r, r);
|
---|
89 | plist.removeAt(r);
|
---|
90 | endRemoveRows();
|
---|
91 | }
|
---|
92 |
|
---|
93 | QModelIndex PhraseModel::index(Phrase * const phr) const
|
---|
94 | {
|
---|
95 | int row;
|
---|
96 | if ((row = plist.indexOf(phr)) == -1)
|
---|
97 | return QModelIndex();
|
---|
98 |
|
---|
99 | return index(row, 0);
|
---|
100 | }
|
---|
101 |
|
---|
102 | int PhraseModel::rowCount(const QModelIndex &) const
|
---|
103 | {
|
---|
104 | return plist.count();
|
---|
105 | }
|
---|
106 |
|
---|
107 | int PhraseModel::columnCount(const QModelIndex &) const
|
---|
108 | {
|
---|
109 | return 3;
|
---|
110 | }
|
---|
111 |
|
---|
112 | QVariant PhraseModel::headerData(int section, Qt::Orientation orientation, int role) const
|
---|
113 | {
|
---|
114 | if ((role == Qt::DisplayRole) && (orientation == Qt::Horizontal)) {
|
---|
115 | switch(section) {
|
---|
116 | case 0:
|
---|
117 | return tr("Source phrase");
|
---|
118 | case 1:
|
---|
119 | return tr("Translation");
|
---|
120 | case 2:
|
---|
121 | return tr("Definition");
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | return QVariant();
|
---|
126 | }
|
---|
127 |
|
---|
128 | Qt::ItemFlags PhraseModel::flags(const QModelIndex &index) const
|
---|
129 | {
|
---|
130 | if (!index.isValid())
|
---|
131 | return 0;
|
---|
132 | Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
|
---|
133 | // Edit is allowed for source & translation if item is from phrasebook
|
---|
134 | if (plist.at(index.row())->phraseBook()
|
---|
135 | && (index.column() != 2))
|
---|
136 | flags |= Qt::ItemIsEditable;
|
---|
137 | return flags;
|
---|
138 | }
|
---|
139 |
|
---|
140 | bool PhraseModel::setData(const QModelIndex & index, const QVariant & value, int role)
|
---|
141 | {
|
---|
142 | int row = index.row();
|
---|
143 | int column = index.column();
|
---|
144 |
|
---|
145 | if (!index.isValid() || row >= plist.count() || role != Qt::EditRole)
|
---|
146 | return false;
|
---|
147 |
|
---|
148 | Phrase *phrase = plist.at(row);
|
---|
149 |
|
---|
150 | switch (column) {
|
---|
151 | case 0:
|
---|
152 | phrase->setSource(value.toString());
|
---|
153 | break;
|
---|
154 | case 1:
|
---|
155 | phrase->setTarget(value.toString());
|
---|
156 | break;
|
---|
157 | case 2:
|
---|
158 | phrase->setDefinition(value.toString());
|
---|
159 | break;
|
---|
160 | default:
|
---|
161 | return false;
|
---|
162 | }
|
---|
163 |
|
---|
164 | emit dataChanged(index, index);
|
---|
165 | return true;
|
---|
166 | }
|
---|
167 |
|
---|
168 | QVariant PhraseModel::data(const QModelIndex &index, int role) const
|
---|
169 | {
|
---|
170 | int row = index.row();
|
---|
171 | int column = index.column();
|
---|
172 |
|
---|
173 | if (row >= plist.count() || !index.isValid())
|
---|
174 | return QVariant();
|
---|
175 |
|
---|
176 | Phrase *phrase = plist.at(row);
|
---|
177 |
|
---|
178 | if (role == Qt::DisplayRole || (role == Qt::ToolTipRole && column != 2)) {
|
---|
179 | switch (column) {
|
---|
180 | case 0: // source phrase
|
---|
181 | return phrase->source().simplified();
|
---|
182 | case 1: // translation
|
---|
183 | return phrase->target().simplified();
|
---|
184 | case 2: // definition
|
---|
185 | return phrase->definition();
|
---|
186 | }
|
---|
187 | }
|
---|
188 | else if (role == Qt::EditRole && column != 2) {
|
---|
189 | switch (column) {
|
---|
190 | case 0: // source phrase
|
---|
191 | return phrase->source();
|
---|
192 | case 1: // translation
|
---|
193 | return phrase->target();
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | return QVariant();
|
---|
198 | }
|
---|
199 |
|
---|
200 | QT_END_NAMESPACE
|
---|