modify abstracttool iface
[yamf.git] / yamf / tools / selection / contour.cpp
blobbd69d1f450bc984a209f8b1548aefa52275ab332
1 /***************************************************************************
2 * Copyright (C) 2006 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 "contour.h"
22 #include <dgui/action.h>
24 #include <drawing/photogram.h>
25 #include <drawing/paintarea.h>
26 #include <drawing/brushmanager.h>
28 #include <item/path.h>
29 #include <item/proxy.h>
30 #include <item/serializer.h>
32 #include <model/command/convertitem.h>
33 #include <model/command/editnodesitem.h>
34 #include <model/frame.h>
36 #include <QGraphicsItem>
37 #include <QGraphicsSceneMouseEvent>
39 #include <dgui/iconloader.h>
40 #include <dcore/debug.h>
41 #include "private/contoureditor.h"
43 #include <QList>
44 #include <QKeyEvent>
46 namespace YAMF {
47 namespace Drawing {
48 namespace Tool {
50 struct Contour::Private
52 DGui::Action *action;
53 QList<Drawing::Tool::Private::ContourEditor *> editors;
56 /**
57 * @~spanish
58 * Construye la herramienta para editar los nodos de un grafico.
60 Contour::Contour(QObject *parent) : Drawing::AbstractTool(parent), d(new Private)
62 d->action = new DGui::Action( DGui::IconLoader::self()->load("edit-path.svg"), tr("Contour selection"), this);
66 /**
67 * Destructor
69 Contour::~Contour()
71 delete d;
74 /**
75 * @~spanish
76 * Inicializa la herramienta, permite que los items dentro de el fotograma se puedan seleccionar o pero no mover.
78 void Contour::init(Photogram *photogram)
80 qDeleteAll(d->editors);
81 d->editors.clear();
83 foreach(QGraphicsView * view, photogram->views())
85 foreach(QGraphicsItem *item, view->scene()->items())
87 item->setFlags(QGraphicsItem::ItemIsSelectable);
88 item->setFlag(QGraphicsItem::ItemIsMovable, false);
93 /**
94 * @~spanish
95 * Retorna el id de la herramienta.
97 QString Contour::id() const
99 return "contour";
103 * @~spanish
104 * Función sobrecargada para implementar la edición de nodos.
106 void Contour::press(const QGraphicsSceneMouseEvent *input)
108 Q_UNUSED(input);
109 PaintArea *paintArea = this->paintArea();
110 aboutToChangePhotogram(paintArea->photogram());
114 * @~spanish
115 * Función sobrecargada para implementar la edición de nodos.
117 void Contour::move(const QGraphicsSceneMouseEvent *input)
119 Q_UNUSED(input);
123 * @~spanish
124 * Función sobrecargada para implementar la edición de nodos.
126 void Contour::release(const QGraphicsSceneMouseEvent *input)
128 Q_UNUSED(input);
129 PaintArea *paintArea = this->paintArea();
131 if(paintArea->scene()->selectedItems().count() > 0)
133 QList<QGraphicsItem *> selecteds = paintArea->scene()->selectedItems();
134 QList<Drawing::Tool::Private::ContourEditor *>::iterator it = d->editors.begin();
135 QList<Drawing::Tool::Private::ContourEditor *>::iterator itEnd = d->editors.end();
137 while(it != itEnd)
139 int parentIndex = paintArea->scene()->selectedItems().indexOf((*it)->item() );
140 if(parentIndex != -1 )
142 selecteds.removeAt(parentIndex);
144 else
146 delete d->editors.takeAt(d->editors.indexOf((*it)));
148 ++it;
151 foreach(QGraphicsItem *item, selecteds)
153 if(item)
155 int index = paintArea->currentFrame()->logicalIndexOf(item);
156 if(index > -1)
158 item->setFlag(QGraphicsItem::ItemIsMovable, false);
159 if(Item::Path *path = qgraphicsitem_cast<Item::Path*>(item) )
161 d->editors << new Drawing::Tool::Private::ContourEditor(path, paintArea->scene());
163 else
165 if(item->type() != QGraphicsItemGroup::Type)
167 Command::ConvertItem *convertItem = new Command::ConvertItem(item, Item::Path::Type, paintArea->currentFrame());
168 paintArea->addCommand(convertItem);
170 if(convertItem->isValid())
172 if(Item::Path *path = qgraphicsitem_cast<Item::Path*>( paintArea->currentFrame()->item(index) ) )
174 d->editors << new Drawing::Tool::Private::ContourEditor(path, paintArea->photogram());
176 else if(Item::Proxy *proxy = qgraphicsitem_cast<Item::Proxy*>( paintArea->currentFrame()->item(index) ))
178 if(Item::Path *path = qgraphicsitem_cast<Item::Path*>( proxy->item() ) )
180 d->editors << new Drawing::Tool::Private::ContourEditor(path, paintArea->photogram());
181 d->editors.last()->setProxyItem(proxy);
191 Model::Frame *currentFrame = paintArea->currentFrame();
193 foreach(Drawing::Tool::Private::ContourEditor *editor, d->editors)
195 if(editor->isModified())
197 if(currentFrame->logicalIndexOf(editor->item()) > -1)
199 QPainterPath path = editor->item()->path();
200 // editor->restore();
202 paintArea->addCommand(new Command::EditNodesItem(editor->item(), editor->oldPath(), paintArea->currentFrame()));
204 editor->save();
209 else
211 qDeleteAll(d->editors);
212 d->editors.clear();
217 * @~spanish
218 * Elimina un nodo si se recibe que se presiono la conbinación de teclas Control+supr.
220 void Contour::keyPressEvent(QKeyEvent *event)
222 if(event->key() == Qt::Key_Delete & Qt::Key_Control)
224 foreach(Drawing::Tool::Private::ContourEditor *editor, d->editors)
226 editor->removeSelectedNodes();
232 * @~spanish
233 * Función reimplemenatada para evita que se des-aparescan los nodos del fotograma.
235 void Contour::photogramChanged(Photogram *const photogram)
237 if(photogram->selectedItems().count() > 0)
239 foreach(Drawing::Tool::Private::ContourEditor *editor, d->editors)
241 editor->show();
244 else
246 qDeleteAll(d->editors);
247 d->editors.clear();
252 * @~spanish
253 * Retorna la acción que representa la herramienta.
255 DGui::Action *Contour::action() const
257 return d->action;
261 * @~spanish
262 * Retorna que la herramienta es de tipo Selection.
264 int Contour::type() const
266 return AbstractTool::Selection;
270 * @~spanish
271 * Elimina los nodos cuando se cambia de herramienta.
273 void Contour::aboutToChangeTool()
275 qDeleteAll(d->editors);
276 d->editors.clear();