-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocumentslistmodel.cpp
More file actions
103 lines (85 loc) · 2.5 KB
/
documentslistmodel.cpp
File metadata and controls
103 lines (85 loc) · 2.5 KB
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
#include "documentslistmodel.h"
#include "documentslistmodule.h"
DocumentsListModel::DocumentsListModel(QObject *parent)
: QAbstractListModel(parent)
, mList(nullptr)
{
}
int DocumentsListModel::rowCount(const QModelIndex &parent) const
{
// For list models only the root node (an invalid parent) should return the list's size. For all
// other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
if (parent.isValid() || !mList)
return 0;
return mList->items().size();
// FIXME: Implement me!
}
QVariant DocumentsListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || !mList)
return QVariant();
const documentsListItem item = mList->items().at(index.row());
switch (role) {
case PathFile:
return QVariant(item.pathFile);
case NameFile:
return QVariant(item.nameFile);
}
return QVariant();
}
bool DocumentsListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if(!mList)
return false;
if (!hasIndex(index.row(), index.column(), index.parent()) || !value.isValid())
return false;
documentsListItem item = mList->items().at(index.row());
switch (role) {
case PathFile:
item.pathFile = value.toString();
break;
case NameFile:
item.nameFile = value.toString();
break;
}
if (mList->setItemAt(index.row(), item)) {
emit dataChanged(index, index, QVector<int>() << role);
return true;
}
return false;
}
Qt::ItemFlags DocumentsListModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEditable; // FIXME: Implement me!
}
QHash<int, QByteArray> DocumentsListModel::roleNames() const
{
QHash<int, QByteArray> names;
names[PathFile] = "pathFile";
names[NameFile] = "nameFile";
return names;
}
DocumentsListModule *DocumentsListModel::list() const
{
return mList;
}
void DocumentsListModel::setList(DocumentsListModule *list)
{
beginResetModel();
if(mList)
mList->disconnect(this);
mList = list;
if(mList)
{
connect(mList, &DocumentsListModule::preItemAppend, this, [=]() {
const int index = mList->items().size();
beginInsertRows(QModelIndex(), index, index);
});
connect(mList, &DocumentsListModule::postItemAppend, this, [=]() {
endInsertRows();
});
}
endResetModel();
}