qse  0.5.4
qsegraphscansmodel.cpp
Go to the documentation of this file.
1 #include "qsegraphscansmodel.h"
2 #include "qsedataset.h"
3 #include <QBrush>
4 #include <QTableView>
5 
6 #include <stdio.h>
7 
9 (QseDataSet *dataSet, QTableView *table, QObject *parent)
10  : QAbstractTableModel(parent),
11  m_DataSet(dataSet),
12  m_TableView(table),
13  m_PrevRowCount(0),
14  m_PrevColumnCount(3)
15 {
16 // connect(dataSet, SIGNAL(dataChanged()),
17 // this, SLOT(dataChanged()));
18  connect(dataSet, SIGNAL(operationCompleted()),
19  this, SLOT(operationCompleted()));
20 }
21 
22 bool
24 {
25  QItemSelectionModel *m = m_TableView -> selectionModel();
26 
27  return (m->isRowSelected(s, QModelIndex()));
28 }
29 
30 void
32 {
33  int nrows = m_DataSet -> scanCount();
34  int ncols = 3;
35 
36  if (ncols > m_PrevColumnCount) {
37  beginInsertColumns(QModelIndex(), m_PrevColumnCount, ncols-1);
38  m_PrevColumnCount = ncols;
39  endInsertColumns();
40  } else if (ncols < m_PrevColumnCount) {
41  beginRemoveColumns(QModelIndex(), ncols, m_PrevColumnCount-1);
42  m_PrevColumnCount = ncols;
43  endRemoveColumns();
44  }
45 
46  if (nrows > m_PrevRowCount) {
47  beginInsertRows(QModelIndex(), m_PrevRowCount, nrows-1);
48 
50  QItemSelectionModel *m = m_TableView -> selectionModel();
51 
53  m -> select(index(nrows-1,0),
54  QItemSelectionModel::Current | QItemSelectionModel::Rows |
55  QItemSelectionModel::Select);
56  } else {
57  QItemSelection ss(index(m_PrevRowCount-1,0),index(m_PrevRowCount-1,0));
58  ss.select(index(nrows-1,0),index(nrows-1,0));
59  m -> select(ss,
60  QItemSelectionModel::Current | QItemSelectionModel::Rows |
61  QItemSelectionModel::Toggle);
62  }
63  }
64 
65  m_PrevRowCount = nrows;
66  endInsertRows();
67  } else if (nrows < m_PrevRowCount) {
68  beginRemoveRows(QModelIndex(), nrows, m_PrevRowCount-1);
69  m_PrevRowCount = nrows;
70  endRemoveRows();
71  }
72 
73  QModelIndex topLeft = createIndex(0,0);
74  QModelIndex botRight = createIndex(m_PrevRowCount,m_PrevColumnCount);
75 
76  emit dataChanged(topLeft, botRight);
77 
78  m_TableView -> resizeRowsToContents();
79  m_TableView -> resizeColumnsToContents();
80 }
81 
82 QModelIndex
84 {
85  return index(n,0,QModelIndex());
86 }
87 
88 int
89 QseGraphScansModel::rowCount(const QModelIndex &parent) const
90 {
91  if (parent.column() > 0) {
92  return 0;
93  }
94 
95  int res = m_DataSet -> scanCount();
96 
97 // if (res > m_PrevRowCount) {
98 // printf("QseGraphScansModel::rowCount(%d>%d)\n", res, m_PrevRowCount);
99 // } else if (res < m_PrevRowCount) {
100 // printf("QseGraphScansModel::rowCount(%d<%d)\n", res, m_PrevRowCount);
101 // }
102 
103 // m_PrevRowCount = res;
104 
105  return res;
106 }
107 
108 int
109 QseGraphScansModel::columnCount(const QModelIndex &parent) const
110 {
111  int res = 3;
112 
113 // if (res > m_PrevColumnCount) {
114 // printf("QseGraphScansModel::columnCount(%d>%d)\n", res, m_PrevColumnCount);
115 // } else if (res < m_PrevColumnCount) {
116 // printf("QseGraphScansModel::columnCount(%d<%d)\n", res, m_PrevColumnCount);
117 // }
118 
119 // m_PrevColumnCount = res;
120 
121  return res;
122 }
123 
124 static int min(int a, int b)
125 {
126  if (a<b) {
127  return a;
128  } else {
129  return b;
130  }
131 }
132 
133 static
134 const char* role_string(int role)
135 {
136  switch (role) {
137  case Qt::DisplayRole:
138  return "Qt::DisplayRole";
139  break;
140  case Qt::DecorationRole:
141  return "Qt::DecorationRole";
142  break;
143  case Qt::EditRole:
144  return "Qt::EditRole";
145  break;
146  case Qt::ToolTipRole:
147  return "Qt::ToolTipRole";
148  break;
149  case Qt::StatusTipRole:
150  return "Qt::StatusTipRole";
151  break;
152  case Qt::WhatsThisRole:
153  return "Qt::WhatsThisRole";
154  break;
155  case Qt::SizeHintRole:
156  return "Qt::SizeHintRole";
157  break;
158  case Qt::FontRole:
159  return "Qt::FontRole";
160  break;
161  case Qt::TextAlignmentRole:
162  return "Qt::TextAlignmentRole";
163  break;
164  case Qt::BackgroundRole:
165  return "Qt::BackgroundRole";
166  break;
167 // case Qt::BackgroundColorRole:
168 // return "Qt::BackgroundColorRole";
169 // break;
170  case Qt::ForegroundRole:
171  return "Qt::ForegroundRole";
172  break;
173 // case Qt::TextColorRole:
174 // return "Qt::TextColorRole";
175 // break;
176  case Qt::CheckStateRole:
177  return "Qt::CheckStateRole";
178  break;
179  case Qt::AccessibleTextRole:
180  return "Qt::AccessibleTextRole";
181  break;
182  case Qt::AccessibleDescriptionRole:
183  return "Qt::AccessibleDescriptionRole";
184  break;
185  case Qt::UserRole:
186  return "Qt::UserRole";
187  break;
188  default:
189  return "Other role";
190  }
191 }
192 
193 QVariant
194 QseGraphScansModel::data(const QModelIndex &index, int role) const
195 {
196  if (!index.isValid()) {
197  return QVariant();
198  }
199 
200  int row = index.row();
201  int col = index.column();
202 
203  QseScan *scan = m_DataSet->scan(row);
204 
205  switch (role) {
206  case Qt::DisplayRole:
207  switch (col) {
208  case 0:
209  return scan -> name();
210  break;
211  case 1:
212  return scan -> maxRowCount();
213  break;
214  case 2:
215  return scan -> columnCount();
216  break;
217  default:
218  return QVariant();
219  }
220  break;
221 
222 // case Qt::BackgroundRole:
223 // return QColor(255,255,192);
224 // break;
225 
226 // case Qt::ForegroundRole:
227 // return QBrush(Qt::red);
228 // break;
229 
230  case Qt::ToolTipRole:
231  {
232  const int maxlines = 15;
233  const int maxlength = 80;
234 
235  QString res;
236 
237  res = scan->scanCommand()+"\n";
238  res += scan->scanDateLine()+"\n";
239 
240  QStringList hdr = scan -> header();
241  int nlines = min(hdr.size(),maxlines);
242  for (int i=0; i<nlines; i++) {
243  QString l = hdr[i];
244  if (l.size()>maxlength) {
245  res += l.mid(0,maxlength) + " ...\n";
246  } else {
247  res += l + "\n";
248  }
249  }
250  if (hdr.size()>maxlines) {
251  res += "...";
252  } else {
253  int ncols = scan->columnCount();
254  res += scan->scanColumnCountLine()+"\n";
255  QString l = scan->scanColumnNamesLine();
256  if (l.size()>maxlength) {
257  res += l.mid(0,maxlength) + " ...\n";
258  } else {
259  res += l + "\n";
260  }
261  }
262  return res;
263  }
264  break;
265 
266  case Qt::SizeHintRole:
267  switch (col) {
268  case 0:
269  return QSize(200,15);
270  break;
271 
272  case 1:
273  return QSize(20,15);
274  break;
275 
276  case 2:
277  return QSize(20,15);
278  break;
279 
280  default:
281  return QVariant();
282  }
283  break;
284 
285  default:
286 // printf("QseGraphScansModel::data(%d,%d,%s)\n", row,col, role_string(role));
287  return QVariant();
288  }
289 }
290 
291 QVariant
293 (int section, Qt::Orientation orientation, int role) const
294 {
295  if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
296  switch (section) {
297  case 0:
298  return "Scan";
299  break;
300  case 1:
301  return "rows";
302  break;
303  case 2:
304  return "cols";
305  break;
306  default:
307  return QVariant();
308  }
309  }
310 
311  if (orientation == Qt::Vertical && role == Qt::DisplayRole) {
312  return section;
313  }
314 
315  return QVariant();
316 }
QseScan * scan(int n) const
Definition: qsedataset.cpp:70
QTableView * m_TableView
QseGraphScansModel(QseDataSet *dataSet, QTableView *table, QObject *parent=0)
static const char * role_string(int role)
QString scanDateLine() const
Definition: qsescan.cpp:321
int columnCount() const
Definition: qsescan.cpp:45
QString scanCommand() const
Definition: qsescan.cpp:226
QString scanColumnNamesLine() const
Definition: qsescan.cpp:335
int columnCount(const QModelIndex &parent=QModelIndex()) const
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
QModelIndex indexOfScan(int n)
QVariant data(const QModelIndex &index, int role) const
QString scanColumnCountLine() const
Definition: qsescan.cpp:328
static int min(int a, int b)
int rowCount(const QModelIndex &parent=QModelIndex()) const