1 /* This file is part of the KDE project
2  * Copyright (C) 2008 Fredy Yanardi <fyanardi@gmail.com>
3  * Copyright (C) 2009 Thorsten Zachmann <zachmann@kde.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #include "KPrViewModeNotes.h"
22 
23 #include <QEvent>
24 #include <QPainter>
25 #include <QGraphicsWidget>
26 
27 #include <KoDocumentResourceManager.h>
28 #include <KoRuler.h>
29 #include <KoSelection.h>
30 #include <KoShapeLayer.h>
31 #include <KoShapeManager.h>
32 #include <KoText.h>
33 #include <KoToolManager.h>
34 #include <KoToolProxy.h>
35 #include <KoInteractionTool.h>
36 
37 #include <KoPACanvasBase.h>
38 #include <KoPADocument.h>
39 #include <KoPAPageBase.h>
40 #include <KoPAView.h>
41 
42 #include "StageDebug.h"
43 #include "KPrNotes.h"
44 #include "KPrPage.h"
45 
KPrViewModeNotes(KoPAViewBase * view,KoPACanvasBase * canvas)46 KPrViewModeNotes::KPrViewModeNotes(KoPAViewBase *view, KoPACanvasBase *canvas)
47     : KoPAViewMode( view, canvas )
48 {
49     setName(i18n("Notes"));
50 }
51 
~KPrViewModeNotes()52 KPrViewModeNotes::~KPrViewModeNotes()
53 {
54 }
55 
paint(KoPACanvasBase * canvas,QPainter & painter,const QRectF & paintRect)56 void KPrViewModeNotes::paint(KoPACanvasBase *canvas, QPainter &painter, const QRectF &paintRect)
57 {
58 #ifdef NDEBUG
59     Q_UNUSED(canvas);
60 #endif
61     Q_ASSERT(m_canvas == canvas);
62 
63     painter.translate(m_canvas->documentOrigin());
64     painter.translate(-m_canvas->documentOffset());
65     painter.setRenderHint(QPainter::Antialiasing);
66     QRectF clipRect = paintRect.translated(m_canvas->documentOffset() - m_canvas->documentOrigin());
67     painter.setClipRect(clipRect);
68 
69     KoViewConverter *converter = m_view->viewConverter(m_canvas);
70     const KoPageLayout &layout = activePageLayout();
71     painter.fillRect(converter->documentToView(QRectF(0, 0, layout.width, layout.height)), Qt::white);
72     m_canvas->shapeManager()->paint(painter, *converter, false);
73     m_toolProxy->paint(painter, *converter);
74 }
75 
tabletEvent(QTabletEvent * event,const QPointF & point)76 void KPrViewModeNotes::tabletEvent(QTabletEvent *event, const QPointF &point)
77 {
78     m_toolProxy->tabletEvent(event, point);
79 }
80 
mousePressEvent(QMouseEvent * event,const QPointF & point)81 void KPrViewModeNotes::mousePressEvent(QMouseEvent *event, const QPointF &point)
82 {
83     m_toolProxy->mousePressEvent(event, point);
84 }
85 
mouseDoubleClickEvent(QMouseEvent * event,const QPointF & point)86 void KPrViewModeNotes::mouseDoubleClickEvent(QMouseEvent *event, const QPointF &point)
87 {
88     m_toolProxy->mouseDoubleClickEvent(event, point);
89 }
90 
mouseMoveEvent(QMouseEvent * event,const QPointF & point)91 void KPrViewModeNotes::mouseMoveEvent(QMouseEvent *event, const QPointF &point)
92 {
93     m_toolProxy->mouseMoveEvent(event, point);
94 }
95 
mouseReleaseEvent(QMouseEvent * event,const QPointF & point)96 void KPrViewModeNotes::mouseReleaseEvent(QMouseEvent *event, const QPointF &point)
97 {
98     m_toolProxy->mouseReleaseEvent(event, point);
99 }
100 
shortcutOverrideEvent(QKeyEvent * event)101 void KPrViewModeNotes::shortcutOverrideEvent(QKeyEvent *event)
102 {
103     m_toolProxy->shortcutOverrideEvent(event);
104 }
105 
keyPressEvent(QKeyEvent * event)106 void KPrViewModeNotes::keyPressEvent(QKeyEvent *event)
107 {
108     m_toolProxy->keyPressEvent(event);
109     KoPageApp::PageNavigation pageNavigation;
110 
111     if (!event->isAccepted()) {
112         event->accept();
113 
114         switch (event->key()) {
115             case Qt::Key_Home:
116                 pageNavigation = KoPageApp::PageFirst;
117                 break;
118             case Qt::Key_PageUp:
119                 pageNavigation = KoPageApp::PagePrevious;
120                 break;
121             case Qt::Key_PageDown:
122                 pageNavigation = KoPageApp::PageNext;
123                 break;
124             case Qt::Key_End:
125                 pageNavigation = KoPageApp::PageLast;
126                 break;
127             default:
128                 event->ignore();
129                 return;
130         }
131 
132         KoPAPageBase *activePage = m_view->activePage();
133         KoPAPageBase *newPage = m_view->kopaDocument()->pageByNavigation(activePage, pageNavigation);
134 
135         if (newPage != activePage) {
136             updateActivePage( newPage );
137         }
138     }
139 }
140 
keyReleaseEvent(QKeyEvent * event)141 void KPrViewModeNotes::keyReleaseEvent(QKeyEvent *event)
142 {
143     m_toolProxy->keyReleaseEvent(event);
144 }
145 
wheelEvent(QWheelEvent * event,const QPointF & point)146 void KPrViewModeNotes::wheelEvent(QWheelEvent *event, const QPointF &point)
147 {
148     m_toolProxy->wheelEvent(event, point);
149 }
150 
activate(KoPAViewMode * previousViewMode)151 void KPrViewModeNotes::activate(KoPAViewMode *previousViewMode)
152 {
153     Q_UNUSED( previousViewMode );
154     m_canvas->resourceManager()->setResource(KoCanvasResourceManager::ShowTextShapeOutlines, QVariant(true));
155     m_view->setActionEnabled( KoPAView::AllActions, false );
156     updateActivePage( m_view->activePage() );
157 }
158 
deactivate()159 void KPrViewModeNotes::deactivate()
160 {
161     m_canvas->resourceManager()->setResource(KoCanvasResourceManager::ShowTextShapeOutlines, QVariant(false));
162     m_view->setActionEnabled( KoPAView::AllActions, true );
163     m_view->doUpdateActivePage(m_view->activePage());
164 }
165 
updateActivePage(KoPAPageBase * page)166 void KPrViewModeNotes::updateActivePage(KoPAPageBase *page)
167 {
168     if (m_view->activePage() != page) {
169         m_view->setActivePage(page);
170     }
171 
172     KPrPage *prPage = static_cast<KPrPage *>(page);
173     if (prPage == 0) {
174         return;
175     }
176     KPrNotes *notes = prPage->pageNotes();
177     notes->updatePageThumbnail();
178     KoShapeLayer* layer = static_cast<KoShapeLayer*>(notes->shapes().last());
179 
180     m_canvas->shapeManager()->setShapes(layer->shapes());
181     m_canvas->masterShapeManager()->setShapes(QList<KoShape*>());
182 
183     static_cast<KoPAView*>(m_view)->updateCanvasSize(true);
184 
185     m_view->updatePageNavigationActions();
186 
187     KoSelection *selection = m_canvas->shapeManager()->selection();
188     selection->select(notes->textShape());
189     selection->setActiveLayer( layer );
190     QString tool = KoToolManager::instance()->preferredToolForSelection(selection->selectedShapes());
191     // we need to make sue to switch to the default tool so that the text tool does notice the selection chane
192     KoToolManager::instance()->switchToolRequested(KoInteractionTool_ID);
193     // we need to set the focus to the text tool again so that we can start typing
194     // otherwise you need to click on the shape again
195     m_canvas->canvasWidget() ? canvas()->canvasWidget()->setFocus() : canvas()->canvasItem()->setFocus();
196     KoToolManager::instance()->switchToolRequested(tool);
197 }
198 
addShape(KoShape * shape)199 void KPrViewModeNotes::addShape( KoShape *shape )
200 {
201     KoShape *parent = shape;
202     KPrNotes *notes = 0;
203     // similar to KoPADocument::pageByShape()
204     while ( !notes && ( parent = parent->parent() ) ) {
205         notes = dynamic_cast<KPrNotes *>( parent );
206     }
207 
208     if ( notes ) {
209         Q_ASSERT(dynamic_cast<KPrPage *>(m_view->activePage()));
210         KPrPage *activePage = static_cast<KPrPage *>(m_view->activePage());
211         if ( notes == activePage->pageNotes() ) {
212             m_view->kopaCanvas()->shapeManager()->addShape( shape );
213         }
214     }
215 }
216 
removeShape(KoShape * shape)217 void KPrViewModeNotes::removeShape( KoShape *shape )
218 {
219     KoShape *parent = shape;
220     KPrNotes *notes = 0;
221     while ( !notes && ( parent = parent->parent() ) ) {
222         notes = dynamic_cast<KPrNotes *>( parent );
223     }
224 
225     if ( notes ) {
226         KPrPage *activePage = static_cast<KPrPage *>( m_view->activePage() );
227         if ( notes == activePage->pageNotes() ) {
228             m_view->kopaCanvas()->shapeManager()->remove( shape );
229         }
230     }
231 }
232 
activePageLayout() const233 const KoPageLayout &KPrViewModeNotes::activePageLayout() const
234 {
235     KPrPage *activePage = static_cast<KPrPage *>( m_view->activePage() );
236     KPrNotes *notes = activePage->pageNotes();
237 
238     return notes->pageLayout();
239 }
240 
241