File indexing completed on 2024-05-12 04:21:26

0001 
0002 /*
0003    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
0004    All rights reserved.
0005 
0006    Redistribution and use in source and binary forms, with or without
0007    modification, are permitted provided that the following conditions
0008    are met:
0009 
0010    1. Redistributions of source code must retain the above copyright
0011       notice, this list of conditions and the following disclaimer.
0012    2. Redistributions in binary form must reproduce the above copyright
0013       notice, this list of conditions and the following disclaimer in the
0014       documentation and/or other materials provided with the distribution.
0015 
0016    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0017    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0018    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0019    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0020    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0021    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0022    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0023    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0025    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026 */
0027 
0028 
0029 #define DEBUG_KP_TOOL_POLYLINE 0
0030 
0031 
0032 #include "kpToolPolyline.h"
0033 #include "kpLogCategories.h"
0034 #include "environments/tools/kpToolEnvironment.h"
0035 #include "pixmapfx/kpPixmapFX.h"
0036 
0037 #include <KLocalizedString>
0038 
0039 #include <QPainter>
0040 #include <QPen>
0041 
0042 //--------------------------------------------------------------------------------
0043 
0044 kpToolPolyline::kpToolPolyline (kpToolEnvironment *environ, QObject *parent)
0045     : kpToolPolygonalBase (
0046         i18n ("Connected Lines"),
0047         i18n ("Draws connected lines"),
0048         &drawShape,
0049         Qt::Key_N,
0050         environ, parent,
0051         QStringLiteral("tool_polyline"))
0052 {
0053 }
0054 
0055 //--------------------------------------------------------------------------------
0056 
0057 // private virtual [base kpToolPolygonalBase]
0058 QString kpToolPolyline::haventBegunShapeUserMessage () const
0059 {
0060     return i18n ("Drag to draw the first line.");
0061 }
0062 
0063 //--------------------------------------------------------------------------------
0064 // public static
0065 
0066 void kpToolPolyline::drawShape(kpImage *image,
0067         const QPolygon &points,
0068         const kpColor &fcolor, int penWidth,
0069         const kpColor &bcolor,
0070         bool isFinal)
0071 {
0072     (void) bcolor;
0073     (void) isFinal;
0074 
0075   QPainter painter(image);
0076   painter.setRenderHint(QPainter::Antialiasing, kpToolEnvironment::drawAntiAliased);
0077 
0078   painter.setPen(QPen(fcolor.toQColor(), penWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
0079 
0080   if ( kpPixmapFX::Only1PixelInPointArray(points) ) {
0081     painter.drawPoint(points[0]);
0082   }
0083   else {
0084     painter.drawPolyline(points);
0085   }
0086 }
0087 
0088 //--------------------------------------------------------------------------------
0089 
0090 // public virtual [base kpTool]
0091 void kpToolPolyline::endDraw (const QPoint &, const QRect &)
0092 {
0093 #if DEBUG_KP_TOOL_POLYLINE
0094     qCDebug(kpLogTools) << "kpToolPolyline::endDraw()  points="
0095         << points ()->toList ();
0096 #endif
0097 
0098     // A click of the other mouse button (to finish shape, instead of adding
0099     // another control point) would have caused endShape() to have been
0100     // called in kpToolPolygonalBase::beginDraw().  The points list would now
0101     // be empty.  We are being called by kpTool::mouseReleaseEvent().
0102     if (points ()->count () == 0) {
0103         return;
0104     }
0105 
0106     if (points ()->count () >= kpToolPolygonalBase::MaxPoints)
0107     {
0108     #if DEBUG_KP_TOOL_POLYLINE
0109         qCDebug(kpLogTools) << "\tending shape";
0110     #endif
0111         endShape ();
0112         return;
0113     }
0114 
0115     if (originatingMouseButton () == 0)
0116     {
0117         setUserMessage (i18n ("Left drag another line or right click to finish."));
0118     }
0119     else
0120     {
0121         setUserMessage (i18n ("Right drag another line or left click to finish."));
0122     }
0123 }
0124 
0125 #include "moc_kpToolPolyline.cpp"