Commit 51585312 authored by Peter Zhigalov's avatar Peter Zhigalov

Добавил возможность сохранять и в PDF

parent dbcfb661
......@@ -12,7 +12,7 @@ QT += core gui svg
INCLUDEPATH += src_qt
greaterThan(QT_MAJOR_VERSION, 4) {
QT += widgets
QT += widgets printsupport
DEFINES += HAVE_QT5
}
......
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QList>
#include <QVector>
#include <QSvgGenerator>
#include <QPrinter>
#include <algorithm>
// Конструктор
MainWindow::MainWindow(QWidget *parent) :
......@@ -183,60 +187,88 @@ void MainWindow::on_actionOpen_Tecplot_File_triggered()
// Событие при сохранении
void MainWindow::on_actionSave_Image_File_triggered()
{
QList<QByteArray> supported = QImageWriter::supportedImageFormats();
QList<QByteArray> supported_temp = QImageWriter::supportedImageFormats();
QVector<QString> supported;
for(QList<QByteArray>::const_iterator it = supported_temp.begin(); it != supported_temp.end(); ++it)
supported.push_back(QString(*it).toLower());
supported_temp.clear();
supported.push_back("svg");
// supported.push_back("ps");
supported.push_back("pdf");
bool qt_jpg = true;
if(std::find(supported.begin(), supported.end(), "jpg") == supported.end())
{
supported.push_back("jpg");
qt_jpg = false;
}
bool qt_gif = true;
if(std::find(supported.begin(), supported.end(), "gif") == supported.end())
{
supported.push_back("gif");
qt_gif = false;
}
bool qt_tga = true;
if(std::find(supported.begin(), supported.end(), "tga") == supported.end())
{
supported.push_back("tga");
qt_tga = false;
}
std::sort(supported.begin(), supported.end());
QString formats, formats_all;
for(QList<QByteArray>::const_iterator it = supported.begin(); it != supported.end(); ++it)
for(QVector<QString>::const_iterator it = supported.begin(); it != supported.end(); ++it)
{
QString record(* it);
if(formats_all.length() > 0)
formats_all.append(" *.");
else
formats_all.append("*.");
formats_all.append(record);
formats_all.append(*it);
if(formats.length() > 0)
formats.append(";;");
formats.append(record.toUpper());
formats.append(" Images (*.");
formats.append(record);
formats.append(it->toUpper());
formats.append(" ");
formats.append(trUtf8("Images"));
formats.append(" (*.");
formats.append(*it);
formats.append(")");
}
formats.append(";;SVG Images (*.svg)");
formats_all.append(" *.svg");
supported.push_back("svg");
formats_all.prepend("All Images (");
formats_all.append(");;");
formats.prepend(formats_all);
bool is_svg = false;
QString fileName = QFileDialog::getSaveFileName(this, trUtf8("Save Image File"), last_saved, formats);
if(fileName.length() == 0) return;
int found = fileName.lastIndexOf('.');
QString filename = QFileDialog::getSaveFileName(this, trUtf8("Save Image File"), last_saved, formats);
if(filename.length() == 0) return;
QString def_ext("png"), ext;
int found = filename.lastIndexOf('.');
if(found == -1)
{
fileName.append(".png");
filename.append(".");
filename.append(def_ext);
ext = def_ext;
}
else
{
QString ext = fileName.right(fileName.length() - found - 1);
bool finded = false;
for(QList<QByteArray>::const_iterator it = supported.begin(); it != supported.end() && !finded; ++it)
ext = filename.right(filename.length() - found - 1).toLower();
if(std::find(supported.begin(), supported.end(), ext) == supported.end())
{
if(ext.compare(QString(* it), Qt::CaseInsensitive) == 0)
finded = true;
filename.append(".");
filename.append(def_ext);
ext = def_ext;
}
if(!finded)
fileName.append(".png");
if(ext.compare("svg", Qt::CaseInsensitive) == 0)
is_svg = true;
}
last_saved = fileName;
last_saved = filename;
bool saved = false;
if(is_svg)
if(ext == "svg")
{
QSvgGenerator generator;
generator.setFileName(fileName);
generator.setFileName(filename);
generator.setSize(QSize(ui->widget->width(), ui->widget->height()));
#if !defined HAVE_LESS_THAN_QT45
generator.setViewBox(QRect(0, 0, ui->widget->width(), ui->widget->height()));
......@@ -245,12 +277,37 @@ void MainWindow::on_actionSave_Image_File_triggered()
#endif
ui->widget->draw(& generator, ui->actionTransparent_Image->isChecked(), true);
saved = true;
}
else if(ext == "ps" || ext == "pdf")
{
QPrinter printer(QPrinter::ScreenResolution);
if(ext == "pdf")
printer.setOutputFormat(QPrinter::PdfFormat);
// else if(ext == "ps")
// printer.setOutputFormat(QPrinter::PostScriptFormat);
printer.setOutputFileName(filename);
printer.setPaperSize(QSizeF(ui->widget->width(), ui->widget->height()), QPrinter::DevicePixel);
printer.setPageMargins(0, 0, 0, 0, QPrinter::DevicePixel);
ui->widget->draw(& printer, ui->actionTransparent_Image->isChecked(), true);
saved = true;
}
else if(ext == "jpg" && !qt_jpg)
{
}
else if(ext == "gif" && !qt_gif)
{
}
else if(ext == "tga" && !qt_tga)
{
}
else
{
QImage image(ui->widget->width(), ui->widget->height(), QImage::Format_ARGB32_Premultiplied);
ui->widget->draw(& image, ui->actionTransparent_Image->isChecked());
saved = image.save(fileName);
saved = image.save(filename);
}
if(!saved)
......
......@@ -983,51 +983,6 @@ void paintwidget::draw(QPaintDevice * device, bool transparency, bool is_svg)
painter.drawLine(to_window(-0.01f, y), to_window(1.0f, y));
}
// Координатные оси
painter.setPen(QPen(Qt::black, 2.0f));
painter.drawLine(to_window(0.0f, -0.005f), to_window(0.0f, 1.005f));
painter.drawLine(to_window(-0.005f, 0.0f), to_window(1.005f, 0.0f));
// Шрифты
QFont fnt_mono("Courier");
fnt_mono.setLetterSpacing(QFont::PercentageSpacing, 82);
int fnt_mono_h = height() / 45;
int fnt_mono_w = width() / 55;
fnt_mono.setPixelSize(fnt_mono_h < fnt_mono_w ? fnt_mono_h : fnt_mono_w);
QFont fnt_serif("Times");
int fnt_serif_h = height() / 40;
int fnt_serif_w = width() / 45;
fnt_serif.setPixelSize(fnt_serif_h < fnt_serif_w ? fnt_serif_h : fnt_serif_w);
fnt_serif.setBold(true);
#if defined HAVE_LESS_THAN_QT45
fnt_mono.setPointSizeF(qreal(fnt_mono.pixelSize()) * qreal(0.75));
fnt_serif.setPointSizeF(qreal(fnt_serif.pixelSize()) * qreal(0.75));
#endif
// Подписи осей
painter.setFont(fnt_serif);
painter.setPen(QPen(Qt::black, 1.0f));
painter.drawText(to_window(0.99f, -0.04f), label_x);
painter.drawText(to_window(-0.05f, 0.99f), label_y);
// Отрисовка шкалы
painter.setFont(fnt_mono);
for(size_t i = 0; i < num_ticks_x; i++)
{
float x = (float)i / (float)num_ticks_x;
float x_real = (float)(std::floor((x * size_x + min_x) * 10000.0f + 0.5f)) / 10000.0f;
QString st = QString::number(x_real);
painter.drawText(to_window(x - 0.01f, -0.04f), st);
}
for(size_t i = 0; i < num_ticks_y; i++)
{
float y = (float)i / (float)num_ticks_y;
float y_real = (float)(std::floor((y * size_y + min_y) * 10000.0f + 0.5f)) / 10000.0f;
QString st = QString::number(y_real);
painter.drawText(to_window(-0.05f, y - 0.01f), st);
}
// Раскрашивать будем если запрошено сие
if(draw_color)
{
......@@ -1239,6 +1194,51 @@ void paintwidget::draw(QPaintDevice * device, bool transparency, bool is_svg)
}
}
// Координатные оси
painter.setPen(QPen(Qt::black, 2.0f));
painter.drawLine(to_window(0.0f, -0.005f), to_window(0.0f, 1.005f));
painter.drawLine(to_window(-0.005f, 0.0f), to_window(1.005f, 0.0f));
// Шрифты
QFont fnt_mono("Courier");
fnt_mono.setLetterSpacing(QFont::PercentageSpacing, 82);
int fnt_mono_h = height() / 45;
int fnt_mono_w = width() / 55;
fnt_mono.setPixelSize(fnt_mono_h < fnt_mono_w ? fnt_mono_h : fnt_mono_w);
QFont fnt_serif("Times");
int fnt_serif_h = height() / 40;
int fnt_serif_w = width() / 45;
fnt_serif.setPixelSize(fnt_serif_h < fnt_serif_w ? fnt_serif_h : fnt_serif_w);
fnt_serif.setBold(true);
#if defined HAVE_LESS_THAN_QT45
fnt_mono.setPointSizeF(qreal(fnt_mono.pixelSize()) * qreal(0.75));
fnt_serif.setPointSizeF(qreal(fnt_serif.pixelSize()) * qreal(0.75));
#endif
// Подписи осей
painter.setFont(fnt_serif);
painter.setPen(QPen(Qt::black, 1.0f));
painter.drawText(to_window(0.99f, -0.04f), label_x);
painter.drawText(to_window(-0.05f, 0.99f), label_y);
// Отрисовка шкалы
painter.setFont(fnt_mono);
for(size_t i = 0; i < num_ticks_x; i++)
{
float x = (float)i / (float)num_ticks_x;
float x_real = (float)(std::floor((x * size_x + min_x) * 10000.0f + 0.5f)) / 10000.0f;
QString st = QString::number(x_real);
painter.drawText(to_window(x - 0.01f, -0.04f), st);
}
for(size_t i = 0; i < num_ticks_y; i++)
{
float y = (float)i / (float)num_ticks_y;
float y_real = (float)(std::floor((y * size_y + min_y) * 10000.0f + 0.5f)) / 10000.0f;
QString st = QString::number(y_real);
painter.drawText(to_window(-0.05f, y - 0.01f), st);
}
// Легенда
if(use_legend)
{
......
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