[1f4adec] | 1 | /* |
---|
| 2 | |
---|
| 3 | Copyright 2014-2015 Adam Reichold |
---|
| 4 | |
---|
| 5 | This file is part of qpdfview. |
---|
| 6 | |
---|
| 7 | qpdfview is free software: you can redistribute it and/or modify |
---|
| 8 | it under the terms of the GNU General Public License as published by |
---|
| 9 | the Free Software Foundation, either version 2 of the License, or |
---|
| 10 | (at your option) any later version. |
---|
| 11 | |
---|
| 12 | qpdfview is distributed in the hope that it will be useful, |
---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | GNU General Public License for more details. |
---|
| 16 | |
---|
| 17 | You should have received a copy of the GNU General Public License |
---|
| 18 | along with qpdfview. If not, see <http://www.gnu.org/licenses/>. |
---|
| 19 | |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include "bookmarkmodel.h" |
---|
| 23 | |
---|
| 24 | #include <QApplication> |
---|
| 25 | |
---|
| 26 | namespace qpdfview |
---|
| 27 | { |
---|
| 28 | |
---|
| 29 | static inline bool operator<(int page, const BookmarkItem& bookmark) { return page < bookmark.page; } |
---|
| 30 | static inline bool operator<(const BookmarkItem& bookmark, int page) { return bookmark.page < page; } |
---|
| 31 | |
---|
| 32 | QHash< QString, BookmarkModel* > BookmarkModel::s_instances; |
---|
| 33 | |
---|
| 34 | BookmarkModel* BookmarkModel::fromPath(const QString& path, bool create) |
---|
| 35 | { |
---|
| 36 | BookmarkModel* model = s_instances.value(path, 0); |
---|
| 37 | |
---|
| 38 | if(create && model == 0) |
---|
| 39 | { |
---|
| 40 | model = new BookmarkModel(qApp); |
---|
| 41 | |
---|
| 42 | s_instances.insert(path, model); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | return model; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | QList< QString > BookmarkModel::knownPaths() |
---|
| 49 | { |
---|
| 50 | return s_instances.keys(); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | void BookmarkModel::forgetPath(const QString& path) |
---|
| 54 | { |
---|
| 55 | QHash< QString, BookmarkModel* >::iterator at = s_instances.find(path); |
---|
| 56 | |
---|
| 57 | if(at != s_instances.end()) |
---|
| 58 | { |
---|
| 59 | delete at.value(); |
---|
| 60 | |
---|
| 61 | s_instances.erase(at); |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | void BookmarkModel::forgetAllPaths() |
---|
| 66 | { |
---|
| 67 | qDeleteAll(s_instances); |
---|
| 68 | s_instances.clear(); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | void BookmarkModel::addBookmark(const BookmarkItem& bookmark) |
---|
| 72 | { |
---|
| 73 | QList< BookmarkItem >::iterator at = qBinaryFind(m_bookmarks.begin(), m_bookmarks.end(), bookmark.page); |
---|
| 74 | int row = at - m_bookmarks.begin(); |
---|
| 75 | |
---|
| 76 | if(at != m_bookmarks.end()) |
---|
| 77 | { |
---|
| 78 | *at = bookmark; |
---|
| 79 | |
---|
| 80 | emit dataChanged(createIndex(row, 0), createIndex(row, 1)); |
---|
| 81 | } |
---|
| 82 | else |
---|
| 83 | { |
---|
| 84 | at = qUpperBound(m_bookmarks.begin(), m_bookmarks.end(), bookmark.page); |
---|
| 85 | row = at - m_bookmarks.begin(); |
---|
| 86 | |
---|
| 87 | beginInsertRows(QModelIndex(), row, row); |
---|
| 88 | |
---|
| 89 | m_bookmarks.insert(at, bookmark); |
---|
| 90 | |
---|
| 91 | endInsertRows(); |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | void BookmarkModel::removeBookmark(const BookmarkItem& bookmark) |
---|
| 96 | { |
---|
| 97 | const QList< BookmarkItem >::iterator at = qBinaryFind(m_bookmarks.begin(), m_bookmarks.end(), bookmark.page); |
---|
| 98 | const int row = at - m_bookmarks.begin(); |
---|
| 99 | |
---|
| 100 | if(at != m_bookmarks.end()) |
---|
| 101 | { |
---|
| 102 | beginRemoveRows(QModelIndex(), row, row); |
---|
| 103 | |
---|
| 104 | m_bookmarks.erase(at); |
---|
| 105 | |
---|
| 106 | endRemoveRows(); |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | void BookmarkModel::findBookmark(BookmarkItem& bookmark) const |
---|
| 111 | { |
---|
| 112 | const QList< BookmarkItem >::const_iterator at = qBinaryFind(m_bookmarks.constBegin(), m_bookmarks.constEnd(), bookmark.page); |
---|
| 113 | |
---|
| 114 | if(at != m_bookmarks.constEnd()) |
---|
| 115 | { |
---|
| 116 | bookmark = *at; |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | Qt::ItemFlags BookmarkModel::flags(const QModelIndex&) const |
---|
| 121 | { |
---|
| 122 | return Qt::ItemIsEnabled | Qt::ItemIsSelectable; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | int BookmarkModel::columnCount(const QModelIndex&) const |
---|
| 126 | { |
---|
| 127 | return 2; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | int BookmarkModel::rowCount(const QModelIndex& parent) const |
---|
| 131 | { |
---|
| 132 | return !parent.isValid() ? m_bookmarks.count() : 0; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | QVariant BookmarkModel::data(const QModelIndex& index, int role) const |
---|
| 136 | { |
---|
| 137 | if(!index.isValid() || index.row() >= m_bookmarks.count()) |
---|
| 138 | { |
---|
| 139 | return QVariant(); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | const BookmarkItem& bookmark = m_bookmarks.at(index.row()); |
---|
| 143 | |
---|
| 144 | switch(role) |
---|
| 145 | { |
---|
| 146 | default: |
---|
| 147 | return QVariant(); |
---|
| 148 | case PageRole: |
---|
| 149 | return bookmark.page; |
---|
| 150 | case LabelRole: |
---|
| 151 | case Qt::DisplayRole: |
---|
| 152 | return index.column() == 0 ? bookmark.label : QString::number(bookmark.page); |
---|
| 153 | case CommentRole: |
---|
| 154 | case Qt::ToolTipRole: |
---|
| 155 | return bookmark.comment; |
---|
| 156 | case ModifiedRole: |
---|
| 157 | return bookmark.modified; |
---|
| 158 | case Qt::TextAlignmentRole: |
---|
| 159 | return index.column() == 0 ? Qt::AlignLeft : Qt::AlignRight; |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | BookmarkModel::BookmarkModel(QObject* parent) : QAbstractListModel(parent), |
---|
| 164 | m_bookmarks() |
---|
| 165 | { |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | } // qpdfview |
---|