File: utils.h

package info (click to toggle)
matrix-mirage 0.6.4~dfsg%2B~hsluv1.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 7,624 kB
  • sloc: ansic: 38,559; python: 4,702; cpp: 344; sh: 242; makefile: 29
file content (81 lines) | stat: -rw-r--r-- 1,777 bytes parent folder | download
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
// SPDX-License-Identifier: LGPL-3.0-or-later

// The Utils class exposes various useful functions for QML that aren't
// provided by the `Qt` object.

#ifndef UTILS_H
#define UTILS_H

#include <QColor>
#include <QLocale>
#include <QObject>
#include <QUuid>

#ifdef Q_OS_LINUX
    #ifndef NO_X11
    #define USE_LINUX_AUTOAWAY
    #include <X11/extensions/scrnsaver.h>
    #endif
#endif

#include "../submodules/hsluv-c/src/hsluv.h"


class Utils : public QObject {
    Q_OBJECT

public:
    Utils() {};

public slots:
    QString formattedBytes(qint64 bytes, int precision = 2) {
        return this->appLocale.formattedDataSize(
            bytes, precision, QLocale::DataSizeTraditionalFormat
        );
    }

    QString uuid() const {
        return QUuid::createUuid().toString(QUuid::WithoutBraces);
    }

    QColor hsluv(qreal hue, qreal sat, qreal luv, qreal alpha = 1.0) const {
        double red, green, blue;
        hsluv2rgb(hue, sat, luv, &red, &green, &blue);

        return QColor::fromRgbF(
            qMax(0.0, qMin(1.0, red)),
            qMax(0.0, qMin(1.0, green)),
            qMax(0.0, qMin(1.0, blue)),
            qMax(0.0, qMin(1.0, alpha))
        );
    }

    int idleMilliseconds() const {
        #ifdef Q_OS_DARWIN
        return -1;

        #elif defined(USE_LINUX_AUTOAWAY)
        Display *display = XOpenDisplay(NULL);
        if (! display) return -1;

        XScreenSaverInfo *info = XScreenSaverAllocInfo();
        XScreenSaverQueryInfo(display, DefaultRootWindow(display), info);
        const int idle = info->idle;

        XCloseDisplay(display);
        return idle;

        #elif defined(Q_OS_WINDOWS)
        return -1;

        #else
        return -1;
        #endif
    }

private:
    QLocale appLocale;
};


#endif