1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Linguist of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #include "phrasemodel.h"
30 
31 QT_BEGIN_NAMESPACE
32 
removePhrases()33 void PhraseModel::removePhrases()
34 {
35     int r = plist.count();
36     if (r > 0) {
37         beginResetModel();
38         plist.clear();
39         endResetModel();
40     }
41 }
42 
phrase(const QModelIndex & index) const43 Phrase *PhraseModel::phrase(const QModelIndex &index) const
44 {
45     return plist.at(index.row());
46 }
47 
setPhrase(const QModelIndex & indx,Phrase * ph)48 void PhraseModel::setPhrase(const QModelIndex &indx, Phrase *ph)
49 {
50     int r = indx.row();
51 
52     plist[r] = ph;
53 
54     // update item in view
55     const QModelIndex &si = index(r, 0);
56     const QModelIndex &ei = index(r, 2);
57     emit dataChanged(si, ei);
58 }
59 
addPhrase(Phrase * p)60 QModelIndex PhraseModel::addPhrase(Phrase *p)
61 {
62     int r = plist.count();
63 
64     plist.append(p);
65 
66     // update phrases as we add them
67     beginInsertRows(QModelIndex(), r, r);
68     QModelIndex i = index(r, 0);
69     endInsertRows();
70     return i;
71 }
72 
removePhrase(const QModelIndex & index)73 void PhraseModel::removePhrase(const QModelIndex &index)
74 {
75     int r = index.row();
76     beginRemoveRows(QModelIndex(), r, r);
77     plist.removeAt(r);
78     endRemoveRows();
79 }
80 
index(Phrase * const phr) const81 QModelIndex PhraseModel::index(Phrase * const phr) const
82 {
83     int row;
84     if ((row = plist.indexOf(phr)) == -1)
85         return QModelIndex();
86 
87     return index(row, 0);
88 }
89 
rowCount(const QModelIndex &) const90 int PhraseModel::rowCount(const QModelIndex &) const
91 {
92     return plist.count();
93 }
94 
columnCount(const QModelIndex &) const95 int PhraseModel::columnCount(const QModelIndex &) const
96 {
97     return 3;
98 }
99 
headerData(int section,Qt::Orientation orientation,int role) const100 QVariant PhraseModel::headerData(int section, Qt::Orientation orientation, int role) const
101 {
102     if ((role == Qt::DisplayRole) && (orientation == Qt::Horizontal)) {
103         switch(section) {
104         case 0:
105             return tr("Source phrase");
106         case 1:
107             return tr("Translation");
108         case 2:
109             return tr("Definition");
110         }
111     }
112 
113     return QVariant();
114 }
115 
flags(const QModelIndex & index) const116 Qt::ItemFlags PhraseModel::flags(const QModelIndex &index) const
117 {
118     if (!index.isValid())
119         return {};
120     Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
121     // Edit is allowed for source & translation if item is from phrasebook
122     if (plist.at(index.row())->phraseBook()
123         && (index.column() != 2))
124         flags |= Qt::ItemIsEditable;
125     return flags;
126 }
127 
setData(const QModelIndex & index,const QVariant & value,int role)128 bool PhraseModel::setData(const QModelIndex & index, const QVariant & value, int role)
129 {
130     int row = index.row();
131     int column = index.column();
132 
133     if (!index.isValid() || row >= plist.count() || role != Qt::EditRole)
134         return false;
135 
136     Phrase *phrase = plist.at(row);
137 
138     switch (column) {
139     case 0:
140         phrase->setSource(value.toString());
141         break;
142     case 1:
143         phrase->setTarget(value.toString());
144         break;
145     case 2:
146         phrase->setDefinition(value.toString());
147         break;
148     default:
149         return false;
150     }
151 
152     emit dataChanged(index, index);
153     return true;
154 }
155 
data(const QModelIndex & index,int role) const156 QVariant PhraseModel::data(const QModelIndex &index, int role) const
157 {
158     int row = index.row();
159     int column = index.column();
160 
161     if (row >= plist.count() || !index.isValid())
162         return QVariant();
163 
164     Phrase *phrase = plist.at(row);
165 
166     if (role == Qt::DisplayRole || (role == Qt::ToolTipRole && column != 2)) {
167         switch (column) {
168         case 0: // source phrase
169             return phrase->source().simplified();
170         case 1: // translation
171             return phrase->target().simplified();
172         case 2: // definition
173             return phrase->definition();
174         }
175     }
176     else if (role == Qt::EditRole && column != 2) {
177         switch (column) {
178         case 0: // source phrase
179             return phrase->source();
180         case 1: // translation
181             return phrase->target();
182         }
183     }
184 
185     return QVariant();
186 }
187 
188 QT_END_NAMESPACE
189