modify abstracttool iface
[yamf.git] / yamf / tools / text / text.cpp
blobf3f83d394559f3f29c3f324cb70318f8bb11b779
1 /***************************************************************************
2 * Copyright (C) 2007 Jorge Cuadrado *
3 * kuadrosxx@gmail.com *
4 * *
5 * This library is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU Lesser General Public *
7 * License as published by the Free Software Foundation; either *
8 * version 2.1 of the License, or (at your option) any later version. *
9 * *
10 * This library 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 GNU *
13 * Lesser General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU Lesser General Public *
16 * License along with this library; if not, write to the Free Software *
17 * Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include "text.h"
24 #include <QPointF>
25 #include <QKeySequence>
26 #include <QKeyEvent>
27 #include <QMatrix>
28 #include <QGraphicsView>
30 #include <QGraphicsSceneMouseEvent>
31 #include <QGraphicsLineItem>
32 #include <QToolBar>
34 #include <QHBoxLayout>
35 #include <QLabel>
37 #include <dgraphics/algorithm.h>
38 #include <dcore/algorithm.h>
39 #include <dcore/debug.h>
40 #include <dgui/action.h>
41 #include <dgui/iconloader.h>
43 #include <drawing/photogram.h>
44 #include <drawing/brushmanager.h>
45 #include <drawing/paintarea.h>
47 #include <model/frame.h>
48 #include <model/command/changetext.h>
49 #include <model/command/changetextwidth.h>
50 #include <model/command/changetextfont.h>
52 #include <item/text.h>
54 #include <dgui/fontchooser.h>
57 namespace YAMF {
59 namespace Drawing {
61 namespace Tool {
63 struct Text::Private
65 YAMF::Item::Text *text;
66 QGraphicsLineItem *line;
68 QPointF position;
69 DGui::Action *action;
70 DGui::FontChooser *font;
72 bool editWidth;
73 bool added;
74 QString oldText;
75 double oldWidth;
77 PaintArea *paintArea;
80 /**
81 * @�spanish
82 * Construye la herramienta para insertar texto a la animaci�n.
84 Text::Text(QObject *parent) : AbstractTool(parent), d(new Private)
86 d->action = new DGui::Action( DGui::IconLoader::self()->load("draw-text.svg"), tr("Text"), this);
88 d->added = false;
90 d->paintArea = 0;
91 d->oldWidth = 0;
92 d->font = new DGui::FontChooser;
93 connect(d->font, SIGNAL(fontChanged()), this, SLOT(onChangeFont()));
95 d->text = 0;
97 d->editWidth = false;
102 * Destructor
104 Text::~Text()
106 delete d;
110 * @~spanish
111 * Inicializa la herramienta, evita que los items dentro de el fotograma se puedan mover o seleccionar.
113 void Text::init(Photogram *photogram)
115 foreach(QGraphicsView * view, photogram->views())
117 view->setDragMode ( QGraphicsView::NoDrag );
119 if ( QGraphicsScene *photogram = qobject_cast<QGraphicsScene *>(view->scene()) )
121 foreach(QGraphicsItem *item, photogram->items() )
123 item->setFlag(QGraphicsItem::ItemIsSelectable, false);
124 item->setFlag(QGraphicsItem::ItemIsMovable, false);
127 if(YAMF::Item::Text *text = static_cast< YAMF::Item::Text * >(item))
129 text->setFlags( QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable );
137 * @~spanish
138 * Retorna el id de la herramienta.
140 QString Text::id() const
142 return tr("Text");
146 * @~spanish
147 * Funci�n sobrecargada para implementar la inserci�n de texto
149 void Text::press(const QGraphicsSceneMouseEvent *input)
151 PaintArea *paintArea = this->paintArea();
152 if(input->buttons() == Qt::LeftButton)
154 QList<QGraphicsItem * > items = paintArea->scene()->selectedItems();
156 foreach(QGraphicsItem *item, items)
158 if(item/* && item != d->text*/)
160 if( paintArea->currentFrame()->visualIndexOf(item) != -1)
162 YAMF::Item::Text *text = static_cast< YAMF::Item::Text * >(item);
164 if(text)
166 if(d->text)
168 d->text->setEditable(false);
171 d->text = text;
172 d->position = d->text->scenePos();
173 d->editWidth = true;
174 d->oldWidth = d->text->textWidth();
175 d->added = true;
176 return;
182 d->text = new Item::Text();
184 d->added = false;
185 connect(d->text, SIGNAL(edited()), this, SLOT(
186 () ));
187 d->oldText = "";
188 d->editWidth = false;
189 d->text->setFlags( QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable );
190 d->paintArea = paintArea;
192 d->position = input->scenePos();
193 d->text->setPos(d->position);
194 d->text->setFont(d->font->currentFont());
196 d->text->setDefaultTextColor(paintArea->brushManager()->penBrush().color());
198 d->line = new QGraphicsLineItem;
199 d->line->setPen(QPen(Qt::red, 2));
200 paintArea->photogram()->addItem(d->line);
205 * @~spanish
206 * Funci�n sobrecargada para implementar la inserci�n de texto
208 void Text::move(const QGraphicsSceneMouseEvent *input)
210 if(input->buttons() == Qt::LeftButton)
212 if(d->text)
214 if(d->editWidth)
216 double width = std::abs(d->position.x() - input->scenePos().x());
217 d->text->setTextWidth( width );
219 else
221 d->line->setLine(QLineF(d->position, QPointF(d->line->mapFromScene(input->scenePos()).x(), d->position.y())));
228 * @~spanish
229 * Funci�n sobrecargada para implementar la inserci�n de texto
231 void Text::release(const QGraphicsSceneMouseEvent *input)
233 PaintArea *paintArea = this->paintArea(); // FIXME
235 if(!d->paintArea)
237 d->paintArea = paintArea;
240 if(!d->editWidth)
242 if(d->text)
244 double width = std::abs(d->position.x() - input->scenePos().x());
245 d->text->setTextWidth( width );
246 paintArea->photogram()->removeItem(d->line);
248 paintArea->scene()->addItem(d->text);
249 d->text->setEditable(true);
251 if(d->position.x() > input->scenePos().x())
252 d->text->setPos(d->text->scenePos() - QPointF(d->text->textWidth(), 0));
254 d->editWidth = true;
257 else
259 paintArea->addCommand(new Command::ChangeTextWidth(paintArea->currentFrame(), d->text, d->oldWidth));
260 d->oldWidth = d->text->textWidth();
265 * @~spanish
266 * Retorna la acci�n que representa la herramienta.
268 DGui::Action *Text::action() const
270 return d->action;
274 * @~spanish
275 * Retorna que la herramienta es de tipo Brush.
277 int Text::type() const
279 return AbstractTool::Brush;
282 void Text::aboutToChangeTool()
284 Q_CHECK_PTR(d->paintArea);
285 Q_CHECK_PTR(d->text);
286 if(d->paintArea && d->text)
288 if(!d->text->toPlainText().isEmpty())
290 d->text->setEditable(false);
291 onEdited();
298 * @~spanish
299 * Asigna la interfaz gráfica para configurar el tipo de letra del texto.
301 void Text::setupConfigBar(QToolBar *configBar)
303 configBar->addWidget(d->font)->setVisible(true);
307 * @~spanish
308 * Ignora el evento de presionado de teclas para poder escribir el esto.
310 void Text::keyPressEvent(QKeyEvent *event)
312 event->ignore();
316 * @internal
317 * @~spanish
318 * Cambia el tipo de letra que se seleccione en el configurador.
320 void Text::onChangeFont()
322 if(d->text)
324 QFont oldFont = d->text->font();
326 d->text->setFont(d->font->currentFont());
328 d->text->setEditable(true);
329 d->text->setFocus();
330 d->text->setSelected(true);
332 if(d->added && oldFont != d->text->font())
334 d->paintArea->addCommand(new Command::ChangeTextFont( d->paintArea->currentFrame(), d->text, oldFont));
339 void Text::onEdited()
341 if(d->text && d->paintArea)
343 if(d->oldText.isEmpty())
345 d->paintArea->currentFrame()->addItem(d->text);
346 d->added = true;
348 else
350 d->paintArea->addCommand(new Command::ChangeText( d->paintArea->currentFrame(), d->text, d->oldText));
352 d->oldText = d->text->toPlainText();