aboutsummaryrefslogtreecommitdiff
path: root/src/util.h
blob: 6882854cc0c42e28d66d8c1634244b40b24e7ae1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#ifndef SMOLBOTE_UTIL_H
#define SMOLBOTE_UTIL_H

#include "icons.h"
#include <QIcon>
#include <QPainter>
#include <QStringList>
#include <QStyle>
#include <QSvgRenderer>

namespace Util {

const QStringList files(const QString &location, const QStringList &nameFilters = QStringList());

template <QStyle::StandardPixmap id>
inline QIcon icon()
{
    using namespace std::literals;

    constexpr auto data = icons::get([] {
        switch(id) {
        case QStyle::SP_ArrowBack:
            return "arrow-left.svg"sv;
        case QStyle::SP_ArrowForward:
            return "arrow-right.svg"sv;
        case QStyle::SP_BrowserStop:
            return "circle-x.svg"sv;
        case QStyle::SP_BrowserReload:
            return "refresh.svg"sv;
        case QStyle::SP_DirHomeIcon:
            return "home.svg"sv;
        default:
            return "__unknown__"sv;
        }
    });

    QIcon m;

    // This is a horrible hack that will one day be hopefully fixed:
    // When rendering an svg, you can't actually pick a stroke color through
    // QPainter::setBrush or QPainter::setPen. So instead, replace the stroke
    // color in the data (stroke="currentColor") and then rerender it.

    QSvgRenderer renderer;
    QByteArray arr(data.data(), data.size());
    for(const auto mode : { QIcon::Normal /*, QIcon::Disabled, QIcon::Active, QIcon::Selected*/ }) {
        for(const auto toggle : { QIcon::On, QIcon::Off }) {
            renderer.load(arr.replace("currentColor", (toggle == QIcon::On ? "black" : "gray")));

            QPixmap pm(renderer.defaultSize());
            pm.fill(Qt::transparent);

            QPainter painter(&pm);
            painter.setBrush(Qt::green);
            painter.setPen(Qt::green);
            renderer.render(&painter, pm.rect());

            m.addPixmap(pm, mode, toggle);
        }
    }
    return m;
}
} // namespace Util

#endif // SMOLBOTE_UTIL_H