File: togglebutton.cpp

package info (click to toggle)
hedgewars 1.0.2-13
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 219,164 kB
  • sloc: pascal: 54,829; cpp: 27,221; ansic: 22,809; java: 8,210; haskell: 6,797; xml: 3,076; sh: 580; objc: 113; python: 105; makefile: 32
file content (64 lines) | stat: -rw-r--r-- 2,108 bytes parent folder | download | duplicates (5)
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
/*
 * Hedgewars, a free turn based strategy game
 * Copyright (c) 2009 Kristian Lehmann <email@thexception.net>
 * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "togglebutton.h"

ToggleButtonWidget::ToggleButtonWidget(QWidget * parent, QString img)
    : QPushButton(parent)
{
    setCheckable(true);

    QPixmap pixOffOverlay(":/res/btnDisabled.png");
    QPainter * painter = new QPainter();

    QPixmap pixOn = QPixmap(img);
    QPixmap pixOff = QPixmap(img);

    // Use the same image for disabled (i.e. non-clickable) button.
    // The default would be gray which is a little bit hard on the eye.
    // The disabled state is communicated to the user by the button
    // border, which turns gray.
    icoChecked.addPixmap(pixOn, QIcon::Normal);
    icoChecked.addPixmap(pixOn, QIcon::Disabled);

    pixOff.setDevicePixelRatio(pixOffOverlay.devicePixelRatio());

    setMaximumWidth(pixOn.width() + 6);

    painter->begin(&pixOff);
    painter->drawPixmap(pixOff.rect(), pixOffOverlay);
    painter->end();

    icoUnchecked.addPixmap(pixOff, QIcon::Normal);
    icoUnchecked.addPixmap(pixOff, QIcon::Disabled);

    setIconSize(pixOff.size());
    setIcon(icoUnchecked);

    connect(this, SIGNAL(toggled(bool)), this, SLOT(eventToggled(bool)));
}

ToggleButtonWidget::~ToggleButtonWidget()
{
}

void ToggleButtonWidget::eventToggled(bool checked)
{
    setIcon(checked ? icoChecked : icoUnchecked);
}