1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-06 23:03:22 +02:00
GPXSee/src/exportdialog.cpp

175 lines
5.4 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>
#include <QLocale>
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"
ExportDialog::ExportDialog(QPrinter *printer, QWidget *parent)
: QDialog(parent), _printer(printer)
{
int index;
2016-05-27 22:45:58 +02:00
_units = (QLocale::system().measurementSystem()
== QLocale::ImperialSystem) ? QPrinter::Inch : QPrinter::Millimeter;
2016-05-24 03:01:22 +02:00
_fileSelect = new FileSelectWidget();
_fileSelect->setFilter(tr("PDF files (*.pdf);;All files (*)"));
2016-05-25 23:27:07 +02:00
_fileSelect->setFile(_printer->outputFileName());
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-05-25 23:27:07 +02:00
if ((index = _paperSize->findData(_printer->paperSize())) >= 0)
_paperSize->setCurrentIndex(index);
_portrait = new QRadioButton(tr("Portrait"));
_landscape = new QRadioButton(tr("Landscape"));
QHBoxLayout *orientationLayout = new QHBoxLayout();
orientationLayout->addWidget(_portrait);
orientationLayout->addWidget(_landscape);
if (_printer->orientation() == QPrinter::Portrait)
_portrait->setChecked(true);
else
_landscape->setChecked(true);
2016-05-27 22:45:58 +02:00
qreal top, bottom, left, right;
_printer->getPageMargins(&left, &top, &right, &bottom, _units);
QString us = _units == QPrinter::Inch ? tr("in") : tr("mm");
_topMargin = new QDoubleSpinBox();
_bottomMargin = new QDoubleSpinBox();
_leftMargin = new QDoubleSpinBox();
_rightMargin = new QDoubleSpinBox();
_topMargin->setValue(top);
_topMargin->setSuffix(UNIT_SPACE + us);
_bottomMargin->setValue(bottom);
_bottomMargin->setSuffix(UNIT_SPACE + us);
_leftMargin->setValue(left);
_leftMargin->setSuffix(UNIT_SPACE + us);
_rightMargin->setValue(right);
_rightMargin->setSuffix(UNIT_SPACE + us);
if (_units == QPrinter::Inch) {
_topMargin->setSingleStep(0.1);
_bottomMargin->setSingleStep(0.1);
_leftMargin->setSingleStep(0.1);
_rightMargin->setSingleStep(0.1);
}
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("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);
#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());
_printer->setOutputFormat(QPrinter::PdfFormat);
_printer->setOutputFileName(_fileSelect->file());
_printer->setPaperSize(paperSize);
_printer->setOrientation(orientation);
2016-05-27 22:45:58 +02:00
_printer->setPageMargins(_leftMargin->value(), _topMargin->value(),
_rightMargin->value(), _bottomMargin->value(), _units);
2016-05-24 03:01:22 +02:00
QDialog::accept();
}