Rev 11556 | Rev 11645 | 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 | |||
18 | #include <QDebug> |
||
19 | #include <QMouseEvent> |
||
20 | #include <QPainter> |
||
21 | #include <QPen> |
||
22 | |||
23 | #include "canvas.h" |
||
24 | #include "pageitem_line.h" |
||
25 | #include "scribusdoc.h" |
||
26 | #include "scribusview.h" |
||
27 | #include "selection.h" |
||
28 | #include "util_math.h" |
||
29 | |||
30 | |||
31 | void LineMove::clear() |
||
32 | { |
||
33 | m_haveLineItem = false; |
||
34 | } |
||
35 | |||
36 | |||
37 | void LineMove::prepare(QPointF start, QPointF end) |
||
38 | { |
||
39 | m_haveLineItem = false; |
||
40 | setStartPoint(start); |
||
41 | setStartPoint(end); |
||
42 | } |
||
43 | |||
44 | |||
45 | void LineMove::prepare(PageItem_Line* line, bool useOriginAsEndpoint) |
||
46 | { |
||
47 | m_haveLineItem = (line != NULL); |
||
48 | if (!m_haveLineItem) |
||
49 | return; |
||
50 | m_useOriginAsEndpoint = useOriginAsEndpoint; |
||
51 | m_line = line; |
||
52 | setStartPoint(QPointF(m_line->xPos(), m_line->yPos())); |
||
53 | setEndPoint(QPointF(m_line->xPos() + m_line->width(), m_line->yPos())); |
||
54 | setRotation(m_line->rotation()); |
||
55 | if (m_useOriginAsEndpoint) |
||
56 | { |
||
57 | QPointF tmp = startPoint(); |
||
58 | setStartPoint(endPoint()); |
||
59 | setEndPoint(tmp); |
||
60 | } |
||
61 | } |
||
62 | |||
63 | |||
64 | double LineMove::rotation() const |
||
65 | { |
||
66 | double rot = xy2Deg(m_bounds.width(), m_bounds.height()); |
||
67 | if (rot < 0.0) |
||
68 | return 360 + rot; |
||
69 | else |
||
70 | return rot; |
||
71 | } |
||
72 | |||
73 | |||
74 | void LineMove::setRotation(double rot) |
||
75 | { |
||
76 | m_bounds.setSize(length() * QSizeF(cosd(rot), sind(rot))); |
||
77 | } |
||
78 | |||
79 | |||
80 | double LineMove::length() const |
||
81 | { |
||
82 | return qMax(0.01, distance(m_bounds.width(), m_bounds.height())); |
||
83 | } |
||
84 | |||
85 | |||
86 | void LineMove::setStartPoint(QPointF p) |
||
87 | { |
||
88 | m_bounds.setTopLeft(p); |
||
89 | } |
||
90 | |||
91 | |||
92 | void LineMove::setEndPoint(QPointF p) |
||
93 | { |
||
94 | m_bounds.setBottomRight(p); |
||
95 | } |
||
96 | |||
97 | |||
98 | void LineMove::activate(bool flag) |
||
99 | { |
||
100 | qDebug() << "LineMove::activate" << flag << m_bounds; |
||
101 | } |
||
102 | |||
103 | |||
104 | |||
105 | void LineMove::deactivate(bool flag) |
||
106 | { |
||
107 | qDebug() << "LineMove::deactivate" << flag; |
||
108 | m_haveLineItem = false; |
||
109 | } |
||
110 | |||
111 | |||
112 | |||
113 | void LineMove::drawControls(QPainter* p) |
||
114 | { |
||
115 | p->save(); |
||
116 | p->scale(m_canvas->scale(), m_canvas->scale()); |
||
117 | p->translate(-m_doc->minCanvasCoordinate.x(), -m_doc->minCanvasCoordinate.y()); |
||
118 | p->setBrush(Qt::NoBrush); |
||
119 | p->setPen(QPen(Qt::black, 1.0 / m_canvas->scale(), Qt::DotLine, Qt::FlatCap, Qt::MiterJoin)); |
||
120 | p->drawLine(m_bounds.topLeft(), m_bounds.bottomRight()); |
||
121 | p->restore(); |
||
122 | } |
||
123 | |||
124 | |||
125 | |||
126 | void LineMove::mouseReleaseEvent(QMouseEvent *m) |
||
127 | { |
||
128 | adjustBounds(m); |
||
129 | if (m_haveLineItem) |
||
130 | { |
||
131 | doResize(); |
||
132 | m_doc->setRedrawBounding(m_line); |
||
11576 | avox | 133 | m_view->resetMousePressed(); |
134 | m_line->checkChanges(); |
||
11338 | avox | 135 | m_line->update(); |
136 | } |
||
137 | m->accept(); |
||
138 | m_canvas->update(); |
||
139 | qDebug() << "LineMove::mouseRelease" << m_line->xPos() << "," << m_line->yPos() << "@" << m_line->rotation() << m_line->width() << "x" << m_line->height(); |
||
140 | m_view->stopGesture(); |
||
141 | } |
||
142 | |||
143 | |||
144 | void LineMove::doResize() |
||
145 | { |
||
146 | if (m_useOriginAsEndpoint) |
||
147 | { |
||
148 | m_line->setXYPos(m_bounds.right(), m_bounds.bottom()); |
||
149 | double rot = rotation(); |
||
150 | m_line->setRotation(rot < 180? rot + 180 : rot - 180); |
||
151 | } |
||
152 | else |
||
153 | { |
||
154 | m_line->setXYPos(m_bounds.x(), m_bounds.y()); |
||
155 | m_line->setRotation(rotation()); |
||
156 | } |
||
157 | m_line->setWidth(length()); |
||
158 | m_line->setHeight(1.0); |
||
159 | m_line->updateClip(); |
||
160 | // qDebug() << "LineMove::doresize" << m_line->xPos() << "," << m_line->yPos() << "@" << m_line->rotation() << m_line->width() << "x" << m_line->height(); |
||
161 | } |
||
162 | |||
163 | |||
164 | void LineMove::mouseMoveEvent(QMouseEvent *m) |
||
165 | { |
||
166 | adjustBounds(m); |
||
167 | if (m_haveLineItem) |
||
168 | { |
||
169 | doResize(); |
||
170 | } |
||
171 | m->accept(); |
||
172 | m_canvas->repaint(); |
||
173 | } |
||
174 | |||
175 | |||
176 | |||
177 | void LineMove::adjustBounds(QMouseEvent *m) |
||
178 | { |
||
179 | FPoint mousePointDoc = m_canvas->globalToCanvas(m->globalPos()); |
||
180 | bool constrainRatio = ((m->modifiers() & Qt::ControlModifier) != Qt::NoModifier); |
||
181 | |||
182 | double newX = mousePointDoc.x(); |
||
183 | double newY = mousePointDoc.y(); |
||
184 | |||
185 | if (m_doc->useRaster) |
||
186 | { |
||
187 | newX = qRound(newX / m_doc->guidesSettings.minorGrid) * m_doc->guidesSettings.minorGrid; |
||
188 | newY = qRound(newY / m_doc->guidesSettings.minorGrid) * m_doc->guidesSettings.minorGrid; |
||
189 | } |
||
190 | m_bounds.setBottomRight(QPointF(newX, newY)); |
||
191 | // qDebug() << "LineMove::adjustBounds" << m_bounds << rotation() << length() << m_bounds.bottomRight(); |
||
192 | |||
193 | //Constrain rotation angle, when the mouse is being dragged around for a new line |
||
194 | if (constrainRatio) |
||
195 | { |
||
196 | double newRot = rotation(); |
||
197 | newRot = constrainAngle(newRot, m_doc->toolSettings.constrain); |
||
198 | setRotation(newRot); |
||
199 | } |
||
200 | m_view->updateCanvas(m_bounds.normalized().adjusted(-10, -10, 20, 20)); |
||
201 | } |
||
202 | |||
203 | |||
204 | |||
205 | void LineMove::mousePressEvent(QMouseEvent *m) |
||
206 | { |
||
207 | PageItem_Line* line = m_doc->m_Selection->count() == 1 ? m_doc->m_Selection->itemAt(0)->asLine() : NULL; |
||
208 | if (line) |
||
209 | { |
||
210 | bool hitsOrigin = m_canvas->hitsCanvasPoint(m->globalPos(), line->xyPos()); |
||
211 | prepare(line, hitsOrigin); |
||
212 | // now we also know the line's endpoint: |
||
213 | bool hitsEnd = m_canvas->hitsCanvasPoint(m->globalPos(), endPoint()); |
||
214 | m_haveLineItem = hitsOrigin || hitsEnd; |
||
215 | } |
||
216 | else |
||
217 | { |
||
218 | FPoint point = m_canvas->globalToCanvas(m->globalPos()); |
||
11388 | avox | 219 | setStartPoint(QPointF(point.x(), point.y())); |
220 | setEndPoint(QPointF(point.x(), point.y())); |
||
11338 | avox | 221 | m_haveLineItem = false; |
222 | } |
||
223 | if (m_haveLineItem) |
||
224 | m->accept(); |
||
11556 | fschmid | 225 | } |