F:/KPlato/koffice/libs/flake/KoCanvasController.cpp

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002  * Copyright (C) 2006 Thomas Zander <zander@kde.org>
00003  * Copyright (C) 2006 Peter Simonsson <peter.simonsson@gmail.com>
00004  * Copyright (C) 2006 Thoresten Zachmann <zachmann@kde.org>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public License
00017  * along with this library; see the file COPYING.LIB.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  */
00021 
00022 #include "KoCanvasController.h"
00023 #include "KoShape.h"
00024 #include "KoViewConverter.h"
00025 
00026 #include <kdebug.h>
00027 
00028 #include <QGridLayout>
00029 #include <QScrollBar>
00030 #include <QEvent>
00031 
00032 KoCanvasController::KoCanvasController(QWidget *parent)
00033 : QScrollArea(parent)
00034 , m_canvas(0)
00035 , m_canvasWidget(0)
00036 {
00037     m_viewport = new Viewport();
00038     setWidget(m_viewport);
00039     setWidgetResizable(true);
00040     setAutoFillBackground(false);
00041     connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(updateCanvasOffsetX()));
00042     connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(updateCanvasOffsetY()));
00043 }
00044 
00045 void KoCanvasController::setCanvas(KoCanvasBase *canvas) {
00046     Q_ASSERT(canvas); // param is not null
00047     if(m_canvas) {
00048         emit canvasRemoved(this);
00049         m_viewport->removeCanvas(m_canvas->canvasWidget());
00050     }
00051     m_viewport->setCanvas(canvas->canvasWidget());
00052     m_canvas = canvas;
00053     m_canvas->canvasWidget()->installEventFilter(this);
00054     emit canvasSet(this);
00055 }
00056 
00057 KoCanvasBase* KoCanvasController::canvas() const {
00058     return m_canvas;
00059 }
00060 
00061 int KoCanvasController::visibleHeight() const {
00062     int height1;
00063     if(m_canvasWidget == 0)
00064         height1 = m_viewport->height();
00065     else
00066         height1 = qMin(m_viewport->height(), m_canvasWidget->height());
00067     int height2 = height();
00068     if(horizontalScrollBar() && horizontalScrollBar()->isVisible())
00069         height2 -= horizontalScrollBar()->height();
00070     return qMin(height1, height2);
00071 }
00072 
00073 int KoCanvasController::visibleWidth() const {
00074     int width1;
00075     if(m_canvasWidget == 0)
00076         width1 = m_viewport->width();
00077     else
00078         width1 = qMin(m_viewport->width(), m_canvasWidget->width());
00079     int width2 = width();
00080     if(verticalScrollBar() && verticalScrollBar()->isVisible())
00081         width2 -= verticalScrollBar()->width();
00082     return qMin(width1, width2);
00083 }
00084 
00085 void KoCanvasController::centerCanvas(bool centered) {
00086     m_centerCanvas = centered;
00087     m_viewport->centerCanvas(centered);
00088 }
00089 
00090 bool KoCanvasController::isCanvasCentered() const {
00091     return m_centerCanvas;
00092 }
00093 
00094 int KoCanvasController::canvasOffsetX() const {
00095     int offset = 0;
00096 
00097     if(m_canvas) {
00098         offset = m_canvas->canvasWidget()->x() + frameWidth();
00099     }
00100 
00101     if(horizontalScrollBar()) {
00102         offset -= horizontalScrollBar()->value();
00103     }
00104 
00105     return offset;
00106 }
00107 
00108 int KoCanvasController::canvasOffsetY() const {
00109     int offset = 0;
00110 
00111     if(m_canvas) {
00112         offset = m_canvas->canvasWidget()->y() + frameWidth();
00113     }
00114 
00115     if(verticalScrollBar()) {
00116         offset -= verticalScrollBar()->value();
00117     }
00118 
00119     return offset;
00120 }
00121 
00122 void KoCanvasController::updateCanvasOffsetX() {
00123     emit canvasOffsetXChanged(canvasOffsetX());
00124 }
00125 
00126 void KoCanvasController::updateCanvasOffsetY() {
00127     emit canvasOffsetYChanged(canvasOffsetY());
00128 }
00129 
00130 bool KoCanvasController::eventFilter(QObject* watched, QEvent* event) {
00131     if(m_canvas && m_canvas->canvasWidget() && (watched == m_canvas->canvasWidget())) {
00132         if((event->type() == QEvent::Resize) || event->type() == QEvent::Move) {
00133             updateCanvasOffsetX();
00134             updateCanvasOffsetY();
00135         }
00136     }
00137 
00138     return false;
00139 }
00140 
00141 
00142 // ********** Viewport **********
00143 KoCanvasController::Viewport::Viewport()
00144 : QWidget()
00145 {
00146     setBackgroundRole(QPalette::Dark);
00147     setAutoFillBackground(false);
00148     m_layout = new QGridLayout(this);
00149     m_layout->setSpacing(0);
00150     m_layout->setMargin(0);
00151     centerCanvas(true);
00152 }
00153 
00154 void KoCanvasController::Viewport::setCanvas(QWidget *canvas) {
00155     m_layout->addWidget(canvas, 1, 1, Qt::AlignHCenter | Qt::AlignVCenter);
00156 }
00157 
00158 void KoCanvasController::Viewport::removeCanvas(QWidget *canvas) {
00159     m_layout->removeWidget(canvas);
00160 }
00161 
00162 void KoCanvasController::Viewport::centerCanvas(bool centered) {
00163     m_layout->setColumnStretch(0,centered?1:0);
00164     m_layout->setColumnStretch(1,1);
00165     m_layout->setColumnStretch(2,centered?1:2);
00166     m_layout->setRowStretch(0,centered?1:0);
00167     m_layout->setRowStretch(1,1);
00168     m_layout->setRowStretch(2,centered?1:2);
00169 }
00170 
00171 void KoCanvasController::ensureVisible( KoShape *shape ) {
00172     if( shape )
00173         ensureVisible( shape->boundingRect() );
00174 }
00175 
00176 void KoCanvasController::ensureVisible( const QRectF &rect ) {
00177     // convert the document based rect into a canvas based rect
00178     QRect viewRect = m_canvas->viewConverter()->documentToView( rect ).toRect();
00179 
00180     // calculate position of the centerpoint of the rect we want to make visible
00181     QPoint cp = viewRect.center() + m_canvas->documentOrigin();
00182     cp.rx() += m_canvas->canvasWidget()->x() + frameWidth();
00183     cp.ry() += m_canvas->canvasWidget()->y() + frameWidth();
00184 
00185     // calculate the differance to the viewport centerpoint
00186     QPoint centerDiff = cp - 0.5 * QPoint( m_viewport->width(), m_viewport->height() );
00187 
00188     QScrollBar *hBar = horizontalScrollBar();
00189     // try to centralize the centerpoint of the rect which we want to make visible
00190     if( hBar && hBar->isVisible() ) {
00191         centerDiff.rx() += int( 0.5 * (float)hBar->maximum() );
00192         centerDiff.rx() = qMax( centerDiff.x(), hBar->minimum() );
00193         centerDiff.rx() = qMin( centerDiff.x(), hBar->maximum() );
00194         hBar->setValue( centerDiff.x() );
00195     }
00196     QScrollBar *vBar = verticalScrollBar();
00197     if( vBar && vBar->isVisible() ) {
00198         centerDiff.ry() += int( 0.5 * (float)vBar->maximum() );
00199         centerDiff.ry() = qMax( centerDiff.y(), vBar->minimum() );
00200         centerDiff.ry() = qMin( centerDiff.y(), vBar->maximum() );
00201         vBar->setValue( centerDiff.y() );
00202     }
00203 }
00204 
00205 #include "KoCanvasController.moc"
00206 
00207 // TODO add a paintEvent here and optionally paint a nice shadow to the
00208 // bottom/right of the canvas.

Généré le Wed Nov 22 23:40:59 2006 pour KPlato par  doxygen 1.5.1-p1