iconmanipulator.cpp 5.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#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)
{

Carson Black's avatar
Carson Black committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
}
bool IconManipulator::prepMono(QString inputPath)
{
    QString path;
    if (inputPath.startsWith("file://")) {
        path = inputPath.replace("file://", "");
    } else {
        path = inputPath;
    }
    QFile file(path);

    if (QFile::exists("/tmp/ikonalight.svg"))
        QFile::remove("/tmp/ikonalight.svg");

    if (QFile::exists("/tmp/ikonadark.svg"))
        QFile::remove("/tmp/ikonadark.svg");

    file.copy("/tmp/ikonalight.svg");
    file.copy("/tmp/ikonadark.svg");

    this->injectStylesheet("file:///tmp/ikonalight.svg");
    this->injectStylesheet("file:///tmp/ikonadark.svg");    

    this->classIcon("file:///tmp/ikonalight.svg");
    this->classIcon("file:///tmp/ikonadark.svg");

    this->toDark("file:///tmp/ikonalight.svg");
    this->toLight("file:///tmp/ikonadark.svg");

    return true;
48 49 50 51 52 53
}
bool IconManipulator::tidyIcon(QString inputPath)
{
    QString path;
    if (inputPath.startsWith("file://")) {
        path = inputPath.replace("file://", "");
Carson Black's avatar
Carson Black committed
54 55
    } else {
        path = inputPath;
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    }
    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://", "");
Carson Black's avatar
Carson Black committed
85 86
    } else {
        path = inputPath;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    }
    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;
}
110 111 112 113 114
bool IconManipulator::toDark(QString inputPath)
{
    QString path;
    if (inputPath.startsWith("file://")) {
        path = inputPath.replace("file://", "");
Carson Black's avatar
Carson Black committed
115 116
    } else {
        path = inputPath;
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    }
    QFile icon(path);
    if (icon.open( QFile::ReadWrite ))
    {
        QByteArray iconData = icon.readAll();
        QString text(iconData);

        text.replace("fill=\"#232629\"", "fill=\"#eff0f1\"");
        text.replace("fill=\"#eff0f1\"", "fill=\"#31363b\"");
        text.replace("fill=\"#fcfcfc\"", "fill=\"#232629\"");
        text.replace("color:#232629", "color:#eff0f1");
        text.replace("color:#eff0f1", "color:#31363b");
        text.replace("color:#fcfcfc", "color:#232629");

        icon.seek(0);
        icon.write(text.toUtf8());

        icon.close();
        return true;
    }
    return false;
}
bool IconManipulator::toLight(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=\"#eff0f1\"", "fill=\"#232629\"");
        text.replace("fill=\"#31363b\"", "fill=\"#eff0f1\"");
        text.replace("fill=\"#232629\"", "fill=\"#fcfcfc\"");
        text.replace("color:#eff0f1", "color:#232629");
        text.replace("color:#31363b", "color:#eff0f1");
        text.replace("color:#232629", "color:#fcfcfc");

        icon.seek(0);
        icon.write(text.toUtf8());

        icon.close();
        return true;
    }
    return false;
}
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
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();
        
Carson Black's avatar
Carson Black committed
187
        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>");
188 189 190 191 192 193 194 195 196

        icon.seek(0);
        icon.write(text.toUtf8());

        icon.close();
        return true;
    }
    return false;
}