diff --git a/ExChangeSVGColor/.gitignore b/ExChangeSVGColor/.gitignore new file mode 100644 index 0000000..1358d83 --- /dev/null +++ b/ExChangeSVGColor/.gitignore @@ -0,0 +1,73 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe + diff --git a/ExChangeSVGColor/CMakeLists.txt b/ExChangeSVGColor/CMakeLists.txt new file mode 100644 index 0000000..2021ea6 --- /dev/null +++ b/ExChangeSVGColor/CMakeLists.txt @@ -0,0 +1,68 @@ +cmake_minimum_required(VERSION 3.5) + +project(ChangeSVGColor VERSION 0.1 LANGUAGES CXX) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(CMAKE_AUTOUIC ON) +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# QtCreator supports the following variables for Android, which are identical to qmake Android variables. +# Check https://doc.qt.io/qt/deployment-android.html for more information. +# They need to be set before the find_package( ...) calls below. + +#if(ANDROID) +# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android") +# if (ANDROID_ABI STREQUAL "armeabi-v7a") +# set(ANDROID_EXTRA_LIBS +# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so +# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so) +# endif() +#endif() + +find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets Svg Xml REQUIRED) +find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets Svg Xml REQUIRED) + +set(PROJECT_SOURCES + main.cpp + widget.cpp + widget.h + resource.qrc +) + +if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) + qt_add_executable(ExQRect + MANUAL_FINALIZATION + ${PROJECT_SOURCES} + ) +else() + if(ANDROID) + add_library(ExQRect SHARED + ${PROJECT_SOURCES} + ) + else() + add_executable(ExQRect + ${PROJECT_SOURCES} + ) + endif() +endif() + +target_link_libraries(ExQRect PRIVATE + Qt${QT_VERSION_MAJOR}::Widgets + Qt${QT_VERSION_MAJOR}::Svg + Qt${QT_VERSION_MAJOR}::Xml + ) + +set_target_properties(ExQRect PROPERTIES + MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com + MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} + MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} +) + +if(QT_VERSION_MAJOR EQUAL 6) + qt_finalize_executable(ExQRect) +endif() diff --git a/ExChangeSVGColor/README.md b/ExChangeSVGColor/README.md new file mode 100644 index 0000000..4fde964 --- /dev/null +++ b/ExChangeSVGColor/README.md @@ -0,0 +1,7 @@ +### 效果:修改 `.svg` 的图片颜色,且不模糊(保持相同大小) + + + + + + \ No newline at end of file diff --git a/ExChangeSVGColor/display.gif b/ExChangeSVGColor/display.gif new file mode 100644 index 0000000..53e16c1 Binary files /dev/null and b/ExChangeSVGColor/display.gif differ diff --git a/ExChangeSVGColor/main.cpp b/ExChangeSVGColor/main.cpp new file mode 100644 index 0000000..c7171f6 --- /dev/null +++ b/ExChangeSVGColor/main.cpp @@ -0,0 +1,37 @@ +#include "widget.h" + +#include +#include +#include +#include +#include + + + + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + Widget w; + w.show(); + + +// QVector> vLab; +// vLab.append(QMap({"labRed", "#DB000F"})); +// vLab.append(QMap({"labYellow", "#FFCF53"})); +// vLab.append(QMap({"labGreen", "#12F63B"})); +// vLab.append(QMap({"labBlue", "#0E70FF"})); +// vLab.append(QMap({"labPink", "#FB4288"})); +// vLab.append(QMap({"labBlack", "#323232"})); +// vLab.append(QMap({"labWhite", "#FBFBFB"})); +// vLab.append(QMap({"labPick", "#FFDB34"})); // + +// qDebug() << vLab[2].first() << " "; + + +// QPushButton* btn = new QPushButton("btn123"); +// QIcon icon("D:/projects/PicShot/src/resourcesicons/normal/gif.svg"); +// btn->setIcon(icon); +// btn->show(); + return a.exec(); +} diff --git a/ExChangeSVGColor/mosaic.svg b/ExChangeSVGColor/mosaic.svg new file mode 100644 index 0000000..812a3bc --- /dev/null +++ b/ExChangeSVGColor/mosaic.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/ExChangeSVGColor/resource.qrc b/ExChangeSVGColor/resource.qrc new file mode 100644 index 0000000..3d544ee --- /dev/null +++ b/ExChangeSVGColor/resource.qrc @@ -0,0 +1,5 @@ + + + mosaic.svg + + diff --git a/ExChangeSVGColor/widget.cpp b/ExChangeSVGColor/widget.cpp new file mode 100644 index 0000000..635294d --- /dev/null +++ b/ExChangeSVGColor/widget.cpp @@ -0,0 +1,143 @@ +#include "widget.h" + +#include +#include + +// test +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +QIcon ChangeSVGColor(QString path, QString color, QSize size) +{ + QFile file(path); + file.open(QIODevice::ReadOnly); + QByteArray baData = file.readAll(); + + QDomDocument doc; + doc.setContent(baData); + QDomElement elem = doc.documentElement(); // const 和 值传递 + SetAttrRecur(elem, "path", "fill", color); + + QSvgRenderer svgRenderer(doc.toByteArray()); + // create pixmap target (could be a QImage) +// QPixmap pix(svgRenderer.defaultSize()); + QPixmap pix(svgRenderer.defaultSize().scaled(size.width(), size.height(), Qt::KeepAspectRatio)); // fix: 修改 svg 颜色后略有模糊 + pix.fill(Qt::transparent); + // create painter to act over pixmap + QPainter pixPainter(&pix); + // use renderer to render over painter which paints on pixmap + svgRenderer.render(&pixPainter); + QIcon myicon(pix); + return myicon; +} + + +void SetAttrRecur(QDomElement &elem, QString strtagname, QString strattr, QString strattrval) +{ + // if it has the tagname then overwritte desired attribute + if (elem.tagName().compare(strtagname) == 0) + { + elem.setAttribute(strattr, strattrval); + } + // loop all children + for (int i = 0; i < elem.childNodes().count(); i++) + { + if (!elem.childNodes().at(i).isElement()) + { + continue; + } + QDomElement t = elem.childNodes().at(i).toElement(); + SetAttrRecur(t, strtagname, strattr, strattrval); + } +} + + + + +Widget::Widget(QWidget *parent) + : QWidget(parent) + , m_btn() +{ + setWindowTitle("change .svg color"); + resize(1600, 1000); + const int width = 80; + + QIcon icon(ChangeSVGColor(":/mosaic.svg", QColor(Qt::green).name(), QSize(width, width))); + m_btn = new QToolButton(this); + m_btn->setCheckable(true); + m_btn->setToolButtonStyle(Qt::ToolButtonIconOnly); + m_btn->setAutoRaise(true); // 自动浮动模式 + m_btn->setIcon(icon); + m_btn->setIconSize(QSize(width, width)); +// m_btn->resize(200, 200); + m_btn->move(0, 0); + + + QIcon icon2(":/mosaic.svg"); + m_btn2 = new QToolButton(this); + m_btn2->setCheckable(true); + m_btn2->setToolButtonStyle(Qt::ToolButtonIconOnly); + m_btn2->setAutoRaise(true); // 自动浮动模式 + m_btn2->setIcon(icon); + m_btn2->setIconSize(QSize(width, width)); +// m_btn->resize(200, 200); + m_btn2->move(100, 100); + + + m_btn->show(); + m_btn2->show(); + + connect(m_btn, &QToolButton::clicked, this, [&](bool checked){ + QString path(":/mosaic.svg"); + + QIcon ico; + if (checked) + ico.addFile(path); + else + ico = ChangeSVGColor(":/mosaic.svg", QColor(Qt::green).name(), QSize(width, width)); + + m_btn->setIcon(ico); + }); + + + +} + +Widget::~Widget() +{ +} + +void Widget::paintEvent(QPaintEvent *event) +{ + QPainter pa(this); + pa.setBrush(Qt::NoBrush); + pa.setPen(QPen(Qt::black, 1)); + + pa.setPen(Qt::blue); + QRect rt(200, 200, 200, 200); + pa.drawRect(rt); + + QPoint pos(500, 200); + int off = 30; + pa.drawText(pos + QPoint(0, off * 0), QString("rt.x():%1 rt.y():%2 rt.width():%3 rt.height():%4") + .arg(rt.x()).arg(rt.y()).arg(rt.width()).arg(rt.height())); + pa.drawText(pos + QPoint(0, off * 1), QString("rt.left():%1 rt.top():%2 rt.right():%3 rt.bottom():%4") + .arg(rt.left()).arg(rt.top()).arg(rt.right()).arg(rt.bottom())); + pa.drawText(pos + QPoint(0, off * 2), QString("rt.topLeft().x():%1 rt.topLeft().y():%2 rt.bottomRight().x():%3 rt.bottomRight().y():%4") + .arg(rt.topLeft().x()).arg(rt.topLeft().y()).arg(rt.bottomRight().x()).arg(rt.bottomRight().y())); + + update(); +} + + + + diff --git a/ExChangeSVGColor/widget.h b/ExChangeSVGColor/widget.h new file mode 100644 index 0000000..4b197c7 --- /dev/null +++ b/ExChangeSVGColor/widget.h @@ -0,0 +1,29 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include +#include +#include + +class QDomElement; +class Widget : public QWidget +{ + Q_OBJECT + +public: + Widget(QWidget *parent = nullptr); + ~Widget(); + + // QWidget interface +protected: + virtual void paintEvent(QPaintEvent *event) override; + + +private: + QToolButton* m_btn; + QToolButton* m_btn2; +}; + + +void SetAttrRecur(QDomElement &elem, QString strtagname, QString strattr, QString strattrval); +#endif // WIDGET_H