[go: up one dir, main page]

File: graphicsbtn.cpp

package info (click to toggle)
colorcode 0.6.1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 676 kB
  • ctags: 544
  • sloc: cpp: 4,828; makefile: 55
file content (151 lines) | stat: -rw-r--r-- 4,039 bytes parent folder | download
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
/* ColorCode, a free MasterMind clone with built in solver
 * Copyright (C) 2009  Dirk Laebisch
 * http://www.laebisch.com/
 *
 * ColorCode 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 3 of the License, or
 * (at your option) any later version.
 *
 * ColorCode 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 ColorCode. If not, see <http://www.gnu.org/licenses/>.
*/

#include <QtGui>
#include <QFontMetrics>
#include "graphicsbtn.h"

using namespace std;

GraphicsBtn::GraphicsBtn()
{
    mWidth = 152;

    setCursor(Qt::PointingHandCursor);
    setAcceptHoverEvents (true);
    mLabel = "";
    mLabelWidth = 0;
    mYOffs = 0;

    InitGraphics();
    SetShapes();

    mFillBrush = &mFillOutBrush;
    mFrameBrush = &mFrameOutBrush;

    mFont = QFont("Arial", 12, QFont::Bold, false);
    mFont.setStyleHint(QFont::SansSerif);
    mFont.setStyleStrategy(QFont::PreferAntialias);
}

void GraphicsBtn::SetWidth(int w)
{
    mWidth = w;
    SetShapes();
}

void GraphicsBtn::SetLabel(QString str)
{
    mLabel = str;
}

void GraphicsBtn::ShowBtn(bool b)
{
    if (b)
    {
        mFillBrush = &mFillOutBrush;
        mFrameBrush = &mFrameOutBrush;
    }
    setVisible(b);
}

void GraphicsBtn::hoverEnterEvent(QGraphicsSceneHoverEvent* e)
{
    mFillBrush = &mFillOverBrush;
    QGraphicsItem::hoverEnterEvent(e);
}

void GraphicsBtn::hoverLeaveEvent(QGraphicsSceneHoverEvent* e)
{
    mFillBrush = &mFillOutBrush;
    QGraphicsItem::hoverLeaveEvent(e);
}

void GraphicsBtn::mousePressEvent(QGraphicsSceneMouseEvent*)
{
    mFrameBrush = &mFrameOverBrush;
    mYOffs = 1;
    update(mRect);
}

void GraphicsBtn::mouseReleaseEvent(QGraphicsSceneMouseEvent* e)
{
    mFrameBrush = &mFrameOutBrush;
    mYOffs = 0;
    if ( e->pos().x() >= 0
         && e->pos().x() <= boundingRect().width()
         && e->pos().y() >= 0
         && e->pos().y() <= boundingRect().height() )
    {
        emit BtnPressSignal(this);
    }
    update(mRect);
}

void GraphicsBtn::InitGraphics()
{
    QLinearGradient fillgrad(0, 2, 0, 36);
    fillgrad.setColorAt(0.0, QColor("#f7f8fa"));
    fillgrad.setColorAt(0.5, QColor("#b9babc"));
    mFillOutBrush = QBrush(fillgrad);

    QLinearGradient fillovergrad(0, 2, 0, 36);
    fillovergrad.setColorAt(0.0, QColor("#f7f8fa"));
    fillovergrad.setColorAt(1.0, QColor("#b9babc"));
    mFillOverBrush = QBrush(fillovergrad);

    QLinearGradient framegrad(0, 0, 0, 40);
    framegrad.setColorAt(1.0, QColor(0, 0, 0, 0xa0));
    framegrad.setColorAt(0.0, QColor(0xff, 0xff, 0xff, 0xa0));
    mFrameOutBrush = QBrush(framegrad);

    QLinearGradient frameovergrad(0, 0, 0, 40);
    frameovergrad.setColorAt(0.0, QColor(0, 0, 0, 0xa0));
    frameovergrad.setColorAt(1.0, QColor(0xff, 0xff, 0xff, 0xa0));
    mFrameOverBrush = QBrush(frameovergrad);
}

void GraphicsBtn::SetShapes()
{
    mRect = QRectF(0, 0, mWidth, 38);
    mRectFill = QRectF(1, 1, mWidth - 2, 36);
}

QRectF GraphicsBtn::boundingRect() const
{
    const double margin = 0.5;
    return mRect.adjusted(-margin, -margin, 2 * margin, 2 * margin);
}

void GraphicsBtn::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget* /* widget */)
{
    painter->setPen(Qt::NoPen);
    painter->setBrush(*mFrameBrush);
    painter->drawRoundedRect(mRect, 20, 20);

    painter->setBrush(*mFillBrush);
    painter->drawRoundedRect(mRectFill, 18, 18);

    if (mLabel != "")
    {
        painter->setRenderHint(QPainter::TextAntialiasing, true);
        painter->setPen(QPen(QColor("#303133")));
        painter->setFont(mFont);
        painter->drawText(mRectFill.adjusted(0, mYOffs, 0, mYOffs), Qt::AlignCenter, mLabel);
    }
}