1 /* ColorCode, a free MasterMind clone with built in solver
2 * Copyright (C) 2009 Dirk Laebisch
3 * http://www.laebisch.com/
4 *
5 * ColorCode is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * ColorCode is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with ColorCode. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <QtWidgets>
20 #include <QFontMetrics>
21 #include "graphicsbtn.h"
22
23 using namespace std;
24
GraphicsBtn()25 GraphicsBtn::GraphicsBtn()
26 {
27 mWidth = 152;
28
29 setAcceptHoverEvents (true);
30 mLabel = "";
31 mLabelWidth = 0;
32 mYOffs = 0;
33
34 mOpOver = 1.0;
35 mOpOut = 1.0;
36
37 InitGraphics();
38 SetShapes();
39
40 mFillBrush = &mFillOutBrush;
41 mFrameBrush = &mFrameOutBrush;
42
43 mFont = QFont("Arial", 12, QFont::Bold, false);
44 mFont.setStyleHint(QFont::SansSerif);
45 mFont.setStyleStrategy(QFont::PreferAntialias);
46
47 SetEnabled(true);
48 }
49
SetEnabled(bool b)50 void GraphicsBtn::SetEnabled(bool b)
51 {
52 if (b)
53 {
54 setCursor(Qt::PointingHandCursor);
55 }
56 else
57 {
58 setCursor(Qt::ArrowCursor);
59 }
60 setEnabled(b);
61 }
62
SetWidth(int w)63 void GraphicsBtn::SetWidth(int w)
64 {
65 mWidth = w;
66 SetShapes();
67 }
68
SetLabel(QString str)69 void GraphicsBtn::SetLabel(QString str)
70 {
71 mLabel = str;
72 }
73
ShowBtn(bool b)74 void GraphicsBtn::ShowBtn(bool b)
75 {
76 if (b)
77 {
78 mFillBrush = &mFillOutBrush;
79 mFrameBrush = &mFrameOutBrush;
80 }
81 setVisible(b);
82 }
83
SetStatesOpacity(const qreal opOver,const qreal opOut)84 void GraphicsBtn::SetStatesOpacity(const qreal opOver, const qreal opOut)
85 {
86 mOpOver = opOver;
87 mOpOut = opOut;
88 setOpacity(mOpOut);
89 }
90
InvertStates()91 void GraphicsBtn::InvertStates()
92 {
93 QBrush tmp = mFillOutBrush;
94 mFillOutBrush = mFillOverBrush;
95 mFillOverBrush = tmp;
96 }
97
SetHoverState()98 void GraphicsBtn::SetHoverState()
99 {
100 if (mFillBrush != &mFillOverBrush)
101 {
102 mFillBrush = &mFillOverBrush;
103 }
104 setOpacity(mOpOver);
105 update(mRect);
106 }
107
SetOutState()108 void GraphicsBtn::SetOutState()
109 {
110 if (mFillBrush != &mFillOutBrush)
111 {
112 mFillBrush = &mFillOutBrush;
113 }
114 setOpacity(mOpOut);
115 update(mRect);
116 }
117
SetPressedState()118 void GraphicsBtn::SetPressedState()
119 {
120 if (mFrameBrush != &mFrameOverBrush)
121 {
122 mFrameBrush = &mFrameOverBrush;
123 }
124 mYOffs = 1;
125 update(mRect);
126 }
127
hoverEnterEvent(QGraphicsSceneHoverEvent * e)128 void GraphicsBtn::hoverEnterEvent(QGraphicsSceneHoverEvent* e)
129 {
130 SetHoverState();
131 QGraphicsItem::hoverEnterEvent(e);
132 }
133
hoverLeaveEvent(QGraphicsSceneHoverEvent * e)134 void GraphicsBtn::hoverLeaveEvent(QGraphicsSceneHoverEvent* e)
135 {
136 SetOutState();
137 QGraphicsItem::hoverLeaveEvent(e);
138 }
139
mousePressEvent(QGraphicsSceneMouseEvent * e)140 void GraphicsBtn::mousePressEvent(QGraphicsSceneMouseEvent* e)
141 {
142 if (e->button() != Qt::LeftButton)
143 {
144 return;
145 }
146 SetPressedState();
147 }
148
mouseReleaseEvent(QGraphicsSceneMouseEvent * e)149 void GraphicsBtn::mouseReleaseEvent(QGraphicsSceneMouseEvent* e)
150 {
151 if (e->button() != Qt::LeftButton)
152 {
153 return;
154 }
155 mFrameBrush = &mFrameOutBrush;
156 mYOffs = 0;
157 if ( e->pos().x() >= 0
158 && e->pos().x() <= boundingRect().width()
159 && e->pos().y() >= 0
160 && e->pos().y() <= boundingRect().height() )
161 {
162 emit BtnPressSignal(this);
163 }
164 update(mRect);
165 }
166
InitGraphics()167 void GraphicsBtn::InitGraphics()
168 {
169 QLinearGradient fillgrad(0, 2, 0, 36);
170 fillgrad.setColorAt(0.0, QColor("#f7f8fa"));
171 fillgrad.setColorAt(0.5, QColor("#b9babc"));
172 mFillOutBrush = QBrush(fillgrad);
173
174 QLinearGradient fillovergrad(0, 2, 0, 36);
175 fillovergrad.setColorAt(0.0, QColor("#f7f8fa"));
176 fillovergrad.setColorAt(1.0, QColor("#b9babc"));
177 mFillOverBrush = QBrush(fillovergrad);
178
179 QLinearGradient framegrad(0, 0, 0, 40);
180 framegrad.setColorAt(1.0, QColor(0, 0, 0, 0xa0));
181 framegrad.setColorAt(0.0, QColor(0xff, 0xff, 0xff, 0xa0));
182 mFrameOutBrush = QBrush(framegrad);
183
184 QLinearGradient frameovergrad(0, 0, 0, 40);
185 frameovergrad.setColorAt(0.0, QColor(0, 0, 0, 0xa0));
186 frameovergrad.setColorAt(1.0, QColor(0xff, 0xff, 0xff, 0xa0));
187 mFrameOverBrush = QBrush(frameovergrad);
188 }
189
SetShapes()190 void GraphicsBtn::SetShapes()
191 {
192 mRect = QRectF(0, 0, mWidth, 38);
193 mRectFill = QRectF(1, 1, mWidth - 2, 36);
194 }
195
boundingRect() const196 QRectF GraphicsBtn::boundingRect() const
197 {
198 const double margin = 0.5;
199 return mRect.adjusted(-margin, -margin, 2 * margin, 2 * margin);
200 }
201
paint(QPainter * painter,const QStyleOptionGraphicsItem *,QWidget *)202 void GraphicsBtn::paint(QPainter *painter, const QStyleOptionGraphicsItem * , QWidget* )
203 {
204 painter->setPen(Qt::NoPen);
205 painter->setBrush(*mFrameBrush);
206 painter->drawRoundedRect(mRect, 20, 20);
207
208 painter->setBrush(*mFillBrush);
209 painter->drawRoundedRect(mRectFill, 18, 18);
210
211 if (mLabel != "")
212 {
213 painter->setRenderHint(QPainter::TextAntialiasing, true);
214 painter->setPen(QPen(QColor("#303133")));
215 painter->setFont(mFont);
216 painter->drawText(mRectFill.adjusted(0, mYOffs, 0, mYOffs), Qt::AlignCenter, mLabel);
217 }
218 }
219