2019-09-19 05:19:50 +00:00
|
|
|
#include "CutterConfig.h"
|
|
|
|
|
|
|
|
#include "MultitypeFileSaveDialog.h"
|
|
|
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
2021-01-24 14:50:13 +00:00
|
|
|
MultitypeFileSaveDialog::MultitypeFileSaveDialog(QWidget *parent, const QString &caption,
|
2019-09-19 05:19:50 +00:00
|
|
|
const QString &directory)
|
|
|
|
: QFileDialog(parent, caption, directory)
|
|
|
|
{
|
|
|
|
this->setAcceptMode(AcceptMode::AcceptSave);
|
|
|
|
this->setFileMode(QFileDialog::AnyFile);
|
|
|
|
|
|
|
|
connect(this, &QFileDialog::filterSelected, this, &MultitypeFileSaveDialog::onFilterSelected);
|
|
|
|
}
|
|
|
|
|
2021-01-24 14:50:13 +00:00
|
|
|
void MultitypeFileSaveDialog::setTypes(
|
|
|
|
const QVector<MultitypeFileSaveDialog::TypeDescription> types, bool useDetection)
|
2019-09-19 05:19:50 +00:00
|
|
|
{
|
|
|
|
this->hasTypeDetection = useDetection;
|
|
|
|
this->types.clear();
|
|
|
|
this->types.reserve(types.size() + (useDetection ? 1 : 0));
|
|
|
|
if (useDetection) {
|
2021-01-24 14:50:13 +00:00
|
|
|
this->types.push_back(TypeDescription { tr("Detect type (*)"), "", QVariant() });
|
2019-09-19 05:19:50 +00:00
|
|
|
}
|
|
|
|
this->types.append(types);
|
|
|
|
QStringList filters;
|
|
|
|
for (auto &type : this->types) {
|
|
|
|
filters.append(type.description);
|
|
|
|
}
|
|
|
|
setNameFilters(filters);
|
|
|
|
onFilterSelected(this->types.first().description);
|
|
|
|
}
|
|
|
|
|
|
|
|
MultitypeFileSaveDialog::TypeDescription MultitypeFileSaveDialog::selectedType() const
|
|
|
|
{
|
|
|
|
auto filterIt = findType(this->selectedNameFilter());
|
|
|
|
if (filterIt == this->types.end()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
if (hasTypeDetection && filterIt == this->types.begin()) {
|
|
|
|
QFileInfo info(this->selectedFiles().first());
|
|
|
|
QString currentSuffix = info.suffix();
|
2021-01-24 14:50:13 +00:00
|
|
|
filterIt = std::find_if(types.begin(), types.end(),
|
|
|
|
[¤tSuffix](const TypeDescription &v) {
|
|
|
|
return currentSuffix == v.extension;
|
|
|
|
});
|
2019-09-19 05:19:50 +00:00
|
|
|
if (filterIt != types.end()) {
|
|
|
|
return *filterIt;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
} else {
|
|
|
|
return *filterIt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultitypeFileSaveDialog::done(int r)
|
|
|
|
{
|
|
|
|
if (r == QDialog::Accepted) {
|
|
|
|
QFileInfo info(selectedFiles().first());
|
|
|
|
auto selectedType = this->selectedType();
|
|
|
|
if (selectedType.extension.isEmpty()) {
|
|
|
|
QMessageBox::warning(this, tr("File save error"),
|
|
|
|
tr("Unrecognized extension '%1'").arg(info.suffix()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QFileDialog::done(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultitypeFileSaveDialog::onFilterSelected(const QString &filter)
|
|
|
|
{
|
|
|
|
auto it = findType(filter);
|
|
|
|
if (it == types.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool detectionSelected = hasTypeDetection && it == types.begin();
|
|
|
|
if (detectionSelected) {
|
|
|
|
setDefaultSuffix(types[1].extension);
|
|
|
|
} else {
|
|
|
|
setDefaultSuffix(it->extension);
|
|
|
|
}
|
|
|
|
if (!this->selectedFiles().empty()) {
|
|
|
|
QString currentSelection = this->selectedFiles().first();
|
|
|
|
QFileInfo info(currentSelection);
|
|
|
|
if (!detectionSelected) {
|
|
|
|
QString currentSuffix = info.suffix();
|
|
|
|
if (currentSuffix != it->extension) {
|
|
|
|
selectFile(info.dir().filePath(info.completeBaseName() + "." + it->extension));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QVector<MultitypeFileSaveDialog::TypeDescription>::const_iterator
|
|
|
|
MultitypeFileSaveDialog::findType(const QString &description) const
|
|
|
|
{
|
2021-01-24 14:50:13 +00:00
|
|
|
return std::find_if(types.begin(), types.end(), [&description](const TypeDescription &v) {
|
2019-09-19 05:19:50 +00:00
|
|
|
return v.description == description;
|
|
|
|
});
|
|
|
|
}
|