File indexing completed on 2024-04-28 16:13:35
0001 /* 0002 * This file is part of the KDE project 0003 * 0004 * Copyright (C) 2013 Arjen Hiemstra <ahiemstra@heimr.nl> 0005 * 0006 * This library is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU Library General Public 0008 * License as published by the Free Software Foundation; either 0009 * version 2 of the License, or (at your option) any later version. 0010 * 0011 * This library is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0014 * Library General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU Library General Public License 0017 * along with this library; see the file COPYING.LIB. If not, write to 0018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0019 * Boston, MA 02110-1301, USA. 0020 * 0021 */ 0022 0023 #include "View.h" 0024 0025 #include <QTimer> 0026 #include <QPainter> 0027 #include <QQuickWindow> 0028 #include <QGraphicsWidget> 0029 #include <QStyleOptionGraphicsItem> 0030 0031 #include "Document.h" 0032 #include <KoCanvasController.h> 0033 #include <KoZoomController.h> 0034 0035 using namespace Calligra::Components; 0036 0037 class View::Private 0038 { 0039 public: 0040 Private(View* qq) : q{qq}, document{nullptr}, canvas{nullptr} 0041 { } 0042 0043 void updateCanvas(); 0044 0045 View* q; 0046 0047 Document* document; 0048 QGraphicsWidget* canvas; 0049 0050 QTimer updateTimer; 0051 }; 0052 0053 View::View(QQuickItem* parent) 0054 : QQuickPaintedItem{parent}, d{new Private{this}} 0055 { 0056 } 0057 0058 View::~View() 0059 { 0060 delete d; 0061 } 0062 0063 void View::paint(QPainter* painter) 0064 { 0065 if(!d->document || !d->canvas) { 0066 return; 0067 } 0068 0069 //TODO: This should probably be double-buffered to prevent flickering 0070 QStyleOptionGraphicsItem option; 0071 option.exposedRect = QRectF{0, 0, width(), height()}; 0072 option.rect = option.exposedRect.toAlignedRect(); 0073 d->canvas->paint(painter, &option); 0074 } 0075 0076 Document* View::document() const 0077 { 0078 return d->document; 0079 } 0080 0081 void View::setDocument(Document* newValue) 0082 { 0083 if(newValue != d->document) { 0084 if(d->document) { 0085 disconnect(d->document, SIGNAL(requestViewUpdate()), this, SLOT(update())); 0086 } 0087 0088 d->document = newValue; 0089 connect(d->document, &Document::statusChanged, [&]() { d->updateCanvas(); }); 0090 connect(d->document, SIGNAL(requestViewUpdate()), this, SLOT(update())); 0091 0092 d->updateCanvas(); 0093 emit documentChanged(); 0094 } 0095 } 0096 0097 float View::zoom() const 0098 { 0099 if(d->document && d->document->zoomController()) { 0100 return d->document->zoomController()->zoomAction()->effectiveZoom(); 0101 } 0102 0103 return -1.f; 0104 } 0105 0106 void View::setZoom(float newValue) 0107 { 0108 if(zoom() == newValue) 0109 return; 0110 0111 if(d->document && d->document->zoomController()) { 0112 d->document->zoomController()->setZoom(KoZoomMode::ZOOM_CONSTANT, newValue); 0113 update(); 0114 } 0115 } 0116 0117 void View::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) 0118 { 0119 if (d->canvas) { 0120 d->canvas->setGeometry(newGeometry); 0121 } 0122 QQuickPaintedItem::geometryChanged(newGeometry, oldGeometry); 0123 } 0124 0125 void View::Private::updateCanvas() 0126 { 0127 if(document && document->status() == DocumentStatus::Loaded) { 0128 canvas = document->canvas(); 0129 canvas->setGeometry(0, 0, q->width(), q->height()); 0130 q->update(); 0131 } else { 0132 canvas = nullptr; 0133 } 0134 }