Commit d9edfd41 authored by Boudewijn Rempt's avatar Boudewijn Rempt

Add a utility class for painting a subset of shapes (used to

create thumbnails for the kopageapp and karbon document section
views)

svn path=/trunk/koffice/; revision=729985
parent 2dbff881
......@@ -21,10 +21,6 @@ set(koguiutils_LIB_SRCS
KoImageResource.cpp
KoInsertLink.cpp
KoItemToolTip.cpp
#KoPageLayoutColumns.cpp
#KoPageLayoutDia.cpp
#KoPageLayoutHeader.cpp
#KoPageLayoutSize.cpp
KoPartSelectDia.cpp
KoPartSelectAction.cpp
KoPictureFilePreview.cpp
......@@ -63,6 +59,7 @@ set(koguiutils_LIB_SRCS
KoLineStyleSelector.cpp
KoProgressUpdater.cpp
KoPrintingDialog.cpp
KoShapePainter.cpp
)
kde4_add_ui_files( koguiutils_LIB_SRCS
......@@ -96,10 +93,6 @@ install(
KoImageResource.h
KoInsertLink.h
KoItemToolTip.h
#KoPageLayoutColumns.h
#KoPageLayoutDia.h
#KoPageLayoutHeader.h
#KoPageLayoutSize.h
KoPartSelectAction.h
KoPartSelectDia.h
KoPictureFilePreview.h
......@@ -117,6 +110,7 @@ install(
KoToolBoxFactory.h
KoToolDocker.h
KoToolDockerFactory.h
KoShapePainter.h
DESTINATION
${INCLUDE_INSTALL_DIR}
)
......@@ -28,7 +28,7 @@ class KoDocumentSectionModel;
/**
* The KoDocumentSectionDelegate is the gui pendant of a
* KoDocumentSectionModel: the graphical representation one item in
* KoDocumentSectionModel: the graphical representation of one item in
* a KoDocumentSectionView.
*/
class KOGUIUTILS_EXPORT KoDocumentSectionDelegate: public QAbstractItemDelegate
......
/* This file is part of the KDE project
*
* Copyright (C) 2007 Jan Hambrecht <jaham@gmx.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "KoShapePainter.h"
#include <KoCanvasBase.h>
#include <KoShapeManager.h>
#include <KoZoomHandler.h>
#include <KoUnit.h>
#include <KoShape.h>
#include <KoShapeBorderModel.h>
#include <QtGui/QImage>
class SimpleCanvas : public KoCanvasBase
{
public:
SimpleCanvas()
: KoCanvasBase(0), m_shapeManager( new KoShapeManager( this ) )
, m_zoomHandler( new KoZoomHandler() )
{
}
~SimpleCanvas()
{
delete m_shapeManager;
delete m_zoomHandler;
}
virtual void gridSize(double *horizontal, double *vertical) const
{
if( horizontal )
*horizontal = 0;
if( vertical )
*vertical = 0;
};
virtual bool snapToGrid() const
{
return false;
}
virtual void addCommand(QUndoCommand *command) {};
virtual KoShapeManager *shapeManager() const
{
return m_shapeManager;
};
virtual void updateCanvas(const QRectF& rc) {};
virtual KoToolProxy * toolProxy() const
{
return 0;
};
virtual const KoViewConverter *viewConverter() const
{
return m_zoomHandler;
}
virtual QWidget* canvasWidget()
{
return 0;
};
virtual KoUnit unit() const
{
return KoUnit( KoUnit::Point );
}
virtual void updateInputMethodInfo() {};
private:
KoShapeManager * m_shapeManager;
KoZoomHandler * m_zoomHandler;
};
class KoShapePainter::Private
{
public:
Private()
: canvas( new SimpleCanvas() )
{
}
~Private() { delete canvas; }
SimpleCanvas * canvas;
};
KoShapePainter::KoShapePainter()
: d( new Private() )
{
}
KoShapePainter::~KoShapePainter()
{
delete d;
}
void KoShapePainter::setShapes( const QList<KoShape*> &shapes )
{
d->canvas->shapeManager()->setShapes( shapes, false );
}
void KoShapePainter::paintShapes( QPainter & painter, KoViewConverter & converter )
{
d->canvas->shapeManager()->paint( painter, converter, true );
}
bool KoShapePainter::paintShapes( QImage & image )
{
if( image.isNull() )
return false;
QRectF bound = contentRect();
QSizeF size = image.size();
KoZoomHandler zoomHandler;
// calculate the image size in document coordinates
QRectF imageBox = zoomHandler.viewToDocument( QRectF( 0, 0, size.width(), size.height() ) );
// compute the zoom factor based on the bounding rects in document coordinates
// so that the content fits into the image
double zoomW = imageBox.width() / bound.width();
double zoomH = imageBox.height() / bound.height();
double zoom = qMin( zoomW, zoomH );
// now set the zoom into the zoom handler used for painting the shape
zoomHandler.setZoom( zoom );
QPainter painter( &image );
// initialize painter
painter.setPen( QPen(Qt::NoPen) );
painter.setBrush( Qt::NoBrush );
painter.setRenderHint(QPainter::Antialiasing);
painter.setClipRect( QRectF(QPoint(),size) );
QRectF zoomedBound = zoomHandler.documentToView( bound );
QPointF offset = QPointF( 0.5 * size.width(), 0.5 * size.height() ) - zoomedBound.center();
// center content in image
painter.translate( offset.x(), offset.y() );
// finally paint the shapes
paintShapes( painter, zoomHandler );
return true;
}
QRectF KoShapePainter::contentRect()
{
QRectF bound;
foreach( KoShape * shape, d->canvas->shapeManager()->shapes() )
{
QPainterPath outline = shape->absoluteTransformation(0).map( shape->outline() );
QRectF shapeRect = outline.boundingRect();
// correct shape box with border sizes
if( shape->border() )
{
KoInsets inset;
shape->border()->borderInsets( shape, inset );
shapeRect.adjust( -inset.left, -inset.top, inset.right, inset.bottom );
}
if( bound.isEmpty() )
bound = shapeRect;
else
bound = bound.united( shapeRect );
}
return bound;
}
/* This file is part of the KDE project
*
* Copyright (C) 2007 Jan Hambrecht <jaham@gmx.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef KOSHAPEPAINTER_H
#define KOSHAPEPAINTER_H
#include "koguiutils_export.h"
#include <QtCore/QList>
#include <QtCore/QRectF>
class KoShape;
class KoViewConverter;
class QPainter;
class QImage;
/**
* A utility class to paint a subset of shapes onto a QPainter.
*/
class KOGUIUTILS_EXPORT KoShapePainter
{
public:
KoShapePainter();
~KoShapePainter();
void setShapes( const QList<KoShape*> &shapes );
/// paints the shapes on the given painter and using the zoom handler
void paintShapes( QPainter & painter, KoViewConverter & converter );
/// paints shapes to the given image, so that all shapes fit onto it
bool paintShapes( QImage & image );
/// returns the bounding rect of the shapes to paint
QRectF contentRect();
private:
class Private;
Private const * d;
};
#endif // KOSHAPEPAINTER_H
......@@ -101,7 +101,6 @@ void KoToolBox::setup() {
setWidget(widget);
layout()->setAlignment(widget, Qt::AlignLeft | Qt::AlignTop);
layout()->setMargin(0);
QList<QString> empty;
setButtonsVisible(m_canvas, empty);
}
......
......@@ -89,6 +89,8 @@ QValidator::State KoUnitDoubleSpinBox::validate(QString &input, int &pos) const
{
#ifdef DEBUG_VALIDATOR
kDebug(30004) <<"KoUnitDoubleSpinBox::validate :" << input <<" at" << pos;
#else
Q_UNUSED(pos);
#endif
QRegExp regexp ("([ a-zA-Z]+)$"); // Letters or spaces at end
......
......@@ -36,19 +36,24 @@ class QSize;
/**
* This controller class handles zoom levels for any canvas.
*
* For each KoCanvasController you should have one instance of this class to go with it. This class then creates
* a KoZoomAction and basically handles all zooming for you.
* For each KoCanvasController you should have one instance of this
* class to go with it. This class then creates a KoZoomAction and
* basically handles all zooming for you.
*
* All you need to do is connect to the setDocumentSize() slot and keep the controller up-to-date
* if your on-screen document ever changes (note that this is in document units, so this is a
* zoom independent size).
* If you choose to have zoom modes of 'page' and 'width' you are required to set the page size
* using the setPageSize() method.
* Addiotionally you can connect to the zoomChanged() signal if you want to store the latest
* zoom level and mode, for example to restore the last used one at next restart.
*
* The specialAspectMode toggle is only a UI element. It does nothing except emit the
* aspectModeChanged signal.
* All you need to do is connect to the setDocumentSize() slot and
* keep the controller up-to-date if your on-screen document ever
* changes (note that this is in document units, so this is a zoom
* independent size).
*
* If you choose to have zoom modes of 'page' and 'width' you are
* required to set the page size using the setPageSize() method.
*
* Additionally you can connect to the zoomChanged() signal if you
* want to store the latest zoom level and mode, for example to
* restore the last used one at next restart.
*
* The specialAspectMode toggle is only a UI element. It does nothing
* except emit the aspectModeChanged signal.
*
*/
class KOGUIUTILS_EXPORT KoZoomController : public QObject {
......
......@@ -52,7 +52,7 @@ void KoZoomHandler::setZoomAndResolution( int zoom, int dpiX, int dpiY )
void KoZoomHandler::setResolutionToStandard()
{
setResolution( POINT_TO_INCH( double(KoGlobal::dpiX())),
POINT_TO_INCH( double(KoGlobal::dpiY())) );
POINT_TO_INCH( double(KoGlobal::dpiY())) );
}
void KoZoomHandler::setResolution( double resolutionX, double resolutionY )
......
......@@ -77,6 +77,8 @@ public:
/**
* Set a resolution for X and Y of the output device.
* The zoom factor is not changed.
*
* XXX: Is this also in dots per inch?
*/
void setResolution( double resolutionX, double resolutionY );
......@@ -98,7 +100,7 @@ public:
* @param zoom the zoom factor (e.g. 100 for 100%)
* \deprecated
*/
void setZoom( int zoom );
KDE_DEPRECATED void setZoom( int zoom );
/**
* Change the zoom level, keeping the resolution unchanged.
* @param zoom the zoom factor (e.g. 1.0 for 100%)
......
......@@ -34,19 +34,19 @@ public:
{
ZOOM_CONSTANT = 0, ///< zoom x %
ZOOM_WIDTH = 1, ///< zoom pagewidth
ZOOM_PAGE = 2, ///< zoom to pagesize
ZOOM_PAGE = 2, ///< zoom to pagesize
ZOOM_PIXELS = 4 ///< zoom to actual pixels
};
Q_DECLARE_FLAGS(Modes, Mode)
/// \param mode the mode name
/// \return the to Mode converted QString \c mode
static Mode toMode(const QString& mode);
/// \return the to QString converted and translated Mode \c mode
static QString toString(Mode mode);
/// \param mode the mode name
/// \return true if \c mode isn't dependent on windowsize
static bool isConstant(const QString& mode)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment