| 1 | // SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-only |
| 4 | |
| 5 | #include "deviceinfoitem.h" |
| 6 | #include "widgetutil.h" |
| 7 | |
| 8 | #include <DApplication> |
| 9 | #include <DProgressBar> |
| 10 | #include <DApplicationHelper> |
| 11 | #include <DFontSizeManager> |
| 12 | |
| 13 | #include <QDebug> |
| 14 | #include <QHBoxLayout> |
| 15 | #include <QPixmap> |
| 16 | #include <QPainter> |
| 17 | |
| 18 | DeviceDelegate::DeviceDelegate(QObject *parent) |
| 19 | : QItemDelegate(parent) |
| 20 | { |
| 21 | |
| 22 | } |
| 23 | |
| 24 | void DeviceDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
| 25 | { |
| 26 | // do nothing |
| 27 | // return QItemDelegate::paint (painter, option, index); |
| 28 | } |
| 29 | |
| 30 | |
| 31 | DeviceInfoItem::DeviceInfoItem(const QString &name, const QString &device, |
| 32 | const QString &cap, int percent, DWidget *parent) |
| 33 | : DWidget(parent) |
| 34 | { |
| 35 | s_removeDevice = WidgetUtil::getDpiPixmap(":/theme/light/image/drive.svg", this); |
| 36 | |
| 37 | auto mainLayout = new QHBoxLayout(this); |
| 38 | mainLayout->setContentsMargins(9, 0, 6, 0); |
| 39 | mainLayout->setSpacing(0); |
| 40 | |
| 41 | m_deviceIcon = new DLabel; |
| 42 | m_deviceIcon->setObjectName("DeviceInfoIcon"); |
| 43 | m_deviceIcon->setPixmap(s_removeDevice); |
| 44 | |
| 45 | auto m_deviceLabel = new DLabel; |
| 46 | m_deviceLabel->setObjectName("DeviceInfoLabel"); |
| 47 | DPalette pa = DApplicationHelper::instance()->palette(m_deviceLabel); |
| 48 | QBrush brush = DApplicationHelper::instance()->palette(m_deviceLabel).text(); |
| 49 | pa.setBrush(DPalette::Text, brush); |
| 50 | m_deviceLabel->setText(name); |
| 51 | m_deviceLabel->setFixedHeight(25); |
| 52 | DFontSizeManager::instance()->bind(m_deviceLabel, DFontSizeManager::T6); |
| 53 | |
| 54 | auto m_deviceDevName = new DLabel; |
| 55 | m_deviceDevName->setObjectName("DeviceInfoDevName"); |
| 56 | m_deviceDevName->setText(QString("%1").arg(device)); |
| 57 | m_deviceDevName->setFixedHeight(20); |
| 58 | DFontSizeManager::instance()->bind(m_deviceDevName, DFontSizeManager::T8); |
| 59 | |
| 60 | m_deviceDevName->setAlignment(Qt::AlignCenter); |
| 61 | auto m_deviceCapacity = new DLabel; |
| 62 | m_deviceCapacity->setObjectName("DeviceInfoCapacity"); |
| 63 | m_deviceCapacity->setText(cap); |
| 64 | m_deviceCapacity->setFixedHeight(18); |
| 65 | DFontSizeManager::instance()->bind(m_deviceCapacity, DFontSizeManager::T10); |
| 66 | |
| 67 | m_deviceCapacity->setAlignment(Qt::AlignCenter); |
| 68 | #if defined(Q_OS_WIN32)||defined(Q_OS_MAC) |
| 69 | m_deviceCapacity->hide(); |
| 70 | #endif |
| 71 | |
| 72 | auto m_deviceCapacityBar = new DProgressBar; |
| 73 | m_deviceCapacityBar->setObjectName("DeviceInfoCapacityBar"); |
| 74 | m_deviceCapacityBar->setTextVisible(false); |
| 75 | m_deviceCapacityBar->setFixedSize(292, 4); |
| 76 | m_deviceCapacityBar->setValue(percent); |
| 77 | |
| 78 | m_fillingposition = new DLabel; |
| 79 | |
| 80 | auto m_bodywidget = new DWidget; |
| 81 | auto bodyLayout = new QVBoxLayout(m_bodywidget); |
| 82 | bodyLayout->setMargin(0); |
| 83 | auto m_middlewidget = new DWidget; |
| 84 | m_middlewidget->setFixedWidth(292); |
| 85 | m_middlewidget->setFixedHeight(18); |
| 86 | auto middleLayout = new QHBoxLayout(m_middlewidget); |
| 87 | middleLayout->setMargin(0); |
| 88 | |
| 89 | middleLayout->addWidget(m_deviceDevName, 0, Qt::AlignLeft); |
| 90 | middleLayout->addWidget(m_deviceCapacity, 0, Qt::AlignRight); |
| 91 | |
| 92 | bodyLayout->addSpacing(6); |
| 93 | bodyLayout->addWidget(m_deviceLabel, 0, Qt::AlignLeft); |
| 94 | bodyLayout->addSpacing(0); |
| 95 | bodyLayout->addWidget(m_middlewidget, 0, Qt::AlignLeft); |
| 96 | bodyLayout->addSpacing(5); |
| 97 | bodyLayout->addWidget(m_deviceCapacityBar, 0, Qt::AlignCenter); |
| 98 | bodyLayout->addSpacing(9); |
| 99 | |
| 100 | mainLayout->addWidget(m_deviceIcon, 0, Qt::AlignLeft); |
| 101 | mainLayout->addSpacing(7); |
| 102 | mainLayout->addWidget(m_bodywidget, 0, Qt::AlignLeft); |
| 103 | mainLayout->addSpacing(10); |
| 104 | mainLayout->addWidget(m_fillingposition, 0, Qt::AlignRight); |
| 105 | setFixedSize(400, 70); |
| 106 | setProperty("needformat", false); |
| 107 | |
| 108 | connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, |
| 109 | this, [ = ] { |
| 110 | DPalette pa; |
| 111 | DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType(); |
| 112 | if (themeType == DGuiApplicationHelper::LightType) |
| 113 | { |
| 114 | pa = m_deviceDevName->palette(); |
| 115 | pa.setColor(DPalette::Text, QColor("#8AA1B4")); |
| 116 | m_deviceDevName->setPalette(pa); |
| 117 | pa = m_deviceCapacity->palette(); |
| 118 | pa.setColor(DPalette::Text, QColor("#8AA1B4")); |
| 119 | m_deviceCapacity->setPalette(pa); |
| 120 | } else if (themeType == DGuiApplicationHelper::DarkType) |
| 121 | { |
| 122 | pa = m_deviceDevName->palette(); |
| 123 | pa.setColor(DPalette::Text, QColor("#6D7C88")); |
| 124 | m_deviceDevName->setPalette(pa); |
| 125 | pa = m_deviceCapacity->palette(); |
| 126 | pa.setColor(DPalette::Text, QColor("#6D7C88")); |
| 127 | m_deviceCapacity->setPalette(pa); |
| 128 | } |
| 129 | |
| 130 | setCheck(checked); |
| 131 | }); |
| 132 | |
| 133 | emit DGuiApplicationHelper::instance()->themeTypeChanged(DGuiApplicationHelper::instance()->themeType()); |
| 134 | } |
| 135 | |
| 136 | DeviceInfoItem::DeviceInfoItem(DWidget *parent) : |
| 137 | DeviceInfoItem("Remove Device", "NULL", "0/0G", 0, parent) |
| 138 | { |
| 139 | setProperty("needformat", false); |
| 140 | } |
| 141 | |
| 142 | void DeviceInfoItem::paintEvent(QPaintEvent *event) |
| 143 | { |
| 144 | QColor baccolor; |
| 145 | DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType(); |
| 146 | if (themeType == DGuiApplicationHelper::LightType) { |
| 147 | baccolor = QColor(0, 0, 0, 8); |
| 148 | } else if (themeType == DGuiApplicationHelper::DarkType) { |
| 149 | baccolor = QColor(255, 255, 255, 13); |
| 150 | } |
| 151 | QPainter painter(this); |
| 152 | painter.setRenderHint(QPainter::Antialiasing); |
| 153 | painter.setBrush(QBrush(baccolor)); |
| 154 | painter.setPen(Qt::transparent); |
| 155 | QRect rect = this->rect(); |
| 156 | rect.setWidth(rect.width() - 1); |
| 157 | rect.setHeight(rect.height() - 1); |
| 158 | painter.drawRoundedRect(rect, 8, 8); |
| 159 | DWidget::paintEvent(event); |
| 160 | } |
| 161 | |
| 162 | QPixmap DeviceInfoItem::renderSVG(const QString &filePath, const QSize &size) |
| 163 | { |
| 164 | QImageReader reader; |
| 165 | QPixmap pixmap; |
| 166 | |
| 167 | reader.setFileName(filePath); |
| 168 | |
| 169 | if (reader.canRead()) { |
| 170 | const qreal ratio = qApp->devicePixelRatio(); |
| 171 | reader.setScaledSize(size * ratio); |
| 172 | pixmap = QPixmap::fromImage(reader.read()); |
| 173 | pixmap.setDevicePixelRatio(ratio); |
| 174 | } else { |
| 175 | pixmap.load(filePath); |
| 176 | } |
| 177 | |
| 178 | return pixmap; |
| 179 | } |
| 180 | |
| 181 | void DeviceInfoItem::setCheck(bool flag) |
| 182 | { |
| 183 | if (flag) { |
| 184 | auto icon = qobject_cast<DStyle *>(style())->standardIcon(DStyle::SP_IndicatorChecked); |
| 185 | QPixmap pixmap = icon.pixmap(QSize(16, 16), QIcon::Active); |
| 186 | m_fillingposition->setPixmap(pixmap); |
| 187 | } else { |
| 188 | m_fillingposition->clear(); |
| 189 | } |
| 190 | |
| 191 | checked = flag; |
| 192 | } |
| 193 | |
| 194 | bool DeviceInfoItem::isChecked() |
| 195 | { |
| 196 | return checked; |
| 197 | } |
| 198 | |
| 199 | bool DeviceInfoItem::needFormat() const |
| 200 | { |
| 201 | return property("needformat").toBool(); |
| 202 | } |
| 203 | |
| 204 | void DeviceInfoItem::setNeedFormat(bool format) |
| 205 | { |
| 206 | setProperty("needformat", format); |
| 207 | } |