summaryrefslogtreecommitdiffstats
path: root/ksquirrel/sq_glselectionpainter.cpp
blob: 3d60389e485a44e6a262c36f178e80247b4cd5b7 (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
/***************************************************************************
                          sq_glselectionpainter.cpp  -  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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <qbitmap.h>
#include <qpainter.h>

#include <cmath>

#include "sq_glwidget.h"
#include "sq_glselectionpainter.h"

static const int len  = 4;
static const int len2 = 10;
static const int lenc = 2;
static const int lenc2 = lenc / 2;
static const double rad_const = 3.14159265358979323846 / 180.0;

/* ******************************************************************* */

SQ_GLSelectionPainter::SQ_GLSelectionPainter(SQ_GLWidget *widget) 
    : w(widget), sourcew(1), sourceh(1), sw(0), sh(0), sx(0), sy(0), 
      angle(0), m_valid(false), m_shown(false)
{}

SQ_GLSelectionPainter::~SQ_GLSelectionPainter()
{}

void SQ_GLSelectionPainter::begin(Type tp, int x, int y, bool U)
{
    // end previous drawing if any
    end();

    m_type = tp;

    hackXY(x, y);

    sx = xmoveold = x;
    sy = ymoveold = y;
    sw = 0;
    sh = 0;

    m_valid = true;
    m_shown = true;

    if(U) w->updateGLA();
}

void SQ_GLSelectionPainter::move(int x, int y)
{
    hackXY(x, y);

    int X = QMAX(x, xmoveold);
    int Y = QMIN(y, ymoveold);
    int Xmin = QMIN(x, xmoveold);
    int Ymin = QMAX(y, ymoveold);

    sx = Xmin;
    sy = Ymin;
    sw = X - Xmin;
    sh = Ymin - Y;

    angle += 3;

    if(angle > 360)
        angle = 0;

    // SQ_GLWidget::paintGL() will call draw()
    w->updateGLA();
}

void SQ_GLSelectionPainter::end()
{
    m_valid = false;
    m_shown = false;

    w->updateGLA();
}

void SQ_GLSelectionPainter::drawEllipse(float xradius, float yradius)
{
    w->makeCurrent();

    double degInRad;

    glBegin(GL_LINE_LOOP);
    glColor4f(1,0,1,1);

    for(int i = 0; i < 360; i++)
    {
        degInRad = rad_const * i;
        glVertex2f(cos(degInRad) * xradius, sin(degInRad) * yradius);
    }

    glColor4f(1,1,1,1);
    glEnd();
}

void SQ_GLSelectionPainter::drawRect()
{
    w->makeCurrent();

    glBegin(GL_LINE_LOOP);
    glColor4f(1,0,1,1);

    glVertex2f(-sw/2, sh/2);
    glVertex2f(sw/2,  sh/2);
    glVertex2f(sw/2,  -sh/2);
    glVertex2f(-sw/2, -sh/2);

    glColor4f(1,1,1,1);
    glEnd();
}

void SQ_GLSelectionPainter::draw()
{
    if(!sw || !sh)
        return;

    if(m_type == Ellipse)
        drawEllipse(sw/2, sh/2);
    else
        drawRect();

    // center rectangle
    if(sw > lenc && sh > lenc)
    {
        glColor4f(1,0,1,1);
        glRectf(-lenc2, lenc2, lenc2, -lenc2);
        glColor4f(1,1,1,1);
    }
}