GUI: implement automatically setting the output file name

This commit is contained in:
Moritz Bunkus 2014-09-14 11:48:24 +02:00
parent c19da845fc
commit 45e3984571
5 changed files with 120 additions and 23 deletions

View File

@ -12,7 +12,9 @@
#include "mkvtoolnix-gui/util/settings.h"
#include "mkvtoolnix-gui/util/util.h"
#include <QDir>
#include <QFileDialog>
#include <QFileInfo>
#include <QList>
#include <QMessageBox>
#include <QString>
@ -507,6 +509,7 @@ MergeWidget::addOrAppendFiles(bool append) {
reinitFilesTracksControls();
setTitleMaybe(identifiedFiles);
setOutputFileNameMaybe(identifiedFiles[0]->m_fileName);
}
QStringList
@ -641,3 +644,76 @@ MergeWidget::setTitleMaybe(QString const &title) {
ui->title->setText(title);
m_config.m_title = title;
}
QString
MergeWidget::suggestOutputFileNameExtension()
const {
auto hasTracks = false, hasVideo = false, hasAudio = false, hasStereoscopy = false;
for (auto const &t : m_config.m_tracks) {
if (!t->m_muxThis)
continue;
hasTracks = true;
if (t->isVideo()) {
hasVideo = true;
if (t->m_stereoscopy >= 2)
hasStereoscopy = true;
} else if (t->isAudio())
hasAudio = true;
}
return m_config.m_webmMode ? "webm"
: hasStereoscopy ? "mk3d"
: hasVideo ? "mkv"
: hasAudio ? "mka"
: hasTracks ? "mks"
: "mkv";
}
void
MergeWidget::setOutputFileNameMaybe(QString const &fileName) {
auto &settings = Settings::get();
auto policy = settings.m_outputFileNamePolicy;
if (fileName.isEmpty() || (Settings::DontSetOutputFileName == policy))
return;
auto currentOutput = ui->output->text();
auto srcFileName = QFileInfo{ currentOutput.isEmpty() ? fileName : currentOutput };
QDir outputDir;
if (Settings::ToPreviousDirectory == policy)
outputDir = settings.m_lastOutputDir;
else if (Settings::ToFixedDirectory == policy)
outputDir = settings.m_fixedOutputDir;
else if (Settings::ToParentOfFirstInputFile == policy)
outputDir = srcFileName.absoluteDir();
else
Q_ASSERT_X(false, "setOutputFileNameMaybe", "Untested output file name policy");
if (!outputDir.exists())
outputDir = srcFileName.absoluteDir();
auto baseName = srcFileName.baseName();
auto idx = 0;
while (true) {
auto suffix = suggestOutputFileNameExtension();
auto currentBaseName = QString{"%1%2.%3"}.arg(baseName).arg(idx ? QString{" (%1)"}.arg(idx) : "").arg(suffix);
auto outputFileName = QFileInfo{outputDir, currentBaseName};
if (!settings.m_uniqueOutputFileNames || !outputFileName.exists()) {
ui->output->setText(outputFileName.absoluteFilePath());
m_config.m_destination = outputFileName.absoluteFilePath();
break;
}
++idx;
}
}

View File

@ -169,6 +169,9 @@ protected:
virtual void setTitleMaybe(QList<SourceFilePtr> const &files);
virtual void setTitleMaybe(QString const &title);
virtual void setOutputFileNameMaybe(QString const &fileName);
virtual QString suggestOutputFileNameExtension() const;
};
#endif // MTX_MKVTOOLNIX_GUI_MERGE_WIDGET_MERGE_WIDGET_H

View File

@ -33,8 +33,11 @@ void
MergeWidget::onBrowseOutput() {
auto filter = m_config.m_webmMode ? QY("WebM files") + Q(" (*.webm)") : QY("Matroska files") + Q(" (*.mkv *.mka *.mks *.mk3d)");
auto fileName = getSaveFileName(QY("Select output file name"), filter, ui->output);
if (!fileName.isEmpty())
m_config.m_destination = fileName;
if (!fileName.isEmpty()) {
m_config.m_destination = fileName;
Settings::get().m_lastOutputDir = QFileInfo{ fileName }.absoluteDir();
Settings::get().save();
}
}
void

View File

