Modified text and path XML format
[yamf.git] / yamf / item / text.cpp
blob9afa73b344ce0df7b9dc694e51428f2b29b67230
1 /***************************************************************************
2 * Copyright (C) 2006 by David Cuadrado *
3 * krawek@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "text.h"
23 #include "serializer.h"
25 #include <QFont>
26 #include <QFocusEvent>
27 #include <QTimer>
28 #include <QPainter>
29 #include <QGraphicsSceneMouseEvent>
31 #include <dcore/debug.h>
33 namespace YAMF {
34 namespace Item {
36 struct Text::Private {
37 Private() : isEditable(false) {}
39 GraphicsItemFlags flags;
40 bool isEditable;
43 /**
44 * @~spanish
45 * Construye un texto con un �tem "padre" @p parent y dentro de la escena @p scene.
47 Text::Text(QGraphicsItem * parent, QGraphicsScene * scene)
48 : QGraphicsTextItem(parent, scene), d(new Private)
50 d->flags = flags();
51 setOpenExternalLinks(true);
52 setEditable( false );
56 /**
57 * Destructor
59 Text::~Text()
61 delete d;
64 /**
65 * @~spanish
66 * Toma los valores dentro del documento xml @p xml y se las asigna a las propiedades del texto.
68 void Text::fromXml(const QString &xml)
73 /**
74 * @~spanish
75 * Crea un elemento del documento xml @p doc con las propiedades actuales del texto.
77 QDomElement Text::toXml(QDomDocument &doc) const
79 QDomElement root = doc.createElement("text");
80 root.setAttribute("width", textWidth() );
81 root.setAttribute("text-color", defaultTextColor().name() );
83 root.setAttribute("data", toPlainText() );
85 root.appendChild( Serializer::properties( this, doc));
86 QFont font = this->font();
87 root.appendChild( Serializer::font( font, doc ) );
89 return root;
92 /**
93 * @~spanish
94 * Si @p editable es verdadero permitira editar el texto, de lo contrario no.
96 void Text::setEditable(bool editable)
98 d->isEditable = editable;
100 if ( editable )
102 d->flags = flags(); // save flags
103 setTextInteractionFlags(Qt::TextEditorInteraction);
104 setFlags( QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable );
105 setFocus(Qt::MouseFocusReason);
107 else
109 setTextInteractionFlags(Qt::NoTextInteraction/*Qt::TextBrowserInteraction*/);
110 // setFlags( QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable ); // restore flags
112 update();
115 void Text::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget * widget)
117 if(toPlainText().isEmpty())
119 painter->drawRect(boundingRect());
121 QGraphicsTextItem::paint(painter, option, widget);
125 * @~spanish
126 * Si el item es editable lo cambia a no editable, y si es no editable lo cambia a editable.
128 void Text::toggleEditable()
130 setEditable( !d->isEditable );
134 * @~spanish
135 * Desactiva la edicion del texto cuando pierde el foco.
137 void Text::focusOutEvent(QFocusEvent * event )
139 QGraphicsTextItem::focusOutEvent(event);
140 if ( textInteractionFlags() & Qt::TextEditorInteraction && d->isEditable )
142 QTimer::singleShot( 0, this, SLOT(toggleEditable()));
143 emit edited();
148 * @~spanish
149 * Activa la edicion del texto cuando el texto recibe un evento de doble click.
151 void Text::mouseDoubleClickEvent ( QGraphicsSceneMouseEvent * event )
153 setEditable( true );
156 void Text::mousePressEvent ( QGraphicsSceneMouseEvent * event )
158 QGraphicsTextItem::mousePressEvent( event );
159 // event->setAccepted( false );
160 // dfDebug << event->isAccepted();
161 // event->ignore();