| 1 | /* |
| 2 | * Copyright 2014 Mihai Niculescu <[email protected]> |
| 3 | * |
| 4 | * This file is part of EqualX Project (https://launchpad.net/equalx/) |
| 5 | * |
| 6 | * EqualX is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * EqualX is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "Library/Library.h" |
| 21 | #include "HistoryPanel/HistoryListModel.h" |
| 22 | |
| 23 | HistoryListModel::HistoryListModel(LibraryManager *library, QObject *parent) : |
| 24 | QAbstractListModel(parent), |
| 25 | mLibrary(library), |
| 26 | mChildren(0) |
| 27 | { |
| 28 | mHistorySize = mLibrary->historySize(); |
| 29 | |
| 30 | connect(mLibrary, SIGNAL(historyChanged()), this, SLOT(onHistoryChanged()) ); |
| 31 | } |
| 32 | |
| 33 | HistoryListModel::~HistoryListModel() |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | QVariant HistoryListModel::data(const QModelIndex &index, int role) const |
| 38 | { |
| 39 | if (!index.isValid()) |
| 40 | return QVariant(); |
| 41 | |
| 42 | if (index.row() >= mHistorySize || index.row() < 0) |
| 43 | return QVariant(); |
| 44 | |
| 45 | if (role == Qt::DisplayRole){ |
| 46 | return mLibrary->getHistoryRow(mHistorySize-index.row()).basicData(); |
| 47 | } |
| 48 | |
| 49 | return QVariant(); |
| 50 | } |
| 51 | |
| 52 | bool HistoryListModel::canFetchMore(const QModelIndex &/*parent*/) const |
| 53 | { |
| 54 | if(mChildren < mHistorySize){ |
| 55 | return true; |
| 56 | } |
| 57 | else { |
| 58 | return false; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void HistoryListModel::fetchMore(const QModelIndex &/*parent*/) |
| 63 | { |
| 64 | int remainder = mHistorySize - mChildren; |
| 65 | int itemsToFetch = qMin(10, remainder); |
| 66 | |
| 67 | beginInsertRows(QModelIndex(), mChildren, mChildren+itemsToFetch-1); |
| 68 | |
| 69 | mChildren += itemsToFetch; |
| 70 | |
| 71 | endInsertRows(); |
| 72 | |
| 73 | } |
| 74 | |
| 75 | int HistoryListModel::rowCount(const QModelIndex &/*parent*/) const |
| 76 | { |
| 77 | return mChildren; |
| 78 | } |
| 79 | |
| 80 | Qt::DropActions HistoryListModel::supportedDropActions() const |
| 81 | { |
| 82 | return Qt::IgnoreAction; |
| 83 | } |
| 84 | |
| 85 | Qt::DropActions HistoryListModel::supportedDragActions() const |
| 86 | { |
| 87 | return Qt::IgnoreAction; |
| 88 | } |
| 89 | |
| 90 | Qt::ItemFlags HistoryListModel::flags(const QModelIndex &index) const |
| 91 | { |
| 92 | if (!index.isValid()) |
| 93 | return Qt::NoItemFlags; |
| 94 | |
| 95 | Qt::ItemFlags itemFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable; |
| 96 | |
| 97 | return itemFlags; |
| 98 | } |
| 99 | |
| 100 | void HistoryListModel::onHistoryChanged() |
| 101 | { |
| 102 | beginResetModel(); |
| 103 | mHistorySize = mLibrary->historySize(); |
| 104 | |
| 105 | if(mHistorySize==0) |
| 106 | mChildren = 0; |
| 107 | |
| 108 | endResetModel(); |
| 109 | } |