~ubuntu-branches/ubuntu/vivid/qpdfview/vivid

« back to all changes in this revision

Viewing changes to sources/bookmarkmodel.cpp

  • Committer: Package Import Robot
  • Author(s): Benjamin Eltzner
  • Date: 2014-10-22 21:49:15 UTC
  • mfrom: (1.2.15)
  • Revision ID: package-import@ubuntu.com-20141022214915-agqeoe318lzs2s4d
Tags: 0.4.12-1
* New upstream release.
* Fixed option to zoom to selection and implemented tiled rendering
  (Closes: #739554)
* Enable support for qt5 and poppler-qt5.
* Explicit dependence on hicolor-icon-theme.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
Copyright 2014 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
#include <QHash>
 
26
 
 
27
namespace
 
28
{
 
29
 
 
30
using namespace qpdfview;
 
31
 
 
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; }
 
34
 
 
35
QHash< QString, BookmarkModel* > modelsByPath;
 
36
 
 
37
}
 
38
 
 
39
namespace qpdfview
 
40
{
 
41
 
 
42
BookmarkModel::BookmarkModel(QObject* parent) : QAbstractListModel(parent),
 
43
    m_bookmarks()
 
44
{
 
45
}
 
46
 
 
47
void BookmarkModel::addBookmark(const BookmarkItem& bookmark)
 
48
{
 
49
    QList< BookmarkItem >::iterator at = qBinaryFind(m_bookmarks.begin(), m_bookmarks.end(), bookmark.page);
 
50
    int row = at - m_bookmarks.begin();
 
51
 
 
52
    if(at != m_bookmarks.end())
 
53
    {
 
54
        *at = bookmark;
 
55
 
 
56
        emit dataChanged(createIndex(row, 0), createIndex(row, 1));
 
57
    }
 
58
    else
 
59
    {
 
60
        at = qUpperBound(m_bookmarks.begin(), m_bookmarks.end(), bookmark.page);
 
61
        row = at - m_bookmarks.begin();
 
62
 
 
63
        beginInsertRows(QModelIndex(), row, row);
 
64
 
 
65
        m_bookmarks.insert(at, bookmark);
 
66
 
 
67
        endInsertRows();
 
68
    }
 
69
}
 
70
 
 
71
void BookmarkModel::removeBookmark(const BookmarkItem& bookmark)
 
72
{
 
73
    const QList< BookmarkItem >::iterator at = qBinaryFind(m_bookmarks.begin(), m_bookmarks.end(), bookmark.page);
 
74
    const int row = at - m_bookmarks.begin();
 
75
 
 
76
    if(at != m_bookmarks.end())
 
77
    {
 
78
        beginRemoveRows(QModelIndex(), row, row);
 
79
 
 
80
        m_bookmarks.erase(at);
 
81
 
 
82
        endRemoveRows();
 
83
    }
 
84
}
 
85
 
 
86
void BookmarkModel::findBookmark(BookmarkItem& bookmark) const
 
87
{
 
88
    const QList< BookmarkItem >::const_iterator at = qBinaryFind(m_bookmarks.constBegin(), m_bookmarks.constEnd(), bookmark.page);
 
89
 
 
90
    if(at != m_bookmarks.constEnd())
 
91
    {
 
92
        bookmark = *at;
 
93
    }
 
94
}
 
95
 
 
96
BookmarkModel* BookmarkModel::fromPath(const QString& path, bool create)
 
97
{
 
98
    BookmarkModel* model = modelsByPath.value(path, 0);
 
99
 
 
100
    if(create && model == 0)
 
101
    {
 
102
        model = new BookmarkModel(qApp);
 
103
 
 
104
        modelsByPath.insert(path, model);
 
105
    }
 
106
 
 
107
    return model;
 
108
}
 
109
 
 
110
QList< QString > BookmarkModel::knownPaths()
 
111
{
 
112
    return modelsByPath.keys();
 
113
}
 
114
 
 
115
void BookmarkModel::forgetPath(const QString& path)
 
116
{
 
117
    QHash< QString, BookmarkModel* >::iterator at = modelsByPath.find(path);
 
118
 
 
119
    if(at != modelsByPath.end())
 
120
    {
 
121
        delete at.value();
 
122
 
 
123
        modelsByPath.erase(at);
 
124
    }
 
125
}
 
126
 
 
127
void BookmarkModel::forgetAllPaths()
 
128
{
 
129
    qDeleteAll(modelsByPath);
 
130
    modelsByPath.clear();
 
131
}
 
132
 
 
133
Qt::ItemFlags BookmarkModel::flags(const QModelIndex&) const
 
134
{
 
135
    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
 
136
}
 
137
 
 
138
int BookmarkModel::columnCount(const QModelIndex&) const
 
139
{
 
140
    return 2;
 
141
}
 
142
 
 
143
int BookmarkModel::rowCount(const QModelIndex& parent) const
 
144
{
 
145
    return !parent.isValid() ? m_bookmarks.count() : 0;
 
146
}
 
147
 
 
148
QVariant BookmarkModel::data(const QModelIndex& index, int role) const
 
149
{
 
150
    if(!index.isValid() || index.row() < 0 || index.row() >= m_bookmarks.count())
 
151
    {
 
152
        return QVariant();
 
153
    }
 
154
 
 
155
    const BookmarkItem& bookmark = m_bookmarks.at(index.row());
 
156
 
 
157
    switch(role)
 
158
    {
 
159
    default:
 
160
        return QVariant();
 
161
    case PageRole:
 
162
        return bookmark.page;
 
163
    case LabelRole:
 
164
    case Qt::DisplayRole:
 
165
        return index.column() == 0 ? bookmark.label : QString::number(bookmark.page);
 
166
    case CommentRole:
 
167
    case Qt::ToolTipRole:
 
168
        return bookmark.comment;
 
169
    case ModifiedRole:
 
170
        return bookmark.modified;
 
171
    case Qt::TextAlignmentRole:
 
172
        return index.column() == 0 ? Qt::AlignLeft : Qt::AlignRight;
 
173
    }
 
174
}
 
175
 
 
176
} // qpdfview