summaryrefslogtreecommitdiffstats
path: root/helper.cpp
blob: 5fa3a3b09038d8dcaeb19e596f76667b2d5ed4dc (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
#include <QPainter>
#include <QAction>
#include <QLabel>
#include <QApplication>

#include <taglib/fileref.h>
#include <taglib/audioproperties.h>

#include "helper.h"

namespace Helper {
    QIcon iconFromQChar(const QChar &c, int pixelSize){
        QPixmap pm(QSize(64,64));
        QPainter p(&pm);
        QFont f = p.font();
        f.setPixelSize(pixelSize);
        p.setFont(f);
        p.fillRect(pm.rect(), qApp->palette().color(QPalette::Window));
        p.setBrush(QBrush(Qt::black));
        p.drawText(pm.rect(), Qt::AlignCenter, c);
        return QIcon(pm);
    }

    QAction* createSeparator(QObject *parent){
        QAction *a = new QAction(parent);
        a->setSeparator(true);
        return a;
    }

    quint64 lengthInSeconds(const QString &file){
        TagLib::FileRef f(QString(file).toUtf8());
        if(f.isNull()){
            return 0;
        }
        TagLib::AudioProperties *props = f.audioProperties();
        return props->lengthInSeconds();
    }

    QColor colorFromLabel(QLabel *l){
        QColor retval;
        const QPixmap *pm = l->pixmap();
        if(pm){
            QImage img = pm->toImage();
            retval = img.pixelColor(0, 0);
        }
        return retval;
    }

    void fillLabel(QLabel *l, QColor c){
        QPixmap newPm(l->sizeHint());
        newPm.fill(c);
        l->setPixmap(newPm);
    }
}