Exemplo n.º 1
0
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();
}
Exemplo n.º 2
0
void ListModel::removeItem(const QModelIndex& index)
{
    if (!index.isValid())
        return;

    ListItem* item = itemFromIndex(index);
    if (!item)
        return;

    bool isProject = item->isProject();

    ListItem* parent = item->parent();

    // disable removing the last child
    if (parent == root() && root()->childCount() == 1)
        return;

    int row = item->row();

    beginRemoveRows(indexFromItem(parent), row, row);
    QSqlDatabase db = QSqlDatabase::database();
    db.transaction();
    if (_removeItem(item)) {
        db.commit();
        if (isProject)
            emit projectRemoved();
    } else {
        db.rollback();
    }
    endRemoveRows();
}