1 /* This file is part of the KDE project
2 * Copyright (C) 2002-2006 David Faure <faure@kde.org>
3 * Copyright (C) 2005-2006 Thomas Zander <zander@kde.org>
4 * Copyright (C) 2009 Inge Wallin <inge@lysator.liu.se>
5 * Copyright (C) 2010 Boudewijn Rempt <boud@kogmbh.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 // Own
24 #include "KWCanvas.h"
25
26 // words includes
27 #include "KWGui.h"
28 #include "KWView.h"
29 #include "KWViewMode.h"
30 #include "KWPage.h"
31
32 // calligra libs includes
33 #include <KoAnnotationLayoutManager.h>
34 #include <KoPointerEvent.h>
35 #include <KoCanvasController.h>
36 #include <KoToolProxy.h>
37 #include <KoGridData.h>
38
39 // Qt includes
40 #include <QBrush>
41 #include <QPainter>
42 #include <QPainterPath>
43
KWCanvas(const QString & viewMode,KWDocument * document,KWView * view,KWGui * parent)44 KWCanvas::KWCanvas(const QString &viewMode, KWDocument *document, KWView *view, KWGui *parent)
45 : QWidget(parent),
46 KWCanvasBase(document, this),
47 m_view(view)
48 {
49 setAttribute(Qt::WA_OpaquePaintEvent, true);
50 setAttribute(Qt::WA_InputMethodEnabled, true);
51 setFocusPolicy(Qt::StrongFocus);
52 connect(document, SIGNAL(pageSetupChanged()), this, SLOT(pageSetupChanged()));
53 m_viewConverter = m_view->viewConverter();
54 m_viewMode = KWViewMode::create(viewMode, document);
55 }
56
~KWCanvas()57 KWCanvas::~KWCanvas()
58 {
59 }
60
pageSetupChanged()61 void KWCanvas::pageSetupChanged()
62 {
63 m_viewMode->pageSetupChanged();
64 updateSize();
65 }
66
updateSize()67 void KWCanvas::updateSize()
68 {
69 resourceManager()->setResource(Words::CurrentPageCount, m_document->pageCount());
70 QSizeF canvasSize = m_viewMode->contentsSize();
71 if (showAnnotations()) {
72 canvasSize += QSize(AnnotationAreaWidth, 0.0);
73 }
74 emit documentSize(canvasSize);
75 }
76
setDocumentOffset(const QPoint & offset)77 void KWCanvas::setDocumentOffset(const QPoint &offset)
78 {
79 m_documentOffset = offset;
80 }
81
snapToGrid() const82 bool KWCanvas::snapToGrid() const
83 {
84 return m_view->snapToGrid();
85 }
86
viewToDocument(const QPointF & viewPoint) const87 QPointF KWCanvas::viewToDocument(const QPointF &viewPoint) const
88 {
89 return m_viewMode->viewToDocument(viewPoint, m_viewConverter);
90 }
91
contextMenuEvent(QContextMenuEvent * e)92 void KWCanvas::contextMenuEvent(QContextMenuEvent *e)
93 {
94 m_view->popupContextMenu(e->globalPos(), m_toolProxy->popupActionList());
95 e->setAccepted(true);
96 }
97
mouseMoveEvent(QMouseEvent * e)98 void KWCanvas::mouseMoveEvent(QMouseEvent *e)
99 {
100 m_view->viewMouseMoveEvent(e);
101 m_toolProxy->mouseMoveEvent(e, m_viewMode->viewToDocument(e->pos() + m_documentOffset, m_viewConverter));
102 }
103
mousePressEvent(QMouseEvent * e)104 void KWCanvas::mousePressEvent(QMouseEvent *e)
105 {
106 m_toolProxy->mousePressEvent(e, m_viewMode->viewToDocument(e->pos() + m_documentOffset, m_viewConverter));
107 }
108
mouseReleaseEvent(QMouseEvent * e)109 void KWCanvas::mouseReleaseEvent(QMouseEvent *e)
110 {
111 m_toolProxy->mouseReleaseEvent(e, m_viewMode->viewToDocument(e->pos() + m_documentOffset, m_viewConverter));
112 }
113
mouseDoubleClickEvent(QMouseEvent * e)114 void KWCanvas::mouseDoubleClickEvent(QMouseEvent *e)
115 {
116 m_toolProxy->mouseDoubleClickEvent(e, m_viewMode->viewToDocument(e->pos() + m_documentOffset, m_viewConverter));
117 }
118
event(QEvent * e)119 bool KWCanvas::event(QEvent *e)
120 {
121 if(toolProxy()) {
122 toolProxy()->processEvent(e);
123 }
124 return QWidget::event(e);
125 }
126
keyPressEvent(QKeyEvent * e)127 void KWCanvas::keyPressEvent(QKeyEvent *e)
128 {
129 m_toolProxy->keyPressEvent(e);
130 if (! e->isAccepted()) {
131 if (e->key() == Qt::Key_Backtab
132 || (e->key() == Qt::Key_Tab && (e->modifiers() & Qt::ShiftModifier)))
133 focusNextPrevChild(false);
134 else if (e->key() == Qt::Key_Tab)
135 focusNextPrevChild(true);
136 else if (e->key() == Qt::Key_PageUp)
137 m_view->goToPreviousPage(e->modifiers());
138 else if (e->key() == Qt::Key_PageDown)
139 m_view->goToNextPage(e->modifiers());
140 }
141 if(e->key() == Qt::Key_Escape)
142 m_view->exitFullscreenMode();
143
144 }
145
inputMethodQuery(Qt::InputMethodQuery query) const146 QVariant KWCanvas::inputMethodQuery(Qt::InputMethodQuery query) const
147 {
148 if (query == Qt::ImMicroFocus) {
149 QRectF rect = (m_toolProxy->inputMethodQuery(query, *(viewConverter())).toRectF()).toRect();
150 rect = m_viewMode->documentToView(viewConverter()->viewToDocument(rect), viewConverter());
151 QPointF scroll(canvasController()->scrollBarValue());
152 if (canvasWidget()->layoutDirection() == Qt::RightToLeft) {
153 scroll.setX(-scroll.x());
154 }
155 rect.translate(documentOrigin() - scroll);
156 return rect.toRect();
157 }
158 return m_toolProxy->inputMethodQuery(query, *(viewConverter()));
159 }
160
keyReleaseEvent(QKeyEvent * e)161 void KWCanvas::keyReleaseEvent(QKeyEvent *e)
162 {
163 m_toolProxy->keyReleaseEvent(e);
164 }
165
tabletEvent(QTabletEvent * e)166 void KWCanvas::tabletEvent(QTabletEvent *e)
167 {
168 m_toolProxy->tabletEvent(e, m_viewMode->viewToDocument(e->pos() + m_documentOffset, m_viewConverter));
169 }
170
wheelEvent(QWheelEvent * e)171 void KWCanvas::wheelEvent(QWheelEvent *e)
172 {
173 m_toolProxy->wheelEvent(e, m_viewMode->viewToDocument(e->pos() + m_documentOffset, m_viewConverter));
174 }
175
inputMethodEvent(QInputMethodEvent * event)176 void KWCanvas::inputMethodEvent(QInputMethodEvent *event)
177 {
178 m_toolProxy->inputMethodEvent(event);
179 }
180
paintEvent(QPaintEvent * ev)181 void KWCanvas::paintEvent(QPaintEvent *ev)
182 {
183 QPainter painter(this);
184 painter.eraseRect(ev->rect());
185 paint(painter, ev->rect()); // In KWCanvasBase
186
187 painter.end();
188 }
189
setCursor(const QCursor & cursor)190 void KWCanvas::setCursor(const QCursor &cursor)
191 {
192 QWidget::setCursor(cursor);
193 }
194
updateInputMethodInfo()195 void KWCanvas::updateInputMethodInfo()
196 {
197 updateMicroFocus();
198 }
199
200