Rev 22638 | Rev 23613 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
11338 | avox | 1 | /* |
2 | For general Scribus (>=1.3.2) copyright and licensing information please refer |
||
3 | to the COPYING file provided with the program. Following this notice may exist |
||
4 | a copyright and/or license notice that predates the release of Scribus 1.3.2 |
||
5 | for which a new license (GPL+exception) is in place. |
||
6 | */ |
||
7 | /*************************************************************************** |
||
8 | * * |
||
9 | * This program is free software; you can redistribute it and/or modify * |
||
10 | * it under the terms of the GNU General Public License as published by * |
||
11 | * the Free Software Foundation; either version 2 of the License, or * |
||
12 | * (at your option) any later version. * |
||
13 | * * |
||
14 | ***************************************************************************/ |
||
15 | |||
16 | #include "canvasgesture_linemove.h" |
||
17 | |||
11645 | fschmid | 18 | //#include <QDebug> |
11338 | avox | 19 | #include <QMouseEvent> |
20 | #include <QPainter> |
||
21 | #include <QPen> |
||
22 | |||
23 | #include "canvas.h" |
||
24 | #include "pageitem_line.h" |
||
25 | #include "scribusview.h" |
||
26 | #include "selection.h" |
||
23415 | jghali | 27 | #include "undomanager.h" |
11338 | avox | 28 | #include "util_math.h" |
29 | |||
30 | void LineMove::clear() |
||
31 | { |
||
32 | m_haveLineItem = false; |
||
23415 | jghali | 33 | if (m_transaction.isStarted()) |
34 | { |
||
35 | m_transaction.cancel(); |
||
36 | m_transaction.reset(); |
||
37 | } |
||
11338 | avox | 38 | } |
39 | |||
40 | |||
41 | void LineMove::prepare(QPointF start, QPointF end) |
||
42 | { |
||
43 | m_haveLineItem = false; |
||
44 | setStartPoint(start); |
||
45 | setStartPoint(end); |
||
46 | } |
||
47 | |||
48 | |||
49 | void LineMove::prepare(PageItem_Line* line, bool useOriginAsEndpoint) |
||
50 | { |
||
22518 | craig | 51 | m_haveLineItem = (line != nullptr); |
11338 | avox | 52 | if (!m_haveLineItem) |
53 | return; |
||
54 | m_useOriginAsEndpoint = useOriginAsEndpoint; |
||
55 | m_line = line; |
||
56 | setStartPoint(QPointF(m_line->xPos(), m_line->yPos())); |
||
57 | setEndPoint(QPointF(m_line->xPos() + m_line->width(), m_line->yPos())); |
||
58 | setRotation(m_line->rotation()); |
||
59 | if (m_useOriginAsEndpoint) |
||
60 | { |
||
61 | QPointF tmp = startPoint(); |
||
62 | setStartPoint(endPoint()); |
||
63 | setEndPoint(tmp); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | |||
68 | double LineMove::rotation() const |
||
69 | { |
||
70 | double rot = xy2Deg(m_bounds.width(), m_bounds.height()); |
||
71 | if (rot < 0.0) |
||
72 | return 360 + rot; |
||
22638 | craig | 73 | return rot; |
11338 | avox | 74 | } |
75 | |||
76 | |||
77 | void LineMove::setRotation(double rot) |
||
78 | { |
||
79 | m_bounds.setSize(length() * QSizeF(cosd(rot), sind(rot))); |
||
80 | } |
||
81 | |||
82 | |||
83 | double LineMove::length() const |
||
84 | { |
||
85 | return qMax(0.01, distance(m_bounds.width(), m_bounds.height())); |
||
86 | } |
||
87 | |||
88 | |||
89 | void LineMove::setStartPoint(QPointF p) |
||
90 | { |
||
91 | m_bounds.setTopLeft(p); |
||
92 | } |
||
93 | |||
94 | |||
95 | void LineMove::setEndPoint(QPointF p) |
||
96 | { |
||
97 | m_bounds.setBottomRight(p); |
||
98 | } |
||
99 | |||
100 | |||
101 | void LineMove::activate(bool flag) |
||
102 | { |
||
11645 | fschmid | 103 | // qDebug() << "LineMove::activate" << flag << m_bounds; |
11338 | avox | 104 | } |
105 | |||
106 | |||
107 | |||
23415 | jghali | 108 | void LineMove::deactivate(bool forGesture) |
11338 | avox | 109 | { |
11645 | fschmid | 110 | // qDebug() << "LineMove::deactivate" << flag; |
23415 | jghali | 111 | if (!forGesture) |
112 | clear(); |
||
11338 | avox | 113 | } |
114 | |||
115 | |||
116 | |||
117 | void LineMove::drawControls(QPainter* p) |
||
118 | { |
||
119 | p->save(); |
||
120 | p->scale(m_canvas->scale(), m_canvas->scale()); |
||
121 | p->translate(-m_doc->minCanvasCoordinate.x(), -m_doc->minCanvasCoordinate.y()); |
||
122 | p->setBrush(Qt::NoBrush); |
||
123 | p->setPen(QPen(Qt::black, 1.0 / m_canvas->scale(), Qt::DotLine, Qt::FlatCap, Qt::MiterJoin)); |
||
124 | p->drawLine(m_bounds.topLeft(), m_bounds.bottomRight()); |
||
125 | p->restore(); |
||
126 | } |
||
127 | |||
15394 | jghali | 128 | void LineMove::mousePressEvent(QMouseEvent *m) |
129 | { |
||
22518 | craig | 130 | PageItem_Line* line = m_doc->m_Selection->count() == 1 ? m_doc->m_Selection->itemAt(0)->asLine() : nullptr; |
15394 | jghali | 131 | if (line) |
132 | { |
||
133 | bool hitsOrigin = m_canvas->hitsCanvasPoint(m->globalPos(), line->xyPos()); |
||
134 | prepare(line, hitsOrigin); |
||
135 | // now we also know the line's endpoint: |
||
136 | bool hitsEnd = m_canvas->hitsCanvasPoint(m->globalPos(), endPoint()); |
||
137 | m_haveLineItem = hitsOrigin || hitsEnd; |
||
138 | } |
||
139 | else |
||
140 | { |
||
141 | FPoint point = m_canvas->globalToCanvas(m->globalPos()); |
||
142 | setStartPoint(QPointF(point.x(), point.y())); |
||
143 | setEndPoint(QPointF(point.x(), point.y())); |
||
144 | m_haveLineItem = false; |
||
145 | } |
||
146 | if (m_haveLineItem) |
||
147 | { |
||
23415 | jghali | 148 | if (!m_transaction) |
149 | { |
||
150 | QString targetName = line->getUName(); |
||
151 | QPixmap* targetIcon = line->getUPixmap(); |
||
152 | m_transaction = Um::instance()->beginTransaction(targetName, targetIcon, Um::Resize, "", Um::IResize); |
||
153 | } |
||
15394 | jghali | 154 | adjustBounds(m, false); |
155 | m_initialBounds = m_bounds; |
||
156 | m->accept(); |
||
157 | } |
||
158 | } |
||
11338 | avox | 159 | |
15394 | jghali | 160 | void LineMove::mouseMoveEvent(QMouseEvent *m) |
161 | { |
||
162 | adjustBounds(m); |
||
163 | if (m_haveLineItem) |
||
164 | { |
||
165 | doResize(); |
||
166 | double angle = rotation(); |
||
167 | if (angle > 0) |
||
168 | angle = 360 - angle; |
||
169 | m_canvas->displaySizeHUD(m->globalPos(), length(), fabs(angle), true); |
||
170 | } |
||
171 | m->accept(); |
||
172 | m_canvas->repaint(); |
||
173 | } |
||
11338 | avox | 174 | |
175 | void LineMove::mouseReleaseEvent(QMouseEvent *m) |
||
176 | { |
||
177 | adjustBounds(m); |
||
178 | if (m_haveLineItem) |
||
179 | { |
||
15394 | jghali | 180 | if (m_bounds != m_initialBounds) |
181 | doResize(); |
||
11338 | avox | 182 | m_doc->setRedrawBounding(m_line); |
11576 | avox | 183 | m_view->resetMousePressed(); |
184 | m_line->checkChanges(); |
||
11338 | avox | 185 | m_line->update(); |
186 | } |
||
23415 | jghali | 187 | if (m_transaction.isStarted()) |
188 | { |
||
189 | m_transaction.commit(); |
||
190 | m_transaction.reset(); |
||
191 | } |
||
11338 | avox | 192 | m->accept(); |
193 | m_canvas->update(); |
||
11645 | fschmid | 194 | // qDebug() << "LineMove::mouseRelease" << m_line->xPos() << "," << m_line->yPos() << "@" << m_line->rotation() << m_line->width() << "x" << m_line->height(); |
11338 | avox | 195 | m_view->stopGesture(); |
196 | } |
||
197 | |||
198 | |||
199 | void LineMove::doResize() |
||
200 | { |
||
201 | if (m_useOriginAsEndpoint) |
||
202 | { |
||
203 | m_line->setXYPos(m_bounds.right(), m_bounds.bottom()); |
||
204 | double rot = rotation(); |
||
205 | m_line->setRotation(rot < 180? rot + 180 : rot - 180); |
||
206 | } |
||
207 | else |
||
208 | { |
||
209 | m_line->setXYPos(m_bounds.x(), m_bounds.y()); |
||
210 | m_line->setRotation(rotation()); |
||
211 | } |
||
212 | m_line->setWidth(length()); |
||
213 | m_line->setHeight(1.0); |
||
214 | m_line->updateClip(); |
||
215 | // qDebug() << "LineMove::doresize" << m_line->xPos() << "," << m_line->yPos() << "@" << m_line->rotation() << m_line->width() << "x" << m_line->height(); |
||
216 | } |
||
217 | |||
15394 | jghali | 218 | void LineMove::adjustBounds(QMouseEvent *m, bool updateCanvas) |
11338 | avox | 219 | { |
220 | FPoint mousePointDoc = m_canvas->globalToCanvas(m->globalPos()); |
||
221 | bool constrainRatio = ((m->modifiers() & Qt::ControlModifier) != Qt::NoModifier); |
||
222 | |||
223 | double newX = mousePointDoc.x(); |
||
224 | double newY = mousePointDoc.y(); |
||
225 | |||
18027 | jghali | 226 | if (m_doc->SnapGrid) |
11338 | avox | 227 | { |
14932 | cbradney | 228 | newX = qRound(newX / m_doc->guidesPrefs().minorGridSpacing) * m_doc->guidesPrefs().minorGridSpacing; |
229 | newY = qRound(newY / m_doc->guidesPrefs().minorGridSpacing) * m_doc->guidesPrefs().minorGridSpacing; |
||
11338 | avox | 230 | } |
13618 | cbradney | 231 | //<<#8099 |
13600 | cbradney | 232 | FPoint np2 = m_doc->ApplyGridF(FPoint(newX, newY)); |
233 | double nx = np2.x(); |
||
234 | double ny = np2.y(); |
||
235 | m_doc->ApplyGuides(&nx, &ny); |
||
17744 | craig | 236 | m_doc->ApplyGuides(&nx, &ny,true); |
17451 | jghali | 237 | newX = nx; |
238 | newY = ny; |
||
13618 | cbradney | 239 | //>>#8099 |
13600 | cbradney | 240 | |
11338 | avox | 241 | m_bounds.setBottomRight(QPointF(newX, newY)); |
242 | //Constrain rotation angle, when the mouse is being dragged around for a new line |
||
243 | if (constrainRatio) |
||
244 | { |
||
245 | double newRot = rotation(); |
||
14932 | cbradney | 246 | newRot = constrainAngle(newRot, m_doc->opToolPrefs().constrain); |
11338 | avox | 247 | setRotation(newRot); |
248 | } |
||
15394 | jghali | 249 | if (updateCanvas) |
11338 | avox | 250 | { |
15394 | jghali | 251 | m_view->updateCanvas(m_bounds.normalized().adjusted(-10, -10, 20, 20)); |
11338 | avox | 252 | } |
11556 | fschmid | 253 | } |