1 /* This file is part of the KDE project
2 *
3 * Copyright (C) 2010 Boudewijn Rempt <boud@valdyas.org>
4 * Copyright (C) 2002-2006 David Faure <faure@kde.org>
5 * Copyright (C) 2005-2006 Thomas Zander <zander@kde.org>
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 // words includes
24 #include "KWCanvasItem.h"
25 #include "KWGui.h"
26 #include "KWViewMode.h"
27 #include "KWPage.h"
28
29 // calligra libs includes
30 #include <KoPointerEvent.h>
31 #include <KoCanvasController.h>
32 #include <KoToolProxy.h>
33 #include <KoGridData.h>
34 #include <KoShape.h>
35 #include <KoZoomHandler.h>
36
37 // Qt includes
38 #include <QBrush>
39 #include <QPainter>
40 #include <QPainterPath>
41 #include <QGraphicsSceneMouseEvent>
42 #include <QGraphicsSceneWheelEvent>
43 #include <QStyleOptionGraphicsItem>
44
45
46
KWCanvasItem(const QString & viewMode,KWDocument * document)47 KWCanvasItem::KWCanvasItem(const QString &viewMode, KWDocument *document)
48 : QGraphicsWidget(0),
49 KWCanvasBase(document, this)
50 {
51 setAttribute(Qt::WA_OpaquePaintEvent, true);
52 setFocusPolicy(Qt::StrongFocus);
53 connect(document, SIGNAL(pageSetupChanged()), this, SLOT(pageSetupChanged()));
54 m_viewConverter = new KoZoomHandler();
55 m_viewMode = KWViewMode::create(viewMode, document);
56 }
57
~KWCanvasItem()58 KWCanvasItem::~KWCanvasItem()
59 {
60 delete m_viewConverter;
61 }
62
pageSetupChanged()63 void KWCanvasItem::pageSetupChanged()
64 {
65 m_viewMode->pageSetupChanged();
66 updateSize();
67 }
68
updateSize()69 void KWCanvasItem::updateSize()
70 {
71 resourceManager()->setResource(Words::CurrentPageCount, m_document->pageCount());
72 emit documentSize(m_viewMode->contentsSize());
73 }
74
setDocumentOffset(const QPoint & offset)75 void KWCanvasItem::setDocumentOffset(const QPoint &offset)
76 {
77 m_documentOffset = offset;
78 }
79
snapToGrid() const80 bool KWCanvasItem::snapToGrid() const
81 {
82 return false;
83 }
84
mouseMoveEvent(QGraphicsSceneMouseEvent * e)85 void KWCanvasItem::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
86 {
87 QMouseEvent me(e->type(), e->pos().toPoint(), e->button(), e->buttons(), e->modifiers());
88 m_toolProxy->mouseMoveEvent(&me, m_viewMode->viewToDocument(e->pos() + m_documentOffset, m_viewConverter));
89 e->setAccepted(me.isAccepted());
90 }
91
mousePressEvent(QGraphicsSceneMouseEvent * e)92 void KWCanvasItem::mousePressEvent(QGraphicsSceneMouseEvent *e)
93 {
94 QMouseEvent me(e->type(), e->pos().toPoint(), e->button(), e->buttons(), e->modifiers());
95 m_toolProxy->mousePressEvent(&me, m_viewMode->viewToDocument(e->pos() + m_documentOffset, m_viewConverter));
96 if (!me.isAccepted() && me.button() == Qt::RightButton) {
97 // XXX: Port to graphicsitem!
98 //m_view->popupContextMenu(e->globalPos(), m_toolProxy->popupActionList());
99 me.setAccepted(true);
100 }
101 e->setAccepted(me.isAccepted());
102 }
103
mouseReleaseEvent(QGraphicsSceneMouseEvent * e)104 void KWCanvasItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
105 {
106 QMouseEvent me(e->type(), e->pos().toPoint(), e->button(), e->buttons(), e->modifiers());
107 m_toolProxy->mouseReleaseEvent(&me, m_viewMode->viewToDocument(e->pos() + m_documentOffset, m_viewConverter));
108 e->setAccepted(me.isAccepted());
109 }
110
mouseDoubleClickEvent(QGraphicsSceneMouseEvent * e)111 void KWCanvasItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *e)
112 {
113 QMouseEvent me(e->type(), e->pos().toPoint(), e->button(), e->buttons(), e->modifiers());
114 m_toolProxy->mouseDoubleClickEvent(&me, m_viewMode->viewToDocument(e->pos() + m_documentOffset, m_viewConverter));
115 e->setAccepted(me.isAccepted());
116 }
117
keyPressEvent(QKeyEvent * e)118 void KWCanvasItem::keyPressEvent(QKeyEvent *e)
119 {
120 m_toolProxy->keyPressEvent(e);
121 if (! e->isAccepted()) {
122 if (e->key() == Qt::Key_Backtab
123 || (e->key() == Qt::Key_Tab && (e->modifiers() & Qt::ShiftModifier)))
124 focusNextPrevChild(false);
125 else if (e->key() == Qt::Key_Tab)
126 focusNextPrevChild(true);
127 }
128 }
129
inputMethodQuery(Qt::InputMethodQuery query) const130 QVariant KWCanvasItem::inputMethodQuery(Qt::InputMethodQuery query) const
131 {
132 return m_toolProxy->inputMethodQuery(query, *(viewConverter()));
133 }
134
keyReleaseEvent(QKeyEvent * e)135 void KWCanvasItem::keyReleaseEvent(QKeyEvent *e)
136 {
137 m_toolProxy->keyReleaseEvent(e);
138 }
139
wheelEvent(QGraphicsSceneWheelEvent * event)140 void KWCanvasItem::wheelEvent(QGraphicsSceneWheelEvent *event)
141 {
142 QWheelEvent ev(event->pos().toPoint(), event->delta(), event->buttons(), event->modifiers(), event->orientation());
143 m_toolProxy->wheelEvent(&ev, m_viewMode->viewToDocument(event->pos() + m_documentOffset, m_viewConverter));
144 event->setAccepted(ev.isAccepted());
145 }
146
inputMethodEvent(QInputMethodEvent * event)147 void KWCanvasItem::inputMethodEvent(QInputMethodEvent *event)
148 {
149 m_toolProxy->inputMethodEvent(event);
150 }
151
updateInputMethodInfo()152 void KWCanvasItem::updateInputMethodInfo()
153 {
154 updateMicroFocus();
155 }
156
updateCanvas(const QRectF & rc)157 void KWCanvasItem::updateCanvas(const QRectF &rc)
158 {
159 Q_UNUSED(rc)
160 emit canvasUpdated();
161 }
162
paint(QPainter * painter,const QStyleOptionGraphicsItem * option,QWidget *)163 void KWCanvasItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
164 {
165 painter->fillRect(option->exposedRect, QColor::fromRgb(232, 233, 234));
166 KWCanvasBase::paint(*painter, option->exposedRect);
167 }
168
setCursor(const QCursor & cursor)169 void KWCanvasItem::setCursor(const QCursor &cursor)
170 {
171 QGraphicsWidget::setCursor(cursor);
172 }
173