Skip to content

Commit

Permalink
Bug fixed #1476
Browse files Browse the repository at this point in the history
- Fixed the color change problem of OMPlot.
- No more checkboxes with + items of plottree.
- Double derivates can be plotted now. Tested with double pendulum example.

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@8632 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adeas31 committed Apr 13, 2011
1 parent b5bd979 commit 5f5917a
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 42 deletions.
70 changes: 51 additions & 19 deletions OMPlot/OMPlotGUI/Legend.cpp
Expand Up @@ -42,6 +42,19 @@ Legend::Legend(Plot *pParent)
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this,SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(legendMenu(const QPoint&)));
mpPlot = pParent;

// create actions for context menu
mpChangeColorAction = new QAction(QString("Change Color"), this);
connect(mpChangeColorAction, SIGNAL(triggered()), this, SLOT(selectColor()));

mpAutomaticColorAction = new QAction(QString("Automatic Color"), this);
mpAutomaticColorAction->setCheckable(true);
mpAutomaticColorAction->setChecked(true);
connect(mpAutomaticColorAction, SIGNAL(triggered(bool)), this, SLOT(automaticColor(bool)));

mpHideAction = new QAction(QString("Hide"), this);
mpHideAction->setCheckable(true);
connect(mpHideAction, SIGNAL(triggered(bool)), this, SLOT(toggleHide(bool)));
}

Legend::~Legend()
Expand All @@ -55,26 +68,13 @@ void Legend::legendMenu(const QPoint& pos)

if(lgdItem)
{

QAction *color = new QAction(QString("Change Color"), this);
color->setCheckable(false);
connect(color, SIGNAL(triggered()), this, SLOT(selectColor()));

QAction *hide;
if(lgdItem->text().color() == Qt::gray)
hide = new QAction(QString("Show"), this);
else
hide = new QAction(QString("Hide"), this);
hide->setCheckable(false);
connect(hide, SIGNAL(triggered()), this, SLOT(toggleShow()));


legendItem = lgdItem->text().text();

QMenu menu(mpPlot);
menu.addAction(color);
menu.addAction(mpChangeColorAction);
menu.addAction(mpAutomaticColorAction);
menu.addSeparator();
menu.addAction(hide);
menu.addAction(mpHideAction);
menu.exec(mapToGlobal(pos));
}
}
Expand All @@ -90,31 +90,33 @@ void Legend::selectColor()
{
if(list[i]->title() == legendItem)
{
list[i]->setCustomColor(true);
QPen pen = list[i]->pen();
pen.setColor(c);
list[i]->setPen(pen);
mpAutomaticColorAction->setChecked(false);
}
}
mpPlot->replot();
}
}

void Legend::toggleShow()
void Legend::toggleHide(bool hide)
{
QList<PlotCurve*> list = mpPlot->getPlotCurvesList();

for(int i = 0; i < list.length(); i++)
{
if(list[i]->title().text() == legendItem)
{
if(list[i]->isVisible())
if (hide)
{
QwtText text = list[i]->title();
text.setColor(QColor(Qt::gray));
list[i]->setTitle(text);
list[i]->setVisible(false);
}
else if(!list[i]->isVisible())
else
{
QwtText text = list[i]->title();
text.setColor(QColor(Qt::black));
Expand All @@ -125,3 +127,33 @@ void Legend::toggleShow()
}
mpPlot->replot();
}

void Legend::automaticColor(bool automatic)
{
QList<PlotCurve*> list = mpPlot->getPlotCurvesList();

for(int i = 0; i < list.length(); i++)
{
if(list[i]->title().text() == legendItem)
{
if (automatic)
{
list[i]->setCustomColor(false);
}
else
{
if (list[i]->hasCustomColor())
{
list[i]->setCustomColor(true);
}
else
{
mpAutomaticColorAction->blockSignals(true);
mpAutomaticColorAction->setChecked(true);
mpAutomaticColorAction->blockSignals(false);
}
}
}
}
mpPlot->replot();
}
9 changes: 6 additions & 3 deletions OMPlot/OMPlotGUI/Legend.h
Expand Up @@ -47,15 +47,18 @@ class Legend : public QwtLegend
public:
Legend(Plot *pParent);
~Legend();
public Q_SLOTS:
public slots:
void legendMenu(const QPoint&);
void selectColor();
void toggleShow();

void toggleHide(bool hide);
void automaticColor(bool automatic);
private:
PlotCurve *mpPlotCurve;
Plot *mpPlot;
QString legendItem;
QAction *mpChangeColorAction;
QAction *mpAutomaticColorAction;
QAction *mpHideAction;
};
}

Expand Down
6 changes: 4 additions & 2 deletions OMPlot/OMPlotGUI/Plot.cpp
Expand Up @@ -139,12 +139,14 @@ QColor Plot::getUniqueColor(int index, int total)
return mColorsList.at(index);
}

