File indexing completed on 2024-05-12 16:36:50

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2008 Fredy Yanardi <fyanardi@gmail.com>
0003  * Copyright (C) 2009 Thorsten Zachmann <zachmann@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "KPrViewModeNotes.h"
0022 
0023 #include <QEvent>
0024 #include <QPainter>
0025 #include <QGraphicsWidget>
0026 
0027 #include <KoDocumentResourceManager.h>
0028 #include <KoRuler.h>
0029 #include <KoSelection.h>
0030 #include <KoShapeLayer.h>
0031 #include <KoShapeManager.h>
0032 #include <KoText.h>
0033 #include <KoToolManager.h>
0034 #include <KoToolProxy.h>
0035 #include <KoInteractionTool.h>
0036 
0037 #include <KoPACanvasBase.h>
0038 #include <KoPADocument.h>
0039 #include <KoPAPageBase.h>
0040 #include <KoPAView.h>
0041 
0042 #include "StageDebug.h"
0043 #include "KPrNotes.h"
0044 #include "KPrPage.h"
0045 
0046 KPrViewModeNotes::KPrViewModeNotes(KoPAViewBase *view, KoPACanvasBase *canvas)
0047     : KoPAViewMode( view, canvas )
0048 {
0049     setName(i18n("Notes"));
0050 }
0051 
0052 KPrViewModeNotes::~KPrViewModeNotes()
0053 {
0054 }
0055 
0056 void KPrViewModeNotes::paint(KoPACanvasBase *canvas, QPainter &painter, const QRectF &paintRect)
0057 {
0058 #ifdef NDEBUG
0059     Q_UNUSED(canvas);
0060 #endif
0061     Q_ASSERT(m_canvas == canvas);
0062 
0063     painter.translate(m_canvas->documentOrigin());
0064     painter.translate(-m_canvas->documentOffset());
0065     painter.setRenderHint(QPainter::Antialiasing);
0066     QRectF clipRect = paintRect.translated(m_canvas->documentOffset() - m_canvas->documentOrigin());
0067     painter.setClipRect(clipRect);
0068 
0069     KoViewConverter *converter = m_view->viewConverter(m_canvas);
0070     const KoPageLayout &layout = activePageLayout();
0071     painter.fillRect(converter->documentToView(QRectF(0, 0, layout.width, layout.height)), Qt::white);
0072     m_canvas->shapeManager()->paint(painter, *converter, false);
0073     m_toolProxy->paint(painter, *converter);
0074 }
0075 
0076 void KPrViewModeNotes::tabletEvent(QTabletEvent *event, const QPointF &point)
0077 {
0078     m_toolProxy->tabletEvent(event, point);
0079 }
0080 
0081 void KPrViewModeNotes::mousePressEvent(QMouseEvent *event, const QPointF &point)
0082 {
0083     m_toolProxy->mousePressEvent(event, point);
0084 }
0085 
0086 void KPrViewModeNotes::mouseDoubleClickEvent(QMouseEvent *event, const QPointF &point)
0087 {
0088     m_toolProxy->mouseDoubleClickEvent(event, point);
0089 }
0090 
0091 void KPrViewModeNotes::mouseMoveEvent(QMouseEvent *event, const QPointF &point)
0092 {
0093     m_toolProxy->mouseMoveEvent(event, point);
0094 }
0095 
0096 void KPrViewModeNotes::mouseReleaseEvent(QMouseEvent *event, const QPointF &point)
0097 {
0098     m_toolProxy->mouseReleaseEvent(event, point);
0099 }
0100 
0101 void KPrViewModeNotes::shortcutOverrideEvent(QKeyEvent *event)
0102 {
0103     m_toolProxy->shortcutOverrideEvent(event);
0104 }
0105 
0106 void KPrViewModeNotes::keyPressEvent(QKeyEvent *event)
0107 {
0108     m_toolProxy->keyPressEvent(event);
0109     KoPageApp::PageNavigation pageNavigation;
0110 
0111     if (!event->isAccepted()) {
0112         event->accept();
0113 
0114         switch (event->key()) {
0115             case Qt::Key_Home:
0116                 pageNavigation = KoPageApp::PageFirst;
0117                 break;
0118             case Qt::Key_PageUp:
0119                 pageNavigation = KoPageApp::PagePrevious;
0120                 break;
0121             case Qt::Key_PageDown:
0122                 pageNavigation = KoPageApp::PageNext;
0123                 break;
0124             case Qt::Key_End:
0125                 pageNavigation = KoPageApp::PageLast;
0126                 break;
0127             default:
0128                 event->ignore();
0129                 return;
0130         }
0131 
0132         KoPAPageBase *activePage = m_view->activePage();
0133         KoPAPageBase *newPage = m_view->kopaDocument()->pageByNavigation(activePage, pageNavigation);
0134 
0135         if (newPage != activePage) {
0136             updateActivePage( newPage );
0137         }
0138     }
0139 }
0140 
0141 void KPrViewModeNotes::keyReleaseEvent(QKeyEvent *event)
0142 {
0143     m_toolProxy->keyReleaseEvent(event);
0144 }
0145 
0146 void KPrViewModeNotes::wheelEvent(QWheelEvent *event, const QPointF &point)
0147 {
0148     m_toolProxy->wheelEvent(event, point);
0149 }
0150 
0151 void KPrViewModeNotes::activate(KoPAViewMode *previousViewMode)
0152 {
0153     Q_UNUSED( previousViewMode );
0154     m_canvas->resourceManager()->setResource(KoCanvasResourceManager::ShowTextShapeOutlines, QVariant(true));
0155     m_view->setActionEnabled( KoPAView::AllActions, false );
0156     updateActivePage( m_view->activePage() );
0157 }
0158 
0159 void KPrViewModeNotes::deactivate()
0160 {
0161     m_canvas->resourceManager()->setResource(KoCanvasResourceManager::ShowTextShapeOutlines, QVariant(false));
0162     m_view->setActionEnabled( KoPAView::AllActions, true );
0163     m_view->doUpdateActivePage(m_view->activePage());
0164 }
0165 
0166 void KPrViewModeNotes::updateActivePage(KoPAPageBase *page)
0167 {
0168     if (m_view->activePage() != page) {
0169         m_view->setActivePage(page);
0170     }
0171 
0172     KPrPage *prPage = static_cast<KPrPage *>(page);
0173     if (prPage == 0) {
0174         return;
0175     }
0176     KPrNotes *notes = prPage->pageNotes();
0177     notes->updatePageThumbnail();
0178     KoShapeLayer* layer = static_cast<KoShapeLayer*>(notes->shapes().last());
0179 
0180     m_canvas->shapeManager()->setShapes(layer->shapes());
0181     m_canvas->masterShapeManager()->setShapes(QList<KoShape*>());
0182 
0183     static_cast<KoPAView*>(m_view)->updateCanvasSize(true);
0184 
0185     m_view->updatePageNavigationActions();
0186 
0187     KoSelection *selection = m_canvas->shapeManager()->selection();
0188     selection->select(notes->textShape());
0189     selection->setActiveLayer( layer );
0190     QString tool = KoToolManager::instance()->preferredToolForSelection(selection->selectedShapes());
0191     // we need to make sue to switch to the default tool so that the text tool does notice the selection chane
0192     KoToolManager::instance()->switchToolRequested(KoInteractionTool_ID);
0193     // we need to set the focus to the text tool again so that we can start typing
0194     // otherwise you need to click on the shape again
0195     m_canvas->canvasWidget() ? canvas()->canvasWidget()->setFocus() : canvas()->canvasItem()->setFocus();
0196     KoToolManager::instance()->switchToolRequested(tool);
0197 }
0198 
0199 void KPrViewModeNotes::addShape( KoShape *shape )
0200 {
0201     KoShape *parent = shape;
0202     KPrNotes *notes = 0;
0203     // similar to KoPADocument::pageByShape()
0204     while ( !notes && ( parent = parent->parent() ) ) {
0205         notes = dynamic_cast<KPrNotes *>( parent );
0206     }
0207 
0208     if ( notes ) {
0209         Q_ASSERT(dynamic_cast<KPrPage *>(m_view->activePage()));
0210         KPrPage *activePage = static_cast<KPrPage *>(m_view->activePage());
0211         if ( notes == activePage->pageNotes() ) {
0212             m_view->kopaCanvas()->shapeManager()->addShape( shape );
0213         }
0214     }
0215 }
0216 
0217 void KPrViewModeNotes::removeShape( KoShape *shape )
0218 {
0219     KoShape *parent = shape;
0220     KPrNotes *notes = 0;
0221     while ( !notes && ( parent = parent->parent() ) ) {
0222         notes = dynamic_cast<KPrNotes *>( parent );
0223     }
0224 
0225     if ( notes ) {
0226         KPrPage *activePage = static_cast<KPrPage *>( m_view->activePage() );
0227         if ( notes == activePage->pageNotes() ) {
0228             m_view->kopaCanvas()->shapeManager()->remove( shape );
0229         }
0230     }
0231 }
0232 
0233 const KoPageLayout &KPrViewModeNotes::activePageLayout() const
0234 {
0235     KPrPage *activePage = static_cast<KPrPage *>( m_view->activePage() );
0236     KPrNotes *notes = activePage->pageNotes();
0237 
0238     return notes->pageLayout();
0239 }
0240