-
Notifications
You must be signed in to change notification settings - Fork 4
/
SimpleViewModulesModel.cpp
334 lines (298 loc) · 9.81 KB
/
SimpleViewModulesModel.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* FTSPlot - fast time series dataset plotter
Copyright (C) 2013 Michael Riss <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
#include <QDebug>
#include "SimpleViewModulesModel.h"
#include "SimpleViewWidget.h"
using namespace FTSPlot;
SimpleViewModulesModel::SimpleViewModulesModel ( SimpleViewWidget* upper ) : QAbstractTableModel ( upper )
{
dataObject = upper;
}
int SimpleViewModulesModel::rowCount ( const QModelIndex& parent ) const
{
return dataObject->modules.size();
}
int SimpleViewModulesModel::columnCount ( const QModelIndex& parent ) const
{
return 5;
}
QVariant SimpleViewModulesModel::data ( const QModelIndex& index, int role ) const
{
if ( ( index.row() >= dataObject->modules.size() ) || ( index.column() >= 5 ) )
{
return QVariant();
}
// access data structure and return value
if ( index.column() == 0 && role == Qt::CheckStateRole )
{
if ( dataObject->modules[index.row() ].visible )
{
return QVariant ( Qt::Checked );
}
else
{
return QVariant ( Qt::Unchecked );
}
}
if ( index.column() == 1 && role == Qt::CheckStateRole )
{
if ( dataObject->modules[index.row() ].module->GUIvisible() )
{
return QVariant ( Qt::Checked );
}
else
{
return QVariant ( Qt::Unchecked );
}
}
if ( index.column() == 2 && role == Qt::CheckStateRole )
{
if ( dataObject->activeInputModule == dataObject->modules[index.row() ].module )
{
return QVariant ( Qt::Checked );
}
else
{
return QVariant ( Qt::Unchecked );
}
}
if ( index.column() == 3 && ( role == Qt::DisplayRole || role == Qt::EditRole ) )
{
return QVariant ( dataObject->modules[index.row() ].name );
}
if ( index.column() == 3 && role == Qt::ToolTipRole )
{
return QVariant ( dataObject->modules[index.row() ].module->getPath() );
}
if ( index.column() == 4 && role == Qt::DecorationRole )
{
return QVariant ( dataObject->modules[index.row() ].color );
}
// else just return the empty value
return QVariant();
}
bool SimpleViewModulesModel::setData ( const QModelIndex& index, const QVariant& value, int role )
{
if ( ( index.row() >= dataObject->modules.size() ) || ( index.column() >= 5 ) )
{
return false;
}
if ( index.column() == 0 && role == Qt::CheckStateRole )
{
dataObject->modules[index.row() ].visible = value.toBool();
emit dataChanged ( index, index );
dataObject->updateVizList();
return true;
}
if ( index.column() == 1 && role == Qt::CheckStateRole )
{
if ( value.toBool() )
{
dataObject->modules[index.row() ].module->showGUI();
}
else
{
dataObject->modules[index.row() ].module->hideGUI();
}
emit dataChanged ( index, index );
return true;
}
if ( index.column() == 2 && role == Qt::CheckStateRole )
{
if ( value.toBool() )
{
dataObject->activeInputModule = dataObject->modules[index.row() ].module;
emit dataChanged ( QAbstractItemModel::createIndex ( 0, 2 ), QAbstractItemModel::createIndex ( dataObject->modules.size()-1, 2 ) );
return true;
}
else
{
if ( dataObject->activeInputModule == dataObject->modules[index.row() ].module )
{
dataObject->activeInputModule = NULL;
emit dataChanged ( index, index );
return true;
}
else
{
// we cannot deactivate the current module if it's not activated
return false;
}
}
}
if ( index.column() == 3 && role == Qt::EditRole )
{
dataObject->modules[index.row() ].name = value.toString();
emit dataChanged ( index, index );
return true;
}
if ( index.column() == 4 && role == Qt::DecorationRole )
{
dataObject->modules[index.row() ].color = value.value<QColor>();
dataObject->modules[index.row() ].module->setColor ( value.value<QColor>() );
dataObject->update();
emit dataChanged ( index, index );
return true;
}
return false;
}
QVariant SimpleViewModulesModel::headerData ( int section, Qt::Orientation orientation, int role ) const
{
if ( ( orientation == Qt::Horizontal ) && ( role == Qt::DisplayRole ) )
{
switch ( section )
{
case 0:
return QVariant ( QString ( "Graph" ) );
break;
case 1:
return QVariant ( QString ( "GUI" ) );
break;
case 2:
return QVariant ( QString ( "Edit active" ) );
break;
case 3:
return QVariant ( QString ( "Name" ) );
break;
case 4:
return QVariant ( QString ( "Color" ) );
break;
default:
qDebug() << "QVariant SimpleViewModulesModel::headerData() should never get sections >= 4!";
break;
}
}
return QVariant();
}
Qt::ItemFlags SimpleViewModulesModel::flags ( const QModelIndex& index ) const
{
switch ( index.column() )
{
case 0:
return ( Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable );
break;
case 1:
if ( dataObject->modules[index.row() ].module->hasGUI() )
{
return ( Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable );
}
else
{
return ( Qt::ItemIsSelectable | Qt::ItemIsUserCheckable );
}
break;
case 2:
if ( dataObject->modules[index.row() ].editable )
{
return ( Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable );
}
else
{
return ( Qt::ItemIsSelectable | Qt::ItemIsUserCheckable );
}
break;
case 3:
return ( Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable );
break;
case 4:
return ( Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable );
break;
default:
qDebug() << "Qt::ItemFlags SimpleViewModulesModel::flags() should never get columns >= 4!";
break;
}
return Qt::NoItemFlags;
}
bool SimpleViewModulesModel::insertRows ( int position, int rows, const QModelIndex& index )
{
if ( position >= dataObject->modules.size() )
{
return false;
}
beginInsertRows ( QModelIndex(), position, position+rows-1 );
vizModule tmp;
tmp.visible = false;
tmp.editable = false;
for ( int i = 0; i < rows; i++ )
{
dataObject->modules.insert ( position, tmp );
}
endInsertRows();
return true;
}
bool SimpleViewModulesModel::removeRows ( int position, int rows, const QModelIndex& index )
{
if ( position >= dataObject->modules.size() )
{
return false;
}
if ( position + rows > dataObject->modules.size() )
{
return false;
}
beginRemoveRows ( QModelIndex(), position, position+rows-1 );
for ( int i = 0; i < rows; i++ )
{
if( dataObject->activeInputModule == dataObject->modules[position].module )
{
dataObject->activeInputModule = NULL;
}
dataObject->modules.removeAt ( position );
}
endRemoveRows();
return true;
}
void SimpleViewModulesModel::append ( vizModule& mod )
{
beginInsertRows ( QModelIndex(), dataObject->modules.size(), dataObject->modules.size() );
dataObject->modules.append ( mod );
endInsertRows();
}
void SimpleViewModulesModel::prepend ( vizModule& mod )
{
beginInsertRows ( QModelIndex(), 0, 0 );
dataObject->modules.prepend ( mod );
endInsertRows();
}
void SimpleViewModulesModel::GUIupdate()
{
emit dataChanged ( QAbstractItemModel::createIndex ( 0, 1 ), QAbstractItemModel::createIndex ( dataObject->modules.size()-1, 1 ) );
}
void SimpleViewModulesModel::swapRows ( int first, int second )
{
if( first >= 0 && first < dataObject->modules.size() &&
second >= 0 && second < dataObject->modules.size() )
{
dataObject->modules.swap( first, second );
dataObject->updateVizList();
emit dataChanged ( QAbstractItemModel::createIndex ( first, 0 ), QAbstractItemModel::createIndex ( first, 4 ) );
emit dataChanged ( QAbstractItemModel::createIndex ( second, 0 ), QAbstractItemModel::createIndex ( second, 4 ) );
}
}
void SimpleViewModulesModel::deleteModule ( int idx )
{
// take module out of module list
vizModule tmp = dataObject->modules[idx];
removeRows ( idx, 1, QModelIndex() );
// purge all instances of the module in temporary lists
dataObject->useList.removeAll( tmp.module );
dataObject->reqList.removeAll( tmp.module );
dataObject->vizList.removeAll( tmp.module );
dataObject->updateList.removeAll( tmp.module );
dataObject->reqSet.remove( tmp.module );
// delete module
delete( tmp.module );
dataObject->updateVizList();
}
// kate: indent-mode cstyle; space-indent on; indent-width 4;