Skip to content

Commit

Permalink
Merge branch 'nvdl-export-native-dialog'
Browse files Browse the repository at this point in the history
Close #290
  • Loading branch information
in3otd committed Jul 17, 2015
2 parents a16372c + 9ab1d11 commit b9ae939
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 38 deletions.
70 changes: 40 additions & 30 deletions qucs/qucs/dialogs/exportdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ ExportDialog::ExportDialog(int w, int h, int wsel, int hsel, QString filename_,
filename = filename_;

lblFilename = new QLabel(tr("Save to file (Graphics format by extension)"));
lblResolutionX = new QLabel(tr("Width in pixels"));
lblResolutionX = new QLabel(tr("Width in pixels"));
lblResolutionY = new QLabel(tr("Height in pixels"));
lblRatio = new QLabel(tr("Scale factor: "));
lblFormat = new QLabel(tr("Image format:"));
Expand All @@ -52,7 +52,7 @@ ExportDialog::ExportDialog(int w, int h, int wsel, int hsel, QString filename_,
connect(ExportButt,SIGNAL(clicked()),this,SLOT(accept()));
CancelButt = new QPushButton(tr("Cancel"));
connect(CancelButt,SIGNAL(clicked()),this,SLOT(reject()));
SaveButt = new QPushButton(tr("File"));
SaveButt = new QPushButton(tr("Browse"));
connect(SaveButt,SIGNAL(clicked()),this,SLOT(setFileName()));

editFilename = new QLineEdit(filename);
Expand Down Expand Up @@ -156,35 +156,45 @@ int ExportDialog::Ypixels()

void ExportDialog::setFileName()
{
/*QString nam = dialog.getSaveFileName(this,tr("Export diagram to file"),QDir::homeDirPath(),
"SVG vector graphics (*.svg) ;;"
"PNG images (*.png) ;;"
"JPEG images (*.jpg *.jpeg)");
*/
//QFileInfo inf(filename);
QFileDialog dialog(this, tr("Export to image"), editFilename->text(),
"SVG vector graphics (*.svg) ;;"
"PNG images (*.png) ;;"
"JPEG images (*.jpg *.jpeg) ;;"
"PDF (*.pdf) ;;"
"PDF + LaTeX (*.pdf_tex) ;;"
"EPS Encapsulated Postscript (*.eps)");
dialog.setAcceptMode(QFileDialog::AcceptSave);
if(dialog.exec())
{
QString nam = dialog.selectedFile();
QString extension;
if(dialog.selectedNameFilter().contains("*.png")) extension=QString(".png");
if(dialog.selectedNameFilter().contains("*.jpg")) extension=QString(".jpg");
if(dialog.selectedNameFilter().contains("*.svg")) extension=QString(".svg");
if(dialog.selectedNameFilter().contains("*.pdf")) extension=QString(".pdf");
if(dialog.selectedNameFilter().contains("*.pdf_tex")) extension=QString(".pdf_tex");
if(dialog.selectedNameFilter().contains("*.eps")) extension=QString(".eps");
if(nam.toLower().section("/",-1,-1).contains(".")) //has the user specified an extension?
editFilename->setText(nam); //yes, just leave unchanged
else
editFilename->setText(nam+extension); //no, add extension
QString selectedFilter;
QString currentExtension;
QString filterExtension;

QString fileName = QFileDialog::getSaveFileName(this, tr("Export Schematic to Image"),
editFilename->text(),
"PNG images (*.png);;"
"JPEG images (*.jpg *.jpeg);;"
"SVG vector graphics (*.svg);;"
"PDF (*.pdf);;"
"PDF + LaTeX (*.pdf_tex);;"
"EPS Encapsulated Postscript (*.eps)",
&selectedFilter);

if (fileName.isEmpty()) return;

if (selectedFilter.contains("*.png", Qt::CaseInsensitive)) filterExtension = QString(".png");
if (selectedFilter.contains("*.jpg", Qt::CaseInsensitive)) filterExtension = QString(".jpg");
if (selectedFilter.contains("*.svg", Qt::CaseInsensitive)) filterExtension = QString(".svg");
if (selectedFilter.contains("*.pdf", Qt::CaseInsensitive)) filterExtension = QString(".pdf");
if (selectedFilter.contains("*.pdf_tex", Qt::CaseInsensitive)) filterExtension = QString(".pdf_tex");
if (selectedFilter.contains("*.eps", Qt::CaseInsensitive)) filterExtension = QString(".eps");

QFileInfo fileInfo(fileName);

currentExtension = fileInfo.suffix();

QString allowedExtensions = "png;jpg;jpeg;svg;pdf;pdf_tex;eps";
QStringList extensionsList = allowedExtensions.split (';');

if (currentExtension.isEmpty()) {
fileName.append(filterExtension);
} else {
if (!extensionsList.contains(currentExtension)) {
fileName.append(filterExtension);
}
}

editFilename->setText(fileName);
}

void ExportDialog::calcWidth()
Expand Down
14 changes: 10 additions & 4 deletions qucs/qucs/imagewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <QtSvg>


ImageWriter::ImageWriter(QString lastfile)
{
onlyDiagram = false;
Expand Down Expand Up @@ -124,14 +125,15 @@ QString ImageWriter::getLastSavedFile()
return lastExportFilename;
}

void
ImageWriter::print(QWidget *doc)
// FIXME: should check if filename exists and not silently overwrite
int ImageWriter::print(QWidget *doc)
{
Schematic *sch = static_cast<Schematic*>(doc);
const int border = 30;

int w,h,wsel,hsel,
xmin, ymin, xmin_sel, ymin_sel;
int status = -1;

sch->getSchWidthAndHeight(w, h, xmin, ymin);
sch->getSelAreaWidthAndHeight(wsel, hsel, xmin_sel, ymin_sel);
Expand Down Expand Up @@ -256,18 +258,22 @@ ImageWriter::print(QWidget *doc)
}

if (QFile::exists(filename)) {
QMessageBox::information(0, QObject::tr("Export to image"),
QObject::tr("Successfully exported"), QMessageBox::Ok);
//QMessageBox::information(0, QObject::tr("Export to image"),
// QObject::tr("Successfully exported"), QMessageBox::Ok);
status = 0;
}
else {
QMessageBox::information(0, QObject::tr("Export to image"),
QObject::tr("Disk write error!"), QMessageBox::Ok);
status = -1;
}
} else {
QMessageBox::critical(0, QObject::tr("Export to image"),
QObject::tr("Unsupported format of graphics file. \n"
"Use PNG, JPEG or SVG graphics!"), QMessageBox::Ok);
status = -1;
}
}
delete dlg;
return status;
}
2 changes: 1 addition & 1 deletion qucs/qucs/imagewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ImageWriter
public:
ImageWriter (QString lastfile);
virtual ~ImageWriter ();
void print(QWidget *);
int print(QWidget *);
void noGuiPrint(QWidget *, QString printFile, QString color);

