Adjust buttons to theme (Fix #89)

This commit is contained in:
Florian Märkl 2017-12-03 17:26:01 +01:00
parent f7cb84994a
commit 2417963329
11 changed files with 129 additions and 35 deletions

View File

@ -114,6 +114,9 @@ MainWindow::~MainWindow()
{ {
} }
#include <QSvgRenderer>
#include "utils/SvgIconEngine.h"
void MainWindow::initUI() void MainWindow::initUI()
{ {
ui->setupUi(this); ui->setupUi(this);
@ -134,10 +137,11 @@ void MainWindow::initUI()
spacer4->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); spacer4->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
spacer4->setMinimumSize(10, 10); spacer4->setMinimumSize(10, 10);
ui->mainToolBar->insertWidget(ui->actionForward, spacer4); ui->mainToolBar->insertWidget(ui->actionForward, spacer4);
ui->actionForward->setIcon(QIcon(new SvgIconEngine(QString(":/img/icons/arrow_right.svg"), palette().buttonText().color())));
// Popup menu on theme toolbar button // Popup menu on theme toolbar button
QToolButton *backButton = new QToolButton(this); QToolButton *backButton = new QToolButton(this);
backButton->setIcon(QIcon(":/img/icons/arrow_left.svg")); backButton->setIcon(QIcon(new SvgIconEngine(QString(":/img/icons/arrow_left.svg"), palette().buttonText().color())));
//backButton->setPopupMode(QToolButton::DelayedPopup); //backButton->setPopupMode(QToolButton::DelayedPopup);
ui->mainToolBar->insertWidget(ui->actionForward, backButton); ui->mainToolBar->insertWidget(ui->actionForward, backButton);
connect(backButton, SIGNAL(clicked()), this, SLOT(backButton_clicked())); connect(backButton, SIGNAL(clicked()), this, SLOT(backButton_clicked()));

View File

@ -277,20 +277,19 @@ border-top: 0px;
</property> </property>
<property name="styleSheet"> <property name="styleSheet">
<string notr="true">QToolButton { /* all types of tool button */ <string notr="true">QToolButton { /* all types of tool button */
border: 0px solid rgb(255, 255, 255);
border-radius: 6px;
margin-bottom: 1px; margin-bottom: 1px;
margin-top: 1px; margin-top: 1px;
margin-left: 2px; margin-left: 2px;
margin-right: 2px; margin-right: 2px;
background-color: rgb(255, 255, 255);
padding: 2px; padding: 2px;
padding-left: 7px; padding-left: 7px;
padding-right: 7px; padding-right: 7px;
color: rgb(255, 0, 0); background-color: palette(light);
border-radius: 6px;
} }
QToolButton .svg-icon path {
fill: #ff0000; QToolButton:pressed {
background-color: palette(dark);
}</string> }</string>
</property> </property>
<property name="movable"> <property name="movable">

View File

@ -78,7 +78,8 @@ SOURCES += \
utils/Configuration.cpp \ utils/Configuration.cpp \
utils/Colors.cpp \ utils/Colors.cpp \
dialogs/SaveProjectDialog.cpp \ dialogs/SaveProjectDialog.cpp \
utils/TempConfig.cpp utils/TempConfig.cpp \
utils/SvgIconEngine.cpp
HEADERS += \ HEADERS += \
cutter.h \ cutter.h \
@ -127,7 +128,8 @@ HEADERS += \
utils/Configuration.h \ utils/Configuration.h \
utils/Colors.h \ utils/Colors.h \
dialogs/SaveProjectDialog.h \ dialogs/SaveProjectDialog.h \
utils/TempConfig.h utils/TempConfig.h \
utils/SvgIconEngine.h
FORMS += \ FORMS += \
dialogs/AboutDialog.ui \ dialogs/AboutDialog.ui \

View File

@ -58,8 +58,6 @@ static QString formatBytecount(const long bytecount)
return stream.readAll(); return stream.readAll();
} }
#include "utils/Helpers.h"
NewFileDialog::NewFileDialog(QWidget *parent) : NewFileDialog::NewFileDialog(QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::NewFileDialog) ui(new Ui::NewFileDialog)
@ -69,8 +67,8 @@ NewFileDialog::NewFileDialog(QWidget *parent) :
ui->recentsListWidget->addAction(ui->actionRemove_item); ui->recentsListWidget->addAction(ui->actionRemove_item);
ui->recentsListWidget->addAction(ui->actionClear_all); ui->recentsListWidget->addAction(ui->actionClear_all);
QColor logoColor = (palette().window().color().value() < 127) ? QColor(255, 255, 255) : QColor(0, 0, 0); QString logoFile = (palette().window().color().value() < 127) ? ":/img/cutter_white.svg" : ":/img/cutter.svg";
ui->logoSvgWidget->load(qhelpers::applyColorToSvg(":/img/cutter.svg", logoColor)); ui->logoSvgWidget->load(logoFile);
fillRecentFilesList(); fillRecentFilesList();
bool projectsExist = fillProjectsList(); bool projectsExist = fillProjectsList();

