ListItem* ListModel::itemFromId(int itemId, ListItem* parent) const { if (!parent) parent = root(); for (int i = 0, n = parent->childCount(); i < n; ++i) { ListItem* child = parent->child(i); if (child->id() == itemId) return child; } for (int i = 0, n = parent->childCount(); i < n; ++i) { ListItem* found = itemFromId(itemId, parent->child(i)); if (found) return found; } return nullptr; }
bool ListModel::_removeItem(ListItem* item) { for (int i = item->childCount(); i > 0; --i) _removeItem(item->child(i - 1)); ListItem* parent = item->parent(); int id = item->id(); int row = item->row(); SqlQuery sql; sql.prepare("UPDATE list_item SET weight = weight - 1 WHERE parent_id = :parent AND weight > :weight"); sql.bindValue(":parent", parent->id()); sql.bindValue(":weight", row); if (!sql.exec()) return false; sql.prepare("DELETE FROM list_item WHERE id = :id"); sql.bindValue(":id", id); if (!sql.exec()) return false; parent->removeChild(row); return true; }
QVariant ListModel::data(const QModelIndex& index, int role) const { if (index.isValid()) { ListItem* item = itemFromIndex(index); if (item) { // all columns switch (role) { case Qt::BackgroundRole: if (item->isHighlighted()) return App::HighlightBackgroundColor; else if (item->isProject()) return App::ProjectBackgroundColor; else if (item->isMilestone()) return App::MilestoneBackgroundColor; else return QVariant(); } // specific column switch (index.column()) { case 0: switch (role) { case Qt::DisplayRole: return item->html(); case Qt::EditRole: return item->markdown(); case Qt::CheckStateRole: if (item->isCheckable()) if (item->isCompleted()) return Qt::Checked; else if (item->isCancelled()) return Qt::PartiallyChecked; else return Qt::Unchecked; break; case Qt::DecorationRole: if (item->isProject()) return Util::findIcon("project"); if (item->isMilestone()) return Util::findIcon("milestone"); if (item->isNote()) return Util::findIcon("note"); break; #ifdef QT_DEBUG case Qt::ToolTipRole: return QString("id: %1 row: %2 parent: %3 milestone: %4 priority: %5").arg(item->id()).arg(item->row()).arg(item->parent()->id()).arg(item->isMilestone()).arg(item->priority()); #endif } break; case 1: switch (role) { case Qt::DisplayRole: return item->dueDate(); } break; } } } return QVariant(); }