-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_system_custom_model.cpp
150 lines (133 loc) · 5.45 KB
/
file_system_custom_model.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
#include <QTableView>
#include <QDebug> // TODO: delete
#include <QMessageBox> // TODO; delete
#include <stdexcept>
#include "file_system_custom_model.h"
#include "lines_counter.h"
FileSystemCustomModel::FileSystemCustomModel(QObject *parent)
: QFileSystemModel(parent)
{
resizeLinesQuantity(10);
}
QVariant FileSystemCustomModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) return QVariant();
/* NOTE: There are solution for setting the checking state of the checkboxes in the views
* There are need to get knows how allow users to change state of the checkboxes
*/
// if (role == Qt::CheckStateRole && index.column() == 0)
// return Qt::Checked;
/* NOTE: cannot correct fill background color, because view contents changed dynamically,
* but this method fill model items statically
* Want to do the changes rows colors, for example (rotated to 90 grad): | white | red | white | red | white |
*/
// // fill model background color
// if (role == Qt::BackgroundRole) {
// qDebug() << "background role, row =" << index.row();
// if (index.row() % 2) {
// qDebug() << "set background";
// return QColor("#eeeeec");
// }
// }
QVariant data;
QModelIndex sourceModelIndex;
switch(index.column()) {
case ihh_Name:
if (role == Qt::TextAlignmentRole)
data = int(Qt::AlignLeft | Qt::AlignVCenter);
else
data = QFileSystemModel::data(index, role);
break;
case ihh_Size:
if (role == Qt::TextAlignmentRole)
data = int(Qt::AlignRight | Qt::AlignVCenter);
// else if (role == Qt::BackgroundRole)
// data = QBrush(QColor(234, 150, 174), Qt::Dense3Pattern);
else
data = QFileSystemModel::data(index, role);
break;
case ihh_Type:
if (role == Qt::TextAlignmentRole)
data = int(Qt::AlignRight | Qt::AlignVCenter);
else if (role == Qt::DisplayRole) {
t_linesQnty value = m_linesQnts.at(index.row());
if (value != empty_value) data = QVariant(value);
// qDebug() << "model::data(), row =" << index.row() << ", col =" << index.column() << ", data =" << data;
// qDebug() << "Quantities:" << m_linesQnties << ", size =" << m_linesQnties.size();
}
// else if (role == Qt::BackgroundRole)
// data = QBrush(QColor(234, 150, 174), Qt::Dense2Pattern);
break;
case ihh_Modified:
if (role == Qt::TextAlignmentRole)
data = int(Qt::AlignHCenter | Qt::AlignVCenter);
else if (role == Qt::DisplayRole) {
sourceModelIndex = QFileSystemModel::index(index.row(), ihh_Type, index.parent());
data = QFileSystemModel::data(sourceModelIndex, role);
}
// else if (role == Qt::BackgroundRole)
// data = QBrush(QColor(234, 150, 174), Qt::Dense1Pattern);
break;
case ihh_Empty:
if (role == Qt::TextAlignmentRole)
data = int(Qt::AlignLeft | Qt::AlignVCenter);
// else if (role == Qt::BackgroundRole)
// data = QBrush(QColor(234, 150, 174), Qt::SolidPattern);
else {
sourceModelIndex = QFileSystemModel::index(index.row(), ihh_Modified, index.parent());
data = QFileSystemModel::data(sourceModelIndex, role);
}
break;
}
return data;
}
QVariant FileSystemCustomModel::headerData(int section, Qt::Orientation orientation, int role) const
{
QVariant name;
if (orientation == Qt::Horizontal && (role == Qt::DisplayRole || role == Qt::TextAlignmentRole)) {
switch(section) {
case ihh_Name:
name = QFileSystemModel::headerData(ihh_Name, orientation); break;
case ihh_Size:
name = QFileSystemModel::headerData(ihh_Size, orientation); break;
case ihh_Type:
name = QVariant("Lines"); break;
case ihh_Modified:
name = QFileSystemModel::headerData(ihh_Type, orientation); break;
case ihh_Empty:
name = QFileSystemModel::headerData(ihh_Modified, orientation); break;
}
}
// else
// name = QFileSystemModel::headerData(section, orientation, role);
return name;
}
int FileSystemCustomModel::columnCount(const QModelIndex &parent) const
{
return QFileSystemModel::columnCount(parent) + 1;
}
void FileSystemCustomModel::setLinesQuantity(int index, t_linesQnty value)
{
if(index < 0 || index >= m_linesQnts.size())
throw std::runtime_error( QString("FileSystemCustomModel::setLinesQuantity()\ninvalid index = %1").arg(index).toStdString() );
m_linesQnts[index] = value;
// qDebug() << "model::setLinesQuantity(): index =" << index << ", data =" << value << ", all data =" << m_linesQnts;
}
t_linesQnty FileSystemCustomModel::getLinesQuantity(int index) const
{
return (index < 0 || index >= m_linesQnts.size())
? (t_linesQnty)invalid_value
: m_linesQnts.at(index);
}
void FileSystemCustomModel::resizeLinesQuantity(int size)
{
if (!m_linesQnts.isEmpty())
m_linesQnts.clear();
m_linesQnts.fill(empty_value, size);
// qDebug() << "<!!!>model::resize():" << m_linesQnts << ", size =" << m_linesQnts.size();
}
bool FileSystemCustomModel::canCalcLines(const QModelIndex &indexSource) const
{
QFileInfo infoFile = fileInfo(indexSource);
return infoFile.isFile() && infoFile.isReadable() && !infoFile.isExecutable();
}