separation of ui from backend in project

This commit is contained in:
itsmattkc
2019-03-30 13:26:00 +11:00
parent 114606c28e
commit 5ddf2726dd
32 changed files with 1046 additions and 810 deletions
+355
View File
@@ -36,6 +36,57 @@ ProjectModel::~ProjectModel() {
destroy_root();
}
int ProjectModel::PrepareToSave()
{
int element_count = 0;
PrepareToSaveInternal(element_count, get_root());
return element_count;
}
void ProjectModel::PrepareToSaveInternal(int& element_count, Media *root)
{
for (int i=0;i<root->childCount();i++) {
Media* child = root->child(i);
switch (child->get_type()) {
case MEDIA_TYPE_FOLDER:
child->temp_id = element_count;
break;
case MEDIA_TYPE_FOOTAGE:
child->to_footage()->save_id = element_count;
break;
case MEDIA_TYPE_SEQUENCE:
child->to_sequence()->save_id = element_count;
break;
}
element_count++;
if (child->childCount() > 0) {
PrepareToSaveInternal(element_count, child);
}
}
}
void ProjectModel::Save(QXmlStreamWriter &stream, Media* root)
{
if (root == nullptr) {
root = get_root();
}
for (int i=0;i<root->childCount();i++) {
Media* child = root->child(i);
child->Save(stream);
if (child->childCount() > 0) {
Save(stream, child);
}
}
}
void ProjectModel::make_root() {
root_item_ = std::make_shared<Media>();
root_item_->temp_id = 0;
@@ -211,6 +262,58 @@ QVector<Media *> ProjectModel::GetAllFolders()
return GetAllMediaOfType(MEDIA_TYPE_FOLDER);
}
QString ProjectModel::GetNextSequenceName(QString prepend)
{
if (prepend.isEmpty()) {
prepend = tr("Sequence %1");
}
int sequence_number = 1;
QString test;
QVector<Media*> all_sequences = GetAllSequences();
bool found;
do {
found = false;
test = prepend.arg(sequence_number);
for (int i=0;i<all_sequences.size();i++) {
if (all_sequences.at(i)->get_name() == test) {
found = true;
sequence_number++;
break;
}
}
} while (found);
return test;
}
MediaPtr ProjectModel::CreateSequence(ComboAction *ca, SequencePtr s, bool open, Media *parent)
{
MediaPtr item = std::make_shared<Media>();
item->set_sequence(s);
if (ca != nullptr) {
ca->append(new AddMediaCommand(item, parent));
if (open) {
ca->append(new ChangeSequenceAction(s));
}
} else {
appendChild(parent, item);
if (open) {
olive::Global->set_sequence(s);
}
}
return item;
}
QVector<Media *> ProjectModel::GetAllMediaOfType(int search_type)
{
QVector<Media*> media_list;
@@ -296,3 +399,255 @@ int ProjectModel::childCount(Media *parent) {
}
return parent->childCount();
}
void ProjectModel::process_file_list(QStringList& files, bool recursive, MediaPtr replace, Media* parent) {
bool imported = false;
// retrieve the array of image formats from the user's configuration
QStringList image_sequence_formats = olive::CurrentConfig.img_seq_formats.split("|");
// a cache of image sequence formatted URLS to assist the user in importing image sequences
QVector<QString> image_sequence_urls;
QVector<bool> image_sequence_importassequence;
if (!recursive) last_imported_media.clear();
bool create_undo_action = (!recursive && replace == nullptr);
ComboAction* ca = nullptr;
if (create_undo_action) ca = new ComboAction();
// Loop through received files
for (int i=0;i<files.size();i++) {
// If this file is a directory, we'll recursively call this function again to process the directory's contents
if (QFileInfo(files.at(i)).isDir()) {
QString folder_name = get_file_name_from_path(files.at(i));
MediaPtr folder = CreateFolder(folder_name);
QDir directory(files.at(i));
directory.setFilter(QDir::NoDotAndDotDot | QDir::AllEntries);
QFileInfoList subdir_files = directory.entryInfoList();
QStringList subdir_filenames;
for (int j=0;j<subdir_files.size();j++) {
subdir_filenames.append(subdir_files.at(j).filePath());
}
if (create_undo_action) {
ca->append(new AddMediaCommand(folder, parent));
} else {
appendChild(parent, folder);
}
process_file_list(subdir_filenames, true, nullptr, folder.get());
imported = true;
} else if (!files.at(i).isEmpty()) {
QString file = files.at(i);
// Check if the user is importing an Olive project file
if (file.endsWith(".ove", Qt::CaseInsensitive)) {
// This file is an Olive project file. Ask the user if they really want to import it.
if (QMessageBox::question(this,
tr("Import a Project"),
tr("\"%1\" is an Olive project file. It will merge with this project. "
"Do you wish to continue?").arg(file),
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
// load the project without clearing the current one
olive::Global->ImportProject(file);
}
} else {
// This file is NOT an Olive project file
// Used later if this file is part of an already processed image sequence
bool skip = false;
/* Heuristic to determine whether file is part of an image sequence */
// Firstly, we run a heuristic on whether this file is an image by checking its file extension
bool file_is_an_image = false;
// Get the string position of the extension in the filename
int lastcharindex = file.lastIndexOf(".");
if (lastcharindex != -1 && lastcharindex > file.lastIndexOf('/')) {
QString ext = file.mid(lastcharindex+1);
// If the file extension is part of a predetermined list (from Config::img_seq_formats), we'll treat it
// as an image
if (image_sequence_formats.contains(ext, Qt::CaseInsensitive)) {
file_is_an_image = true;
}
} else {
// If we're here, the file has no extension, but we'll still check if its an image sequence just in case
lastcharindex = file.length();
file_is_an_image = true;
}
// Some image sequence's don't start at "0", if it is indeed an image sequence, we'll use this variable
// later to determine where it does start
int start_number = 0;
// Check if we passed the earlier heuristic to check whether this is a file, and whether the last number in
// the filename (before the extension) is a number
if (file_is_an_image && file[lastcharindex-1].isDigit()) {
// Check how many digits are at the end of this filename
int digit_count = 0;
int digit_test = lastcharindex-1;
while (file[digit_test].isDigit()) {
digit_count++;
digit_test--;
}
// Retrieve the integer represented at the end of this filename
digit_test++;
int file_number = file.mid(digit_test, digit_count).toInt();
// Check whether a file exists with the same format but one number higher or one number lower
if (QFileInfo::exists(QString(file.left(digit_test) + QString("%1").arg(file_number-1, digit_count, 10, QChar('0')) + file.mid(lastcharindex)))
|| QFileInfo::exists(QString(file.left(digit_test) + QString("%1").arg(file_number+1, digit_count, 10, QChar('0')) + file.mid(lastcharindex)))) {
//
// If so, it certainly looks like it *could* be an image sequence, but we'll ask the user just in case
//
// Firstly we should check if this file is part of a sequence the user has already confirmed as either a
// sequence or not a sequence (e.g. if the user happened to select a bunch of images that happen to increase
// consecutively). We format the filename with FFmpeg's '%Nd' (N = digits) formatting for reading image
// sequences
QString new_filename = file.left(digit_test) + "%" + QString::number(digit_count) + "d" + file.mid(lastcharindex);
int does_url_cache_already_contain_this = image_sequence_urls.indexOf(new_filename);
if (does_url_cache_already_contain_this > -1) {
// We've already processed an image with the same formatting
// Check if the last time we saw this formatting, the user chose to import as a sequence
if (image_sequence_importassequence.at(does_url_cache_already_contain_this)) {
// If so, no need to import this file too, so we signal to the rest of the function to skip this file
skip = true;
}
// If not, we can fall-through to the next step which is importing normally
} else {
// If we're here, we've never seen a file with this formatting before, so we'll ask whether to import
// as a sequence or not
// Add this file formatting file to the URL cache
image_sequence_urls.append(new_filename);
// This does look like an image sequence, let's ask the user if it'll indeed be an image sequence
if (QMessageBox::question(this,
tr("Image sequence detected"),
tr("The file '%1' appears to be part of an image sequence. "
"Would you like to import it as such?").arg(file),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::Yes) == QMessageBox::Yes) {
// Proceed to the next step of this with the formatted filename
file = new_filename;
// Cache the user's answer alongside the image_sequence_urls value - in this case, YES, this will be an
// image sequence
image_sequence_importassequence.append(true);
// FFmpeg needs to know what file number to start at in the sequence. In case the image sequence doesn't
// start at a zero, we'll loop decreasing the number until it doesn't exist anymore
QString test_filename_format = QString("%1%2%3").arg(file.left(digit_test), "%1", file.mid(lastcharindex));
int test_file_number = file_number;
do {
test_file_number--;
} while (QFileInfo::exists(test_filename_format.arg(QString("%1").arg(test_file_number, digit_count, 10, QChar('0')))));
// set the image sequence's start number to the last that existed
start_number = test_file_number + 1;
} else {
// Cache the user's response to the image sequence question - i.e. none of the files imported with this
// formatting should be imported as an image sequence
image_sequence_importassequence.append(false);
}
}
}
}
// If we're not skipping this file, let's import it
if (!skip) {
MediaPtr item;
FootagePtr m;
if (replace != nullptr) {
item = replace;
} else {
item = std::make_shared<Media>();
}
m = std::make_shared<Footage>();
// Edge case for PNGs that standardized unassociated alpha
if (file.endsWith("png", Qt::CaseInsensitive)) {
m->alpha_is_associated = false;
}
m->using_inout = false;
m->url = file;
m->name = QFileInfo(files.at(i)).fileName();
m->start_number = start_number;
item->set_footage(m);
last_imported_media.append(item.get());
if (replace == nullptr) {
if (create_undo_action) {
ca->append(new AddMediaCommand(item, parent));
} else {
appendChild(parent, item);
}
}
imported = true;
}
}
}
}
if (create_undo_action) {
if (imported) {
olive::UndoStack.push(ca);
for (int i=0;i<last_imported_media.size();i++) {
// generate waveform/thumbnail in another thread
PreviewGenerator::AnalyzeMedia(last_imported_media.at(i));
}
} else {
delete ca;
}
}
}