modify abstracttool iface
[yamf.git] / yamf / tools / brush / brush.cpp
blobd83b74ff912e09767667267375b1ab6a94f885e3
1 /***************************************************************************
2 * Copyright (C) 2005 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 "brush.h"
23 #include <QPointF>
24 #include <QKeySequence>
25 #include <QGraphicsPathItem>
26 #include <QPainterPath>
27 #include <QMatrix>
28 #include <QGraphicsLineItem>
29 #include <QGraphicsView>
31 #include <QGraphicsSceneMouseEvent>
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>
48 #include <model/frame.h>
49 #include <item/path.h>
51 namespace YAMF {
52 namespace Drawing {
53 namespace Tool {
55 struct Brush::Private
57 Private() : item(0), exactness(0), configWidget(0) {}
59 QPointF firstPoint;
60 QPointF oldPos;
61 QPainterPath path;
63 DGui::Action *action;
65 YAMF::Item::Path *item;
67 QDoubleSpinBox *exactness;
68 QWidget *configWidget;
71 /**
72 * @~spanish
73 * Construye la herramienta.
75 Brush::Brush(QObject *parent) : AbstractTool(parent), d(new Private)
77 d->action = new DGui::Action( DGui::IconLoader::self()->load("draw-freehand.svg"), tr("Pencil"), this);
78 d->action->setShortcut( QKeySequence(tr("Ctrl+B")) );
80 d->exactness = new QDoubleSpinBox;
81 d->exactness->setValue(3.0);
84 d->configWidget = new QWidget;
85 QHBoxLayout *layout = new QHBoxLayout(d->configWidget);
86 layout->setMargin(0);
87 layout->setSpacing(2);
88 layout->addWidget(new QLabel(tr("Smoothness")));
89 layout->addWidget(d->exactness);
92 /**
93 * @~spanish
94 * Destructor
96 Brush::~Brush()
98 if( d->configWidget )
100 d->configWidget->setParent(0);
101 delete d->configWidget;
103 delete d;
107 * @~spanish
108 * Inicializa la herramienta, evita que los items dentro de el fotograma se puedan mover o seleccionar
110 void Brush::init(Photogram *photogram)
112 foreach(QGraphicsView * view, photogram->views())
114 view->setDragMode ( QGraphicsView::NoDrag );
116 if ( QGraphicsScene *photogram = qobject_cast<QGraphicsScene *>(view->scene()) )
118 foreach(QGraphicsItem *item, photogram->items() )
120 item->setFlag(QGraphicsItem::ItemIsSelectable, false);
121 item->setFlag(QGraphicsItem::ItemIsMovable, false);
128 * @~spanish
129 * Retorna el id de la herramienta
131 QString Brush::id() const
133 return tr("Pencil");
137 * @~spanish
138 * Función sobrecargada para implementar el trazo de lineas.
140 void Brush::press(const QGraphicsSceneMouseEvent *input)
142 PaintArea *paintArea = this->paintArea();
144 d->firstPoint = input->scenePos();
145 d->path = QPainterPath();
146 d->path.moveTo(d->firstPoint);
147 d->oldPos = input->scenePos();
148 d->item = new Item::Path();
149 d->item->setPen(paintArea->brushManager()->pen());
150 paintArea->photogram()->addItem( d->item );
151 d->item->setZValue(1000);
155 * @~spanish
156 * Función sobrecargada para implementar el trazo de lineas.
158 void Brush::move(const QGraphicsSceneMouseEvent *input)
160 PaintArea *paintArea = this->paintArea();
162 if(input->buttons() == Qt::LeftButton)
164 QPointF lastPoint = input->scenePos();
166 foreach(QGraphicsView * view, paintArea->photogram()->views())
168 // view->setDragMode (QGraphicsView::NoDrag);
171 // d->path.moveTo( d->oldPos);
172 d->path.lineTo( lastPoint );
174 d->item->setPath(d->path);
175 paintArea->brushManager()->map(d->item);
177 d->oldPos = lastPoint;
182 * @~spanish
183 * Función sobrecargada para implementar el trazo de lineas.
185 void Brush::release(const QGraphicsSceneMouseEvent *input)
187 PaintArea *paintArea = this->paintArea();
188 BrushManager *brushManager = paintArea->brushManager();
190 double smoothness = 0.1;
191 if( d->exactness )
193 smoothness = d->exactness->value();
196 if ( d->firstPoint == input->pos() && d->path.elementCount() == 1)
198 smoothness = 0;
199 d->path.addEllipse(input->pos().x(), input->pos().y(), brushManager->pen().width(), brushManager->pen().width());
202 smoothPath( d->path, smoothness );
204 d->firstPoint = QPoint(0,0);
205 d->item->setBrush( brushManager->brush() );
206 d->item->setPath(d->path);
207 paintArea->brushManager()->map(d->item);
210 paintArea->currentFrame()->addItem(d->item);
214 * @~spanish
215 * @internal
216 * Suaviza el trazo hecho por el usuario.
218 void Brush::smoothPath(QPainterPath &path, double smoothness, int from, int to)
220 if(smoothness > 0 )
222 QPolygonF polygon = path.toFillPolygon();
223 if(!polygon.isEmpty() && polygon.isClosed())
224 polygon.pop_back();
226 path = DGraphics::Algorithm::smooth(polygon, smoothness, from, to);
231 * @~spanish
232 * Retorna la acción que representa la herramienta.
234 DGui::Action * Brush::action() const
236 return d->action;
240 * @~spanish
241 * Retorna que la herramienta es de tipo Brush.
243 int Brush::type() const
245 return AbstractTool::Brush;
248 void Brush::aboutToChangeTool()
253 * @~spanish
254 * Asigna la interfaz gráfica para configurar el grado de suavizado.
256 void Brush::setupConfigBar(QToolBar *configBar)
258 configBar->addWidget(d->configWidget)->setVisible(true);