@ -23,20 +23,22 @@ Settings::load() {
QSettings reg;
reg.beginGroup("settings");
m_mkvmergeExe = reg.value("mkvmergeExe", "mkvmerge").toString();
m_priority = static_cast<ProcessPriority>(reg.value("priority", static_cast<int>(NormalPriority)).toInt());
m_lastOpenDir = QDir{reg.value("lastOpenDir").toString()};
m_lastOutputDir = QDir{reg.value("lastOutputDir").toString()};
m_lastConfigDir = QDir{reg.value("lastConfigDir").toString()};
m_scanForPlaylistsPolicy = static_cast<ScanForPlaylistsPolicy>(reg.value("scanForPlaylistsPolicy", static_cast<int>(AskBeforeScanning)).toInt());
m_minimumPlaylistDuration = reg.value("minimumPlaylistDuration", 120).toUInt();
reg.endGroup();
m_mkvmergeExe = reg.value("mkvmergeExe", "mkvmerge").toString();
m_priority = static_cast<ProcessPriority>(reg.value("priority", static_cast<int>(NormalPriority)).toInt());
m_lastOpenDir = QDir{reg.value("lastOpenDir").toString()};
m_lastOutputDir = QDir{reg.value("lastOutputDir").toString()};
m_lastConfigDir = QDir{reg.value("lastConfigDir").toString()};
m_scanForPlaylistsPolicy = static_cast<ScanForPlaylistsPolicy>(reg.value("scanForPlaylistsPolicy", static_cast<int>(AskBeforeScanning)).toInt());
m_minimumPlaylistDuration = reg.value("minimumPlaylistDuration", 120).toUInt();
reg.beginGroup("features");
m_setAudioDelayFromFileName = reg.value("setAudioDelayFromFileName", true).toBool();
m_disableAVCompression = reg.value("disableAVCompression", false).toBool();
m_autoSetFileTitle = reg.value("autoSetFileTitle", true).toBool();
m_autoSetOutputFileName = reg.value("autoSetOutputFileName", true).toBool();
m_uniqueOutputFileNames = reg.value("uniqueOutputFileNames", true).toBool();
m_outputFileNamePolicy = static_cast<OutputFileNamePolicy>(reg.value("outputFileNamePolicy", static_cast<int>(ToPreviousDirectory)).toInt());
m_fixedOutputDir = QDir{reg.value("fixedOutputDir").toString()};
reg.endGroup();
reg.beginGroup("defaults");
@ -56,20 +58,22 @@ Settings::save()
QSettings reg;
reg.beginGroup("settings");
reg.setValue("mkvmergeExe", m_mkvmergeExe);
reg.setValue("priority", static_cast<int>(m_priority));
reg.setValue("lastOpenDir", m_lastOpenDir.path());
reg.setValue("lastOutputDir", m_lastOutputDir.path());
reg.setValue("lastConfigDir", m_lastConfigDir.path());
reg.setValue("scanForPlaylistsPolicy", static_cast<int>(m_scanForPlaylistsPolicy));
reg.setValue("minimumPlaylistDuration", m_minimumPlaylistDuration);
reg.endGroup();
reg.setValue("mkvmergeExe", m_mkvmergeExe);
reg.setValue("priority", static_cast<int>(m_priority));
reg.setValue("lastOpenDir", m_lastOpenDir.path());
reg.setValue("lastOutputDir", m_lastOutputDir.path());
reg.setValue("lastConfigDir", m_lastConfigDir.path());
reg.setValue("scanForPlaylistsPolicy", static_cast<int>(m_scanForPlaylistsPolicy));
reg.setValue("minimumPlaylistDuration", m_minimumPlaylistDuration);
reg.beginGroup("features");
reg.setValue("setAudioDelayFromFileName", m_setAudioDelayFromFileName);
reg.setValue("autoSetFileTitle", m_autoSetFileTitle);
reg.setValue("autoSetOutputFileName", m_autoSetOutputFileName);
reg.setValue("disableAVCompression", m_disableAVCompression);
reg.setValue("outputFileNamePolicy", static_cast<int>(m_outputFileNamePolicy));
reg.setValue("fixedOutputDir", m_fixedOutputDir.path());
reg.setValue("uniqueOutputFileNames", m_uniqueOutputFileNames);
reg.endGroup();
reg.beginGroup("defaults");

View File

@ -23,10 +23,21 @@ public:
NeverScan,
};
enum OutputFileNamePolicy {
DontSetOutputFileName = 0,
ToPreviousDirectory,
ToFixedDirectory,
ToParentOfFirstInputFile,
};
QString m_mkvmergeExe, m_defaultTrackLanguage;
ProcessPriority m_priority;
QDir m_lastOpenDir, m_lastOutputDir, m_lastConfigDir;
bool m_setAudioDelayFromFileName, m_disableAVCompression, m_autoSetFileTitle, m_autoSetOutputFileName;
bool m_setAudioDelayFromFileName, m_disableAVCompression, m_autoSetFileTitle;
OutputFileNamePolicy m_outputFileNamePolicy;
QDir m_fixedOutputDir;
bool m_uniqueOutputFileNames;
ScanForPlaylistsPolicy m_scanForPlaylistsPolicy;
unsigned int m_minimumPlaylistDuration;