1 /*
2 * Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 or (at your option)
7 * version 3 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "AutoTypeAssociationsModel.h"
19
AutoTypeAssociationsModel(QObject * parent)20 AutoTypeAssociationsModel::AutoTypeAssociationsModel(QObject* parent)
21 : QAbstractListModel(parent)
22 , m_autoTypeAssociations(nullptr)
23 {
24 }
25
setAutoTypeAssociations(AutoTypeAssociations * autoTypeAssociations)26 void AutoTypeAssociationsModel::setAutoTypeAssociations(AutoTypeAssociations* autoTypeAssociations)
27 {
28 beginResetModel();
29
30 if (m_autoTypeAssociations) {
31 m_autoTypeAssociations->disconnect(this);
32 }
33
34 m_autoTypeAssociations = autoTypeAssociations;
35
36 if (m_autoTypeAssociations) {
37 connect(m_autoTypeAssociations, SIGNAL(dataChanged(int)), SLOT(associationChange(int)));
38 connect(m_autoTypeAssociations, SIGNAL(aboutToAdd(int)), SLOT(associationAboutToAdd(int)));
39 connect(m_autoTypeAssociations, SIGNAL(added(int)), SLOT(associationAdd()));
40 connect(m_autoTypeAssociations, SIGNAL(aboutToRemove(int)), SLOT(associationAboutToRemove(int)));
41 connect(m_autoTypeAssociations, SIGNAL(removed(int)), SLOT(associationRemove()));
42 connect(m_autoTypeAssociations, SIGNAL(aboutToReset()), SLOT(aboutToReset()));
43 connect(m_autoTypeAssociations, SIGNAL(reset()), SLOT(reset()));
44 }
45
46 endResetModel();
47 }
48
rowCount(const QModelIndex & parent) const49 int AutoTypeAssociationsModel::rowCount(const QModelIndex& parent) const
50 {
51 if (!m_autoTypeAssociations || parent.isValid()) {
52 return 0;
53 }
54 else {
55 return m_autoTypeAssociations->size();
56 }
57 }
58
columnCount(const QModelIndex & parent) const59 int AutoTypeAssociationsModel::columnCount(const QModelIndex& parent) const
60 {
61 Q_UNUSED(parent);
62
63 return 2;
64 }
65
headerData(int section,Qt::Orientation orientation,int role) const66 QVariant AutoTypeAssociationsModel::headerData(int section, Qt::Orientation orientation, int role) const
67 {
68 if ((orientation == Qt::Horizontal) && (role == Qt::DisplayRole)) {
69 if (section == 0) {
70 return tr("Window");
71 }
72 else {
73 return tr("Sequence");
74 }
75 }
76 else {
77 return QVariant();
78 }
79 }
80
data(const QModelIndex & index,int role) const81 QVariant AutoTypeAssociationsModel::data(const QModelIndex& index, int role) const
82 {
83 if (!index.isValid()) {
84 return QVariant();
85 }
86
87 if (role == Qt::DisplayRole) {
88 if (index.column() == 0) {
89 return m_autoTypeAssociations->get(index.row()).window;
90 }
91 else {
92 QString sequence = m_autoTypeAssociations->get(index.row()).sequence;
93 if (sequence.isEmpty()) {
94 sequence = tr("Default sequence");
95 }
96 return sequence;
97 }
98 }
99 else {
100 return QVariant();
101 }
102 }
103
associationChange(int i)104 void AutoTypeAssociationsModel::associationChange(int i)
105 {
106 Q_EMIT dataChanged(index(i, 0), index(i, columnCount() - 1));
107 }
108
associationAboutToAdd(int i)109 void AutoTypeAssociationsModel::associationAboutToAdd(int i)
110 {
111 beginInsertRows(QModelIndex(), i, i);
112 }
113
associationAdd()114 void AutoTypeAssociationsModel::associationAdd()
115 {
116 endInsertRows();
117 }
118
associationAboutToRemove(int i)119 void AutoTypeAssociationsModel::associationAboutToRemove(int i)
120 {
121 beginRemoveRows(QModelIndex(), i, i);
122 }
123
associationRemove()124 void AutoTypeAssociationsModel::associationRemove()
125 {
126 endRemoveRows();
127 }
128
aboutToReset()129 void AutoTypeAssociationsModel::aboutToReset()
130 {
131 beginResetModel();
132 }
133
reset()134 void AutoTypeAssociationsModel::reset()
135 {
136 endResetModel();
137 }
138