Skip to content

Commit

Permalink
- Using setRawData instead of setData (Martin's request)
Browse files Browse the repository at this point in the history
- Make the OMPlot a single instance application.
- Added an extra argument -ew true/false. if true will pop up new OMPlot window.
- Fixed the segmentation faults problem.

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@8441 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adeas31 committed Mar 31, 2011
1 parent 66f5d22 commit e86bba9
Show file tree
Hide file tree
Showing 16 changed files with 324 additions and 285 deletions.
10 changes: 4 additions & 6 deletions OMPlot/OMPlotGUI/OMPlotGUI.pro
Expand Up @@ -12,25 +12,23 @@ TEMPLATE = app
SOURCES += main.cpp \
../../c_runtime/read_matlab4.c \
Plot.cpp \
PlotCanvas.cpp \
PlotZoomer.cpp \
Legend.cpp \
PlotPanner.cpp \
PlotGrid.cpp \
PlotCurve.cpp \
PlotPicker.cpp \
PlotWindow.cpp
PlotWindow.cpp \
PlotApplication.cpp

HEADERS += ../../c_runtime/read_matlab4.h \
Plot.h \
PlotCanvas.h \
PlotZoomer.h \
Legend.h \
PlotPanner.h \
PlotGrid.h \
PlotCurve.h \
PlotPicker.h \
PlotWindow.h
PlotWindow.h \
PlotApplication.h

win32 {
LIBS += -L$$(OMDEV)/lib/qwt-5.2.1-mingw/lib -lqwtd5
Expand Down
15 changes: 9 additions & 6 deletions OMPlot/OMPlotGUI/OMPlotLib.pro
Expand Up @@ -14,25 +14,23 @@ CONFIG += release staticlib
SOURCES += main.cpp \
../../c_runtime/read_matlab4.c \
Plot.cpp \
PlotCanvas.cpp \
PlotZoomer.cpp \
Legend.cpp \
PlotPanner.cpp \
PlotGrid.cpp \
PlotCurve.cpp \
PlotPicker.cpp \
PlotWindow.cpp
PlotWindow.cpp \
PlotApplication.cpp

HEADERS += ../../c_runtime/read_matlab4.h \
Plot.h \
PlotCanvas.h \
PlotZoomer.h \
Legend.h \
PlotPanner.h \
PlotGrid.h \
PlotCurve.h \
PlotPicker.h \
PlotWindow.h
PlotWindow.h \
PlotApplication.h

win32 {
LIBS += -L$$(OMDEV)/lib/qwt-5.2.1-mingw/lib -lqwt5
Expand All @@ -41,6 +39,8 @@ INCLUDEPATH += $$(OMDEV)/lib/qwt-5.2.1-mingw/include
include(OMPlotGUI.config)
}

INCLUDEPATH += .

CONFIG += warn_off

DESTDIR = ../bin
Expand All @@ -50,3 +50,6 @@ UI_DIR = ../generatedfiles/ui
MOC_DIR = ../generatedfiles/moc

RCC_DIR = ../generatedfiles/rcc

RESOURCES += \
Resources.qrc
20 changes: 8 additions & 12 deletions OMPlot/OMPlotGUI/Plot.cpp
Expand Up @@ -47,18 +47,19 @@ Plot::Plot(PlotWindow *pParent)
mpLegend = new Legend(this);
insertLegend(mpLegend, QwtPlot::RightLegend);

// create an instance opicker
mpPlotPicker = new PlotPicker(QwtPlot::xBottom, QwtPlot::yLeft, canvas());
// create an instance of picker
mpPlotPicker = new QwtPlotPicker(canvas());
mpPlotPicker->setTrackerPen(QPen(Qt::black));
mpPlotPicker->setRubberBandPen(QPen(Qt::black));
mpPlotPicker->setTrackerMode(QwtPicker::AlwaysOn);
// create an instance of grid
mpPlotGrid = new PlotGrid(this);
// create an instance of zoomer
mpPlotZoomer = new PlotZoomer(QwtPlot::xBottom, QwtPlot::yLeft, canvas());
// create an instance of panner
mpPlotPanner = new PlotPanner(canvas());
// create an instance of canvas we use it to capture events of canvas()
mpPlotCanvas = new PlotCanvas(this);
mpPlotPanner = new PlotPanner(canvas(), this);
// set canvas arrow
canvas()->setCursor(Qt::ArrowCursor);
canvas()->installEventFilter(mpPlotCanvas);
}

Plot::~Plot()
Expand All @@ -76,7 +77,7 @@ Legend* Plot::getLegend()
return mpLegend;
}

