File indexing completed on 2024-05-12 12:58:01
0001 /* 0002 * This file is part of the KDE project 0003 * 0004 * SPDX-FileCopyrightText: 2013 Arjen Hiemstra <ahiemstra@heimr.nl> 0005 * 0006 * SPDX-License-Identifier: LGPL-2.0-or-later 0007 * 0008 */ 0009 0010 #include "View.h" 0011 0012 #include <QTimer> 0013 #include <QPainter> 0014 #include <QQuickWindow> 0015 #include <QGraphicsWidget> 0016 #include <QStyleOptionGraphicsItem> 0017 0018 #include "Document.h" 0019 #include <KoCanvasController.h> 0020 #include <KoZoomController.h> 0021 0022 using namespace Calligra::Components; 0023 0024 class View::Private 0025 { 0026 public: 0027 Private(View* qq) : q{qq}, document{nullptr}, canvas{nullptr} 0028 { } 0029 0030 void updateCanvas(); 0031 0032 View* q; 0033 0034 Document* document; 0035 QGraphicsWidget* canvas; 0036 0037 QTimer updateTimer; 0038 }; 0039 0040 View::View(QQuickItem* parent) 0041 : QQuickPaintedItem{parent}, d{new Private{this}} 0042 { 0043 } 0044 0045 View::~View() 0046 { 0047 delete d; 0048 } 0049 0050 void View::paint(QPainter* painter) 0051 { 0052 if(!d->document || !d->canvas) { 0053 return; 0054 } 0055 0056 //TODO: This should probably be double-buffered to prevent flickering 0057 QStyleOptionGraphicsItem option; 0058 option.exposedRect = QRectF{0, 0, width(), height()}; 0059 option.rect = option.exposedRect.toAlignedRect(); 0060 d->canvas->paint(painter, &option); 0061 } 0062 0063 Document* View::document() const 0064 { 0065 return d->document; 0066 } 0067 0068 void View::setDocument(Document* newValue) 0069 { 0070 if(newValue != d->document) { 0071 if(d->document) { 0072 disconnect(d->document, SIGNAL(requestViewUpdate()), this, SLOT(update())); 0073 } 0074 0075 d->document = newValue; 0076 connect(d->document, &Document::statusChanged, this, [&]() { d->updateCanvas(); }); 0077 connect(d->document, &Document::requestViewUpdate, this, [&]() { update(); }); 0078 0079 d->updateCanvas(); 0080 emit documentChanged(); 0081 } 0082 } 0083 0084 float View::zoom() const 0085 { 0086 if(d->document && d->document->zoomController()) { 0087 return d->document->zoomController()->zoomAction()->effectiveZoom(); 0088 } 0089 0090 return -1.f; 0091 } 0092 0093 void View::setZoom(float newValue) 0094 { 0095 if(zoom() == newValue) 0096 return; 0097 0098 if(d->document && d->document->zoomController()) { 0099 d->document->zoomController()->setZoom(KoZoomMode::ZOOM_CONSTANT, newValue); 0100 update(); 0101 } 0102 } 0103 0104 void View::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) 0105 { 0106 if (d->canvas) { 0107 d->canvas->setGeometry(newGeometry); 0108 } 0109 QQuickPaintedItem::geometryChanged(newGeometry, oldGeometry); 0110 } 0111 0112 void View::Private::updateCanvas() 0113 { 0114 if(document && document->status() == DocumentStatus::Loaded) { 0115 canvas = document->canvas(); 0116 canvas->setGeometry(0, 0, q->width(), q->height()); 0117 q->update(); 0118 } else { 0119 canvas = nullptr; 0120 } 0121 }