-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdirectionselector.cpp
140 lines (128 loc) · 4.38 KB
/
directionselector.cpp
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
/***************************************************************************
* Copyright (C) 2006-2023 M Wellings *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License version 2 as *
* published by the Free Software Foundation *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <QtGui>
#include "directionselector.h"
#include "wordsearch.h"
DirectionSelector::DirectionSelector(QWidget *parent)
: QWidget(parent)
{
this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
for (int x = 0; x < 8; x++)
direction[x]=false;
QPalette palette(QColor(255, 255, 255));
this->setPalette(palette);
this->setAutoFillBackground(true);
this->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
setMinimumSize(180,180);
}
void DirectionSelector::setWS(wordsearch *wordsearch)
{
ws = wordsearch;
for (int x = 0; x < 8; x++)
direction[x]=ws->DirectionAllowed(x);
}
void DirectionSelector::commit()
{
for (int x = 0; x < 8; x++)
ws->setDirectionAllowed(x,direction[x]);
}
void DirectionSelector::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawRect(0,0,width()-1,height()-1);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(width()/2, height()/2);
int side = qMin(width(), height());
painter.scale(side / 150.0, side / 150.0);
QPen pen1(Qt::black);
QPen pen2(Qt::gray);
painter.setPen(pen1);
for (int x = 0; x < 8; x++)
{
if (direction[x])
painter.setPen(pen1);
else
painter.setPen(pen2);
drawArrow(&painter);
painter.rotate(45);
}
}
double DirectionSelector::mouseAngle(QPoint mousePos)
{
int x = mousePos.x(), y = mousePos.y();
y = y * -1;
if ((y == 0) && (x >= 0))
return 0;
if ((y == 0) && (x < 0))
return M_PI;
if ((x == 0) && (y > 0))
return M_PI_2;
if ((x == 0) && (y < 0))
return M_PI+M_PI_2;;
double angle = atan(double(y)/double(x));
if (x < 0)
angle = M_PI - -1.0 * angle;
if ((x > 0) && (y < 0))
angle = 2 * M_PI + angle;
return angle;
}
void DirectionSelector::mouseMoveEvent ( QMouseEvent * e )
{
QPoint mousePos(e->pos());
mousePos.setX(mousePos.x()-(width()/2));
mousePos.setY(mousePos.y()-(height()/2));
double mouseAngle = this->mouseAngle(mousePos);
mouseAngle = 2 * M_PI - mouseAngle;
mouseAngle = mouseAngle - (M_PI+M_PI_2-(M_PI/8));
if (mouseAngle < 0) mouseAngle = mouseAngle + 2 * M_PI;
int tdirection = int(mouseAngle / (M_PI/4));
if (hldirection!=tdirection)
{
hldirection = tdirection;
update();
}
if (e->buttons() & Qt::LeftButton)
direction[tdirection] = firstdirection;
}
void DirectionSelector::drawArrow(QPainter *painter)
{
painter->drawLine(7,-17,7,-50);
painter->drawLine(-7,-17,-7,-50);
painter->drawLine(15,-50,7,-50);
painter->drawLine(-15,-50,-7,-50);
painter->drawLine(15,-50,0,-65);
painter->drawLine(-15,-50,0,-65);
}
void DirectionSelector::mousePressEvent(QMouseEvent *e)
{
QPoint mousePos(e->pos());
mousePos.setX(mousePos.x()-(width()/2));
mousePos.setY(mousePos.y()-(height()/2));
double mouseAngle = this->mouseAngle(mousePos);
mouseAngle = 2 * M_PI - mouseAngle;
mouseAngle = mouseAngle - (M_PI+M_PI_2-(M_PI/8));
if (mouseAngle < 0) mouseAngle = mouseAngle + 2 * M_PI;
int tdirection = int(mouseAngle / (M_PI/4));
if (direction[tdirection] == false)
firstdirection = true;
else
firstdirection = false;
direction[tdirection] = firstdirection;
update();
}