// just overloaded this function to get colors for curves.
void Plot::replot()
{
// just overloaded this function to get colors for curves.
for (int i = 0 ; i < mPlotCurvesList.length() ; i++)
{
mPlotCurvesList[i]->setPen(getUniqueColor(i, mPlotCurvesList.length()));
// if user has set the custom color for the curve then dont get automatic color for it
if (!mPlotCurvesList[i]->hasCustomColor())
mPlotCurvesList[i]->setPen(getUniqueColor(i, mPlotCurvesList.length()));
}

QwtPlot::replot();
Expand Down
11 changes: 11 additions & 0 deletions OMPlot/OMPlotGUI/PlotCurve.cpp
Expand Up @@ -38,6 +38,7 @@
using namespace OMPlot;

PlotCurve::PlotCurve(Plot *pParent)
: mCustomColor(false)
{
mpParentPlot = pParent;
}
Expand Down Expand Up @@ -112,6 +113,16 @@ QString PlotCurve::getYVariable()
return mYVariable;
}

void PlotCurve::setCustomColor(bool value)
{
mCustomColor = value;
}

bool PlotCurve::hasCustomColor()
{
return mCustomColor;
}

void PlotCurve::updateLegend(QwtLegend *legend) const
{
QwtPlotCurve::updateLegend(legend);
Expand Down
3 changes: 3 additions & 0 deletions OMPlot/OMPlotGUI/PlotCurve.h
Expand Up @@ -46,6 +46,7 @@ class PlotCurve : public QwtPlotCurve
QString mFileName;
QString mXVariable;
QString mYVariable;
bool mCustomColor;

Plot *mpParentPlot;
public:
Expand All @@ -65,6 +66,8 @@ class PlotCurve : public QwtPlotCurve
QString getXVariable();
void setYVariable(QString yVariable);
QString getYVariable();
void setCustomColor(bool value);
bool hasCustomColor();
virtual void updateLegend(QwtLegend *legend) const;
};
}
Expand Down
22 changes: 5 additions & 17 deletions OMPlot/OMPlotGUI/PlotWindow.cpp
Expand Up @@ -155,6 +155,10 @@ PlotWindow::PlotType PlotWindow::getPlotType()
void PlotWindow::openFile(QString file)
{
//Open file to a textstream
// close the file if it is already opened
if (mFile.isOpen())
mFile.close();

mFile.setFileName(file);
if(!mFile.exists())
throw NoFileException(QString("File not found : ").append(file).toStdString().c_str());
Expand All @@ -180,12 +184,6 @@ void PlotWindow::setupToolbar()
connect(mpPanButton, SIGNAL(toggled(bool)), SLOT(enablePanMode(bool)));
toolBar->addWidget(mpPanButton);
toolBar->addSeparator();
//ORIGINAL SIZE
QToolButton *originalButton = new QToolButton(toolBar);
originalButton->setText("Original");
connect(originalButton, SIGNAL(clicked()), SLOT(setOriginal()));
toolBar->addWidget(originalButton);
toolBar->addSeparator();
//Fit in View
QToolButton *fitInViewButton = new QToolButton(toolBar);
fitInViewButton->setText("Fit in View");
Expand Down Expand Up @@ -359,7 +357,7 @@ void PlotWindow::plot()

//Read in mat file
if(0 != (msg = omc_new_matlab4_reader(mFile.fileName().toStdString().c_str(), &reader)))
return;
throw PlotException(msg);

//Read in timevector
double startTime = omc_matlab4_startTime(&reader);
Expand Down Expand Up @@ -414,8 +412,6 @@ void PlotWindow::plot()
if (getPlotType() == PlotWindow::PLOT)
checkForErrors(mVariablesList, variablesPlotted);
}
// close the file
mFile.close();
}

void PlotWindow::plotParametric()
Expand Down Expand Up @@ -582,8 +578,6 @@ void PlotWindow::plotParametric()
pPlotCurve->attach(mpPlot);
mpPlot->replot();
}
// close the file
mFile.close();
}

void PlotWindow::setTitle(QString title)
Expand Down Expand Up @@ -749,12 +743,6 @@ void PlotWindow::setGrid(bool on)
mpPlot->replot();
}

void PlotWindow::setOriginal()
{
mpPlot->getPlotZoomer()->zoom(0);
mpPlot->replot();
}

void PlotWindow::fitInView()
{
mpPlot->getPlotZoomer()->zoom(0);
Expand Down
1 change: 0 additions & 1 deletion OMPlot/OMPlotGUI/PlotWindow.h
Expand Up @@ -93,7 +93,6 @@ public slots:
void exportDocument();
void printPlot();
void setGrid(bool on);
void setOriginal();
void fitInView();
void setLogX(bool on);
void setLogY(bool on);
Expand Down

0 comments on commit 5f5917a

Please sign in to comment.