File indexing completed on 2024-05-12 16:37:09

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2002-2006 David Faure <faure@kde.org>
0003  * Copyright (C) 2005-2006 Thomas Zander <zander@kde.org>
0004  * Copyright (C) 2009 Inge Wallin <inge@lysator.liu.se>
0005  * Copyright (C) 2010 Boudewijn Rempt <boud@kogmbh.com>
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 // Own
0024 #include "KWCanvas.h"
0025 
0026 // words includes
0027 #include "KWGui.h"
0028 #include "KWView.h"
0029 #include "KWViewMode.h"
0030 #include "KWPage.h"
0031 
0032 // calligra libs includes
0033 #include <KoAnnotationLayoutManager.h>
0034 #include <KoPointerEvent.h>
0035 #include <KoCanvasController.h>
0036 #include <KoToolProxy.h>
0037 #include <KoGridData.h>
0038 
0039 // Qt includes
0040 #include <QBrush>
0041 #include <QPainter>
0042 #include <QPainterPath>
0043 
0044 KWCanvas::KWCanvas(const QString &viewMode, KWDocument *document, KWView *view, KWGui *parent)
0045         : QWidget(parent),
0046         KWCanvasBase(document, this),
0047         m_view(view)
0048 {
0049     setAttribute(Qt::WA_OpaquePaintEvent, true);
0050     setAttribute(Qt::WA_InputMethodEnabled, true);
0051     setFocusPolicy(Qt::StrongFocus);
0052     connect(document, SIGNAL(pageSetupChanged()), this, SLOT(pageSetupChanged()));
0053     m_viewConverter = m_view->viewConverter();
0054     m_viewMode = KWViewMode::create(viewMode, document);
0055 }
0056 
0057 KWCanvas::~KWCanvas()
0058 {
0059 }
0060 
0061 void KWCanvas::pageSetupChanged()
0062 {
0063     m_viewMode->pageSetupChanged();
0064     updateSize();
0065 }
0066 
0067 void KWCanvas::updateSize()
0068 {
0069     resourceManager()->setResource(Words::CurrentPageCount, m_document->pageCount());
0070     QSizeF  canvasSize = m_viewMode->contentsSize();
0071     if (showAnnotations()) {
0072         canvasSize += QSize(AnnotationAreaWidth, 0.0);
0073     }
0074     emit documentSize(canvasSize);
0075 }
0076 
0077 void KWCanvas::setDocumentOffset(const QPoint &offset)
0078 {
0079     m_documentOffset = offset;
0080 }
0081 
0082 bool KWCanvas::snapToGrid() const
0083 {
0084     return m_view->snapToGrid();
0085 }
0086 
0087 QPointF KWCanvas::viewToDocument(const QPointF &viewPoint) const
0088 {
0089     return m_viewMode->viewToDocument(viewPoint, m_viewConverter);
0090 }
0091 
0092 void KWCanvas::contextMenuEvent(QContextMenuEvent *e)
0093 {
0094     m_view->popupContextMenu(e->globalPos(), m_toolProxy->popupActionList());
0095     e->setAccepted(true);
0096 }
0097 
0098 void KWCanvas::mouseMoveEvent(QMouseEvent *e)
0099 {
0100     m_view->viewMouseMoveEvent(e);
0101     m_toolProxy->mouseMoveEvent(e, m_viewMode->viewToDocument(e->pos() + m_documentOffset, m_viewConverter));
0102 }
0103 
0104 void KWCanvas::mousePressEvent(QMouseEvent *e)
0105 {
0106     m_toolProxy->mousePressEvent(e, m_viewMode->viewToDocument(e->pos() + m_documentOffset, m_viewConverter));
0107 }
0108 
0109 void KWCanvas::mouseReleaseEvent(QMouseEvent *e)
0110 {
0111     m_toolProxy->mouseReleaseEvent(e, m_viewMode->viewToDocument(e->pos() + m_documentOffset, m_viewConverter));
0112 }
0113 
0114 void KWCanvas::mouseDoubleClickEvent(QMouseEvent *e)
0115 {
0116     m_toolProxy->mouseDoubleClickEvent(e, m_viewMode->viewToDocument(e->pos() + m_documentOffset, m_viewConverter));
0117 }
0118 
0119 bool KWCanvas::event(QEvent *e)
0120 {
0121     if(toolProxy()) {
0122         toolProxy()->processEvent(e);
0123     }
0124     return QWidget::event(e);
0125 }
0126 
0127 void KWCanvas::keyPressEvent(QKeyEvent *e)
0128 {
0129     m_toolProxy->keyPressEvent(e);
0130     if (! e->isAccepted()) {
0131         if (e->key() == Qt::Key_Backtab
0132                 || (e->key() == Qt::Key_Tab && (e->modifiers() & Qt::ShiftModifier)))
0133             focusNextPrevChild(false);
0134         else if (e->key() == Qt::Key_Tab)
0135             focusNextPrevChild(true);
0136         else if (e->key() == Qt::Key_PageUp)
0137             m_view->goToPreviousPage(e->modifiers());
0138         else if (e->key() == Qt::Key_PageDown)
0139             m_view->goToNextPage(e->modifiers());
0140          }
0141     if(e->key() == Qt::Key_Escape)
0142         m_view->exitFullscreenMode();
0143 
0144 }
0145 
0146 QVariant KWCanvas::inputMethodQuery(Qt::InputMethodQuery query) const
0147 {
0148     if (query == Qt::ImMicroFocus) {
0149         QRectF rect = (m_toolProxy->inputMethodQuery(query, *(viewConverter())).toRectF()).toRect();
0150         rect = m_viewMode->documentToView(viewConverter()->viewToDocument(rect), viewConverter());
0151         QPointF scroll(canvasController()->scrollBarValue());
0152         if (canvasWidget()->layoutDirection() == Qt::RightToLeft) {
0153             scroll.setX(-scroll.x());
0154         }
0155         rect.translate(documentOrigin() - scroll);
0156         return rect.toRect();
0157     }
0158     return m_toolProxy->inputMethodQuery(query, *(viewConverter()));
0159 }
0160 
0161 void KWCanvas::keyReleaseEvent(QKeyEvent *e)
0162 {
0163     m_toolProxy->keyReleaseEvent(e);
0164 }
0165 
0166 void KWCanvas::tabletEvent(QTabletEvent *e)
0167 {
0168     m_toolProxy->tabletEvent(e, m_viewMode->viewToDocument(e->pos() + m_documentOffset, m_viewConverter));
0169 }
0170 
0171 void KWCanvas::wheelEvent(QWheelEvent *e)
0172 {
0173     m_toolProxy->wheelEvent(e, m_viewMode->viewToDocument(e->pos() + m_documentOffset, m_viewConverter));
0174 }
0175 
0176 void KWCanvas::inputMethodEvent(QInputMethodEvent *event)
0177 {
0178     m_toolProxy->inputMethodEvent(event);
0179 }
0180 
0181 void KWCanvas::paintEvent(QPaintEvent *ev)
0182 {
0183     QPainter painter(this);
0184     painter.eraseRect(ev->rect());
0185     paint(painter, ev->rect()); // In KWCanvasBase
0186 
0187     painter.end();
0188 }
0189 
0190 void KWCanvas::setCursor(const QCursor &cursor)
0191 {
0192     QWidget::setCursor(cursor);
0193 }
0194 
0195 void KWCanvas::updateInputMethodInfo()
0196 {
0197     updateMicroFocus();
0198 }
0199