2015-01-23 11:07:58 +01:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file dialogsavelayout.cpp
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 22 1, 2015
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** This source code is part of the Valentine project, a pattern making
|
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
2015-02-27 11:27:48 +01:00
|
|
|
** Copyright (C) 2013-2015 Valentina project
|
2015-01-23 11:07:58 +01:00
|
|
|
** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
|
|
|
|
**
|
|
|
|
** Valentina is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** Valentina is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "dialogsavelayout.h"
|
|
|
|
#include "ui_dialogsavelayout.h"
|
2015-06-19 13:21:46 +02:00
|
|
|
#include "../options.h"
|
|
|
|
#include "../core/vapplication.h"
|
2015-10-08 20:11:50 +02:00
|
|
|
#include "../vmisc/vsettings.h"
|
2015-10-11 11:06:14 +02:00
|
|
|
#include "../ifc/exception/vexception.h"
|
2015-01-23 11:07:58 +01:00
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QMessageBox>
|
2015-08-26 05:39:33 +02:00
|
|
|
#include <QProcess>
|
|
|
|
#include <QDebug>
|
2015-01-23 11:07:58 +01:00
|
|
|
|
2015-10-05 15:08:26 +02:00
|
|
|
const QString baseFilenameRegExp = QStringLiteral("^[\\w\\-. ]+$");
|
|
|
|
|
2015-10-23 19:47:33 +02:00
|
|
|
bool DialogSaveLayout::havePdf = false;
|
|
|
|
bool DialogSaveLayout::tested = false;
|
2015-08-25 19:53:03 +02:00
|
|
|
|
2015-01-23 11:07:58 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-10-05 15:08:26 +02:00
|
|
|
DialogSaveLayout::DialogSaveLayout(int count, const QString &fileName, QWidget *parent)
|
2015-11-05 14:01:33 +01:00
|
|
|
:QDialog(parent), ui(new Ui::DialogSaveLAyout), count(count), isInitialized(false), availFormats(InitAvailFormats())
|
2015-01-23 11:07:58 +01:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
2015-12-09 11:40:43 +01:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
|
|
|
|
ui->lineEditPath->setClearButtonEnabled(true);
|
|
|
|
ui->lineEditFileName->setClearButtonEnabled(true);
|
|
|
|
#endif
|
|
|
|
|
2015-07-24 14:06:53 +02:00
|
|
|
qApp->ValentinaSettings()->GetOsSeparator() ? setLocale(QLocale::system()) : setLocale(QLocale(QLocale::C));
|
2015-02-10 20:27:11 +01:00
|
|
|
|
2015-01-23 11:07:58 +01:00
|
|
|
QPushButton *bOk = ui->buttonBox->button(QDialogButtonBox::Ok);
|
|
|
|
SCASSERT(bOk != nullptr);
|
|
|
|
bOk->setEnabled(false);
|
|
|
|
|
2015-10-08 19:45:26 +02:00
|
|
|
#if QT_VERSION > QT_VERSION_CHECK(5, 1, 0)
|
|
|
|
ui->lineEditFileName->setValidator( new QRegularExpressionValidator(QRegularExpression(baseFilenameRegExp), this));
|
|
|
|
#else
|
|
|
|
ui->lineEditFileName->setValidator( new QRegExpValidator(QRegExp(baseFilenameRegExp), this));
|
|
|
|
#endif
|
|
|
|
|
2015-10-05 15:08:26 +02:00
|
|
|
const QString mask = fileName+QLatin1Literal("_");
|
|
|
|
if (VApplication::CheckGUI())
|
|
|
|
{
|
|
|
|
ui->lineEditFileName->setText(mask);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (QRegularExpression(baseFilenameRegExp).match(mask).hasMatch())
|
|
|
|
{
|
|
|
|
ui->lineEditFileName->setText(mask);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-10-11 11:06:14 +02:00
|
|
|
VException e(tr("The base filename has not match regular expression."));
|
|
|
|
throw e;
|
2015-10-05 15:08:26 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-23 11:07:58 +01:00
|
|
|
|
2015-08-25 19:53:03 +02:00
|
|
|
foreach (auto& v , availFormats)
|
2015-01-23 11:07:58 +01:00
|
|
|
{
|
2015-10-23 19:47:33 +02:00
|
|
|
ui->comboBoxFormat->addItem(v.first, QVariant(v.second));
|
2015-01-23 11:07:58 +01:00
|
|
|
}
|
|
|
|
connect(bOk, &QPushButton::clicked, this, &DialogSaveLayout::Save);
|
2015-01-26 14:38:51 +01:00
|
|
|
connect(ui->lineEditFileName, &QLineEdit::textChanged, this, &DialogSaveLayout::ShowExample);
|
2015-01-23 11:07:58 +01:00
|
|
|
connect(ui->comboBoxFormat, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this,
|
|
|
|
&DialogSaveLayout::ShowExample);
|
|
|
|
connect(ui->pushButtonBrowse, &QPushButton::clicked, this, &DialogSaveLayout::Browse);
|
|
|
|
connect(ui->lineEditPath, &QLineEdit::textChanged, this, &DialogSaveLayout::PathChanged);
|
|
|
|
|
2015-07-24 14:06:53 +02:00
|
|
|
ui->lineEditPath->setText(qApp->ValentinaSettings()->GetPathLayout());
|
2015-03-31 15:38:59 +02:00
|
|
|
ShowExample();//Show example for current format.
|
2015-01-23 11:07:58 +01:00
|
|
|
}
|
2015-08-25 19:53:03 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
2015-10-23 19:47:33 +02:00
|
|
|
void DialogSaveLayout::SelectFormate(const int formate)
|
2015-08-25 19:53:03 +02:00
|
|
|
{
|
|
|
|
if (formate >= availFormats.size())
|
|
|
|
{
|
2015-10-11 11:06:14 +02:00
|
|
|
VException e(tr("Tried to use out of range format number."));
|
|
|
|
throw e;
|
2015-08-25 19:53:03 +02:00
|
|
|
}
|
|
|
|
|
2015-10-23 19:47:33 +02:00
|
|
|
const int i = ui->comboBoxFormat->findData(availFormats.at(formate).second);
|
2015-08-25 19:53:03 +02:00
|
|
|
if (i < 0)
|
|
|
|
{
|
2015-10-11 11:06:14 +02:00
|
|
|
VException e(tr("Selected not present format."));
|
|
|
|
throw e;
|
2015-08-25 19:53:03 +02:00
|
|
|
}
|
|
|
|
ui->comboBoxFormat->setCurrentIndex(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QString DialogSaveLayout::MakeHelpFormatList()
|
|
|
|
{
|
|
|
|
QString out = "\n";
|
|
|
|
int cntr = 0;
|
2015-10-23 19:47:33 +02:00
|
|
|
const QVector<std::pair<QString, QString>> availFormats = InitAvailFormats();
|
2015-08-25 19:53:03 +02:00
|
|
|
foreach(auto& v, availFormats)
|
|
|
|
{
|
2015-10-23 19:47:33 +02:00
|
|
|
out += "\t"+v.first+" = "+ QString::number(cntr++)+"\n";
|
2015-08-25 19:53:03 +02:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
2015-01-23 11:07:58 +01:00
|
|
|
|
2015-08-27 23:29:08 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-10-05 15:08:26 +02:00
|
|
|
void DialogSaveLayout::SetDestinationPath(const QString &cmdDestinationPath)
|
2015-08-27 23:29:08 +02:00
|
|
|
{
|
2015-10-05 15:08:26 +02:00
|
|
|
QString path;
|
|
|
|
if (cmdDestinationPath.isEmpty())
|
|
|
|
{
|
|
|
|
path = QDir::currentPath();
|
|
|
|
}
|
|
|
|
else if (QDir(cmdDestinationPath).isAbsolute())
|
|
|
|
{
|
|
|
|
path = cmdDestinationPath;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QDir dir;
|
|
|
|
if (not dir.cd(cmdDestinationPath))
|
|
|
|
{
|
2015-10-11 11:06:14 +02:00
|
|
|
VException e(tr("The destination directory doesn't exists or is not readable."));
|
|
|
|
throw e;
|
2015-10-05 15:08:26 +02:00
|
|
|
}
|
|
|
|
path = dir.absolutePath();
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "Output full path: " << path << "\n";
|
|
|
|
ui->lineEditPath->setText(path);
|
2015-08-27 23:29:08 +02:00
|
|
|
}
|
|
|
|
|
2015-01-23 11:07:58 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
DialogSaveLayout::~DialogSaveLayout()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QString DialogSaveLayout::Path() const
|
|
|
|
{
|
|
|
|
return ui->lineEditPath->text();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-26 14:38:51 +01:00
|
|
|
QString DialogSaveLayout::FileName() const
|
2015-01-23 11:07:58 +01:00
|
|
|
{
|
2015-01-26 14:38:51 +01:00
|
|
|
return ui->lineEditFileName->text();
|
2015-01-23 11:07:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QString DialogSaveLayout::Formate() const
|
|
|
|
{
|
2015-04-02 11:51:27 +02:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 2, 0)
|
2015-04-02 13:53:42 +02:00
|
|
|
return ui->comboBoxFormat->itemData(ui->comboBoxFormat->currentIndex()).toString();
|
2015-04-02 11:51:27 +02:00
|
|
|
#else
|
2015-01-23 11:07:58 +01:00
|
|
|
return ui->comboBoxFormat->currentData().toString();
|
2015-04-02 11:51:27 +02:00
|
|
|
#endif
|
2015-01-23 11:07:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void DialogSaveLayout::Save()
|
|
|
|
{
|
|
|
|
for (int i=0; i < count; ++i)
|
2015-09-28 20:54:41 +02:00
|
|
|
{
|
2015-01-26 14:38:51 +01:00
|
|
|
const QString name = Path()+"/"+FileName()+QString::number(i+1)+Formate();
|
2015-01-23 11:07:58 +01:00
|
|
|
if (QFile::exists(name))
|
|
|
|
{
|
|
|
|
QMessageBox::StandardButton res = QMessageBox::question(this, tr("Name conflict"),
|
|
|
|
tr("Folder already contain file with name %1. Rewrite all conflict file names?")
|
|
|
|
.arg(name), QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
|
|
|
|
if (res == QMessageBox::No)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void DialogSaveLayout::ShowExample()
|
|
|
|
{
|
2015-01-26 14:38:51 +01:00
|
|
|
ui->labelExample->setText(tr("Example:") + FileName() + "1" + Formate());
|
2015-01-23 11:07:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void DialogSaveLayout::Browse()
|
|
|
|
{
|
|
|
|
const QString dir = QFileDialog::getExistingDirectory(this, tr("Select folder"), QDir::homePath(),
|
|
|
|
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
|
|
|
|
ui->lineEditPath->setText(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void DialogSaveLayout::PathChanged(const QString &text)
|
|
|
|
{
|
|
|
|
QPushButton *bOk = ui->buttonBox->button(QDialogButtonBox::Ok);
|
|
|
|
SCASSERT(bOk != nullptr);
|
|
|
|
|
|
|
|
QPalette palette = ui->lineEditPath->palette();
|
|
|
|
|
|
|
|
QDir dir(text);
|
|
|
|
dir.setPath(text);
|
|
|
|
if (dir.exists(text))
|
|
|
|
{
|
|
|
|
bOk->setEnabled(true);
|
|
|
|
palette.setColor(ui->lineEditPath->foregroundRole(), Qt::black);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bOk->setEnabled(false);
|
|
|
|
palette.setColor(ui->lineEditPath->foregroundRole(), Qt::red);
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->lineEditPath->setPalette(palette);
|
|
|
|
}
|
2015-10-23 19:47:33 +02:00
|
|
|
|
2015-11-05 14:01:33 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void DialogSaveLayout::showEvent(QShowEvent *event)
|
|
|
|
{
|
|
|
|
QDialog::showEvent( event );
|
|
|
|
if ( event->spontaneous() )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isInitialized)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// do your init stuff here
|
|
|
|
|
|
|
|
setMaximumSize(size());
|
|
|
|
setMinimumSize(size());
|
|
|
|
|
|
|
|
isInitialized = true;//first show windows are held
|
|
|
|
}
|
|
|
|
|
2015-08-25 19:53:03 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-10-23 19:47:33 +02:00
|
|
|
bool DialogSaveLayout::SupportPSTest()
|
|
|
|
{
|
|
|
|
if (!tested)
|
|
|
|
{
|
|
|
|
havePdf = TestPdf();
|
|
|
|
tested = true;
|
|
|
|
}
|
|
|
|
return havePdf;
|
|
|
|
}
|
2015-08-25 19:53:03 +02:00
|
|
|
|
2015-10-23 19:47:33 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool DialogSaveLayout::TestPdf()
|
2015-08-25 19:53:03 +02:00
|
|
|
{
|
|
|
|
bool res = false;
|
|
|
|
|
|
|
|
QProcess proc;
|
|
|
|
#if defined(Q_OS_WIN) || defined(Q_OS_OSX)
|
|
|
|
proc.start(qApp->applicationDirPath()+"/"+PDFTOPS); // Seek pdftops in app bundle or near valentin.exe
|
|
|
|
#else
|
|
|
|
proc.start(PDFTOPS); // Seek pdftops in standard path
|
|
|
|
#endif
|
|
|
|
if (proc.waitForFinished(15000))
|
|
|
|
{
|
|
|
|
res = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug()<<PDFTOPS<<"error"<<proc.error()<<proc.errorString();
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2015-10-23 19:47:33 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QVector<std::pair<QString, QString>> DialogSaveLayout::InitAvailFormats()
|
|
|
|
{
|
|
|
|
QVector<std::pair<QString, QString>> list;
|
|
|
|
list.append(std::make_pair(tr("Svg files (*.svg)"), QLatin1Literal(".svg")));
|
|
|
|
list.append(std::make_pair(tr("PDF files (*.pdf)"), QLatin1Literal(".pdf")));
|
|
|
|
list.append(std::make_pair(tr("Images (*.png)"), QLatin1Literal(".png")));
|
|
|
|
list.append(std::make_pair(tr("Wavefront OBJ (*.obj)"), QLatin1Literal(".obj")));
|
|
|
|
if (SupportPSTest())
|
|
|
|
{
|
|
|
|
list.append(std::make_pair(tr("PS files (*.ps)"), QLatin1Literal(".ps")));
|
|
|
|
list.append(std::make_pair(tr("EPS files (*.eps)"), QLatin1Literal(".eps")));
|
|
|
|
}
|
|
|
|
list.append(std::make_pair(tr("DXF files (*.dxf)"), QLatin1Literal(".dxf")));
|
|
|
|
return list;
|
|
|
|
}
|