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
31QT_BEGIN_NAMESPACE
32
33void PhraseModel::removePhrases()
34{
35 int r = plist.count();
36 if (r > 0) {
37 beginResetModel();
38 plist.clear();
39 endResetModel();
40 }
41}
42
43Phrase *PhraseModel::phrase(const QModelIndex &index) const
44{
45 return plist.at(i: index.row());
46}
47
48void 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(row: r, column: 0);
56 const QModelIndex &ei = index(row: r, column: 2);
57 emit dataChanged(topLeft: si, bottomRight: ei);
58}
59
60QModelIndex PhraseModel::addPhrase(Phrase *p)
61{
62 int r = plist.count();
63
64 plist.append(t: p);
65
66 // update phrases as we add them
67 beginInsertRows(parent: QModelIndex(), first: r, last: r);
68 QModelIndex i = index(row: r, column: 0);
69 endInsertRows();
70 return i;
71}
72
73void PhraseModel::removePhrase(const QModelIndex &index)
74{
75 int r = index.row();
76 beginRemoveRows(parent: QModelIndex(), first: r, last: r);
77 plist.removeAt(i: r);
78 endRemoveRows();
79}
80
81QModelIndex PhraseModel::index(Phrase * const phr) const
82{
83 int row;
84 if ((row = plist.indexOf(t: phr)) == -1)
85 return QModelIndex();
86
87 return index(row, column: 0);
88}
89
90int PhraseModel::rowCount(const QModelIndex &) const
91{
92 return plist.count();
93}
94
95int PhraseModel::columnCount(const QModelIndex &) const
96{
97 return 3;
98}
99
100QVariant 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(s: "Source phrase");
106 case 1:
107 return tr(s: "Translation");
108 case 2:
109 return tr(s: "Definition");
110 }
111 }
112
113 return QVariant();
114}
115
116Qt::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(i: index.row())->phraseBook()
123 && (index.column() != 2))
124 flags |= Qt::ItemIsEditable;
125 return flags;
126}
127
128bool 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(i: 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(topLeft: index, bottomRight: index);
153 return true;
154}
155
156QVariant 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(i: 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
188QT_END_NAMESPACE
189

source code of qttools/src/linguist/linguist/phrasemodel.cpp