Commit 48c44648 authored by Thomas Zander's avatar Thomas Zander

Add new KoShapeSelector class which will work in close cooperation with

the KoCreateShapesTool so the user can visually select what it is that
the tool will create.

svn path=/trunk/koffice/; revision=551632
parent 5843a8ed
......@@ -36,7 +36,7 @@ kde4_automoc(${flake_SRCS})
kde4_add_library(flake SHARED ${flake_SRCS})
target_link_libraries(flake ${KDE4_KDECORE_LIBS} ${KDE4_KDE3SUPPORT_LIBS} ${KDE4_KDEUI_LIBS})
target_link_libraries(flake ${KDE4_KDECORE_LIBS} ${KDE4_KDE3SUPPORT_LIBS} ${KDE4_KDEUI_LIBS} kofficecore)
set_target_properties(flake PROPERTIES VERSION ${GENERIC_KOFFICE_LIB_VERSION} SOVERSION ${GENERIC_KOFFICE_LIB_SOVERSION} )
......
......@@ -87,7 +87,7 @@ public:
/**
* The undo / redo command for shape sizing.
* @param shapes all the shapes that will be rezised at the same time
* @parem previousSizes the old sizes; in a list with a member for each shape
* @param previousSizes the old sizes; in a list with a member for each shape
* @param newSizes the new sizes; in a list with a member for each shape
*/
KoShapeSizeCommand(const KoSelectionSet &shapes, QList<QSizeF> &previousSizes, QList<QSizeF> &newSizes);
......
......@@ -24,6 +24,8 @@
#include "KoCommand.h"
#include "KoCanvasBase.h"
#include "KoProperties.h"
#include <kdebug.h>
KoCreateShapeStrategy::KoCreateShapeStrategy( KoCreateShapesTool *tool, KoCanvasBase *canvas, const QPointF &clicked)
......@@ -33,14 +35,19 @@ KoCreateShapeStrategy::KoCreateShapeStrategy( KoCreateShapesTool *tool, KoCanvas
}
KCommand* KoCreateShapeStrategy::createCommand() {
KoShapeFactory *factory = KoShapeRegistry::instance()->get(
static_cast<KoCreateShapesTool*>(m_parent)->shapeId());
KoCreateShapesTool *parent = static_cast<KoCreateShapesTool*>(m_parent);
KoShapeFactory *factory = KoShapeRegistry::instance()->get(parent->shapeId());
if(! factory) {
kWarning(30001) << "Application requested a shape that is not registred '" <<
static_cast<KoCreateShapesTool*>(m_parent)->shapeId() << "'" << endl;
return 0;
}
KoShape *shape = factory->createDefaultShape();
const KoProperties *props = parent->shapeProperties();
KoShape *shape;
if(props)
shape = factory->createShape(props);
else
shape = factory->createDefaultShape();
QRectF rect = selectRect();
shape->setPosition(rect.topLeft());
QSizeF newSize = rect.size();
......
......@@ -80,9 +80,22 @@ public:
*/
const QString &shapeId() const { return m_shapeId; }
/**
* Set the shape properties that the create tool will use for the next shape it will
* create.
* @param properties the properties or 0 if the default shape should be created.
*/
void setShapeProperties(KoProperties *properties) { m_newShapeProperties = properties; }
/**
* return the properties to be used for creating the next shape
* @return the properties to be used for creating the next shape
*/
KoProperties const * shapeProperties() { return m_newShapeProperties; }
private:
KoShapeControllerBase *m_shapeController;
QString m_shapeId;
KoProperties *m_newShapeProperties;
};
#endif
......@@ -32,7 +32,7 @@ KoShape * KoPathShapeFactory::createDefaultShape() {
return new KoPathShape();
}
KoShape * KoPathShapeFactory::createShape(KoProperties * params) const {
KoShape * KoPathShapeFactory::createShape(const KoProperties * params) const {
Q_UNUSED(params);
return new KoPathShape();
}
......
......@@ -32,7 +32,7 @@ public:
KoPathShapeFactory(QObject *parent, const QStringList&);
~KoPathShapeFactory() {}
KoShape * createDefaultShape();
KoShape * createShape(KoProperties * params) const;
KoShape * createShape(const KoProperties * params) const;
QWidget * optionWidget() { return 0; }
};
......
......@@ -16,6 +16,8 @@
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <KoProperties.h>
#include "KoRectangleShapeFactory.h"
#include "KoRectangleShape.h"
......@@ -26,6 +28,23 @@ KoRectangleShapeFactory::KoRectangleShapeFactory(QObject *parent, const QStringL
{
setToolTip(i18n("A simple square shape"));
// XXX: Add a nice icon using the KIconLoader
KoShapeTemplate t;
t.name = "Red Square";
t.description = "A Red Square that is very pretty";
t.toolTip = "Nicely colored square";
KoProperties *props = new KoProperties();
t.properties = props;
props->setProperty("fill", "red");
addTemplate(t);
t.name = "Blue Square";
t.description = "A Cold Blue Square";
t.toolTip = "Coldly colored square";
props = new KoProperties();
t.properties = props;
props->setProperty("fill", "blue");
addTemplate(t);
}
......@@ -36,6 +55,11 @@ KoShape * KoRectangleShapeFactory::createDefaultShape() {
return s;
}
KoShape * KoRectangleShapeFactory::createShape(KoProperties * params) const {
return new KoRectangleShape();
KoShape * KoRectangleShapeFactory::createShape(const KoProperties * params) const {
KoRectangleShape *shape = new KoRectangleShape();
if(params->getProperty("fill") == "red")
shape->setBackground(QBrush(Qt::red));
if(params->getProperty("fill") == "blue")
shape->setBackground(QBrush(Qt::blue));
return shape;
}
......@@ -31,7 +31,7 @@ public:
KoRectangleShapeFactory(QObject *parent, const QStringList&);
~KoRectangleShapeFactory() {}
KoShape * createDefaultShape();
KoShape * createShape(KoProperties * params) const;
KoShape * createShape(const KoProperties * params) const;
QWidget * optionWidget() { return 0; }
};
......
......@@ -42,7 +42,8 @@ const QString& KoShapeFactory::name() const {
return m_name;
}
void KoShapeFactory::addTemplate(KoShapeTemplate * params) {
void KoShapeFactory::addTemplate(KoShapeTemplate params) {
params.id = shapeId();
m_templates.append(params);
}
......
......@@ -38,6 +38,7 @@ class KoProperties;
* and a pixmap for use in the user interface.
*/
struct FLAKE_EXPORT KoShapeTemplate {
QString id; ///< The id of the shape
QString name; ///< The name to be shown for this template
QString description;///< The longer description to explain what this template is for
QString toolTip; ///< The tooltip text for the template
......@@ -99,7 +100,7 @@ public:
* @return a new shape
* @see KoShapeTemplate::properties
*/
virtual KoShape * createShape(KoProperties * params) const = 0;
virtual KoShape * createShape(const KoProperties * params) const = 0;
virtual QWidget * optionWidget() = 0;
/**
......@@ -111,7 +112,7 @@ public:
* @return the id for the shape this factory creates.
*/
const QString & shapeId() const;
const QList<KoShapeTemplate*> templates() const { return m_templates; }
const QList<KoShapeTemplate> templates() const { return m_templates; }
/**
* return a translated tooltip Text for a selector of shapes
* @return a translated tooltip Text
......@@ -130,7 +131,7 @@ public:
protected:
void addTemplate(KoShapeTemplate * params);
void addTemplate(KoShapeTemplate params);
/**
* Set the tooltip to be used for a selector of shapes
* @param tooltip the tooltip
......@@ -145,7 +146,7 @@ protected:
private:
QList<KoShapeTemplate*> m_templates;
QList<KoShapeTemplate> m_templates;
QString m_tooltip;
QPixmap m_icon;
const QString m_id, m_name;
......
......@@ -27,13 +27,15 @@
#include <QPointF>
#include <koffice_export.h>
class KoCanvasBase;
class KoTool;
/**
* Implements the Move action on an object or selected objects.
*/
class KoShapeMoveStrategy : public KoInteractionStrategy
class FLAKE_EXPORT KoShapeMoveStrategy : public KoInteractionStrategy
{
public:
/**
......
......@@ -85,7 +85,7 @@ public:
painter.setClipRect(ev->rect());
m_shapeManager->paint( painter, *(viewConverter()), false );
m_tool->paint( painter, *m_doc );
m_tool->paint( painter, *(viewConverter()) );
painter.end();
}
......
......@@ -40,6 +40,7 @@ set(kofficeui_LIB_SRCS
KoPageLayoutHeader.cpp
KoPen.cpp
KoZoomMode.cpp
KoShapeSelector.cpp
KoToolManager.cpp
KoToolRegistry.cpp
KoGeneralPropertyUi.cpp )
......
/*
* Copyright (c) 2006 Thomas Zander <zander@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "KoShapeSelector.h"
#include <KoShapeManager.h>
#include <KoPointerEvent.h>
//#include <KoTool.h>
#include <KoShapeRegistry.h>
#include <KoToolManager.h>
#include <KoShapeFactory.h>
#include <KoShape.h>
#include <KoInteractionTool.h>
#include <KoShapeMoveStrategy.h>
#include <KoCanvasController.h>
#include <QKeyEvent>
#include <QPainter>
#include <QMouseEvent>
#include <kdebug.h>
#include <kiconloader.h>
// ******** IconShape *********
/// \internal
class IconShape : public KoShape {
public:
IconShape(KoShapeTemplate shapeTemplate) {
m_icon = KIconLoader::unknown();
resize(m_icon.size());
m_shapeTemplate = shapeTemplate;
}
void paint(QPainter &painter, KoViewConverter &converter) {
Q_UNUSED(converter);
painter.drawPixmap(QRect( QPoint(0,0), m_icon.size()), m_icon);
}
KoShapeTemplate const *shapeTemplate() {
return &m_shapeTemplate;
}
private:
QPixmap m_icon;
KoShapeTemplate m_shapeTemplate;
};
// ******* MoveTool *********
/// \internal
class MoveTool : public KoInteractionTool {
public:
MoveTool(KoCanvasBase *canvas) : KoInteractionTool(canvas) {};
void mousePressEvent( KoPointerEvent *event ) {
KoShape *clickedShape = m_canvas->shapeManager()->getObjectAt(event->point);
repaintDecorations();
m_canvas->shapeManager()->selection()->deselectAll();
if(clickedShape) {
m_canvas->shapeManager()->selection()->select(clickedShape);
}
m_currentStrategy = new KoShapeMoveStrategy(this, m_canvas, event->point);
}
};
// ************** KoShapeSelector ************
KoShapeSelector::KoShapeSelector(QWidget *parent, KoCanvasController *cc, QString regExp)
: QWidget(parent)
, m_canvasController(cc)
{
m_canvas = new Canvas(this);
m_tool = new MoveTool(m_canvas);
m_shapeManager = new KoShapeManager(m_canvas);
setMinimumSize(20, 200);
setAutoFillBackground(true);
setBackgroundRole(QPalette::Base);
foreach(KoID id, KoShapeRegistry::instance()->listKeys()) {
KoShapeFactory *factory = KoShapeRegistry::instance()->get(id);
foreach(KoShapeTemplate shapeTemplate, factory->templates()) {
//IconShape *shape = new IconShape(shapeTemplate->pixmap); TODO
IconShape *shape = new IconShape(shapeTemplate);
add(shape);
}
}
connect(m_shapeManager->selection(), SIGNAL(selectionChanged()), this, SLOT(itemSelected()));
}
KoShapeSelector::~KoShapeSelector() {
delete m_shapeManager;
delete m_canvas;
}
void KoShapeSelector::itemSelected() {
QList<KoShape*> allSelected = m_shapeManager->selection()->selectedObjects().toList();
if(allSelected.isEmpty())
return;
IconShape *icon = static_cast<IconShape*> (allSelected.first());
KoCreateShapesTool *tool = KoToolManager::instance()->shapeCreatorTool(m_canvasController->canvas());
tool->setShapeId(icon->shapeTemplate()->id);
tool->setShapeProperties(icon->shapeTemplate()->properties);
}
void KoShapeSelector::add(KoShape *shape) {
int x=5, y=5; // 5 = gap
int w = (int) shape->size().width();
bool ok=true; // lets be optimistic ;)
do {
int rowHeight=0;
ok=true;
foreach(const KoShape *shape, m_shapeManager->objects()) {
rowHeight = qMax(rowHeight, qRound(shape->size().height()));
x = qMax(x, qRound(shape->position().x() + shape->size().width()) + 5); // 5=gap
if(x + w > width()) {
y += rowHeight + 5; // 5 = gap
ok=false;
break;
}
}
} while(! ok);
shape->setPosition(QPointF(x, y));
m_shapeManager->add(shape);
}
// event handlers
void KoShapeSelector::mouseMoveEvent(QMouseEvent *e) {
KoPointerEvent ev(e, QPointF( m_canvas->viewConverter()->viewToNormal(e->pos()) ));
m_tool->mouseMoveEvent( &ev );
}
void KoShapeSelector::mousePressEvent(QMouseEvent *e) {
KoPointerEvent ev(e, QPointF( m_canvas->viewConverter()->viewToNormal(e->pos()) ));
m_tool->mousePressEvent( &ev );
}
void KoShapeSelector::mouseReleaseEvent(QMouseEvent *e) {
KoPointerEvent ev(e, QPointF( m_canvas->viewConverter()->viewToNormal(e->pos()) ));
m_tool->mouseReleaseEvent( &ev );
}
void KoShapeSelector::keyReleaseEvent (QKeyEvent *e) {
m_tool->keyReleaseEvent(e);
}
void KoShapeSelector::keyPressEvent( QKeyEvent *e ) {
m_tool->keyPressEvent(e);
}
void KoShapeSelector::paintEvent(QPaintEvent * ev) {
QPainter painter( this );
painter.setRenderHint(QPainter::Antialiasing);
painter.setClipRect(ev->rect());
m_shapeManager->paint( painter, *(m_canvas->viewConverter()), false );
painter.end();
}
// ************ DummyViewConverter **********
QPointF KoShapeSelector::DummyViewConverter::normalToView (const QPointF &normalPoint) {
return normalPoint;
}
QPointF KoShapeSelector::DummyViewConverter::viewToNormal (const QPointF &viewPoint) {
return viewPoint;
}
QRectF KoShapeSelector::DummyViewConverter::normalToView (const QRectF &normalRect) {
return normalRect;
}
QRectF KoShapeSelector::DummyViewConverter::viewToNormal (const QRectF &viewRect) {
return viewRect;
}
void KoShapeSelector::DummyViewConverter::zoom (double *zoomX, double *zoomY) const {
*zoomX = 1.0;
*zoomY = 1.0;
}
// ********* Canvas **********
KoShapeSelector::Canvas::Canvas(KoShapeSelector *parent)
: m_parent(parent)
{
}
void KoShapeSelector::Canvas::gridSize (double *horizontal, double *vertical) const {
}
void KoShapeSelector::Canvas::updateCanvas (const QRectF &rc) {
QRect rect = rc.toRect();
rect.adjust(-2, -2, 2, 2); // grow for to anti-aliasing
m_parent->update(rect);
}
void KoShapeSelector::Canvas::addCommand (KCommand *command, bool execute) {
if(execute)
command->execute();
delete command;
};
#include "KoShapeSelector.moc"
/*
* Copyright (c) 2006 Thomas Zander <zander@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef KOSHAPESELECTOR_H
#define KOSHAPESELECTOR_H
#include <KoCanvasBase.h>
#include <KoViewConverter.h>
#include <koffice_export.h>
#include <QWidget>
#include <QRectF>
class KoTool;
class KoShape;
class KoViewConverter;
class KCommand;
class KoCanvasController;
class KoShapeManager;
class QKeyEvent;
class QPainter;
class KOFFICEUI_EXPORT KoShapeSelector : public QWidget {
Q_OBJECT
public:
KoShapeSelector(QWidget *parent, KoCanvasController *cc, QString regExp);
~KoShapeSelector();
protected: // event handlers
void mouseMoveEvent(QMouseEvent *e);
void mousePressEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *e);
void keyReleaseEvent (QKeyEvent *e);
void keyPressEvent( QKeyEvent *e );
void paintEvent(QPaintEvent * ev);
private slots:
void itemSelected();
private:
void add(KoShape *item);
private:
/// \internal
class DummyViewConverter : public KoViewConverter {
QPointF normalToView (const QPointF &normalPoint);
QPointF viewToNormal (const QPointF &viewPoint);
QRectF normalToView (const QRectF &normalRect);
QRectF viewToNormal (const QRectF &viewRect);
void zoom (double *zoomX, double *zoomY) const;
};
/// \internal
class Canvas : public KoCanvasBase {
public:
Canvas(KoShapeSelector *parent);
void gridSize (double *horizontal, double *vertical) const;
bool snapToGrid() const { return false; }
void addCommand (KCommand *command, bool execute=true);
KoShapeManager * shapeManager () const { return m_parent->m_shapeManager; }
void updateCanvas (const QRectF &rc);
KoTool *tool () { return m_parent->m_tool; }
void setTool (KoTool *tool) { Q_UNUSED(tool); }
KoViewConverter * viewConverter() { return &m_converter; }
QWidget *canvasWidget () { return m_parent; }
private:
DummyViewConverter m_converter;
KoShapeSelector *m_parent;
};
friend class Canvas;
KoShapeManager *m_shapeManager;
KoCanvasController *m_canvasController;
Canvas *m_canvas;
KoTool *m_tool;
};
#endif
......@@ -34,6 +34,7 @@ public:
void paint(QPainter &painter, KoViewConverter &converter);
private:
/// \internal
class KoTextShapePrivate {
public:
KoTextShapePrivate();
......
......@@ -19,6 +19,7 @@
#include <KoTextShapeFactory.h>
#include "KoTextShape.h"
#include "KoProperties.h"
#include <klocale.h>
#include <kgenericfactory.h>
......@@ -30,13 +31,22 @@ KoTextShapeFactory::KoTextShapeFactory(QObject *parent, const QStringList&)
: KoShapeFactory(parent, "TextShape", i18n("A shape that shows text"))
{
setToolTip(i18n("A text shape"));
KoShapeTemplate t;
t.name = "Simple text";
t.description = "Texty";
t.toolTip = "Text shape with some text";
KoProperties *props = new KoProperties();
t.properties = props;
props->setProperty("text", "<b>Koffie</b>, koffie... Querelanten\ndrinken geen KOffice maar groene thee.");
addTemplate(t);
}
KoShape *KoTextShapeFactory::createDefaultShape() {
return new KoTextShape();
}
KoShape *KoTextShapeFactory::createShape(KoProperties * params) const {
KoShape *KoTextShapeFactory::createShape(const KoProperties * params) const {
return new KoTextShape();
}
......
......@@ -35,7 +35,7 @@ public:
~KoTextShapeFactory() {}
KoShape *createDefaultShape();
KoShape *createShape(KoProperties * params) const;
KoShape *createShape(const KoProperties * params) const;
QWidget *optionWidget() { return 0; }
};
......
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