File indexing completed on 2022-12-06 18:50:30

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2012 Martin Kuettler <martin.kuettler@gmail.com>
0004 */
0005 
0006 #include "worksheetimageitem.h"
0007 #include "worksheet.h"
0008 
0009 #include <QMovie>
0010 #include <QImage>
0011 #include <QGraphicsSceneContextMenuEvent>
0012 #include <QUrl>
0013 #include <QMenu>
0014 #include <QDebug>
0015 #include <QPainter>
0016 
0017 WorksheetImageItem::WorksheetImageItem(QGraphicsObject* parent)
0018     : QGraphicsObject(parent)
0019 {
0020     setFlag(QGraphicsItem::ItemIsFocusable, true);
0021     connect(this, SIGNAL(menuCreated(QMenu*,QPointF)), parent,
0022             SLOT(populateMenu(QMenu*,QPointF)), Qt::DirectConnection);
0023 }
0024 
0025 WorksheetImageItem::~WorksheetImageItem()
0026 {
0027     if (worksheet())
0028         worksheet()->removeRequestedWidth(this);
0029 }
0030 
0031 int WorksheetImageItem::type() const
0032 {
0033     return Type;
0034 }
0035 
0036 bool WorksheetImageItem::imageIsValid()
0037 {
0038     return !m_pixmap.isNull();
0039 }
0040 
0041 qreal WorksheetImageItem::setGeometry(qreal x, qreal y, qreal w, bool centered)
0042 {
0043     if (width() <= w && centered) {
0044         setPos(x + w/2 - width()/2, y);
0045     } else {
0046         setPos(x, y);
0047     }
0048     worksheet()->setRequestedWidth(this, scenePos().x() + width());
0049 
0050     return height();
0051 }
0052 
0053 qreal WorksheetImageItem::height() const
0054 {
0055     return m_size.height();
0056 }
0057 
0058 qreal WorksheetImageItem::width() const
0059 {
0060     return m_size.width();
0061 }
0062 
0063 QSizeF WorksheetImageItem::size()
0064 {
0065     return m_size;
0066 }
0067 
0068 void WorksheetImageItem::setSize(QSizeF size)
0069 {
0070     m_size = size;
0071 
0072     qreal width = scenePos().x() + size.width();
0073     worksheet()->setRequestedWidth(this, width);
0074 }
0075 
0076 QSize WorksheetImageItem::imageSize()
0077 {
0078     return m_pixmap.size();
0079 }
0080 
0081 QRectF WorksheetImageItem::boundingRect() const
0082 {
0083     return QRectF(QPointF(0, 0), m_size);
0084 }
0085 
0086 #include <QStyleOptionGraphicsItem>
0087 
0088 void WorksheetImageItem::paint(QPainter *painter,
0089                                const QStyleOptionGraphicsItem *option,
0090                                QWidget *widget)
0091 {
0092     Q_UNUSED(option);
0093     Q_UNUSED(widget);
0094     painter->drawPixmap(QRectF(QPointF(0,0), m_size), m_pixmap,
0095                         m_pixmap.rect());
0096     if (hasFocus())
0097     {
0098         painter->setPen(Qt::DashLine);
0099         painter->drawRect(0, 0, width(), height());
0100     }
0101 }
0102 
0103 void WorksheetImageItem::setEps(const QUrl& url)
0104 {
0105     const QImage img = worksheet()->renderer()->renderToImage(url, Cantor::Renderer::EPS, &m_size);
0106     m_pixmap = QPixmap::fromImage(img.convertToFormat(QImage::Format_ARGB32));
0107 }
0108 
0109 void WorksheetImageItem::setImage(QImage img)
0110 {
0111     m_pixmap = QPixmap::fromImage(img);
0112     setSize(m_pixmap.size());
0113 }
0114 
0115 void WorksheetImageItem::setImage(QImage img, QSize displaySize)
0116 {
0117     m_pixmap = QPixmap::fromImage(img);
0118     setSize(displaySize);
0119 }
0120 
0121 void WorksheetImageItem::setPixmap(QPixmap pixmap)
0122 {
0123     m_pixmap = pixmap;
0124 }
0125 
0126 QPixmap WorksheetImageItem::pixmap() const
0127 {
0128     return m_pixmap;
0129 }
0130 
0131 void WorksheetImageItem::populateMenu(QMenu* menu, QPointF pos)
0132 {
0133     emit menuCreated(menu, mapToParent(pos));
0134 }
0135 
0136 void WorksheetImageItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
0137 {
0138     QMenu *menu = worksheet()->createContextMenu();
0139     populateMenu(menu, event->pos());
0140 
0141     menu->popup(event->screenPos());
0142 }
0143 
0144 Worksheet* WorksheetImageItem::worksheet()
0145 {
0146     return qobject_cast<Worksheet*>(scene());
0147 }
0148 
0149 
0150