View File

@ -25,8 +25,8 @@ OptionsDialog::OptionsDialog(MainWindow *main):
ui->progressBar->setVisible(0); ui->progressBar->setVisible(0);
ui->statusLabel->setVisible(0); ui->statusLabel->setVisible(0);
QColor logoColor = (palette().window().color().value() < 127) ? QColor(255, 255, 255) : QColor(0, 0, 0); QString logoFile = (palette().window().color().value() < 127) ? ":/img/cutter_white.svg" : ":/img/cutter.svg";
ui->logoSvgWidget->load(qhelpers::applyColorToSvg(":/img/cutter.svg", logoColor)); ui->logoSvgWidget->load(logoFile);
ui->analSlider->setValue(defaultAnalLevel); ui->analSlider->setValue(defaultAnalLevel);

View File

@ -127,17 +127,14 @@ namespace qhelpers
/ fontMetrics.lineSpacing(); / fontMetrics.lineSpacing();
} }
QByteArray applyColorToSvg(const QByteArray &data, QColor color)
QByteArray applyColorToSvg(const QString &filename, QColor color)
{ {
static QRegularExpression styleRegExp("(?:style=\".*fill:(.*?);.*?\")|(?:fill=\"(.*?)\")"); static const QRegularExpression styleRegExp("(?:style=\".*fill:(.*?);.*?\")|(?:fill=\"(.*?)\")");
QString replaceStr = QString("#%1").arg(color.rgb() & 0xffffff, 6, 16, QLatin1Char('0')); QString replaceStr = QString("#%1").arg(color.rgb() & 0xffffff, 6, 16, QLatin1Char('0'));
int replaceStrLen = replaceStr.length(); int replaceStrLen = replaceStr.length();
QFile file(filename); QString xml = QString::fromUtf8(data);
file.open(QIODevice::ReadOnly);
QString xml = QString::fromUtf8(file.readAll());
int offset = 0; int offset = 0;
while(true) while(true)
@ -156,4 +153,12 @@ namespace qhelpers
return xml.toUtf8(); return xml.toUtf8();
} }
QByteArray applyColorToSvg(const QString &filename, QColor color)
{
QFile file(filename);
file.open(QIODevice::ReadOnly);
return applyColorToSvg(file.readAll(), color);
}
} // end namespace } // end namespace

View File

@ -42,6 +42,7 @@ namespace qhelpers
int getMaxFullyDisplayedLines(QPlainTextEdit *plainTextEdit); int getMaxFullyDisplayedLines(QPlainTextEdit *plainTextEdit);
QByteArray applyColorToSvg(const QByteArray &data, QColor color);
QByteArray applyColorToSvg(const QString &filename, QColor color); QByteArray applyColorToSvg(const QString &filename, QColor color);
} }

View File

