1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-07 07:13:21 +02:00
GPXSee/src/exportdialog.cpp

188 lines
6.0 KiB
C++
Raw Normal View History

2016-05-24 03:01:22 +02:00
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFormLayout>
2016-05-27 22:45:58 +02:00
#include <QGridLayout>
#include <QDialogButtonBox>
2016-05-24 03:01:22 +02:00
#include <QGroupBox>
#include <QComboBox>
2016-05-25 23:27:07 +02:00
#include <QRadioButton>
2016-05-24 03:01:22 +02:00
#include <QPushButton>
#include <QFileInfo>
#include <QMessageBox>
2016-05-27 22:45:58 +02:00
#include <QTabWidget>
#include <QDoubleSpinBox>
2016-05-24 03:01:22 +02:00
#include "fileselectwidget.h"
2016-05-27 22:45:58 +02:00
#include "units.h"
2016-05-24 03:01:22 +02:00
#include "exportdialog.h"
2016-12-06 01:48:26 +01:00
ExportDialog::ExportDialog(Export *exp, QWidget *parent)
: QDialog(parent), _export(exp)
2016-05-24 03:01:22 +02:00
{
int index;
_fileSelect = new FileSelectWidget();
_fileSelect->setFilter(tr("PDF files (*.pdf);;All files (*)"));
2016-12-06 01:48:26 +01:00
_fileSelect->setFile(_export->fileName);
2016-05-24 03:01:22 +02:00
_paperSize = new QComboBox();
_paperSize->addItem("A3", QPrinter::A3);
_paperSize->addItem("A4", QPrinter::A4);
_paperSize->addItem("A5", QPrinter::A5);
_paperSize->addItem("Tabloid", QPrinter::Tabloid);
_paperSize->addItem("Legal", QPrinter::Legal);
_paperSize->addItem("Letter", QPrinter::Letter);
2016-12-06 01:48:26 +01:00
if ((index = _paperSize->findData(_export->paperSize)) >= 0)
2016-05-25 23:27:07 +02:00
_paperSize->setCurrentIndex(index);
_resolution = new QComboBox();
_resolution->addItem("300 DPI", 300);
_resolution->addItem("600 DPI", 600);
_resolution->addItem("1200 DPI", 1200);
if ((index = _resolution->findData(_export->resolution)) >= 0)
_resolution->setCurrentIndex(index);
2016-05-25 23:27:07 +02:00
_portrait = new QRadioButton(tr("Portrait"));
_landscape = new QRadioButton(tr("Landscape"));
QHBoxLayout *orientationLayout = new QHBoxLayout();
orientationLayout->addWidget(_portrait);
orientationLayout->addWidget(_landscape);
2016-12-06 01:48:26 +01:00
if (_export->orientation == QPrinter::Portrait)
2016-05-25 23:27:07 +02:00
_portrait->setChecked(true);
else
_landscape->setChecked(true);
2016-05-27 22:45:58 +02:00
_topMargin = new QDoubleSpinBox();
_bottomMargin = new QDoubleSpinBox();
_leftMargin = new QDoubleSpinBox();
_rightMargin = new QDoubleSpinBox();
2016-12-07 21:38:36 +01:00
QString us = (_export->units == Imperial) ? tr("in") : tr("mm");
2016-05-27 22:45:58 +02:00
_topMargin->setSuffix(UNIT_SPACE + us);
_bottomMargin->setSuffix(UNIT_SPACE + us);
_leftMargin->setSuffix(UNIT_SPACE + us);
_rightMargin->setSuffix(UNIT_SPACE + us);
2016-12-07 21:38:36 +01:00
if (_export->units == Imperial) {
2016-12-06 01:48:26 +01:00
_topMargin->setValue(_export->margins.top() * MM2IN);
_bottomMargin->setValue(_export->margins.bottom() * MM2IN);
_leftMargin->setValue(_export->margins.left() * MM2IN);
_rightMargin->setValue(_export->margins.right() * MM2IN);
2016-05-27 22:45:58 +02:00
_topMargin->setSingleStep(0.1);
_bottomMargin->setSingleStep(0.1);
_leftMargin->setSingleStep(0.1);
_rightMargin->setSingleStep(0.1);
2016-12-06 01:48:26 +01:00
} else {
_topMargin->setValue(_export->margins.top());
_bottomMargin->setValue(_export->margins.bottom());
_leftMargin->setValue(_export->margins.left());
_rightMargin->setValue(_export->margins.right());
2016-05-27 22:45:58 +02:00
}
QGridLayout *marginsLayout = new QGridLayout();
marginsLayout->addWidget(_topMargin, 0, 0, 1, 2, Qt::AlignCenter);
2016-05-28 07:39:40 +02:00
marginsLayout->addWidget(_leftMargin, 1, 0, 1, 1, Qt::AlignRight);
marginsLayout->addWidget(_rightMargin, 1, 1, 1, 1, Qt::AlignLeft);
2016-05-27 22:45:58 +02:00
marginsLayout->addWidget(_bottomMargin, 2, 0, 1, 2, Qt::AlignCenter);
#ifndef Q_OS_MAC
QGroupBox *pageSetupBox = new QGroupBox(tr("Page Setup"));
#endif // Q_OS_MAC
QFormLayout *pageSetupLayout = new QFormLayout;
pageSetupLayout->addRow(tr("Page size:"), _paperSize);
pageSetupLayout->addRow(tr("Resolution:"), _resolution);
2016-05-27 22:45:58 +02:00
pageSetupLayout->addRow(tr("Orientation:"), orientationLayout);
pageSetupLayout->addRow(tr("Margins:"), marginsLayout);
#ifdef Q_OS_MAC
QFrame *line = new QFrame();
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);
pageSetupLayout->addRow(line);
pageSetupLayout->addRow(tr("File:"), _fileSelect);
2016-10-12 22:37:40 +02:00
pageSetupLayout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
2016-05-27 22:45:58 +02:00
#else // Q_OS_MAC
pageSetupBox->setLayout(pageSetupLayout);
#endif // Q_OS_MAC
#ifndef Q_OS_MAC
QGroupBox *outputFileBox = new QGroupBox(tr("Output file"));
QHBoxLayout *outputFileLayout = new QHBoxLayout();
outputFileLayout->addWidget(_fileSelect);
outputFileBox->setLayout(outputFileLayout);
#endif // Q_OS_MAC
QDialogButtonBox *buttonBox = new QDialogButtonBox();
buttonBox->addButton(tr("Export"), QDialogButtonBox::AcceptRole);
buttonBox->addButton(QDialogButtonBox::Cancel);
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
2016-05-24 03:01:22 +02:00
QVBoxLayout *layout = new QVBoxLayout;
2016-05-27 22:45:58 +02:00
#ifdef Q_OS_MAC
layout->addLayout(pageSetupLayout);
#else // Q_OS_MAC
layout->addWidget(pageSetupBox);
layout->addWidget(outputFileBox);
#endif // Q_OS_MAC
layout->addWidget(buttonBox);
2016-05-24 03:01:22 +02:00
setLayout(layout);
2016-05-27 22:45:58 +02:00
setWindowTitle(tr("Export to PDF"));
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
2016-05-24 03:01:22 +02:00
}
bool ExportDialog::checkFile()
{
2016-05-25 23:27:07 +02:00
if (_fileSelect->file().isEmpty()) {
2016-05-24 03:01:22 +02:00
QMessageBox::warning(this, tr("Error"), tr("No output file selected."));
return false;
}
2016-05-25 23:27:07 +02:00
QFile file(_fileSelect->file());
QFileInfo fi(file);
bool exists = fi.exists();
bool opened = false;
2016-05-24 03:01:22 +02:00
2016-05-25 23:27:07 +02:00
if (exists && fi.isDir()) {
QMessageBox::warning(this, tr("Error"), tr("%1 is a directory.")
.arg(file.fileName()));
2016-05-24 03:01:22 +02:00
return false;
2016-05-25 23:27:07 +02:00
} else if ((exists && !fi.isWritable())
|| !(opened = file.open(QFile::Append))) {
QMessageBox::warning(this, tr("Error"), tr("%1 is not writable.")
.arg(file.fileName()));
2016-05-24 03:01:22 +02:00
return false;
}
2016-05-25 23:27:07 +02:00
if (opened) {
file.close();
if (!exists)
file.remove();
}
2016-05-24 03:01:22 +02:00
return true;
}
void ExportDialog::accept()
{
if (!checkFile())
return;
2016-05-25 23:27:07 +02:00
QPrinter::Orientation orientation = _portrait->isChecked()
? QPrinter::Portrait : QPrinter::Landscape;
2016-05-24 03:01:22 +02:00
QPrinter::PaperSize paperSize = static_cast<QPrinter::PaperSize>
(_paperSize->itemData(_paperSize->currentIndex()).toInt());
int resolution = _resolution->itemData(_resolution->currentIndex()).toInt();
2016-05-24 03:01:22 +02:00
2016-12-06 01:48:26 +01:00
_export->fileName = _fileSelect->file();
_export->paperSize = paperSize;
_export->resolution = resolution;
2016-12-06 01:48:26 +01:00
_export->orientation = orientation;
2016-12-07 21:38:36 +01:00
if (_export->units == Imperial)
2016-12-06 01:48:26 +01:00
_export->margins = MarginsF(_leftMargin->value() / MM2IN,
_topMargin->value() / MM2IN, _rightMargin->value() / MM2IN,
_bottomMargin->value() / MM2IN);
else
_export->margins = MarginsF(_leftMargin->value(), _topMargin->value(),
_rightMargin->value(), _bottomMargin->value());
2016-05-24 03:01:22 +02:00
QDialog::accept();
}