forked from GLDsuh-a/qt-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirentrymodelinfo.cpp
138 lines (126 loc) · 5.71 KB
/
direntrymodelinfo.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
/*
* This file is part of the PathWalker software
*
* Copyright (C) 2014 Denis Kvita <[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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <QColor>
#include <QBrush>
#include <QDateTime>
#include <QLocale>
#include <QtDebug>
#include "direntrymodelinfo.h"
#include "direntry.h"
DirEntryModelInfo::DirEntryModelInfo(const EntityInfo &info)
:EntityListModelInfo(info)
{
aColProp.append(DirEntryProperty::Mark);
aColProp.append(DirEntryProperty::Name);
aColProp.append(DirEntryProperty::Size);
aColProp.append(DirEntryProperty::LastModified);
aColProp.append(DirEntryProperty::isDir);
aColFlag<<ColumnFlags(Qt::NoItemFlags,Qt::ItemIsEnabled|Qt::ItemIsUserCheckable)
<<ColumnFlags(Qt::NoItemFlags,Qt::ItemIsEnabled|Qt::ItemIsEditable)
<<ColumnFlags(Qt::ItemIsEditable,Qt::ItemIsEnabled)
<<ColumnFlags(Qt::ItemIsEditable,Qt::ItemIsEnabled)
<<ColumnFlags(Qt::ItemIsEditable,Qt::ItemIsEnabled);
aColRole.append(RoleSet()<<Qt::BackgroundRole<<Qt::ToolTipRole<<Qt::CheckStateRole);
aColRole.append(RoleSet()<<Qt::BackgroundRole<<Qt::ToolTipRole<<Qt::DisplayRole<<Qt::EditRole<<Qt::DecorationRole);
aColRole.append(RoleSet()<<Qt::BackgroundRole<<Qt::ToolTipRole<<Qt::DisplayRole<<Qt::EditRole);
aColRole.append(RoleSet()<<Qt::BackgroundRole<<Qt::ToolTipRole<<Qt::DisplayRole<<Qt::EditRole);
aColRole.append(RoleSet()<<Qt::BackgroundRole<<Qt::ToolTipRole<<Qt::DisplayRole<<Qt::EditRole<<Qt::CheckStateRole);
if(aRolesMap.isEmpty()){
RolesMap roles;
roles.insert((int)Qt::DecorationRole, (int)DirEntryProperty::icon);
aRolesMap.insert((int)DirEntryProperty::Name, roles);
}
}
QVariant DirEntryModelInfo::data(const EntityInstance &e, int column, int role) const
{
if(!aColRole[column].contains(role)) return QVariant();
if(role==Qt::ToolTipRole){
if(aColProp[column]==EntityProperty::Modify){
if(!e.isSet(aColProp[column])) return QVariant();
return e.get(aColProp[column]).typeName()
+QString::number((int)e.get(aColProp[column]).value<int>());
}
return e.get(aColProp[column]).toString().prepend("<p>").append("</p>");
}
if(role==Qt::EditRole){
return e.get(aColProp[column]);
}
if(role==Qt::DisplayRole){
if(aColProp[column]==DirEntryProperty::isDir) return QVariant();
if(aColProp[column]==DirEntryProperty::LastModified) return e.get(DirEntryProperty::LastModified).value<QDateTime>().toString("dd.MM.yyyy hh:mm:ss");
if(aColProp[column]==DirEntryProperty::Size){
if(e.get(DirEntryProperty::isDir).value<bool>()) return QVariant();
qint64 sz= e.get(DirEntryProperty::Size).value<qint64>();
QString ret= QString::number(sz);
QString sfx= " b";
if(sz>=1000000000000){
ret= ret.left(ret.length()-10);
sfx= " Tb";
}else if(sz>=1000000000){
ret= ret.left(ret.length()-7);
sfx= " Gb";
}else if(sz>=1000000){
ret= ret.left(ret.length()-4);
sfx= " Mb";
}else if(sz>=1000){
ret= ret.left(ret.length()-1);
sfx= " Kb";
}
return (sfx!=" b"? ret.insert(ret.length()-2,QLocale::system().decimalPoint()):ret)+sfx;
}
return e.get(aColProp[column]).toString();
}
if(role==Qt::CheckStateRole){
if(aColRole[column].contains(Qt::CheckStateRole)){
if(aColProp[column]==DirEntryProperty::isDir){
return e.get(aColProp[column]).value<bool>()?Qt::Checked:Qt::Unchecked;
}
return (e.isSet(aColProp[column])
?e.get(aColProp[column]):Qt::Unchecked);
}
}
if(role==Qt::BackgroundRole){
if(e.get(DirEntryProperty::Mark).value<Qt::CheckState>()==Qt::Checked) return QBrush(QColor(0xff,0xff,0x00,0x4f));
}
if(role==Qt::DecorationRole){
if(aColProp[column]==EntityProperty::Modify){
if(!e.isSet(aColProp[column])) return QVariant();
int v= e.get(aColProp[column]).value<int>();
if(v==EntityModify::Saved) return QVariant();
if(v==EntityModify::New) return QPixmap(":/img/new.png");
if(v==EntityModify::Edited) return QPixmap(":/img/pencil-small.png");
}
}
return value(e, aColProp[column], role);
}
QVariant DirEntryModelInfo::headerData(int section, Qt::Orientation orientation, int role) const
{
if(orientation==Qt::Horizontal){
if(role==Qt::DecorationRole){
if(aColProp[section]==EntityProperty::Mark) return QPixmap(":/img/ui-check-boxes.png");
if(aColProp[section]==EntityProperty::Modify) return QPixmap(":/img/database-small.png");
}else if(role==Qt::ToolTipRole){
if(aColProp[section]==EntityProperty::Mark) return QString(QObject().tr("mark"));
return aEntInfo.propertyName(aColProp[section]);
}else if(role==Qt::DisplayRole || role==Qt::EditRole){
return aEntInfo.propertyName(aColProp[section]);
}
}
return QVariant();
}