summaryrefslogtreecommitdiffstats
path: root/kolourpaint/tools/kptoolairspray.cpp
blob: 022c534db0ef017c0f592783085ba56cb573c203 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376

/*
   Copyright (c) 2003,2004,2005 Clarence Dang <dang@kde.org>
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#define DEBUG_KP_TOOL_SPRAYCAN 0

#include <stdlib.h>

#include <tqbitmap.h>
#include <tqpainter.h>
#include <tqpen.h>
#include <tqpixmap.h>
#include <tqpoint.h>
#include <tqpointarray.h>
#include <tqrect.h>
#include <tqtimer.h>

#include <kdebug.h>
#include <klocale.h>

#include <kpcommandhistory.h>
#include <kpdefs.h>
#include <kpdocument.h>
#include <kpmainwindow.h>
#include <kppixmapfx.h>
#include <kptoolairspray.h>
#include <kptooltoolbar.h>
#include <kptoolwidgetspraycansize.h>
#include <kpview.h>
#include <kpviewmanager.h>


/*
 * kpToolAirSpray
 */

kpToolAirSpray::kpToolAirSpray (kpMainWindow *mainWindow)
    : kpTool (i18n ("Spraycan"), i18n ("Sprays graffiti"),
              TQt::Key_Y,
              mainWindow, "tool_spraycan"),
      m_currentCommand (0)
{
    m_timer = new TQTimer (this);
    connect (m_timer, TQT_SIGNAL (timeout ()), this, TQT_SLOT (actuallyDraw ()));
}

kpToolAirSpray::~kpToolAirSpray ()
{
    delete m_currentCommand;
}


// private
TQString kpToolAirSpray::haventBegunDrawUserMessage () const
{
    return i18n ("Click or drag to spray graffiti.");
}

// public virtual
void kpToolAirSpray::begin ()
{
    kpToolToolBar *tb = toolToolBar ();

    m_toolWidgetSpraycanSize = 0;
    m_size = 10;

    if (tb)
    {
        m_toolWidgetSpraycanSize = tb->toolWidgetSpraycanSize ();

        if (m_toolWidgetSpraycanSize)
        {
            m_size = m_toolWidgetSpraycanSize->spraycanSize ();
            connect (m_toolWidgetSpraycanSize, TQT_SIGNAL (spraycanSizeChanged (int)),
                     this, TQT_SLOT (slotSpraycanSizeChanged (int)));

            m_toolWidgetSpraycanSize->show ();
        }
    }

    setUserMessage (haventBegunDrawUserMessage ());
}

// public virtual
void kpToolAirSpray::end ()
{
    if (m_toolWidgetSpraycanSize)
    {
        disconnect (m_toolWidgetSpraycanSize, TQT_SIGNAL (spraycanSizeChanged (int)),
                    this, TQT_SLOT (slotSpraycanSizeChanged (int)));
        m_toolWidgetSpraycanSize = 0;
    }

    setUserMessage (haventBegunDrawUserMessage ());
}

// private slot
void kpToolAirSpray::slotSpraycanSizeChanged (int size)
{
    m_size = size;
}


void kpToolAirSpray::beginDraw ()
{
    m_currentCommand = new kpToolAirSprayCommand (
        color (m_mouseButton),
        m_size,
        mainWindow ());

    // without delay
    actuallyDraw ();

    // use a timer instead of reimplementing draw() (we don't draw all the time)
    m_timer->start (25);

    setUserMessage (cancelUserMessage ());
}

void kpToolAirSpray::draw (const TQPoint &thisPoint, const TQPoint &, const TQRect &)
{
    // if the user is moving the spray, make the spray line continuous
    if (thisPoint != m_lastPoint)
    {
        // without delay
        actuallyDraw ();
    }

    setUserShapePoints (thisPoint);
}

void kpToolAirSpray::actuallyDraw ()
{
    TQPointArray pArray (10);
    int numPoints = 0;

    TQPoint p = m_currentPoint;

#if DEBUG_KP_TOOL_SPRAYCAN
    kdDebug () << "kpToolAirSpray::actuallyDraw() currentPoint=" << p
               << " size=" << m_size
               << endl;
#endif

    int radius = m_size / 2;

    for (int i = 0; i < 10; i++)
    {
        int dx, dy;

        dx = (rand () % m_size) - radius;
        dy = (rand () % m_size) - radius;

        // make it look circular
        // OPT: can be done better
        if (dx * dx + dy * dy <= radius * radius)
            pArray [numPoints++] = TQPoint (p.x () + dx, p.y () + dy);
    }

    pArray.resize (numPoints);

    if (numPoints > 0)
    {
        // leave the command to draw
        m_currentCommand->addPoints (pArray);
    }
}

// virtual
void kpToolAirSpray::cancelShape ()
{
#if 0
    endDraw (TQPoint (), TQRect ());
    mainWindow ()->commandHistory ()->undo ();
#else
    m_timer->stop ();

    m_currentCommand->finalize ();
    m_currentCommand->cancel ();

    delete m_currentCommand;
    m_currentCommand = 0;
#endif

    setUserMessage (i18n ("Let go of all the mouse buttons."));
}

