ukui-theme-customizer/iconwidgetfeature.cpp

238 lines
7.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "iconwidgetfeature.h"
MainInterFaceFeature::MainInterFaceFeature(QWidget *parent) : QWidget(parent)
{
}
TypeButton::TypeButton(QWidget *parent): QPushButton(parent)
{
setCheckable(true);
}
void TypeButton::paintEvent(QPaintEvent *event)
{
QPushButton::paintEvent(event);
if (isChecked()) {
QPainter painter(this);
QIcon icon = QIcon::fromTheme("kylin-settings-account");
QPixmap pixmap = icon.pixmap(QSize(16, 16));
QRect pixmapRect(rect().right() - pixmap.width(), (rect().height() - pixmap.height()) / 2, pixmap.width(), pixmap.height());
painter.drawPixmap(pixmapRect, pixmap);
}
}
IconGraphicsView::IconGraphicsView(QGraphicsScene* scene) : QGraphicsView(scene)
{
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
setDragMode(QGraphicsView::ScrollHandDrag);
setRenderHint(QPainter::Antialiasing, true);
setOptimizationFlags(QGraphicsView::DontAdjustForAntialiasing |QGraphicsView::DontSavePainterState);
this->setStyleSheet("background-color: transparent;");
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
void IconGraphicsView::wheelEvent(QWheelEvent* event)
{
if (QGuiApplication::keyboardModifiers() == Qt::ControlModifier) {
qreal scaleFactor = std::pow(qreal(2), event->delta() / 240.0);
scale(scaleFactor, scaleFactor);
} else {
QGraphicsView::wheelEvent(event);
}
}
ImageWidget::ImageWidget(QWidget *parent, const QMap<QString, QString>* iconMap)
: QWidget(parent), m_iconMap(iconMap)
{
QGraphicsScene* scene = new QGraphicsScene(this);
scene->setBackgroundBrush(Qt::transparent);
graphicsView = new IconGraphicsView(scene);
QVBoxLayout* layout = new QVBoxLayout(this);
layout->addWidget(graphicsView);
setLayout(layout);
int count = m_iconMap->size();
if(count == 11){
image = new QPixmap(":/resource/background/controlcenter-light.png");
if (image->isNull()) {
qDebug() << "Failed to load image.";
}
}else{
image = new QPixmap(":/resource/background/background-light.png");
if (image->isNull()) {
qDebug() << "Failed to load image.";
}
}
scene->addPixmap(*image);
graphicsView->setScene(scene);
graphicsView->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
graphicsView->show();
if (count == 11) {
int columnCount = 4;
int row = 1;
int col = 0; // 将col设置为0因为会在循环开始时自增
for (auto it = m_iconMap->begin(); it != m_iconMap->end(); ++it) {
const QString& widgetName = it.key();
const QString& filePath = it.value();
QPixmap pixmap(filePath);
if (!pixmap.isNull()) {
QGraphicsPixmapItem* item = scene->addPixmap(pixmap.scaled(128, 128));
if (col == 0 && row == 1) {
// 设置第一个item的位置
item->setPos(col * 320, row * 320);
} else {
// 设置其他item的位置
item->setPos(col % columnCount * 320, row * 320);
}
col++;
if (col >= columnCount) {
col = 0;
row++;
}
} else {
qDebug() << "Failed to load image:" << filePath;
}
}
}
else{
int columnCount = 10;
int row = 1;
int col = 2;
for (auto it = m_iconMap->begin(); it != m_iconMap->end(); ++it) {
const QString& widgetName = it.key();
const QString& filePath = it.value();
QPixmap pixmap(filePath);
if (!pixmap.isNull()) {
QGraphicsPixmapItem* item = scene->addPixmap(pixmap.scaled(128, 128));
item->setPos(col * 160, row * 160);
item->setScale(1.0);
item->setData(0, widgetName);
item->setData(1, filePath);
qDebug()<<"widgetName"<<widgetName<<"filePath"<<filePath;
col++;
if (col >= columnCount) {
col = 2;
row++;
}
} else {
qDebug() << "Failed to load image:" << filePath;
}
}
}
}
void ImageWidget::updateIcon(const QString& widgetName, const QString& newFilePath)
{
QGraphicsScene* scene = graphicsView->scene();
QList<QGraphicsItem*> items = scene->items();
for (QGraphicsItem* item : items) {
QGraphicsPixmapItem* pixmapItem = dynamic_cast<QGraphicsPixmapItem*>(item);
if (pixmapItem && pixmapItem->data(0).toString() == widgetName) {
QPointF oldPosition = pixmapItem->pos();
scene->removeItem(pixmapItem);
delete pixmapItem;
QPixmap newPixmap(newFilePath);
if (!newPixmap.isNull()) {
QGraphicsPixmapItem* newPixmapItem = scene->addPixmap(newPixmap.scaled(128, 128));
newPixmapItem->setData(0, widgetName);
newPixmapItem->setData(1, newFilePath);
newPixmapItem->setPos(oldPosition);
} else {
qDebug() << "Failed to load image:" << newFilePath;
}
return;
}
}
}
IconEditWidget::IconEditWidget(QWidget *parent)
{
this->setFixedHeight(110);
QHBoxLayout *m_iconwidgetlayout = new QHBoxLayout();
m_icondefaultlabel = new QLabel();
m_icondefaultlabel->setFixedSize(48,48);
m_icondecustomlabel = new CustomLabel();
m_addiconbutton = new QPushButton();
m_addiconbutton->setIcon(QIcon::fromTheme("list-add-symbolic"));
m_addiconbutton->setFixedSize(36,36);
QLabel *m_tiplabel = new QLabel();
m_tiplabel->setWordWrap(true);
m_tiplabel->setText("<html>格式svg<br>尺寸96*96</html>");
m_iconwidgetlayout->addWidget(m_icondefaultlabel);
m_iconwidgetlayout->addWidget(m_icondecustomlabel);
m_iconwidgetlayout->addWidget(m_addiconbutton);
m_iconwidgetlayout->addWidget(m_tiplabel);
this->setLayout(m_iconwidgetlayout);
}
void IconEditWidget::setdefaulticon(QString iconname)
{
m_icondefaultlabel->setPixmap(QIcon::fromTheme(iconname).pixmap(48,48));
}
void IconEditWidget::setcustomicon(QString iconFilePath)
{
QPixmap pixmap(iconFilePath);
m_icondecustomlabel->setPixmap(pixmap.scaled(48, 48));
}
CustomLabel::CustomLabel(QWidget *parent): QLabel(parent), pixmap()
{
this->setFixedSize(48,48);
}
void CustomLabel::paintEvent(QPaintEvent *event)
{
QLabel::paintEvent(event);
QPainter painter(this);
QPen pen(Qt::blue);
pen.setStyle(Qt::DashLine);
painter.setPen(pen);
QBrush brush(QColor("#F5F5F5"));
painter.setBrush(brush);
QRect roundedRect = rect().adjusted(1, 1, -1, -1);
QPainterPath path;
int radius = 6;
path.addRoundedRect(roundedRect, radius, radius);
painter.drawPath(path);
if (!pixmap.isNull()) {
painter.drawPixmap(rect(), pixmap);
}
}
void CustomLabel::setPixmap(const QPixmap &pixmap)
{
this->pixmap = pixmap;
update();
}