~feng-kylin/youker-assistant/youker-assistant

« back to all changes in this revision

Viewing changes to component/myswitcher.cpp

  • Committer: lixiang
  • Date: 2018-03-06 03:13:06 UTC
  • Revision ID: lixiang@kylinos.cn-20180306031306-fd7qnru3vm4a1xjd
Rewrite with Qt5, and add system monitor

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 ~ 2018 National University of Defense Technology(NUDT) & Tianjin Kylin Ltd.
 
3
 *
 
4
 * Authors:
 
5
 *  Kobe Lee    xiangli@ubuntukylin.com/kobe24_lixiang@126.com
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; version 3.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include "myswitcher.h"
 
21
 
 
22
#include <QPainter>
 
23
#include <QPainterPath>
 
24
#include <QMouseEvent>
 
25
#include <QDebug>
 
26
 
 
27
MySwitcher::MySwitcher(QWidget *parent) :
 
28
    QFrame(parent),
 
29
    m_isOn(false)
 
30
{
 
31
    this->setFixedSize(76, 29);
 
32
    m_offImage.load("://res/off.png");
 
33
    m_onImage.load("://res/on.png");
 
34
}
 
35
 
 
36
bool MySwitcher::isOn() const
 
37
{
 
38
    return m_isOn;
 
39
}
 
40
 
 
41
void MySwitcher::setOnStatus(bool b)
 
42
{
 
43
    if (m_isOn != b) {
 
44
        m_isOn = b;
 
45
        emit this->statusChanged(b);
 
46
        this->update();
 
47
    }
 
48
}
 
49
 
 
50
void MySwitcher::mousePressEvent(QMouseEvent *event)
 
51
{
 
52
    if (event->button() == Qt::LeftButton) {
 
53
        this->setOnStatus(!m_isOn);
 
54
        event->accept();
 
55
    }
 
56
}
 
57
 
 
58
void MySwitcher::paintEvent(QPaintEvent *event)
 
59
{
 
60
    QPixmap *m_nowImagePix;
 
61
 
 
62
    if(this->m_isOn) {
 
63
        m_nowImagePix = &m_onImage;
 
64
    }
 
65
    else {
 
66
        m_nowImagePix = &m_offImage;
 
67
    }
 
68
 
 
69
    if(m_nowImagePix->isNull())
 
70
        return;
 
71
 
 
72
    QPainter painter(this);
 
73
    painter.setRenderHints(QPainter::Antialiasing, true);
 
74
 
 
75
    QPainterPath path;
 
76
    path.addRoundedRect(rect(), m_nowImagePix->height() / 2.0, m_nowImagePix->height() / 2.0);
 
77
    path.closeSubpath();
 
78
 
 
79
    painter.setClipPath(path);
 
80
    painter.drawPixmap(rect(), *m_nowImagePix);
 
81
}