summaryrefslogtreecommitdiff
path: root/src/libaudqt/audqt.cc
blob: a5d3e957ca04244d34fd477d90321fdfa61582a0 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 * util.cc
 * Copyright 2014 Ariadne Conill
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include <stdlib.h>

#include <QApplication>
#include <QDesktopWidget>
#include <QPushButton>
#include <QVBoxLayout>

#include <libaudcore/audstrings.h>
#include <libaudcore/i18n.h>
#include <libaudcore/runtime.h>

#include "libaudqt-internal.h"
#include "libaudqt.h"

namespace audqt
{

static int init_count;

static PixelSizes sizes_local;
static PixelMargins margins_local;

EXPORT const PixelSizes & sizes = sizes_local;
EXPORT const PixelMargins & margins = margins_local;

EXPORT void init()
{
    if (init_count++)
        return;

    static char app_name[] = "audacious";
    static int dummy_argc = 1;
    static char * dummy_argv[] = {app_name, nullptr};

    auto qapp = new QApplication(dummy_argc, dummy_argv);

    qapp->setAttribute(Qt::AA_UseHighDpiPixmaps);
#if QT_VERSION >= QT_VERSION_CHECK(5, 3, 0)
    qapp->setAttribute(Qt::AA_ForceRasterWidgets);
#endif
#if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
    qapp->setAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
#endif

    qapp->setApplicationName(_("Audacious"));
    if (qapp->windowIcon().isNull())
        qapp->setWindowIcon(audqt::get_icon(app_name));

    qapp->setQuitOnLastWindowClosed(false);

    auto desktop = qapp->desktop();
    sizes_local.OneInch =
        aud::max(96, (desktop->logicalDpiX() + desktop->logicalDpiY()) / 2);
    sizes_local.TwoPt = aud::rescale(2, 72, sizes_local.OneInch);
    sizes_local.FourPt = aud::rescale(4, 72, sizes_local.OneInch);
    sizes_local.EightPt = aud::rescale(8, 72, sizes_local.OneInch);

    margins_local.TwoPt =
        QMargins(sizes.TwoPt, sizes.TwoPt, sizes.TwoPt, sizes.TwoPt);
    margins_local.FourPt =
        QMargins(sizes.FourPt, sizes.FourPt, sizes.FourPt, sizes.FourPt);
    margins_local.EightPt =
        QMargins(sizes.EightPt, sizes.EightPt, sizes.EightPt, sizes.EightPt);

#ifdef Q_OS_MAC // Mac-specific font tweaks
    QApplication::setFont(QApplication::font("QSmallFont"), "QDialog");
    QApplication::setFont(QApplication::font("QSmallFont"), "QTreeView");
    QApplication::setFont(QApplication::font("QTipLabel"), "QStatusBar");
#endif

    log_init();
}

EXPORT void run() { qApp->exec(); }

EXPORT void quit() { qApp->quit(); }

EXPORT void cleanup()
{
    if (--init_count)
        return;

    aboutwindow_hide();
    eq_presets_hide();
    equalizer_hide();
    infopopup_hide_now();
    infowin_hide();
    log_inspector_hide();
    plugin_prefs_hide();
    prefswin_hide();
    queue_manager_hide();

    log_cleanup();

    delete qApp;
}

EXPORT QIcon get_icon(const char * name)
{
    auto icon = QIcon::fromTheme(name);

    if (icon.isNull())
        icon = QIcon(QString(":/") + name + ".svg");

    return icon;
}

EXPORT QGradientStops dark_bg_gradient(const QColor & base)
{
    constexpr int s[4] = {40, 28, 16, 24};

    QColor c[4];
    for (int i = 0; i < 4; i++)
        c[i] = QColor(s[i], s[i], s[i]);

    /* in a dark theme, try to match the tone of the base color */
    int v = base.value();
    if (v >= 10 && v < 80)
    {
        int r = base.red(), g = base.green(), b = base.blue();

        for (int i = 0; i < 4; i++)
        {
            c[i] = QColor(r * s[i] / v, g * s[i] / v, b * s[i] / v);
        }
    }

    return {{0, c[0]}, {0.45, c[1]}, {0.55, c[2]}, {1, c[3]}};
}

EXPORT QColor vis_bar_color(const QColor & hue, int bar, int n_bars)
{
    qreal h, s, v;
    hue.getHsvF(&h, &s, &v);

    if (s < 0.1) /* monochrome? use blue instead */
        h = 0.67;

    s = 1 - 0.9 * bar / (n_bars - 1);
    v = 0.75 + 0.25 * bar / (n_bars - 1);

    return QColor::fromHsvF(h, s, v);
}

EXPORT QHBoxLayout * make_hbox(QWidget * parent, int spacing)
{
    auto layout = new QHBoxLayout(parent);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(spacing);
    return layout;
}

EXPORT QVBoxLayout * make_vbox(QWidget * parent, int spacing)
{
    auto layout = new QVBoxLayout(parent);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(spacing);
    return layout;
}

EXPORT void enable_layout(QLayout * layout, bool enabled)
{
    int count = layout->count();
    for (int i = 0; i < count; i++)
    {
        auto item = layout->itemAt(i);
        if (QLayout * layout2 = item->layout())
            enable_layout(layout2, enabled);
        if (QWidget * widget = item->widget())
            widget->setEnabled(enabled);
    }
}

EXPORT void clear_layout(QLayout * layout)
{
    while (QLayoutItem * item = layout->takeAt(0))
    {
        if (QLayout * layout2 = item->layout())
            clear_layout(layout2);
        if (QWidget * widget = item->widget())
            delete widget;

        delete item;
    }
}

/* the goal is to force a window to come to the front on any Qt platform */
EXPORT void window_bring_to_front(QWidget * window)
{
    window->show();

    Qt::WindowStates state = window->windowState();

    state &= ~Qt::WindowMinimized;
    state |= Qt::WindowActive;

    window->setWindowState(state);
    window->raise();
    window->activateWindow();
}

EXPORT void simple_message(const char * title, const char * text)
{
    simple_message(title, text, QMessageBox::NoIcon);
}

EXPORT void simple_message(const char * title, const char * text,
                           QMessageBox::Icon icon)
{
    auto msgbox = new QMessageBox(icon, title, text, QMessageBox::Close);
    msgbox->button(QMessageBox::Close)->setText(translate_str(N_("_Close")));
    msgbox->setAttribute(Qt::WA_DeleteOnClose);
    msgbox->setTextInteractionFlags(Qt::TextSelectableByMouse);
    msgbox->show();
}

/* translate GTK+ accelerators and also handle dgettext() */
EXPORT QString translate_str(const char * str, const char * domain)
{
    /* handle null and empty strings */
    if (!str || !str[0])
        return QString(str);

    /* translate the GTK+ accelerator (_) into a Qt accelerator (&) */
    return QString(dgettext(domain, str)).replace('_', '&');
}

} // namespace audqt