mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-19 23:20:52 +01:00
Fichier > exporter permet desormais d'enregistrer les schemas au format SVG.
git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@159 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
11
diagram.cpp
11
diagram.cpp
@@ -127,7 +127,7 @@ void Diagram::keyReleaseEvent(QKeyEvent *e) {
|
|||||||
Exporte le schema vers une image
|
Exporte le schema vers une image
|
||||||
@return Une QImage representant le schema
|
@return Une QImage representant le schema
|
||||||
*/
|
*/
|
||||||
QImage Diagram::toImage(int width, int height, Qt::AspectRatioMode aspectRatioMode) {
|
bool Diagram::toPaintDevice(QPaintDevice &pix, int width, int height, Qt::AspectRatioMode aspectRatioMode) {
|
||||||
// determine la zone source = contenu du schema + marges
|
// determine la zone source = contenu du schema + marges
|
||||||
QRectF source_area;
|
QRectF source_area;
|
||||||
if (!use_border) {
|
if (!use_border) {
|
||||||
@@ -147,12 +147,9 @@ QImage Diagram::toImage(int width, int height, Qt::AspectRatioMode aspectRatioMo
|
|||||||
// si les dimensions ne sont pas precisees, l'image est exportee a l'echelle 1:1
|
// si les dimensions ne sont pas precisees, l'image est exportee a l'echelle 1:1
|
||||||
QSize image_size = (width == -1 && height == -1) ? source_area.size().toSize() : QSize(width, height);
|
QSize image_size = (width == -1 && height == -1) ? source_area.size().toSize() : QSize(width, height);
|
||||||
|
|
||||||
// initialise une image avec ces dimensions
|
|
||||||
QImage pix = QImage(image_size, QImage::Format_RGB32);
|
|
||||||
|
|
||||||
// prepare le rendu
|
// prepare le rendu
|
||||||
QPainter p;
|
QPainter p;
|
||||||
if (!p.begin(&pix)) return(QImage());
|
if (!p.begin(&pix)) return(false);
|
||||||
|
|
||||||
// rendu antialiase
|
// rendu antialiase
|
||||||
p.setRenderHint(QPainter::Antialiasing, true);
|
p.setRenderHint(QPainter::Antialiasing, true);
|
||||||
@@ -164,13 +161,13 @@ QImage Diagram::toImage(int width, int height, Qt::AspectRatioMode aspectRatioMo
|
|||||||
foreach (QGraphicsItem *qgi, selected_elmts) qgi -> setSelected(false);
|
foreach (QGraphicsItem *qgi, selected_elmts) qgi -> setSelected(false);
|
||||||
|
|
||||||
// effectue le rendu lui-meme
|
// effectue le rendu lui-meme
|
||||||
render(&p, pix.rect(), source_area, aspectRatioMode);
|
render(&p, QRect(QPoint(0, 0), image_size), source_area, aspectRatioMode);
|
||||||
p.end();
|
p.end();
|
||||||
|
|
||||||
// restaure les elements selectionnes
|
// restaure les elements selectionnes
|
||||||
foreach (QGraphicsItem *qgi, selected_elmts) qgi -> setSelected(true);
|
foreach (QGraphicsItem *qgi, selected_elmts) qgi -> setSelected(true);
|
||||||
|
|
||||||
return(pix);
|
return(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ class Diagram : public QGraphicsScene {
|
|||||||
void setDrawTerminals(bool);
|
void setDrawTerminals(bool);
|
||||||
|
|
||||||
QRectF border() const;
|
QRectF border() const;
|
||||||
QImage toImage(int = -1, int = -1, Qt::AspectRatioMode = Qt::KeepAspectRatio);
|
bool toPaintDevice(QPaintDevice &, int = -1, int = -1, Qt::AspectRatioMode = Qt::KeepAspectRatio);
|
||||||
QSize imageSize() const;
|
QSize imageSize() const;
|
||||||
|
|
||||||
void invalidateMovedElements();
|
void invalidateMovedElements();
|
||||||
|
|||||||
112
exportdialog.cpp
112
exportdialog.cpp
@@ -1,4 +1,6 @@
|
|||||||
#include "exportdialog.h"
|
#include "exportdialog.h"
|
||||||
|
#include <QSvgGenerator>
|
||||||
|
#include <QtXml>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Constructeur
|
Constructeur
|
||||||
@@ -14,6 +16,9 @@ ExportDialog::ExportDialog(Diagram *dia, QWidget *parent) : QDialog(parent) {
|
|||||||
diagram_ratio = (qreal)diagram_size.width() / (qreal)diagram_size.height();
|
diagram_ratio = (qreal)diagram_size.width() / (qreal)diagram_size.height();
|
||||||
dontchangewidth = dontchangeheight = false;
|
dontchangewidth = dontchangeheight = false;
|
||||||
|
|
||||||
|
// vecteur contenant 256 nuances de gris, pour exporter les images sans couleur
|
||||||
|
for (int i = 0 ; i < 256 ; ++ i) ColorTab << qRgb(i, i, i);
|
||||||
|
|
||||||
// la taille du dialogue est fixee
|
// la taille du dialogue est fixee
|
||||||
setFixedSize(800, 360);
|
setFixedSize(800, 360);
|
||||||
setWindowTitle(tr("Exporter"));
|
setWindowTitle(tr("Exporter"));
|
||||||
@@ -174,6 +179,7 @@ QWidget *ExportDialog::leftPart() {
|
|||||||
format -> addItem(tr("PNG (*.png)"), "PNG");
|
format -> addItem(tr("PNG (*.png)"), "PNG");
|
||||||
format -> addItem(tr("JPEG (*.jpg)"), "JPG");
|
format -> addItem(tr("JPEG (*.jpg)"), "JPG");
|
||||||
format -> addItem(tr("Bitmap (*.bmp)"), "BMP");
|
format -> addItem(tr("Bitmap (*.bmp)"), "BMP");
|
||||||
|
format -> addItem(tr("SVG (*.svg)"), "SVG");
|
||||||
|
|
||||||
vboxLayout -> addLayout(hboxLayout1);
|
vboxLayout -> addLayout(hboxLayout1);
|
||||||
|
|
||||||
@@ -248,7 +254,7 @@ void ExportDialog::slot_chooseAFile() {
|
|||||||
this,
|
this,
|
||||||
tr("Exporter vers le fichier"),
|
tr("Exporter vers le fichier"),
|
||||||
QDir::homePath(),
|
QDir::homePath(),
|
||||||
tr("Images (*.png *.bmp *.jpg)")
|
tr("Images (*.png *.bmp *.jpg *.svg)")
|
||||||
);
|
);
|
||||||
if (user_file != "") {
|
if (user_file != "") {
|
||||||
diagram_path = user_file;
|
diagram_path = user_file;
|
||||||
@@ -261,22 +267,11 @@ void ExportDialog::slot_chooseAFile() {
|
|||||||
@return l'image a exporter
|
@return l'image a exporter
|
||||||
*/
|
*/
|
||||||
QImage ExportDialog::generateImage() {
|
QImage ExportDialog::generateImage() {
|
||||||
// memorise les parametres relatifs au schema
|
saveReloadDiagramParameters(true);
|
||||||
bool state_drawBorder = diagram -> border_and_inset.borderIsDisplayed();
|
|
||||||
bool state_drawColumns = diagram -> border_and_inset.columnsAreDisplayed();
|
|
||||||
bool state_drawInset = diagram -> border_and_inset.insetIsDisplayed();
|
|
||||||
bool state_drawGrid = diagram -> displayGrid();
|
|
||||||
bool state_drawTerm = diagram -> drawTerminals();
|
|
||||||
bool state_useBorder = diagram -> useBorder();
|
|
||||||
|
|
||||||
// genere l'image
|
QImage image(width -> value(), height -> value(), QImage::Format_RGB32);
|
||||||
diagram -> setUseBorder(export_border -> isChecked());
|
diagram -> toPaintDevice(
|
||||||
diagram -> setDrawTerminals(draw_terminals -> isChecked());
|
image,
|
||||||
diagram -> setDisplayGrid(draw_grid -> isChecked());
|
|
||||||
diagram -> border_and_inset.displayBorder(draw_border -> isChecked());
|
|
||||||
diagram -> border_and_inset.displayColumns(draw_columns -> isChecked());
|
|
||||||
diagram -> border_and_inset.displayInset(draw_inset -> isChecked());
|
|
||||||
QImage image = diagram -> toImage(
|
|
||||||
width -> value(),
|
width -> value(),
|
||||||
height -> value(),
|
height -> value(),
|
||||||
keep_aspect_ratio -> isChecked() ? Qt::KeepAspectRatio : Qt::IgnoreAspectRatio
|
keep_aspect_ratio -> isChecked() ? Qt::KeepAspectRatio : Qt::IgnoreAspectRatio
|
||||||
@@ -284,22 +279,79 @@ QImage ExportDialog::generateImage() {
|
|||||||
|
|
||||||
// convertit l'image en niveaux de gris si besoin
|
// convertit l'image en niveaux de gris si besoin
|
||||||
if (!keep_colors -> isChecked()) {
|
if (!keep_colors -> isChecked()) {
|
||||||
QVector<QRgb> ColorTab;
|
|
||||||
for (int i = 0 ; i < 256 ; ++ i) ColorTab << qRgb(i, i, i);
|
|
||||||
image = image.convertToFormat(QImage::Format_Indexed8, ColorTab, Qt::ThresholdDither);
|
image = image.convertToFormat(QImage::Format_Indexed8, ColorTab, Qt::ThresholdDither);
|
||||||
}
|
}
|
||||||
|
|
||||||
// restaure les parametres relatifs au schema
|
saveReloadDiagramParameters(false);
|
||||||
diagram -> border_and_inset.displayBorder(state_drawBorder);
|
|
||||||
diagram -> border_and_inset.displayColumns(state_drawColumns);
|
|
||||||
diagram -> border_and_inset.displayInset(state_drawInset);
|
|
||||||
diagram -> setDisplayGrid(state_drawGrid);
|
|
||||||
diagram -> setDrawTerminals(state_drawTerm);
|
|
||||||
diagram -> setUseBorder(state_useBorder);
|
|
||||||
|
|
||||||
return(image);
|
return(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Sauve ou restaure les parametres du schema
|
||||||
|
@param save true pour memoriser les parametres du schema et appliquer ceux
|
||||||
|
definis par le formulaire, false pour restaurer les parametres
|
||||||
|
*/
|
||||||
|
void ExportDialog::saveReloadDiagramParameters(bool save) {
|
||||||
|
static bool state_drawBorder;
|
||||||
|
static bool state_drawColumns;
|
||||||
|
static bool state_drawInset;
|
||||||
|
static bool state_drawGrid;
|
||||||
|
static bool state_drawTerm;
|
||||||
|
static bool state_useBorder;
|
||||||
|
|
||||||
|
if (save) {
|
||||||
|
// memorise les parametres relatifs au schema
|
||||||
|
state_drawBorder = diagram -> border_and_inset.borderIsDisplayed();
|
||||||
|
state_drawColumns = diagram -> border_and_inset.columnsAreDisplayed();
|
||||||
|
state_drawInset = diagram -> border_and_inset.insetIsDisplayed();
|
||||||
|
state_drawGrid = diagram -> displayGrid();
|
||||||
|
state_drawTerm = diagram -> drawTerminals();
|
||||||
|
state_useBorder = diagram -> useBorder();
|
||||||
|
|
||||||
|
diagram -> setUseBorder(export_border -> isChecked());
|
||||||
|
diagram -> setDrawTerminals(draw_terminals -> isChecked());
|
||||||
|
diagram -> setDisplayGrid(draw_grid -> isChecked());
|
||||||
|
diagram -> border_and_inset.displayBorder(draw_border -> isChecked());
|
||||||
|
diagram -> border_and_inset.displayColumns(draw_columns -> isChecked());
|
||||||
|
diagram -> border_and_inset.displayInset(draw_inset -> isChecked());
|
||||||
|
} else {
|
||||||
|
// restaure les parametres relatifs au schema
|
||||||
|
diagram -> border_and_inset.displayBorder(state_drawBorder);
|
||||||
|
diagram -> border_and_inset.displayColumns(state_drawColumns);
|
||||||
|
diagram -> border_and_inset.displayInset(state_drawInset);
|
||||||
|
diagram -> setDisplayGrid(state_drawGrid);
|
||||||
|
diagram -> setDrawTerminals(state_drawTerm);
|
||||||
|
diagram -> setUseBorder(state_useBorder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Exporte le schema en SVG
|
||||||
|
@param file Fichier dans lequel sera enregistre le code SVG
|
||||||
|
*/
|
||||||
|
void ExportDialog::generateSvg(QFile &file) {
|
||||||
|
saveReloadDiagramParameters(true);
|
||||||
|
|
||||||
|
// genere une QPicture a partir du schema
|
||||||
|
QPicture picture;
|
||||||
|
diagram -> toPaintDevice(
|
||||||
|
picture,
|
||||||
|
width -> value(),
|
||||||
|
height -> value(),
|
||||||
|
keep_aspect_ratio -> isChecked() ? Qt::KeepAspectRatio : Qt::IgnoreAspectRatio
|
||||||
|
);
|
||||||
|
|
||||||
|
// "joue" la QPicture sur un QSvgGenerator
|
||||||
|
QSvgGenerator svg_engine;
|
||||||
|
svg_engine.setSize(QSize(width -> value(), height -> value()));
|
||||||
|
svg_engine.setOutputDevice(&file);
|
||||||
|
QPainter svg_painter(&svg_engine);
|
||||||
|
picture.play(&svg_painter);
|
||||||
|
/// @todo gerer l'enlevement des couleurs
|
||||||
|
saveReloadDiagramParameters(false);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Slot effectuant les verifications necessaires apres la validation du
|
Slot effectuant les verifications necessaires apres la validation du
|
||||||
dialogue.
|
dialogue.
|
||||||
@@ -341,17 +393,19 @@ void ExportDialog::slot_check() {
|
|||||||
// ouvre le fichier
|
// ouvre le fichier
|
||||||
QFile fichier(diagram_path);
|
QFile fichier(diagram_path);
|
||||||
|
|
||||||
QImage image = generateImage();
|
|
||||||
|
|
||||||
// enregistre l'image dans le fichier
|
// enregistre l'image dans le fichier
|
||||||
image.save(&fichier, format_acronym.toUtf8().data());
|
if (format_acronym == "SVG") {
|
||||||
|
generateSvg(fichier);
|
||||||
|
} else {
|
||||||
|
QImage image = generateImage();
|
||||||
|
image.save(&fichier, format_acronym.toUtf8().data());
|
||||||
|
}
|
||||||
fichier.close();
|
fichier.close();
|
||||||
|
|
||||||
// fermeture du dialogue
|
// fermeture du dialogue
|
||||||
accept();
|
accept();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Slot appele lorsque l'utilisateur change la zone du schema qui doit etre
|
Slot appele lorsque l'utilisateur change la zone du schema qui doit etre
|
||||||
exportee. Il faut alors calculer le nouveau ratio et corriger les
|
exportee. Il faut alors calculer le nouveau ratio et corriger les
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#define EXPORTDIALOG_H
|
#define EXPORTDIALOG_H
|
||||||
#include <QtGui>
|
#include <QtGui>
|
||||||
#include "diagram.h"
|
#include "diagram.h"
|
||||||
|
class QSvgGenerator;
|
||||||
/**
|
/**
|
||||||
Cette classe represente le dialogue permettant d'exporter un schema
|
Cette classe represente le dialogue permettant d'exporter un schema
|
||||||
sous forme d'image selon les desirs de l'utilisateur
|
sous forme d'image selon les desirs de l'utilisateur
|
||||||
@@ -47,6 +48,7 @@ class ExportDialog : public QDialog {
|
|||||||
QSize diagram_size;
|
QSize diagram_size;
|
||||||
QString diagram_path;
|
QString diagram_path;
|
||||||
qreal diagram_ratio;
|
qreal diagram_ratio;
|
||||||
|
QVector<QRgb> ColorTab;
|
||||||
|
|
||||||
// methodes
|
// methodes
|
||||||
private:
|
private:
|
||||||
@@ -54,6 +56,8 @@ class ExportDialog : public QDialog {
|
|||||||
QWidget *rightPart();
|
QWidget *rightPart();
|
||||||
QGroupBox *setupDimensionsGroupBox();
|
QGroupBox *setupDimensionsGroupBox();
|
||||||
QGroupBox *setupOptionsGroupBox();
|
QGroupBox *setupOptionsGroupBox();
|
||||||
|
void saveReloadDiagramParameters(bool = true);
|
||||||
|
void generateSvg(QFile &file);
|
||||||
QImage generateImage();
|
QImage generateImage();
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ SOURCES += aboutqet.cpp \
|
|||||||
RESOURCES += qelectrotech.qrc
|
RESOURCES += qelectrotech.qrc
|
||||||
TRANSLATIONS += lang/qet_en.ts lang/qt_fr.ts
|
TRANSLATIONS += lang/qet_en.ts lang/qt_fr.ts
|
||||||
RC_FILE = ico/windows_icon/application_icon/qelectrotech.rc
|
RC_FILE = ico/windows_icon/application_icon/qelectrotech.rc
|
||||||
QT += xml
|
QT += xml svg
|
||||||
CONFIG += debug_and_release
|
CONFIG += debug_and_release
|
||||||
CONFIG(debug, debug|release) {
|
CONFIG(debug, debug|release) {
|
||||||
TARGET = qelectrotech
|
TARGET = qelectrotech
|
||||||
|
|||||||
Reference in New Issue
Block a user