Associate Gwenview with folders too, but ensure we stay behind Dolphin and Konqueror.
[kdegraphics.git] / kolourpaint / tools / kpTool_Utilities.cpp
blob3105aee8579f9f2a0510f0dd15f910c0f9e0d7fc
2 /*
3 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
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
37 #include <kpTool.h>
38 #include <kpToolPrivate.h>
40 #include <limits.h>
42 #include <qapplication.h>
43 #include <qclipboard.h>
44 #include <qcursor.h>
45 #include <qevent.h>
46 #include <qlayout.h>
47 #include <qpainter.h>
48 #include <qpixmap.h>
50 #include <kactioncollection.h>
51 #include <kapplication.h>
52 #include <kdebug.h>
53 #include <kiconloader.h>
54 #include <klocale.h>
55 #include <kmessagebox.h>
57 #include <kpBug.h>
58 #include <kpColor.h>
59 #include <kpColorToolBar.h>
60 #include <kpCommandSize.h>
61 #include <kpDefs.h>
62 #include <kpPainter.h>
63 #include <kpPixmapFX.h>
64 #include <kpToolAction.h>
65 #include <kpToolToolBar.h>
66 #include <kpView.h>
67 #include <kpViewManager.h>
70 // static
71 QRect kpTool::neededRect (const QRect &rect, int lineWidth)
73 int x1, y1, x2, y2;
74 rect.getCoords (&x1, &y1, &x2, &y2);
76 if (lineWidth < 1)
77 lineWidth = 1;
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));
84 // static
85 QPixmap kpTool::neededPixmap (const QPixmap &pixmap, const QRect &boundingRect)
87 return kpPixmapFX::getPixmapAt (pixmap, boundingRect);
91 // public
92 bool kpTool::hasCurrentPoint () const
94 return (viewUnderStartPoint () || viewUnderCursor ());
97 // public
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)")
106 << endl;
107 #endif
109 kpView *v = viewUnderStartPoint ();
110 if (!v)
112 v = viewUnderCursor ();
113 if (!v)
115 #if DEBUG_KP_TOOL && 0
116 kDebug () << "\tno view - returning sentinel";
117 #endif
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
128 << endl;
129 #endif
130 if (!zoomToDoc)
131 return viewPos;
134 const QPoint docPos = v->transformViewToDoc (viewPos);
135 #if DEBUG_KP_TOOL && 0
136 kDebug () << "\tdocPos=" << docPos;
137 #endif
138 return docPos;
142 // public slot
143 void kpTool::somethingBelowTheCursorChanged ()
145 somethingBelowTheCursorChanged (calculateCurrentPoint (),
146 calculateCurrentPoint (false/*view point*/));
149 // private
150 // TODO: don't dup code from mouseMoveEvent()
151 void kpTool::somethingBelowTheCursorChanged (const QPoint &currentPoint_,
152 const QPoint &currentViewPoint_)
154 #if DEBUG_KP_TOOL && 1
155 kDebug () << "kpTool::somethingBelowTheCursorChanged(docPoint="
156 << currentPoint_
157 << " viewPoint="
158 << currentViewPoint_
159 << ")" << endl;
160 kDebug () << "\tviewUnderStartPoint="
161 << (viewUnderStartPoint () ? viewUnderStartPoint ()->objectName () : "(none)")
162 << " viewUnderCursor="
163 << (viewUnderCursor () ? viewUnderCursor ()->objectName () : "(none)")
164 << endl;
165 kDebug () << "\tbegan draw=" << d->beganDraw;
166 #endif
168 d->currentPoint = currentPoint_;
169 d->currentViewPoint = currentViewPoint_;
171 if (d->beganDraw)
173 if (d->currentPoint != KP_INVALID_POINT)
175 draw (d->currentPoint, d->lastPoint, normalizedRect ());
176 d->lastPoint = d->currentPoint;
179 else
181 hover (d->currentPoint);
186 bool kpTool::currentPointNextToLast () const
188 if (d->lastPoint == QPoint (-1, -1))
189 return true;
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))
200 return true;
202 return (d->currentPoint == d->lastPoint ||
203 kpPainter::pointsAreCardinallyAdjacent (d->currentPoint, d->lastPoint));
207 // static
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)
213 return -1;
215 // both left & right together is quite meaningless...
216 const Qt::MouseButtons bothButtons = (Qt::LeftButton | Qt::RightButton);
217 if ((mouseButtons & bothButtons) == bothButtons)
218 return -1;
220 if (mouseButtons & Qt::LeftButton)
221 return 0;
222 else if (mouseButtons & Qt::RightButton)
223 return 1;
224 else
225 return -1;
230 // public static
231 int kpTool::calculateLength (int start, int end)
233 if (start <= end)
235 return end - start + 1;
237 else
239 return end - start - 1;
244 // public static
245 bool kpTool::warnIfBigImageSize (int oldWidth, int oldHeight,
246 int newWidth, int newHeight,
247 const QString &text,
248 const QString &caption,
249 const QString &continueButtonText,
250 QWidget *parent)
252 #if DEBUG_KP_TOOL
253 kDebug () << "kpTool::warnIfBigImageSize()"
254 << " old: w=" << oldWidth << " h=" << oldWidth
255 << " new: w=" << newWidth << " h=" << newHeight
256 << " pixmapSize="
257 << kpPixmapFX::pixmapSize (newWidth,
258 newHeight,
259 QPixmap::defaultDepth ())
260 << " vs BigImageSize=" << KP_BIG_IMAGE_SIZE
261 << endl;
262 #endif
264 // Only got smaller or unchanged - don't complain
265 if (!(newWidth > oldWidth || newHeight > oldHeight))
267 return true;
270 // Was already large - user was warned before, don't annoy him/her again
271 if (kpCommandSize::PixmapSize (oldWidth, oldHeight, QPixmap::defaultDepth ()) >=
272 KP_BIG_IMAGE_SIZE)
274 return true;
277 if (kpCommandSize::PixmapSize (newWidth, newHeight, QPixmap::defaultDepth ()) >=
278 KP_BIG_IMAGE_SIZE)
280 int accept = KMessageBox::warningContinueCancel (parent,
281 text,
282 caption,
283 KGuiItem (continueButtonText),
284 KStandardGuiItem::cancel(),
285 QLatin1String ("BigImageDontAskAgain"));
287 return (accept == KMessageBox::Continue);
289 else
291 return true;