3
Copyright 2014 Adam Reichold
5
This file is part of qpdfview.
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.
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.
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/>.
22
#include "bookmarkmodel.h"
24
#include <QApplication>
30
using namespace qpdfview;
32
inline bool operator<(int page, const BookmarkItem& bookmark) { return page < bookmark.page; }
33
inline bool operator<(const BookmarkItem& bookmark, int page) { return bookmark.page < page; }
35
QHash< QString, BookmarkModel* > modelsByPath;
42
BookmarkModel::BookmarkModel(QObject* parent) : QAbstractListModel(parent),
47
void BookmarkModel::addBookmark(const BookmarkItem& bookmark)
49
QList< BookmarkItem >::iterator at = qBinaryFind(m_bookmarks.begin(), m_bookmarks.end(), bookmark.page);
50
int row = at - m_bookmarks.begin();
52
if(at != m_bookmarks.end())
56
emit dataChanged(createIndex(row, 0), createIndex(row, 1));
60
at = qUpperBound(m_bookmarks.begin(), m_bookmarks.end(), bookmark.page);
61
row = at - m_bookmarks.begin();
63
beginInsertRows(QModelIndex(), row, row);
65
m_bookmarks.insert(at, bookmark);
71
void BookmarkModel::removeBookmark(const BookmarkItem& bookmark)
73
const QList< BookmarkItem >::iterator at = qBinaryFind(m_bookmarks.begin(), m_bookmarks.end(), bookmark.page);
74
const int row = at - m_bookmarks.begin();
76
if(at != m_bookmarks.end())
78
beginRemoveRows(QModelIndex(), row, row);
80
m_bookmarks.erase(at);
86
void BookmarkModel::findBookmark(BookmarkItem& bookmark) const
88
const QList< BookmarkItem >::const_iterator at = qBinaryFind(m_bookmarks.constBegin(), m_bookmarks.constEnd(), bookmark.page);
90
if(at != m_bookmarks.constEnd())
96
BookmarkModel* BookmarkModel::fromPath(const QString& path, bool create)
98
BookmarkModel* model = modelsByPath.value(path, 0);
100
if(create && model == 0)
102
model = new BookmarkModel(qApp);
104
modelsByPath.insert(path, model);
110
QList< QString > BookmarkModel::knownPaths()
112
return modelsByPath.keys();
115
void BookmarkModel::forgetPath(const QString& path)
117
QHash< QString, BookmarkModel* >::iterator at = modelsByPath.find(path);
119
if(at != modelsByPath.end())
123
modelsByPath.erase(at);
127
void BookmarkModel::forgetAllPaths()
129
qDeleteAll(modelsByPath);
130
modelsByPath.clear();
133
Qt::ItemFlags BookmarkModel::flags(const QModelIndex&) const
135
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
138
int BookmarkModel::columnCount(const QModelIndex&) const
143
int BookmarkModel::rowCount(const QModelIndex& parent) const
145
return !parent.isValid() ? m_bookmarks.count() : 0;
148
QVariant BookmarkModel::data(const QModelIndex& index, int role) const
150
if(!index.isValid() || index.row() < 0 || index.row() >= m_bookmarks.count())
155
const BookmarkItem& bookmark = m_bookmarks.at(index.row());
162
return bookmark.page;
164
case Qt::DisplayRole:
165
return index.column() == 0 ? bookmark.label : QString::number(bookmark.page);
167
case Qt::ToolTipRole:
168
return bookmark.comment;
170
return bookmark.modified;
171
case Qt::TextAlignmentRole:
172
return index.column() == 0 ? Qt::AlignLeft : Qt::AlignRight;