Rev 23145 | Rev 23390 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
20561 | 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 | |||
17 | #include "canvasmode_drawfreehand.h" |
||
18 | |||
19 | #include <QEvent> |
||
20 | #include <QMouseEvent> |
||
21 | #include <QPainterPath> |
||
22 | #include <QPoint> |
||
23 | #include <QRect> |
||
24 | |||
25 | #include "KarbonCurveFit.h" |
||
26 | #include "appmodes.h" |
||
27 | #include "canvas.h" |
||
28 | #include "fpoint.h" |
||
29 | #include "prefsmanager.h" |
||
30 | #include "scribus.h" |
||
31 | #include "scribusXml.h" |
||
32 | #include "scribusdoc.h" |
||
33 | #include "scribusview.h" |
||
34 | #include "selection.h" |
||
35 | #include "ui/pageselector.h" |
||
36 | #include "ui/scrspinbox.h" |
||
37 | #include "undomanager.h" |
||
38 | #include "util.h" |
||
39 | #include "util_math.h" |
||
40 | |||
41 | |||
42 | FreehandMode::FreehandMode(ScribusView* view) : CanvasMode(view) |
||
43 | { |
||
23144 | jghali | 44 | m_xp = m_yp = -1; |
45 | m_mouseButtonPressed = false; |
||
20561 | jghali | 46 | } |
47 | |||
48 | void FreehandMode::drawControls(QPainter* p) |
||
49 | { |
||
50 | // |
||
51 | } |
||
52 | |||
53 | inline bool FreehandMode::GetItem(PageItem** pi) |
||
54 | { |
||
55 | *pi = m_doc->m_Selection->itemAt(0); |
||
22518 | craig | 56 | return (*pi) != nullptr; |
20561 | jghali | 57 | } |
58 | |||
59 | void FreehandMode::enterEvent(QEvent *) |
||
60 | { |
||
23144 | jghali | 61 | if (!m_mouseButtonPressed) |
20561 | jghali | 62 | { |
63 | setModeCursor(); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | |||
68 | void FreehandMode::leaveEvent(QEvent *e) |
||
69 | { |
||
70 | } |
||
71 | |||
72 | |||
73 | void FreehandMode::activate(bool flag) |
||
74 | { |
||
23144 | jghali | 75 | m_xp = m_yp = -1; |
76 | m_mouseButtonPressed = false; |
||
20561 | jghali | 77 | setModeCursor(); |
78 | } |
||
79 | |||
80 | void FreehandMode::deactivate(bool flag) |
||
81 | { |
||
82 | m_view->setRedrawMarkerShown(false); |
||
83 | } |
||
84 | |||
85 | void FreehandMode::mouseDoubleClickEvent(QMouseEvent *m) |
||
86 | { |
||
87 | m->accept(); |
||
23144 | jghali | 88 | m_mouseButtonPressed = false; |
20561 | jghali | 89 | m_canvas->resetRenderMode(); |
90 | mousePressEvent(m); |
||
91 | } |
||
92 | |||
93 | |||
94 | void FreehandMode::mouseMoveEvent(QMouseEvent *m) |
||
95 | { |
||
96 | const FPoint mousePointDoc = m_canvas->globalToCanvas(m->globalPos()); |
||
97 | PageItem *currItem; |
||
98 | FPoint npf, npf2; |
||
22289 | craig | 99 | // QRect tx; |
20561 | jghali | 100 | m->accept(); |
101 | m_canvas->displayCorrectedXYHUD(m->globalPos(), mousePointDoc.x(), mousePointDoc.y()); |
||
102 | if (commonMouseMove(m)) |
||
103 | return; |
||
23144 | jghali | 104 | if (m_mouseButtonPressed && (m_doc->appMode == modeDrawFreehandLine)) |
20561 | jghali | 105 | { |
106 | double newXF = mousePointDoc.x(); //m_view->translateToDoc(m->x(), m->y()).x(); |
||
107 | double newYF = mousePointDoc.y(); //m_view->translateToDoc(m->x(), m->y()).y(); |
||
23144 | jghali | 108 | if (!m_poly.empty()) |
20561 | jghali | 109 | { |
23144 | jghali | 110 | if (FPoint(newXF, newYF) != m_poly.point(m_poly.size()-1)) |
111 | m_poly.addPoint(FPoint(newXF, newYF)); |
||
20561 | jghali | 112 | } |
113 | else |
||
23144 | jghali | 114 | m_poly.addPoint(FPoint(newXF, newYF)); |
20561 | jghali | 115 | QPolygon& redrawPolygon(m_canvas->newRedrawPolygon()); |
23144 | jghali | 116 | for (int pp = 0; pp < m_poly.size(); pp++) |
20561 | jghali | 117 | { |
23144 | jghali | 118 | redrawPolygon << m_poly.pointQ(pp); |
20561 | jghali | 119 | } |
120 | m_canvas->m_viewMode.operItemResizing = true; |
||
121 | QRect bRect = m_canvas->redrawPolygon().boundingRect(); |
||
122 | m_view->updateCanvas(bRect); |
||
123 | return; |
||
124 | } |
||
125 | if (GetItem(&currItem)) |
||
126 | { |
||
127 | if (m_doc->DragP) |
||
128 | return; |
||
129 | |||
23144 | jghali | 130 | if ((!m_mouseButtonPressed) && (m_doc->appMode != modeDrawBezierLine)) |
20561 | jghali | 131 | { |
132 | if (m_doc->m_Selection->isMultipleSelection()) |
||
133 | { |
||
134 | setModeCursor(); |
||
135 | return; |
||
136 | } |
||
137 | for (int a = 0; a < m_doc->m_Selection->count(); ++a) |
||
138 | { |
||
139 | currItem = m_doc->m_Selection->itemAt(a); |
||
140 | if (currItem->locked()) |
||
141 | break; |
||
142 | setModeCursor(); |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 | else |
||
147 | { |
||
23144 | jghali | 148 | if ((m_mouseButtonPressed) && (m->buttons() & Qt::LeftButton)) |
20561 | jghali | 149 | { |
23144 | jghali | 150 | QPoint startP = m_canvas->canvasToGlobal(QPointF(m_xp, m_yp)); |
20561 | jghali | 151 | m_view->redrawMarker->setGeometry(QRect(m_view->mapFromGlobal(startP), m_view->mapFromGlobal(m->globalPos())).normalized()); |
152 | m_view->setRedrawMarkerShown(true); |
||
153 | m_view->HaveSelRect = true; |
||
154 | return; |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | |||
159 | void FreehandMode::mousePressEvent(QMouseEvent *m) |
||
160 | { |
||
161 | const FPoint mousePointDoc = m_canvas->globalToCanvas(m->globalPos()); |
||
162 | double Rxp = 0; |
||
163 | double Ryp = 0; |
||
164 | FPoint npf, npf2; |
||
22289 | craig | 165 | // QRect tx; |
20561 | jghali | 166 | QTransform pm; |
23144 | jghali | 167 | m_mouseButtonPressed = true; |
20561 | jghali | 168 | m_view->HaveSelRect = false; |
169 | m_doc->DragP = false; |
||
170 | m_doc->leaveDrag = false; |
||
171 | m->accept(); |
||
172 | m_view->registerMousePress(m->globalPos()); |
||
23144 | jghali | 173 | m_xp = mousePointDoc.x(); //qRound(m->x()/m_canvas->scale() + 0*m_doc->minCanvasCoordinate.x()); |
174 | m_yp = mousePointDoc.y(); //qRound(m->y()/m_canvas->scale() + 0*m_doc->minCanvasCoordinate.y()); |
||
22289 | craig | 175 | // QRect mpo(m->x()-m_doc->guidesPrefs().grabRadius, m->y()-m_doc->guidesPrefs().grabRadius, m_doc->guidesPrefs().grabRadius*2, m_doc->guidesPrefs().grabRadius*2); |
23144 | jghali | 176 | Rxp = m_doc->ApplyGridF(FPoint(m_xp, m_yp)).x(); |
177 | m_xp = qRound(Rxp); |
||
178 | Ryp = m_doc->ApplyGridF(FPoint(m_xp, m_yp)).y(); |
||
179 | m_yp = qRound(Ryp); |
||
20561 | jghali | 180 | if (m->button() == Qt::MidButton) |
181 | { |
||
182 | m_view->MidButt = true; |
||
183 | if (m->modifiers() & Qt::ControlModifier) |
||
184 | m_view->DrawNew(); |
||
185 | return; |
||
186 | } |
||
187 | if (m->button() != Qt::LeftButton) |
||
188 | { |
||
189 | m_view->stopGesture(); |
||
190 | return; |
||
191 | } |
||
23144 | jghali | 192 | m_poly.resize(0); |
20561 | jghali | 193 | m_view->Deselect(false); |
23144 | jghali | 194 | m_xp = mousePointDoc.x(); //qRound(m->x()/m_canvas->scale() + 0*m_doc->minCanvasCoordinate.x()); |
195 | m_yp = mousePointDoc.y(); //qRound(m->y()/m_canvas->scale() + 0*m_doc->minCanvasCoordinate.y()); |
||
20561 | jghali | 196 | m_canvas->setRenderModeFillBuffer(); |
197 | undoManager->setUndoEnabled(false); |
||
198 | } |
||
199 | |||
200 | |||
201 | |||
202 | void FreehandMode::mouseReleaseEvent(QMouseEvent *m) |
||
203 | { |
||
204 | undoManager->setUndoEnabled(true); |
||
205 | PageItem *currItem; |
||
23144 | jghali | 206 | m_mouseButtonPressed = false; |
20561 | jghali | 207 | m_canvas->resetRenderMode(); |
208 | m->accept(); |
||
209 | if (m_doc->appMode == modeDrawFreehandLine) |
||
210 | { |
||
23144 | jghali | 211 | if (m_poly.size() > 1) |
20561 | jghali | 212 | { |
213 | UndoTransaction createTransaction(UndoManager::instance()->beginTransaction()); |
||
23144 | jghali | 214 | uint z = m_doc->itemAdd(PageItem::PolyLine, PageItem::Unspecified, m_xp, m_yp, 1, 1, m_doc->itemToolPrefs().lineWidth, CommonStrings::None, m_doc->itemToolPrefs().lineColor); |
20561 | jghali | 215 | currItem = m_doc->Items->at(z); |
216 | currItem->PoLine.resize(0); |
||
217 | if (m->modifiers() & Qt::ControlModifier) |
||
218 | { |
||
219 | QList<QPointF> clip; |
||
23144 | jghali | 220 | for (int px = 0; px < m_poly.size()-1; ++px) |
20561 | jghali | 221 | { |
23144 | jghali | 222 | FPoint clp = m_poly.point(px); |
20561 | jghali | 223 | clip.append(QPointF(clp.x(), clp.y())); |
224 | } |
||
225 | QPainterPath pp = bezierFit(clip, 5.0); |
||
226 | currItem->PoLine.fromQPainterPath(pp); |
||
227 | } |
||
228 | else |
||
229 | { |
||
23144 | jghali | 230 | currItem->PoLine.addPoint(m_poly.point(0)); |
231 | currItem->PoLine.addPoint(m_poly.point(0)); |
||
232 | for (int px = 1; px < m_poly.size()-1; ++px) |
||
20561 | jghali | 233 | { |
23144 | jghali | 234 | currItem->PoLine.addPoint(m_poly.point(px)); |
235 | currItem->PoLine.addPoint(m_poly.point(px)); |
||
236 | currItem->PoLine.addPoint(m_poly.point(px)); |
||
237 | currItem->PoLine.addPoint(m_poly.point(px)); |
||
20561 | jghali | 238 | } |
23144 | jghali | 239 | currItem->PoLine.addPoint(m_poly.point(m_poly.size()-1)); |
240 | currItem->PoLine.addPoint(m_poly.point(m_poly.size()-1)); |
||
20561 | jghali | 241 | } |
242 | FPoint tp2(getMinClipF(&currItem->PoLine)); |
||
243 | currItem->setXYPos(tp2.x(), tp2.y(), true); |
||
244 | currItem->PoLine.translate(-tp2.x(), -tp2.y()); |
||
245 | FPoint tp(getMaxClipF(&currItem->PoLine)); |
||
20694 | craig | 246 | m_doc->sizeItem(tp.x(), tp.y(), currItem, false, false, false); |
247 | m_doc->adjustItemSize(currItem); |
||
20561 | jghali | 248 | m_doc->m_Selection->clear(); |
249 | m_doc->m_Selection->addItem(currItem); |
||
250 | currItem->ClipEdited = true; |
||
251 | currItem->FrameType = 3; |
||
252 | currItem->OwnPage = m_doc->OnPage(currItem); |
||
253 | m_view->resetMousePressed(); |
||
254 | currItem->checkChanges(); |
||
255 | QString targetName = Um::ScratchSpace; |
||
256 | if (currItem->OwnPage > -1) |
||
257 | targetName = m_doc->Pages->at(currItem->OwnPage)->getUName(); |
||
258 | createTransaction.commit(targetName, currItem->getUPixmap(), Um::Create + " " + currItem->getUName(), "", Um::ICreate); |
||
259 | //FIXME |
||
260 | m_canvas->m_viewMode.operItemResizing = false; |
||
261 | m_doc->changed(); |
||
262 | } |
||
23060 | craig | 263 | if (!PrefsManager::instance().appPrefs.uiPrefs.stickyTools) |
20561 | jghali | 264 | { |
265 | m_view->requestMode(modeNormal); |
||
266 | } |
||
267 | else |
||
268 | m_view->requestMode(m_doc->appMode); |
||
269 | // itemAdd calls PageItem::update emit DocChanged(); |
||
270 | return; |
||
271 | } |
||
272 | |||
273 | m_canvas->setRenderModeUseBuffer(false); |
||
274 | |||
275 | m_doc->DragP = false; |
||
276 | m_doc->leaveDrag = false; |
||
277 | m_view->MidButt = false; |
||
278 | if (m_view->groupTransactionStarted()) |
||
279 | { |
||
280 | for (int i = 0; i < m_doc->m_Selection->count(); ++i) |
||
281 | m_doc->m_Selection->itemAt(i)->checkChanges(true); |
||
282 | m_view->endGroupTransaction(); |
||
283 | } |
||
284 | |||
285 | for (int i = 0; i < m_doc->m_Selection->count(); ++i) |
||
286 | m_doc->m_Selection->itemAt(i)->checkChanges(true); |
||
287 | |||
288 | //Commit drag created items to undo manager. |
||
22518 | craig | 289 | if (m_doc->m_Selection->itemAt(0)!=nullptr) |
20561 | jghali | 290 | { |
291 | m_doc->itemAddCommit(m_doc->m_Selection->itemAt(0)); |
||
292 | } |
||
21026 | craig | 293 | //Make sure the Zoom spinbox and page selector don't have focus if we click on the canvas |
20561 | jghali | 294 | m_view->m_ScMW->zoomSpinBox->clearFocus(); |
295 | m_view->m_ScMW->pageSelector->clearFocus(); |
||
22599 | craig | 296 | if (m_doc->m_Selection->itemAt(0) != nullptr) // is there the old clip stored for the undo action |
20561 | jghali | 297 | { |
298 | currItem = m_doc->m_Selection->itemAt(0); |
||
299 | m_doc->nodeEdit.finishTransaction(currItem); |
||
300 | } |
||
301 | } |
||
302 | |||
303 | void FreehandMode::selectPage(QMouseEvent *m) |
||
304 | { |
||
23144 | jghali | 305 | m_mouseButtonPressed = true; |
20561 | jghali | 306 | FPoint mousePointDoc = m_canvas->globalToCanvas(m->globalPos()); |
23144 | jghali | 307 | m_xp = mousePointDoc.x(); //static_cast<int>(m->x()/m_canvas->scale()); |
308 | m_yp = mousePointDoc.y(); //static_cast<int>(m->y()/m_canvas->scale()); |
||
20561 | jghali | 309 | m_doc->nodeEdit.deselect(); |
310 | m_view->Deselect(false); |
||
23145 | jghali | 311 | |
312 | if (m_doc->masterPageMode()) |
||
313 | return; |
||
314 | |||
315 | int i = m_doc->OnPage(m_xp, m_yp); |
||
316 | if (i < 0) |
||
317 | return; |
||
318 | |||
319 | uint docCurrPageNo = m_doc->currentPageNumber(); |
||
320 | uint j=static_cast<uint>(i); |
||
321 | if (docCurrPageNo != j) |
||
20561 | jghali | 322 | { |
23145 | jghali | 323 | m_doc->setCurrentPage(m_doc->Pages->at(j)); |
324 | m_view->m_ScMW->slotSetCurrentPage(j); |
||
325 | m_view->DrawNew(); |
||
20561 | jghali | 326 | } |
327 | } |