summaryrefslogtreecommitdiffstats
path: root/ksquirrel/sq_glselectionpainter.h
blob: ea39286f7efd81d26a902e1bf58b29345a4573ac (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
/***************************************************************************
                          sq_glselectionpainter.h  -  description
                             -------------------
    begin                : Apr 4 2007
    copyright            : (C) 2007 by Baryshev Dmitry
    email                : ksquirrel.iv@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef SQ_GLSELECTIONPAINTER_H
#define SQ_GLSELECTIONPAINTER_H

#include <tqrect.h>
#include <tqpoint.h>

class SQ_GLWidget;

/*
 *  This is a selection painter for SQ_GLWidget.
 *
 *  Selection can be shown as rectangle and ellipse.
 *
 *  Ellipsis selection mainly used in redeye filter.
 */

class SQ_GLSelectionPainter
{
    public:
        enum Type { Rectangle, Ellipse };

        SQ_GLSelectionPainter(SQ_GLWidget *widget);
        ~SQ_GLSelectionPainter();

        void setSourceSize(int, int);

        TQPoint center() const;

        void draw();

        /*
         *  Set selection type - rectangle or ellipse
         */
        int type() const;

        /*
         *  when selection is drawn and visible, it's valid.
         *  After end() it becomes invalid.
         */
        bool valid() const;

        void setVisible(bool vis);

        void begin(Type tp, int x, int y, bool U = true);
        void move(int x, int y);
        void setGeometry(const TQRect &rc);
        void end();

        /*
         *  Selected rectangle geometry
         */
        TQPoint pos() const;
        TQSize size() const;

    private:
        void drawEllipse(float xradius, float yradius);
        void drawRect();
        void hackXY(int &x, int &y);

    private:
        SQ_GLWidget *w;
        int       sourcew, sourceh;
        int       sw, sh, sx, sy;

        int       angle;
        int       xmoveold, ymoveold;
        bool      m_valid, m_shown;
        Type      m_type;
};

inline
TQPoint SQ_GLSelectionPainter::pos() const
{
    return valid() ? TQPoint(sourcew/2 + sx, sourceh/2 - sy) : TQPoint();
}

inline
TQSize SQ_GLSelectionPainter::size() const
{
    return valid() ? TQSize(sw, sh) : TQSize();
}

inline
int SQ_GLSelectionPainter::type() const
{
    return m_type;
}

inline
void SQ_GLSelectionPainter::setGeometry(const TQRect &rc)
{
    int X = rc.x(), Y = rc.y();

    hackXY(X, Y);

    sx = X;
    sy = Y;
    sw = rc.width();
    sh = rc.height();
}

inline
void SQ_GLSelectionPainter::setSourceSize(int w, int h)
{
    sourcew = w;
    sourceh = h;
}

inline
void SQ_GLSelectionPainter::setVisible(bool vis)
{
    if(m_valid) m_shown = vis;
}

inline
bool SQ_GLSelectionPainter::valid() const
{
    return m_valid && m_shown;
}

inline
TQPoint SQ_GLSelectionPainter::center() const
{
    return TQPoint(sx + sw/2, sy - sh/2);
}

inline
void SQ_GLSelectionPainter::hackXY(int &x, int &y)
{
    x -= sourcew / 2;
    y =  sourceh / 2 - y;
}

#endif