Commit fe993130 authored by Thomas Zander's avatar Thomas Zander

* Allow KWCanvas to get keyboard input

* Refactor to avoid using KoTextShape directly (which we can't since its
  in a plugin)
* Add KoTextTool to allow editing of the text frames in KWord :)
* Provide KoShape::shapeId() setter/getter
* Provide a 'userData' class per KoShape which the application + plugin
  can implement to store data in there since you can't use a class in a
  plugin as well as in an application.
* Various api docs fixes cleanups
* Alter the ToolFactory to use a string instead of a QPixmap for the icon.

svn path=/trunk/koffice/; revision=555165
parent fee16f14
......@@ -30,6 +30,7 @@ set(flake_SRCS
KoRectangleShapeFactory.cpp
KoPathShapeFactory.cpp
KoShapeFactory.cpp
KoShapeUserData.cpp
)
kde4_automoc(${flake_SRCS})
......
......@@ -174,4 +174,11 @@ bool KoSelection::isSelected(const KoShape *object) const {
return false;
}
KoShape *KoSelection::firstSelectedObject(KoFlake::SelectionType strip) const {
KoSelectionSet set = selectedObjects(strip);
if(set.isEmpty())
return 0;
return *(set.begin());
}
#include "KoSelection.moc"
......@@ -69,6 +69,12 @@ public:
* of a grouped object if the group-parent is itself also in the set.
*/
virtual const KoSelectionSet selectedObjects(KoFlake::SelectionType strip = KoFlake::FullSelection) const;
/**
* Return the first selected object, or 0 if there is nothing selected.
* @param strip if StrippedSelection, the returned list will not include any children
* of a grouped object if the group-parent is itself also in the set.
*/
KoShape *firstSelectedObject(KoFlake::SelectionType strip = KoFlake::FullSelection) const;
/// return if the object is selected
virtual bool isSelected(const KoShape *object) const;
/// return the selection count
......
......@@ -26,9 +26,11 @@
#include "KoPointerEvent.h"
#include "KoInsets.h"
#include "KoShapeBorderModel.h"
#include "KoShapeUserData.h"
#include <QPainter>
#include <QtDebug>
#include <QVariant>
#include <QPainterPath>
KoShape::KoShape()
......@@ -47,12 +49,14 @@ KoShape::KoShape()
, m_locked( false )
, m_keepAspect( false )
, m_repaintManager(0)
, m_userData(0)
{
recalcMatrix();
}
KoShape::~KoShape()
{
delete m_userData;
}
void KoShape::paintDecorations(QPainter &painter, KoViewConverter &converter, bool selected) {
......@@ -301,6 +305,16 @@ void KoShape::moveBy(double distanceX, double distanceY) {
setAbsolutePosition(QPointF(p.x() + distanceX, p.y() + distanceY));
}
void KoShape::setUserData(KoShapeUserData *userData) {
if(m_userData)
delete m_userData;
m_userData = userData;
}
KoShapeUserData *KoShape::userData() const {
return m_userData;
}
// static
void KoShape::applyConversion(QPainter &painter, const KoViewConverter &converter) {
double zoomX, zoomY;
......
......@@ -32,12 +32,14 @@
class QPainter;
class QRectF;
class QPainterPath;
class QVariant;
class KoSelection;
class KoPointerEvent;
class KoShapeContainer;
class KoRepaintManager;
class KoShapeBorderModel;
class KoShapeUserData;
/**
*
......@@ -429,6 +431,32 @@ public:
*/
void moveBy(double distanceX, double distanceY);
/**
* Set a data object on the shape to be used by an application.
* This is specifically usefull when a shape is created in a plugin and that data from that
* shape should be accessible outside the plugin.
* @param userData the new user data, or 0 to delete the current one.
*/
void setUserData(KoShapeUserData *userData);
/**
* Return the current userData.
*/
KoShapeUserData *userData() const;
/**
* Return the Id of this shape, indentifying the type of shape by the id of the factory.
* @see KoShapeFactory::shapeId()
* @return the id of the shape-type
*/
const QString & shapeId() const { return m_shapeId; }
/**
* Set the Id of this shape. A shapeFactory is expected to set the Id at creation
* so applications can find out what kind of shape this is.
* @see KoShapeFactory::shapeId()
* @param id the ID from the factory that created this shape
*/
void setShapeId(const QString &id) { m_shapeId = id; }
protected:
QMatrix m_invMatrix; ///< The inverted matrix; for convenience
QBrush m_backgroundBrush; ///< Stands for the background color / fill etc.
......@@ -460,6 +488,7 @@ private:
QSizeF m_size; // size in pt
QPointF m_pos; // position (top left) in pt
QString m_shapeId;
QMatrix m_matrix;
......@@ -476,6 +505,7 @@ private:
friend class KoShapeManager;
friend class KoShapeContainer;
void setRepaintManager(KoRepaintManager *manager);
KoShapeUserData *m_userData;
/**
* Create a matrix that describes all the transformations done on this shape.
......
......@@ -87,7 +87,7 @@ public:
* This method should be implemented by factories to create a shape that the user
* gets when doing a base insert. For example from a script. The created shape
* should have its values set to good defaults that the user can then adjust further if
* needed.
* needed. Including the KoShape:setShapeId(), with the Id from this factory
* The default shape position is not relevant, it will be moved by the caller.
* @return a new shape
*/
......@@ -95,6 +95,7 @@ public:
/**
* This method should be implemented by factories to create a shape based on a set of
* properties that are specifically made for this shape-type.
* This method should also set this factories shapeId on the shape using KoShape::setShapeId()
* @return a new shape
* @see KoShapeTemplate::properties
*/
......@@ -110,6 +111,10 @@ public:
* @return the id for the shape this factory creates.
*/
const QString & shapeId() const;
/**
* Return all the templates this factory knows about.
* Each template shows a different way to create a shape this factory is specialized in.
*/
const QList<KoShapeTemplate> templates() const { return m_templates; }
/**
* return a translated tooltip Text for a selector of shapes
......@@ -117,8 +122,8 @@ public:
*/
const QString & toolTip() const;
/**
* return an icon for this tool for a selector of shapes
* @return an icon for this tool
* return the basename of i the icon for this tool for a selector of shapes
* @return the basename of the icon for this selector of shapes
*/
const QString & icon() const;
/**
......@@ -129,6 +134,11 @@ public:
protected:
/**
* Add a template with the properties of a speficic type of shape this factory can generate
* using the createShape() method.
* @param params the new template this factory knows to produce
*/
void addTemplate(KoShapeTemplate params);
/**
* Set the tooltip to be used for a selector of shapes
......
/* This file is part of the KDE project
* Copyright (C) 2006 Thomas Zander <zander@kde.org>
*
* 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 "KoShapeUserData.moc"
/* This file is part of the KDE project
* Copyright (C) 2006 Thomas Zander <zander@kde.org>
*
* 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 KOSHAPEUSERDATA_H
#define KOSHAPEUSERDATA_H
#include <QObject>
#include <koffice_export.h>
/**
* The KoShapeUserData class is used to associate custom data with a shape.
*
* KoShapeUserData provides an abstract interface for container classes
* that are used to associate application-specific user data with shapes in KoShape
* Generally, subclasses of this class provide functions to allow data to
* be stored and retrieved, and instances are attached to KoShape using
* KoShape::setUserData(). This makes it possible to store additional data per
* shape in a way that allows applications to not know the implementation of a
* specific KoShape extending class.
* Each subclass should provide a reimplementation of the destructor to ensure that
* any private data is automatically cleaned up when user data objects are deleted.
* Please note that this object is a QObject to allow a
* qobject_cast<MyData*> (shape->userData()) to work which is usefull in an environment
* where classes from plugins may not be castable using a static_cast or a dynamic_cast
*/
class FLAKE_EXPORT KoShapeUserData : public QObject {
Q_OBJECT
public:
/// Constructor
KoShapeUserData() {}
virtual ~KoShapeUserData() {}
};
#endif
......@@ -46,7 +46,7 @@ class FLAKE_EXPORT KoTool : public QObject
public:
/**
* Constructor, normally only called by the factory (see KoTooLFactory)
* Constructor, normally only called by the factory (see KoToolFactory)
* @param canvas the canvas interface this tool will work for.
*/
KoTool(KoCanvasBase *canvas );
......@@ -95,7 +95,7 @@ public:
/**
* Return if dragging (moving with the mouse down) to the edge of a canvas should scroll the
* canvas.
* canvas (default is true).
* @return if this tool wants mouse events to cause scrolling of canvas.
*/
virtual bool wantsAutoScroll();
......
......@@ -46,7 +46,7 @@ const QString& KoToolFactory::toolTip() const {
return m_tooltip;
}
const QPixmap& KoToolFactory::icon() const {
const QString& KoToolFactory::icon() const {
return m_icon;
}
......@@ -66,7 +66,7 @@ void KoToolFactory::setToolType(const QString & toolType) {
m_toolType = toolType;
}
void KoToolFactory::setIcon(const QPixmap & icon) {
void KoToolFactory::setIcon(const QString & icon) {
m_icon = icon;
}
......
......@@ -26,7 +26,6 @@
#include <klocale.h>
#include <QObject>
#include <QPixmap>
/**
* A factory for KoTool objects.
......@@ -102,10 +101,10 @@ public:
*/
const QString &toolTip() const;
/**
* return an icon for this tool
* @return an icon for this tool
* return the basename of the icon for this tool
* @return the basename of the icon for this tool
*/
virtual const QPixmap& icon() const;
const QString& icon() const;
/**
* Return the id of the shape we can process.
* This is the shape ID the tool we create is associated with. So a TextTool for a TextShape.
......@@ -126,10 +125,10 @@ protected:
void setToolType(const QString & toolType);
/**
* Set an icon to be used in the toolBox.
* @param icon the pixmap from the iconLoader
* @param iconName the basename (without extention) of the icon
* @see KIconLoader
*/
void setIcon(const QPixmap & icon);
void setIcon(const QString & iconName);
/**
* Set the priority of this tool, as it is shown in the toolBox; lower number means
* it will be show more to the front of the list.
......@@ -149,10 +148,9 @@ private:
QString m_toolType;
QString m_tooltip;
QString m_activationId;
QPixmap m_icon;
QString m_icon;
const QString m_name, m_id;
int m_priority;
};
#endif
......@@ -27,6 +27,7 @@
#include <KoInteractionToolFactory.h>
#include <KoShapeControllerBase.h>
#include <KoCanvasController.h>
#include <KoShapeRegistry.h>
#include "kactioncollection.h"
#include "kdebug.h"
......@@ -72,6 +73,7 @@ void KoToolManager::setup() {
m_defaultTool = new ToolHelper(new KoInteractionToolFactory(this, QStringList()));
m_tools.append(m_defaultTool);
KoShapeRegistry::instance();
KoToolRegistry *registry = KoToolRegistry::instance();
foreach(KoID id, registry->listKeys()) {
ToolHelper *t = new ToolHelper(registry->get(id));
......
......@@ -19,8 +19,6 @@ set(libkohyphen_SRCS
# KoComplexText.cpp not compiled
set(kotext_LIB_SRCS ${libkohyphen_SRCS}
KoTextShape.cpp
KoTextShapeFactory.cpp
KoRichText.cpp
KoTextZoomHandler.cpp
KoBorder.cpp
......@@ -67,6 +65,7 @@ set(kotext_LIB_SRCS ${libkohyphen_SRCS}
KoParagStyle.cpp
KoTextBookmark.cpp
KoBorderPreview.cpp
KoTextShapeData.cpp
)
kde4_automoc(${kotext_LIB_SRCS})
......@@ -89,7 +88,7 @@ kde4_add_ui3_files(kotext_LIB_SRCS
kde4_add_library(kotext SHARED ${kotext_LIB_SRCS})
target_link_libraries(kotext ${KDE4_KSPELL2_LIBS} kutils kofficecore kofficeui )
target_link_libraries(kotext ${KDE4_KSPELL2_LIBS} flake kutils kofficecore kofficeui )
set_target_properties(kotext PROPERTIES VERSION ${GENERIC_KOFFICE_LIB_VERSION} SOVERSION ${GENERIC_KOFFICE_LIB_SOVERSION} )
install_targets(${LIB_INSTALL_DIR} kotext )
......@@ -99,6 +98,8 @@ install_targets(${LIB_INSTALL_DIR} kotext )
set(kotext2_PART_SRCS
KoTextShape.cpp
KoTextShapeFactory.cpp
KoTextTool.cpp
KoTextToolFactory.cpp
)
kde4_automoc(${kotext2_PART_SRCS})
......@@ -107,7 +108,7 @@ kde4_add_plugin(kotext2 ${kotext2_PART_SRCS})
kde4_install_libtool_file( ${PLUGIN_INSTALL_DIR} kotext2 )
target_link_libraries(kotext2 flake)
target_link_libraries(kotext2 kotext flake kofficeui)
install_targets(${PLUGIN_INSTALL_DIR} kotext2 )
......
......@@ -28,13 +28,12 @@
// ############ KoTextShape ################
KoTextShape::KoTextShape()
: m_document(0)
{
setDocument(new QTextDocument());
m_textShapeData = new KoTextShapeData();
setUserData(m_textShapeData);
}
KoTextShape::~KoTextShape() {
delete m_document;
}
void KoTextShape::paint(QPainter &painter, KoViewConverter &converter) {
......@@ -43,21 +42,7 @@ void KoTextShape::paint(QPainter &painter, KoViewConverter &converter) {
QAbstractTextDocumentLayout::PaintContext pc;
pc.cursorPosition = -1;
m_document->setPageSize(size());
m_document->documentLayout()->draw( &painter, pc);
}
void KoTextShape::setDocument(QTextDocument *document) {
Q_ASSERT(document);
delete m_document;
m_document = document;
// The following avoids the normal case where the glyph metrices are rounded to integers and
// hinted to the screen by freetype, which you of course don't want for WYSIWYG
m_document->setUseDesignMetrics(true);
}
QTextDocument *KoTextShape::takeDocument() {
QTextDocument *doc = m_document;
m_document = 0;
return doc;
QTextDocument *doc = m_textShapeData->document();
doc->setPageSize(size());
doc->documentLayout()->draw( &painter, pc);
}
......@@ -21,8 +21,8 @@
#ifndef KOTEXTSHAPE_H
#define KOTEXTSHAPE_H
#include <koffice_export.h>
#include <KoShape.h>
#include <KoTextShapeData.h>
#include <QTextDocument>
#include <QPainter>
......@@ -31,20 +31,15 @@
#define KoTextShape_SHAPEID "TextShapeID"
class KOTEXT_EXPORT KoTextShape : public KoShape {
class KoTextShape : public KoShape {
public:
KoTextShape();
virtual ~KoTextShape();
void paint(QPainter &painter, KoViewConverter &converter);
void setDocument(QTextDocument *document);
QTextDocument *document() { return m_document; }
QTextDocument *takeDocument();
private:
QTextDocument *m_document;
KoTextShapeData *m_textShapeData;
};
#endif
/* This file is part of the KDE project
* Copyright (C) 2006 Thomas Zander <zander@kde.org>
*
* 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 "KoTextShapeData.h"
#include <QTextDocument>
KoTextShapeData::KoTextShapeData()
: m_ownsDocument(false)
{
m_document = new QTextDocument();
}
KoTextShapeData::~KoTextShapeData() {
if(m_ownsDocument)
delete m_document;
}
void KoTextShapeData::setDocument(QTextDocument *document, bool transferOwnership) {
Q_ASSERT(document);
if(m_ownsDocument)
delete m_document;
m_document = document;
// The following avoids the normal case where the glyph metrices are rounded to integers and
// hinted to the screen by freetype, which you of course don't want for WYSIWYG
m_document->setUseDesignMetrics(true);
m_ownsDocument = transferOwnership;
}
QTextDocument *KoTextShapeData::document() {
return m_document;
}
#include "KoTextShapeData.moc"
/* This file is part of the KDE project
* Copyright (C) 2006 Thomas Zander <zander@kde.org>
*
* 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 KOTEXTSHAPEDATA_H
#define KOTEXTSHAPEDATA_H
#include <KoShapeUserData.h>
#include <koffice_export.h>
class QTextDocument;
class KOTEXT_EXPORT KoTextShapeData : public KoShapeUserData {
Q_OBJECT
public:
KoTextShapeData();
~KoTextShapeData();
void setDocument(QTextDocument *document, bool transferOwnership = true);
QTextDocument *document();
private:
QTextDocument *m_document;
bool m_ownsDocument;
};
#endif
......@@ -17,9 +17,12 @@
* Boston, MA 02110-1301, USA.
*/
#include <KoTextShapeFactory.h>
#include "KoTextShape.h"
#include "KoProperties.h"
#include "KoTextShape.h"
#include "KoTextShapeData.h"
#include "KoTextToolFactory.h"
#include "KoToolRegistry.h"
#include <KoTextShapeFactory.h>
#include <klocale.h>
#include <kgenericfactory.h>
......@@ -27,7 +30,7 @@
K_EXPORT_COMPONENT_FACTORY(kotext2,
KGenericFactory<KoTextShapeFactory>( "TextShape" ) )
KoTextShapeFactory::KoTextShapeFactory(QObject *parent, const QStringList&)
KoTextShapeFactory::KoTextShapeFactory(QObject *parent, const QStringList& list)
: KoShapeFactory(parent, KoTextShape_SHAPEID, i18n("A shape that shows text"))
{
setToolTip(i18n("A text shape"));
......@@ -39,18 +42,25 @@ KoTextShapeFactory::KoTextShapeFactory(QObject *parent, const QStringList&)
t.properties = props;
props->setProperty("text", "<b>Koffie</b>, koffie... Querelanten\ndrinken geen KOffice maar groene thee.");
addTemplate(t);
// init tool factory here, since this is the only public factory in the lib
KoToolRegistry::instance()->add(new KoTextToolFactory(parent, list));
}
KoShape *KoTextShapeFactory::createDefaultShape() {
return new KoTextShape();
KoTextShape *text = new KoTextShape();
text->setShapeId(shapeId());
return text;
}
KoShape *KoTextShapeFactory::createShape(const KoProperties * params) const {
KoTextShape *shape = new KoTextShape();
shape->setShapeId(shapeId());
QTextDocument *doc = new QTextDocument();
doc->setDefaultFont(QFont("Sans", 10, QFont::Normal, false));
doc->setHtml( params->getProperty("text").toString() );
shape->setDocument(doc);
KoTextShapeData *data = static_cast<KoTextShapeData*> (shape->userData());
data->setDocument(doc);
return shape;
}
......
/* This file is part of the KDE project
* Copyright (C) 2006 Thomas Zander <zander@kde.org>
*
* 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 <KoTextTool.h>
#include <KoCanvasBase.h>
#include <KoSelection.h>
#include <KoShapeManager.h>
#include <kdebug.h>
#include <QKeyEvent>
KoTextTool::KoTextTool(KoCanvasBase *canvas)
: KoTool(canvas)
, m_textShape(0)
{
}
KoTextTool::~KoTextTool() {
}
void KoTextTool::paint( QPainter &painter, KoViewConverter &converter) {
}
void KoTextTool::mousePressEvent( KoPointerEvent *event ) {
}
void KoTextTool::mouseDoubleClickEvent( KoPointerEvent *event ) {
}
void KoTextTool::mouseMoveEvent( KoPointerEvent *event ) {
}
void KoTextTool::mouseReleaseEvent( KoPointerEvent *event ) {
}
void KoTextTool::keyPressEvent(QKeyEvent *event) {
QTextCursor::MoveOperation moveOperation = QTextCursor::NoMove;
switch(event->key()) { // map input to moveOperation
case Qt::Key_Backspace:
m_caret.deletePreviousChar();
break;
case Qt::Key_Tab:
kDebug() << "Tab key pressed";
break;
case Qt::Key_Delete:
m_caret.deleteChar();
break;
case Qt::Key_Left:
if(event->modifiers() & Qt::ControlModifier)
moveOperation = QTextCursor::WordLeft;
else
moveOperation = QTextCursor::Left;
break;
case Qt::Key_Up:
moveOperation = QTextCursor::Up;
break;
case Qt::Key_Right:
if(event->modifiers() & Qt::ControlModifier)
moveOperation = QTextCursor::NextWord;
else
moveOperation = QTextCursor::Right;
break;
case Qt::Key_Down:
moveOperation = QTextCursor::Down;
break;
case Qt::Key_PageUp:
//moveOperation = QTextCursor::Left;
break;
case Qt::Key_PageDown:
//moveOperation = QTextCursor::Left;
break;
case Qt::Key_End:
moveOperation = QTextCursor::EndOfLine;
break;
case Qt::Key_Home:
moveOperation = QTextCursor::StartOfLine;
break;
default:
m_caret.insertText(event->text());
}
m_caret.movePosition(moveOperation,
(event->modifiers() & Qt::ShiftModifier)?QTextCursor::KeepAnchor:QTextCursor::MoveAnchor);
event->accept();
m_textShape->repaint();// TODO more fine grained repainting..
}
void KoTextTool::keyReleaseEvent(QKeyEvent *event) {
event->accept();
}
void KoTextTool::activate (bool temporary) {
Q_UNUSED(temporary);
KoShape *shape = m_canvas->shapeManager()->selection()->firstSelectedObject();
m_textShape = dynamic_cast<KoTextShape*> (shape);
if(m_textShape == 0) {