Commit 0615dcb8 authored by Aleks Kissinger's avatar Aleks Kissinger

finished export dialog (closes #49)

parent f68cf0d4
......@@ -5,11 +5,13 @@
#include <QFileDialog>
#include <QSettings>
#include <QStandardPaths>
ExportDialog::ExportDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::ExportDialog)
{
QSettings settings("tikzit", "tikzit");
ui->setupUi(this);
QIntValidator *v = new QIntValidator(this);
......@@ -31,6 +33,32 @@ ExportDialog::ExportDialog(QWidget *parent) :
ui->width->blockSignals(false);
ui->height->blockSignals(false);
}
if (!settings.value("previous-export-file-format").isNull()) {
ui->fileFormat->setCurrentIndex(settings.value("previous-export-file-format").toInt());
}
// set a default export file
QString path = (!settings.value("previous-export-file-path").isNull()) ?
settings.value("previous-export-file-path").toString() :
QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
QString suffix;
switch (ui->fileFormat->currentIndex()) {
case PNG: suffix = ".png"; break;
case JPG: suffix = ".jpg"; break;
case PDF: suffix = ".pdf"; break;
}
QString fileName;
int i = 0;
bool exists = true;
while (exists) {
fileName = path + "/tikzit_image" + QString::number(i) + suffix;
exists = QFileInfo::exists(fileName);
++i;
}
ui->filePath->setText(QDir::toNativeSeparators(fileName));
}
ExportDialog::~ExportDialog()
......@@ -38,6 +66,31 @@ ExportDialog::~ExportDialog()
delete ui;
}
QString ExportDialog::filePath()
{
return ui->filePath->text();
}
QSize ExportDialog::size()
{
return QSize(ui->width->text().toInt(),
ui->height->text().toInt());
}
ExportDialog::Format ExportDialog::fileFormat()
{
return static_cast<Format>(ui->fileFormat->currentIndex());
}
void ExportDialog::accept()
{
QSettings settings("tikzit", "tikzit");
QFileInfo fi(filePath());
settings.setValue("previous-export-file-path", fi.absolutePath());
settings.setValue("previous-export-file-format", fileFormat());
QDialog::accept();
}
void ExportDialog::setHeightFromWidth()
{
if (ui->keepAspect->isChecked()) {
......@@ -88,8 +141,14 @@ void ExportDialog::on_browseButton_clicked()
dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setNameFilter(ui->fileFormat->currentText());
dialog.setFileMode(QFileDialog::AnyFile);
if (!settings.value("previous-export-file-path").isNull())
dialog.setDirectory(settings.value("previous-export-file-path").toString());
dialog.setLabelText(QFileDialog::Accept, "Select");
QFileInfo fi(ui->filePath->text());
if (!fi.absolutePath().isEmpty()) {
dialog.setDirectory(fi.absolutePath());
dialog.selectFile(fi.baseName());
}
dialog.setOption(QFileDialog::DontUseNativeDialog);
if (dialog.exec()) {
......@@ -101,6 +160,7 @@ void ExportDialog::on_fileFormat_currentIndexChanged(int f)
{
ui->width->setEnabled(f != PDF);
ui->height->setEnabled(f != PDF);
ui->keepAspect->setEnabled(f != PDF);
QString path = ui->filePath->text();
if (!path.isEmpty()) {
......
......@@ -13,12 +13,17 @@ class ExportDialog : public QDialog
public:
explicit ExportDialog(QWidget *parent = nullptr);
~ExportDialog();
~ExportDialog() override;
enum Format {
PNG = 0,
JPG = 1,
PDF = 2
};
QString filePath();
QSize size();
Format fileFormat();
public slots:
void accept() override;
protected slots:
void setHeightFromWidth();
......
......@@ -156,11 +156,27 @@ void PreviewWindow::render() {
void PreviewWindow::exportImage()
{
QSettings settings("tikzit", "tikzit");
if (_doc == nullptr) return;
ExportDialog *d = new ExportDialog(this);
int ret = d->exec();
if (ret == QDialog::Accepted) {
qDebug() << "save accepted";
bool success;
if (d->fileFormat() == ExportDialog::PDF) {
success = _doc->exportPdf(d->filePath());
} else {
success = _doc->exportImage(
d->filePath(),
(d->fileFormat() == ExportDialog::PNG) ? "PNG" : "JPG",
d->size());
}
if (!success) {
QMessageBox::warning(this,
"Error",
"Could not write to: '" + d->filePath() +
"'. Check file permissions or choose a new location.");
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment