-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclinetablemodel.cpp
More file actions
176 lines (158 loc) · 4.25 KB
/
Copy pathclinetablemodel.cpp
File metadata and controls
176 lines (158 loc) · 4.25 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
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
#include "clinetablemodel.h"
#include <QDebug>
CLineTableModel::CLineTableModel(QObject *parent) : QAbstractTableModel(parent)
{
listClines.clear();
}
QVariant CLineTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole)
{
if(orientation == Qt::Horizontal)
{
switch(section)
{
case 0: return tr("Server");
case 1: return tr("Port");
case 2: return tr("Username");
case 3: return tr("Password");
}
}
else
{
return section+1;
}
}
return QVariant();
}
int CLineTableModel::rowCount(const QModelIndex &parent) const
{
return listClines.size();
}
int CLineTableModel::columnCount(const QModelIndex &parent) const
{
return 4;
}
QVariant CLineTableModel::data(const QModelIndex &index, int role) const
{
if(role == Qt::DisplayRole || role == Qt::EditRole && index.row() <= listClines.size()-1)
{
CLine cline = listClines[index.row()];
switch(index.column())
{
case 0:
return cline.getServer();
case 1:
return cline.getPort();
case 2:
return cline.getUsername();
case 3:
return cline.getPassword();
default:
return "";
}
}
return QVariant();
}
bool CLineTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role == Qt::EditRole)
{
int port = 0;
QString txt = value.toString();
switch(index.column())
{
case 0:
if(CLine::isServerValid(txt))
listClines[index.row()].setServer(txt);
break;
case 1:
bool ok;
port = value.toInt(&ok);
if(ok && CLine::isPortValid(port))
listClines[index.row()].setPort(port);
break;
case 2:
if(CLine::isUsernameValid(txt))
listClines[index.row()].setUsername(txt);
break;
case 3:
if(CLine::isPasswordValid(txt))
listClines[index.row()].setPassword(txt);
break;
}
}
return true;
}
bool CLineTableModel::removeRow(int row, const QModelIndex &parent)
{
if(row >= listClines.size()) return false;
beginRemoveRows(QModelIndex(), row, row);
listClines.removeAt(row);
endRemoveRows();
return true;
}
void CLineTableModel::sort(int column, Qt::SortOrder order)
{
if(order == Qt::AscendingOrder)
{
emit layoutAboutToBeChanged();
switch (column)
{
case 0:
qSort(listClines.begin(), listClines.end(), CLine::sortOnServerAsc);
break;
case 1:
qSort(listClines.begin(), listClines.end(), CLine::sortOnPortAsc);
break;
case 2:
qSort(listClines.begin(), listClines.end(), CLine::sortOnUsernameAsc);
break;
case 3:
qSort(listClines.begin(), listClines.end(), CLine::sortOnPasswordAsc);
break;
}
emit layoutChanged();
}
else
{
emit layoutAboutToBeChanged();
switch (column)
{
case 0:
qSort(listClines.begin(), listClines.end(), CLine::sortOnServerDesc);
break;
case 1:
qSort(listClines.begin(), listClines.end(), CLine::sortOnPortDesc);
break;
case 2:
qSort(listClines.begin(), listClines.end(), CLine::sortOnUsernameDesc);
break;
case 3:
qSort(listClines.begin(), listClines.end(), CLine::sortOnPasswordDesc);
break;
}
emit layoutChanged();
}
}
Qt::ItemFlags CLineTableModel::flags(const QModelIndex &index) const
{
return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled ;
}
void CLineTableModel::clear()
{
int n=rowCount()-1;
beginRemoveRows(QModelIndex(), 0, n);
listClines.clear();
endRemoveRows();
}
void CLineTableModel::addCline(CLine cline)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
listClines.append(cline);
endInsertRows();
}
QList<CLine> CLineTableModel::getClines()
{
return listClines;
}