File indexing completed on 2024-05-26 16:15:02
0001 /* This file is part of the KDE project 0002 Copyright (C) 2006-2007 Thorsten Zachmann <zachmann@kde.org> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This library is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this library; see the file COPYING.LIB. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "KoPACanvas.h" 0021 0022 #include <KoToolProxy.h> 0023 #include <KoCanvasController.h> 0024 #include <KoZoomHandler.h> 0025 #include <KoPageLayout.h> 0026 0027 #include "KoPADocument.h" 0028 #include "KoPAView.h" 0029 0030 #include <kxmlguifactory.h> 0031 0032 #include <QMenu> 0033 #include <QMouseEvent> 0034 #include <QPainter> 0035 0036 KoPACanvas::KoPACanvas( KoPAViewBase * view, KoPADocument * doc, QWidget *parent , Qt::WindowFlags f) 0037 : QWidget( parent, f ) 0038 , KoPACanvasBase( doc ) 0039 { 0040 setView(view); 0041 setFocusPolicy(Qt::StrongFocus); 0042 // this is much faster than painting it in the paintevent 0043 setAutoFillBackground(true); 0044 updateSize(); 0045 setAttribute(Qt::WA_InputMethodEnabled, true); 0046 } 0047 0048 void KoPACanvas::repaint() 0049 { 0050 update(); 0051 } 0052 0053 0054 QWidget* KoPACanvas::canvasWidget() 0055 { 0056 return this; 0057 } 0058 0059 const QWidget* KoPACanvas::canvasWidget() const 0060 { 0061 return this; 0062 } 0063 0064 void KoPACanvas::updateSize() 0065 { 0066 QSize size; 0067 0068 if ( koPAView()->activePage() ) { 0069 KoPageLayout pageLayout = koPAView()->viewMode()->activePageLayout(); 0070 size.setWidth( qRound( koPAView()->zoomHandler()->zoomItX( pageLayout.width ) ) ); 0071 size.setHeight( qRound( koPAView()->zoomHandler()->zoomItX( pageLayout.height ) ) ); 0072 } 0073 0074 emit documentSize(size); 0075 } 0076 0077 void KoPACanvas::updateCanvas( const QRectF& rc ) 0078 { 0079 QRect clipRect(viewToWidget(viewConverter()->documentToView(rc).toRect())); 0080 clipRect.adjust( -2, -2, 2, 2 ); // Resize to fit anti-aliasing 0081 clipRect.moveTopLeft( clipRect.topLeft() - documentOffset()); 0082 update( clipRect ); 0083 0084 emit canvasUpdated(); 0085 } 0086 0087 bool KoPACanvas::event(QEvent *e) 0088 { 0089 if(toolProxy()) { 0090 if (e->type() == QEvent::TouchBegin || 0091 e->type() == QEvent::TouchUpdate || 0092 e->type() == QEvent::TouchEnd) 0093 { 0094 toolProxy()->touchEvent(dynamic_cast<QTouchEvent*>(e)); 0095 } 0096 toolProxy()->processEvent(e); 0097 } 0098 return QWidget::event(e); 0099 } 0100 0101 void KoPACanvas::paintEvent( QPaintEvent *event ) 0102 { 0103 QPainter painter(this); 0104 paint(painter, event->rect()); 0105 painter.end(); 0106 } 0107 0108 void KoPACanvas::tabletEvent( QTabletEvent *event ) 0109 { 0110 koPAView()->viewMode()->tabletEvent(event, viewConverter()->viewToDocument(widgetToView(event->pos() + documentOffset()))); 0111 } 0112 0113 void KoPACanvas::mousePressEvent( QMouseEvent *event ) 0114 { 0115 koPAView()->viewMode()->mousePressEvent(event, viewConverter()->viewToDocument(widgetToView(event->pos() + documentOffset()))); 0116 0117 if(!event->isAccepted() && event->button() == Qt::RightButton) 0118 { 0119 showContextMenu( event->globalPos(), toolProxy()->popupActionList() ); 0120 } 0121 0122 event->setAccepted( true ); 0123 } 0124 0125 void KoPACanvas::mouseDoubleClickEvent( QMouseEvent *event ) 0126 { 0127 koPAView()->viewMode()->mouseDoubleClickEvent( event, viewConverter()->viewToDocument(widgetToView(event->pos() + documentOffset()))); 0128 } 0129 0130 void KoPACanvas::mouseMoveEvent( QMouseEvent *event ) 0131 { 0132 koPAView()->viewMode()->mouseMoveEvent( event, viewConverter()->viewToDocument(widgetToView(event->pos() + documentOffset()))); 0133 } 0134 0135 void KoPACanvas::mouseReleaseEvent( QMouseEvent *event ) 0136 { 0137 koPAView()->viewMode()->mouseReleaseEvent( event, viewConverter()->viewToDocument(widgetToView(event->pos() + documentOffset()))); 0138 } 0139 0140 void KoPACanvas::keyPressEvent( QKeyEvent *event ) 0141 { 0142 koPAView()->viewMode()->keyPressEvent( event ); 0143 if (! event->isAccepted()) { 0144 if (event->key() == Qt::Key_Backtab 0145 || (event->key() == Qt::Key_Tab && (event->modifiers() & Qt::ShiftModifier))) 0146 focusNextPrevChild(false); 0147 else if (event->key() == Qt::Key_Tab) 0148 focusNextPrevChild(true); 0149 } 0150 } 0151 0152 void KoPACanvas::keyReleaseEvent( QKeyEvent *event ) 0153 { 0154 koPAView()->viewMode()->keyReleaseEvent( event ); 0155 } 0156 0157 void KoPACanvas::wheelEvent ( QWheelEvent * event ) 0158 { 0159 koPAView()->viewMode()->wheelEvent( event, viewConverter()->viewToDocument(widgetToView(event->pos() + documentOffset()))); 0160 } 0161 0162 void KoPACanvas::closeEvent( QCloseEvent * event ) 0163 { 0164 koPAView()->viewMode()->closeEvent( event ); 0165 } 0166 0167 void KoPACanvas::updateInputMethodInfo() 0168 { 0169 updateMicroFocus(); 0170 } 0171 0172 QVariant KoPACanvas::inputMethodQuery(Qt::InputMethodQuery query) const 0173 { 0174 if (query == Qt::ImMicroFocus) { 0175 QRectF rect = (toolProxy()->inputMethodQuery(query, *(viewConverter())).toRectF()).toRect(); 0176 QPointF scroll(canvasController()->scrollBarValue()); 0177 if (canvasController()->canvasMode() == KoCanvasController::Spreadsheet && 0178 canvasWidget()->layoutDirection() == Qt::RightToLeft) { 0179 scroll.setX(-scroll.x()); 0180 } 0181 rect.translate(documentOrigin() - scroll); 0182 return rect.toRect(); 0183 } 0184 return toolProxy()->inputMethodQuery(query, *(viewConverter()) ); 0185 } 0186 0187 void KoPACanvas::inputMethodEvent(QInputMethodEvent *event) 0188 { 0189 toolProxy()->inputMethodEvent(event); 0190 } 0191 0192 void KoPACanvas::resizeEvent( QResizeEvent * event ) 0193 { 0194 emit sizeChanged( event->size() ); 0195 } 0196 0197 void KoPACanvas::showContextMenu( const QPoint& globalPos, const QList<QAction*>& actionList ) 0198 { 0199 KoPAView *view = dynamic_cast<KoPAView*>(koPAView()); 0200 if (!view || !view->factory()) return; 0201 0202 view->unplugActionList( "toolproxy_action_list" ); 0203 view->plugActionList( "toolproxy_action_list", actionList ); 0204 0205 0206 QMenu *menu = dynamic_cast<QMenu*>( view->factory()->container( "default_canvas_popup", view ) ); 0207 0208 if( menu ) 0209 menu->exec( globalPos ); 0210 } 0211 0212 void KoPACanvas::setCursor(const QCursor &cursor) 0213 { 0214 QWidget::setCursor(cursor); 0215 }