kylin-nm/plugins/netconnect/lanitem.cpp

181 lines
5.5 KiB
C++
Raw Permalink Normal View History

2022-07-05 10:13:48 +08:00
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* Copyright (C) 2022 Tianjin KYLIN Information Technology Co., Ltd.
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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.
*
*/
2021-10-27 12:28:16 +08:00
#include "lanitem.h"
2023-04-06 14:09:59 +08:00
#include <QApplication>
2021-10-27 12:28:16 +08:00
#define FRAME_SPEED 150
#define LIMIT_TIME 60*1000
#define TOTAL_PAGE 8
#define THEME_QT_SCHEMA "org.ukui.style"
#define MODE_QT_KEY "style-name"
LanItem::LanItem(bool isAcitve, QWidget *parent)
: QPushButton(parent),isAcitve(isAcitve)
{
this->setMinimumSize(550, 58);
this->setProperty("useButtonPalette", true);
2022-06-01 17:13:25 +08:00
this->setFlat(true);
// setStyleSheet("QPushButton:!checked{background-color: palette(base)}");
2021-10-27 12:28:16 +08:00
QHBoxLayout *mLanLyt = new QHBoxLayout(this);
mLanLyt->setContentsMargins(16,0,16,0);
mLanLyt->setSpacing(16);
iconLabel = new QLabel(this);
2021-11-09 15:46:47 +08:00
iconLabel->setProperty("useIconHighlightEffect", 0x2);
2021-10-27 12:28:16 +08:00
titileLabel = new FixLabel(this);
statusLabel = new QLabel(this);
2023-04-06 14:09:59 +08:00
statusLabel->setProperty("useIconHighlightEffect", 0x2);
2021-10-27 12:28:16 +08:00
statusLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
// statusLabel->setMinimumSize(36,36);
infoLabel = new GrayInfoButton(this);
2023-04-07 14:20:19 +08:00
//【更多】菜单
m_moreButton = new QToolButton(this);
m_moreButton->setProperty("useButtonPalette", true);
m_moreButton->setPopupMode(QToolButton::InstantPopup);
m_moreButton->setAutoRaise(true);
m_moreButton->setIcon(QIcon::fromTheme("view-more-horizontal-symbolic"));
m_moreMenu = new QMenu(m_moreButton);
m_connectAction = new QAction(m_moreMenu);
m_deleteAction = new QAction(tr("Delete"), m_moreMenu);
setConnectActionText(isAcitve);
m_moreMenu->addAction(m_connectAction);
m_moreMenu->addAction(m_deleteAction);
m_moreButton->setMenu(m_moreMenu);
2021-10-27 12:28:16 +08:00
mLanLyt->addWidget(iconLabel);
mLanLyt->addWidget(titileLabel,Qt::AlignLeft);
mLanLyt->addStretch();
mLanLyt->addWidget(statusLabel);
mLanLyt->addWidget(infoLabel);
2023-04-07 14:20:19 +08:00
mLanLyt->addWidget(m_moreButton);
2022-01-17 14:29:24 +08:00
loadIcons.append(QIcon::fromTheme("ukui-loading-1-symbolic"));
loadIcons.append(QIcon::fromTheme("ukui-loading-2-symbolic"));
loadIcons.append(QIcon::fromTheme("ukui-loading-3-symbolic"));
loadIcons.append(QIcon::fromTheme("ukui-loading-4-symbolic"));
loadIcons.append(QIcon::fromTheme("ukui-loading-5-symbolic"));
loadIcons.append(QIcon::fromTheme("ukui-loading-6-symbolic"));
loadIcons.append(QIcon::fromTheme("ukui-loading-7-symbolic"));
2021-10-27 12:28:16 +08:00
waitTimer = new QTimer(this);
2022-01-17 14:29:24 +08:00
connect(waitTimer, &QTimer::timeout, this, &LanItem::updateIcon);
2023-04-07 14:20:19 +08:00
connect(m_connectAction, &QAction::triggered, this, &LanItem::onConnectTriggered);
connect(m_deleteAction, &QAction::triggered, this, &LanItem::onDeletetTriggered);
m_moreMenu->installEventFilter(this);
2021-10-27 12:28:16 +08:00
}
LanItem::~LanItem()
{
}
2023-04-07 14:20:19 +08:00
2022-01-17 14:29:24 +08:00
void LanItem::updateIcon()
2021-10-27 12:28:16 +08:00
{
2022-01-17 14:29:24 +08:00
if (currentIconIndex > 6) {
currentIconIndex = 0;
}
statusLabel->setPixmap(loadIcons.at(currentIconIndex).pixmap(16,16));
currentIconIndex ++;
2021-10-27 12:28:16 +08:00
}
void LanItem::startLoading()
{
waitTimer->start(FRAME_SPEED);
loading = true;
}
void LanItem::stopLoading(){
waitTimer->stop();
loading = false;
}
2023-04-07 14:20:19 +08:00
/**
* @brief LanItem::setConnectActionText
* /
* @param isAcitve
*/
void LanItem::setConnectActionText(bool isAcitve)
{
if (isAcitve) {
m_connectAction->setText(tr("Disconnect"));
} else {
m_connectAction->setText(tr("Connect"));
}
}
void LanItem::onConnectTriggered()
{
if (!m_connectAction) {
return;
}
if (m_connectAction->text() == tr("Connect")) {
Q_EMIT connectActionTriggered();
} else if (m_connectAction->text() == tr("Disconnect")) {
Q_EMIT disconnectActionTriggered();
}
}
void LanItem::onDeletetTriggered()
{
if (!m_deleteAction) {
return;
}
Q_EMIT deleteActionTriggered();
}
2021-12-01 11:07:21 +08:00
void LanItem::paintEvent(QPaintEvent *event)
{
// QPalette pal = qApp->palette();
2021-12-01 11:07:21 +08:00
QPainter painter(this);
painter.setRenderHint(QPainter:: Antialiasing, true); //设置渲染,启动反锯齿
painter.setPen(Qt::NoPen);
2023-04-06 14:09:59 +08:00
painter.setBrush(this->palette().base().color());
// QColor color = pal.color(QPalette::Button);
// color.setAlphaF(0.5);
// pal.setColor(QPalette::Button, color);
// this->setPalette(pal);
2021-12-01 11:07:21 +08:00
QRect rect = this->rect();
painter.drawRect(rect);
QPushButton::paintEvent(event);
}
2023-04-07 14:20:19 +08:00
bool LanItem::eventFilter(QObject *watched, QEvent *event)
{
//菜单右边界与按钮右边界对齐
if (event->type() == QEvent::Show && watched == m_moreMenu) {
int menuXPos = m_moreMenu->pos().x();
int menuWidth = m_moreMenu->size().width();
int btnWidth = m_moreButton->size().width();
QPoint pos = QPoint (menuXPos - menuWidth + btnWidth, m_moreMenu->pos().y());
m_moreMenu->move(pos);
return true;
}
return false;
}