summaryrefslogtreecommitdiff
path: root/noncore/graphics/drawpad/filltool.cpp
blob: d323cc208b2087246a6f9e0f74fbbfe129106143 (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
/***************************************************************************
 *                                                                         *
 *   DrawPad - a drawing program for Opie Environment                      *
 *                                                                         *
 *   (C) 2002 by S. Prud'homme <prudhomme@laposte.net>                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "filltool.h"

#include "drawpad.h"
#include "drawpadcanvas.h"
#include "page.h"

#include <qimage.h>

FillTool::FillTool(DrawPad* drawPad, DrawPadCanvas* drawPadCanvas)
    : Tool(drawPad, drawPadCanvas)
{
}

FillTool::~FillTool()
{
}

void FillTool::mousePressEvent(QMouseEvent* e)
{
    int x = e->x();
    int y = e->y();

    m_image = m_pDrawPadCanvas->currentPage()->convertToImage();
    m_fillRgb = m_pDrawPad->brush().color().rgb();
    m_oldRgb = m_image.pixel(x, y);

    if (m_oldRgb != m_fillRgb) {
        fillLine(x, y);

        m_pDrawPadCanvas->currentPage()->convertFromImage(m_image);
        m_pDrawPadCanvas->viewport()->update();

        m_pDrawPadCanvas->backupPage();
    }
}

void FillTool::mouseReleaseEvent(QMouseEvent* e)
{
    Q_UNUSED(e)
}

void FillTool::mouseMoveEvent(QMouseEvent* e)
{
    Q_UNUSED(e)
}

void FillTool::fillLine(int x, int y)
{
    if ((x >= 0) && (x < m_image.width()) && (y >= 0) && (y < m_image.height())) {
        if (m_image.pixel(x, y) == m_oldRgb) {
            int x1, x2;

            x1 = x - 1;
            x2 = x + 1;

            while ((x1 >= 0) && (m_image.pixel(x1, y) == m_oldRgb)) {
                x1--;
            }

            while ((x2 < m_image.width()) && (m_image.pixel(x2, y) == m_oldRgb)) {
                x2++;
            }

            for (int i = x1 + 1; i < x2; i++) {
                m_image.setPixel(i, y, m_fillRgb);
            }

            for (int i = x1 + 1; i < x2; i++) {
                fillLine(i, y - 1);
            }

            for (int i = x1 + 1; i < x2; i++) {
                fillLine(i, y + 1);
            }
        }
    }
}