-
Notifications
You must be signed in to change notification settings - Fork 1
/
element.cpp
executable file
·146 lines (131 loc) · 3.66 KB
/
element.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
140
141
142
143
144
145
146
/****************************************************************************
*
* This file may be used under the terms of the GNU General Public
* License version 2.0 as published by the Free Software Foundation
* and appearing in the file LICENSE.GPL included in the packaging of
* this file.
*
* author: Andreas Bricelj
* history: initial version 1.0 09/21/2005
* several updates 1.1 09/12/2006
*
* ****************************************************************************/
#include "element.h"
Element::Element(QWidget* parent, int _r, int _c):
QWidget(parent), row(_r), column(_c), value(0),
posVal(0),
changable(false),
wrong(false)
{
}
QFont big("Arial",24,QFont::Normal);
QFont sml("Arial",10,QFont::Normal);
void Element::paintEvent(QPaintEvent*)
{
QPainter painter(this);
QPen pen(Qt::black,1);
painter.setPen(pen);
if (wrong)
painter.setBrush(QBrush(Qt::red));
else if (!changable)
painter.setBrush(QBrush(Qt::gray));
painter.drawRect(0,0,width()-1,height()-1);
if (value!=0) {
painter.setFont(big);
painter.drawText(0,0,width()-1,height()-1,
Qt::AlignVCenter|Qt::AlignHCenter|Qt::TextSingleLine,QString("%1").arg(value));
} else {
if (posVal==0)
return;
painter.setFont(sml);
painter.drawLine((width()/3),2,(width()/3),height()-3);
painter.drawLine((2*width()/3),2,(2*width()/3),height()-3);
painter.drawLine(2,(height()/3),width()-3,(height()/3));
painter.drawLine(2,(2*height()/3),width()-3,(2*height()/3));
for (int i=0; i<9; i++)
if ((1<<i)&posVal)
painter.drawText(((i%3)*width())/3,((i/3)*height())/3,width()/3,height()/3,
Qt::AlignVCenter|Qt::AlignHCenter|Qt::TextSingleLine,QString("%1").arg(i+1));
}
}
void Element::mousePressEvent(QMouseEvent* event)
{
if (!changable)
return;
if (value) {
value=0;
if (posVal==0x1ff)
setPosVal(0);
update();
emit valueChanged(row,column,value);
} else {
int x=(event->pos().x())/(width()/3);
int y=(event->pos().y())/(height()/3);
int v=y*3+x;
if (v>=9) v=8;
if (v<0) v=0;
if (event->button() == Qt::LeftButton) {
if (posVal&(1<<v))
setValue(v+1);
else if (posVal==0)
setPosVal(0x1ff);
else
setPosVal((posVal&0x1ff)|(1<<v));
} else if (event->button() == Qt::RightButton) {
if (posVal==0)
setPosVal(0x200);
else if (posVal&(1<<v))
setPosVal(posVal&~(1<<v));
else
setPosVal(posVal|(1<<v));
}
}
}
int Element::getValue()
{
return value;
}
void Element::setValue(int _value)
{
if (value==_value) return;
value=_value;
if (value==0)
setPosVal(0x1ff);
update();
emit valueChanged(row,column,value);
}
void Element::setPosVal(int _posVal)
{
if (posVal==_posVal) return;
posVal=_posVal;
emit posValChanged(row,column,posVal);
update();
}
int Element::getPosVal()
{
return posVal;
}
void Element::setWrong()
{
if (wrong) return;
wrong=true;
update();
}
void Element::setCorrect()
{
if (!wrong) return;
wrong=false;
update();
}
void Element::setChangable()
{
if (changable) return;
changable=true;
update();
}
void Element::setUnchangable()
{
if (!changable) return;
changable=false;
update();
}