Menu

[r638]: / src / controlbutton.cpp  Maximize  Restore  History

Download this file

124 lines (102 with data), 3.4 kB

  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
/***************************************************************************
//
// softProjector - an open source media projection software
// Copyright (C) 2014 Vladislav Kobzar, Matvey Adzhigirey and Ilya Spivakov
//
// 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 version 3 of the License.
//
// 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, see <http://www.gnu.org/licenses/>.
//
***************************************************************************/
#include "controlbutton.h"
ControlButton::ControlButton(QWidget * parent)
: QPushButton(parent)
{
m_hovered = false;
m_pressed = false;
m_opacity = 1.0;
}
ControlButton::ControlButton(const QIcon & icon, const QIcon &iconHovered, const QIcon &iconPressed, QWidget *parent)
: QPushButton(parent)
{
m_icon = icon;
m_iconHovered = iconHovered;
m_iconPressed = iconPressed;
m_hovered = false;
m_pressed = false;
m_opacity = 1.0;
}
ControlButton::~ControlButton(){}
void ControlButton::paintEvent(QPaintEvent * pe)
{
Q_UNUSED(pe);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
//test for state changes
QColor button_color;
QIcon button_icon;
if(this->isEnabled())
{
if(m_hovered)
button_icon = m_iconHovered;
else
button_icon = m_icon;
if(m_pressed)
button_icon = m_iconPressed;
}
else
button_color = QColor(50, 50, 50);
QRect button_rect = this->geometry();
if(!button_icon.isNull())
{
QSize icon_size = this->iconSize();
QRect icon_position = this->calculateIconPosition(button_rect, icon_size);
painter.setOpacity(m_opacity);
painter.drawPixmap(icon_position, QPixmap(button_icon.pixmap(icon_size)));
}
}
void ControlButton::enterEvent(QEvent * e)
{
m_hovered = true;
this->repaint();
QPushButton::enterEvent(e);
}
void ControlButton::leaveEvent(QEvent * e)
{
m_hovered = false;
this->repaint();
QPushButton::leaveEvent(e);
}
void ControlButton::mousePressEvent(QMouseEvent * e)
{
m_pressed = true;
this->repaint();
QPushButton::mousePressEvent(e);
}
void ControlButton::mouseReleaseEvent(QMouseEvent * e)
{
m_pressed = false;
this->repaint();
QPushButton::mouseReleaseEvent(e);
}
QRect ControlButton::calculateIconPosition(QRect button_rect, QSize icon_size)
{
int x = (button_rect.width() / 2) - (icon_size.width() / 2);
int y = (button_rect.height() / 2) - (icon_size.height() / 2);
int width = icon_size.width();
int height = icon_size.height();
QRect icon_position;
icon_position.setX(x);
icon_position.setY(y);
icon_position.setWidth(width);
icon_position.setHeight(height);
return icon_position;
}