#ifndef COLORINGCANVAS_H
#define COLORINGCANVAS_H

#include <QtDeclarative/QDeclarativeItem>
#include <QtGui>

class ColoringCanvas : public QDeclarativeItem
{
    Q_OBJECT
    Q_DISABLE_COPY(ColoringCanvas)

    Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
    Q_PROPERTY(QString source READ source WRITE setSource NOTIFY sourceChanged)

public:
    explicit ColoringCanvas(QDeclarativeItem *parent = 0);
    void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
    QColor color() const { return color_; };
    QString source() const { return source_; };

signals:
    void colorChanged();
    void sourceChanged();

public slots:
    void setSource(const QString& newSource);
    void setColor(const QColor newColor);
    void click(int x, int y);

protected:
    bool test(const QPoint& point, const QRgb& color);
    void floodFill(const QPoint& point, const QRgb& fillColor);
    void drawHorizontalLine(const QPoint & p1, const QPoint & p2, const QRgb& fillColor);

    // Checks if point has ignore color and puts a valid point in valid
    // returns false if no valid point found
    bool check(const QPoint& point, QPoint& valid);

private:
    QColor color_;
    QString source_;
    QColor ignore_;
    QImage image_;
};

QML_DECLARE_TYPE(ColoringCanvas)

#endif // COLORINGCANVAS_H

