| 1 |
/*************************************************************************** |
| 2 |
stylemodel.cpp |
| 3 |
------------------- |
| 4 |
Style model |
| 5 |
------------------- |
| 6 |
Copyright 2006-2008, David Johnson |
| 7 |
Please see the header file for copyright and license information |
| 8 |
***************************************************************************/ |
| 9 |
|
| 10 |
#include <QApplication> |
| 11 |
#include <QMessageBox> |
| 12 |
#include <QPair> |
| 13 |
|
| 14 |
#include "data.h" |
| 15 |
#include "stylemodel.h" |
| 16 |
|
| 17 |
using namespace Resource; |
| 18 |
|
| 19 |
////////////////////////////////////////////////////////////////////////////// |
| 20 |
// StyleModel() |
| 21 |
// ------------ |
| 22 |
// Constructor |
| 23 |
|
| 24 |
StyleModel::StyleModel(QObject *parent, StyleList *list) |
| 25 |
: QAbstractTableModel(parent), list_(list) |
| 26 |
{} |
| 27 |
|
| 28 |
StyleModel::~StyleModel(){} |
| 29 |
|
| 30 |
////////////////////////////////////////////////////////////////////////////// |
| 31 |
// flush() |
| 32 |
// ------- |
| 33 |
// Reset the model |
| 34 |
|
| 35 |
void StyleModel::flush() |
| 36 |
{ |
| 37 |
reset(); |
| 38 |
} |
| 39 |
|
| 40 |
////////////////////////////////////////////////////////////////////////////// |
| 41 |
// data() |
| 42 |
// ------ |
| 43 |
// Return data at index |
| 44 |
|
| 45 |
QVariant StyleModel::data(const QModelIndex &index, int role) const |
| 46 |
{ |
| 47 |
if (!index.isValid()) return QVariant(); |
| 48 |
if (index.row() >= list_->count()) return QVariant(); |
| 49 |
|
| 50 |
// row is the entry in the QList |
| 51 |
const Style &style = list_->at(index.row()); |
| 52 |
|
| 53 |
// column is the style "field" |
| 54 |
if (role == Qt::DisplayRole) { |
| 55 |
switch (index.column()) { |
| 56 |
case NAME: |
| 57 |
return style.name(); |
| 58 |
case OGLOW: |
| 59 |
return QString::number(style.oglow_, 'f', 3); |
| 60 |
case OGHI: |
| 61 |
return QString::number(style.oghi_, 'f', 3); |
| 62 |
case FGLOW: |
| 63 |
return QString::number(style.fglow_, 'f', 3); |
| 64 |
case FGHI: |
| 65 |
return QString::number(style.fghi_, 'f', 3); |
| 66 |
case IBULOW: |
| 67 |
return QString::number(style.ibulow_); |
| 68 |
case IBUHI: |
| 69 |
return QString::number(style.ibuhi_); |
| 70 |
case SRMLOW: |
| 71 |
return QString::number(style.srmlow_); |
| 72 |
case SRMHI: |
| 73 |
return QString::number(style.srmhi_); |
| 74 |
default: |
| 75 |
return QVariant(); |
| 76 |
} |
| 77 |
} else if (role == Qt::EditRole) { |
| 78 |
switch (index.column()) { |
| 79 |
case NAME: |
| 80 |
return style.name(); |
| 81 |
case OGLOW: |
| 82 |
return style.oglow_; |
| 83 |
case OGHI: |
| 84 |
return style.oghi_; |
| 85 |
case FGLOW: |
| 86 |
return style.fglow_; |
| 87 |
case FGHI: |
| 88 |
return style.fghi_; |
| 89 |
case IBULOW: |
| 90 |
return style.ibulow_; |
| 91 |
case IBUHI: |
| 92 |
return style.ibuhi_; |
| 93 |
case SRMLOW: |
| 94 |
return style.srmlow_; |
| 95 |
case SRMHI: |
| 96 |
return style.srmhi_; |
| 97 |
default: |
| 98 |
return QVariant(); |
| 99 |
} |
| 100 |
} else if (role == Qt::TextAlignmentRole) { |
| 101 |
switch (index.column()) { |
| 102 |
case NAME: |
| 103 |
return Qt::AlignLeft; |
| 104 |
case OGLOW: |
| 105 |
case OGHI: |
| 106 |
case FGLOW: |
| 107 |
case FGHI: |
| 108 |
case IBULOW: |
| 109 |
case IBUHI: |
| 110 |
case SRMLOW: |
| 111 |
case SRMHI: |
| 112 |
default: |
| 113 |
return Qt::AlignRight; |
| 114 |
} |
| 115 |
} else { |
| 116 |
return QVariant(); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
////////////////////////////////////////////////////////////////////////////// |
| 121 |
// setData() |
| 122 |
// --------- |
| 123 |
// Set data at index |
| 124 |
|
| 125 |
bool StyleModel::setData(const QModelIndex &index, |
| 126 |
const QVariant &value, int role) |
| 127 |
{ |
| 128 |
static bool deleting = false; |
| 129 |
|
| 130 |
Style style; |
| 131 |
QString name; |
| 132 |
int row = index.row(); |
| 133 |
int column = index.column(); |
| 134 |
|
| 135 |
if (!index.isValid()) return false; |
| 136 |
if (role != Qt::EditRole) return false; |
| 137 |
if ((row >= list_->count()) |
| 138 |
&& (column != NAME)) return false; |
| 139 |
|
| 140 |
// grab existing style |
| 141 |
if (row < list_->count()) style = list_->value(row); |
| 142 |
|
| 143 |
switch (column) { |
| 144 |
case NAME: |
| 145 |
// editing name as several special cases |
| 146 |
name = value.toString(); |
| 147 |
|
| 148 |
// deleting name deletes style |
| 149 |
if (name.isEmpty()) { |
| 150 |
if (row >= list_->count()) return false; // empty |
| 151 |
|
| 152 |
// TODO: for some reason this gets entered recursively... |
| 153 |
if (deleting) return false; |
| 154 |
deleting = true; |
| 155 |
|
| 156 |
// remove style |
| 157 |
beginRemoveRows(index.parent(), row, row); |
| 158 |
|
| 159 |
list_->removeAt(row); |
| 160 |
emit modified(); |
| 161 |
endRemoveRows(); |
| 162 |
|
| 163 |
deleting = false; |
| 164 |
return true; |
| 165 |
} |
| 166 |
|
| 167 |
// changed name |
| 168 |
style.setName(name); |
| 169 |
break; |
| 170 |
|
| 171 |
case OGLOW: |
| 172 |
style.oglow_ = value.toDouble(); |
| 173 |
break; |
| 174 |
|
| 175 |
case OGHI: |
| 176 |
style.oghi_ = value.toDouble(); |
| 177 |
break; |
| 178 |
|
| 179 |
case FGLOW: |
| 180 |
style.fglow_ = value.toDouble(); |
| 181 |
break; |
| 182 |
|
| 183 |
case FGHI: |
| 184 |
style.fghi_ = value.toDouble(); |
| 185 |
break; |
| 186 |
|
| 187 |
case IBULOW: |
| 188 |
style.ibulow_ = value.toUInt(); |
| 189 |
break; |
| 190 |
|
| 191 |
case IBUHI: |
| 192 |
style.ibuhi_ = value.toUInt(); |
| 193 |
break; |
| 194 |
|
| 195 |
case SRMLOW: |
| 196 |
style.srmlow_ = value.toUInt(); |
| 197 |
break; |
| 198 |
|
| 199 |
case SRMHI: |
| 200 |
style.srmhi_ = value.toUInt(); |
| 201 |
break; |
| 202 |
|
| 203 |
default: |
| 204 |
return false; |
| 205 |
} |
| 206 |
|
| 207 |
list_->replace(row, style); |
| 208 |
emit modified(); |
| 209 |
|
| 210 |
// whole row may have changed |
| 211 |
emit dataChanged(index.sibling(row, NAME), |
| 212 |
index.sibling(row, SRMHI)); |
| 213 |
|
| 214 |
return true; |
| 215 |
} |
| 216 |
|
| 217 |
////////////////////////////////////////////////////////////////////////////// |
| 218 |
// insertRows() |
| 219 |
// ------------ |
| 220 |
// Insert rows into table |
| 221 |
|
| 222 |
bool StyleModel::insertRows(int row, int count, const QModelIndex&) |
| 223 |
{ |
| 224 |
if (count != 1) return false; // only insert one row at a time |
| 225 |
if ((row < 0) || (row >= list_->count())) row = list_->count(); |
| 226 |
|
| 227 |
Style style = Data::instance()->style(tr("Generic")); |
| 228 |
|
| 229 |
beginInsertRows(QModelIndex(), row, row); |
| 230 |
list_->insert(row, style); |
| 231 |
emit modified(); |
| 232 |
endInsertRows(); |
| 233 |
return true; |
| 234 |
} |
| 235 |
|
| 236 |
////////////////////////////////////////////////////////////////////////////// |
| 237 |
// removeRows() |
| 238 |
// ------------ |
| 239 |
// Remove rows from table |
| 240 |
|
| 241 |
bool StyleModel::removeRows(int row, int count, const QModelIndex&) |
| 242 |
{ |
| 243 |
if (count != 1) return false; // only remove one row at a time |
| 244 |
if ((row < 0) || (row >= list_->count())) return false; |
| 245 |
|
| 246 |
int status = QMessageBox::question(QApplication::activeWindow(), |
| 247 |
TITLE + tr(" - Delete?"), |
| 248 |
tr("Do you wish to remove this entry?"), |
| 249 |
QMessageBox::Yes | QMessageBox::Cancel); |
| 250 |
if (status == QMessageBox::Cancel) { |
| 251 |
return false; |
| 252 |
} |
| 253 |
|
| 254 |
beginRemoveRows(QModelIndex(), row, row); |
| 255 |
list_->removeAt(row); |
| 256 |
emit modified(); |
| 257 |
endRemoveRows(); |
| 258 |
return true; |
| 259 |
} |
| 260 |
|
| 261 |
////////////////////////////////////////////////////////////////////////////// |
| 262 |
// headerData() |
| 263 |
// ------------ |
| 264 |
// Return header information |
| 265 |
|
| 266 |
QVariant StyleModel::headerData(int section, Qt::Orientation orientation, |
| 267 |
int role) const |
| 268 |
{ |
| 269 |
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { |
| 270 |
switch (section) { |
| 271 |
case NAME: |
| 272 |
return tr("Style"); |
| 273 |
case OGLOW: |
| 274 |
return tr("Min. OG"); |
| 275 |
case OGHI: |
| 276 |
return tr("Max. OG"); |
| 277 |
case FGLOW: |
| 278 |
return tr("Min. FG"); |
| 279 |
case FGHI: |
| 280 |
return tr("Max. FG"); |
| 281 |
case IBULOW: |
| 282 |
return tr("Min. IBU"); |
| 283 |
case IBUHI: |
| 284 |
return tr("Max. IBU"); |
| 285 |
case SRMLOW: |
| 286 |
return tr("Min. SRM"); |
| 287 |
case SRMHI: |
| 288 |
return tr("Max. SRM"); |
| 289 |
default: |
| 290 |
return QVariant(); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
return QVariant(); |
| 295 |
} |
| 296 |
|
| 297 |
////////////////////////////////////////////////////////////////////////////// |
| 298 |
// flags() |
| 299 |
// ------ |
| 300 |
// Return flags at index |
| 301 |
|
| 302 |
Qt::ItemFlags StyleModel::flags(const QModelIndex &index) const |
| 303 |
{ |
| 304 |
if (!index.isValid()) return Qt::ItemIsEnabled; |
| 305 |
|
| 306 |
return QAbstractTableModel::flags(index) | Qt::ItemIsEditable; |
| 307 |
} |
| 308 |
|
| 309 |
////////////////////////////////////////////////////////////////////////////// |
| 310 |
// rowCount() |
| 311 |
// ---------- |
| 312 |
// Return number of rows of data |
| 313 |
|
| 314 |
int StyleModel::rowCount(const QModelIndex &) const |
| 315 |
{ |
| 316 |
return list_->count(); |
| 317 |
} |
| 318 |
|
| 319 |
////////////////////////////////////////////////////////////////////////////// |
| 320 |
// columnCount() |
| 321 |
// ------------- |
| 322 |
// Return number of columns of data |
| 323 |
|
| 324 |
int StyleModel::columnCount(const QModelIndex &) const |
| 325 |
{ |
| 326 |
return COUNT; |
| 327 |
} |
| 328 |
|
| 329 |
////////////////////////////////////////////////////////////////////////////// |
| 330 |
// sort() |
| 331 |
// ------ |
| 332 |
// Sort by column |
| 333 |
|
| 334 |
void StyleModel::sort(int column, Qt::SortOrder order) |
| 335 |
{ |
| 336 |
QList<QPair<QString,Style> > sortlist; |
| 337 |
|
| 338 |
foreach(Style style, *list_) { |
| 339 |
QString field; |
| 340 |
switch (column) { |
| 341 |
case NAME: |
| 342 |
field = style.name(); |
| 343 |
break; |
| 344 |
case OGLOW: |
| 345 |
field = QString::number(style.oglow_).rightJustified(6,'0'); |
| 346 |
break; |
| 347 |
case OGHI: |
| 348 |
field = QString::number(style.oghi_).rightJustified(6,'0'); |
| 349 |
break; |
| 350 |
case FGLOW: |
| 351 |
field = QString::number(style.fglow_).rightJustified(6,'0'); |
| 352 |
break; |
| 353 |
case FGHI: |
| 354 |
field = QString::number(style.fghi_).rightJustified(6,'0'); |
| 355 |
break; |
| 356 |
case IBULOW: |
| 357 |
field = QString::number(style.ibulow_).rightJustified(6,'0'); |
| 358 |
break; |
| 359 |
case IBUHI: |
| 360 |
field = QString::number(style.ibuhi_).rightJustified(6,'0'); |
| 361 |
break; |
| 362 |
case SRMLOW: |
| 363 |
field = QString::number(style.srmlow_).rightJustified(6,'0'); |
| 364 |
break; |
| 365 |
case SRMHI: |
| 366 |
field = QString::number(style.srmhi_).rightJustified(6,'0'); |
| 367 |
break; |
| 368 |
default: |
| 369 |
field = QString(); |
| 370 |
break; |
| 371 |
} |
| 372 |
sortlist.append(QPair<QString,Style>(field, style)); |
| 373 |
} |
| 374 |
|
| 375 |
// sort list |
| 376 |
qSort(sortlist.begin(), sortlist.end()); |
| 377 |
|
| 378 |
// create new list |
| 379 |
list_->clear(); |
| 380 |
QPair<QString,Style> pair; |
| 381 |
foreach(pair, sortlist) { |
| 382 |
if (order == Qt::AscendingOrder) |
| 383 |
list_->append(pair.second); |
| 384 |
else |
| 385 |
list_->prepend(pair.second); |
| 386 |
} |
| 387 |
|
| 388 |
emit layoutChanged(); |
| 389 |
} |