/* * Copyright (C) 2012 Felix Geyer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 or (at your option) * version 3 of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "EntryHistoryModel.h" #include "core/Entry.h" EntryHistoryModel::EntryHistoryModel(QObject* parent) : QAbstractTableModel(parent) { } Entry* EntryHistoryModel::entryFromIndex(const QModelIndex& index) const { Q_ASSERT(index.isValid() && index.row() < m_historyEntries.size()); return m_historyEntries.at(index.row()); } int EntryHistoryModel::columnCount(const QModelIndex& parent) const { Q_UNUSED(parent); return 4; } int EntryHistoryModel::rowCount(const QModelIndex& parent) const { if (!parent.isValid()) { return m_historyEntries.count(); } else { return 0; } } QVariant EntryHistoryModel::data(const QModelIndex& index, int role) const { if (!index.isValid()) { return QVariant(); } int row = index.row(); int column = index.column(); Entry* entry = m_historyEntries.at(row); TimeInfo timeInfo = entry->timeInfo(); QDateTime lastModificationLocalTime = timeInfo.lastModificationTime().toLocalTime(); if (role == Qt::DisplayRole) { switch (column) { case 0: return lastModificationLocalTime.toString(Qt::SystemLocaleShortDate); case 1: return entry->title(); case 2: return entry->username(); case 3: return entry->url(); } } return QVariant(); } QVariant EntryHistoryModel::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { switch (section) { case 0: return tr("Last modified"); case 1: return tr("Title"); case 2: return tr("Username"); case 3: return tr("URL"); } } return QVariant(); } void EntryHistoryModel::setEntries(const QList& entries) { beginResetModel(); m_historyEntries = entries; endResetModel(); } void EntryHistoryModel::clear() { beginResetModel(); m_historyEntries.clear(); endResetModel(); }