Rev 23159 | Rev 23612 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
23152 | jghali | 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_rulermove.h" |
||
17 | |||
18 | #include <QEvent> |
||
19 | #include <QMouseEvent> |
||
20 | #include <QPainter> |
||
21 | |||
22 | #include "appmodes.h" |
||
23 | #include "guidemanagercore.h" |
||
24 | #include "scribus.h" |
||
25 | #include "scribusdoc.h" |
||
26 | #include "scribusview.h" |
||
27 | #include "ui/guidemanager.h" |
||
28 | #include "iconmanager.h" |
||
29 | |||
30 | |||
31 | RulerGesture::RulerGesture(ScribusView *view, RulerGesture::Mode mode) : |
||
32 | CanvasGesture(view), |
||
33 | m_ScMW(m_view->m_ScMW), |
||
34 | m_mode(mode), |
||
35 | m_haveGuide(false), |
||
36 | m_page(0), |
||
37 | m_guide(0.0), |
||
38 | m_currentGuide(0.0), |
||
39 | m_haveCursor(false), |
||
40 | m_xy(0,0), |
||
41 | m_mousePoint(0,0) |
||
42 | { |
||
43 | } |
||
44 | |||
45 | |||
46 | void RulerGesture::drawControls(QPainter* p) |
||
47 | { |
||
48 | int page = -1; |
||
49 | //This is !null where we've entered the RulerGesture::mouseMoveEvent |
||
50 | if (!m_mousePoint.isNull()) |
||
51 | page = m_doc->OnPage(m_mousePoint.x(), m_mousePoint.y()); |
||
52 | if (page == -1) |
||
53 | return; |
||
54 | ScPage* dragToPage=m_doc->Pages->at(page); |
||
55 | if (!dragToPage) |
||
56 | return; |
||
57 | if (m_haveGuide) |
||
58 | dragToPage = m_doc->Pages->at(m_page); |
||
59 | QColor color(m_doc->guidesPrefs().guideColor); |
||
60 | p->save(); |
||
61 | QPoint pageOrigin = m_canvas->canvasToLocal(QPointF(dragToPage->xOffset(), dragToPage->yOffset())); |
||
62 | QSize pageSize = (QSizeF(dragToPage->width(), dragToPage->height()) * m_canvas->scale()).toSize(); |
||
63 | switch (m_mode) |
||
64 | { |
||
65 | case HORIZONTAL: |
||
66 | p->setPen(QPen(color, 1.0, Qt::DashDotLine, Qt::FlatCap, Qt::MiterJoin)); |
||
67 | p->drawLine(QPoint(pageOrigin.x(), m_xy.y()), QPoint(pageOrigin.x() + pageSize.width(), m_xy.y())); |
||
68 | break; |
||
69 | case VERTICAL: |
||
70 | p->setPen(QPen(color, 1.0, Qt::DashDotLine, Qt::FlatCap, Qt::MiterJoin)); |
||
71 | p->drawLine(QPoint(m_xy.x(), pageOrigin.y()), QPoint(m_xy.x(), pageOrigin.y() + pageSize.height())); |
||
72 | break; |
||
73 | case ORIGIN: |
||
74 | p->setPen(QPen(color, 1.0, Qt::DotLine, Qt::FlatCap, Qt::MiterJoin)); |
||
75 | p->drawLine(QPoint(m_xy.x(), 0), QPoint(m_xy.x(), m_canvas->height())); |
||
76 | p->drawLine(QPoint(0, m_xy.y()), QPoint(m_canvas->width(), m_xy.y())); |
||
77 | break; |
||
78 | } |
||
79 | p->restore(); |
||
80 | } |
||
81 | |||
82 | |||
83 | void RulerGesture::clear() |
||
84 | { |
||
85 | m_haveGuide = false; |
||
86 | } |
||
87 | |||
88 | |||
89 | void RulerGesture::prepare(Mode mode) |
||
90 | { |
||
91 | m_haveGuide = false; |
||
92 | m_mode = mode; |
||
93 | } |
||
94 | |||
95 | |||
96 | void RulerGesture::activate(bool fromGesture) |
||
97 | { |
||
98 | m_haveCursor = (qApp->overrideCursor() != nullptr); |
||
99 | if ( (!fromGesture) && qApp->overrideCursor()) |
||
100 | { |
||
101 | m_haveCursor = true; |
||
102 | m_cursor = *(qApp->overrideCursor()); |
||
103 | } |
||
104 | else |
||
105 | { |
||
106 | m_haveCursor = false; |
||
107 | } |
||
108 | switch (m_mode) |
||
109 | { |
||
110 | case HORIZONTAL: |
||
111 | qApp->changeOverrideCursor(QCursor(Qt::SplitVCursor)); |
||
112 | break; |
||
113 | case VERTICAL: |
||
114 | qApp->changeOverrideCursor(QCursor(Qt::SplitHCursor)); |
||
115 | break; |
||
116 | case ORIGIN: |
||
117 | qApp->changeOverrideCursor(QCursor(Qt::CrossCursor)); |
||
118 | break; |
||
119 | } |
||
120 | emit guideInfo(m_mode, m_guide); |
||
121 | } |
||
122 | |||
123 | void RulerGesture::deactivate(bool) |
||
124 | { |
||
125 | if (m_haveCursor) |
||
126 | qApp->changeOverrideCursor(m_cursor); |
||
127 | m_haveGuide = false; |
||
128 | } |
||
129 | |||
130 | |||
131 | bool RulerGesture::mouseHitsGuide(const FPoint& mousePointDoc) |
||
132 | { |
||
133 | const int page = m_doc->OnPage(mousePointDoc.x(), mousePointDoc.y()); |
||
134 | if ((m_doc->guidesPrefs().guidesShown) && (!m_doc->GuideLock) && page >= 0) |
||
135 | { |
||
136 | double grabRadScale = m_doc->guidesPrefs().grabRadius / m_canvas->scale(); |
||
137 | int index = m_doc->Pages->at(page)->guides.isMouseOnVertical(mousePointDoc.x() + grabRadScale, mousePointDoc.x() - grabRadScale, GuideManagerCore::Standard); |
||
138 | if (index >= 0) |
||
139 | { |
||
140 | m_mode = VERTICAL; |
||
141 | m_haveGuide = true; |
||
142 | m_guide = m_doc->Pages->at(page)->guides.vertical(index, GuideManagerCore::Standard); |
||
143 | m_currentGuide = m_guide; |
||
144 | m_page = page; |
||
145 | return true; |
||
146 | } |
||
147 | index = m_doc->Pages->at(page)->guides.isMouseOnHorizontal(mousePointDoc.y() + grabRadScale, mousePointDoc.y() - grabRadScale, GuideManagerCore::Standard); |
||
148 | if (index >= 0) |
||
149 | { |
||
150 | m_mode = HORIZONTAL; |
||
151 | m_haveGuide = true; |
||
152 | m_guide = m_doc->Pages->at(page)->guides.horizontal(index, GuideManagerCore::Standard); |
||
153 | m_currentGuide = m_guide; |
||
154 | m_page = page; |
||
155 | return true; |
||
156 | } |
||
157 | } |
||
158 | m_haveGuide = false; |
||
159 | return false; |
||
160 | } |
||
161 | |||
162 | void RulerGesture::mouseSelectGuide(QMouseEvent *m) |
||
163 | { |
||
164 | FPoint mousePointDoc = m_canvas->globalToCanvas(m->globalPos()); |
||
165 | const int page = m_doc->OnPage(mousePointDoc.x(), mousePointDoc.y()); |
||
166 | if ((m_doc->guidesPrefs().guidesShown) && page >= 0) |
||
167 | { |
||
168 | double grabRadScale = m_doc->guidesPrefs().grabRadius / m_canvas->scale(); |
||
169 | int index = m_doc->Pages->at(page)->guides.isMouseOnVertical(mousePointDoc.x() + grabRadScale, mousePointDoc.x() - grabRadScale, GuideManagerCore::Standard); |
||
170 | if (index >= 0) |
||
171 | { |
||
172 | emit guideInfo(VERTICAL, m_doc->Pages->at(page)->guides.vertical(index, GuideManagerCore::Standard)); |
||
173 | return; |
||
174 | } |
||
175 | index = m_doc->Pages->at(page)->guides.isMouseOnHorizontal(mousePointDoc.y() + grabRadScale, mousePointDoc.y() - grabRadScale, GuideManagerCore::Standard); |
||
176 | if (index >= 0) |
||
177 | { |
||
178 | emit guideInfo(HORIZONTAL, m_doc->Pages->at(page)->guides.horizontal(index, GuideManagerCore::Standard)); |
||
179 | return; |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | |||
184 | void RulerGesture::movePoint(QMouseEvent* m, bool mouseRelease) |
||
185 | { |
||
186 | FPoint mousePointDoc = m_canvas->globalToCanvas(m->globalPos()); |
||
187 | const int page = m_doc->OnPage(mousePointDoc.x(), mousePointDoc.y()); |
||
188 | QRect viewport(m_view->viewport()->mapToGlobal(QPoint(0,0)), QSize(m_view->visibleWidth(), m_view->visibleHeight())); |
||
189 | QPoint newMousePoint = m->globalPos() - (m_canvas->mapToParent(QPoint(0, 0)) + m_canvas->parentWidget()->mapToGlobal(QPoint(0, 0))); |
||
190 | double x = mousePointDoc.x(); |
||
191 | double y = mousePointDoc.y(); |
||
192 | switch (m_mode) |
||
193 | { |
||
194 | case ORIGIN: |
||
195 | m_canvas->repaint(); |
||
196 | m_canvas->displayCorrectedXYHUD(m->globalPos(), x, y); |
||
197 | break; |
||
198 | case HORIZONTAL: |
||
199 | if (!m_ScMW->doc->guidesPrefs().guidesShown) |
||
200 | break; |
||
201 | m_canvas->update(0, m_xy.y() - 2, m_canvas->width(), 4); |
||
202 | |||
203 | if ((page >= 0) && (viewport.contains(m->globalPos()))) |
||
204 | { |
||
205 | ScPage* currentPage = m_doc->Pages->at(page); |
||
206 | if (m_doc->SnapElement) |
||
207 | { |
||
208 | double xout = 0; |
||
209 | double yout = 0; |
||
23177 | jghali | 210 | m_doc->getClosestElementBorder(x, y, &xout, &yout, ScribusDoc::IncludeSelection); |
23159 | jghali | 211 | if (yout != y) |
23152 | jghali | 212 | { |
23159 | jghali | 213 | y = yout; |
23152 | jghali | 214 | newMousePoint = m_canvas->canvasToLocal(QPointF(x, y)); |
215 | } |
||
216 | } |
||
217 | if (!m_haveGuide) |
||
218 | { |
||
219 | qApp->changeOverrideCursor(QCursor(Qt::SplitVCursor)); |
||
220 | if (mouseRelease) |
||
221 | { |
||
222 | currentPage->guides.addHorizontal(y - currentPage->yOffset(), GuideManagerCore::Standard); |
||
223 | m_guide = y - currentPage->yOffset(); |
||
224 | m_page = page; |
||
225 | m_haveGuide = true; |
||
226 | m_doc->changed(); |
||
227 | } |
||
228 | } |
||
229 | else if (mouseRelease) |
||
230 | { |
||
231 | if (page == m_page) |
||
232 | currentPage->guides.moveHorizontal(m_guide, y - currentPage->yOffset(), GuideManagerCore::Standard); |
||
233 | else |
||
234 | { |
||
235 | m_doc->Pages->at(m_page)->guides.deleteHorizontal(m_guide, GuideManagerCore::Standard); |
||
236 | currentPage->guides.addHorizontal(y - currentPage->yOffset(), GuideManagerCore::Standard); |
||
237 | m_page = page; |
||
238 | } |
||
239 | if (m_doc->currentPage() != m_doc->Pages->at(m_page)) |
||
240 | m_doc->setCurrentPage( m_doc->Pages->at(m_page) ); |
||
241 | m_doc->changed(); |
||
242 | } |
||
243 | else |
||
244 | { |
||
245 | QCursor* cursor = qApp->overrideCursor(); |
||
246 | if (cursor && (cursor->shape() != Qt::SplitVCursor)) |
||
247 | qApp->changeOverrideCursor(QCursor(Qt::SplitVCursor)); |
||
248 | } |
||
249 | m_currentGuide = y - currentPage->yOffset(); |
||
250 | } |
||
251 | else |
||
252 | { |
||
253 | if (m_haveGuide) |
||
254 | { |
||
255 | qApp->changeOverrideCursor(IconManager::instance().loadCursor("DelPoint.png")); |
||
256 | if (mouseRelease) |
||
257 | { |
||
258 | m_doc->Pages->at(m_page)->guides.deleteHorizontal( m_guide, GuideManagerCore::Standard); |
||
259 | m_haveGuide = false; |
||
260 | m_doc->changed(); |
||
261 | } |
||
262 | } |
||
263 | } |
||
264 | m_canvas->update(0, newMousePoint.y() - 2, m_canvas->width(), 4); |
||
265 | m_canvas->displayCorrectedSingleHUD(m->globalPos(), y, false); |
||
266 | break; |
||
267 | case VERTICAL: |
||
268 | if (!m_ScMW->doc->guidesPrefs().guidesShown) |
||
269 | break; |
||
270 | m_canvas->update(m_xy.x() - 2, 0, 4, m_canvas->height()); |
||
271 | if ((page >= 0) && viewport.contains(m->globalPos())) |
||
272 | { |
||
273 | ScPage* currentPage = m_doc->Pages->at(page); |
||
274 | if (m_doc->SnapElement) |
||
275 | { |
||
276 | double xout = 0; |
||
277 | double yout = 0; |
||
23177 | jghali | 278 | m_doc->getClosestElementBorder(x, y, &xout, &yout, ScribusDoc::IncludeSelection); |
23159 | jghali | 279 | if (xout != x) |
23152 | jghali | 280 | { |
23159 | jghali | 281 | x = xout; |
23152 | jghali | 282 | newMousePoint = m_canvas->canvasToLocal(QPointF(x, y)); |
283 | } |
||
284 | } |
||
285 | if (!m_haveGuide) |
||
286 | { |
||
287 | qApp->changeOverrideCursor(QCursor(Qt::SplitHCursor)); |
||
288 | if (mouseRelease) |
||
289 | { |
||
290 | currentPage->guides.addVertical(x - currentPage->xOffset(), GuideManagerCore::Standard); |
||
291 | m_guide = x - currentPage->xOffset(); |
||
292 | m_page = page; |
||
293 | m_haveGuide = true; |
||
294 | m_doc->changed(); |
||
295 | } |
||
296 | } |
||
297 | else if (mouseRelease) |
||
298 | { |
||
299 | if (page == m_page) |
||
300 | currentPage->guides.moveVertical(m_guide, x - currentPage->xOffset(), GuideManagerCore::Standard); |
||
301 | else |
||
302 | { |
||
303 | m_doc->Pages->at(m_page)->guides.deleteVertical(m_guide, GuideManagerCore::Standard); |
||
304 | currentPage->guides.addVertical(x - currentPage->xOffset(), GuideManagerCore::Standard); |
||
305 | m_page = page; |
||
306 | } |
||
307 | if (m_doc->currentPage() != m_doc->Pages->at(m_page)) |
||
308 | m_doc->setCurrentPage( m_doc->Pages->at(m_page) ); |
||
309 | m_doc->changed(); |
||
310 | } |
||
311 | else |
||
312 | { |
||
313 | QCursor* cursor = qApp->overrideCursor(); |
||
314 | if (cursor && (cursor->shape() != Qt::SplitHCursor)) |
||
315 | qApp->changeOverrideCursor(QCursor(Qt::SplitHCursor)); |
||
316 | } |
||
317 | m_currentGuide = x - currentPage->xOffset(); |
||
318 | } |
||
319 | else |
||
320 | { |
||
321 | if (m_haveGuide) |
||
322 | { |
||
323 | qApp->changeOverrideCursor(IconManager::instance().loadCursor("DelPoint.png")); |
||
324 | if (mouseRelease) |
||
325 | { |
||
326 | m_doc->Pages->at(m_page)->guides.deleteVertical( m_guide, GuideManagerCore::Standard); |
||
327 | m_haveGuide = false; |
||
328 | m_doc->changed(); |
||
329 | } |
||
330 | } |
||
331 | } |
||
332 | m_canvas->update(newMousePoint.x() - 2, 0, 4, m_canvas->height()); |
||
333 | m_canvas->displayCorrectedSingleHUD(m->globalPos(), x, true); |
||
334 | break; |
||
335 | } |
||
336 | m_xy = newMousePoint; |
||
337 | } |
||
338 | |||
339 | |||
340 | void RulerGesture::mouseMoveEvent(QMouseEvent* m) |
||
341 | { |
||
342 | m_mousePoint=m_canvas->globalToCanvas(m->globalPos()); |
||
343 | m->accept(); |
||
344 | if (m_view->moveTimerElapsed()) |
||
345 | { |
||
346 | movePoint(m, false); |
||
347 | if (m_ScMW->doc->guidesPrefs().guidesShown) |
||
348 | emit guideInfo(m_mode, m_currentGuide); |
||
349 | } |
||
350 | } |
||
351 | |||
352 | |||
353 | void RulerGesture::mouseReleaseEvent(QMouseEvent* m) |
||
354 | { |
||
355 | m->accept(); |
||
356 | if (m_view->moveTimerElapsed()) |
||
357 | { |
||
358 | movePoint(m, true); |
||
359 | if (m_mode == ORIGIN) |
||
360 | m_view->setNewRulerOrigin(m); |
||
361 | else |
||
362 | { |
||
363 | if (m_ScMW->doc->guidesPrefs().guidesShown) |
||
364 | m_ScMW->guidePalette->setupPage(); |
||
365 | } |
||
366 | } |
||
367 | m_haveGuide = false; |
||
368 | //#9391: Force redraw to get the guide drawn if we draw a guide in an edit mode |
||
369 | if (m_ScMW->doc->appMode != modeNormal) |
||
370 | m_canvas->setForcedRedraw(true); |
||
371 | m_canvas->repaint(); |
||
372 | m_view->stopGesture(); |
||
373 | m_mousePoint=QPoint(0,0); |
||
374 | if (m_ScMW->doc->guidesPrefs().guidesShown) |
||
375 | emit guideInfo(m_mode, m_currentGuide); |
||
376 | } |
||
377 | |||
378 | |||
379 | void RulerGesture::mousePressEvent(QMouseEvent* m) |
||
380 | { |
||
381 | m->accept(); |
||
382 | FPoint mousePointDoc = m_canvas->globalToCanvas(m->globalPos()); |
||
383 | m_view->registerMousePress(m->globalPos()); |
||
384 | if (mouseHitsGuide(mousePointDoc)) |
||
385 | { |
||
386 | m_xy = m->globalPos() - (m_canvas->mapToParent(QPoint(0, 0)) + m_canvas->parentWidget()->mapToGlobal(QPoint(0, 0))); |
||
387 | } |
||
388 | if (m_ScMW->doc->guidesPrefs().guidesShown) |
||
389 | emit guideInfo(m_mode, m_currentGuide); |
||
390 | } |
||
391 |