Repository navigation
Expand file tree
/
Copy pathParameterEditor.cpp
More file actions
247 lines (235 loc) · 14.4 KB
/
Copy pathParameterEditor.cpp
File metadata and controls
247 lines (235 loc) · 14.4 KB
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
#include "ui/ParameterEditor.h"
#include "graph/FilterCatalog.h"
#include "graph/FilterGraph.h"
#include <QCheckBox>
#include <algorithm>
#include <QComboBox>
#include <QCompleter>
#include <QDoubleSpinBox>
#include <QFileDialog>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QSpinBox>
#include <QAbstractItemView>
#include <QScreen>
#include <QFileInfo>
#include <QDir>
#include <QSignalBlocker>
#include <cmath>
#include <QTimer>
#include <QResizeEvent>
namespace vsr {
ParameterEditor::ParameterEditor(QWidget *parent)
: QWidget(parent)
{
setObjectName(QStringLiteral("parameterEditor"));
form_ = new QGridLayout(this);
form_->setContentsMargins(8, 8, 8, 8);
form_->setHorizontalSpacing(8);
form_->setVerticalSpacing(10);
form_->setColumnStretch(0, 1);
form_->setColumnStretch(1, 0);
form_->setAlignment(Qt::AlignTop);
clear();
form_->addWidget(new QLabel(QStringLiteral("选择处理链中的节点以编辑参数。"), this), 0, 0, 1, 2);
}
void ParameterEditor::setNode(const FilterDefinition *definition, const FilterNode *node)
{
clear();
if (!definition || !node) {
form_->addWidget(new QLabel(QStringLiteral("选择处理链中的节点以编辑参数。"), this), 0, 0, 1, 2);
return;
}
auto *description = new QLabel(definition->description, this);
description->setObjectName("parameterDescription");description->setWordWrap(true);
description->setMinimumWidth(0);auto descriptionPolicy=QSizePolicy(QSizePolicy::Ignored,QSizePolicy::Preferred);descriptionPolicy.setHeightForWidth(true);description->setSizePolicy(descriptionPolicy);
description->setContentsMargins(0,2,0,4);description->setAlignment(Qt::AlignTop|Qt::AlignLeft);
description->setTextFormat(Qt::PlainText);
form_->addWidget(description, 0, 0, 1, 2);
int row = 1;
QComboBox *shaderMode = nullptr;
QComboBox *shaderCategory = nullptr;
QComboBox *outputMode = nullptr;
QComboBox *outputScale = nullptr;
QLineEdit *shaderPath = nullptr;
dimensionAxis_=node->parameters.value("dimension_axis","width").toString();
for (const auto ¶meter : definition->parameters) {
QWidget *editor = nullptr;
const QVariant value = node->parameters.value(parameter.id, parameter.defaultValue);
const QString id = parameter.id;
if(definition->id=="anime4k" && id=="dimension_axis")continue;
switch (parameter.type) {
case ParameterType::Integer: {
auto *spin = new QSpinBox(this);
spin->setRange(static_cast<int>(parameter.minimum), static_cast<int>(parameter.maximum));
spin->setSingleStep(static_cast<int>(parameter.step));
spin->setValue(value.toInt());
if(definition->id=="anime4k" && id=="width")shaderWidth_=spin;
if(definition->id=="anime4k" && id=="height")shaderHeight_=spin;
connect(spin, &QSpinBox::valueChanged, this, [this, id, spin](int v) {
if(spin==shaderWidth_ || spin==shaderHeight_){dimensionAxis_=id;emit parameterChanged("dimension_axis",id);setInputSize(inputSize_);}
emit parameterChanged(id, v);
});
editor = spin;
break;
}
case ParameterType::Real: {
auto *spin = new QDoubleSpinBox(this);
spin->setRange(parameter.minimum, parameter.maximum);
spin->setSingleStep(parameter.step);
spin->setDecimals(parameter.step < 0.01 ? 3 : 2);
spin->setValue(value.toDouble());
connect(spin, &QDoubleSpinBox::valueChanged, this, [this, id](double v) { emit parameterChanged(id, v); });
editor = spin;
break;
}
case ParameterType::Boolean: {
auto *check = new QCheckBox(this);
check->setChecked(value.toBool());
if(definition->id=="anime4k" && id=="keep_aspect")keepAspect_=check;
connect(check, &QCheckBox::toggled, this, [this, id](bool v) { emit parameterChanged(id, v); });
editor = check;
break;
}
case ParameterType::Choice: {
auto *combo = new QComboBox(this);
if (definition->id == "anime4k" && id == "mode") shaderMode = combo;
if(definition->id=="anime4k" && id=="category")shaderCategory=combo;
if(definition->id=="anime4k" && id=="output_mode")outputMode=combo;
if(definition->id=="anime4k" && id=="scale")outputScale=combo;
combo->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLengthWithIcon);
combo->setMinimumContentsLength(8);
if(combo!=shaderMode){combo->addItems(parameter.choices);if(combo==shaderCategory){const int custom=combo->findText("自定义 GLSL");if(custom>=0)combo->setItemText(custom,"自定义 GLSL/HLSL");}}
if(combo==shaderMode){combo->setEditable(true);combo->setInsertPolicy(QComboBox::NoInsert);combo->completer()->setCompletionMode(QCompleter::PopupCompletion);combo->completer()->setFilterMode(Qt::MatchContains);combo->completer()->setCaseSensitivity(Qt::CaseInsensitive);}
int popupWidth = 0;
for (const auto &item : parameter.choices) popupWidth = std::max(popupWidth, combo->fontMetrics().horizontalAdvance(item) + 48);
combo->view()->setMinimumWidth(combo==shaderMode && screen()?std::min(popupWidth,screen()->availableGeometry().width()-32):popupWidth);
if(combo==shaderMode){combo->view()->setMinimumWidth(420);combo->view()->setMaximumWidth(520);combo->view()->setTextElideMode(Qt::ElideMiddle);combo->view()->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);}
combo->setToolTip(value.toString());
if(combo!=shaderMode)combo->setCurrentText(value.toString());
const auto fit = [combo, shaderMode, shaderCategory] { const int width=std::max(70, combo->fontMetrics().horizontalAdvance(combo->currentText()) + 40);combo->setFixedWidth(combo==shaderMode || combo==shaderCategory?120:std::min(width,120)); };
fit();
if(combo!=shaderMode)connect(combo, &QComboBox::currentTextChanged, this,
[this, id, combo, fit](const QString &v) { fit(); combo->setToolTip(v); emit parameterChanged(id, v==QStringLiteral("自定义 GLSL/HLSL")?QStringLiteral("自定义 GLSL"):v); });
editor = combo;
break;
}
case ParameterType::File: {
auto *container = new QWidget(this);
auto *layout = new QHBoxLayout(container);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(4);
auto *path = new QLineEdit(value.toString(), container);
if (definition->id == "anime4k" && id == "shader") shaderPath = path;
auto *browse = new QPushButton(QStringLiteral("浏览…"), container);
connect(path, &QLineEdit::editingFinished, this,
[this, id, path] { emit parameterChanged(id, path->text()); });
connect(browse, &QPushButton::clicked, this, [this, id, path] {
const QString selected = QFileDialog::getOpenFileName(
this, QStringLiteral("选择 GLSL/HLSL 着色器"), path->text(),
QStringLiteral("Shaders (*.glsl *.hook *.hlsl);;所有文件 (*.*)"));
if (!selected.isEmpty()) {
path->setText(selected);
emit parameterChanged(id, selected);
}
});
layout->addWidget(path, 1);
layout->addWidget(browse);
editor = container;
break;
}
}
auto *label = new QLabel(parameter.label, this);
label->setObjectName(QStringLiteral("parameterLabel_") + id);
label->setWordWrap(!parameter.label.contains("Vulkan",Qt::CaseInsensitive));
label->setMinimumWidth(0);
auto labelPolicy=QSizePolicy(parameter.label.contains("Vulkan",Qt::CaseInsensitive)?QSizePolicy::Minimum:QSizePolicy::Ignored,QSizePolicy::Preferred);labelPolicy.setHeightForWidth(label->wordWrap());label->setSizePolicy(labelPolicy);
editor->setObjectName(QStringLiteral("parameter_") + id);
if (parameter.type == ParameterType::File) {
form_->addWidget(label, row++, 0, 1, 2);
editor->setMinimumWidth(0);
editor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
form_->addWidget(editor, row++, 0, 1, 2);
} else {
editor->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
form_->addWidget(label, row, 0, Qt::AlignVCenter);
form_->addWidget(editor, row++, 1, Qt::AlignRight | Qt::AlignVCenter);
}
}
if (shaderMode && shaderPath) {
QStringList choices;for(const auto &p:definition->parameters)if(p.id=="mode")choices=p.choices;
const auto populate=[shaderMode,shaderCategory,choices](const QString &selected) {
const QSignalBlocker blocker(shaderMode);shaderMode->clear();
for(const auto &path:choices){
if(shaderCategory->currentText()!="全部" && FilterCatalog::shaderCategory(path)!=(shaderCategory->currentText()=="自定义 GLSL/HLSL"?QString("自定义 GLSL"):shaderCategory->currentText()))continue;
QString label=path=="自定义 GLSL"?QString("自定义 GLSL/HLSL"):QFileInfo(path).fileName();const auto hardware=FilterCatalog::shaderHardwareLabel(path);
if(!hardware.isEmpty())label+=" · "+hardware;
if(path.contains('/'))label+=" · "+path.section('/',1,1)+" / "+QFileInfo(path).dir().dirName();
shaderMode->addItem(label,path);shaderMode->setItemData(shaderMode->count()-1,path+"\n"+FilterCatalog::shaderCategory(path)+"\n"+hardware,Qt::ToolTipRole);
}
const int index=shaderMode->findData(selected);shaderMode->setCurrentIndex(index>=0?index:0);
shaderMode->setToolTip(shaderMode->currentData(Qt::ToolTipRole).toString());shaderMode->lineEdit()->setCursorPosition(0);
};
const auto selected=node->parameters.value("shader").toString().isEmpty()?node->parameters.value("mode").toString():QString("自定义 GLSL");
if(shaderCategory->currentText()!="全部" && shaderCategory->currentText()!=FilterCatalog::shaderCategory(selected)){const QSignalBlocker blocker(shaderCategory);shaderCategory->setCurrentText(FilterCatalog::shaderCategory(selected)=="自定义 GLSL"?QString("自定义 GLSL/HLSL"):FilterCatalog::shaderCategory(selected));}
populate(selected);
shaderPath->parentWidget()->setEnabled(selected=="自定义 GLSL");
const auto select=[this,shaderPath,shaderMode] {
const auto mode=shaderMode->currentData().toString();if(mode.isEmpty())return;
const bool custom = mode == QStringLiteral("自定义 GLSL");
shaderPath->parentWidget()->setEnabled(custom);
if (!custom && !shaderPath->text().isEmpty()){shaderPath->clear();emit parameterChanged("shader",QString());}
shaderMode->setToolTip(shaderMode->currentData(Qt::ToolTipRole).toString());shaderMode->lineEdit()->setCursorPosition(0);emit parameterChanged("mode",mode);
};
connect(shaderMode,qOverload<int>(&QComboBox::activated),this,[select](int){select();});
connect(shaderCategory,&QComboBox::currentTextChanged,this,[populate,select,shaderMode](const QString &){const auto selected=shaderMode->currentData().toString();populate(selected);select();});
connect(shaderPath,&QLineEdit::textChanged,this,[shaderCategory](const QString &path){if(!path.isEmpty())shaderCategory->setCurrentText("自定义 GLSL/HLSL");});
auto *nativeCheck=findChild<QCheckBox *>("parameter_hlsl");
const auto updateNative=[shaderPath,nativeCheck]{const bool native=QFileInfo(shaderPath->text()).suffix().compare("hlsl",Qt::CaseInsensitive)==0;nativeCheck->setEnabled(!native);if(native)nativeCheck->setChecked(true);};
connect(shaderPath,&QLineEdit::textChanged,this,[updateNative]{updateNative();});updateNative();
const auto updateOutput=[this,outputMode,outputScale]{const bool resolution=outputMode->currentText()=="指定分辨率";outputScale->setEnabled(!resolution);shaderWidth_->setEnabled(resolution);shaderHeight_->setEnabled(resolution);keepAspect_->setEnabled(resolution);};
updateOutput();connect(outputMode,&QComboBox::currentTextChanged,this,[updateOutput](const QString &){updateOutput();});
connect(keepAspect_,&QCheckBox::toggled,this,[this](bool enabled){if(enabled)setInputSize(inputSize_);});
}
if(definition->id=="anime4k"){
auto *hint=new QLabel(QStringLiteral("勾选预览使用HLSL加速,导出仍然使用GLSL"),this);hint->setObjectName("shaderPreviewExportHint");hint->setWordWrap(true);hint->setMinimumWidth(0);
auto policy=QSizePolicy(QSizePolicy::Ignored,QSizePolicy::Preferred);policy.setHeightForWidth(true);hint->setSizePolicy(policy);form_->addWidget(hint,row++,0,1,2);
}
QTimer::singleShot(0,this,[this]{updateTextHeights();});
if (definition->parameters.isEmpty())
form_->addWidget(new QLabel(QStringLiteral("该节点没有可调参数。"), this), row, 0, 1, 2);
}
void ParameterEditor::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
updateTextHeights();
}
void ParameterEditor::updateTextHeights()
{
form_->activate();
for(int i=0;i<form_->count();++i)if(auto *label=qobject_cast<QLabel*>(form_->itemAt(i)->widget());label && label->wordWrap() && label->width()>0){
label->setMinimumHeight(0);
label->setMinimumHeight(label->heightForWidth(label->width()));
}
}
void ParameterEditor::clear()
{
shaderWidth_=shaderHeight_=nullptr;keepAspect_=nullptr;
while (auto *item = form_->takeAt(0)) {
delete item->widget();
delete item;
}
}
void ParameterEditor::setInputSize(QSize size)
{
inputSize_=size;
if(size.isEmpty() || !shaderWidth_ || !shaderHeight_ || !keepAspect_->isChecked())return;
const bool width=dimensionAxis_!="height";auto *other=width?shaderHeight_:shaderWidth_;
const int computed=std::clamp(int(std::lround(width?double(shaderWidth_->value())*size.height()/size.width():double(shaderHeight_->value())*size.width()/size.height())),other->minimum(),other->maximum());
if(other->value()==computed)return;
const QSignalBlocker blocker(other);other->setValue(computed);emit parameterChanged(width?"height":"width",computed);
}
}