1 /*
2    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
3    Copyright (c) 2017      Martin Koller <kollix@aon.at>
4    All rights reserved.
5 
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9 
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.
15 
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.
26 */
27 
28 
29 #include "kpToolPen.h"
30 
31 #include "imagelib/kpColor.h"
32 #include "document/kpDocument.h"
33 #include "imagelib/kpImage.h"
34 #include "imagelib/kpPainter.h"
35 #include "commands/tools/flow/kpToolFlowCommand.h"
36 #include "environments/tools/kpToolEnvironment.h"
37 
38 #include <KLocalizedString>
39 
40 #include <QPainter>
41 #include <QPen>
42 
43 //---------------------------------------------------------------------
44 
kpToolPen(kpToolEnvironment * environ,QObject * parent)45 kpToolPen::kpToolPen (kpToolEnvironment *environ, QObject *parent)
46     : kpToolFlowBase (i18n ("Pen"), i18n ("Draws dots and freehand strokes"),
47         Qt::Key_P,
48         environ, parent, QStringLiteral("tool_pen"))
49 {
50 }
51 
52 //---------------------------------------------------------------------
53 
54 // protected virtual [base kpToolFlowBase]
haventBegunDrawUserMessage() const55 QString kpToolPen::haventBegunDrawUserMessage () const
56 {
57     return i18n ("Click to draw dots or drag to draw strokes.");
58 }
59 
60 //---------------------------------------------------------------------
61 
62 // protected virtual [base kpToolFlowBase]
drawLine(const QPoint & thisPoint,const QPoint & lastPoint)63 QRect kpToolPen::drawLine (const QPoint &thisPoint, const QPoint &lastPoint)
64 {
65   QRect docRect = kpPainter::normalizedRect(thisPoint, lastPoint);
66   docRect = neededRect (docRect, 1/*pen width*/);
67   kpImage image = document ()->getImageAt (docRect);
68 
69   const QPoint sp = lastPoint - docRect.topLeft (),
70                ep = thisPoint - docRect.topLeft ();
71 
72   QPainter painter(&image);
73 
74   // never use AA - it does not look good for the usually very short lines
75   //painter.setRenderHint(QPainter::Antialiasing, kpToolEnvironment::drawAntiAliased);
76 
77   painter.setPen(color(mouseButton()).toQColor());
78   painter.drawLine(sp, ep);
79 
80   document ()->setImageAt (image, docRect.topLeft ());
81   return docRect;
82 }
83 
84 //--------------------------------------------------------------------------------
85