1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
3 | |
4 | #include "phrasemodel.h" |
5 | |
6 | QT_BEGIN_NAMESPACE |
7 | |
8 | void PhraseModel::removePhrases() |
9 | { |
10 | int r = plist.size(); |
11 | if (r > 0) { |
12 | beginResetModel(); |
13 | plist.clear(); |
14 | endResetModel(); |
15 | } |
16 | } |
17 | |
18 | Phrase *PhraseModel::phrase(const QModelIndex &index) const |
19 | { |
20 | return plist.at(i: index.row()); |
21 | } |
22 | |
23 | void PhraseModel::setPhrase(const QModelIndex &indx, Phrase *ph) |
24 | { |
25 | int r = indx.row(); |
26 | |
27 | plist[r] = ph; |
28 | |
29 | // update item in view |
30 | const QModelIndex &si = index(row: r, column: 0); |
31 | const QModelIndex &ei = index(row: r, column: 2); |
32 | emit dataChanged(topLeft: si, bottomRight: ei); |
33 | } |
34 | |
35 | QModelIndex PhraseModel::addPhrase(Phrase *p) |
36 | { |
37 | int r = plist.size(); |
38 | |
39 | plist.append(t: p); |
40 | |
41 | // update phrases as we add them |
42 | beginInsertRows(parent: QModelIndex(), first: r, last: r); |
43 | QModelIndex i = index(row: r, column: 0); |
44 | endInsertRows(); |
45 | return i; |
46 | } |
47 | |
48 | void PhraseModel::removePhrase(const QModelIndex &index) |
49 | { |
50 | int r = index.row(); |
51 | beginRemoveRows(parent: QModelIndex(), first: r, last: r); |
52 | plist.removeAt(i: r); |
53 | endRemoveRows(); |
54 | } |
55 | |
56 | QModelIndex PhraseModel::index(Phrase * const phr) const |
57 | { |
58 | int row; |
59 | if ((row = plist.indexOf(t: phr)) == -1) |
60 | return QModelIndex(); |
61 | |
62 | return index(row, column: 0); |
63 | } |
64 | |
65 | int PhraseModel::rowCount(const QModelIndex &) const |
66 | { |
67 | return plist.size(); |
68 | } |
69 | |
70 | int PhraseModel::columnCount(const QModelIndex &) const |
71 | { |
72 | return 3; |
73 | } |
74 | |
75 | QVariant PhraseModel::(int section, Qt::Orientation orientation, int role) const |
76 | { |
77 | if ((role == Qt::DisplayRole) && (orientation == Qt::Horizontal)) { |
78 | switch(section) { |
79 | case 0: |
80 | return tr(s: "Source phrase" ); |
81 | case 1: |
82 | return tr(s: "Translation" ); |
83 | case 2: |
84 | return tr(s: "Definition" ); |
85 | } |
86 | } |
87 | |
88 | return QVariant(); |
89 | } |
90 | |
91 | Qt::ItemFlags PhraseModel::flags(const QModelIndex &index) const |
92 | { |
93 | if (!index.isValid()) |
94 | return {}; |
95 | Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled; |
96 | // Edit is allowed for source & translation if item is from phrasebook |
97 | if (plist.at(i: index.row())->phraseBook() |
98 | && (index.column() != 2)) |
99 | flags |= Qt::ItemIsEditable; |
100 | return flags; |
101 | } |
102 | |
103 | bool PhraseModel::setData(const QModelIndex & index, const QVariant & value, int role) |
104 | { |
105 | int row = index.row(); |
106 | int column = index.column(); |
107 | |
108 | if (!index.isValid() || row >= plist.size() || role != Qt::EditRole) |
109 | return false; |
110 | |
111 | Phrase *phrase = plist.at(i: row); |
112 | |
113 | switch (column) { |
114 | case 0: |
115 | phrase->setSource(value.toString()); |
116 | break; |
117 | case 1: |
118 | phrase->setTarget(value.toString()); |
119 | break; |
120 | case 2: |
121 | phrase->setDefinition(value.toString()); |
122 | break; |
123 | default: |
124 | return false; |
125 | } |
126 | |
127 | emit dataChanged(topLeft: index, bottomRight: index); |
128 | return true; |
129 | } |
130 | |
131 | QVariant PhraseModel::data(const QModelIndex &index, int role) const |
132 | { |
133 | int row = index.row(); |
134 | int column = index.column(); |
135 | |
136 | if (row >= plist.size() || !index.isValid()) |
137 | return QVariant(); |
138 | |
139 | Phrase *phrase = plist.at(i: row); |
140 | |
141 | if (role == Qt::DisplayRole || (role == Qt::ToolTipRole && column != 2)) { |
142 | switch (column) { |
143 | case 0: // source phrase |
144 | return phrase->source().simplified(); |
145 | case 1: // translation |
146 | return phrase->target().simplified(); |
147 | case 2: // definition |
148 | return phrase->definition(); |
149 | } |
150 | } |
151 | else if (role == Qt::EditRole && column != 2) { |
152 | switch (column) { |
153 | case 0: // source phrase |
154 | return phrase->source(); |
155 | case 1: // translation |
156 | return phrase->target(); |
157 | } |
158 | } |
159 | |
160 | return QVariant(); |
161 | } |
162 | |
163 | QT_END_NAMESPACE |
164 | |