File size: 11,793 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2020 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <unordered_map>
#include <QCheckBox>
#include <QStringList>
#include "citra_qt/dumping/option_set_dialog.h"
#include "common/logging/log.h"
#include "common/string_util.h"
#include "ui_option_set_dialog.h"

static const std::unordered_map<AVOptionType, const char*> TypeNameMap{{
    {AV_OPT_TYPE_BOOL, QT_TR_NOOP("boolean")},
    {AV_OPT_TYPE_FLAGS, QT_TR_NOOP("flags")},
    {AV_OPT_TYPE_DURATION, QT_TR_NOOP("duration")},
    {AV_OPT_TYPE_INT, QT_TR_NOOP("int")},
    {AV_OPT_TYPE_UINT64, QT_TR_NOOP("uint64")},
    {AV_OPT_TYPE_INT64, QT_TR_NOOP("int64")},
    {AV_OPT_TYPE_DOUBLE, QT_TR_NOOP("double")},
    {AV_OPT_TYPE_FLOAT, QT_TR_NOOP("float")},
    {AV_OPT_TYPE_RATIONAL, QT_TR_NOOP("rational")},
    {AV_OPT_TYPE_PIXEL_FMT, QT_TR_NOOP("pixel format")},
    {AV_OPT_TYPE_SAMPLE_FMT, QT_TR_NOOP("sample format")},
    {AV_OPT_TYPE_COLOR, QT_TR_NOOP("color")},
    {AV_OPT_TYPE_IMAGE_SIZE, QT_TR_NOOP("image size")},
    {AV_OPT_TYPE_STRING, QT_TR_NOOP("string")},
    {AV_OPT_TYPE_DICT, QT_TR_NOOP("dictionary")},
    {AV_OPT_TYPE_VIDEO_RATE, QT_TR_NOOP("video rate")},
    {AV_OPT_TYPE_CHANNEL_LAYOUT, QT_TR_NOOP("channel layout")},
}};

static const std::unordered_map<AVOptionType, const char*> TypeDescriptionMap{{
    {AV_OPT_TYPE_DURATION, QT_TR_NOOP("[&lt;hours (integer)>:][&lt;minutes (integer):]&lt;seconds "
                                      "(decimal)> e.g. 03:00.5 (3min 500ms)")},
    {AV_OPT_TYPE_RATIONAL, QT_TR_NOOP("&lt;num>/&lt;den>")},
    {AV_OPT_TYPE_COLOR, QT_TR_NOOP("0xRRGGBBAA")},
    {AV_OPT_TYPE_IMAGE_SIZE, QT_TR_NOOP("&lt;width>x&lt;height>, or preset values like 'vga'.")},
    {AV_OPT_TYPE_DICT,
     QT_TR_NOOP("Comma-splitted list of &lt;key>=&lt;value>. Do not put spaces.")},
    {AV_OPT_TYPE_VIDEO_RATE, QT_TR_NOOP("&lt;num>/&lt;den>, or preset values like 'pal'.")},
    {AV_OPT_TYPE_CHANNEL_LAYOUT, QT_TR_NOOP("Hexadecimal channel layout mask starting with '0x'.")},
}};

/// Get the preset values of an option. returns {display value, real value}
std::vector<std::pair<QString, QString>> GetPresetValues(const VideoDumper::OptionInfo& option) {
    switch (option.type) {
    case AV_OPT_TYPE_BOOL: {
        return {{QObject::tr("auto"), QStringLiteral("auto")},
                {QObject::tr("true"), QStringLiteral("true")},
                {QObject::tr("false"), QStringLiteral("false")}};
    }
    case AV_OPT_TYPE_PIXEL_FMT: {
        std::vector<std::pair<QString, QString>> out{{QObject::tr("none"), QStringLiteral("none")}};
        for (const auto& name : VideoDumper::GetPixelFormats()) {
            out.emplace_back(QString::fromUtf8(name), QString::fromUtf8(name));
        }
        return out;
    }
    case AV_OPT_TYPE_SAMPLE_FMT: {
        std::vector<std::pair<QString, QString>> out{{QObject::tr("none"), QStringLiteral("none")}};
        for (const auto& name : VideoDumper::GetSampleFormats()) {
            out.emplace_back(QString::fromUtf8(name), QString::fromUtf8(name));
        }
        return out;
    }
    case AV_OPT_TYPE_INT:
    case AV_OPT_TYPE_INT64:
    case AV_OPT_TYPE_UINT64: {
        std::vector<std::pair<QString, QString>> out;
        // Add in all named constants
        for (const auto& constant : option.named_constants) {
            out.emplace_back(QObject::tr("%1 (0x%2)")
                                 .arg(QString::fromStdString(constant.name))
                                 .arg(constant.value, 0, 16),
                             QString::fromStdString(constant.name));
        }
        return out;
    }
    default:
        return {};
    }
}

