split passage get into separate io device

This commit is contained in:
itsmattkc
2019-01-10 13:14:49 +11:00
parent b0f5b42865
commit 69ccb55d61
3 changed files with 280 additions and 274 deletions
+44 -38
View File
@@ -2,55 +2,61 @@
#include <QXmlStreamReader>
#include <QLabel>
#include <QFile>
#include "ui/collapsiblewidget.h"
#include "debug.h"
VoidEffect::VoidEffect(Clip *c, const QString& n) : Effect(c, NULL) {
name = n;
QString display_name;
if (n.isEmpty()) {
display_name = "(unknown)";
} else {
display_name = n;
}
EffectRow* row = add_row("Missing Effect", false, false);
row->add_widget(new QLabel(display_name));
container->setText(display_name);
name = n;
QString display_name;
if (n.isEmpty()) {
display_name = "(unknown)";
} else {
display_name = n;
}
EffectRow* row = add_row("Missing Effect", false, false);
row->add_widget(new QLabel(display_name));
container->setText(display_name);
}
void VoidEffect::load(QXmlStreamReader &stream) {
QString tag = stream.name().toString();
qint64 start_index = stream.characterOffset();
qint64 end_index = start_index;
while (!stream.atEnd() && !(stream.name() == tag && stream.isEndElement())) {
end_index = stream.characterOffset();
stream.readNext();
}
qint64 passage_length = end_index - start_index;
if (passage_length > 0) {
// store xml data verbatim
QIODevice* device = stream.device();
device->seek(start_index);
bytes = device->read(passage_length);
int passage_end = bytes.lastIndexOf('>')+1;
bytes.remove(passage_end, bytes.size()-passage_end);
}
QString tag = stream.name().toString();
qint64 start_index = stream.characterOffset();
qint64 end_index = start_index;
while (!stream.atEnd() && !(stream.name() == tag && stream.isEndElement())) {
end_index = stream.characterOffset();
stream.readNext();
}
qint64 passage_length = end_index - start_index;
if (passage_length > 0) {
// store xml data verbatim
QFile* device = static_cast<QFile*>(stream.device());
QFile passage_get(device->fileName());
if (passage_get.open(QFile::ReadOnly)) {
passage_get.seek(start_index);
bytes = passage_get.read(passage_length);
int passage_end = bytes.lastIndexOf('>')+1;
bytes.remove(passage_end, bytes.size()-passage_end);
passage_get.close();
}
}
}
void VoidEffect::save(QXmlStreamWriter &stream) {
if (!name.isEmpty()) {
stream.writeAttribute("name", name);
stream.writeAttribute("enabled", QString::number(is_enabled()));
if (!name.isEmpty()) {
stream.writeAttribute("name", name);
stream.writeAttribute("enabled", QString::number(is_enabled()));
// force xml writer to expand <effect> tag, ignored when loading
stream.writeStartElement("void");
stream.writeEndElement();
// force xml writer to expand <effect> tag, ignored when loading
stream.writeStartElement("void");
stream.writeEndElement();
if (!bytes.isEmpty()) {
// write stored data
QIODevice* device = stream.device();
device->write(bytes);
}
}
if (!bytes.isEmpty()) {
// write stored data
QIODevice* device = stream.device();
device->write(bytes);
}
}
}
+30 -30
View File
@@ -34,7 +34,7 @@ LoadThread::LoadThread(LoadDialog* l, bool a) : ld(l), autorecovery(a), cancelle
connect(this, SIGNAL(success()), this, SLOT(success_func()));
connect(this, SIGNAL(error()), this, SLOT(error_func()));
connect(this, SIGNAL(start_create_dual_transition(const TransitionData*,Clip*,Clip*,const EffectMeta*)), this, SLOT(create_dual_transition(const TransitionData*,Clip*,Clip*,const EffectMeta*)));
connect(this, SIGNAL(start_create_effect_ui(QXmlStreamReader*, Clip*, int, const QString*, const EffectMeta*, long, bool)), this, SLOT(create_effect_ui(QXmlStreamReader*, Clip*, int, const QString*, const EffectMeta*, long, bool)));
connect(this, SIGNAL(start_create_effect_ui(QXmlStreamReader*, Clip*, int, const QString*, const EffectMeta*, long, bool)), this, SLOT(create_effect_ui(QXmlStreamReader*, Clip*, int, const QString*, const EffectMeta*, long, bool)));
}
const EffectMeta* get_meta_from_name(const QString& name) {
@@ -62,10 +62,10 @@ void LoadThread::load_effect(QXmlStreamReader& stream, Clip* c) {
} else if (attr.name() == "length") {
effect_length = attr.value().toLong();
}
}
}
// wait for effects to be loaded
panel_effect_controls->effects_loaded.lock();
panel_effect_controls->effects_loaded.lock();
const EffectMeta* meta = NULL;
@@ -74,21 +74,21 @@ void LoadThread::load_effect(QXmlStreamReader& stream, Clip* c) {
meta = get_meta_from_name(effect_name);
}
panel_effect_controls->effects_loaded.unlock();
panel_effect_controls->effects_loaded.unlock();
QString tag = stream.name().toString();
QString tag = stream.name().toString();
int type;
if (tag == "opening") {
type = TA_OPENING_TRANSITION;
} else if (tag == "closing") {
type = TA_CLOSING_TRANSITION;
} else {
type = TA_NO_TRANSITION;
}
int type;
if (tag == "opening") {
type = TA_OPENING_TRANSITION;
} else if (tag == "closing") {
type = TA_CLOSING_TRANSITION;
} else {
type = TA_NO_TRANSITION;
}
emit start_create_effect_ui(&stream, c, type, &effect_name, meta, effect_length, effect_enabled);
waitCond.wait(&mutex);
emit start_create_effect_ui(&stream, c, type, &effect_name, meta, effect_length, effect_enabled);
waitCond.wait(&mutex);
}
void LoadThread::read_next(QXmlStreamReader &stream) {
@@ -162,7 +162,7 @@ bool LoadThread::load_worker(QFile& f, QXmlStreamReader& stream, int type) {
internal_proj_url = stream.readElementText();
internal_proj_dir = QFileInfo(internal_proj_url).absoluteDir();
} else {
while (!cancelled && !(stream.name() == root_search && stream.isEndElement())) {
while (!cancelled && !stream.atEnd() && !(stream.name() == root_search && stream.isEndElement())) {
read_next(stream);
if (stream.name() == child_search && stream.isStartElement()) {
switch (type) {
@@ -582,7 +582,7 @@ void LoadThread::run() {
xml_error = false;
if (show_err) emit error();
} else if (stream.hasError()) {
error_str = stream.errorString();
error_str = stream.errorString() + " - Line: " + QString::number(stream.lineNumber()) + " Col:" + QString::number(stream.columnNumber());
xml_error = true;
emit error();
cont = false;
@@ -657,7 +657,7 @@ void LoadThread::create_effect_ui(
QXmlStreamReader* stream,
Clip* c,
int type,
const QString* effect_name,
const QString* effect_name,
const EffectMeta* meta,
long effect_length,
bool effect_enabled)
@@ -685,19 +685,19 @@ void LoadThread::create_effect_ui(
if (cancelled) return;
if (type == TA_NO_TRANSITION) {
if (meta == NULL) {
// create void effect
VoidEffect* ve = new VoidEffect(c, *effect_name);
ve->set_enabled(effect_enabled);
ve->load(*stream);
c->effects.append(ve);
} else {
Effect* e = create_effect(c, meta);
e->set_enabled(effect_enabled);
e->load(*stream);
if (meta == NULL) {
// create void effect
VoidEffect* ve = new VoidEffect(c, *effect_name);
ve->set_enabled(effect_enabled);
ve->load(*stream);
c->effects.append(ve);
} else {
Effect* e = create_effect(c, meta);
e->set_enabled(effect_enabled);
e->load(*stream);
c->effects.append(e);
}
c->effects.append(e);
}
} else {
int transition_index = create_transition(c, NULL, meta);
Transition* t = c->sequence->transitions.at(transition_index);
+206 -206
View File
@@ -93,7 +93,7 @@ void load_internal_effects() {
em.name = "Volume";
em.internal = EFFECT_INTERNAL_VOLUME;
effects.append(em);
effects.append(em);
em.name = "Pan";
em.internal = EFFECT_INTERNAL_PAN;
@@ -244,7 +244,7 @@ void init_effects() {
}
EffectInit::EffectInit() {
panel_effect_controls->effects_loaded.lock();
panel_effect_controls->effects_loaded.lock();
}
void EffectInit::run() {
@@ -252,7 +252,7 @@ void EffectInit::run() {
load_internal_effects();
load_shader_effects();
load_vst_effects();
panel_effect_controls->effects_loaded.unlock();
panel_effect_controls->effects_loaded.unlock();
dout << "[INFO] Finished initializing effects";
}
@@ -280,195 +280,195 @@ Effect::Effect(Clip* c, const EffectMeta *em) :
connect(container->title_bar, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(show_context_menu(const QPoint&)));
if (em != NULL) {
// set up UI from effect file
container->setText(em->name);
if (em != NULL) {
// set up UI from effect file
container->setText(em->name);
if (!em->filename.isEmpty()) {
QFile effect_file(em->filename);
if (effect_file.open(QFile::ReadOnly)) {
QXmlStreamReader reader(&effect_file);
if (!em->filename.isEmpty()) {
QFile effect_file(em->filename);
if (effect_file.open(QFile::ReadOnly)) {
QXmlStreamReader reader(&effect_file);
while (!reader.atEnd()) {
if (reader.name() == "row" && reader.isStartElement()) {
QString row_name;
const QXmlStreamAttributes& attributes = reader.attributes();
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "name") {
row_name = attr.value().toString();
}
}
if (!row_name.isEmpty()) {
EffectRow* row = add_row(row_name);
while (!reader.atEnd() && !(reader.name() == "row" && reader.isEndElement())) {
reader.readNext();
if (reader.name() == "field" && reader.isStartElement()) {
int type = EFFECT_TYPE_VIDEO;
QString id;
while (!reader.atEnd()) {
if (reader.name() == "row" && reader.isStartElement()) {
QString row_name;
const QXmlStreamAttributes& attributes = reader.attributes();
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "name") {
row_name = attr.value().toString();
}
}
if (!row_name.isEmpty()) {
EffectRow* row = add_row(row_name);
while (!reader.atEnd() && !(reader.name() == "row" && reader.isEndElement())) {
reader.readNext();
if (reader.name() == "field" && reader.isStartElement()) {
int type = EFFECT_TYPE_VIDEO;
QString id;
// get field type
const QXmlStreamAttributes& attributes = reader.attributes();
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "type") {
QString comp = attr.value().toString().toUpper();
if (comp == "DOUBLE") {
type = EFFECT_FIELD_DOUBLE;
} else if (comp == "BOOL") {
type = EFFECT_FIELD_BOOL;
} else if (comp == "COLOR") {
type = EFFECT_FIELD_COLOR;
} else if (comp == "COMBO") {
type = EFFECT_FIELD_COMBO;
} else if (comp == "FONT") {
type = EFFECT_FIELD_FONT;
} else if (comp == "STRING") {
type = EFFECT_FIELD_STRING;
} else if (comp == "FILE") {
type = EFFECT_FIELD_FILE;
}
} else if (attr.name() == "id") {
id = attr.value().toString();
}
}
// get field type
const QXmlStreamAttributes& attributes = reader.attributes();
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "type") {
QString comp = attr.value().toString().toUpper();
if (comp == "DOUBLE") {
type = EFFECT_FIELD_DOUBLE;
} else if (comp == "BOOL") {
type = EFFECT_FIELD_BOOL;
} else if (comp == "COLOR") {
type = EFFECT_FIELD_COLOR;
} else if (comp == "COMBO") {
type = EFFECT_FIELD_COMBO;
} else if (comp == "FONT") {
type = EFFECT_FIELD_FONT;
} else if (comp == "STRING") {
type = EFFECT_FIELD_STRING;
} else if (comp == "FILE") {
type = EFFECT_FIELD_FILE;
}
} else if (attr.name() == "id") {
id = attr.value().toString();
}
}
if (id.isEmpty()) {
dout << "[ERROR] Couldn't load field from" << em->filename << "- ID cannot be empty.";
} else if (type > -1) {
EffectField* field = row->add_field(type, id);
connect(field, SIGNAL(changed()), this, SLOT(field_changed()));
switch (type) {
case EFFECT_FIELD_DOUBLE:
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "default") {
field->set_double_default_value(attr.value().toDouble());
} else if (attr.name() == "min") {
field->set_double_minimum_value(attr.value().toDouble());
} else if (attr.name() == "max") {
field->set_double_maximum_value(attr.value().toDouble());
}
}
break;
case EFFECT_FIELD_COLOR:
{
QColor color;
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "r") {
color.setRed(attr.value().toInt());
} else if (attr.name() == "g") {
color.setGreen(attr.value().toInt());
} else if (attr.name() == "b") {
color.setBlue(attr.value().toInt());
} else if (attr.name() == "rf") {
color.setRedF(attr.value().toFloat());
} else if (attr.name() == "gf") {
color.setGreenF(attr.value().toFloat());
} else if (attr.name() == "bf") {
color.setBlueF(attr.value().toFloat());
} else if (attr.name() == "hex") {
color.setNamedColor(attr.value().toString());
}
}
field->set_color_value(color);
}
break;
case EFFECT_FIELD_STRING:
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "default") {
field->set_string_value(attr.value().toString());
}
}
break;
case EFFECT_FIELD_BOOL:
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "default") {
field->set_bool_value(attr.value() == "1");
}
}
break;
case EFFECT_FIELD_COMBO:
{
int combo_index = 0;
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "default") {
combo_index = attr.value().toInt();
break;
}
}
while (!reader.atEnd() && !(reader.name() == "field" && reader.isEndElement())) {
reader.readNext();
if (reader.name() == "option" && reader.isStartElement()) {
reader.readNext();
field->add_combo_item(reader.text().toString(), 0);
}
}
field->set_combo_index(combo_index);
}
break;
case EFFECT_FIELD_FONT:
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "default") {
field->set_font_name(attr.value().toString());
}
}
break;
case EFFECT_FIELD_FILE:
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "filename") {
field->set_filename(attr.value().toString());
}
}
break;
}
}
}
}
}
} else if (reader.name() == "shader" && reader.isStartElement()) {
enable_shader = true;
const QXmlStreamAttributes& attributes = reader.attributes();
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "vert") {
vertPath = attr.value().toString();
} else if (attr.name() == "frag") {
fragPath = attr.value().toString();
}
}
}/* else if (reader.name() == "superimpose" && reader.isStartElement()) {
enable_superimpose = true;
const QXmlStreamAttributes& attributes = reader.attributes();
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "script") {
QFile script_file(get_effects_dir() + "/" + attr.value().toString());
if (script_file.open(QFile::ReadOnly)) {
script = script_file.readAll();
} else {
dout << "[ERROR] Failed to open superimpose script file for" << em->filename;
enable_superimpose = false;
}
break;
}
}
}*/
reader.readNext();
}
if (id.isEmpty()) {
dout << "[ERROR] Couldn't load field from" << em->filename << "- ID cannot be empty.";
} else if (type > -1) {
EffectField* field = row->add_field(type, id);
connect(field, SIGNAL(changed()), this, SLOT(field_changed()));
switch (type) {
case EFFECT_FIELD_DOUBLE:
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "default") {
field->set_double_default_value(attr.value().toDouble());
} else if (attr.name() == "min") {
field->set_double_minimum_value(attr.value().toDouble());
} else if (attr.name() == "max") {
field->set_double_maximum_value(attr.value().toDouble());
}
}
break;
case EFFECT_FIELD_COLOR:
{
QColor color;
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "r") {
color.setRed(attr.value().toInt());
} else if (attr.name() == "g") {
color.setGreen(attr.value().toInt());
} else if (attr.name() == "b") {
color.setBlue(attr.value().toInt());
} else if (attr.name() == "rf") {
color.setRedF(attr.value().toFloat());
} else if (attr.name() == "gf") {
color.setGreenF(attr.value().toFloat());
} else if (attr.name() == "bf") {
color.setBlueF(attr.value().toFloat());
} else if (attr.name() == "hex") {
color.setNamedColor(attr.value().toString());
}
}
field->set_color_value(color);
}
break;
case EFFECT_FIELD_STRING:
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "default") {
field->set_string_value(attr.value().toString());
}
}
break;
case EFFECT_FIELD_BOOL:
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "default") {
field->set_bool_value(attr.value() == "1");
}
}
break;
case EFFECT_FIELD_COMBO:
{
int combo_index = 0;
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "default") {
combo_index = attr.value().toInt();
break;
}
}
while (!reader.atEnd() && !(reader.name() == "field" && reader.isEndElement())) {
reader.readNext();
if (reader.name() == "option" && reader.isStartElement()) {
reader.readNext();
field->add_combo_item(reader.text().toString(), 0);
}
}
field->set_combo_index(combo_index);
}
break;
case EFFECT_FIELD_FONT:
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "default") {
field->set_font_name(attr.value().toString());
}
}
break;
case EFFECT_FIELD_FILE:
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "filename") {
field->set_filename(attr.value().toString());
}
}
break;
}
}
}
}
}
} else if (reader.name() == "shader" && reader.isStartElement()) {
enable_shader = true;
const QXmlStreamAttributes& attributes = reader.attributes();
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "vert") {
vertPath = attr.value().toString();
} else if (attr.name() == "frag") {
fragPath = attr.value().toString();
}
}
}/* else if (reader.name() == "superimpose" && reader.isStartElement()) {
enable_superimpose = true;
const QXmlStreamAttributes& attributes = reader.attributes();
for (int i=0;i<attributes.size();i++) {
const QXmlStreamAttribute& attr = attributes.at(i);
if (attr.name() == "script") {
QFile script_file(get_effects_dir() + "/" + attr.value().toString());
if (script_file.open(QFile::ReadOnly)) {
script = script_file.readAll();
} else {
dout << "[ERROR] Failed to open superimpose script file for" << em->filename;
enable_superimpose = false;
}
break;
}
}
}*/
reader.readNext();
}
effect_file.close();
} else {
dout << "[ERROR] Failed to open effect file" << em->filename;
}
}
}
effect_file.close();
} else {
dout << "[ERROR] Failed to open effect file" << em->filename;
}
}
}
}
Effect::~Effect() {
@@ -794,28 +794,28 @@ void Effect::open() {
validate_meta_path();
bool glsl_compiled = true;
if (!vertPath.isEmpty()) {
if (glslProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, meta->path + "/" + vertPath)) {
dout << "[INFO] Vertex shader added successfully";
} else {
glsl_compiled = false;
dout << "[WARNING] Vertex shader could not be added";
}
if (glslProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, meta->path + "/" + vertPath)) {
dout << "[INFO] Vertex shader added successfully";
} else {
glsl_compiled = false;
dout << "[WARNING] Vertex shader could not be added";
}
}
if (!fragPath.isEmpty()) {
if (glslProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, meta->path + "/" + fragPath)) {
dout << "[INFO] Fragment shader added successfully";
} else {
glsl_compiled = false;
dout << "[WARNING] Fragment shader could not be added";
}
if (glslProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, meta->path + "/" + fragPath)) {
dout << "[INFO] Fragment shader added successfully";
} else {
glsl_compiled = false;
dout << "[WARNING] Fragment shader could not be added";
}
}
if (glsl_compiled) {
if (glslProgram->link()) {
dout << "[INFO] Shader program linked successfully";
} else {
dout << "[WARNING] Shader program failed to link";
}
}
if (glsl_compiled) {
if (glslProgram->link()) {
dout << "[INFO] Shader program linked successfully";
} else {
dout << "[WARNING] Shader program failed to link";
}
}
isOpen = true;
}
} else {