3 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 // Tool utility methods - mainly for subclasses' convenience.
33 #define DEBUG_KP_TOOL 0
36 // TODO: reduce number of includes
38 #include <kpToolPrivate.h>
42 #include <qapplication.h>
43 #include <qclipboard.h>
50 #include <kactioncollection.h>
51 #include <kapplication.h>
53 #include <kiconloader.h>
55 #include <kmessagebox.h>
59 #include <kpColorToolBar.h>
60 #include <kpCommandSize.h>
62 #include <kpPainter.h>
63 #include <kpPixmapFX.h>
64 #include <kpToolAction.h>
65 #include <kpToolToolBar.h>
67 #include <kpViewManager.h>
71 QRect
kpTool::neededRect (const QRect
&rect
, int lineWidth
)
74 rect
.getCoords (&x1
, &y1
, &x2
, &y2
);
79 // TODO: why not divide by 2?
80 return QRect (QPoint (x1
- lineWidth
+ 1, y1
- lineWidth
+ 1),
81 QPoint (x2
+ lineWidth
- 1, y2
+ lineWidth
- 1));
85 QPixmap
kpTool::neededPixmap (const QPixmap
&pixmap
, const QRect
&boundingRect
)
87 return kpPixmapFX::getPixmapAt (pixmap
, boundingRect
);
92 bool kpTool::hasCurrentPoint () const
94 return (viewUnderStartPoint () || viewUnderCursor ());
98 QPoint
kpTool::calculateCurrentPoint (bool zoomToDoc
) const
100 #if DEBUG_KP_TOOL && 0
101 kDebug () << "kpTool::currentPoint(zoomToDoc=" << zoomToDoc
<< ")";
102 kDebug () << "\tviewUnderStartPoint="
103 << (viewUnderStartPoint () ? viewUnderStartPoint ()->objectName () : "(none)")
104 << " viewUnderCursor="
105 << (viewUnderCursor () ? viewUnderCursor ()->objectName () : "(none)")
109 kpView
*v
= viewUnderStartPoint ();
112 v
= viewUnderCursor ();
115 #if DEBUG_KP_TOOL && 0
116 kDebug () << "\tno view - returning sentinel";
118 return KP_INVALID_POINT
;
123 const QPoint globalPos
= QCursor::pos ();
124 const QPoint viewPos
= v
->mapFromGlobal (globalPos
);
125 #if DEBUG_KP_TOOL && 0
126 kDebug () << "\tglobalPos=" << globalPos
127 << " viewPos=" << viewPos
134 const QPoint docPos
= v
->transformViewToDoc (viewPos
);
135 #if DEBUG_KP_TOOL && 0
136 kDebug () << "\tdocPos=" << docPos
;
143 void kpTool::somethingBelowTheCursorChanged ()
145 somethingBelowTheCursorChanged (calculateCurrentPoint (),
146 calculateCurrentPoint (false/*view point*/));
150 // TODO: don't dup code from mouseMoveEvent()
151 void kpTool::somethingBelowTheCursorChanged (const QPoint
¤tPoint_
,
152 const QPoint
¤tViewPoint_
)
154 #if DEBUG_KP_TOOL && 1
155 kDebug () << "kpTool::somethingBelowTheCursorChanged(docPoint="
160 kDebug () << "\tviewUnderStartPoint="
161 << (viewUnderStartPoint () ? viewUnderStartPoint ()->objectName () : "(none)")
162 << " viewUnderCursor="
163 << (viewUnderCursor () ? viewUnderCursor ()->objectName () : "(none)")
165 kDebug () << "\tbegan draw=" << d
->beganDraw
;
168 d
->currentPoint
= currentPoint_
;
169 d
->currentViewPoint
= currentViewPoint_
;
173 if (d
->currentPoint
!= KP_INVALID_POINT
)
175 draw (d
->currentPoint
, d
->lastPoint
, normalizedRect ());
176 d
->lastPoint
= d
->currentPoint
;
181 hover (d
->currentPoint
);
186 bool kpTool::currentPointNextToLast () const
188 if (d
->lastPoint
== QPoint (-1, -1))
191 int dx
= qAbs (d
->currentPoint
.x () - d
->lastPoint
.x ());
192 int dy
= qAbs (d
->currentPoint
.y () - d
->lastPoint
.y ());
194 return (dx
<= 1 && dy
<= 1);
197 bool kpTool::currentPointCardinallyNextToLast () const
199 if (d
->lastPoint
== QPoint (-1, -1))
202 return (d
->currentPoint
== d
->lastPoint
||
203 kpPainter::pointsAreCardinallyAdjacent (d
->currentPoint
, d
->lastPoint
));
208 // TODO: we don't handle Qt::XButton1 and Qt::XButton2 at the moment.
209 int kpTool::mouseButton (Qt::MouseButtons mouseButtons
)
211 // we have nothing to do with mid-buttons
212 if (mouseButtons
& Qt::MidButton
)
215 // both left & right together is quite meaningless...
216 const Qt::MouseButtons bothButtons
= (Qt::LeftButton
| Qt::RightButton
);
217 if ((mouseButtons
& bothButtons
) == bothButtons
)
220 if (mouseButtons
& Qt::LeftButton
)
222 else if (mouseButtons
& Qt::RightButton
)
231 int kpTool::calculateLength (int start
, int end
)
235 return end
- start
+ 1;
239 return end
- start
- 1;
245 bool kpTool::warnIfBigImageSize (int oldWidth
, int oldHeight
,
246 int newWidth
, int newHeight
,
248 const QString
&caption
,
249 const QString
&continueButtonText
,
253 kDebug () << "kpTool::warnIfBigImageSize()"
254 << " old: w=" << oldWidth
<< " h=" << oldWidth
255 << " new: w=" << newWidth
<< " h=" << newHeight
257 << kpPixmapFX::pixmapSize (newWidth
,
259 QPixmap::defaultDepth ())
260 << " vs BigImageSize=" << KP_BIG_IMAGE_SIZE
264 // Only got smaller or unchanged - don't complain
265 if (!(newWidth
> oldWidth
|| newHeight
> oldHeight
))
270 // Was already large - user was warned before, don't annoy him/her again
271 if (kpCommandSize::PixmapSize (oldWidth
, oldHeight
, QPixmap::defaultDepth ()) >=
277 if (kpCommandSize::PixmapSize (newWidth
, newHeight
, QPixmap::defaultDepth ()) >=
280 int accept
= KMessageBox::warningContinueCancel (parent
,
283 KGuiItem (continueButtonText
),
284 KStandardGuiItem::cancel(),
285 QLatin1String ("BigImageDontAskAgain"));
287 return (accept
== KMessageBox::Continue
);