Skip to content

Commit

Permalink
chore: Add file output option in output mapping (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
royshil authored Aug 23, 2024
1 parent 1f32609 commit c1e1211
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/mapping-data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ std::string serialize_output_mapping_data(const output_mapping_data &data)
j_mapping["template_string"] = mapping.template_string;
j_mapping["css_props"] = mapping.css_props;
j_mapping["unhide_output_source"] = mapping.unhide_output_source;
j_mapping["file_path"] = mapping.file_path;
j.push_back(j_mapping);
}
return j.dump();
Expand All @@ -28,6 +29,7 @@ output_mapping_data deserialize_output_mapping_data(const std::string &data)
mapping.template_string = j_mapping.value("template_string", "");
mapping.css_props = j_mapping.value("css_props", "");
mapping.unhide_output_source = j_mapping.value("unhide_output_source", false);
mapping.file_path = j_mapping.value("file_path", "");
result.mappings.push_back(mapping);
}
return result;
Expand Down
2 changes: 2 additions & 0 deletions src/mapping-data.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

const std::string none_internal_rendering = "None / Internal rendering";
const std::string save_to_setting = "Save to source settings";
const std::string file_output_rendering = "File output";

struct output_mapping {
std::string name;
std::string output_source;
std::string template_string;
std::string css_props;
bool unhide_output_source = false;
std::string file_path;
};

struct output_mapping_data {
Expand Down
33 changes: 33 additions & 0 deletions src/ui/outputmapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
#include <QComboBox>
#include <QHeaderView>
#include <QStandardItem>
#include <QStandardItemModel>
#include <QFileDialog>
#include <QCheckBox>
#include <QPlainTextEdit>
#include <QToolButton>

namespace {

Expand Down Expand Up @@ -39,6 +44,7 @@ OutputMapping::OutputMapping(const output_mapping_data &mapping_data_in,
ui->plainTextEdit_cssProps->setEnabled(enable);
ui->checkBox_unhide_Source->setEnabled(enable);

// populate the props with the data of the selected row
if (enable) {
// get the selected row
const auto row = selected.indexes().first().row();
Expand All @@ -48,16 +54,20 @@ OutputMapping::OutputMapping(const output_mapping_data &mapping_data_in,
ui->plainTextEdit_template->blockSignals(true);
ui->plainTextEdit_cssProps->blockSignals(true);
ui->checkBox_unhide_Source->blockSignals(true);
ui->lineEdit_file_output->blockSignals(true);
// set the plainTextEdit_template and plainTextEdit_cssProps to the template_string and css_props of the selected row
ui->plainTextEdit_template->setPlainText(
this->mapping_data.mappings[row].template_string.c_str());
ui->plainTextEdit_cssProps->setPlainText(
this->mapping_data.mappings[row].css_props.c_str());
ui->checkBox_unhide_Source->setChecked(
this->mapping_data.mappings[row].unhide_output_source);
ui->lineEdit_file_output->setText(
this->mapping_data.mappings[row].file_path.c_str());
ui->plainTextEdit_template->blockSignals(false);
ui->plainTextEdit_cssProps->blockSignals(false);
ui->checkBox_unhide_Source->blockSignals(false);
ui->lineEdit_file_output->blockSignals(false);
}
});

Expand Down Expand Up @@ -96,6 +106,21 @@ OutputMapping::OutputMapping(const output_mapping_data &mapping_data_in,
this->update_handler(this->mapping_data);
});

// connect toolButton_selectFile to open a file dialog to select a file and update lineEdit_file_output
connect(ui->toolButton_selectFile, &QToolButton::clicked, [this]() {
// open a file dialog to select a file
const auto file =
QFileDialog::getSaveFileName(this, "Select file", "", "All files (*.*)");
// set the lineEdit_file_output to the selected file
ui->lineEdit_file_output->setText(file);
// get the selected row
const auto row = ui->tableView->currentIndex().row();
// set the output_source of the selected row to the selected file
this->mapping_data.mappings[row].file_path = file.toStdString();
// call update_handler
this->update_handler(this->mapping_data);
});

// populate the model with the mapping data
for (const auto &mapping : mapping_data.mappings) {
model.appendRow(QList<QStandardItem *>() << new QStandardItem(mapping.name.c_str())
Expand Down Expand Up @@ -143,6 +168,8 @@ QComboBox *OutputMapping::createSourcesComboBox()
QComboBox *comboBox = new QComboBox(this);
// add "Internal Renderer" to the comboBox
comboBox->addItem(QString::fromStdString(none_internal_rendering));
// add "File output" to the comboBox
comboBox->addItem(QString::fromStdString(file_output_rendering));
// add "Save to source settings" to the comboBox
comboBox->addItem(QString::fromStdString(save_to_setting));
// add all text and media sources to the comboBox
Expand All @@ -153,6 +180,12 @@ QComboBox *OutputMapping::createSourcesComboBox()
const auto row = ui->tableView->currentIndex().row();
// get the output_name of the selected item in the comboBox
const auto output_name = comboBox->currentText().toStdString();
// if output is file, enable the group box groupBox_outputFile
if (output_name == file_output_rendering) {
ui->widget_file_output->setEnabled(true);
} else {
ui->widget_file_output->setEnabled(false);
}
// remove the prefix from the output_name if it exists
std::string output_name_without_prefix = output_name;
if (output_name.find("(Text) ") == 0) {
Expand Down
61 changes: 50 additions & 11 deletions src/ui/outputmapping.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>457</width>
<height>387</height>
<height>423</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -146,16 +146,6 @@ Use {{body}} variable for unparsed object/array representation of the entire res
</layout>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox_unhide_Source">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Unhide output source on update</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="text">
Expand All @@ -169,6 +159,55 @@ Use {{body}} variable for unparsed object/array representation of the entire res
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="widget_file_output" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>Output File</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_file_output">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="toolButton_selectFile">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox_unhide_Source">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Unhide output source on update</string>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
Expand Down
20 changes: 20 additions & 0 deletions src/url-source-callbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,22 @@ std::string prepare_text_from_template(const output_mapping &mapping,
return text;
}

void save_text_to_file(const std::string &text, const std::string &file_path)
{
// save the text to a file
if (file_path.empty()) {
obs_log(LOG_ERROR, "No file path specified");
return;
}
std::ofstream file(file_path);
if (!file.is_open()) {
obs_log(LOG_ERROR, "Failed to open file '%s'", file_path.c_str());
return;
}
file << text;
file.close();
}

void output_with_mapping(const request_data_handler_response &response, struct url_source_data *usd)
{
std::vector<output_mapping> mappings;
Expand Down Expand Up @@ -237,6 +253,7 @@ void output_with_mapping(const request_data_handler_response &response, struct u

if (is_valid_output_source_name(mapping.output_source.c_str()) &&
mapping.output_source != none_internal_rendering &&
mapping.output_source != file_output_rendering &&
mapping.output_source != save_to_setting) {
// If an output source is selected - use it for rendering
setTextCallback(text, mapping);
Expand All @@ -252,6 +269,9 @@ void output_with_mapping(const request_data_handler_response &response, struct u
obs_source_update(usd->source, source_settings);
obs_data_release(source_settings);
}
} else if (mapping.output_source == file_output_rendering) {
// If the output source is set to file output - save the text to a file
save_text_to_file(text, mapping.file_path);
} else {
// render the text internally
any_internal_rendering = true;
Expand Down

0 comments on commit c1e1211

Please sign in to comment.