Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
phrasemodel.cpp
Go to the documentation of this file.
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
7
8void PhraseModel::removePhrases()
9{
10 int r = plist.size();
11 if (r > 0) {
12 beginResetModel();
13 plist.clear();
14 endResetModel();
15 }
16}
17
18Phrase *PhraseModel::phrase(const QModelIndex &index) const
19{
20 return plist.at(index.row());
21}
22
23void 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(r, 0);
31 const QModelIndex &ei = index(r, 2);
32 emit dataChanged(si, ei);
33}
34
36{
37 int r = plist.size();
38
39 plist.append(p);
40
41 // update phrases as we add them
42 beginInsertRows(QModelIndex(), r, r);
43 QModelIndex i = index(r, 0);
44 endInsertRows();
45 return i;
46}
47
48void PhraseModel::removePhrase(const QModelIndex &index)
49{
50 int r = index.row();
51 beginRemoveRows(QModelIndex(), r, r);
52 plist.removeAt(r);
53 endRemoveRows();
54}
55
57{
58 int row;
59 if ((row = plist.indexOf(phr)) == -1)
60 return QModelIndex();
61
62 return index(row, 0);
63}
64
65int PhraseModel::rowCount(const QModelIndex &) const
66{
67 return plist.size();
68}
69
70int PhraseModel::columnCount(const QModelIndex &) const
71{
72 return 3;
73}
74
75QVariant PhraseModel::headerData(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("Source phrase");
81 case 1:
82 return tr("Translation");
83 case 2:
84 return tr("Definition");
85 }
86 }
87
88 return QVariant();
89}
90
91Qt::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(index.row())->phraseBook()
98 && (index.column() != 2))
99 flags |= Qt::ItemIsEditable;
100 return flags;
101}
102
103bool 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(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(index, index);
128 return true;
129}
130
131QVariant 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(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
163QT_END_NAMESPACE
Phrase * phrase(const QModelIndex &index) const
int rowCount(const QModelIndex &) const override
Returns the number of rows under the given parent.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Sets the role data for the item at index to value.
void setPhrase(const QModelIndex &indx, Phrase *ph)
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns the item flags for the given index.
QModelIndex addPhrase(Phrase *p)
int columnCount(const QModelIndex &) const override
Returns the number of columns for the children of the given parent.
QModelIndex index(Phrase *const phr) const
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Returns the data for the given role and section in the header with the specified orientation.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Returns the data stored under the given role for the item referred to by the index.
void removePhrase(const QModelIndex &index)
Combined button and popup list for selecting options.