canvasgesture_linemove.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "canvasgesture_linemove.h"
00017
00018 #include <QDebug>
00019 #include <QMouseEvent>
00020 #include <QPainter>
00021 #include <QPen>
00022
00023 #include "canvas.h"
00024 #include "pageitem_line.h"
00025 #include "scribusdoc.h"
00026 #include "scribusview.h"
00027 #include "selection.h"
00028 #include "util_math.h"
00029
00030
00031 void LineMove::clear()
00032 {
00033 m_haveLineItem = false;
00034 }
00035
00036
00037 void LineMove::prepare(QPointF start, QPointF end)
00038 {
00039 m_haveLineItem = false;
00040 setStartPoint(start);
00041 setStartPoint(end);
00042 }
00043
00044
00045 void LineMove::prepare(PageItem_Line* line, bool useOriginAsEndpoint)
00046 {
00047 m_haveLineItem = (line != NULL);
00048 if (!m_haveLineItem)
00049 return;
00050 m_useOriginAsEndpoint = useOriginAsEndpoint;
00051 m_line = line;
00052 setStartPoint(QPointF(m_line->xPos(), m_line->yPos()));
00053 setEndPoint(QPointF(m_line->xPos() + m_line->width(), m_line->yPos()));
00054 setRotation(m_line->rotation());
00055 if (m_useOriginAsEndpoint)
00056 {
00057 QPointF tmp = startPoint();
00058 setStartPoint(endPoint());
00059 setEndPoint(tmp);
00060 }
00061 }
00062
00063
00064 double LineMove::rotation() const
00065 {
00066 double rot = xy2Deg(m_bounds.width(), m_bounds.height());
00067 if (rot < 0.0)
00068 return 360 + rot;
00069 else
00070 return rot;
00071 }
00072
00073
00074 void LineMove::setRotation(double rot)
00075 {
00076 m_bounds.setSize(length() * QSizeF(cosd(rot), sind(rot)));
00077 }
00078
00079
00080 double LineMove::length() const
00081 {
00082 return qMax(0.01, distance(m_bounds.width(), m_bounds.height()));
00083 }
00084
00085
00086 void LineMove::setStartPoint(QPointF p)
00087 {
00088 m_bounds.setTopLeft(p);
00089 }
00090
00091
00092 void LineMove::setEndPoint(QPointF p)
00093 {
00094 m_bounds.setBottomRight(p);
00095 }
00096
00097
00098 void LineMove::activate(bool flag)
00099 {
00100 qDebug() << "LineMove::activate" << flag << m_bounds;
00101 }
00102
00103
00104
00105 void LineMove::deactivate(bool flag)
00106 {
00107 qDebug() << "LineMove::deactivate" << flag;
00108 m_haveLineItem = false;
00109 }
00110
00111
00112
00113 void LineMove::drawControls(QPainter* p)
00114 {
00115 p->save();
00116 p->scale(m_canvas->scale(), m_canvas->scale());
00117 p->translate(-m_doc->minCanvasCoordinate.x(), -m_doc->minCanvasCoordinate.y());
00118 p->setBrush(Qt::NoBrush);
00119 p->setPen(QPen(Qt::black, 1.0 / m_canvas->scale(), Qt::DotLine, Qt::FlatCap, Qt::MiterJoin));
00120 p->drawLine(m_bounds.topLeft(), m_bounds.bottomRight());
00121 p->restore();
00122 }
00123
00124
00125
00126 void LineMove::mouseReleaseEvent(QMouseEvent *m)
00127 {
00128 adjustBounds(m);
00129 if (m_haveLineItem)
00130 {
00131 doResize();
00132 m_doc->setRedrawBounding(m_line);
00133 m_view->resetMousePressed();
00134 m_line->checkChanges();
00135 m_line->update();
00136 }
00137 m->accept();
00138 m_canvas->update();
00139 qDebug() << "LineMove::mouseRelease" << m_line->xPos() << "," << m_line->yPos() << "@" << m_line->rotation() << m_line->width() << "x" << m_line->height();
00140 m_view->stopGesture();
00141 }
00142
00143
00144 void LineMove::doResize()
00145 {
00146 if (m_useOriginAsEndpoint)
00147 {
00148 m_line->setXYPos(m_bounds.right(), m_bounds.bottom());
00149 double rot = rotation();
00150 m_line->setRotation(rot < 180? rot + 180 : rot - 180);
00151 }
00152 else
00153 {
00154 m_line->setXYPos(m_bounds.x(), m_bounds.y());
00155 m_line->setRotation(rotation());
00156 }
00157 m_line->setWidth(length());
00158 m_line->setHeight(1.0);
00159 m_line->updateClip();
00160
00161 }
00162
00163
00164 void LineMove::mouseMoveEvent(QMouseEvent *m)
00165 {
00166 adjustBounds(m);
00167 if (m_haveLineItem)
00168 {
00169 doResize();
00170 }
00171 m->accept();
00172 m_canvas->repaint();
00173 }
00174
00175
00176
00177 void LineMove::adjustBounds(QMouseEvent *m)
00178 {
00179 FPoint mousePointDoc = m_canvas->globalToCanvas(m->globalPos());
00180 bool constrainRatio = ((m->modifiers() & Qt::ControlModifier) != Qt::NoModifier);
00181
00182 double newX = mousePointDoc.x();
00183 double newY = mousePointDoc.y();
00184
00185 if (m_doc->useRaster)
00186 {
00187 newX = qRound(newX / m_doc->guidesSettings.minorGrid) * m_doc->guidesSettings.minorGrid;
00188 newY = qRound(newY / m_doc->guidesSettings.minorGrid) * m_doc->guidesSettings.minorGrid;
00189 }
00190 m_bounds.setBottomRight(QPointF(newX, newY));
00191
00192
00193
00194 if (constrainRatio)
00195 {
00196 double newRot = rotation();
00197 newRot = constrainAngle(newRot, m_doc->toolSettings.constrain);
00198 setRotation(newRot);
00199 }
00200 m_view->updateCanvas(m_bounds.normalized().adjusted(-10, -10, 20, 20));
00201 }
00202
00203
00204
00205 void LineMove::mousePressEvent(QMouseEvent *m)
00206 {
00207 PageItem_Line* line = m_doc->m_Selection->count() == 1 ? m_doc->m_Selection->itemAt(0)->asLine() : NULL;
00208 if (line)
00209 {
00210 bool hitsOrigin = m_canvas->hitsCanvasPoint(m->globalPos(), line->xyPos());
00211 prepare(line, hitsOrigin);
00212
00213 bool hitsEnd = m_canvas->hitsCanvasPoint(m->globalPos(), endPoint());
00214 m_haveLineItem = hitsOrigin || hitsEnd;
00215 }
00216 else
00217 {
00218 FPoint point = m_canvas->globalToCanvas(m->globalPos());
00219 setStartPoint(QPointF(point.x(), point.y()));
00220 setEndPoint(QPointF(point.x(), point.y()));
00221 m_haveLineItem = false;
00222 }
00223 if (m_haveLineItem)
00224 m->accept();
00225 }