void OptionSetDialog::InitializeUI(const std::string& initial_value) {
    const QString type_name =
        TypeNameMap.count(option.type) ? tr(TypeNameMap.at(option.type)) : tr("unknown");
    ui->nameLabel->setText(tr("%1 &lt;%2> %3")
                               .arg(QString::fromStdString(option.name), type_name,
                                    QString::fromStdString(option.description)));
    if (TypeDescriptionMap.count(option.type)) {
        ui->formatLabel->setVisible(true);
        ui->formatLabel->setText(tr(TypeDescriptionMap.at(option.type)));
    }

    if (option.type == AV_OPT_TYPE_INT || option.type == AV_OPT_TYPE_INT64 ||
        option.type == AV_OPT_TYPE_UINT64 || option.type == AV_OPT_TYPE_FLOAT ||
        option.type == AV_OPT_TYPE_DOUBLE || option.type == AV_OPT_TYPE_DURATION ||
        option.type == AV_OPT_TYPE_RATIONAL) { // scalar types

        ui->formatLabel->setVisible(true);
        if (!ui->formatLabel->text().isEmpty()) {
            ui->formatLabel->text().append(QStringLiteral("\n"));
        }
        ui->formatLabel->setText(
            ui->formatLabel->text().append(tr("Range: %1 - %2").arg(option.min).arg(option.max)));
    }

    // Decide and initialize layout
    if (option.type == AV_OPT_TYPE_BOOL || option.type == AV_OPT_TYPE_PIXEL_FMT ||
        option.type == AV_OPT_TYPE_SAMPLE_FMT ||
        ((option.type == AV_OPT_TYPE_INT || option.type == AV_OPT_TYPE_INT64 ||
          option.type == AV_OPT_TYPE_UINT64) &&
         !option.named_constants.empty())) { // Use the combobox layout

        layout_type = 1;
        ui->comboBox->setVisible(true);
        ui->comboBoxHelpLabel->setVisible(true);

        QString real_initial_value = QString::fromStdString(initial_value);
        if (option.type == AV_OPT_TYPE_INT || option.type == AV_OPT_TYPE_INT64 ||
            option.type == AV_OPT_TYPE_UINT64) {

            // Get the name of the initial value
            try {
                s64 initial_value_integer = std::stoll(initial_value, nullptr, 0);
                for (const auto& constant : option.named_constants) {
                    if (constant.value == initial_value_integer) {
                        real_initial_value = QString::fromStdString(constant.name);
                        break;
                    }
                }
            } catch (...) {
                // Not convertible to integer, ignore
            }
        }

        bool found = false;
        for (const auto& [display, value] : GetPresetValues(option)) {
            ui->comboBox->addItem(display, value);
            if (value == real_initial_value) {
                found = true;
                ui->comboBox->setCurrentIndex(ui->comboBox->count() - 1);
            }
        }
        ui->comboBox->addItem(tr("custom"));

        if (!found) {
            ui->comboBox->setCurrentIndex(ui->comboBox->count() - 1);
            ui->lineEdit->setText(QString::fromStdString(initial_value));
        }

        UpdateUIDisplay();

        connect(ui->comboBox, &QComboBox::currentTextChanged, this,
                &OptionSetDialog::UpdateUIDisplay);
    } else if (option.type == AV_OPT_TYPE_FLAGS &&
               !option.named_constants.empty()) { // Use the check boxes layout

        layout_type = 2;

        for (const auto& constant : option.named_constants) {
            auto* checkBox = new QCheckBox(tr("%1 (0x%2) %3")
                                               .arg(QString::fromStdString(constant.name))
                                               .arg(constant.value, 0, 16)
                                               .arg(QString::fromStdString(constant.description)));
            checkBox->setProperty("value", static_cast<unsigned long long>(constant.value));
            checkBox->setProperty("name", QString::fromStdString(constant.name));
            ui->checkBoxLayout->addWidget(checkBox);
        }
        SetCheckBoxDefaults(initial_value);
    } else { // Use the line edit layout
        layout_type = 0;
        ui->lineEdit->setVisible(true);
        ui->lineEdit->setText(QString::fromStdString(initial_value));
    }

    adjustSize();
}

