File indexing completed on 2024-05-19 16:09:55

0001 /* This file is part of the KDE project
0002  *
0003  * Copyright (C) 2010 Boudewijn Rempt <boud@valdyas.org>
0004  * Copyright (C) 2002-2006 David Faure <faure@kde.org>
0005  * Copyright (C) 2005-2006 Thomas Zander <zander@kde.org>
0006  *
0007  * This library is free software; you can redistribute it and/or
0008  * modify it under the terms of the GNU Library General Public
0009  * License as published by the Free Software Foundation; either
0010  * version 2 of the License, or (at your option) any later version.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Library General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Library General Public License
0018  * along with this library; see the file COPYING.LIB.  If not, write to
0019  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020  * Boston, MA 02110-1301, USA.
0021  */
0022 
0023 // words includes
0024 #include "KWCanvasItem.h"
0025 #include "KWGui.h"
0026 #include "KWViewMode.h"
0027 #include "KWPage.h"
0028 
0029 // calligra libs includes
0030 #include <KoPointerEvent.h>
0031 #include <KoCanvasController.h>
0032 #include <KoToolProxy.h>
0033 #include <KoGridData.h>
0034 #include <KoShape.h>
0035 #include <KoZoomHandler.h>
0036 
0037 // Qt includes
0038 #include <QBrush>
0039 #include <QPainter>
0040 #include <QPainterPath>
0041 #include <QGraphicsSceneMouseEvent>
0042 #include <QGraphicsSceneWheelEvent>
0043 #include <QStyleOptionGraphicsItem>
0044 
0045 
0046 
0047 KWCanvasItem::KWCanvasItem(const QString &viewMode, KWDocument *document)
0048         : QGraphicsWidget(0),
0049         KWCanvasBase(document, this)
0050 {
0051     setAttribute(Qt::WA_OpaquePaintEvent, true);
0052     setFocusPolicy(Qt::StrongFocus);
0053     connect(document, SIGNAL(pageSetupChanged()), this, SLOT(pageSetupChanged()));
0054     m_viewConverter = new KoZoomHandler();
0055     m_viewMode = KWViewMode::create(viewMode, document);
0056 }
0057 
0058 KWCanvasItem::~KWCanvasItem()
0059 {
0060     delete m_viewConverter;
0061 }
0062 
0063 void KWCanvasItem::pageSetupChanged()
0064 {
0065     m_viewMode->pageSetupChanged();
0066     updateSize();
0067 }
0068 
0069 void KWCanvasItem::updateSize()
0070 {
0071     resourceManager()->setResource(Words::CurrentPageCount, m_document->pageCount());
0072     emit documentSize(m_viewMode->contentsSize());
0073 }
0074 
0075 void KWCanvasItem::setDocumentOffset(const QPoint &offset)
0076 {
0077     m_documentOffset = offset;
0078 }
0079 
0080 bool KWCanvasItem::snapToGrid() const
0081 {
0082     return false;
0083 }
0084 
0085 void KWCanvasItem::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
0086 {
0087     QMouseEvent me(e->type(), e->pos().toPoint(), e->button(), e->buttons(), e->modifiers());
0088     m_toolProxy->mouseMoveEvent(&me, m_viewMode->viewToDocument(e->pos() + m_documentOffset, m_viewConverter));
0089     e->setAccepted(me.isAccepted());
0090 }
0091 
0092 void KWCanvasItem::mousePressEvent(QGraphicsSceneMouseEvent *e)
0093 {
0094     QMouseEvent me(e->type(), e->pos().toPoint(), e->button(), e->buttons(), e->modifiers());
0095     m_toolProxy->mousePressEvent(&me, m_viewMode->viewToDocument(e->pos() + m_documentOffset, m_viewConverter));
0096     if (!me.isAccepted() && me.button() == Qt::RightButton) {
0097         // XXX: Port to graphicsitem!
0098         //m_view->popupContextMenu(e->globalPos(), m_toolProxy->popupActionList());
0099         me.setAccepted(true);
0100     }
0101     e->setAccepted(me.isAccepted());
0102 }
0103 
0104 void KWCanvasItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
0105 {
0106     QMouseEvent me(e->type(), e->pos().toPoint(), e->button(), e->buttons(), e->modifiers());
0107     m_toolProxy->mouseReleaseEvent(&me, m_viewMode->viewToDocument(e->pos() + m_documentOffset, m_viewConverter));
0108     e->setAccepted(me.isAccepted());
0109 }
0110 
0111 void KWCanvasItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *e)
0112 {
0113     QMouseEvent me(e->type(), e->pos().toPoint(), e->button(), e->buttons(), e->modifiers());
0114     m_toolProxy->mouseDoubleClickEvent(&me, m_viewMode->viewToDocument(e->pos() + m_documentOffset, m_viewConverter));
0115     e->setAccepted(me.isAccepted());
0116 }
0117 
0118 void KWCanvasItem::keyPressEvent(QKeyEvent *e)
0119 {
0120     m_toolProxy->keyPressEvent(e);
0121     if (! e->isAccepted()) {
0122         if (e->key() == Qt::Key_Backtab
0123                 || (e->key() == Qt::Key_Tab && (e->modifiers() & Qt::ShiftModifier)))
0124             focusNextPrevChild(false);
0125         else if (e->key() == Qt::Key_Tab)
0126             focusNextPrevChild(true);
0127     }
0128 }
0129 
0130 QVariant KWCanvasItem::inputMethodQuery(Qt::InputMethodQuery query) const
0131 {
0132     return m_toolProxy->inputMethodQuery(query, *(viewConverter()));
0133 }
0134 
0135 void KWCanvasItem::keyReleaseEvent(QKeyEvent *e)
0136 {
0137     m_toolProxy->keyReleaseEvent(e);
0138 }
0139 
0140 void KWCanvasItem::wheelEvent(QGraphicsSceneWheelEvent *event)
0141 {
0142     QWheelEvent ev(event->pos().toPoint(), event->delta(), event->buttons(), event->modifiers(), event->orientation());
0143     m_toolProxy->wheelEvent(&ev, m_viewMode->viewToDocument(event->pos() + m_documentOffset, m_viewConverter));
0144     event->setAccepted(ev.isAccepted());
0145 }
0146 
0147 void KWCanvasItem::inputMethodEvent(QInputMethodEvent *event)
0148 {
0149     m_toolProxy->inputMethodEvent(event);
0150 }
0151 
0152 void KWCanvasItem::updateInputMethodInfo()
0153 {
0154     updateMicroFocus();
0155 }
0156 
0157 void KWCanvasItem::updateCanvas(const QRectF &rc)
0158 {
0159     Q_UNUSED(rc)
0160     emit canvasUpdated();
0161 }
0162 
0163 void KWCanvasItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
0164 {
0165     painter->fillRect(option->exposedRect, QColor::fromRgb(232, 233, 234));
0166     KWCanvasBase::paint(*painter, option->exposedRect);
0167 }
0168 
0169 void KWCanvasItem::setCursor(const QCursor &cursor)
0170 {
0171     QGraphicsWidget::setCursor(cursor);
0172 }