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 "pageitem.h"
|
|
|
|
#include "documentitem.h"
|
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QStyleOptionGraphicsItem>
|
|
|
|
|
2012-08-01 16:14:12 +00:00
|
|
|
#include <core/bookmarkmanager.h>
|
2012-05-15 15:23:15 +00:00
|
|
|
#include <core/document.h>
|
|
|
|
#include <core/generator.h>
|
2012-08-01 16:14:12 +00:00
|
|
|
#include <core/page.h>
|
2012-05-14 18:37:22 +00:00
|
|
|
|
2012-10-02 12:19:09 +00:00
|
|
|
#include "ui/pagepainter.h"
|
2012-05-14 18:37:22 +00:00
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
#define REDRAW_TIMEOUT 250
|
|
|
|
|
|
|
|
PageItem::PageItem(QDeclarativeItem *parent)
|
|
|
|
: QDeclarativeItem(parent),
|
|
|
|
Okular::View( QString::fromLatin1( "PageView" ) ),
|
|
|
|
m_page(0),
|
|
|
|
m_pageNumber(0),
|
2012-05-25 19:28:53 +00:00
|
|
|
m_smooth(false),
|
2012-08-01 16:14:12 +00:00
|
|
|
m_intentionalDraw(true),
|
|
|
|
m_bookmarked(false)
|
2012-05-14 18:37:22 +00:00
|
|
|
{
|
|
|
|
m_observerId = PAGEVIEW_ID;
|
|
|
|
setFlag(QGraphicsItem::ItemHasNoContents, false);
|
|
|
|
|
|
|
|
m_redrawTimer = new QTimer(this);
|
|
|
|
m_redrawTimer->setSingleShot(true);
|
|
|
|
connect(m_redrawTimer, SIGNAL(timeout()), this, SLOT(delayedRedraw()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PageItem::~PageItem()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DocumentItem *PageItem::document() const
|
|
|
|
{
|
|
|
|
return m_documentItem.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageItem::setDocument(DocumentItem *doc)
|
|
|
|
{
|
|
|
|
if (doc == m_documentItem.data() || !doc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-22 13:57:32 +00:00
|
|
|
m_page = 0;
|
2012-08-01 16:14:12 +00:00
|
|
|
disconnect(doc, 0, this, 0);
|
2012-05-14 18:37:22 +00:00
|
|
|
m_documentItem = doc;
|
|
|
|
Observer *observer = m_documentItem.data()->observerFor(m_observerId);
|
|
|
|
connect(observer, SIGNAL(pageChanged(int, int)), this, SLOT(pageHasChanged(int, int)));
|
2012-08-01 17:09:48 +00:00
|
|
|
connect(doc->document()->bookmarkManager(), SIGNAL(bookmarksChanged(KUrl)),
|
2012-08-01 16:14:12 +00:00
|
|
|
this, SLOT(checkBookmarksChanged()));
|
2012-05-14 18:37:22 +00:00
|
|
|
setPageNumber(0);
|
|
|
|
emit documentChanged();
|
|
|
|
m_redrawTimer->start(REDRAW_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
int PageItem::pageNumber() const
|
|
|
|
{
|
|
|
|
return m_pageNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageItem::setPageNumber(int number)
|
|
|
|
{
|
|
|
|
if ((m_page && m_pageNumber == number) ||
|
|
|
|
!m_documentItem ||
|
|
|
|
!m_documentItem.data()->isOpened() ||
|
|
|
|
number < 0 ||
|
|
|
|
(uint)number > m_documentItem.data()->document()->pages()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pageNumber = number;
|
|
|
|
m_page = m_documentItem.data()->document()->page(number);
|
|
|
|
|
|
|
|
emit pageNumberChanged();
|
|
|
|
emit implicitWidthChanged();
|
|
|
|
emit implicitHeightChanged();
|
2012-08-01 16:14:12 +00:00
|
|
|
checkBookmarksChanged();
|
2012-05-14 18:37:22 +00:00
|
|
|
m_redrawTimer->start(REDRAW_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
int PageItem::implicitWidth() const
|
|
|
|
{
|
|
|
|
if (m_page) {
|
|
|
|
return m_page->width();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PageItem::implicitHeight() const
|
|
|
|
{
|
|
|
|
if (m_page) {
|
|
|
|
return m_page->height();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageItem::setSmooth(const bool smooth)
|
|
|
|
{
|
|
|
|
if (smooth == m_smooth) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_smooth = smooth;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PageItem::smooth() const
|
|
|
|
{
|
|
|
|
return m_smooth;
|
|
|
|
}
|
|
|
|
|
2012-08-01 16:14:12 +00:00
|
|
|
bool PageItem::isBookmarked()
|
|
|
|
{
|
|
|
|
return m_bookmarked;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageItem::setBookmarked(bool bookmarked)
|
|
|
|
{
|
|
|
|
if (!m_documentItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bookmarked == m_bookmarked) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bookmarked) {
|
|
|
|
m_documentItem.data()->document()->bookmarkManager()->addBookmark(m_pageNumber);
|
|
|
|
} else {
|
|
|
|
m_documentItem.data()->document()->bookmarkManager()->removeBookmark(m_pageNumber);
|
|
|
|
}
|
|
|
|
m_bookmarked = bookmarked;
|
|
|
|
emit bookmarkedChanged();
|
|
|
|
}
|
|
|
|
|
2012-05-14 18:37:22 +00:00
|
|
|
//Reimplemented
|
|
|
|
void PageItem::geometryChanged(const QRectF &newGeometry,
|
|
|
|
const QRectF &oldGeometry)
|
|
|
|
{
|
|
|
|
if (newGeometry.size().isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-25 19:28:53 +00:00
|
|
|
if (newGeometry.size() != oldGeometry.size()) {
|
|
|
|
m_redrawTimer->start(REDRAW_TIMEOUT);
|
|
|
|
}
|
2012-05-14 18:37:22 +00:00
|
|
|
|
|
|
|
QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
|
|
|
|
//Why aren't they automatically emuitted?
|
|
|
|
emit widthChanged();
|
|
|
|
emit heightChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
2012-05-22 14:08:03 +00:00
|
|
|
if (!m_documentItem || !m_page) {
|
2012-05-14 18:37:22 +00:00
|
|
|
QDeclarativeItem::paint(painter, option, widget);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-22 13:57:52 +00:00
|
|
|
const bool setAA = m_smooth && !(painter->renderHints() & QPainter::Antialiasing);
|
|
|
|
if (setAA) {
|
2012-05-14 18:37:22 +00:00
|
|
|
painter->save();
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing, true);
|
|
|
|
}
|
|
|
|
|
2012-05-22 14:08:03 +00:00
|
|
|
const int priority = m_observerId == PAGEVIEW_ID ? PAGEVIEW_PRELOAD_PRIO : THUMBNAILS_PRELOAD_PRIO;
|
|
|
|
|
2012-05-25 19:28:53 +00:00
|
|
|
if (m_intentionalDraw) {
|
|
|
|
QLinkedList< Okular::PixmapRequest * > requestedPixmaps;
|
|
|
|
requestedPixmaps.push_back(new Okular::PixmapRequest(m_observerId, m_pageNumber, width(), height(), priority, true));
|
|
|
|
m_documentItem.data()->document()->requestPixmaps( requestedPixmaps );
|
|
|
|
}
|
|
|
|
m_intentionalDraw = false;
|
2012-05-22 14:08:03 +00:00
|
|
|
const int flags = PagePainter::Accessibility | PagePainter::Highlights | PagePainter::Annotations;
|
2012-05-25 19:45:40 +00:00
|
|
|
PagePainter::paintPageOnPainter(painter, m_page, m_observerId, flags, width(), height(), option->exposedRect.toRect());
|
2012-05-14 18:37:22 +00:00
|
|
|
|
2012-05-22 13:57:52 +00:00
|
|
|
if (setAA) {
|
2012-05-14 18:37:22 +00:00
|
|
|
painter->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Protected slots
|
|
|
|
void PageItem::delayedRedraw()
|
|
|
|
{
|
2012-05-22 14:08:03 +00:00
|
|
|
if (m_documentItem && m_page) {
|
2012-05-25 19:28:53 +00:00
|
|
|
m_intentionalDraw = true;
|
2012-05-22 14:08:03 +00:00
|
|
|
update();
|
2012-05-14 18:37:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageItem::pageHasChanged( int page, int flags )
|
|
|
|
{
|
|
|
|
Q_UNUSED(flags)
|
2012-05-22 14:18:41 +00:00
|
|
|
if (m_pageNumber == page) {
|
2012-05-22 14:03:34 +00:00
|
|
|
m_redrawTimer->start(REDRAW_TIMEOUT);
|
|
|
|
}
|
2012-05-14 18:37:22 +00:00
|
|
|
}
|
|
|
|
|
2012-08-01 16:14:12 +00:00
|
|
|
void PageItem::checkBookmarksChanged()
|
|
|
|
{
|
|
|
|
if (!m_documentItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool newBookmarked = m_documentItem.data()->document()->bookmarkManager()->isBookmarked(m_pageNumber);
|
|
|
|
if (m_bookmarked != newBookmarked) {
|
|
|
|
m_bookmarked = newBookmarked;
|
|
|
|
emit bookmarkedChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-14 18:37:22 +00:00
|
|
|
#include "pageitem.moc"
|