File size: 2,417 Bytes
034d0a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Copyright 2018 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <QFileDialog>
#include <QImageReader>
#include <QMessageBox>
#include <QThread>
#include "citra_qt/camera/still_image_camera.h"
#include "common/logging/log.h"

namespace Camera {

StillImageCamera::StillImageCamera(QImage image_, const Service::CAM::Flip& flip)
    : QtCameraInterface(flip), image(std::move(image_)) {}

StillImageCamera::~StillImageCamera() {
    StillImageCameraFactory::last_path.clear();
}

void StillImageCamera::StartCapture() {}

void StillImageCamera::StopCapture() {}

QImage StillImageCamera::QtReceiveFrame() {
    return image;
}

bool StillImageCamera::IsPreviewAvailable() {
    return !image.isNull();
}

std::string StillImageCameraFactory::last_path;

const std::string StillImageCameraFactory::GetFilePath() const {
    if (!last_path.empty()) {
        return last_path;
    }
    QList<QByteArray> types = QImageReader::supportedImageFormats();
    QStringList temp_filters;
    for (QByteArray type : types) {
        temp_filters << QStringLiteral("*.%1").arg(QString::fromUtf8(type));
    }

    QString filter =
        QObject::tr("Supported image files (%1)").arg(temp_filters.join(QLatin1Char{' '}));
    last_path =
        QFileDialog::getOpenFileName(nullptr, QObject::tr("Open File"), QStringLiteral("."), filter)
            .toStdString();
    return last_path;
}

std::unique_ptr<CameraInterface> StillImageCameraFactory::Create(const std::string& config,
                                                                 const Service::CAM::Flip& flip) {
    std::string real_config = config;
    if (config.empty()) {
        // call GetFilePath() in UI thread (note: StillImageCameraFactory itself is initialized in
        // UI thread, so we can just pass in "this" here)
        if (thread() == QThread::currentThread()) {
            real_config = GetFilePath();
        } else {
            QMetaObject::invokeMethod(this, "GetFilePath", Qt::BlockingQueuedConnection,
                                      Q_RETURN_ARG(std::string, real_config));
        }
    }
    QImage image(QString::fromStdString(real_config));
    if (image.isNull()) {
        LOG_ERROR(Service_CAM, "Couldn't load image \"{}\"", real_config.c_str());
    }
    return std::make_unique<StillImageCamera>(image, flip);
}

} // namespace Camera