JustAudio/src/svg_icon.cpp
Maurice ONeal 79ea070b75 not sure what was done to this project to be honest.
committing the changes anyway.
2022-07-08 18:37:53 -04:00

127 lines
3.0 KiB
C++

#include "svg_icon.h"
// This file is part of JustAudio.
// JustAudio is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// JustAudio is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with BashWire under the GPL.txt file. If not, see
// <http://www.gnu.org/licenses/>.
SVGIcon::SVGIcon(const QString &name, const QString &desc, bool toggle, QWidget *parent) : QToolButton(parent)
{
auto action = new QAction(this);
setObjectName(name);
setMinimumHeight(32);
setMinimumWidth(32);
setToolTip(desc);
setToolButtonStyle(Qt::ToolButtonIconOnly);
setPopupMode(QToolButton::InstantPopup);
setCursor(Qt::PointingHandCursor);
action->setCheckable(toggle);
connect(action, &QAction::enabledChanged, this, &SVGIcon::enabledChanged);
connect(action, &QAction::triggered, this, &SVGIcon::clickedInternal);
setDefaultAction(action);
setActiveAppearance();
}
void SVGIcon::setInactiveAppearance()
{
loadSvgFile(":/svg/" + objectName() + "_inactive.svg");
}
void SVGIcon::setActiveAppearance()
{
if (defaultAction()->isCheckable())
{
loadSvgFile(":/svg/" + objectName() + "_active.svg");
}
else
{
loadSvgFile(":/svg/" + objectName() + ".svg");
}
}
void SVGIcon::enabledChanged(bool state)
{
if (defaultAction()->isCheckable())
{
if (state) setActiveAppearance();
else setInactiveAppearance();
}
else
{
setHidden(!state);
}
}
void SVGIcon::clickedInternal()
{
if (defaultAction()->isCheckable())
{
if (defaultAction()->isChecked()) setActiveAppearance();
else setInactiveAppearance();
}
}
void SVGIcon::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QSvgRenderer svg(localData, this);
svg.render(&painter);
}
void SVGIcon::loadSvgFile(const QString &path)
{
fileCopy(path);
update();
}
void SVGIcon::fileCopy(const QString &path)
{
QFile file(path, this);
if (file.open(QFile::ReadOnly))
{
auto data = file.readAll();
QXmlStreamReader xml(data);
while (!xml.atEnd())
{
xml.readNext();
if (xml.hasError()) break;
}
if (xml.hasError())
{
qDebug() << tr("Invalid svg image (xml parse error)");
}
else
{
localData = data;
}
}
else
{
qDebug() << tr("Unable to read file: ") + path;
}
file.close();
}