2
* Copyright 2013 Kevin Funk <kfunk@kde.org>
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License as
6
* published by the Free Software Foundation; either version 2 of
7
* the License or (at your option) version 3 or any later version
8
* accepted by the membership of KDE e.V. (or its successor approved
9
* by the membership of KDE e.V.), which shall act as a proxy
10
* defined in Section 14 of version 3 of the license.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22
#include "placeholderitemproxymodel.h"
24
#include <KColorScheme>
26
using namespace KDevelop;
28
struct PlaceholderItemProxyModel::Private
30
Private(PlaceholderItemProxyModel* qq)
34
inline int sourceRowCount()
36
return q->sourceModel() ? q->sourceModel()->rowCount() : 0;
39
inline bool isPlaceholderRow(const QModelIndex& index) const
41
if (!q->sourceModel()) {
44
return index.row() == q->sourceModel()->rowCount();
47
PlaceholderItemProxyModel* const q;
49
/// column -> hint mapping
50
QMap<int, QVariant> m_columnHints;
53
PlaceholderItemProxyModel::PlaceholderItemProxyModel(QObject* parent)
54
: KIdentityProxyModel(parent)
55
, d(new Private(this))
58
PlaceholderItemProxyModel::~PlaceholderItemProxyModel()
62
QVariant PlaceholderItemProxyModel::columnHint(int column) const
64
return d->m_columnHints.value(column);
67
void PlaceholderItemProxyModel::setColumnHint(int column, const QVariant& hint)
73
d->m_columnHints[column] = hint;
75
const int row = d->sourceRowCount();
76
emit dataChanged(index(row, 0), index(row, columnCount()));
79
Qt::ItemFlags PlaceholderItemProxyModel::flags(const QModelIndex& index) const
81
if (d->isPlaceholderRow(index)) {
82
Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
83
const int column = index.column();
84
// if the column doesn't provide a hint we assume that we can't edit this field
85
if (d->m_columnHints.contains(column)) {
86
flags |= Qt::ItemIsEditable;
91
return KIdentityProxyModel::flags(index);
94
void PlaceholderItemProxyModel::setSourceModel(QAbstractItemModel* sourceModel)
96
KIdentityProxyModel::setSourceModel(sourceModel);
97
// TODO: Listen to layoutDataChanged signals?
100
int PlaceholderItemProxyModel::rowCount(const QModelIndex& parent) const
105
// only flat models supported for now, assert early in case that's not true
106
Q_ASSERT(!parent.isValid());
108
return sourceModel()->rowCount() + 1;
111
QVariant PlaceholderItemProxyModel::data(const QModelIndex& proxyIndex, int role) const
113
const int column = proxyIndex.column();
114
if (d->isPlaceholderRow(proxyIndex)) {
116
case Qt::DisplayRole:
117
return columnHint(column);
118
case Qt::ForegroundRole: {
119
const KColorScheme scheme(QPalette::Normal);
120
return scheme.foreground(KColorScheme::InactiveText);
126
return KIdentityProxyModel::data(proxyIndex, role);
129
QModelIndex PlaceholderItemProxyModel::parent(const QModelIndex& child) const
131
if (d->isPlaceholderRow(child)) {
132
return QModelIndex();
135
return KIdentityProxyModel::parent(child);
138
QModelIndex PlaceholderItemProxyModel::buddy(const QModelIndex& index) const
140
if (d->isPlaceholderRow(index)) {
143
return KIdentityProxyModel::buddy(index);
146
QModelIndex PlaceholderItemProxyModel::mapToSource(const QModelIndex& proxyIndex) const
148
if (d->isPlaceholderRow(proxyIndex)) {
149
return QModelIndex();
151
return KIdentityProxyModel::mapToSource(proxyIndex);
154
bool PlaceholderItemProxyModel::setData(const QModelIndex& index, const QVariant& value, int role)
156
const int column = index.column();
157
if (d->isPlaceholderRow(index) && role == Qt::EditRole && d->m_columnHints.contains(column)) {
158
const bool accept = validateRow(index, value);
159
// if validation fails, clear the complete line
161
emit dataChanged(index, index);
166
emit dataChanged(index, index);
169
emit dataInserted(column, value);
172
return KIdentityProxyModel::setData(index, value, role);
175
QModelIndex PlaceholderItemProxyModel::index(int row, int column, const QModelIndex& parent) const
177
Q_ASSERT(!parent.isValid());
180
const bool isPlaceHolderRow = (sourceModel() ? row == sourceModel()->rowCount() : false);
181
if (isPlaceHolderRow) {
182
return createIndex(row, column);
184
return KIdentityProxyModel::index(row, column, parent);
187
bool PlaceholderItemProxyModel::validateRow(const QModelIndex& index, const QVariant& value) const
190
return !value.toString().isEmpty();
193
#include "moc_placeholderitemproxymodel.cpp"