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

« back to all changes in this revision

Viewing changes to plugins/startupmanager/startupitem.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 "startupitem.h"
 
21
#include "../../component/myswitcher.h"
 
22
#include "startupdata.h"
 
23
 
 
24
#include <QApplication>
 
25
#include <QDebug>
 
26
#include <QPainter>
 
27
#include <QLabel>
 
28
 
 
29
inline QPixmap getAppIconPix(const QString &iconName, int iconSize)
 
30
{
 
31
    QIcon defaultExecutableIcon = QIcon::fromTheme("application-x-executable");//gnome-mine-application-x-executable
 
32
    if (defaultExecutableIcon.isNull()) {
 
33
        defaultExecutableIcon = QIcon("/usr/share/icons/kylin-icon-theme/48x48/mimetypes/application-x-executable.png");
 
34
        if (defaultExecutableIcon.isNull())
 
35
            defaultExecutableIcon = QIcon(":/res/autostart-default.png");
 
36
    }
 
37
 
 
38
    QIcon icon;
 
39
 
 
40
    if (iconName.contains("/")) {
 
41
        icon = QIcon(iconName);
 
42
    }
 
43
    else {
 
44
        icon = QIcon::fromTheme(iconName, defaultExecutableIcon);
 
45
    }
 
46
 
 
47
    qreal devicePixelRatio = qApp->devicePixelRatio();
 
48
 
 
49
    QPixmap pixmap = icon.pixmap(iconSize * devicePixelRatio, iconSize * devicePixelRatio);
 
50
    pixmap.setDevicePixelRatio(devicePixelRatio);
 
51
 
 
52
    return pixmap;
 
53
}
 
54
 
 
55
StartupItem::StartupItem(StartupData info, QWidget *parent) : QWidget(parent)
 
56
  ,isEntered(false)
 
57
{
 
58
    item = new QListWidgetItem();
 
59
//    item->setSizeHint(QSize(400, 60));
 
60
 
 
61
    m_layout = new QHBoxLayout();
 
62
    m_leftLayout = new QHBoxLayout();
 
63
    m_switchLayout = new QHBoxLayout();
 
64
 
 
65
    m_appIcon = new QLabel();
 
66
    m_appIcon->setFixedSize(40, 40);
 
67
    m_appIcon->setScaledContents(true);//自动缩放,显示图像大小自动调整为Qlabel大小
 
68
    m_appIcon->setPixmap(getAppIconPix(info.icon, 40));
 
69
 
 
70
    m_appNameLabel = new QLabel();
 
71
    this->setAppName(info.name);
 
72
    m_appDescLabel = new QLabel();
 
73
    m_appDescLabel->setText(info.comment);
 
74
 
 
75
    switcher = new MySwitcher();
 
76
    switcher->setOnStatus(info.enabled);
 
77
    connect(switcher, &MySwitcher::statusChanged, [=] (const bool b) {
 
78
        emit changeStartup(info.exec, b);
 
79
    });
 
80
 
 
81
    m_switchLayout->addWidget(switcher, 0, Qt::AlignCenter);
 
82
    m_leftLayout->addWidget(m_appIcon);
 
83
 
 
84
    m_labelWidget = new QWidget();
 
85
    m_labelLayout = new QVBoxLayout(m_labelWidget);
 
86
    m_labelLayout->addWidget(m_appNameLabel);
 
87
    m_labelLayout->addWidget(m_appDescLabel);
 
88
    m_leftLayout->addWidget(m_labelWidget);
 
89
 
 
90
    m_layout->addLayout(m_leftLayout);
 
91
    m_layout->addStretch();
 
92
    m_layout->addLayout(m_switchLayout);
 
93
    m_layout->setContentsMargins(10, 0, 10, 0);
 
94
    this->setLayout(m_layout);
 
95
}
 
96
 
 
97
void StartupItem::setSwitcherOn(const bool b)
 
98
{
 
99
    switcher->blockSignals(true);
 
100
    switcher->setOnStatus(b);
 
101
    switcher->blockSignals(false);
 
102
}
 
103
 
 
104
QListWidgetItem* StartupItem::getItem()
 
105
{
 
106
    return item;
 
107
}
 
108
 
 
109
void StartupItem::setItemHovered()
 
110
{
 
111
    isEntered = true;
 
112
    repaint();
 
113
}
 
114
 
 
115
void StartupItem::unsetItemHovered()
 
116
{
 
117
    isEntered = false;
 
118
    repaint();
 
119
}
 
120
 
 
121
void StartupItem::setAppName(const QString &name)
 
122
{
 
123
    m_appName = name;
 
124
    m_appNameLabel->setText(name);
 
125
}
 
126
 
 
127
QString StartupItem::getAppName()
 
128
{
 
129
    return this->m_appName;
 
130
}
 
131
 
 
132
void StartupItem::enterEvent(QEvent *event)
 
133
{
 
134
    emit this->enter();
 
135
 
 
136
    QWidget::enterEvent(event);
 
137
}
 
138
 
 
139
void StartupItem::paintEvent(QPaintEvent *event)
 
140
{
 
141
    if (isEntered) {
 
142
        QPainter painter(this);
 
143
        painter.setRenderHint(QPainter::Antialiasing, true);
 
144
 
 
145
        QPainterPath path;
 
146
        path.addRoundedRect(QRectF(rect()), 2, 2);
 
147
        painter.setOpacity(0.1);
 
148
        painter.fillPath(path, QColor("#2bb6ea"));
 
149
    }
 
150
 
 
151
    QWidget::paintEvent(event);
 
152
}