@ -0,0 +1,54 @@
#include "SvgIconEngine.h"
#include <QSvgRenderer>
#include <QPainter>
#include <QFile>
#include "Helpers.h"
SvgIconEngine::SvgIconEngine(const QString &filename)
{
QFile file(filename);
file.open(QFile::ReadOnly);
this->svgData = file.readAll();
}
SvgIconEngine::SvgIconEngine(const QByteArray &svgData)
{
this->svgData = svgData;
}
SvgIconEngine::SvgIconEngine(const QString &filename, QColor tintColor) : SvgIconEngine(filename)
{
this->svgData = qhelpers::applyColorToSvg(svgData, tintColor);
}
SvgIconEngine::SvgIconEngine(const QByteArray &svgData, QColor tintColor)
{
this->svgData = qhelpers::applyColorToSvg(svgData, tintColor);
}
void SvgIconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state)
{
QSvgRenderer renderer(svgData);
renderer.render(painter, rect);
}
QIconEngine *SvgIconEngine::clone() const
{
return new SvgIconEngine(*this);
}
QPixmap SvgIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
{
QImage img(size, QImage::Format_ARGB32);
img.fill(qRgba(0, 0, 0, 0));
QPixmap pix = QPixmap::fromImage(img, Qt::NoFormatConversion);
{
QPainter painter(&pix);
QRect r(QPoint(0.0, 0.0), size);
this->paint(&painter, r, mode, state);
}
return pix;
}

25
src/utils/SvgIconEngine.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef SVGICONENGINE_H
#define SVGICONENGINE_H
#include <QIconEngine>
class SvgIconEngine: public QIconEngine
{
private:
QByteArray svgData;
public:
explicit SvgIconEngine(const QString &filename);
explicit SvgIconEngine(const QByteArray &svgData);
SvgIconEngine(const QString &filename, QColor tintColor);
SvgIconEngine(const QByteArray &svgData, QColor tintColor);
void paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state) override;
QIconEngine *clone() const override;
QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) override;
};
#endif //SVGICONENGINE_H

View File

@ -8,6 +8,7 @@
#include "ConsoleWidget.h" #include "ConsoleWidget.h"
#include "ui_ConsoleWidget.h" #include "ui_ConsoleWidget.h"
#include "utils/Helpers.h" #include "utils/Helpers.h"
#include "utils/SvgIconEngine.h"
// TODO: Find a way to get to this without copying it here // TODO: Find a way to get to this without copying it here
@ -89,8 +90,6 @@ static bool isForbidden(const QString &input)
return false; return false;
} }
ConsoleWidget::ConsoleWidget(QWidget *parent) : ConsoleWidget::ConsoleWidget(QWidget *parent) :
QWidget(parent), QWidget(parent),
ui(new Ui::ConsoleWidget), ui(new Ui::ConsoleWidget),
@ -103,6 +102,8 @@ ConsoleWidget::ConsoleWidget(QWidget *parent) :
// Adjust console lineedit // Adjust console lineedit
ui->inputLineEdit->setTextMargins(10, 0, 0, 0); ui->inputLineEdit->setTextMargins(10, 0, 0, 0);
ui->execButton->setIcon(QIcon(new SvgIconEngine(QString(":/img/icons/arrow_right.svg"), palette().buttonText().color())));
setupFont(); setupFont();
// Adjust text margins of consoleOutputTextEdit // Adjust text margins of consoleOutputTextEdit

View File

@ -105,23 +105,28 @@
<string>Execute command</string> <string>Execute command</string>
</property> </property>
<property name="styleSheet"> <property name="styleSheet">
<string notr="true">/* <string notr="true">
border: none;
padding: 2px;
*/
QToolButton { /* all types of tool button */ QToolButton { /* all types of tool button */
border: 0px solid rgb(255, 255, 255);
border-radius: 6px;
border-top: 2px solid rgb(255, 255, 255);
border-bottom: 2px solid rgb(255, 255, 255);
border-left: 2px solid rgb(255, 255, 255);
border-right: 2px solid rgb(255, 255, 255);
margin-bottom: 1px; margin-bottom: 1px;
margin-top: 1px; margin-top: 1px;
padding-left: 8px; padding-left: 8px;
padding-right: 5px; padding-right: 5px;
background-color: rgb(255, 255, 255); padding-top: 1px;
padding-bottom: 1px;
background-color: palette(light);
border-radius: 6px;
border-top: 2px solid palette(light);
border-bottom: 2px solid palette(light);
border-left: 2px solid palette(light);
border-right: 2px solid palette(light);
}
QToolButton:pressed {
background-color: palette(dark);
border-top: 2px solid palette(dark);
border-bottom: 2px solid palette(dark);
border-left: 2px solid palette(dark);
border-right: 2px solid palette(dark);
}</string> }</string>
</property> </property>
<property name="text"> <property name="text">