Skip to content

Commit

Permalink
- Use QImage instead of QPixmap while exporting to image.
Browse files Browse the repository at this point in the history
- Added tiff in the export as image option.
- Added copy to clipboard option.

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@23418 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adeas31 committed Nov 18, 2014
1 parent db2f024 commit 13e8a6f
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 63 deletions.
123 changes: 82 additions & 41 deletions OMEdit/OMEditGUI/MainWindow.cpp
Expand Up @@ -1608,19 +1608,21 @@ void MainWindow::importNgspiceNetlist()
}

//! Exports the current model as image
void MainWindow::exportModelAsImage()
void MainWindow::exportModelAsImage(bool copyToClipboard)
{
ModelWidget *pModelWidget = mpModelWidgetContainer->getCurrentModelWidget();
if (pModelWidget)
{
if (pModelWidget) {
LibraryTreeNode *pLibraryTreeNode = pModelWidget->getLibraryTreeNode();
if (pLibraryTreeNode)
{
QString fileName = StringHandler::getSaveFileName(this, QString(Helper::applicationName).append(" - ").append(Helper::exportAsImage),
NULL, Helper::imageFileTypes, NULL, "svg", &pLibraryTreeNode->getName());
// if user cancels the operation. or closes the export dialog box.
if (fileName.isEmpty())
return;
if (pLibraryTreeNode) {
QString fileName;
if (!copyToClipboard) {
fileName = StringHandler::getSaveFileName(this, QString(Helper::applicationName).append(" - ").append(Helper::exportAsImage),
NULL, Helper::imageFileTypes, NULL, "svg", &pLibraryTreeNode->getName());
// if user cancels the operation. or closes the export dialog box.
if (fileName.isEmpty()) {
return;
}
}
bool oldSkipDrawBackground;
// show the progressbar and set the message in status bar
mpProgressBar->setRange(0, 0);
Expand All @@ -1629,63 +1631,65 @@ void MainWindow::exportModelAsImage()
QPainter painter;
QSvgGenerator svgGenerator;
GraphicsView *pGraphicsView;
if (pLibraryTreeNode->getModelWidget()->getIconGraphicsView()->isVisible())
if (pLibraryTreeNode->getModelWidget()->getIconGraphicsView()->isVisible()) {
pGraphicsView = pLibraryTreeNode->getModelWidget()->getIconGraphicsView();
else
} else {
pGraphicsView = pLibraryTreeNode->getModelWidget()->getDiagramGraphicsView();
QPixmap modelImage(pGraphicsView->viewport()->size());
}
QRect destinationRect = pGraphicsView->itemsBoundingRect().toAlignedRect();
QImage modelImage(destinationRect.size(), QImage::Format_ARGB32_Premultiplied);
// export svg
if (fileName.endsWith(".svg"))
{
QRect bbox = pGraphicsView->itemsBoundingRect().toAlignedRect();
QSize bigSize = pGraphicsView->viewport()->size();
if (fileName.endsWith(".svg")) {
svgGenerator.setTitle(QString(Helper::applicationName).append(" - ").append(Helper::applicationIntroText));
svgGenerator.setDescription("Generated by OMEdit - OpenModelica Connection Editor");
svgGenerator.setSize(bigSize);
svgGenerator.setViewBox(bbox);
svgGenerator.setSize(destinationRect.size());
svgGenerator.setViewBox(QRect(0, 0, destinationRect.width(), destinationRect.height()));
svgGenerator.setFileName(fileName);
painter.begin(&svgGenerator);
}
else
{
modelImage.fill(QColor(Qt::transparent));
} else {
if (fileName.endsWith(".png") || fileName.endsWith(".tiff")) {
modelImage.fill(QColor(Qt::transparent));
} else if (fileName.endsWith(".bmp") || copyToClipboard) {
modelImage.fill(QColor(Qt::white));
}
painter.begin(&modelImage);
}
painter.setWindow(pGraphicsView->viewport()->rect());
// paint the background color first
if (!fileName.endsWith(".svg")) {
if (pGraphicsView->getViewType() == StringHandler::Diagram)
painter.fillRect(painter.viewport(), pGraphicsView->palette().background());
else
painter.fillRect(painter.viewport(), Qt::white);
}
painter.setWindow(destinationRect);
// paint all the items
oldSkipDrawBackground = pGraphicsView->mSkipBackground;
if (fileName.endsWith(".svg")) {
pGraphicsView->mSkipBackground = true;
}
pGraphicsView->render(&painter);
pGraphicsView->mSkipBackground = true;
pGraphicsView->render(&painter, destinationRect, destinationRect);
painter.end();
pGraphicsView->mSkipBackground = oldSkipDrawBackground;
if (!fileName.endsWith(".svg"))
{
if (!modelImage.save(fileName))
if (!fileName.endsWith(".svg") && !copyToClipboard) {
if (!modelImage.save(fileName)) {
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error),
tr("Error saving the image file"), Helper::ok);
}
} else if (copyToClipboard) {
QClipboard *pClipboard = QApplication::clipboard();
pClipboard->setImage(modelImage);
}
// hide the progressbar and clear the message in status bar
mpStatusBar->clearMessage();
hideProgressBar();
}
}
else
{
} else {
mpMessagesWidget->addGUIMessage(new MessagesTreeItem("", false, 0, 0, 0, 0, GUIMessages::getMessage(GUIMessages::NO_MODELICA_CLASS_OPEN)
.arg(tr("exporting to Image")), Helper::scriptingKind, Helper::notificationLevel,
0, mpMessagesWidget->getMessagesTreeWidget()));
}
}

/*!
Slot activated when mpExportToClipboardAction triggered signal is raised.\n
Copies the current model to clipboard.
*/
void MainWindow::exportToClipboard()
{
exportModelAsImage(true);
}

void MainWindow::openConfigurationOptions()
{
mpOptionsDialog->show();
Expand Down Expand Up @@ -2226,6 +2230,10 @@ void MainWindow::createActions()
mpExportAsImageAction->setStatusTip(Helper::exportAsImageTip);
mpExportAsImageAction->setEnabled(false);
connect(mpExportAsImageAction, SIGNAL(triggered()), SLOT(exportModelAsImage()));
mpExportToClipboardAction = new QAction(tr("Export to Clipboard"), this);
mpExportToClipboardAction->setStatusTip(Helper::exportAsImageTip);
mpExportToClipboardAction->setEnabled(false);
connect(mpExportToClipboardAction, SIGNAL(triggered()), SLOT(exportToClipboard()));
}

//! Creates the menus
Expand Down Expand Up @@ -2394,8 +2402,28 @@ void MainWindow::createMenus()
menuBar()->addAction(pHelpMenu->menuAction());
}

/*!
Stores the window states and geometry of all Plot Windows.
*/
/*
The application window title and window icon gets corrupted when we switch between modeling & plotting perspective.
To solve this we tile the plot windows when we switch to modeling and welcome perspective. But before calling tileSubWindows() we save all
the plot windows states & geometry and then restore it when switching back to plotting view.
*/
void MainWindow::storePlotWindowsStateAndGeometry()
{
if (mPlotWindowsStatesList.isEmpty() && mPlotWindowsGeometriesList.isEmpty()) {
foreach (QMdiSubWindow *pWindow, mpPlotWindowContainer->subWindowList()) {
mPlotWindowsStatesList.append(pWindow->windowState());
mPlotWindowsGeometriesList.append(pWindow->saveGeometry());
}
}
}

void MainWindow::switchToWelcomePerspective()
{
storePlotWindowsStateAndGeometry();
mpPlotWindowContainer->tileSubWindows();
mpCentralStackedWidget->setCurrentWidget(mpWelcomePageWidget);
mpModelWidgetContainer->currentModelWidgetChanged(0);
mpModelSwitcherToolButton->setEnabled(false);
Expand All @@ -2405,6 +2433,8 @@ void MainWindow::switchToWelcomePerspective()

void MainWindow::switchToModelingPerspective()
{
storePlotWindowsStateAndGeometry();
mpPlotWindowContainer->tileSubWindows();
mpCentralStackedWidget->setCurrentWidget(mpModelWidgetContainer);
mpModelWidgetContainer->currentModelWidgetChanged(mpModelWidgetContainer->getCurrentMdiSubWindow());
mpModelSwitcherToolButton->setEnabled(true);
Expand All @@ -2415,6 +2445,17 @@ void MainWindow::switchToModelingPerspective()
void MainWindow::switchToPlottingPerspective()
{
mpCentralStackedWidget->setCurrentWidget(mpPlotWindowContainer);
int i = 0;
foreach (QMdiSubWindow *pWindow, mpPlotWindowContainer->subWindowList()) {
// sanity check
if (mPlotWindowsStatesList.size() > i && mPlotWindowsGeometriesList.size() > i) {
pWindow->setWindowState(pWindow->windowState() & (mPlotWindowsStatesList[i]));
pWindow->restoreGeometry(mPlotWindowsGeometriesList[i]);
}
i++;
}
mPlotWindowsStatesList.clear();
mPlotWindowsGeometriesList.clear();
mpModelWidgetContainer->currentModelWidgetChanged(0);
mpModelSwitcherToolButton->setEnabled(false);
// if not plotwindow is opened then open one for user
Expand Down
8 changes: 7 additions & 1 deletion OMEdit/OMEditGUI/MainWindow.h
Expand Up @@ -139,6 +139,7 @@ class MainWindow : public QMainWindow
QAction* getTextShapeAction();
QAction* getBitmapShapeAction();
QAction* getExportAsImageAction();
QAction* getExportToClipboardAction() {return mpExportToClipboardAction;}
QAction* getExportToOMNotebookAction();
QAction* getImportFromOMNotebookAction();
QAction* getImportNgspiceNetlistAction();
Expand Down Expand Up @@ -188,6 +189,8 @@ class MainWindow : public QMainWindow
FindReplaceDialog *mpFindReplaceDialog;
SimulationDialog *mpSimulationDialog;
PlotWindowContainer *mpPlotWindowContainer;
QList<Qt::WindowStates> mPlotWindowsStatesList;
QList<QByteArray> mPlotWindowsGeometriesList;
//InteractiveSimulationTabWidget *mpInteractiveSimualtionTabWidget;
ModelWidgetContainer *mpModelWidgetContainer;
DebuggerMainWindow *mpDebuggerMainWindow;
Expand Down Expand Up @@ -279,6 +282,7 @@ class MainWindow : public QMainWindow
QAction *mpClearPlotWindowAction;
// Other Actions
QAction *mpExportAsImageAction;
QAction *mpExportToClipboardAction;
// Toolbars
QMenu *mpRecentFilesMenu;
QMenu *mpLibrariesMenu;
Expand Down Expand Up @@ -329,7 +333,8 @@ public slots:
void exportModelToOMNotebook();
void importModelfromOMNotebook();
void importNgspiceNetlist();
void exportModelAsImage();
void exportModelAsImage(bool copyToClipboard = false);
void exportToClipboard();
void openConfigurationOptions();
void openUsersGuide();
void openSystemDocumentation();
Expand All @@ -355,6 +360,7 @@ private slots:
void createActions();
void createToolbars();
void createMenus();
void storePlotWindowsStateAndGeometry();
void switchToWelcomePerspective();
void switchToModelingPerspective();
void switchToPlottingPerspective();
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditGUI/Modeling/LibraryTreeWidget.cpp
Expand Up @@ -1899,7 +1899,7 @@ void LibraryTreeWidget::showModelWidget(LibraryTreeNode *pLibraryTreeNode, bool
pLibraryTreeNode->getModelWidget()->setWindowTitle(pLibraryTreeNode->getNameStructure() + (pLibraryTreeNode->isSaved() ? "" : "*"));
mpMainWindow->getModelWidgetContainer()->addModelWidget(pLibraryTreeNode->getModelWidget());
} else {
ModelWidget *pModelWidget = NULL;
ModelWidget *pModelWidget = 0;
if (pLibraryTreeNode->getLibraryType() == LibraryTreeNode::Modelica) {
pModelWidget = new ModelWidget(newClass, extendsClass, pLibraryTreeNode, mpMainWindow->getModelWidgetContainer());
} else if (pLibraryTreeNode->getLibraryType() == LibraryTreeNode::Text) {
Expand Down
40 changes: 21 additions & 19 deletions OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -1585,6 +1585,7 @@ void GraphicsView::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu(mpModelWidget->getModelWidgetContainer()->getMainWindow());
menu.addAction(mpModelWidget->getModelWidgetContainer()->getMainWindow()->getExportAsImageAction());
menu.addAction(mpModelWidget->getModelWidgetContainer()->getMainWindow()->getExportToClipboardAction());
menu.addSeparator();
menu.addAction(mpModelWidget->getModelWidgetContainer()->getMainWindow()->getExportToOMNotebookAction());
menu.addSeparator();
Expand Down Expand Up @@ -2581,20 +2582,20 @@ void ModelWidget::makeFileWritAble()
void ModelWidget::showIconView(bool checked)
{
// validate the modelica text before switching to icon view
if (checked)
{
if (!mpModelicaTextEditor->validateModelicaText())
{
if (checked) {
if (!mpModelicaTextEditor->validateModelicaText()) {
mpTextViewToolButton->setChecked(true);
return;
}
}
QMdiSubWindow *pSubWindow = mpModelWidgetContainer->getCurrentMdiSubWindow();
if (pSubWindow)
if (pSubWindow) {
pSubWindow->setWindowIcon(QIcon(":/Resources/icons/model.svg"));
}
mpIconGraphicsView->setFocus();
if (!checked or (checked and mpIconGraphicsView->isVisible()))
if (!checked or (checked and mpIconGraphicsView->isVisible())) {
return;
}
mpViewTypeLabel->setText(StringHandler::getViewType(StringHandler::Icon));
mpDiagramGraphicsView->hide();
mpModelicaTextEditor->hide();
Expand All @@ -2607,20 +2608,20 @@ void ModelWidget::showIconView(bool checked)
void ModelWidget::showDiagramView(bool checked)
{
// validate the modelica text before switching to diagram view
if (checked)
{
if (!mpModelicaTextEditor->validateModelicaText())
{
if (checked) {
if (!mpModelicaTextEditor->validateModelicaText()) {
mpTextViewToolButton->setChecked(true);
return;
}
}
QMdiSubWindow *pSubWindow = mpModelWidgetContainer->getCurrentMdiSubWindow();
if (pSubWindow)
if (pSubWindow) {
pSubWindow->setWindowIcon(QIcon(":/Resources/icons/modeling.png"));
}
mpDiagramGraphicsView->setFocus();
if (!checked or (checked and mpDiagramGraphicsView->isVisible()))
if (!checked or (checked and mpDiagramGraphicsView->isVisible())) {
return;
}
mpViewTypeLabel->setText(StringHandler::getViewType(StringHandler::Diagram));
mpIconGraphicsView->hide();
mpModelicaTextEditor->hide();
Expand All @@ -2633,10 +2634,12 @@ void ModelWidget::showDiagramView(bool checked)
void ModelWidget::showModelicaTextView(bool checked)
{
QMdiSubWindow *pSubWindow = mpModelWidgetContainer->getCurrentMdiSubWindow();
if (pSubWindow)
if (pSubWindow) {
pSubWindow->setWindowIcon(QIcon(":/Resources/icons/modeltext.svg"));
if (!checked or (checked and mpModelicaTextEditor->isVisible()))
}
if (!checked or (checked and mpModelicaTextEditor->isVisible())) {
return;
}
mpViewTypeLabel->setText(StringHandler::getViewType(StringHandler::ModelicaText));
// get the modelica text of the model
mpModelicaTextEditor->setPlainText(mpModelWidgetContainer->getMainWindow()->getOMCProxy()->list(getLibraryTreeNode()->getNameStructure()));
Expand All @@ -2653,8 +2656,7 @@ void ModelWidget::showModelicaTextView(bool checked)
void ModelWidget::showDocumentationView()
{
// validate the modelica text before switching to documentation view
if (!mpModelicaTextEditor->validateModelicaText())
{
if (!mpModelicaTextEditor->validateModelicaText()) {
mpTextViewToolButton->setChecked(true);
return;
}
Expand Down Expand Up @@ -2833,12 +2835,11 @@ void ModelWidgetContainer::addModelWidget(ModelWidget *pModelWidget, bool checkP
}
}
} else {
static int firstSubWindow = 0;
int subWindowsSize = subWindowList(QMdiArea::ActivationHistoryOrder).size();
QMdiSubWindow *pSubWindow = addSubWindow(pModelWidget);
pSubWindow->setWindowIcon(QIcon(":/Resources/icons/modeling.png"));
pModelWidget->show();
if (!firstSubWindow) {
firstSubWindow = 1;
if (subWindowsSize == 0) {
pModelWidget->setWindowState(Qt::WindowMaximized);
}
setActiveSubWindow(pSubWindow);
Expand Down Expand Up @@ -3115,6 +3116,7 @@ void ModelWidgetContainer::currentModelWidgetChanged(QMdiSubWindow *pSubWindow)
getMainWindow()->getExportFigaroAction()->setEnabled(enabled && modelica);
getMainWindow()->getExportToOMNotebookAction()->setEnabled(enabled && modelica);
getMainWindow()->getExportAsImageAction()->setEnabled(enabled && modelica);
getMainWindow()->getExportToClipboardAction()->setEnabled(enabled && modelica);
getMainWindow()->getPrintModelAction()->setEnabled(enabled);
/* disable the save actions if class is a system library class. */
if (pModelWidget) {
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditGUI/Util/Helper.cpp
Expand Up @@ -52,7 +52,7 @@ QString Helper::OMCServerName = "OMEdit";
QString Helper::omFileTypes = "Modelica Files (*.mo)";
QString Helper::omnotebookFileTypes = "OMNotebook Files (*.onb *.onbz *.nb)";
QString Helper::ngspiceNetlistFileTypes = "ngspice Netlist Files (*.cir *.sp *.spice)";
QString Helper::imageFileTypes = "SVG (*.svg);;PNG image (*.png);;Windows BMP image (*.bmp);;JPEG (*.jpg *.jpeg)";
QString Helper::imageFileTypes = "SVG (*.svg);;PNG image (*.png);;Windows BMP image (*.bmp);;TIFF (*.tiff)";
QString Helper::bitmapFileTypes = "PNG image (*.png);;Windows BMP image (*.bmp);;JPEG (*.jpg *.jpeg)";
QString Helper::fmuFileTypes = "FMU Files (*.fmu)";
QString Helper::xmlFileTypes = "XML Files (*.xml)";
Expand Down

0 comments on commit 13e8a6f

Please sign in to comment.