scidavis  2.3.0
About: SciDAVis is a free application for Scientific Data Analysis and Visualization (a fork off of QtiPlot).
  Fossies Dox: scidavis-2.3.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

TableModel.cpp
Go to the documentation of this file.
1 
11 
30 #include "core/column/Column.h"
31 #include "table/future_Table.h"
32 #include "table/TableModel.h"
33 #include <QString>
34 #include <QBrush>
35 #include <QIcon>
36 #include <QPixmap>
37 
39  : QAbstractItemModel(0), d_table(table), d_formula_mode(false)
40 #ifdef LEGACY_CODE_0_2_x
41  , d_read_only(false)
42 #endif
43 {
44  connect(d_table, SIGNAL(columnsAboutToBeInserted(int, QList<Column *>)),
45  this, SLOT(handleColumnsAboutToBeInserted(int, QList<Column *>)));
46  connect(d_table, SIGNAL(columnsInserted(int, int)),
47  this, SLOT(handleColumnsInserted(int, int)));
48  connect(d_table, SIGNAL(columnsAboutToBeRemoved(int, int)),
49  this, SLOT(handleColumnsAboutToBeRemoved(int, int)));
50  connect(d_table, SIGNAL(columnsRemoved(int, int)),
51  this, SLOT(handleColumnsRemoved(int, int)));
52  connect(d_table, SIGNAL(rowsAboutToBeInserted(int, int)),
53  this, SLOT(handleRowsAboutToBeInserted(int, int)));
54  connect(d_table, SIGNAL(rowsInserted(int, int)),
55  this, SLOT(handleRowsInserted(int, int)));
56  connect(d_table, SIGNAL(rowsAboutToBeRemoved(int, int)),
57  this, SLOT(handleRowsAboutToBeRemoved(int, int)));
58  connect(d_table, SIGNAL(rowsRemoved(int, int)),
59  this, SLOT(handleRowsRemoved(int, int)));
60  connect(d_table, SIGNAL(dataChanged(int, int, int, int)),
61  this, SLOT(handleDataChanged(int, int, int, int)));
62  connect(d_table, SIGNAL(headerDataChanged(Qt::Orientation, int, int)),
63  this, SIGNAL(headerDataChanged(Qt::Orientation, int, int)));
64 }
65 
67 {
68 }
69 
70 Qt::ItemFlags TableModel::flags(const QModelIndex & index ) const
71 {
72 #ifdef LEGACY_CODE_0_2_x
73  Qt::ItemFlags result = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
74  if (!isReadOnly()) result |= Qt::ItemIsEditable;
75  if (index.isValid())
76  return result;
77 #else
78  if (index.isValid())
79  return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
80 #endif
81  else
82  return Qt::ItemIsEnabled;
83 }
84 
85 QVariant TableModel::data(const QModelIndex &index, int role) const
86 {
87  if( !index.isValid() || !d_table )
88  return QVariant();
89 
90  int row = index.row();
91  int col = index.column();
92  Column * col_ptr = d_table->column(col);
93  if(!col_ptr)
94  return QVariant();
95 
96  QString postfix;
97  switch(role)
98  {
99  case Qt::ToolTipRole:
100  if(col_ptr->isMasked(row))
101  postfix = " " + tr("(masked)");
102  if(col_ptr->isInvalid(row))
103  return QVariant(tr("invalid cell (ignored in all operations)","tooltip string for invalid rows") + postfix);
104  [[fallthrough]];
105  case Qt::EditRole:
106  if(!d_formula_mode && col_ptr->isInvalid(row))
107  return QVariant();
108  [[fallthrough]];
109  case Qt::DisplayRole:
110  {
111  if(d_formula_mode)
112  return QVariant(col_ptr->formula(row));
113  if(col_ptr->isInvalid(row))
114  return QVariant(tr("-","string for invalid rows"));
115 
116  return QVariant(col_ptr->asStringColumn()->textAt(row) + postfix);
117  }
118  case Qt::ForegroundRole:
119  {
120  if(col_ptr->isInvalid(index.row()))
121  return QVariant(QBrush(QColor(0xff,0,0))); // invalid -> red letters
122  else
123  return QVariant(QBrush(QColor(0,0,0)));
124  }
125  case MaskingRole:
126  return QVariant(col_ptr->isMasked(row));
127  case FormulaRole:
128  return QVariant(col_ptr->formula(row));
129  case Qt::DecorationRole:
130  if(d_formula_mode)
131  return QIcon(QPixmap(":/equals.png"));
132  break;
133  }
134 
135  return QVariant();
136 }
137 
138 QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
139 {
140  return d_table? d_table->headerData(section, orientation, role): QVariant();
141 }
142 
143 int TableModel::rowCount(const QModelIndex &) const
144 {
145  return d_table? d_table->rowCount(): 0;
146 }
147 
148 int TableModel::columnCount(const QModelIndex &) const
149 {
150  return d_table? d_table->columnCount(): 0;
151 }
152 
153 bool TableModel::setData(const QModelIndex & index, const QVariant & value, int role)
154 {
155  if (!index.isValid() || !d_table)
156  return false;
157 
158  int row = index.row();
159 
160  switch(role)
161  {
162  case Qt::EditRole:
163  {
164  Column* col_ptr = d_table->column(index.column());
165  // remark: the validity of the cell is determined by the input filter
166  if (d_formula_mode)
167  col_ptr->setFormula(row, value.toString());
168  else
169  col_ptr->asStringColumn()->setTextAt(row, value.toString());
170  return true;
171  }
172  case MaskingRole:
173  {
174  d_table->column(index.column())->setMasked(row, value.toBool());
175  return true;
176  }
177  case FormulaRole:
178  {
179  d_table->column(index.column())->setFormula(row, value.toString());
180  return true;
181  }
182  }
183 
184  return false;
185 }
186 
187 QModelIndex TableModel::index(int row, int column, const QModelIndex &) const
188 {
189  return createIndex(row, column);
190 }
191 
192 QModelIndex TableModel::parent(const QModelIndex & child) const
193 {
194  Q_UNUSED(child)
195  return QModelIndex();
196 }
197 
198 void TableModel::handleColumnsAboutToBeInserted(int before, QList<Column *> cols)
199 {
200  int count = cols.count();
201 
202  beginInsertColumns(QModelIndex(), before, before+count-1);
203 }
204 
205 void TableModel::handleColumnsInserted(int first, int count)
206 {
207  Q_UNUSED(first)
208  Q_UNUSED(count)
209  endInsertColumns();
210 }
211 
213 {
214  beginRemoveColumns(QModelIndex(), first, first+count-1);
215 }
216 
217 void TableModel::handleColumnsRemoved(int first, int count)
218 {
219  Q_UNUSED(first)
220  Q_UNUSED(count)
221  endRemoveColumns();
222 }
223 
224 void TableModel::handleRowsAboutToBeInserted(int before, int count)
225 {
226  beginInsertRows(QModelIndex(), before, before+count-1);
227 }
228 
229 void TableModel::handleRowsInserted(int first, int count)
230 {
231  Q_UNUSED(first)
232  Q_UNUSED(count)
233  endInsertRows();
234 }
235 
236 void TableModel::handleRowsAboutToBeRemoved(int first, int count)
237 {
238  beginRemoveRows(QModelIndex(), first, first+count-1);
239 }
240 
241 void TableModel::handleRowsRemoved(int first, int count)
242 {
243  Q_UNUSED(first)
244  Q_UNUSED(count)
245  endRemoveRows();
246 }
247 
248 void TableModel::handleDataChanged(int top, int left, int bottom, int right)
249 {
250  emit dataChanged(index(top, left), index(bottom, right));
251 }
252 
254 {
255  return d_table? d_table->column(index): nullptr;
256 }
TableModel::handleColumnsAboutToBeInserted
void handleColumnsAboutToBeInserted(int, QList< Column * >)
Definition: TableModel.cpp:198
Column::asStringColumn
ColumnStringIO * asStringColumn() const
Return a wrapper column object used for String I/O.
Definition: Column.h:166
TableModel::data
QVariant data(const QModelIndex &index, int role) const
Definition: TableModel.cpp:85
TableModel::d_table
QPointer< future::Table > d_table
Definition: TableModel.h:108
TableModel::TableModel
TableModel(future::Table *table)
Constructor.
Definition: TableModel.cpp:38
Column.h
TableModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: TableModel.cpp:143
TableModel::handleColumnsRemoved
void handleColumnsRemoved(int first, int count)
Definition: TableModel.cpp:217
TableModel::d_formula_mode
bool d_formula_mode
Toggle flag for formula mode.
Definition: TableModel.h:110
future::Table
Aspect providing a spreadsheet table with column logic.
Definition: future_Table.h:86
TableModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Definition: TableModel.cpp:187
Column
Aspect that manages a column.
Definition: Column.h:59
TableModel::handleRowsAboutToBeInserted
void handleRowsAboutToBeInserted(int before, int count)
Definition: TableModel.cpp:224
TableModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: TableModel.cpp:70
TableModel.h
TableModel::handleColumnsAboutToBeRemoved
void handleColumnsAboutToBeRemoved(int first, int count)
Definition: TableModel.cpp:212
future_Table.h
TableModel::~TableModel
~TableModel()
Destructor.
Definition: TableModel.cpp:66
Column::setFormula
void setFormula(Interval< int > i, QString formula)
Set a formula string for an interval of rows.
Definition: Column.cpp:182
TableModel::handleDataChanged
void handleDataChanged(int top, int left, int bottom, int right)
Definition: TableModel.cpp:248
TableModel::handleRowsAboutToBeRemoved
void handleRowsAboutToBeRemoved(int first, int count)
Definition: TableModel.cpp:236
TableModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role) const
Definition: TableModel.cpp:138
Column::isInvalid
bool isInvalid(int row) const override
Return whether a certain row contains an invalid value
Definition: Column.cpp:598
TableModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: TableModel.cpp:148
ColumnStringIO::textAt
virtual QString textAt(int row) const
Return the content of row 'row'.
Definition: Column.cpp:644
TableModel::handleColumnsInserted
void handleColumnsInserted(int first, int count)
Definition: TableModel.cpp:205
TableModel::parent
QModelIndex parent(const QModelIndex &child) const
Definition: TableModel.cpp:192
Column::isMasked
bool isMasked(int row) const override
Return whether a certain row is masked
Definition: Column.cpp:613
TableModel::FormulaRole
@ FormulaRole
the cells formula
Definition: TableModel.h:66
Column::formula
QString formula(int row) const override
Return the formula associated with row 'row'
Definition: Column.cpp:628
TableModel::handleRowsInserted
void handleRowsInserted(int first, int count)
Definition: TableModel.cpp:229
TableModel::MaskingRole
@ MaskingRole
bool determining whether the cell is masked
Definition: TableModel.h:65
TableModel::handleRowsRemoved
void handleRowsRemoved(int first, int count)
Definition: TableModel.cpp:241
ColumnStringIO::setTextAt
virtual void setTextAt(int row, const QString &value)
Definition: Column.cpp:652
TableModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role)
Definition: TableModel.cpp:153
TableModel::column
Column * column(int index)
Definition: TableModel.cpp:253