UBGraphicsPDFItem.cpp 4.34 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
2
 * Copyright (C) 2012 Webdoc SA
Claudio Valerio's avatar
Claudio Valerio committed
3
 *
4 5 6 7 8 9 10 11 12 13
 * This file is part of Open-Sankoré.
 *
 * Open-Sankoré is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation, version 2,
 * with a specific linking exception for the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the
 * same license as the "OpenSSL" library).
 *
 * Open-Sankoré is distributed in the hope that it will be useful,
Claudio Valerio's avatar
Claudio Valerio committed
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
Claudio Valerio's avatar
Claudio Valerio committed
17
 *
18 19 20
 * You should have received a copy of the GNU Library General Public
 * License along with Open-Sankoré; if not, see
 * <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
21
 */
Claudio Valerio's avatar
Claudio Valerio committed
22

23

Claudio Valerio's avatar
Claudio Valerio committed
24 25 26 27 28 29 30 31
#include "UBGraphicsPDFItem.h"

#include <QtGui>

#include "UBGraphicsScene.h"
#include "UBGraphicsPixmapItem.h"
#include "UBGraphicsItemDelegate.h"

32
#include "core/memcheck.h"
Claudio Valerio's avatar
Claudio Valerio committed
33 34 35 36

UBGraphicsPDFItem::UBGraphicsPDFItem(PDFRenderer *renderer, int pageNumber, QGraphicsItem* parent)
    : GraphicsPDFItem(renderer, pageNumber, parent)
{
shibakaneki's avatar
shibakaneki committed
37 38
    setData(UBGraphicsItemData::ItemLayerType, UBItemLayerType::Object); //deprecated
    setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::BackgroundItem)); //Necessary to set if we want z value to be assigned correctly
39 40 41

    setDelegate(new UBGraphicsItemDelegate(this,0, true, false, false));
    Delegate()->init();
Claudio Valerio's avatar
Claudio Valerio committed
42 43 44 45 46 47 48 49 50 51
}


UBGraphicsPDFItem::~UBGraphicsPDFItem()
{
}


QVariant UBGraphicsPDFItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
52
    QVariant newValue = Delegate()->itemChange(change, value);
Claudio Valerio's avatar
Claudio Valerio committed
53 54 55
    return GraphicsPDFItem::itemChange(change, newValue);
}

root's avatar
root committed
56 57 58 59 60
void UBGraphicsPDFItem::setUuid(const QUuid &pUuid)
{
    UBItem::setUuid(pUuid);
    setData(UBGraphicsItemData::ItemUuid, QVariant(pUuid));
}
Claudio Valerio's avatar
Claudio Valerio committed
61 62 63

void UBGraphicsPDFItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
64
    if (Delegate()->mousePressEvent(event))
Claudio Valerio's avatar
Claudio Valerio committed
65 66 67 68 69 70 71 72 73 74 75 76
    {
        // NOOP
    }
    else
    {
        GraphicsPDFItem::mousePressEvent(event);
    }
}


void UBGraphicsPDFItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
77
    if (Delegate()->mouseMoveEvent(event))
Claudio Valerio's avatar
Claudio Valerio committed
78 79 80 81 82 83 84 85 86 87 88 89
    {
        // NOOP
    }
    else
    {
        GraphicsPDFItem::mouseMoveEvent(event);
    }
}


void UBGraphicsPDFItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
90
    Delegate()->mouseReleaseEvent(event);
Claudio Valerio's avatar
Claudio Valerio committed
91 92 93 94 95 96 97 98
    GraphicsPDFItem::mouseReleaseEvent(event);
}


UBItem* UBGraphicsPDFItem::deepCopy() const
{
    UBGraphicsPDFItem *copy =  new UBGraphicsPDFItem(mRenderer, mPageNumber, parentItem());

99 100 101
    copy->setUuid(this->uuid()); // this is OK for now as long as Widgets are imutable

    copyItemParameters(copy);
Claudio Valerio's avatar
Claudio Valerio committed
102 103 104 105

    return copy;
}

106 107 108 109 110 111 112 113 114 115 116 117 118
void UBGraphicsPDFItem::copyItemParameters(UBItem *copy) const
{
    UBGraphicsPDFItem *cp = dynamic_cast<UBGraphicsPDFItem*>(copy);
    if (cp)
    {
        cp->setPos(this->pos());
        cp->setTransform(this->transform());
        cp->setFlag(QGraphicsItem::ItemIsMovable, true);
        cp->setFlag(QGraphicsItem::ItemIsSelectable, true);
        cp->setData(UBGraphicsItemData::ItemLayerType, this->data(UBGraphicsItemData::ItemLayerType));
        cp->setSourceUrl(this->sourceUrl());
    }
}
Claudio Valerio's avatar
Claudio Valerio committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

void UBGraphicsPDFItem::setRenderingQuality(RenderingQuality pRenderingQuality)
{
    UBItem::setRenderingQuality(pRenderingQuality);

    if (pRenderingQuality == RenderingQualityHigh)
    {
        setCacheMode(QGraphicsItem::NoCache);
    }
    else
    {
        setCacheMode(QGraphicsItem::DeviceCoordinateCache);
    }
}


UBGraphicsScene* UBGraphicsPDFItem::scene()
{
    return qobject_cast<UBGraphicsScene*>(QGraphicsItem::scene());
}


UBGraphicsPixmapItem* UBGraphicsPDFItem::toPixmapItem() const
{   
    QPixmap pixmap(mRenderer->pageSizeF(mPageNumber).toSize());
    QPainter painter(&pixmap);
    mRenderer->render(&painter, mPageNumber);

    UBGraphicsPixmapItem *pixmapItem =  new UBGraphicsPixmapItem();
    pixmapItem->setPixmap(pixmap);

    pixmapItem->setPos(this->pos());
    pixmapItem->setTransform(this->transform());
    pixmapItem->setFlag(QGraphicsItem::ItemIsMovable, true);
    pixmapItem->setFlag(QGraphicsItem::ItemIsSelectable, true);
    pixmapItem->setData(UBGraphicsItemData::ItemLayerType, this->data(UBGraphicsItemData::ItemLayerType));

    return pixmapItem;
}
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
158 159