void OptionSetDialog::SetCheckBoxDefaults(const std::string& initial_value) {
    if (initial_value.size() >= 2 &&
        (initial_value.substr(0, 2) == "0x" || initial_value.substr(0, 2) == "0X")) {
        // This is a hex mask
        try {
            u64 value = std::stoull(initial_value, nullptr, 16);
            for (int i = 0; i < ui->checkBoxLayout->count(); ++i) {
                auto* checkBox = qobject_cast<QCheckBox*>(ui->checkBoxLayout->itemAt(i)->widget());
                if (checkBox) {
                    checkBox->setChecked(value & checkBox->property("value").toULongLong());
                }
            }
        } catch (...) {
            LOG_ERROR(Frontend, "Could not convert {} to number", initial_value);
        }
    } else {
        // This is a combination of constants, splitted with + or |
        const auto tmp = Common::SplitString(initial_value, '+');

        std::vector<std::string> out;
        for (const auto& str : tmp) {
            const auto tmp2 = Common::SplitString(str, '|');
            out.insert(out.end(), tmp2.begin(), tmp2.end());
        }
        for (int i = 0; i < ui->checkBoxLayout->count(); ++i) {
            auto* checkBox = qobject_cast<QCheckBox*>(ui->checkBoxLayout->itemAt(i)->widget());
            if (checkBox) {
                checkBox->setChecked(
                    std::find(out.begin(), out.end(),
                              checkBox->property("name").toString().toStdString()) != out.end());
            }
        }
    }
}

void OptionSetDialog::UpdateUIDisplay() {
    if (layout_type != 1)
        return;

    if (ui->comboBox->currentIndex() == ui->comboBox->count() - 1) { // custom
        ui->comboBoxHelpLabel->setVisible(false);
        ui->lineEdit->setVisible(true);
        adjustSize();
        return;
    }

    ui->lineEdit->setVisible(false);
    for (const auto& constant : option.named_constants) {
        if (constant.name == ui->comboBox->currentData().toString().toStdString()) {
            ui->comboBoxHelpLabel->setVisible(true);
            ui->comboBoxHelpLabel->setText(QString::fromStdString(constant.description));
            return;
        }
    }
}

std::pair<bool, std::string> OptionSetDialog::GetCurrentValue() {
    if (!is_set) {
        return {};
    }

    switch (layout_type) {
    case 0: // line edit layout
        return {true, ui->lineEdit->text().toStdString()};
    case 1: // combo box layout
        if (ui->comboBox->currentIndex() == ui->comboBox->count() - 1) {
            return {true, ui->lineEdit->text().toStdString()}; // custom
        }
        return {true, ui->comboBox->currentData().toString().toStdString()};
    case 2: { // check boxes layout
        std::string out;
        for (int i = 0; i < ui->checkBoxLayout->count(); ++i) {
            auto* checkBox = qobject_cast<QCheckBox*>(ui->checkBoxLayout->itemAt(i)->widget());
            if (checkBox && checkBox->isChecked()) {
                if (!out.empty()) {
                    out.append("+");
                }
                out.append(checkBox->property("name").toString().toStdString());
            }
        }
        if (out.empty()) {
            out = "0x0";
        }
        return {true, out};
    }
    default:
        return {};
    }
}

OptionSetDialog::OptionSetDialog(QWidget* parent, VideoDumper::OptionInfo option_,
                                 const std::string& initial_value)
    : QDialog(parent), ui(std::make_unique<Ui::OptionSetDialog>()), option(std::move(option_)) {

    ui->setupUi(this);
    InitializeUI(initial_value);

    connect(ui->unsetButton, &QPushButton::clicked, [this] {
        is_set = false;
        accept();
    });
    connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &OptionSetDialog::accept);
    connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &OptionSetDialog::reject);
}

OptionSetDialog::~OptionSetDialog() = default;