Commit b6fb8c85 authored by Carson Black's avatar Carson Black

Add icon manipulation options

parent d6f08e22
build
flatbuild
.flatpak-builder
.vscode
CMakeLists.txt.user
\ No newline at end of file
......@@ -13,7 +13,7 @@ find_package(KF5Plasma)
find_package(KF5Kirigami2)
find_package(KF5PlasmaQuick)
add_executable(${PROJECT_NAME} "src/main.cpp" "qrc/qml.qrc" "qrc/images.qrc" "src/iconsetter.cpp" "src/iconsetter.h")
add_executable(${PROJECT_NAME} "src/main.cpp" "qrc/qml.qrc" "qrc/images.qrc" "src/iconsetter.cpp" "src/iconsetter.h" "src/iconmanipulator.cpp" "src/iconmanipulator.h")
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::WebEngine Qt5::Core Qt5::Quick Qt5::QuickControls2 KF5::Kirigami2 KF5::Plasma KF5::PlasmaQuick)
......
......@@ -3,7 +3,7 @@ import QtGraphicalEffects 1.12
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 2.0 as PlasmaComponents
import org.kde.kirigami 2.0 as Kirigami
import me.appadeia.IconSetter 1.0
import me.appadeia.Ikona 1.0
Item {
id: itemRoot
......
......@@ -8,7 +8,7 @@ import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 2.0 as PlasmaComponents
import org.kde.plasma.extras 2.0 as PlasmaExtras
import QtGraphicalEffects 1.12
import me.appadeia.IconSetter 1.0
import me.appadeia.Ikona 1.0
import Qt.labs.settings 1.1
import Qt.labs.platform 1.1 as QNative
import QtWebEngine 1.1
......@@ -193,6 +193,9 @@ Kirigami.ApplicationWindow {
IconSetter {
id: setter
}
IconManipulator {
id: manipulator
}
title: qsTr("Ikona Design Companion")
color: Kirigami.Theme.backgroundColor
......@@ -319,6 +322,43 @@ Kirigami.ApplicationWindow {
}
}
},
Kirigami.Action {
iconSource: "editimage"
text: "Modify Icon"
Kirigami.Action {
iconSource: "edit-clear-all"
text: "Tidy Icon"
onTriggered: {
if (manipulator.tidyIcon(imageSource)) {
root.showPassiveNotification("Your icon has been cleaned.")
} else {
root.showPassiveNotification("Something went wrong cleaning your icon.")
}
}
}
Kirigami.Action {
iconSource: "gtk-convert"
text: "Convert Icon Colors to Classes"
onTriggered: {
if (manipulator.classIcon(imageSource)) {
root.showPassiveNotification("Your icon has been converted. You should inject stylesheets if they have not already been injected..")
} else {
root.showPassiveNotification("Something went wrong converting your icon.")
}
}
}
Kirigami.Action {
iconSource: "insert-image"
text: "Inject Stylesheets"
onTriggered: {
if (manipulator.injectStylesheet(imageSource)) {
root.showPassiveNotification("Stylesheets have been added to your icon.")
} else {
root.showPassiveNotification("Something went wrong adding stylesheets to your icon.")
}
}
}
},
Kirigami.Action {
iconSource: "palette-symbolic"
text: "View Palettes"
......
#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QUrl>
#include <QDesktopServices>
#include <QProcess>
#include <QDebug>
#include <QGuiApplication>
#include <QClipboard>
#include "iconmanipulator.h"
#include <QIcon>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
IconManipulator::IconManipulator(QObject *parent) : QObject(parent)
{
}
bool IconManipulator::tidyIcon(QString inputPath)
{
QString path;
if (inputPath.startsWith("file://")) {
path = inputPath.replace("file://", "");
}
QStringList args = { "--set-precision=8", "--enable-viewboxing", "--enable-comment-stripping", "--remove-descriptive-elements", "--create-groups", "--strip-xml-space", "--nindent=4", path };
QProcess *proc = new QProcess();
proc->start("scour", args);
if (proc->waitForFinished())
{
if (proc->exitCode() == 0)
{
QString output = proc->readAllStandardOutput();
QFile icon(path);
if (icon.open( QFile::WriteOnly | QFile::Truncate))
{
QTextStream iconStream(&icon);
iconStream << output;
return true;
} else
{
return false;
}
return true;
}
}
return false;
}
bool IconManipulator::classIcon(QString inputPath)
{
QString path;
if (inputPath.startsWith("file://")) {
path = inputPath.replace("file://", "");
}
QFile icon(path);
if (icon.open( QFile::ReadWrite ))
{
QByteArray iconData = icon.readAll();
QString text(iconData);
text.replace("fill=\"#232629\"", "class=\"ColorScheme-Text\" fill=\"currentColor\"");
text.replace("fill=\"#eff0f1\"", "class=\"ColorScheme-Background\" fill=\"currentColor\"");
text.replace("fill=\"#fcfcfc\"", "class=\"ColorScheme-ViewBackground\" fill=\"currentColor\"");
text.replace("fill=\"#3daee9\"", "class=\"ColorScheme-ButtonFocus\" fill=\"currentColor\"");
text.replace("fill=\"#27ae60\"", "class=\"ColorScheme-PositiveText\" fill=\"currentColor\"");
text.replace("fill=\"#f67400\"", "class=\"ColorScheme-NeutralText\" fill=\"currentColor\"");
text.replace("fill=\"#da4453\"", "class=\"ColorScheme-NegativeText\" fill=\"currentColor\"");
icon.seek(0);
icon.write(text.toUtf8());
icon.close();
return true;
}
return false;
}
bool IconManipulator::injectStylesheet(QString inputPath)
{
QString path;
if (inputPath.startsWith("file://")) {
path = inputPath.replace("file://", "");
}
QFile icon(path);
if (icon.open( QFile::ReadWrite ))
{
QByteArray iconData = icon.readAll();
QString text(iconData);
QRegularExpression re("<svg.*>");
QRegularExpressionMatch match = re.match(text);
if (!match.hasMatch()) {
return false;
}
int endOffset = match.capturedEnd();
text.insert(endOffset, "<style type=\"text/css\" id=\"current-color-scheme\">.ColorScheme-Text {color:#232629;}. ColorScheme-Background {color:#eff0f1;}. ColorScheme-ViewBackground {color:#fcfcfc;}. ColorScheme-ButtonFocus {color:#3daee9;}. ColorScheme-PositiveText {color:#27ae60;}. ColorScheme-NeutralText {color:#f67400;}. ColorScheme-NegativeText {color:#da4453;}. </style>");
icon.seek(0);
icon.write(text.toUtf8());
icon.close();
return true;
}
return false;
}
\ No newline at end of file
#include <QObject>
class IconManipulator : public QObject
{
Q_OBJECT
public:
explicit IconManipulator(QObject *parent = nullptr);
Q_INVOKABLE bool tidyIcon(QString inputPath);
Q_INVOKABLE bool classIcon(QString inputPath);
Q_INVOKABLE bool injectStylesheet(QString inputPath);
signals:
public slots:
};
\ No newline at end of file
......@@ -11,6 +11,7 @@
#include <QIcon>
#include <QtWebEngine>
#include "iconsetter.h"
#include "iconmanipulator.h"
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
......@@ -71,7 +72,8 @@ int main(int argc, char *argv[])
dir.mkdir(QDir::homePath() + "/.ikona");
}
qmlRegisterType<IconSetter>("me.appadeia.IconSetter", 1, 0, "IconSetter");
qmlRegisterType<IconSetter>("me.appadeia.Ikona", 1, 0, "IconSetter");
qmlRegisterType<IconManipulator>("me.appadeia.Ikona", 1, 0, "IconManipulator");
QIcon::setFallbackSearchPaths(QIcon::fallbackSearchPaths() << QDir::homePath() + "/.ikona");
qDebug() << QIcon::fallbackSearchPaths();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment