Skip to content

Commit

Permalink
Embed svg into code by hand
Browse files Browse the repository at this point in the history
  • Loading branch information
LordTermor committed Nov 19, 2021
1 parent e0c028a commit d2ffdbf
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 16 deletions.
18 changes: 4 additions & 14 deletions src/application/ToggleSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@
#include <QTimer>

#include "ToggleSwitch.h"
#include "ToggleSwitch.svg.h"

static inline QByteArray readSvg(const QString& name){
QFile svgFile(name);
if(!svgFile.open(QFile::ReadOnly)){
return {};
}
return svgFile.readAll();
}

QByteArray ToggleSwitch::s_toggleOffSvgContent;
QByteArray ToggleSwitch::s_toggleOnSvgContent;
const QByteArray ToggleSwitch::s_toggleOffSvgContent = ToggleSwitchSVG::s_toggledOffContent;
const QByteArray ToggleSwitch::s_toggleOnSvgContent = ToggleSwitchSVG::s_toggledOnContent;
const int ToggleSwitch::s_colorPosInToggleOn = ToggleSwitch::s_toggleOnSvgContent.indexOf("#1a73e8");

ToggleSwitch::ToggleSwitch(QWidget *parent) : QCheckBox(parent){
Expand All @@ -30,15 +23,12 @@ ToggleSwitch::ToggleSwitch(QWidget *parent) : QCheckBox(parent){

void ToggleSwitch::paintEvent(QPaintEvent *event){
QPainter p(this);
s_toggleOffSvgContent = readSvg(QStringLiteral(":/toggle/toggle-off.svg"));
s_toggleOnSvgContent = readSvg(QStringLiteral(":/toggle/toggle-on.svg"));


if(isChecked()){
auto accent = palette().highlight().color();
m_toggleOnSvgContentColored = m_toggleOnSvgContentColored.replace(s_colorPosInToggleOn, 7, accent.name().toLatin1());

m_svgr.load(s_toggleOnSvgContent);
m_svgr.load(m_toggleOnSvgContentColored);
} else {
m_svgr.load(s_toggleOffSvgContent);
}
Expand Down
4 changes: 2 additions & 2 deletions src/application/ToggleSwitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class ToggleSwitch : public QCheckBox {
private:
QSvgRenderer m_svgr;

static QByteArray s_toggleOnSvgContent;
static QByteArray s_toggleOffSvgContent;
static const QByteArray s_toggleOnSvgContent;
static const QByteArray s_toggleOffSvgContent;
static const int s_colorPosInToggleOn;

QByteArray m_toggleOnSvgContentColored;
Expand Down
76 changes: 76 additions & 0 deletions src/application/ToggleSwitch.svg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#pragma once

//Embed SVG files into code as debian packages does weird things when svgs are included as qrc
namespace ToggleSwitchSVG {
static constexpr char s_toggledOnContent[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
"<svg\n"
" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n"
" xmlns:cc=\"http://creativecommons.org/ns#\"\n"
" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n"
" xmlns:svg=\"http://www.w3.org/2000/svg\"\n"
" xmlns=\"http://www.w3.org/2000/svg\"\n"
" xmlns:sodipodi=\"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd\"\n"
" xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\"\n"
" inkscape:version=\"1.0rc1 (1.0rc1+100)\"\n"
" sodipodi:docname=\"toggle-on.svg\"\n"
" id=\"svg6\"\n"
" version=\"1.1\"\n"
" viewBox=\"0 0 40 20\"\n"
" height=\"20\"\n"
" width=\"40\">\n"
" <metadata\n"
" id=\"metadata12\">\n"
" <rdf:RDF>\n"
" <cc:Work\n"
" rdf:about=\"\">\n"
" <dc:format>image/svg+xml</dc:format>\n"
" <dc:type\n"
" rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\" />\n"
" </cc:Work>\n"
" </rdf:RDF>\n"
" </metadata>\n"
" <defs\n"
" id=\"defs10\" />\n"
" <sodipodi:namedview\n"
" inkscape:current-layer=\"svg6\"\n"
" inkscape:window-maximized=\"1\"\n"
" inkscape:window-y=\"28\"\n"
" inkscape:window-x=\"65\"\n"
" inkscape:cy=\"10\"\n"
" inkscape:cx=\"20.062112\"\n"
" inkscape:zoom=\"32.2\"\n"
" showgrid=\"false\"\n"
" id=\"namedview8\"\n"
" inkscape:window-height=\"1020\"\n"
" inkscape:window-width=\"1855\"\n"
" inkscape:pageshadow=\"2\"\n"
" inkscape:pageopacity=\"0\"\n"
" guidetolerance=\"10\"\n"
" gridtolerance=\"10\"\n"
" objecttolerance=\"10\"\n"
" borderopacity=\"1\"\n"
" bordercolor=\"#666666\"\n"
" pagecolor=\"#ffffff\" />\n"
" <rect\n"
" style=\"fill:#0068bf;fill-opacity:1\"\n"
" id=\"rect2\"\n"
" fill=\"#1a73e8\"\n"
" ry=\"8\"\n"
" height=\"16\"\n"
" width=\"36\"\n"
" y=\"2\"\n"
" x=\"2\" />\n"
" <circle\n"
" id=\"circle4\"\n"
" fill=\"#ffffff\"\n"
" r=\"6\"\n"
" cy=\"10\"\n"
" cx=\"30\" />\n"
"</svg>\n";
static constexpr char s_toggledOffContent[] =
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"40\" height=\"20\" viewBox=\"0 0 40 20\">\n"
" <rect x=\"2\" y=\"2\" width=\"36\" height=\"16\" ry=\"8\" fill=\"#000000\" fill-opacity=\".26\"/>\n"
" <circle cx=\"10\" cy=\"10\" r=\"6\" fill=\"#ffffff\"/>\n"
"</svg>";
}

0 comments on commit d2ffdbf

Please sign in to comment.