forked from KDE/kdev-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallprofilesmodel.cpp
160 lines (141 loc) · 5.42 KB
/
allprofilesmodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/***************************************************************************
* Copyright 2007 Niko Sams <[email protected]> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "allprofilesmodel.h"
#include "uploadprofilemodel.h"
#include "kdevuploadplugin.h"
#include <interfaces/iproject.h>
#include <project/projectmodel.h>
#include <interfaces/iprojectcontroller.h>
#include <kconfiggroup.h>
#include <kfileitem.h>
AllProfilesModel::AllProfilesModel(UploadPlugin* plugin, QObject *parent)
: QAbstractListModel(parent), m_plugin(plugin)
{
}
AllProfilesModel::~AllProfilesModel()
{
}
QVariant AllProfilesModel::data(const QModelIndex & index, int role) const
{
if (!index.isValid() || index.parent().isValid()) return QVariant();
int rowOffset = 0;
Q_FOREACH (UploadProfileModel* model, m_sourceModels) {
int rowCount = model->rowCount(index.parent());
if (index.row() - rowOffset < rowCount) {
QVariant ret = model->data(model->index(index.row() - rowOffset, index.column()), role);
if (role == Qt::DisplayRole) {
ret = model->project()->name() + ": " + ret.toString();
}
return ret;
}
rowOffset += rowCount;
}
return QVariant();
}
int AllProfilesModel::rowCount(const QModelIndex & parent) const
{
if (parent.isValid()) return 0;
int ret = 0;
Q_FOREACH (UploadProfileModel* model, m_sourceModels) {
ret += model->rowCount();
}
return ret;
}
void AllProfilesModel::addModel(UploadProfileModel* model)
{
beginResetModel();
connect(model, SIGNAL(modelReset()), this, SLOT(sourceReset()));
connect(model, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
this, SLOT(sourceDataChanged(QModelIndex, QModelIndex)));
connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)),
this, SLOT(sourceRowsAboutToBeInserted(QModelIndex, int, int)));
connect(model, SIGNAL(rowsInserted(QModelIndex, int, int)),
this, SLOT(sourceRowsInserted()));
connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)),
this, SLOT(sourceRowsAboutToBeRemoved(QModelIndex, int, int)));
connect(model, SIGNAL(rowsRemoved(QModelIndex, int, int)),
this, SLOT(sourceRowsRemoved()));
m_sourceModels.append(model);
endResetModel();
}
void AllProfilesModel::removeModel(UploadProfileModel* model)
{
beginResetModel();
m_sourceModels.removeAt(m_sourceModels.indexOf(model));
endResetModel();
}
void AllProfilesModel::sourceReset()
{
beginResetModel();
endResetModel();
}
void AllProfilesModel::sourceDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
{
if (topLeft.parent().isValid() || bottomRight.parent().isValid()) return;
UploadProfileModel* sourceModel = qobject_cast<UploadProfileModel*>(sender());
int rowOffset = 0;
Q_FOREACH (UploadProfileModel* model, m_sourceModels) {
if (model == sourceModel) {
emit dataChanged(model->index(topLeft.row()+rowOffset, topLeft.column()),
model->index(bottomRight.row()+rowOffset, bottomRight.column()));
}
rowOffset += model->rowCount();
}
}
void AllProfilesModel::sourceRowsAboutToBeInserted(const QModelIndex& parent, int start, int end)
{
if (parent.isValid()) return;
UploadProfileModel* sourceModel = qobject_cast<UploadProfileModel*>(sender());
int rowOffset = 0;
Q_FOREACH (UploadProfileModel* model, m_sourceModels) {
if (model == sourceModel) {
emit beginInsertRows(parent, start+rowOffset, end+rowOffset);
}
rowOffset += model->rowCount();
}
}
void AllProfilesModel::sourceRowsInserted()
{
emit endInsertRows();
}
void AllProfilesModel::sourceRowsAboutToBeRemoved(const QModelIndex& parent, int start, int end)
{
if (parent.isValid()) return;
UploadProfileModel* sourceModel = qobject_cast<UploadProfileModel*>(sender());
int rowOffset = 0;
Q_FOREACH (UploadProfileModel* model, m_sourceModels) {
if (model == sourceModel) {
emit beginRemoveRows(parent, start+rowOffset, end+rowOffset);
}
rowOffset += model->rowCount();
}
}
void AllProfilesModel::sourceRowsRemoved()
{
emit endRemoveRows();
}
UploadProfileItem* AllProfilesModel::uploadItem(const QModelIndex& index) const
{
if (!index.isValid() || index.parent().isValid()) return nullptr;
int rowOffset = 0;
Q_FOREACH (UploadProfileModel* model, m_sourceModels) {
int rowCount = model->rowCount(index.parent());
if (index.row() - rowOffset < rowCount) {
return model->uploadItem(model->index(index.row() - rowOffset, index.column()));
}
rowOffset += rowCount;
}
return nullptr;
}
UploadProfileItem* AllProfilesModel::uploadItem(int row, int column) const
{
return uploadItem(index(row, column));
}
// kate: space-indent on; indent-width 4; tab-width 4; replace-tabs on