Warning, file /office/calligra/libs/pageapp/KoPACanvasItem.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* This file is part of the KDE project 0002 Copyright (C) 2006-2007 Thorsten Zachmann <zachmann@kde.org> 0003 Copyright (C) 2010 Boudewijn Rempt <boud@valdyas.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 "KoPACanvasItem.h" 0022 0023 #include "KoPADocument.h" 0024 #include "KoPAView.h" 0025 #include "KoPAPageBase.h" 0026 0027 #include <KoZoomHandler.h> 0028 #include <KoToolProxy.h> 0029 #include <KoPageLayout.h> 0030 0031 #include <kxmlguifactory.h> 0032 0033 #include <QStyleOptionGraphicsItem> 0034 #include <QGraphicsSceneResizeEvent> 0035 #include <QGraphicsSceneMouseEvent> 0036 #include <QGraphicsSceneWheelEvent> 0037 #include <QMenu> 0038 0039 KoPACanvasItem::KoPACanvasItem( KoPADocument * doc) 0040 : QGraphicsWidget() 0041 , KoPACanvasBase( doc ) 0042 { 0043 setFocusPolicy( Qt::StrongFocus ); 0044 // this is much faster than painting it in the paintevent 0045 setAutoFillBackground( true ); 0046 } 0047 0048 void KoPACanvasItem::repaint() 0049 { 0050 update(); 0051 } 0052 0053 0054 void KoPACanvasItem::updateSize() 0055 { 0056 QSize size; 0057 0058 if ( koPAView()->activePage() ) { 0059 KoPageLayout pageLayout = koPAView()->activePage()->pageLayout(); 0060 size.setWidth( qRound( koPAView()->zoomHandler()->zoomItX( pageLayout.width ) ) ); 0061 size.setHeight( qRound( koPAView()->zoomHandler()->zoomItX( pageLayout.height ) ) ); 0062 } 0063 emit documentSize(size); 0064 } 0065 0066 void KoPACanvasItem::updateCanvas( const QRectF& rc ) 0067 { QRect clipRect(viewToWidget(viewConverter()->documentToView(rc).toRect())); 0068 clipRect.adjust( -2, -2, 2, 2 ); // Resize to fit anti-aliasing 0069 clipRect.moveTopLeft( clipRect.topLeft() - documentOffset()); 0070 update( clipRect ); 0071 0072 emit canvasUpdated(); 0073 } 0074 0075 void KoPACanvasItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 0076 { 0077 Q_UNUSED(widget) 0078 KoPACanvasBase::paint(*painter, option->exposedRect); 0079 } 0080 0081 bool KoPACanvasItem::event( QEvent *ev ) 0082 { 0083 if (ev->type() == QEvent::ShortcutOverride) { 0084 QKeyEvent *kev = static_cast<QKeyEvent *>(ev); 0085 koPAView()->viewMode()->shortcutOverrideEvent(kev); 0086 } 0087 return QGraphicsWidget::event( ev ); 0088 } 0089 0090 void KoPACanvasItem::mousePressEvent( QGraphicsSceneMouseEvent *e ) 0091 { 0092 QMouseEvent me(e->type(), e->pos().toPoint(), e->button(), e->buttons(), e->modifiers()); 0093 Q_ASSERT(koPAView()); 0094 Q_ASSERT(koPAView()->viewMode()); 0095 Q_ASSERT(viewConverter()); 0096 koPAView()->viewMode()->mousePressEvent(&me, viewConverter()->viewToDocument(widgetToView(me.pos() + documentOffset()))); 0097 0098 if(!me.isAccepted() && me.button() == Qt::RightButton) 0099 { 0100 showContextMenu( me.pos(), toolProxy()->popupActionList() ); 0101 e->setAccepted( true ); 0102 } 0103 } 0104 0105 void KoPACanvasItem::mouseDoubleClickEvent( QGraphicsSceneMouseEvent *e ) 0106 { 0107 QMouseEvent me(e->type(), e->pos().toPoint(), e->button(), e->buttons(), e->modifiers()); 0108 koPAView()->viewMode()->mouseDoubleClickEvent(&me, viewConverter()->viewToDocument(widgetToView(me.pos() + documentOffset()))); 0109 } 0110 0111 void KoPACanvasItem::mouseMoveEvent( QGraphicsSceneMouseEvent *e ) 0112 { 0113 QMouseEvent me(e->type(), e->pos().toPoint(), e->button(), e->buttons(), e->modifiers()); 0114 koPAView()->viewMode()->mouseMoveEvent(&me, viewConverter()->viewToDocument(widgetToView(me.pos() + documentOffset()))); 0115 } 0116 0117 void KoPACanvasItem::mouseReleaseEvent( QGraphicsSceneMouseEvent *e ) 0118 { 0119 QMouseEvent me(e->type(), e->pos().toPoint(), e->button(), e->buttons(), e->modifiers()); 0120 koPAView()->viewMode()->mouseReleaseEvent(&me, viewConverter()->viewToDocument(widgetToView(me.pos() + documentOffset()))); 0121 } 0122 0123 void KoPACanvasItem::keyPressEvent( QKeyEvent *event ) 0124 { 0125 koPAView()->viewMode()->keyPressEvent( event ); 0126 if (! event->isAccepted()) { 0127 if (event->key() == Qt::Key_Backtab 0128 || (event->key() == Qt::Key_Tab && (event->modifiers() & Qt::ShiftModifier))) 0129 focusNextPrevChild(false); 0130 else if (event->key() == Qt::Key_Tab) 0131 focusNextPrevChild(true); 0132 } 0133 } 0134 0135 void KoPACanvasItem::keyReleaseEvent( QKeyEvent *event ) 0136 { 0137 koPAView()->viewMode()->keyReleaseEvent( event ); 0138 } 0139 0140 void KoPACanvasItem::wheelEvent ( QGraphicsSceneWheelEvent * event ) 0141 { 0142 QWheelEvent ev(event->pos().toPoint(), event->delta(), event->buttons(), event->modifiers(), event->orientation()); 0143 koPAView()->viewMode()->wheelEvent( &ev, viewConverter()->viewToDocument(widgetToView(ev.pos() + documentOffset()))); 0144 } 0145 0146 void KoPACanvasItem::closeEvent( QCloseEvent * event ) 0147 { 0148 koPAView()->viewMode()->closeEvent( event ); 0149 } 0150 0151 void KoPACanvasItem::updateInputMethodInfo() 0152 { 0153 updateMicroFocus(); 0154 } 0155 0156 QVariant KoPACanvasItem::inputMethodQuery(Qt::InputMethodQuery query) const 0157 { 0158 return toolProxy()->inputMethodQuery(query, *(viewConverter()) ); 0159 } 0160 0161 void KoPACanvasItem::inputMethodEvent(QInputMethodEvent *event) 0162 { 0163 toolProxy()->inputMethodEvent(event); 0164 } 0165 0166 void KoPACanvasItem::resizeEvent( QGraphicsSceneResizeEvent * event ) 0167 { 0168 emit sizeChanged( event->newSize().toSize() ); 0169 } 0170 0171 void KoPACanvasItem::showContextMenu( const QPoint& globalPos, const QList<QAction*>& actionList ) 0172 { 0173 KoPAView* view = dynamic_cast<KoPAView*>(koPAView()); 0174 if (!view) return; 0175 0176 view->unplugActionList( "toolproxy_action_list" ); 0177 view->plugActionList( "toolproxy_action_list", actionList ); 0178 if( !view->factory() ) return; 0179 0180 QMenu *menu = dynamic_cast<QMenu*>( view->factory()->container( "default_canvas_popup", view ) ); 0181 0182 if( menu ) 0183 menu->exec( globalPos ); 0184 } 0185 0186 void KoPACanvasItem::setCursor(const QCursor &cursor) 0187 { 0188 QGraphicsWidget::setCursor(cursor); 0189 }