| 1 |
/*************************************************************************** |
| 2 |
miscmodel.cpp |
| 3 |
------------------- |
| 4 |
Misc 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 "miscmodel.h" |
| 16 |
|
| 17 |
using namespace Resource; |
| 18 |
|
| 19 |
////////////////////////////////////////////////////////////////////////////// |
| 20 |
// MiscModel() |
| 21 |
// ------------ |
| 22 |
// Constructor |
| 23 |
|
| 24 |
MiscModel::MiscModel(QObject *parent, MiscList *list) |
| 25 |
: QAbstractTableModel(parent), list_(list) |
| 26 |
{} |
| 27 |
|
| 28 |
MiscModel::~MiscModel() {} |
| 29 |
|
| 30 |
////////////////////////////////////////////////////////////////////////////// |
| 31 |
// flush() |
| 32 |
// ------- |
| 33 |
// Reset the model |
| 34 |
|
| 35 |
void MiscModel::flush() |
| 36 |
{ |
| 37 |
reset(); |
| 38 |
} |
| 39 |
|
| 40 |
////////////////////////////////////////////////////////////////////////////// |
| 41 |
// data() |
| 42 |
// ------ |
| 43 |
// Return data at index |
| 44 |
|
| 45 |
QVariant MiscModel::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 Misc &misc = list_->at(index.row()); |
| 52 |
|
| 53 |
// column is the ingredient "field" |
| 54 |
if (role == Qt::DisplayRole) { |
| 55 |
switch (index.column()) { |
| 56 |
case NAME: |
| 57 |
return misc.name(); |
| 58 |
case QUANTITY: |
| 59 |
return misc.quantity().toString(3); |
| 60 |
case TYPE: |
| 61 |
return misc.type(); |
| 62 |
case NOTES: |
| 63 |
return misc.notes(); |
| 64 |
default: |
| 65 |
return QVariant(); |
| 66 |
} |
| 67 |
} else if (role == Qt::EditRole) { |
| 68 |
switch (index.column()) { |
| 69 |
case NAME: |
| 70 |
return misc.name(); |
| 71 |
case QUANTITY: { |
| 72 |
// return converted quantity |
| 73 |
Quantity quantity = misc.quantity(); |
| 74 |
quantity.convert(Data::instance()->defaultMiscUnit()); |
| 75 |
return quantity.amount(); |
| 76 |
} |
| 77 |
case TYPE: |
| 78 |
return misc.type(); |
| 79 |
case NOTES: |
| 80 |
return misc.notes(); |
| 81 |
default: |
| 82 |
return QVariant(); |
| 83 |
} |
| 84 |
} else if (role == Qt::TextAlignmentRole) { |
| 85 |
switch (index.column()) { |
| 86 |
case NAME: |
| 87 |
case TYPE: |
| 88 |
case NOTES: |
| 89 |
return Qt::AlignLeft; |
| 90 |
case QUANTITY: |
| 91 |
default: |
| 92 |
return Qt::AlignRight; |
| 93 |
} |
| 94 |
} else { |
| 95 |
return QVariant(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
////////////////////////////////////////////////////////////////////////////// |
| 100 |
// setData() |
| 101 |
// --------- |
| 102 |
// Set data at index |
| 103 |
|
| 104 |
bool MiscModel::setData(const QModelIndex &index, |
| 105 |
const QVariant &value, int role) |
| 106 |
{ |
| 107 |
static bool deleting = false; |
| 108 |
|
| 109 |
Misc misc; |
| 110 |
QString name; |
| 111 |
int row = index.row(); |
| 112 |
int column = index.column(); |
| 113 |
|
| 114 |
if (!index.isValid()) return false; |
| 115 |
if (role != Qt::EditRole) return false; |
| 116 |
if (row >= list_->count()) return false; |
| 117 |
|
| 118 |
// grab existing misc |
| 119 |
if (row < list_->count()) misc = list_->value(row); |
| 120 |
|
| 121 |
switch (column) { |
| 122 |
case NAME: |
| 123 |
// editing name as several special cases |
| 124 |
name = value.toString(); |
| 125 |
|
| 126 |
// deleting name deletes ingredient |
| 127 |
if (name.isEmpty()) { |
| 128 |
if (row >= list_->count()) return false; // empty |
| 129 |
|
| 130 |
// TODO: for some reason this gets entered recursively... |
| 131 |
if (deleting) return false; |
| 132 |
deleting = true; |
| 133 |
|
| 134 |
int status = QMessageBox::question(QApplication::activeWindow(), |
| 135 |
TITLE + tr(" - Delete?"), |
| 136 |
tr("Do you wish to remove this entry?"), |
| 137 |
QMessageBox::Yes | QMessageBox::Cancel); |
| 138 |
if (status == QMessageBox::Yes) { |
| 139 |
// remove misc |
| 140 |
beginRemoveRows(index.parent(), row, row); |
| 141 |
|
| 142 |
list_->removeAt(row); |
| 143 |
emit modified(); |
| 144 |
endRemoveRows(); |
| 145 |
|
| 146 |
deleting = false; |
| 147 |
return true; |
| 148 |
} else { |
| 149 |
// ignore |
| 150 |
deleting = false; |
| 151 |
return false; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
// no change, nothing to do |
| 156 |
if (name == list_->at(row).name()) { |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
// changed name |
| 161 |
misc.setName(name); |
| 162 |
if (Data::instance()->hasMisc(name)) { |
| 163 |
Misc newmisc = Data::instance()->misc(name); |
| 164 |
// we don't override weight |
| 165 |
misc.setType(newmisc.type()); |
| 166 |
misc.setNotes(newmisc.notes()); |
| 167 |
} |
| 168 |
break; |
| 169 |
|
| 170 |
case QUANTITY: |
| 171 |
misc.setQuantity(Quantity(value.toDouble(), |
| 172 |
Data::instance()->defaultMiscUnit())); |
| 173 |
break; |
| 174 |
|
| 175 |
case TYPE: |
| 176 |
misc.setType(value.toString()); |
| 177 |
break; |
| 178 |
|
| 179 |
case NOTES: |
| 180 |
misc.setNotes(value.toString()); |
| 181 |
break; |
| 182 |
|
| 183 |
default: |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
list_->replace(row, misc); |
| 188 |
emit modified(); |
| 189 |
|
| 190 |
// whole row may have changed |
| 191 |
emit dataChanged(index.sibling(row, NAME), |
| 192 |
index.sibling(row, NOTES)); |
| 193 |
|
| 194 |
return true; |
| 195 |
} |
| 196 |
|
| 197 |
////////////////////////////////////////////////////////////////////////////// |
| 198 |
// insertRows() |
| 199 |
// ------------ |
| 200 |
// Insert rows into table |
| 201 |
|
| 202 |
bool MiscModel::insertRows(int row, int count, const QModelIndex&) |
| 203 |
{ |
| 204 |
if (count != 1) return false; // only insert one row at a time |
| 205 |
if ((row < 0) || (row >= list_->count())) row = list_->count(); |
| 206 |
|
| 207 |
Misc misc = Data::instance()->misc(tr("Generic")); |
| 208 |
|
| 209 |
beginInsertRows(QModelIndex(), row, row); |
| 210 |
list_->insert(row, misc); |
| 211 |
emit modified(); |
| 212 |
endInsertRows(); |
| 213 |
return true; |
| 214 |
} |
| 215 |
|
| 216 |
////////////////////////////////////////////////////////////////////////////// |
| 217 |
// removeRows() |
| 218 |
// ------------ |
| 219 |
// Remove rows from table |
| 220 |
|
| 221 |
bool MiscModel::removeRows(int row, int count, const QModelIndex&) |
| 222 |
{ |
| 223 |
if (count != 1) return false; // only remove one row at a time |
| 224 |
if ((row < 0) || (row >= list_->count())) return false; |
| 225 |
|
| 226 |
int status = QMessageBox::question(QApplication::activeWindow(), |
| 227 |
TITLE + tr(" - Delete?"), |
| 228 |
tr("Do you wish to remove this entry?"), |
| 229 |
QMessageBox::Yes | QMessageBox::Cancel); |
| 230 |
if (status == QMessageBox::Cancel) { |
| 231 |
return false; |
| 232 |
} |
| 233 |
|
| 234 |
beginRemoveRows(QModelIndex(), row, row); |
| 235 |
list_->removeAt(row); |
| 236 |
emit modified(); |
| 237 |
endRemoveRows(); |
| 238 |
return true; |
| 239 |
} |
| 240 |
|
| 241 |
////////////////////////////////////////////////////////////////////////////// |
| 242 |
// headerData() |
| 243 |
// ------------ |
| 244 |
// Return header information |
| 245 |
|
| 246 |
QVariant MiscModel::headerData(int section, Qt::Orientation orientation, |
| 247 |
int role) const |
| 248 |
{ |
| 249 |
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { |
| 250 |
switch (section) { |
| 251 |
case NAME: |
| 252 |
return tr("Misc"); |
| 253 |
case QUANTITY: |
| 254 |
return tr("Quantity"); |
| 255 |
case TYPE: |
| 256 |
return tr("Type"); |
| 257 |
case NOTES: |
| 258 |
return tr("Notes"); |
| 259 |
default: |
| 260 |
return QVariant(); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
return QVariant(); |
| 265 |
} |
| 266 |
|
| 267 |
////////////////////////////////////////////////////////////////////////////// |
| 268 |
// flags() |
| 269 |
// ------- |
| 270 |
// Return flags at index |
| 271 |
|
| 272 |
Qt::ItemFlags MiscModel::flags(const QModelIndex &index) const |
| 273 |
{ |
| 274 |
if (!index.isValid()) return Qt::ItemIsEnabled; |
| 275 |
|
| 276 |
return QAbstractTableModel::flags(index) | Qt::ItemIsEditable; |
| 277 |
} |
| 278 |
|
| 279 |
////////////////////////////////////////////////////////////////////////////// |
| 280 |
// rowCount() |
| 281 |
// ---------- |
| 282 |
// Return number of rows of data |
| 283 |
|
| 284 |
int MiscModel::rowCount(const QModelIndex &) const |
| 285 |
{ |
| 286 |
return list_->count(); |
| 287 |
} |
| 288 |
|
| 289 |
////////////////////////////////////////////////////////////////////////////// |
| 290 |
// columnCount() |
| 291 |
// ------------- |
| 292 |
// Return number of columns of data |
| 293 |
|
| 294 |
int MiscModel::columnCount(const QModelIndex &) const |
| 295 |
{ |
| 296 |
return COUNT; |
| 297 |
} |
| 298 |
|
| 299 |
////////////////////////////////////////////////////////////////////////////// |
| 300 |
// sort() |
| 301 |
// ------ |
| 302 |
// Sort by column |
| 303 |
|
| 304 |
void MiscModel::sort(int column, Qt::SortOrder order) |
| 305 |
{ |
| 306 |
QList<QPair<QString,Misc> > sortlist; |
| 307 |
|
| 308 |
foreach(Misc misc, *list_) { |
| 309 |
QString field; |
| 310 |
switch (column) { |
| 311 |
case NAME: |
| 312 |
field = misc.name(); |
| 313 |
break; |
| 314 |
case QUANTITY: |
| 315 |
field = QString::number(misc.quantity().amount()).rightJustified(8,'0'); |
| 316 |
break; |
| 317 |
case TYPE: |
| 318 |
field = misc.type(); |
| 319 |
break; |
| 320 |
case NOTES: |
| 321 |
default: |
| 322 |
field = misc.notes(); |
| 323 |
break; |
| 324 |
} |
| 325 |
sortlist.append(QPair<QString,Misc>(field, misc)); |
| 326 |
} |
| 327 |
|
| 328 |
// sort list |
| 329 |
qSort(sortlist.begin(), sortlist.end()); |
| 330 |
|
| 331 |
emit layoutAboutToBeChanged(); |
| 332 |
|
| 333 |
// create new list |
| 334 |
list_->clear(); |
| 335 |
QPair<QString,Misc> pair; |
| 336 |
foreach(pair, sortlist) { |
| 337 |
if (order == Qt::AscendingOrder) |
| 338 |
list_->append(pair.second); |
| 339 |
else |
| 340 |
list_->prepend(pair.second); |
| 341 |
} |
| 342 |
|
| 343 |
emit layoutChanged(); |
| 344 |
} |