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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
/*
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 <QIcon>
#include <QColor>
#include <QPalette>
#include <QApplication>
#include "fileinfomodel.h"
#include "fileinfoitem.h"
FileInfoModel::FileInfoModel(QObject *parent) : QAbstractItemModel(parent) {
QList<QVariant> rootData;
rootData << QVariant() << QVariant();
mRootItem = new FileInfoItem(rootData, 0);
}
FileInfoModel::~FileInfoModel() {
delete mRootItem;
}
QModelIndex FileInfoModel::index(int row, int column, const QModelIndex &parent) const{
if(!hasIndex(row, column, parent)){
return QModelIndex();
}
FileInfoItem *parentItem;
if(!parent.isValid()){
parentItem = mRootItem;
}else{
parentItem = static_cast<FileInfoItem*>(parent.internalPointer());
}
FileInfoItem *childItem = parentItem->child(row);
if(childItem){
return createIndex(row, column, childItem);
}
return QModelIndex();
}
QModelIndex FileInfoModel::parent(const QModelIndex &index) const {
if(!index.isValid()){
return QModelIndex();
}
FileInfoItem *childItem = static_cast<FileInfoItem*>(index.internalPointer());
Q_ASSERT(childItem);
if(!childItem){
return QModelIndex();
}
FileInfoItem *parentItem = childItem->parent();
if(parentItem == mRootItem){
return QModelIndex();
}
if(parentItem->row() == -1){
return QModelIndex();
}
return createIndex(parentItem->row(), 0, parentItem);
}
int FileInfoModel::rowCount(const QModelIndex &parent) const {
FileInfoItem *parentItem;
if(parent.column() > 0){
return 0;
}
if(!parent.isValid()){
parentItem = mRootItem;
}else{
parentItem = static_cast<FileInfoItem*>(parent.internalPointer());
}
return parentItem->childCount();
}
int FileInfoModel::columnCount(const QModelIndex &parent) const {
if(!parent.isValid()){
return mRootItem->columnCount();
}
FileInfoItem *item = static_cast<FileInfoItem*>(parent.internalPointer());
return item->columnCount();
}
QVariant FileInfoModel::data(const QModelIndex &index, int role) const {
if(!index.isValid()){
return QVariant();
}
FileInfoItem *item = static_cast<FileInfoItem*>(index.internalPointer());
switch(role) {
case Qt::DisplayRole:
return item->data(index.column());
break;
case Qt::DecorationRole: {
if((item->parent() == mRootItem) && (index.column() == 0)){
return QIcon(":/dildo.png");
}
break;
}
case Qt::ForegroundRole:
if((item->parent() == mRootItem) && (index.column() == 0)){
qApp->palette().color(QPalette::WindowText);
}
return Qt::blue;
break;
}
return QVariant();
}
Qt::ItemFlags FileInfoModel::flags(const QModelIndex &) const {
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}
QVariant FileInfoModel::headerData(int, Qt::Orientation, int) const {
return QVariant();
}
void FileInfoModel::addFiles(const QStringList &files){
mCurrentIndex = QModelIndex();
mTitle = QString();
mMode = File;
mCurrentFiles = files;
insertRows(0, files.count(), createIndex(0, 0, mRootItem));
}
void FileInfoModel::addIndex(const QString &title, const QModelIndex &idx){
mCurrentFiles.clear();
mMode = Index;
mCurrentIndex = idx;
mTitle = title;
insertRows(0, 1, createIndex(0, 0, mRootItem));
}
bool FileInfoModel::insertRows(int row, int count, const QModelIndex &parent){
if(mMode == File){
if(count != mCurrentFiles.count()){
return false;
}
}else{
if((count != 1) || !mCurrentIndex.isValid()){
return false;
}
}
beginInsertRows(parent, row, row + count - 1);
int i(0), j(0);
FileInfoItem *p = static_cast<FileInfoItem*>(parent.internalPointer());
if(!p){
p = mRootItem;
}
for(i = row, j = 0; i < (row + count); ++i, ++j){
FileInfoItem *item = 0;
if(mMode == File){
item = new FileInfoItem(mCurrentFiles.at(j), p);
item->populate();
}else{
item = new FileInfoItem(mTitle, mCurrentIndex, p);
item->populateFromIndex();
}
p->appendChild(item);
}
endInsertRows();
emit layoutChanged();
mCurrentFiles.clear();
return true;
}
bool FileInfoModel::removeRows(int row, int count, const QModelIndex &parent){
FileInfoItem *item = static_cast<FileInfoItem*>(parent.internalPointer());
if(!item){
item = mRootItem;
}
if(((row + count - 1) >= item->childCount()) || (count == 0)) {
return false;
}
beginRemoveRows(parent, row, row + (count - 1));
for(int i = row + count; i > row; --i){
item->removeChild(i - 1);
}
endRemoveRows();
emit layoutChanged();
return true;
}
void FileInfoModel::clear(){
removeRows(0, mRootItem->childCount(), QModelIndex());
}
|