void kpToolAirSpray::releasedAllButtons ()
{
    setUserMessage (haventBegunDrawUserMessage ());
}

// virtual
void kpToolAirSpray::endDraw (const TQPoint &, const TQRect &)
{
    m_timer->stop ();

    m_currentCommand->finalize ();
    mainWindow ()->commandHistory ()->addCommand (m_currentCommand, false /* don't exec */);

    // don't delete - it's up to the commandHistory
    m_currentCommand = 0;

    setUserMessage (haventBegunDrawUserMessage ());
}


/*
 * kpToolAirSprayCommand
 */

kpToolAirSprayCommand::kpToolAirSprayCommand (const kpColor &color, int size,
                                              kpMainWindow *mainWindow)
    : kpCommand (mainWindow),
      m_color (color),
      m_size (size),
      m_newPixmapPtr (0)
{
    m_oldPixmap = *document ()->pixmap ();
}

kpToolAirSprayCommand::~kpToolAirSprayCommand ()
{
    delete m_newPixmapPtr;
}


// public virtual [base kpCommand]
TQString kpToolAirSprayCommand::name () const
{
    return i18n ("Spraycan");
}


// public virtual [base kpCommand]
int kpToolAirSprayCommand::size () const
{
    return kpPixmapFX::pixmapSize (m_newPixmapPtr) +
           kpPixmapFX::pixmapSize (m_oldPixmap);
}


// Redo:
//
// must not call before unexecute() as m_newPixmapPtr is null
// (one reason why we told addCommand() not to execute,
//  the other being that the dots have already been draw onto the doc)
void kpToolAirSprayCommand::execute ()
{
    if (m_newPixmapPtr)
    {
        document ()->setPixmapAt (*m_newPixmapPtr, m_boundingRect.topLeft ());

        // (will be regenerated in unexecute() if required)
        delete m_newPixmapPtr;
        m_newPixmapPtr = 0;
    }
    else
        kdError () << "kpToolAirSprayCommand::execute() has null m_newPixmapPtr" << endl;
}

// Undo:
void kpToolAirSprayCommand::unexecute ()
{
    if (!m_newPixmapPtr)
    {
        // the ultimate in laziness - figure out Redo info only if we Undo
        m_newPixmapPtr = new TQPixmap (m_boundingRect.width (), m_boundingRect.height ());
        *m_newPixmapPtr = document ()->getPixmapAt (m_boundingRect);
    }
    else
        kdError () << "kpToolAirSprayCommand::unexecute() has non-null newPixmapPtr" << endl;

    document ()->setPixmapAt (m_oldPixmap, m_boundingRect.topLeft ());
}


// public
void kpToolAirSprayCommand::addPoints (const TQPointArray &points)
{
    TQRect docRect = points.boundingRect ();

#if DEBUG_KP_TOOL_SPRAYCAN
    kdDebug () << "kpToolAirSprayCommand::addPoints() docRect=" << docRect
               << " numPoints=" << points.count () << endl;
    for (int i = 0; i < (int) points.count (); i++)
        kdDebug () << "\t" << i << ": " << points [i] << endl;
#endif

    TQPixmap pixmap = document ()->getPixmapAt (docRect);
    TQBitmap tqmask;

    TQPainter painter, tqmaskPainter;

    if (m_color.isOpaque ())
    {
        painter.begin (&pixmap);
        painter.setPen (m_color.toTQColor ());
    }

    if (pixmap.tqmask () || m_color.isTransparent ())
    {
        tqmask = kpPixmapFX::getNonNullMask (pixmap);
        tqmaskPainter.begin (&tqmask);
        tqmaskPainter.setPen (m_color.tqmaskColor ());
    }

    for (int i = 0; i < (int) points.count (); i++)
    {
        TQPoint pt (points [i].x () - docRect.x (),
                   points [i].y () - docRect.y ());

        if (painter.isActive ())
            painter.drawPoint (pt);

        if (tqmaskPainter.isActive ())
            tqmaskPainter.drawPoint (pt);
    }

    if (tqmaskPainter.isActive ())
        tqmaskPainter.end ();

    if (painter.isActive ())
        painter.end ();

    if (!tqmask.isNull ())
        pixmap.setMask (tqmask);

    viewManager ()->setFastUpdates ();
    document ()->setPixmapAt (pixmap, docRect.topLeft ());
    viewManager ()->restoreFastUpdates ();

    m_boundingRect = m_boundingRect.unite (docRect);
}

void kpToolAirSprayCommand::finalize ()
{
    // store only needed part of doc pixmap
    m_oldPixmap = kpTool::neededPixmap (m_oldPixmap, m_boundingRect);
}

void kpToolAirSprayCommand::cancel ()
{
    if (m_boundingRect.isValid ())
    {
        viewManager ()->setFastUpdates ();
        document ()->setPixmapAt (m_oldPixmap, m_boundingRect.topLeft ());
        viewManager ()->restoreFastUpdates ();
    }
}

#include <kptoolairspray.moc>