PlotPicker* Plot::getPlotPicker()
QwtPlotPicker* Plot::getPlotPicker()
{
return mpPlotPicker;
}
Expand All @@ -101,11 +102,6 @@ QList<PlotCurve*> Plot::getPlotCurvesList()
return mPlotCurvesList;
}

PlotCanvas* Plot::getPlotCanvas()
{
return mpPlotCanvas;
}

void Plot::addPlotCurve(PlotCurve *pCurve)
{
mPlotCurvesList.append(pCurve);
Expand Down
10 changes: 2 additions & 8 deletions OMPlot/OMPlotGUI/Plot.h
Expand Up @@ -36,48 +36,42 @@

#include "PlotWindow.h"
#include "Legend.h"
#include "PlotPicker.h"
#include "PlotGrid.h"
#include "PlotZoomer.h"
#include "PlotPanner.h"
#include "PlotCurve.h"
#include "PlotCanvas.h"

namespace OMPlot
{
class PlotWindow;
class Legend;
class PlotPicker;
class PlotGrid;
class PlotZoomer;
class PlotPanner;
class PlotCurve;
class PlotCanvas;

class Plot : public QwtPlot
{
Q_OBJECT
private:
PlotWindow *mpParentPlotWindow;
Legend *mpLegend;
PlotPicker *mpPlotPicker;
QwtPlotPicker *mpPlotPicker;
PlotGrid *mpPlotGrid;
PlotZoomer *mpPlotZoomer;
PlotPanner *mpPlotPanner;
QList<PlotCurve*> mPlotCurvesList;
PlotCanvas *mpPlotCanvas;
public:
Plot(PlotWindow *pParent);
~Plot();

PlotWindow* getParentPlotWindow();
Legend* getLegend();
PlotPicker* getPlotPicker();
QwtPlotPicker* getPlotPicker();
PlotGrid* getPlotGrid();
PlotZoomer* getPlotZoomer();
PlotPanner* getPlotPanner();
QList<PlotCurve*> getPlotCurvesList();
PlotCanvas* getPlotCanvas();
void addPlotCurve(PlotCurve *pCurve);
void removeCurve(PlotCurve *pCurve);
};
Expand Down
143 changes: 143 additions & 0 deletions OMPlot/OMPlotGUI/PlotApplication.cpp
@@ -0,0 +1,143 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Linkoping University,
* Department of Computer and Information Science,
* SE-58183 Linkoping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linkoping University, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
* Main Author 2011: Adeel Asghar
*
*/

#include "PlotApplication.h"
#include <QTimer>

PlotApplication::PlotApplication(int &argc, char *argv[], const QString uniqueKey)
: QApplication(argc, argv)
{
mSharedMemory.setKey(uniqueKey);

if (mSharedMemory.attach())
mIsRunning = true;
else
{
mIsRunning = false;
// attach data to shared memory.
QByteArray byteArray("0"); // default value to note that no message is available.
if (!mSharedMemory.create(byteArray.size()))
{
printf("Unable to create shared memory for OMPlot.");
return;
}
mSharedMemory.lock();
char *to = (char*)mSharedMemory.data();
const char *from = byteArray.data();
memcpy(to, from, qMin(mSharedMemory.size(), byteArray.size()));
mSharedMemory.unlock();
// start checking for messages of other instances.
mpTimer = new QTimer(this);
startTimer(); // connect the timer to checkForMessage slot
mpTimer->start(100); // after every 0.1 second we check the shared memory
}

connect(this, SIGNAL(newApplicationLaunched()), SLOT(stopTimer()));
}

bool PlotApplication::isRunning()
{
return mIsRunning;
}

bool PlotApplication::sendMessage(QStringList arguments)
{
if (!mIsRunning)
return false;
QByteArray byteArray("1");
byteArray.append(arguments.join(";").toUtf8());
byteArray.append('\0'); // < should be as char here, not a string!
mSharedMemory.lock();
char *to = (char*)mSharedMemory.data();
const char *from = byteArray.data();
memcpy(to, from, qMin(mSharedMemory.size(), byteArray.size()));
mSharedMemory.unlock();
return true;
}

void PlotApplication::launchNewApplication()
{
QByteArray byteArray("1");
byteArray.append(tr("newomplotwindow").toUtf8());
byteArray.append('\0'); // < should be as char here, not a string!
mSharedMemory.lock();
char *to = (char*)mSharedMemory.data();
const char *from = byteArray.data();
memcpy(to, from, qMin(mSharedMemory.size(), byteArray.size()));
mSharedMemory.unlock();
// new create timer for new app
mpTimer = new QTimer(this);
startTimer(); // connect the timer to checkForMessage slot
mpTimer->start(100); // after every 0.1 second we check the shared memory
}

void PlotApplication::checkForMessage()
{
qDebug() << "looking for message";
mSharedMemory.lock();
QByteArray byteArray = QByteArray((char*)mSharedMemory.constData(), mSharedMemory.size());
mSharedMemory.unlock();
if (byteArray.left(1) == "0")
return;
byteArray.remove(0, 1); // remove the one we put at the start of the bytearray while writing to memory
// check if new application is launched or not
if (QString::fromUtf8(byteArray.constData()).compare("newomplotwindow") == 0)
{
emit newApplicationLaunched();
}
else
{
QStringList arguments = QString::fromUtf8(byteArray.constData()).split(";");
emit messageAvailable(arguments);
}
// remove message from shared memory.
byteArray = "0";
mSharedMemory.lock();
char *to = (char*)mSharedMemory.data();
const char *from = byteArray.data();
memcpy(to, from, qMin(mSharedMemory.size(), byteArray.size()));
mSharedMemory.unlock();
}

void PlotApplication::startTimer()
{
connect(mpTimer, SIGNAL(timeout()), SLOT(checkForMessage()));
}

void PlotApplication::stopTimer()
{
mpTimer->stop();
qDebug() << "stopping timer";
}


Expand Up @@ -31,22 +31,34 @@
*
*/

#ifndef PLOTCANVAS_H
#define PLOTCANVAS_H
#ifndef PLOTAPPLICATION_H
#define PLOTAPPLICATION_H

#include "Plot.h"
#include <QApplication>
#include <QSharedMemory>
#include <QStringList>
#include <QDebug>

namespace OMPlot
class PlotApplication : public QApplication
{
class PlotCanvas : public QwtPlotCanvas
{
private:
Plot *mpParentPlot;
Q_OBJECT
public:
PlotCanvas(Plot *pParent);
~PlotCanvas();
bool eventFilter(QObject *pObject, QEvent *event);
PlotApplication(int &argc, char *argv[], const QString uniqueKey);

bool isRunning();
bool sendMessage(QStringList arguments);
void launchNewApplication();
private:
bool mIsRunning;
QSharedMemory mSharedMemory;
QTimer *mpTimer;
public slots:
void checkForMessage();
void startTimer();
void stopTimer();
signals:
void messageAvailable(QStringList arguments);
void newApplicationLaunched();
};
}

#endif // PLOTCANVAS_H
#endif // PLOTAPPLICATION_H

0 comments on commit e86bba9

Please sign in to comment.