summaryrefslogtreecommitdiffstats
path: root/viewer.cpp
blob: d0077f7ade02fe042f56453a1e96a4f5abac4954 (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
#include <QLabel>
#include <QHBoxLayout>
#include <QDesktopWidget>
#include <QApplication>
#include <QFileInfo>
#include <QMimeDatabase>
#include <QDir>
#include <QWheelEvent>

#include "helper.h"
#include "viewer.h"

Viewer::Viewer(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f), mCurIndex(-1) {
    QDesktopWidget *dw = qApp->desktop();
    setAutoFillBackground(true);
    setBackgroundRole(QPalette::AlternateBase);
    mLabel = new QLabel;
    mLabel->setAlignment(Qt::AlignCenter);
    mMaxSize = dw->size();
    //90 is an arbitrary value, but it works :)
    mMaxSize.setHeight(mMaxSize.height() - 90);
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addWidget(mLabel);
    setLayout(mainLayout);
}

void Viewer::setFile(const QString &file, bool allFiles){
    mFiles.clear();
    mCurIndex = -1;
    QFileInfo fi(file);
    QMimeDatabase db;
    QMimeType mt = db.mimeTypeForFile(fi);
    if(mt.name().startsWith("image")){
        mFiles << fi.absoluteFilePath();
    }
    if(allFiles){
        for(auto curFi : fi.dir().entryInfoList()){
            if(!mFiles.contains(curFi.absoluteFilePath())){
                QMimeType mt = db.mimeTypeForFile(curFi);
                if(mt.name().startsWith("image")){
                    mFiles << curFi.absoluteFilePath();
                }
            }
        }
    }
    if(!mFiles.isEmpty()){
        mCurIndex = 0;
        displayFile(mCurIndex);
    }
}

void Viewer::setFiles(const QStringList &files){
    mFiles.clear();
    mFiles = files;
    mCurIndex = 0;
    displayFile(mCurIndex);
}

void Viewer::preview(const QString &file){
    mFiles.clear();
    mCurIndex = -1;
    QPixmap pm = Helper::preview(file);
    mLabel->setPixmap(pm);
    QString winTitle = QString(tr("%1 Viewer: [Preview %2]")).arg(qApp->applicationName()).arg(file);
    setWindowTitle(winTitle);
}

void Viewer::wheelEvent(QWheelEvent *event){
    if(mCurIndex == -1){
        return;
    }
    QPoint numDeg = event->angleDelta() / 8;
    if(numDeg.y() < 0){ //this is scrolling down -> next!
        if(mCurIndex + 1 >= mFiles.count()){
            mCurIndex = 0;
        }else{
            ++mCurIndex;
        }
    }else if(numDeg.y() > 0){
        if(mCurIndex - 1 < 0){
            mCurIndex = mFiles.count() - 1;
        }else{
            --mCurIndex;
        }
    }
    displayFile(mCurIndex);
    event->accept();
}

void Viewer::displayFile(int index){
    if(index < 0){
        return;
    }
    if(index == 0){
        setBackgroundRole(QPalette::Base);
    }else{
        setBackgroundRole(QPalette::AlternateBase);
    }
    QPixmap pm(mFiles.at(mCurIndex));
    pm = pm.scaled(mMaxSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    mLabel->setPixmap(pm);
    QString winTitle = QString(tr("%1 Viewer [%2]")).arg(qApp->applicationName()).arg(mFiles.at(mCurIndex));
    setWindowTitle(winTitle);
}