leocad/qt/lc_qimagedialog.cpp

121 lines
2.9 KiB
C++
Raw Normal View History

2013-08-09 06:57:18 +02:00
#include "lc_global.h"
#include "lc_qimagedialog.h"
#include "ui_lc_qimagedialog.h"
2013-08-16 03:25:51 +02:00
#include "lc_basewindow.h"
2013-08-09 06:57:18 +02:00
lcQImageDialog::lcQImageDialog(QWidget *parent, void *data) :
QDialog(parent),
ui(new Ui::lcQImageDialog)
{
ui->setupUi(this);
ui->width->setValidator(new QIntValidator(1, 2048));
ui->height->setValidator(new QIntValidator(1, 2048));
2014-10-12 01:26:23 +02:00
ui->firstStep->setValidator(new QIntValidator(1, options->End));
ui->lastStep->setValidator(new QIntValidator(1, options->End));
2013-08-09 06:57:18 +02:00
options = (lcImageDialogOptions*)data;
2014-10-12 01:26:23 +02:00
currentStep = options->Start;
lastStep = options->End;
2013-08-09 06:57:18 +02:00
ui->fileName->setText(options->FileName);
ui->width->setText(QString::number(options->Width));
ui->height->setText(QString::number(options->Height));
2014-10-12 01:26:23 +02:00
ui->firstStep->setText(QString::number(1));
ui->lastStep->setText(QString::number(lastStep));
ui->rangeCurrent->setChecked(true);
2013-08-09 06:57:18 +02:00
}
lcQImageDialog::~lcQImageDialog()
{
delete ui;
}
void lcQImageDialog::accept()
{
QString fileName = ui->fileName->text();
if (fileName.isEmpty())
{
2014-10-12 01:26:23 +02:00
QMessageBox::information(this, tr("Error"), tr("Output File cannot be empty."));
2013-08-09 06:57:18 +02:00
return;
}
int width = ui->width->text().toInt();
if (width < 1 || width > 2048)
{
2014-10-12 01:26:23 +02:00
QMessageBox::information(this, tr("Error"), tr("Please enter a width between 1 and 2048."));
2013-08-09 06:57:18 +02:00
return;
}
int height = ui->height->text().toInt();
if (height < 1 || height > 2048)
{
2014-10-12 01:26:23 +02:00
QMessageBox::information(this, tr("Error"), tr("Please enter a height between 1 and 2048."));
2013-08-09 06:57:18 +02:00
return;
}
2014-10-12 01:26:23 +02:00
int start, end;
2013-08-09 06:57:18 +02:00
2014-10-12 01:26:23 +02:00
if (ui->rangeAll->isChecked())
2013-08-09 06:57:18 +02:00
{
2014-10-12 01:26:23 +02:00
start = 1;
end = lastStep;
2013-08-09 06:57:18 +02:00
}
2014-10-12 01:26:23 +02:00
else if (ui->rangeCurrent->isChecked())
2013-08-09 06:57:18 +02:00
{
2014-10-12 01:26:23 +02:00
start = currentStep;
end = currentStep;
2013-08-09 06:57:18 +02:00
}
2014-10-12 01:26:23 +02:00
else if (ui->rangeCustom->isChecked())
{
start = ui->firstStep->text().toInt();
if (start < 1 || start > lastStep)
{
QMessageBox::information(this, tr("Error"), tr("First step must be between 1 and %1.").arg(QString::number(lastStep)));
return;
}
end = ui->lastStep->text().toInt();
if (end < 1 || end > lastStep)
{
QMessageBox::information(this, tr("Error"), tr("Last step must be between 1 and %1.").arg(QString::number(lastStep)));
return;
}
if (end < start)
{
QMessageBox::information(this, tr("Error"), tr("Last step must be greater than first step."));
return;
}
}
QImageWriter writer(fileName);
2013-08-09 06:57:18 +02:00
2014-10-12 01:26:23 +02:00
if (!writer.canWrite())
2013-08-09 06:57:18 +02:00
{
2014-10-12 01:26:23 +02:00
QMessageBox::information(this, tr("Error"), tr("Cannot save '%1':").arg(writer.errorString()));
2013-08-09 06:57:18 +02:00
return;
}
2014-10-12 01:26:23 +02:00
options->FileName = fileName;
2013-08-09 06:57:18 +02:00
options->Width = width;
options->Height = height;
options->Start = start;
options->End = end;
QDialog::accept();
}
void lcQImageDialog::on_fileNameBrowse_clicked()
{
2014-10-12 01:26:23 +02:00
QString result = QFileDialog::getSaveFileName(this, tr("Save Image File"), ui->fileName->text(), tr("Supported Image Files (*.bmp *.png *.jpg);;BMP Files (*.bmp);;PNG Files (*.png);;JPEG Files (*.jpg);;All Files (*.*)"));
2013-08-09 06:57:18 +02:00
if (!result.isEmpty())
ui->fileName->setText(QDir::toNativeSeparators(result));
}