2012-05-14 18:37:22 +00:00
|
|
|
/*
|
2012-05-14 19:16:56 +00:00
|
|
|
* Copyright 2012 by Marco Martin <mart@kde.org>
|
2012-05-14 18:37:22 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2012-10-03 09:32:42 +00:00
|
|
|
* it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2,
|
|
|
|
* or (at your option) any later version.
|
2012-05-14 18:37:22 +00:00
|
|
|
*
|
|
|
|
* 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
|
2012-10-03 09:32:42 +00:00
|
|
|
* GNU General Public License for more details
|
2012-05-14 18:37:22 +00:00
|
|
|
*
|
2012-10-03 09:32:42 +00:00
|
|
|
* You should have received a copy of the GNU General Public
|
2012-05-14 18:37:22 +00:00
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "documentitem.h"
|
|
|
|
|
2012-07-31 14:28:54 +00:00
|
|
|
#include <QtDeclarative/qdeclarative.h>
|
|
|
|
|
2012-05-15 15:23:15 +00:00
|
|
|
#include <core/page.h>
|
2012-08-01 16:00:56 +00:00
|
|
|
#include <core/bookmarkmanager.h>
|
2012-05-14 18:37:22 +00:00
|
|
|
|
2012-07-31 14:28:54 +00:00
|
|
|
#include "tocmodel.h"
|
2012-05-14 18:37:22 +00:00
|
|
|
|
|
|
|
DocumentItem::DocumentItem(QObject *parent)
|
2012-05-15 10:12:56 +00:00
|
|
|
: QObject(parent),
|
|
|
|
m_searchInProgress(false)
|
2012-05-14 18:37:22 +00:00
|
|
|
{
|
2012-07-31 14:28:54 +00:00
|
|
|
qmlRegisterUncreatableType<TOCModel>("org.kde.okular", 1, 0, "TOCModel", QLatin1String("Do not create objects of this type."));
|
2012-05-14 18:37:22 +00:00
|
|
|
Okular::Settings::instance("okularproviderrc");
|
|
|
|
m_document = new Okular::Document(0);
|
2012-07-31 14:28:54 +00:00
|
|
|
m_tocModel = new TOCModel(m_document, this);
|
|
|
|
|
2012-05-15 10:12:56 +00:00
|
|
|
connect(m_document, SIGNAL(searchFinished(int,Okular::Document::SearchStatus)),
|
|
|
|
this, SLOT(searchFinished(int,Okular::Document::SearchStatus)));
|
2012-08-01 17:09:48 +00:00
|
|
|
connect(m_document->bookmarkManager(), SIGNAL(bookmarksChanged(KUrl)),
|
2012-08-01 16:00:56 +00:00
|
|
|
this, SIGNAL(bookmarksChanged()));
|
2012-05-14 18:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DocumentItem::~DocumentItem()
|
|
|
|
{
|
|
|
|
delete m_document;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentItem::setPath(const QString &path)
|
|
|
|
{
|
|
|
|
//TODO: remote urls
|
2012-05-15 11:50:16 +00:00
|
|
|
m_document->openDocument(path, KUrl(path), KMimeType::findByUrl(KUrl(path)));
|
2012-05-14 18:37:22 +00:00
|
|
|
|
2012-07-31 14:28:54 +00:00
|
|
|
m_tocModel->fill(m_document->documentSynopsis());
|
2012-07-31 19:33:22 +00:00
|
|
|
m_tocModel->setCurrentViewport(m_document->viewport());
|
2012-07-31 14:28:54 +00:00
|
|
|
|
2012-05-15 10:12:56 +00:00
|
|
|
m_matchingPages.clear();
|
|
|
|
for (uint i = 0; i < m_document->pages(); ++i) {
|
|
|
|
m_matchingPages << (int)i;
|
|
|
|
}
|
|
|
|
emit matchingPagesChanged();
|
2012-05-14 18:37:22 +00:00
|
|
|
emit pathChanged();
|
|
|
|
emit pageCountChanged();
|
|
|
|
emit openedChanged();
|
2012-05-15 11:56:08 +00:00
|
|
|
emit supportsSearchingChanged();
|
2012-05-14 18:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DocumentItem::path() const
|
|
|
|
{
|
|
|
|
return m_document->currentDocument().prettyUrl();
|
|
|
|
}
|
|
|
|
|
2012-05-15 11:50:16 +00:00
|
|
|
void DocumentItem::setCurrentPage(int page)
|
|
|
|
{
|
|
|
|
m_document->setViewportPage(page);
|
2012-07-31 19:33:22 +00:00
|
|
|
m_tocModel->setCurrentViewport(m_document->viewport());
|
2012-05-15 11:50:16 +00:00
|
|
|
emit currentPageChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
int DocumentItem::currentPage() const
|
|
|
|
{
|
|
|
|
return m_document->currentPage();
|
|
|
|
}
|
|
|
|
|
2012-05-14 18:37:22 +00:00
|
|
|
bool DocumentItem::isOpened() const
|
|
|
|
{
|
|
|
|
return m_document->isOpened();
|
|
|
|
}
|
|
|
|
|
|
|
|
int DocumentItem::pageCount() const
|
|
|
|
{
|
|
|
|
return m_document->pages();
|
|
|
|
}
|
|
|
|
|
2012-05-15 10:12:56 +00:00
|
|
|
QList<int> DocumentItem::matchingPages() const
|
|
|
|
{
|
|
|
|
return m_matchingPages;
|
|
|
|
}
|
|
|
|
|
2012-07-31 14:28:54 +00:00
|
|
|
TOCModel *DocumentItem::tableOfContents() const
|
|
|
|
{
|
|
|
|
return m_tocModel;
|
|
|
|
}
|
|
|
|
|
2012-08-01 16:00:56 +00:00
|
|
|
QList<int> DocumentItem::bookmarks() const
|
|
|
|
{
|
|
|
|
QList<int> list;
|
2012-10-02 11:25:34 +00:00
|
|
|
foreach (const KBookmark &bookmark, m_document->bookmarkManager()->bookmarks()) {
|
2012-08-01 16:00:56 +00:00
|
|
|
list << bookmark.url().fragment().split(";").first().toInt();
|
|
|
|
}
|
2012-08-01 17:09:48 +00:00
|
|
|
qSort(list);
|
2012-08-01 16:00:56 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2012-05-15 11:56:08 +00:00
|
|
|
bool DocumentItem::supportsSearching() const
|
|
|
|
{
|
|
|
|
return m_document->supportsSearching();
|
|
|
|
}
|
|
|
|
|
2012-05-15 10:12:56 +00:00
|
|
|
bool DocumentItem::isSearchInProgress() const
|
|
|
|
{
|
|
|
|
return m_searchInProgress;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentItem::searchText(const QString &text)
|
|
|
|
{
|
|
|
|
if (text.isEmpty()) {
|
|
|
|
resetSearch();
|
|
|
|
return;
|
|
|
|
}
|
2012-05-15 11:56:08 +00:00
|
|
|
m_document->cancelSearch();
|
2012-05-15 10:12:56 +00:00
|
|
|
m_document->resetSearch(PAGEVIEW_SEARCH_ID);
|
|
|
|
m_document->searchText(PAGEVIEW_SEARCH_ID, text, 1, Qt::CaseInsensitive,
|
|
|
|
Okular::Document::AllDocument, true, QColor(100,100,200,40), true);
|
|
|
|
|
|
|
|
if (!m_searchInProgress) {
|
|
|
|
m_searchInProgress = true;
|
|
|
|
emit searchInProgressChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentItem::resetSearch()
|
|
|
|
{
|
|
|
|
m_document->resetSearch(PAGEVIEW_SEARCH_ID);
|
|
|
|
m_matchingPages.clear();
|
|
|
|
for (uint i = 0; i < m_document->pages(); ++i) {
|
|
|
|
m_matchingPages << (int)i;
|
|
|
|
}
|
|
|
|
if (m_searchInProgress) {
|
|
|
|
m_searchInProgress = false;
|
|
|
|
emit searchInProgressChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
emit matchingPagesChanged();
|
|
|
|
}
|
|
|
|
|
2012-05-14 18:37:22 +00:00
|
|
|
Okular::Document *DocumentItem::document()
|
|
|
|
{
|
|
|
|
return m_document;
|
|
|
|
}
|
|
|
|
|
|
|
|
Observer *DocumentItem::observerFor(int id)
|
|
|
|
{
|
|
|
|
if (!m_observers.contains(id)) {
|
|
|
|
m_observers[id] = new Observer(this, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_observers.value(id);
|
|
|
|
}
|
|
|
|
|
2012-05-15 10:12:56 +00:00
|
|
|
void DocumentItem::searchFinished(int id, Okular::Document::SearchStatus endStatus)
|
|
|
|
{
|
|
|
|
Q_UNUSED(endStatus)
|
|
|
|
|
|
|
|
if (id != PAGEVIEW_SEARCH_ID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_matchingPages.clear();
|
|
|
|
for (uint i = 0; i < m_document->pages(); ++i) {
|
|
|
|
if (m_document->page(i)->hasHighlights(id)) {
|
|
|
|
m_matchingPages << (int)i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_searchInProgress) {
|
|
|
|
m_searchInProgress = false;
|
|
|
|
emit searchInProgressChanged();
|
|
|
|
}
|
|
|
|
emit matchingPagesChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-14 18:37:22 +00:00
|
|
|
//Observer
|
|
|
|
|
|
|
|
Observer::Observer(DocumentItem *parent, int id)
|
|
|
|
: QObject(parent),
|
|
|
|
m_observerId(id),
|
|
|
|
m_document(parent)
|
|
|
|
{
|
|
|
|
parent->document()->addObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Observer::~Observer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Observer::notifyPageChanged(int page, int flags)
|
|
|
|
{
|
|
|
|
emit pageChanged(page, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "documentitem.moc"
|