QString getLastSavedFile();
Expand Down
6 changes: 4 additions & 2 deletions qucs/qucs/qucs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2793,7 +2793,9 @@ void QucsApp::slotSaveSchematicToGraphicsFile(bool diagram)
{
ImageWriter *writer = new ImageWriter(lastExportFilename);
writer->setDiagram(diagram);
writer->print(DocumentTab->currentWidget());
lastExportFilename = writer->getLastSavedFile();
if (!writer->print(DocumentTab->currentWidget())) {
lastExportFilename = writer->getLastSavedFile();
statusBar()->showMessage(QObject::tr("Successfully exported"), 2000);
}
delete writer;
}
4 changes: 3 additions & 1 deletion qucs/qucs/qucs_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,10 @@ void QucsApp::initActions()
//mainAccel->connectItem(mainAccel->insertItem(Qt::Key_Backspace),
// editDelete, SLOT(toggle()) );

exportAsImage = new QAction(tr("Export as image"),this);
exportAsImage = new QAction(tr("Export as image..."),this);
connect(exportAsImage,SIGNAL(triggered()),SLOT(slotSaveSchematicToGraphicsFile()));
exportAsImage->setStatusTip(tr("Exports the current document to an image file"));
exportAsImage->setWhatsThis(tr("Export as image\n\nExports the current document to an image file"));

// cursor left/right/up/down to move marker on a graph
cursorLeft = new QShortcut(QKeySequence(Qt::Key_Left), this);
Expand Down

0 comments on commit b9ae939

Please sign in to comment.