comparison data/model/ModelDataTableModel.cpp @ 454:ba7aaacb7211

* Support transforms that output regions with more than one bin (by creating more than one region) * When model displayed in the spreadsheet dialog is deleted, close the dialog in preference to crashing
author Chris Cannam
date Thu, 09 Oct 2008 13:13:33 +0000
parents 7226ebac8bd3
children b1dc68507e46
comparison
equal deleted inserted replaced
453:1f15beefcd76 454:ba7aaacb7211
31 Model *baseModel = dynamic_cast<Model *>(m); 31 Model *baseModel = dynamic_cast<Model *>(m);
32 32
33 connect(baseModel, SIGNAL(modelChanged()), this, SLOT(modelChanged())); 33 connect(baseModel, SIGNAL(modelChanged()), this, SLOT(modelChanged()));
34 connect(baseModel, SIGNAL(modelChanged(size_t, size_t)), 34 connect(baseModel, SIGNAL(modelChanged(size_t, size_t)),
35 this, SLOT(modelChanged(size_t, size_t))); 35 this, SLOT(modelChanged(size_t, size_t)));
36 connect(baseModel, SIGNAL(aboutToBeDeleted()),
37 this, SLOT(modelAboutToBeDeleted()));
36 } 38 }
37 39
38 ModelDataTableModel::~ModelDataTableModel() 40 ModelDataTableModel::~ModelDataTableModel()
39 { 41 {
40 } 42 }
41 43
42 QVariant 44 QVariant
43 ModelDataTableModel::data(const QModelIndex &index, int role) const 45 ModelDataTableModel::data(const QModelIndex &index, int role) const
44 { 46 {
47 if (!m_model) return QVariant();
45 if (role != Qt::EditRole && role != Qt::DisplayRole) return QVariant(); 48 if (role != Qt::EditRole && role != Qt::DisplayRole) return QVariant();
46 if (!index.isValid()) return QVariant(); 49 if (!index.isValid()) return QVariant();
47 return m_model->getData(getUnsorted(index.row()), index.column(), role); 50 return m_model->getData(getUnsorted(index.row()), index.column(), role);
48 } 51 }
49 52
50 bool 53 bool
51 ModelDataTableModel::setData(const QModelIndex &index, const QVariant &value, int role) 54 ModelDataTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
52 { 55 {
56 if (!m_model) return false;
53 if (!index.isValid()) return false; 57 if (!index.isValid()) return false;
54 Command *command = m_model->getSetDataCommand(getUnsorted(index.row()), 58 Command *command = m_model->getSetDataCommand(getUnsorted(index.row()),
55 index.column(), 59 index.column(),
56 value, role); 60 value, role);
57 if (command) { 61 if (command) {
63 } 67 }
64 68
65 bool 69 bool
66 ModelDataTableModel::insertRow(int row, const QModelIndex &parent) 70 ModelDataTableModel::insertRow(int row, const QModelIndex &parent)
67 { 71 {
72 if (!m_model) return false;
68 if (parent.isValid()) return false; 73 if (parent.isValid()) return false;
69 74
70 emit beginInsertRows(parent, row, row); 75 emit beginInsertRows(parent, row, row);
71 76
72 Command *command = m_model->getInsertRowCommand(getUnsorted(row)); 77 Command *command = m_model->getInsertRowCommand(getUnsorted(row));
81 } 86 }
82 87
83 bool 88 bool
84 ModelDataTableModel::removeRow(int row, const QModelIndex &parent) 89 ModelDataTableModel::removeRow(int row, const QModelIndex &parent)
85 { 90 {
91 if (!m_model) return false;
86 if (parent.isValid()) return false; 92 if (parent.isValid()) return false;
87 93
88 emit beginRemoveRows(parent, row, row); 94 emit beginRemoveRows(parent, row, row);
89 95
90 Command *command = m_model->getRemoveRowCommand(getUnsorted(row)); 96 Command *command = m_model->getRemoveRowCommand(getUnsorted(row));
107 } 113 }
108 114
109 QVariant 115 QVariant
110 ModelDataTableModel::headerData(int section, Qt::Orientation orientation, int role) const 116 ModelDataTableModel::headerData(int section, Qt::Orientation orientation, int role) const
111 { 117 {
118 if (!m_model) return QVariant();
119
112 if (orientation == Qt::Vertical && role == Qt::DisplayRole) { 120 if (orientation == Qt::Vertical && role == Qt::DisplayRole) {
113 return section + 1; 121 return section + 1;
114 } 122 }
115 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { 123 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
116 return m_model->getHeading(section); 124 return m_model->getHeading(section);
131 } 139 }
132 140
133 int 141 int
134 ModelDataTableModel::rowCount(const QModelIndex &parent) const 142 ModelDataTableModel::rowCount(const QModelIndex &parent) const
135 { 143 {
144 if (!m_model) return 0;
136 if (parent.isValid()) return 0; 145 if (parent.isValid()) return 0;
137 return m_model->getRowCount(); 146 return m_model->getRowCount();
138 } 147 }
139 148
140 int 149 int
141 ModelDataTableModel::columnCount(const QModelIndex &parent) const 150 ModelDataTableModel::columnCount(const QModelIndex &parent) const
142 { 151 {
152 if (!m_model) return 0;
143 if (parent.isValid()) return 0; 153 if (parent.isValid()) return 0;
144 return m_model->getColumnCount(); 154 return m_model->getColumnCount();
145 } 155 }
146 156
147 QModelIndex 157 QModelIndex
148 ModelDataTableModel::getModelIndexForFrame(size_t frame) const 158 ModelDataTableModel::getModelIndexForFrame(size_t frame) const
149 { 159 {
160 if (!m_model) return createIndex(0, 0);
150 int row = m_model->getRowForFrame(frame); 161 int row = m_model->getRowForFrame(frame);
151 return createIndex(getSorted(row), 0, 0); 162 return createIndex(getSorted(row), 0, 0);
152 } 163 }
153 164
154 size_t 165 size_t
155 ModelDataTableModel::getFrameForModelIndex(const QModelIndex &index) const 166 ModelDataTableModel::getFrameForModelIndex(const QModelIndex &index) const
156 { 167 {
168 if (!m_model) return 0;
157 return m_model->getFrameForRow(getUnsorted(index.row())); 169 return m_model->getFrameForRow(getUnsorted(index.row()));
158 } 170 }
159 171
160 void 172 void
161 ModelDataTableModel::sort(int column, Qt::SortOrder sortOrder) 173 ModelDataTableModel::sort(int column, Qt::SortOrder sortOrder)
189 //!!! inefficient 201 //!!! inefficient
190 clearSort(); 202 clearSort();
191 emit layoutChanged(); 203 emit layoutChanged();
192 } 204 }
193 205
206 void
207 ModelDataTableModel::modelAboutToBeDeleted()
208 {
209 m_model = 0;
210 emit modelRemoved();
211 }
212
194 int 213 int
195 ModelDataTableModel::getSorted(int row) const 214 ModelDataTableModel::getSorted(int row) const
196 { 215 {
216 if (!m_model) return row;
217
197 if (m_model->isColumnTimeValue(m_sortColumn)) { 218 if (m_model->isColumnTimeValue(m_sortColumn)) {
198 if (m_sortOrdering == Qt::AscendingOrder) { 219 if (m_sortOrdering == Qt::AscendingOrder) {
199 return row; 220 return row;
200 } else { 221 } else {
201 return rowCount() - row - 1; 222 return rowCount() - row - 1;
217 } 238 }
218 239
219 int 240 int
220 ModelDataTableModel::getUnsorted(int row) const 241 ModelDataTableModel::getUnsorted(int row) const
221 { 242 {
243 if (!m_model) return row;
244
222 if (m_model->isColumnTimeValue(m_sortColumn)) { 245 if (m_model->isColumnTimeValue(m_sortColumn)) {
223 if (m_sortOrdering == Qt::AscendingOrder) { 246 if (m_sortOrdering == Qt::AscendingOrder) {
224 return row; 247 return row;
225 } else { 248 } else {
226 return rowCount() - row - 1; 249 return rowCount() - row - 1;
244 } 267 }
245 268
246 void 269 void
247 ModelDataTableModel::resort() const 270 ModelDataTableModel::resort() const
248 { 271 {
272 if (!m_model) return;
273
249 bool numeric = (m_model->getSortType(m_sortColumn) == 274 bool numeric = (m_model->getSortType(m_sortColumn) ==
250 TabularModel::SortNumeric); 275 TabularModel::SortNumeric);
251 276
252 m_sort.clear(); 277 m_sort.clear();
253 m_rsort.clear(); 278 m_rsort.clear();
273 } 298 }
274 299
275 void 300 void
276 ModelDataTableModel::resortNumeric() const 301 ModelDataTableModel::resortNumeric() const
277 { 302 {
303 if (!m_model) return;
304
278 typedef std::multimap<double, int> MapType; 305 typedef std::multimap<double, int> MapType;
279 306
280 MapType rowMap; 307 MapType rowMap;
281 int rows = m_model->getRowCount(); 308 int rows = m_model->getRowCount();
282 309
293 } 320 }
294 321
295 void 322 void
296 ModelDataTableModel::resortAlphabetical() const 323 ModelDataTableModel::resortAlphabetical() const
297 { 324 {
325 if (!m_model) return;
326
298 typedef std::multimap<QString, int> MapType; 327 typedef std::multimap<QString, int> MapType;
299 328
300 MapType rowMap; 329 MapType rowMap;
301 int rows = m_model->getRowCount(); 330 int rows = m_model->getRowCount();
302 331