-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
iROOTWorksheet.h
406 lines (356 loc) · 10.3 KB
/
iROOTWorksheet.h
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#ifndef IROOTWORKSHEET_H
#define IROOTWORKSHEET_H
// File modified by Deepak Samuel on 25 Sep 2019
#include <QApplication>
#include <QAbstractTableModel>
#include <QAction>
#include <QClipboard>
#include <QMenu>
#include <QTableView>
#include <QUndoStack>
#include <QFile>
#include <QTextStream>
#include <QThread>
#include <TTree.h>
#include <TFile.h>
#include <TH1F.h>
#include <TH1S.h>
#include <TEventList.h>
#include <iROOTTree.h>
//#include <IROOTPad.h>
typedef struct {
QStringList columnName;
QStringList type;
} tableHeader;
class IROOTWorksheet : public QAbstractTableModel
{
Q_OBJECT
public:
IROOTWorksheet(int type=0);
IROOTWorksheet(QList <QStringList> Data, int rows, int columns, QStringList Header);
//IROOTWorksheet(QString fileName);
int rowCount(const QModelIndex& parent) const;
int columnCount(const QModelIndex& parent) const;
QVariant data(const QModelIndex& index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
void SetTable(QTableView* view){table = view;}
Qt::ItemFlags flags(const QModelIndex &index) const;
void makeMenus();
bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
void resetTree();
void refreshTree();
void clearData();
void convertData();
// QList <int> GetSelectedColumns(QModelIndexList list);
// QList <int> GetXColumnRowIndexes(QModelIndexList list);
QList<double> GetRange(QModelIndexList list);
QString saveData(QString fileName, QString type);
tableHeader getTableHeader();
QStringList getHeader(){return header;}
void SetCurrentTree(TTree* t, QStringList branches);
void SetCurrentFile(QString rootFile);
void ApplyFilter(QString cut);
QList <TTree*> GetTree(TFile* f);
int PlotData(QString plotString, QString cutString="", QString opt="");
private slots:
void insertRowAbove();
void insertRowBelow();
void deleteRow();
void appendRow();
void insertColumnLeft();
void insertColumnRight();
void deleteColumn();
void appendColumn();
void fillRowNumbers();
void showMenu(QPoint p);
void Copy();
void Paste();
void PlotBarHistogram();
void PlotPieChart();
void PlotMarkerGraph();
void PlotLineGraph();
void PlotCurveGraph();
void PlotHistogram(QString type="BAR");
void PlotGraph(QString type="MARKER");
signals:
void Message(QString message, QString type);
void CreatePad();
private:
friend class SetData;
friend class AppendRow;
friend class RemoveRow;
friend class AppendColumn;
friend class RemoveColumn;
friend class PasteData;
friend class Clear;
friend class RowNumberFiller;
QList <QStringList> rowData;
int nColumns;
int nRows;
static int wsN;
QStringList header;
QStringList branchList;
QMenu *menu;
QMenu *histogramMenu;
QMenu *graphMenu;
QTableView* table; // the table connected to this model
QPoint position;
int columnN; // used for naming columns
TTree *tree; // the tree, is zero if not from tree
QAction *newRowAbove;
QAction *newRowBelow;
QAction *newColLeft;
QAction *newColRight;
QAction *delRow;
QAction *delCol;
QAction *copyAction;
QAction *pasteAction;
QAction *plotHistogram;
QAction *plotGraph;
QAction *plotBarHistogram;
QAction *plotPieChart;
QAction *plotMarkerGraph;
QAction *plotLineGraph;
QAction *plotCurveGraph;
QAction *fillRowNum;
QString currentFile;
QUndoStack *undo_stack;
int dataType; //0: Normal, editable, 1: Tree, 2: Fit data
int nBins;
};
class SetData : public QUndoCommand
{
public:
SetData(const QModelIndex & index, const QVariant & value, IROOTWorksheet* wsModel) : QUndoCommand("set data"),m_model(wsModel),old_index(index)
{
oldData = m_model->data(index,Qt::DisplayRole);
if(index.isValid()){
m_model->rowData[index.row()][index.column()]=value.toString();
}
}
void redo()
{
}
void undo()
{
if(old_index.isValid()){
m_model->rowData[old_index.row()][old_index.column()]=oldData.toString();
}
}
private:
QVariant oldData;
IROOTWorksheet *m_model;
QModelIndex old_index;
};
class AppendRow : public QUndoCommand
{
public:
AppendRow(int atRow, IROOTWorksheet* wsModel) : QUndoCommand("add row"),m_model(wsModel),old_row(atRow)
{
QStringList l;
if(atRow<0) atRow=0;
for(int ii=0;ii<wsModel->nColumns;ii++)
l.append("");
wsModel->rowData.insert(atRow,l);
wsModel->nRows++;
}
void redo()
{
}
void undo()
{
m_model->rowData.removeAt(old_row);
m_model->nRows--;
emit m_model->layoutChanged();
}
private:
int old_row;
IROOTWorksheet *m_model;
};
class RemoveRow : public QUndoCommand
{
public:
RemoveRow(int atRow, IROOTWorksheet* wsModel) : QUndoCommand("remove row"),m_model(wsModel),old_row(atRow)
{
old_row_data = wsModel->rowData.at(atRow);
wsModel->rowData.removeAt(atRow);
wsModel->nRows--;
}
void redo()
{
}
void undo()
{
m_model->rowData.insert(old_row,old_row_data);
m_model->nRows++;
emit m_model->layoutChanged();
}
private:
int old_row;
QStringList old_row_data;
IROOTWorksheet *m_model;
};
class AppendColumn : public QUndoCommand
{
public:
AppendColumn(int atCol, IROOTWorksheet* wsModel) : QUndoCommand("add column"),m_model(wsModel),old_col(atCol)
{
for(int ii=0;ii<wsModel->nRows;ii++)
wsModel->rowData[ii].insert(atCol,"");
wsModel->header.insert(atCol,QString("New Column %1").arg(wsModel->columnN++));
wsModel->nColumns++;
}
void redo()
{
}
void undo()
{
for(int ii=0;ii<m_model->nRows;ii++){
m_model->rowData[ii].removeAt(old_col);
}
m_model->header.removeAt(old_col);
m_model->nColumns--;
emit m_model->layoutChanged();
}
private:
int old_col;
IROOTWorksheet *m_model;
};
class RemoveColumn : public QUndoCommand
{
public:
RemoveColumn(int atCol, IROOTWorksheet* wsModel) : QUndoCommand("remove column"),m_model(wsModel),old_col(atCol)
{
for(int ii=0;ii<wsModel->nRows;ii++){
old_col_data.append(wsModel->rowData[ii].at(atCol));
wsModel->rowData[ii].removeAt(atCol);
}
old_header = wsModel->header.at(atCol);
wsModel->header.removeAt(atCol);
wsModel->nColumns--;
}
void redo()
{
}
void undo()
{
for(int ii=0;ii<m_model->nRows;ii++){
m_model->rowData[ii].insert(old_col,old_col_data.at(ii));
}
m_model->header.insert(old_col,old_header);
m_model->nColumns++;
emit m_model->layoutChanged();
}
private:
int old_col;
QStringList old_col_data;
QString old_header;
IROOTWorksheet *m_model;
};
class PasteData : public QUndoCommand
{
public:
PasteData(IROOTWorksheet* wsModel) : QUndoCommand("paste data"),m_model(wsModel)
{
old_data = wsModel->rowData;
old_nRows = wsModel->nRows;
old_nCols = wsModel->nColumns;
QString string = QApplication::clipboard()->text();
QStringList l = string.split("\n");
int currentRow = wsModel->table->currentIndex().row();
int currentCol = wsModel->table->currentIndex().column();
for(int rr=0;rr<l.count();rr++){
QStringList row = l.at(rr).split("\t");
for(int cc=0;cc<row.count();cc++){
if((currentRow+rr)<wsModel->rowData.count()){ // enough rows
if(cc+currentCol<wsModel->rowData[currentRow+rr].count()){ // enough columns
wsModel->rowData[currentRow+rr][cc+currentCol]=row.at(cc);
}
else{
wsModel->appendColumn();
wsModel->rowData[currentRow+rr][cc+currentCol]=row.at(cc);
}
}
else{
wsModel->appendRow();
if(cc+currentCol<wsModel->rowData[currentRow+rr].count()){ // enough columns
wsModel->rowData[currentRow+rr][cc+currentCol]=row.at(cc);
}
else{
wsModel->appendColumn();
wsModel->rowData[currentRow+rr][cc+currentCol]=row.at(cc);
}
}
}
}
}
void redo()
{
}
void undo()
{
m_model->rowData = old_data;
m_model->nRows = old_nRows;
m_model->nColumns = old_nCols;
emit m_model->layoutChanged();
}
private:
QList <QStringList> old_data;
int old_nRows;
int old_nCols;
IROOTWorksheet *m_model;
};
class Clear : public QUndoCommand
{
public:
Clear(IROOTWorksheet* wsModel) : QUndoCommand("clear data"),m_model(wsModel)
{
old_data = wsModel->rowData;
old_nRows = wsModel->nRows;
old_nCols = wsModel->nColumns;
wsModel->rowData.clear();
wsModel->nRows=0;
wsModel->nColumns=0;
}
void redo()
{
}
void undo()
{
m_model->rowData = old_data;
m_model->nRows = old_nRows;
m_model->nColumns = old_nCols;
emit m_model->layoutChanged();
}
private:
QList <QStringList> old_data;
int old_nRows;
int old_nCols;
IROOTWorksheet *m_model;
};
class RowNumberFiller: public QUndoCommand
{
public:
RowNumberFiller(IROOTWorksheet* wsModel, int col) : QUndoCommand("fill with row numbers"),m_model(wsModel), lastCol(col)
{
for(int ii=0;ii<wsModel->nRows;ii++){
old_data.append(wsModel->rowData.at(ii).at(col));
wsModel->rowData[ii][col]=QString("%1").arg(ii);
}
}
void redo()
{
}
void undo()
{
for(int ii=0;ii<m_model->nRows;ii++){
m_model->rowData[ii][lastCol]=old_data.at(ii);
}
emit m_model->layoutChanged();
}
private:
QStringList old_data;
int lastCol;
IROOTWorksheet *m_model;
};
#endif // IROOTWORKSHEET_H