mirror of
https://github.com/linuxdeepin/dde-dock.git
synced 2025-06-01 07:05:48 +00:00
chore: remove Cooperation in display plugin
Issue: linuxdeepin/developer-center#7023 Log:
This commit is contained in:
parent
0f93647736
commit
945bb03a27
@ -1,267 +0,0 @@
|
||||
// Copyright (C) 2021 ~ 2022 Uniontech Software Technology Co.,Ltd.
|
||||
// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
|
||||
//
|
||||
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
|
||||
#include "collaborationdevmodel.h"
|
||||
|
||||
#include <QIcon>
|
||||
#include <QTimer>
|
||||
#include <QDebug>
|
||||
#include <QDBusArgument>
|
||||
#include <QDBusInterface>
|
||||
#include <QDBusPendingCall>
|
||||
#include <QDBusServiceWatcher>
|
||||
|
||||
#include <DGuiApplicationHelper>
|
||||
|
||||
DGUI_USE_NAMESPACE
|
||||
DCORE_USE_NAMESPACE
|
||||
|
||||
static const QString CollaborationService = "org.deepin.dde.Cooperation1";
|
||||
static const QString CollaborationPath = "/org/deepin/dde/Cooperation1";
|
||||
static const QString CollaborationInterface = "org.deepin.dde.Cooperation1";
|
||||
static const QString ColPropertiesInterface = "org.freedesktop.DBus.Properties";
|
||||
|
||||
CollaborationDevModel::CollaborationDevModel(QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_colDbusInter(new QDBusInterface(CollaborationService, CollaborationPath, CollaborationInterface, QDBusConnection::sessionBus(), this))
|
||||
{
|
||||
if (m_colDbusInter->isValid()) {
|
||||
QList<QDBusObjectPath> paths = m_colDbusInter->property("Machines").value<QList<QDBusObjectPath>>();
|
||||
for (const QDBusObjectPath& path : paths) {
|
||||
CollaborationDevice *device = new CollaborationDevice(path.path(), this);
|
||||
if (device->isValid())
|
||||
m_devices[path.path()] = device;
|
||||
else
|
||||
device->deleteLater();
|
||||
}
|
||||
} else {
|
||||
qWarning() << CollaborationService << " is invalid";
|
||||
}
|
||||
|
||||
m_colDbusInter->connection().connect(CollaborationService, CollaborationPath, ColPropertiesInterface,
|
||||
"PropertiesChanged", "sa{sv}as", this, SLOT(onPropertyChanged(QDBusMessage)));
|
||||
|
||||
auto *dbusWatcher = new QDBusServiceWatcher(CollaborationService, m_colDbusInter->connection(),
|
||||
QDBusServiceWatcher::WatchForUnregistration, this);
|
||||
connect(dbusWatcher, &QDBusServiceWatcher::serviceUnregistered, this, [this](){
|
||||
qWarning() << CollaborationService << "unregistered";
|
||||
clear();
|
||||
});
|
||||
}
|
||||
|
||||
void CollaborationDevModel::checkServiceValid()
|
||||
{
|
||||
if (!m_colDbusInter->isValid()) {
|
||||
clear();
|
||||
}
|
||||
}
|
||||
|
||||
QList<CollaborationDevice *> CollaborationDevModel::devices() const
|
||||
{
|
||||
return m_devices.values();
|
||||
}
|
||||
|
||||
void CollaborationDevModel::onPropertyChanged(const QDBusMessage &msg)
|
||||
{
|
||||
QList<QVariant> arguments = msg.arguments();
|
||||
if (3 != arguments.count())
|
||||
return;
|
||||
|
||||
QString interfaceName = msg.arguments().at(0).toString();
|
||||
if (interfaceName != CollaborationInterface)
|
||||
return;
|
||||
|
||||
QVariantMap changedProps = qdbus_cast<QVariantMap>(arguments.at(1).value<QDBusArgument>());
|
||||
if (changedProps.contains("Machines")) {
|
||||
QList<QDBusObjectPath> paths = m_colDbusInter->property("Machines").value<QList<QDBusObjectPath>>();
|
||||
QStringList devPaths;
|
||||
for (const QDBusObjectPath& path : paths) {
|
||||
devPaths << path.path();
|
||||
}
|
||||
updateDevice(devPaths);
|
||||
}
|
||||
}
|
||||
|
||||
void CollaborationDevModel::updateDevice(const QStringList &devPaths)
|
||||
{
|
||||
if (devPaths.isEmpty()) {
|
||||
qDeleteAll(m_devices);
|
||||
m_devices.clear();
|
||||
} else {
|
||||
// 清除已不存在的设备
|
||||
QMapIterator<QString, CollaborationDevice *> it(m_devices);
|
||||
while (it.hasNext()) {
|
||||
it.next();
|
||||
if (!devPaths.contains(it.key())) {
|
||||
it.value()->deleteLater();
|
||||
m_devices.remove(it.key());
|
||||
}
|
||||
}
|
||||
|
||||
// 添加新增设备
|
||||
for (const QString &path : devPaths) {
|
||||
if (!m_devices.contains(path)) {
|
||||
CollaborationDevice *device = new CollaborationDevice(path, this);
|
||||
if (device->isValid())
|
||||
m_devices[path] = device;
|
||||
else
|
||||
device->deleteLater();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
emit devicesChanged();
|
||||
}
|
||||
|
||||
void CollaborationDevModel::clear()
|
||||
{
|
||||
for (CollaborationDevice *device : m_devices) {
|
||||
device->deleteLater();
|
||||
}
|
||||
m_devices.clear();
|
||||
|
||||
Q_EMIT devicesChanged();
|
||||
}
|
||||
|
||||
CollaborationDevice *CollaborationDevModel::getDevice(const QString &machinePath)
|
||||
{
|
||||
return m_devices.value(machinePath, nullptr);
|
||||
}
|
||||
|
||||
CollaborationDevice::CollaborationDevice(const QString &devPath, QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_path(devPath)
|
||||
, m_OS(-1)
|
||||
, m_isConnected(false)
|
||||
, m_isCooperated(false)
|
||||
, m_isValid(false)
|
||||
, m_isCooperating(false)
|
||||
, m_devDbusInter(new QDBusInterface(CollaborationService, devPath, CollaborationInterface + QString(".Machine"),
|
||||
QDBusConnection::sessionBus(), this))
|
||||
{
|
||||
if (m_devDbusInter->isValid()) {
|
||||
m_name = m_devDbusInter->property("Name").toString();
|
||||
m_OS = m_devDbusInter->property("OS").toInt();
|
||||
m_isConnected = m_devDbusInter->property("Connected").toBool();
|
||||
m_isCooperated = m_devDbusInter->property("DeviceSharing").toBool();
|
||||
m_uuid = m_devDbusInter->property("UUID").toString();
|
||||
m_isValid = true;
|
||||
} else {
|
||||
qWarning() << "CollaborationDevice devPath:" << devPath << " is invalid and get properties failed";
|
||||
}
|
||||
|
||||
m_devDbusInter->connection().connect(CollaborationService, m_path, ColPropertiesInterface, "PropertiesChanged",
|
||||
this, SLOT(onPropertyChanged(QDBusMessage)));
|
||||
}
|
||||
|
||||
bool CollaborationDevice::isValid() const
|
||||
{
|
||||
// not show android device
|
||||
return m_isValid && m_OS != Android;
|
||||
}
|
||||
|
||||
QString CollaborationDevice::name() const
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
QString CollaborationDevice::uuid() const
|
||||
{
|
||||
return m_uuid;
|
||||
}
|
||||
|
||||
QString CollaborationDevice::machinePath() const
|
||||
{
|
||||
return m_path;
|
||||
}
|
||||
|
||||
QString CollaborationDevice::deviceIcon() const
|
||||
{
|
||||
switch (m_OS) {
|
||||
case DeviceType::Android: {
|
||||
if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType)
|
||||
return QString(":/ICON_Device_Headphone_dark.svg");
|
||||
|
||||
return QString(":/ICON_Device_Headphone.svg");
|
||||
}
|
||||
default: {
|
||||
if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType)
|
||||
return QString(":/ICON_Device_Laptop_dark.svg");
|
||||
|
||||
return QString(":/ICON_Device_Laptop.svg");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool CollaborationDevice::isConnected() const
|
||||
{
|
||||
return m_isConnected;
|
||||
}
|
||||
|
||||
bool CollaborationDevice::isCooperated() const
|
||||
{
|
||||
return m_isCooperated;
|
||||
}
|
||||
|
||||
void CollaborationDevice::setDeviceIsCooperating(bool isCooperating)
|
||||
{
|
||||
m_isCooperating = isCooperating;
|
||||
}
|
||||
|
||||
void CollaborationDevice::onPropertyChanged(const QDBusMessage &msg)
|
||||
{
|
||||
QList<QVariant> arguments = msg.arguments();
|
||||
if (3 != arguments.count())
|
||||
return;
|
||||
|
||||
QString interfaceName = msg.arguments().at(0).toString();
|
||||
if (interfaceName != QString("%1.Machine").arg(CollaborationInterface))
|
||||
return;
|
||||
|
||||
QVariantMap changedProps = qdbus_cast<QVariantMap>(arguments.at(1).value<QDBusArgument>());
|
||||
if (changedProps.contains("Connected")) {
|
||||
bool isConnected = changedProps.value("Connected").value<bool>();
|
||||
m_isConnected = isConnected;
|
||||
if (isConnected && m_isCooperating) {
|
||||
// paired 成功之后再去请求cooperate
|
||||
requestCooperate();
|
||||
}
|
||||
|
||||
if (!isConnected){
|
||||
Q_EMIT pairedStateChanged(false);
|
||||
}
|
||||
} else if (changedProps.contains("DeviceSharing")) {
|
||||
m_isCooperated = changedProps.value("DeviceSharing").value<bool>();
|
||||
|
||||
Q_EMIT pairedStateChanged(m_isCooperated);
|
||||
}
|
||||
}
|
||||
|
||||
void CollaborationDevice::requestCooperate() const
|
||||
{
|
||||
callMethod("RequestDeviceSharing");
|
||||
}
|
||||
|
||||
void CollaborationDevice::disconnectDevice() const
|
||||
{
|
||||
callMethod("Disconnect");
|
||||
}
|
||||
|
||||
void CollaborationDevice::connect() const
|
||||
{
|
||||
callMethod("Connect");
|
||||
}
|
||||
|
||||
QDBusMessage CollaborationDevice::callMethod(const QString &methodName) const
|
||||
{
|
||||
if (m_devDbusInter->isValid()) {
|
||||
QDBusMessage msg = m_devDbusInter->call(methodName);
|
||||
qInfo() << "CollaborationDevice callMethod:" << methodName << " " << msg.errorMessage();
|
||||
return msg;
|
||||
}
|
||||
|
||||
qWarning() << "CollaborationDevice callMethod: " << methodName << " failed";
|
||||
return QDBusMessage();
|
||||
}
|
@ -1,108 +0,0 @@
|
||||
// Copyright (C) 2021 ~ 2022 Uniontech Software Technology Co.,Ltd.
|
||||
// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
|
||||
//
|
||||
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
|
||||
#ifndef COLLABORATION_DEV_MODEL_H
|
||||
#define COLLABORATION_DEV_MODEL_H
|
||||
|
||||
#include <QMap>
|
||||
#include <QObject>
|
||||
|
||||
class QTimer;
|
||||
class QDBusInterface;
|
||||
class QDBusMessage;
|
||||
class CollaborationDevice;
|
||||
|
||||
/*!
|
||||
* \brief The CollaborationDevModel class
|
||||
* 协同设备model
|
||||
*/
|
||||
class CollaborationDevModel : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit CollaborationDevModel(QObject *parent = nullptr);
|
||||
|
||||
signals:
|
||||
void devicesChanged();
|
||||
|
||||
public:
|
||||
void checkServiceValid();
|
||||
|
||||
QList<CollaborationDevice *> devices() const;
|
||||
CollaborationDevice *getDevice(const QString &machinePath);
|
||||
|
||||
private slots:
|
||||
void onPropertyChanged(const QDBusMessage &msg);
|
||||
|
||||
private:
|
||||
void updateDevice(const QStringList &devPaths);
|
||||
void clear();
|
||||
|
||||
private:
|
||||
QDBusInterface *m_colDbusInter;
|
||||
// machine path : device object
|
||||
QMap<QString, CollaborationDevice *> m_devices;
|
||||
|
||||
};
|
||||
|
||||
/*!
|
||||
* \brief The CollaborationDevice class
|
||||
* 协同设备类
|
||||
*/
|
||||
class CollaborationDevice : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit CollaborationDevice(const QString &devPath, QObject *parent = nullptr);
|
||||
|
||||
signals:
|
||||
void pairedStateChanged(bool);
|
||||
|
||||
public:
|
||||
bool isValid() const;
|
||||
void connect() const;
|
||||
void requestCooperate() const;
|
||||
void disconnectDevice() const;
|
||||
|
||||
QString name() const;
|
||||
QString uuid() const;
|
||||
QString machinePath() const;
|
||||
QString deviceIcon() const;
|
||||
bool isConnected() const;
|
||||
bool isCooperated() const;
|
||||
void setDeviceIsCooperating(bool isCooperating);
|
||||
|
||||
private slots:
|
||||
void onPropertyChanged(const QDBusMessage &msg);
|
||||
|
||||
private:
|
||||
QDBusMessage callMethod(const QString &methodName) const;
|
||||
|
||||
private:
|
||||
enum DeviceType {
|
||||
Other = 0,
|
||||
UOS,
|
||||
Linux,
|
||||
Windows,
|
||||
MacOS,
|
||||
Android
|
||||
};
|
||||
|
||||
QString m_path;
|
||||
QString m_name;
|
||||
QString m_uuid;
|
||||
int m_OS;
|
||||
|
||||
bool m_isConnected;
|
||||
bool m_isCooperated;
|
||||
bool m_isValid;
|
||||
|
||||
// 标记任务栏点击触发协同连接
|
||||
bool m_isCooperating;
|
||||
|
||||
QDBusInterface *m_devDbusInter;
|
||||
};
|
||||
|
||||
#endif // COLLABORATION_DEV_MODEL_H
|
@ -1,241 +0,0 @@
|
||||
// Copyright (C) 2021 ~ 2022 Uniontech Software Technology Co.,Ltd.
|
||||
// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
|
||||
//
|
||||
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
|
||||
#include "devcollaborationwidget.h"
|
||||
#include "collaborationdevmodel.h"
|
||||
#include "devitemdelegate.h"
|
||||
|
||||
#include <DStyle>
|
||||
|
||||
#include <QMap>
|
||||
#include <QTimer>
|
||||
#include <QLabel>
|
||||
#include <QVBoxLayout>
|
||||
#include <QStandardItemModel>
|
||||
|
||||
#define TITLE_HEIGHT 30
|
||||
#define ITEM_WIDTH 310
|
||||
#define ITEM_HEIGHT 36
|
||||
#define LISTVIEW_ITEM_SPACE 5
|
||||
#define PER_DEGREE 14
|
||||
|
||||
DevCollaborationWidget::DevCollaborationWidget(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
, m_deviceModel(new CollaborationDevModel(this))
|
||||
, m_deviceListView(new DListView(this))
|
||||
, m_viewItemModel(new QStandardItemModel(m_deviceListView))
|
||||
, m_refreshTimer(new QTimer(this))
|
||||
{
|
||||
initUI();
|
||||
loadDevice();
|
||||
|
||||
connect(m_deviceModel, &CollaborationDevModel::devicesChanged, this, &DevCollaborationWidget::loadDevice);
|
||||
connect(m_deviceListView, &DListView::clicked, this, &DevCollaborationWidget::itemClicked);
|
||||
connect(m_refreshTimer, &QTimer::timeout, this, &DevCollaborationWidget::refreshViewItem);
|
||||
}
|
||||
|
||||
void DevCollaborationWidget::showEvent(QShowEvent *event)
|
||||
{
|
||||
m_deviceModel->checkServiceValid();
|
||||
|
||||
QWidget::showEvent(event);
|
||||
}
|
||||
|
||||
void DevCollaborationWidget::resizeEvent(QResizeEvent *event)
|
||||
{
|
||||
Q_EMIT sizeChanged();
|
||||
|
||||
QWidget::resizeEvent(event);
|
||||
}
|
||||
|
||||
void DevCollaborationWidget::initUI()
|
||||
{
|
||||
m_deviceListView->setModel(m_viewItemModel);
|
||||
|
||||
QLabel *title = new QLabel(tr("PC collaboration"), this);
|
||||
title->setFixedHeight(TITLE_HEIGHT);
|
||||
|
||||
QHBoxLayout *hLayout = new QHBoxLayout();
|
||||
hLayout->setContentsMargins(10, 0, 0, 0);
|
||||
hLayout->addWidget(title);
|
||||
|
||||
QVBoxLayout *mainLayout = new QVBoxLayout();
|
||||
mainLayout->setMargin(0);
|
||||
mainLayout->setContentsMargins(0, 0, 0, 0);
|
||||
mainLayout->setSpacing(0);
|
||||
mainLayout->addLayout(hLayout);
|
||||
mainLayout->addWidget(m_deviceListView);
|
||||
|
||||
setLayout(mainLayout);
|
||||
|
||||
m_deviceListView->setContentsMargins(0, 0, 0, 0);
|
||||
m_deviceListView->setFrameShape(QFrame::NoFrame);
|
||||
m_deviceListView->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
||||
m_deviceListView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||
m_deviceListView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
|
||||
m_deviceListView->setResizeMode(QListView::Adjust);
|
||||
m_deviceListView->setViewportMargins(0, 0, 0, 0);
|
||||
m_deviceListView->setSpacing(LISTVIEW_ITEM_SPACE);
|
||||
m_deviceListView->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||||
m_deviceListView->setItemDelegate(new DevItemDelegate(this));
|
||||
}
|
||||
|
||||
void DevCollaborationWidget::loadDevice()
|
||||
{
|
||||
if (!m_deviceListView->count()) {
|
||||
for (CollaborationDevice *device : m_deviceModel->devices()) {
|
||||
addItem(device);
|
||||
}
|
||||
} else {
|
||||
updateDeviceListView();
|
||||
}
|
||||
|
||||
if(!m_deviceListView->count()) {
|
||||
m_deviceListView->hide();
|
||||
} else {
|
||||
if (!m_deviceListView->isVisible())
|
||||
m_deviceListView->setVisible(true);
|
||||
|
||||
m_deviceListView->setFixedSize(ITEM_WIDTH, m_deviceListView->count() * ITEM_HEIGHT + LISTVIEW_ITEM_SPACE * (m_deviceListView->count() * 2));
|
||||
}
|
||||
|
||||
resetWidgetSize();
|
||||
}
|
||||
|
||||
void DevCollaborationWidget::addItem(const CollaborationDevice *device)
|
||||
{
|
||||
if (!device)
|
||||
return;
|
||||
|
||||
QStandardItem *item = new QStandardItem();
|
||||
DevItemDelegate::DevItemData data;
|
||||
data.checkedIconPath = device->deviceIcon(); // TODO
|
||||
data.text = device->name();
|
||||
data.iconPath = device->deviceIcon();
|
||||
int resultState = device->isCooperated() ? DevItemDelegate::Connected : DevItemDelegate::None;
|
||||
|
||||
item->setData(QVariant::fromValue(data), DevItemDelegate::StaticDataRole);
|
||||
item->setData(device->machinePath(), DevItemDelegate::MachinePathDataRole);
|
||||
item->setData(0, DevItemDelegate::DegreeDataRole);
|
||||
item->setData(resultState, DevItemDelegate::ResultDataRole);
|
||||
|
||||
m_viewItemModel->appendRow(item);
|
||||
m_deviceItemMap[device->machinePath()] = item;
|
||||
|
||||
connect(device, &CollaborationDevice::pairedStateChanged, this, &DevCollaborationWidget::itemStatusChanged);
|
||||
}
|
||||
|
||||
void DevCollaborationWidget::updateDeviceListView()
|
||||
{
|
||||
QList<CollaborationDevice *> devices = m_deviceModel->devices();
|
||||
if (devices.isEmpty()) {
|
||||
m_deviceListView->removeItems(0, m_deviceListView->count());
|
||||
m_deviceItemMap.clear();
|
||||
m_connectingDevices.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
// 删除不存在设备
|
||||
for (int row = 0; row < m_deviceListView->count(); row++) {
|
||||
QStandardItem *item = m_viewItemModel->item(row);
|
||||
if (!item)
|
||||
continue;
|
||||
|
||||
QString machinePath = item->data(DevItemDelegate::MachinePathDataRole).toString();
|
||||
if (m_deviceModel->getDevice(machinePath))
|
||||
continue;
|
||||
|
||||
m_deviceListView->removeItem(row);
|
||||
|
||||
if (m_deviceItemMap.contains(machinePath)) {
|
||||
m_deviceItemMap.remove(machinePath);
|
||||
}
|
||||
|
||||
if (m_connectingDevices.contains(machinePath)) {
|
||||
m_connectingDevices.removeAll(machinePath);
|
||||
}
|
||||
}
|
||||
|
||||
// 处理新增
|
||||
for (CollaborationDevice *device : devices) {
|
||||
if (!m_deviceItemMap.contains(device->machinePath())) {
|
||||
addItem(device);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DevCollaborationWidget::resetWidgetSize()
|
||||
{
|
||||
int height = TITLE_HEIGHT + (m_deviceListView->count() ? m_deviceListView->height() : 0);
|
||||
|
||||
setFixedSize(ITEM_WIDTH, height);
|
||||
}
|
||||
|
||||
void DevCollaborationWidget::itemClicked(const QModelIndex &index)
|
||||
{
|
||||
QString machinePath = index.data(DevItemDelegate::MachinePathDataRole).toString();
|
||||
CollaborationDevice *device = m_deviceModel->getDevice(machinePath);
|
||||
if (!device)
|
||||
return;
|
||||
|
||||
if (!device->isConnected()) {
|
||||
device->setDeviceIsCooperating(true);
|
||||
device->connect();
|
||||
if (!m_connectingDevices.contains(machinePath))
|
||||
m_connectingDevices.append(machinePath);
|
||||
} else if (!device->isCooperated()) {
|
||||
device->requestCooperate();
|
||||
if (!m_connectingDevices.contains(machinePath))
|
||||
m_connectingDevices.append(machinePath);
|
||||
} else if (device->isCooperated()) {
|
||||
device->disconnectDevice();
|
||||
if (m_connectingDevices.contains(machinePath))
|
||||
m_connectingDevices.removeOne(machinePath);
|
||||
}
|
||||
|
||||
if (!m_connectingDevices.isEmpty() && !m_refreshTimer->isActive())
|
||||
m_refreshTimer->start(80);
|
||||
}
|
||||
|
||||
void DevCollaborationWidget::itemStatusChanged()
|
||||
{
|
||||
CollaborationDevice *device = qobject_cast<CollaborationDevice *>(sender());
|
||||
if (!device)
|
||||
return;
|
||||
|
||||
device->setDeviceIsCooperating(false);
|
||||
QString machinePath = device->machinePath();
|
||||
if (m_deviceItemMap.contains(machinePath) && m_deviceItemMap[machinePath]) {
|
||||
// 更新item的连接状态
|
||||
int resultState = device->isCooperated() ? DevItemDelegate::Connected : DevItemDelegate::None;
|
||||
m_deviceItemMap[machinePath]->setData(resultState, DevItemDelegate::ResultDataRole);
|
||||
if (device->isCooperated() || !device->isConnected())
|
||||
m_deviceItemMap[machinePath]->setData(0, DevItemDelegate::DegreeDataRole);
|
||||
|
||||
m_deviceListView->update(m_deviceItemMap[machinePath]->index());
|
||||
|
||||
if ((resultState == DevItemDelegate::Connected || !device->isConnected()) && m_connectingDevices.contains(machinePath)) {
|
||||
m_connectingDevices.removeAll(machinePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DevCollaborationWidget::refreshViewItem()
|
||||
{
|
||||
if (m_connectingDevices.isEmpty()) {
|
||||
m_refreshTimer->stop();
|
||||
return;
|
||||
}
|
||||
|
||||
for (const QString &machinePath : m_connectingDevices) {
|
||||
if (m_deviceItemMap.contains(machinePath) && m_deviceItemMap[machinePath]) {
|
||||
int degree = m_deviceItemMap[machinePath]->data(DevItemDelegate::DegreeDataRole).toInt();
|
||||
degree += PER_DEGREE; // 递进值
|
||||
m_deviceItemMap[machinePath]->setData(DevItemDelegate::Connecting, DevItemDelegate::ResultDataRole);
|
||||
m_deviceItemMap[machinePath]->setData(degree, DevItemDelegate::DegreeDataRole);
|
||||
m_deviceListView->update(m_deviceItemMap[machinePath]->index());
|
||||
}
|
||||
}
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
// Copyright (C) 2021 ~ 2022 Uniontech Software Technology Co.,Ltd.
|
||||
// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
|
||||
//
|
||||
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
|
||||
#ifndef DEVICE_COLLABORATION_WIDGET_H
|
||||
#define DEVICE_COLLABORATION_WIDGET_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <DListView>
|
||||
|
||||
DWIDGET_USE_NAMESPACE
|
||||
|
||||
class CollaborationDevice;
|
||||
class CollaborationDevModel;
|
||||
|
||||
/*!
|
||||
* \brief The DevCollaborationWidget class
|
||||
* 设备跨端协同子页面
|
||||
*/
|
||||
class DevCollaborationWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit DevCollaborationWidget(QWidget *parent = nullptr);
|
||||
|
||||
signals:
|
||||
void sizeChanged();
|
||||
|
||||
protected:
|
||||
void showEvent(QShowEvent *event) override;
|
||||
void resizeEvent(QResizeEvent *event) override;
|
||||
|
||||
private slots:
|
||||
void loadDevice();
|
||||
void itemClicked(const QModelIndex &index);
|
||||
void itemStatusChanged();
|
||||
void refreshViewItem();
|
||||
|
||||
private:
|
||||
void initUI();
|
||||
void updateDeviceListView();
|
||||
|
||||
void addItem(const CollaborationDevice *device);
|
||||
void resetWidgetSize();
|
||||
|
||||
private:
|
||||
CollaborationDevModel *m_deviceModel;
|
||||
DListView *m_deviceListView;
|
||||
QStandardItemModel *m_viewItemModel;
|
||||
QMap<QString, QStandardItem *> m_deviceItemMap;
|
||||
QStringList m_connectingDevices;
|
||||
|
||||
QTimer *m_refreshTimer;
|
||||
};
|
||||
|
||||
#endif // DEVICE_COLLABORATION_WIDGET_H
|
@ -1,164 +0,0 @@
|
||||
// Copyright (C) 2021 ~ 2022 Uniontech Software Technology Co.,Ltd.
|
||||
// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
|
||||
//
|
||||
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
|
||||
#include "devitemdelegate.h"
|
||||
|
||||
#include <QtMath>
|
||||
#include <QPainter>
|
||||
#include <QPainterPath>
|
||||
|
||||
#include <DFontSizeManager>
|
||||
|
||||
#define RADIUS_VALUE 10
|
||||
#define ITEM_SPACE 20
|
||||
#define ICON_WIDTH 16
|
||||
#define ICON_HEIGHT 16
|
||||
#define TEXT_RECT_HEIGHT 20
|
||||
#define ITEM_HEIGHT 36
|
||||
#define INDICATOR_SHADOW_OFFSET 10
|
||||
|
||||
DevItemDelegate::DevItemDelegate(QObject *parent)
|
||||
: QStyledItemDelegate(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void DevItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
if (!index.isValid())
|
||||
return;
|
||||
|
||||
painter->setRenderHint(QPainter::Antialiasing);
|
||||
QVariant var = index.data(StaticDataRole);
|
||||
DevItemData itemData = var.value<DevItemData>();
|
||||
QRect rect = option.rect;
|
||||
QPen pen;
|
||||
pen.setWidth(2);
|
||||
|
||||
// 鼠标悬停
|
||||
if (option.state.testFlag(QStyle::State_MouseOver)) {
|
||||
pen.setColor(QColor("#EBECED"));
|
||||
painter->setPen(pen);
|
||||
painter->setBrush(QColor("#EBECED"));
|
||||
painter->drawRoundedRect(rect, RADIUS_VALUE, RADIUS_VALUE);
|
||||
}
|
||||
|
||||
// 选中背景(连接上和选中)
|
||||
int result = index.data(ResultDataRole).toInt();
|
||||
if (option.state.testFlag(QStyle::State_Selected) && result == Connected) {
|
||||
pen.setColor(QColor("#0081FF"));
|
||||
painter->setPen(pen);
|
||||
painter->setBrush(QColor("#0081FF"));
|
||||
painter->drawRoundedRect(rect, RADIUS_VALUE, RADIUS_VALUE);
|
||||
} else {
|
||||
// 绘制默认背景
|
||||
pen.setColor(QColor("#EBECED"));
|
||||
painter->setPen(pen);
|
||||
painter->setBrush(QColor("#EBECED"));
|
||||
painter->drawRoundedRect(rect, RADIUS_VALUE, RADIUS_VALUE);
|
||||
}
|
||||
|
||||
bool selected = (option.state.testFlag(QStyle::State_Selected) && result == Connected);
|
||||
|
||||
// 绘制Icon
|
||||
QString imagePath = selected ? itemData.checkedIconPath : itemData.iconPath;
|
||||
QRect iconRect = QRect(rect.left() + ITEM_SPACE, rect.top() + rect.height() / 2 - ICON_HEIGHT / 2,
|
||||
ICON_WIDTH, ICON_HEIGHT);
|
||||
painter->drawImage(iconRect, QImage(imagePath));
|
||||
|
||||
// 绘制text
|
||||
QFont font = Dtk::Widget::DFontSizeManager::instance()->t4();
|
||||
painter->setFont(font);
|
||||
pen.setColor(selected ? Qt::white : Qt::black);
|
||||
painter->setPen(pen);
|
||||
|
||||
int textRectWidth = rect.width() - ITEM_SPACE - iconRect.width() - iconRect.width() - ITEM_SPACE;
|
||||
QRect textRect = QRect(iconRect.right() + ITEM_SPACE, rect.top() + 2,
|
||||
textRectWidth, rect.height());
|
||||
|
||||
QFontMetrics fm(font);
|
||||
QString itemText = fm.elidedText(itemData.text, Qt::ElideRight, textRectWidth);
|
||||
painter->drawText(textRect, itemText);
|
||||
|
||||
switch (result) {
|
||||
case ResultState::Connected:
|
||||
drawResultState(painter, rect);
|
||||
break;
|
||||
case ResultState::Connecting:
|
||||
drawWaitingState(painter, rect, index.data(DegreeDataRole).toInt());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
QSize DevItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
Q_UNUSED(index)
|
||||
return QSize(option.rect.width(), ITEM_HEIGHT);
|
||||
}
|
||||
|
||||
void DevItemDelegate::drawWaitingState(QPainter *painter, const QRect &rect, int degree) const
|
||||
{
|
||||
int left = rect.width() - ITEM_SPACE;
|
||||
int top = rect.top() + rect.height() / 2 - ICON_HEIGHT / 2;
|
||||
QRect newRect(left, top, ICON_WIDTH, ICON_HEIGHT);
|
||||
|
||||
painter->setRenderHint(QPainter::Antialiasing, true);
|
||||
QList<QList<QColor>> indicatorColors;
|
||||
for (int i = 0; i < 3; i++)
|
||||
indicatorColors << createDefaultIndicatorColorList(QColor("#0081FF"));
|
||||
|
||||
double radius = 16 * 0.66;
|
||||
auto center = QRectF(newRect).center();
|
||||
auto indicatorRadius = radius / 2 / 2 * 1.1;
|
||||
auto indicatorDegreeDelta = 360 / indicatorColors.count();
|
||||
|
||||
for (int i = 0; i < indicatorColors.count(); ++i) {
|
||||
QList<QColor> colors = indicatorColors.value(i);
|
||||
for (int j = 0; j < colors.count(); ++j) {
|
||||
double degreeCurrent = degree - j * INDICATOR_SHADOW_OFFSET + indicatorDegreeDelta * i;
|
||||
auto x = (radius - indicatorRadius) * qCos(qDegreesToRadians(degreeCurrent));
|
||||
auto y = (radius - indicatorRadius) * qSin(qDegreesToRadians(degreeCurrent));
|
||||
|
||||
x = center.x() + x;
|
||||
y = center.y() + y;
|
||||
auto tl = QPointF(x - 1 * indicatorRadius, y - 1 * indicatorRadius);
|
||||
QRectF rf(tl.x(), tl.y(), indicatorRadius * 2, indicatorRadius * 2);
|
||||
|
||||
QPainterPath path;
|
||||
path.addEllipse(rf);
|
||||
|
||||
painter->fillPath(path, colors.value(j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DevItemDelegate::drawResultState(QPainter *painter, const QRect &rect) const
|
||||
{
|
||||
// 绘制对勾,14x12
|
||||
int left = rect.width() - ITEM_SPACE;
|
||||
int top = rect.top() + rect.height() / 2 - 6;
|
||||
|
||||
QPainterPath path;
|
||||
path.moveTo(left, top + 6);
|
||||
path.lineTo(left + 4, top + 11);
|
||||
path.lineTo(left + 12, top + 1);
|
||||
|
||||
painter->drawPath(path);
|
||||
}
|
||||
|
||||
QList<QColor> DevItemDelegate::createDefaultIndicatorColorList(QColor color) const
|
||||
{
|
||||
QList<QColor> colors;
|
||||
QList<int> opacitys;
|
||||
opacitys << 100 << 30 << 15 << 10 << 5 << 4 << 3 << 2 << 1;
|
||||
for (int i = 0; i < opacitys.count(); ++i) {
|
||||
color.setAlpha(255 * opacitys.value(i) / 100);
|
||||
colors << color;
|
||||
}
|
||||
|
||||
return colors;
|
||||
}
|
@ -1,52 +0,0 @@
|
||||
// Copyright (C) 2021 ~ 2022 Uniontech Software Technology Co.,Ltd.
|
||||
// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
|
||||
//
|
||||
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
|
||||
#ifndef DEVITEMDELEGATE_H
|
||||
#define DEVITEMDELEGATE_H
|
||||
|
||||
#include <DStyledItemDelegate>
|
||||
|
||||
/*!
|
||||
* \brief The DevItemDelegate class
|
||||
*/
|
||||
class DevItemDelegate : public QStyledItemDelegate
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum DevItemDataRole {
|
||||
StaticDataRole = Qt::UserRole + 1, // 静态信息
|
||||
MachinePathDataRole = Qt::UserRole + 2, // machinePath, 可唯一代表一个设备
|
||||
DegreeDataRole = Qt::UserRole + 3, // degree 绘制waiting使用的参数
|
||||
ResultDataRole = Qt::UserRole + 4 // 连接结果
|
||||
};
|
||||
|
||||
enum ResultState {
|
||||
None,
|
||||
Connecting,
|
||||
Connected
|
||||
};
|
||||
|
||||
struct DevItemData {
|
||||
QString checkedIconPath;
|
||||
QString iconPath;
|
||||
QString text;
|
||||
};
|
||||
|
||||
public:
|
||||
explicit DevItemDelegate(QObject *parent = nullptr);
|
||||
|
||||
protected:
|
||||
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
|
||||
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
|
||||
|
||||
private:
|
||||
void drawWaitingState(QPainter *painter, const QRect &rect, int degree) const;
|
||||
void drawResultState(QPainter *painter, const QRect &rect) const;
|
||||
QList<QColor> createDefaultIndicatorColorList(QColor color) const;
|
||||
};
|
||||
|
||||
Q_DECLARE_METATYPE(DevItemDelegate::DevItemData)
|
||||
|
||||
#endif // DEVITEMDELEGATE_H
|
@ -4,7 +4,6 @@
|
||||
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
|
||||
#include "displaysettingwidget.h"
|
||||
#include "devcollaborationwidget.h"
|
||||
|
||||
#include <QPushButton>
|
||||
#include <QVBoxLayout>
|
||||
@ -16,18 +15,8 @@ const int ItemSpacing = 10;
|
||||
DisplaySettingWidget::DisplaySettingWidget(BrightnessModel *model, QWidget *parent)
|
||||
: QWidget(parent)
|
||||
, m_brightnessAdjWidget(new BrightnessAdjWidget(model, this))
|
||||
, m_collaborationWidget(new DevCollaborationWidget(this))
|
||||
, m_settingBtn(new QPushButton(tr("Multi-Screen Collaboration"), this))
|
||||
{
|
||||
initUI();
|
||||
|
||||
connect(m_settingBtn, &QPushButton::clicked, this, [ this ](){
|
||||
DDBusSender().service("org.deepin.dde.ControlCenter1")
|
||||
.path("/org/deepin/dde/ControlCenter1")
|
||||
.interface("org.deepin.dde.ControlCenter1")
|
||||
.method("ShowPage").arg(QString("display")).call();
|
||||
Q_EMIT requestHide();
|
||||
});
|
||||
}
|
||||
|
||||
void DisplaySettingWidget::initUI()
|
||||
@ -38,15 +27,11 @@ void DisplaySettingWidget::initUI()
|
||||
mainLayout->setSpacing(ItemSpacing);
|
||||
|
||||
mainLayout->addWidget(m_brightnessAdjWidget);
|
||||
mainLayout->addWidget(m_collaborationWidget);
|
||||
mainLayout->addWidget(m_settingBtn);
|
||||
mainLayout->addStretch();
|
||||
|
||||
setLayout(mainLayout);
|
||||
|
||||
resizeWidgetHeight();
|
||||
connect(m_collaborationWidget, &DevCollaborationWidget::sizeChanged,
|
||||
this, &DisplaySettingWidget::resizeWidgetHeight);
|
||||
connect(m_brightnessAdjWidget, &BrightnessAdjWidget::sizeChanged,
|
||||
this, &DisplaySettingWidget::resizeWidgetHeight);
|
||||
}
|
||||
@ -54,6 +39,5 @@ void DisplaySettingWidget::initUI()
|
||||
void DisplaySettingWidget::resizeWidgetHeight()
|
||||
{
|
||||
QMargins margins = this->contentsMargins();
|
||||
setFixedHeight(margins.top() + margins.bottom() + m_brightnessAdjWidget->height() +
|
||||
m_collaborationWidget->height() + m_settingBtn->height() + ItemSpacing * 2);
|
||||
setFixedHeight(margins.top() + margins.bottom() + m_brightnessAdjWidget->height());
|
||||
}
|
||||
|
@ -12,7 +12,6 @@
|
||||
|
||||
class QPushButton;
|
||||
class BrightnessAdjWidget;
|
||||
class DevCollaborationWidget;
|
||||
|
||||
/*!
|
||||
* \brief The DisplaySettingWidget class
|
||||
@ -34,8 +33,6 @@ private:
|
||||
|
||||
private:
|
||||
BrightnessAdjWidget *m_brightnessAdjWidget; // 亮度调整
|
||||
DevCollaborationWidget *m_collaborationWidget; // 跨端协同
|
||||
QPushButton *m_settingBtn; // 设置按钮
|
||||
};
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user