From af5679d62dadb831a91551779b453cf89b9dac60 Mon Sep 17 00:00:00 2001 From: Adeel Asghar Date: Thu, 8 May 2014 22:32:21 +0000 Subject: [PATCH] #2679 - Fixed "There is no convenient way or replacing the colors by dashed/dotted/whole lines in black-and-white. The current version via the options menu is very cumbersome. The Dymola approach is much easier to use and more intuitive. All information collected in one place in the GUI." git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@20507 f25d12d1-65f4-0310-ae8a-bbce733d8d8e --- OMPlot/OMPlotGUI/Legend.cpp | 134 +++------------ OMPlot/OMPlotGUI/Legend.h | 25 +-- OMPlot/OMPlotGUI/Plot.cpp | 10 ++ OMPlot/OMPlotGUI/Plot.h | 1 + OMPlot/OMPlotGUI/PlotCurve.cpp | 55 +++++-- OMPlot/OMPlotGUI/PlotCurve.h | 9 +- OMPlot/OMPlotGUI/PlotWindow.cpp | 284 ++++++++++++++++++++++++++++++-- OMPlot/OMPlotGUI/PlotWindow.h | 95 +++++++---- 8 files changed, 420 insertions(+), 193 deletions(-) diff --git a/OMPlot/OMPlotGUI/Legend.cpp b/OMPlot/OMPlotGUI/Legend.cpp index 27448fd4ba9..a0c01dcf881 100644 --- a/OMPlot/OMPlotGUI/Legend.cpp +++ b/OMPlot/OMPlotGUI/Legend.cpp @@ -43,22 +43,14 @@ using namespace OMPlot; Legend::Legend(Plot *pParent) { - setContextMenuPolicy(Qt::CustomContextMenu); - connect(this,SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(legendMenu(const QPoint&))); mpPlot = pParent; + mpPlotCurve = 0; - // create actions for context menu - mpChangeColorAction = new QAction(QString(tr("Change Color")), this); - connect(mpChangeColorAction, SIGNAL(triggered()), this, SLOT(selectColor())); - - mpAutomaticColorAction = new QAction(QString(tr("Automatic Color")), this); - mpAutomaticColorAction->setCheckable(true); - mpAutomaticColorAction->setChecked(true); - connect(mpAutomaticColorAction, SIGNAL(triggered(bool)), this, SLOT(automaticColor(bool))); + mpSetupAction = new QAction(tr("Setup"), this); + connect(mpSetupAction, SIGNAL(triggered()), SLOT(showSetupDialog())); - mpHideAction = new QAction(QString(tr("Hide")), this); - mpHideAction->setCheckable(true); - connect(mpHideAction, SIGNAL(triggered(bool)), this, SLOT(toggleHide(bool))); + setContextMenuPolicy(Qt::CustomContextMenu); + connect(this, SIGNAL(customContextMenuRequested(QPoint)), SLOT(legendMenu(QPoint))); } Legend::~Legend() @@ -66,116 +58,28 @@ Legend::~Legend() } +void Legend::showSetupDialog() +{ + if (mpPlotCurve) + { + mpPlot->getParentPlotWindow()->showSetupDialog(mpPlotCurve->getNameStructure()); + mpPlotCurve = 0; + } +} + void Legend::legendMenu(const QPoint& pos) { #if QWT_VERSION >= 0x060100 - QwtLegendLabel *pItem = dynamic_cast(childAt(pos)); + QwtPlotItem *pQwtPlotItem = qvariant_cast(itemInfo(childAt(pos))); + mpPlotCurve = dynamic_cast(pQwtPlotItem); #else - QwtLegendItem *pItem = dynamic_cast(childAt(pos)); + mpPlotCurve = dynamic_cast(find(childAt(pos))); #endif - if(pItem) + if (mpPlotCurve) { - mLegendItemStr = pItem->text().text(); /* context menu */ QMenu menu(mpPlot); - menu.addAction(mpChangeColorAction); - menu.addAction(mpAutomaticColorAction); - menu.addSeparator(); - menu.addAction(mpHideAction); + menu.addAction(mpSetupAction); menu.exec(mapToGlobal(pos)); } } - -void Legend::selectColor() -{ - QColor c = QColorDialog::getColor(); - QList list = mpPlot->getPlotCurvesList(); - - if(c.isValid()) - { - for(int i = 0; i < list.length(); i++) - { - if(list[i]->title() == mLegendItemStr) - { - list[i]->setCustomColor(true); - QPen pen = list[i]->pen(); - pen.setColor(c); - list[i]->setPen(pen); - mpAutomaticColorAction->setChecked(false); - } - } - mpPlot->replot(); - } -} - -void Legend::toggleHide(bool hide) -{ - QList list = mpPlot->getPlotCurvesList(); - - for(int i = 0; i < list.length(); i++) - { - if(list[i]->title().text() == mLegendItemStr) - { - if (hide) - { - QwtText text = list[i]->title(); - text.setColor(QColor(Qt::gray)); - list[i]->setTitle(text); - list[i]->setVisible(false); - } - else - { - QwtText text = list[i]->title(); - text.setColor(QColor(Qt::black)); - list[i]->setTitle(text.text()); - list[i]->setVisible(true); - } - } - } - mpPlot->replot(); -} - -void Legend::automaticColor(bool automatic) -{ - QList list = mpPlot->getPlotCurvesList(); - - for(int i = 0; i < list.length(); i++) - { - if(list[i]->title().text() == mLegendItemStr) - { - 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(); -} - -void Legend::setLegendItemStr(QString value) -{ - mLegendItemStr = value; -} - -QAction* Legend::getAutomaticColorAction() -{ - return mpAutomaticColorAction; -} - -QAction* Legend::getHideAction() -{ - return mpHideAction; -} diff --git a/OMPlot/OMPlotGUI/Legend.h b/OMPlot/OMPlotGUI/Legend.h index 5c1dc9e9839..c11b60af58d 100644 --- a/OMPlot/OMPlotGUI/Legend.h +++ b/OMPlot/OMPlotGUI/Legend.h @@ -43,25 +43,18 @@ class PlotCurve; class Legend : public QwtLegend { - Q_OBJECT + Q_OBJECT public: - Legend(Plot *pParent); - ~Legend(); + Legend(Plot *pParent); + ~Legend(); public slots: - void legendMenu(const QPoint&); - void selectColor(); - void toggleHide(bool hide); - void automaticColor(bool automatic); - void setLegendItemStr(QString value); - QAction* getAutomaticColorAction(); - QAction* getHideAction(); + void showSetupDialog(); + void legendMenu(const QPoint&); private: - PlotCurve *mpPlotCurve; - Plot *mpPlot; - QString mLegendItemStr; - QAction *mpChangeColorAction; - QAction *mpAutomaticColorAction; - QAction *mpHideAction; + Plot *mpPlot; + PlotCurve *mpPlotCurve; + QAction *mpSetupAction; + }; } diff --git a/OMPlot/OMPlotGUI/Plot.cpp b/OMPlot/OMPlotGUI/Plot.cpp index a73d72c7477..3cb2f5bca13 100644 --- a/OMPlot/OMPlotGUI/Plot.cpp +++ b/OMPlot/OMPlotGUI/Plot.cpp @@ -141,6 +141,16 @@ QList Plot::getPlotCurvesList() return mPlotCurvesList; } +PlotCurve* Plot::getPlotCurve(QString nameStructure) +{ + foreach (PlotCurve *pPlotCurve, mPlotCurvesList) + { + if (pPlotCurve->getNameStructure().compare(nameStructure) == 0) + return pPlotCurve; + } + return 0; +} + void Plot::addPlotCurve(PlotCurve *pCurve) { mPlotCurvesList.append(pCurve); diff --git a/OMPlot/OMPlotGUI/Plot.h b/OMPlot/OMPlotGUI/Plot.h index 99430387a0b..1ab19174560 100644 --- a/OMPlot/OMPlotGUI/Plot.h +++ b/OMPlot/OMPlotGUI/Plot.h @@ -75,6 +75,7 @@ class Plot : public QwtPlot PlotZoomer* getPlotZoomer(); PlotPanner* getPlotPanner(); QList getPlotCurvesList(); + PlotCurve* getPlotCurve(QString nameStructure); void addPlotCurve(PlotCurve *pCurve); void removeCurve(PlotCurve *pCurve); QColor getUniqueColor(int index, int total); diff --git a/OMPlot/OMPlotGUI/PlotCurve.cpp b/OMPlot/OMPlotGUI/PlotCurve.cpp index 2e9fc3d1824..588b16ee611 100644 --- a/OMPlot/OMPlotGUI/PlotCurve.cpp +++ b/OMPlot/OMPlotGUI/PlotCurve.cpp @@ -34,24 +34,29 @@ #include "PlotCurve.h" #if QWT_VERSION < 0x060000 #include "qwt_legend_item.h" +#else +#include "qwt_painter.h" #endif #include "qwt_symbol.h" using namespace OMPlot; -PlotCurve::PlotCurve(Plot *pParent) +PlotCurve::PlotCurve(QString fileName, QString variableName, Plot *pParent) : mCustomColor(false) { + mName = variableName; + mNameStructure = fileName + "." + variableName; + mFileName = fileName; + mCustomColor = false; + setTitle(variableName); mpParentPlot = pParent; /* set curve width and style */ - mWidth = mpParentPlot->getParentPlotWindow()->getCurveWidth(); - mStyle = mpParentPlot->getParentPlotWindow()->getCurveStyle(); - QPen customPen = pen(); - customPen.setWidthF(mWidth); - customPen.setStyle(getPenStyle(mStyle)); - setPen(customPen); - if (mpParentPlot->getParentPlotWindow()->getCurveStyle() > 5) - setStyle(getCurveStyle(mStyle)); + setCurveWidth(mpParentPlot->getParentPlotWindow()->getCurveWidth()); + setCurveStyle(mpParentPlot->getParentPlotWindow()->getCurveStyle()); +#if QWT_VERSION > 0x060000 + setLegendAttribute(QwtPlotCurve::LegendShowLine); + setLegendIconSize(QSize(30, 30)); +#endif } PlotCurve::~PlotCurve() @@ -89,6 +94,25 @@ QwtPlotCurve::CurveStyle PlotCurve::getCurveStyle(int style) } } +void PlotCurve::setCurveWidth(qreal width) +{ + mWidth = width; + QPen customPen = pen(); + customPen.setWidthF(mWidth); + setPen(customPen); +} + +void PlotCurve::setCurveStyle(int style) +{ + setStyle(QwtPlotCurve::Lines); + mStyle = style; + QPen customPen = pen(); + customPen.setStyle(getPenStyle(mStyle)); + setPen(customPen); + if (mStyle > 5) + setStyle(getCurveStyle(mStyle)); +} + void PlotCurve::setXAxisVector(QVector vector) { mXAxisVector = vector; @@ -144,6 +168,11 @@ QString PlotCurve::getFileName() return mFileName; } +void PlotCurve::setNameStructure(QString variableName) +{ + mNameStructure = getFileName() + "." + variableName; +} + void PlotCurve::setXVariable(QString xVariable) { mXVariable = xVariable; @@ -187,11 +216,11 @@ void PlotCurve::setData(const double* xData, const double* yData, int size) void PlotCurve::updateLegend(QwtLegend *legend) const { QwtPlotCurve::updateLegend(legend); - QwtLegendItem *lgdItem = dynamic_cast(legend->find(this)); - if (lgdItem) + QwtLegendItem *pQwtLegendItem = dynamic_cast(legend->find(this)); + if (pQwtLegendItem) { - lgdItem->setIdentifierMode(QwtLegendItem::ShowSymbol | QwtLegendItem::ShowText); - lgdItem->setSymbol(QwtSymbol(QwtSymbol::Rect, QBrush(pen().color()), QPen(Qt::black),QSize(20,20))); + pQwtLegendItem->setIdentifierMode(QwtLegendItem::ShowLine); + pQwtLegendItem->setIdentifierWidth(30); } QwtPlotItem::updateLegend(legend); } diff --git a/OMPlot/OMPlotGUI/PlotCurve.h b/OMPlot/OMPlotGUI/PlotCurve.h index 68abcb0b187..2e745919993 100644 --- a/OMPlot/OMPlotGUI/PlotCurve.h +++ b/OMPlot/OMPlotGUI/PlotCurve.h @@ -43,6 +43,8 @@ class PlotCurve : public QwtPlotCurve private: QwtArray mXAxisVector; QwtArray mYAxisVector; + QString mName; + QString mNameStructure; QString mFileName; QString mXVariable; QString mYVariable; @@ -52,12 +54,14 @@ class PlotCurve : public QwtPlotCurve Plot *mpParentPlot; public: - PlotCurve(Plot *pParent); + PlotCurve(QString fileName, QString variableName, Plot *pParent); ~PlotCurve(); Qt::PenStyle getPenStyle(int style); QwtPlotCurve::CurveStyle getCurveStyle(int style); + void setCurveWidth(qreal width); qreal getCurveWidth() {return mWidth;} + void setCurveStyle(int style); int getCurveStyle() {return mStyle;} void setXAxisVector(QVector vector); void addXAxisValue(double value); @@ -68,8 +72,11 @@ class PlotCurve : public QwtPlotCurve void addYAxisValue(double value); const double* getYAxisVector() const; int getSize(); + QString getName() {return mName;} void setFileName(QString fileName); QString getFileName(); + void setNameStructure(QString variableName); + QString getNameStructure() {return mNameStructure;} void setXVariable(QString xVariable); QString getXVariable(); void setYVariable(QString yVariable); diff --git a/OMPlot/OMPlotGUI/PlotWindow.cpp b/OMPlot/OMPlotGUI/PlotWindow.cpp index 9f37dbf3356..4d411b22a16 100644 --- a/OMPlot/OMPlotGUI/PlotWindow.cpp +++ b/OMPlot/OMPlotGUI/PlotWindow.cpp @@ -239,6 +239,12 @@ void PlotWindow::setupToolbar() mpLogYCheckBox = new QCheckBox(tr("Log Y"), this); connect(mpLogYCheckBox, SIGNAL(toggled(bool)), SLOT(setLogY(bool))); toolBar->addWidget(mpLogYCheckBox); + toolBar->addSeparator(); + // setup + mpSetupButton = new QToolButton(toolBar); + mpSetupButton->setText(tr("Setup")); + connect(mpSetupButton, SIGNAL(clicked()), SLOT(showSetupDialog())); + toolBar->addWidget(mpSetupButton); // finally add the tool bar to the mainwindow addToolBar(toolBar); } @@ -279,8 +285,7 @@ void PlotWindow::plot() if (mVariablesList.contains(currentVariable) or getPlotType() == PlotWindow::PLOTALL) { variablesPlotted.append(currentVariable); - PlotCurve *pPlotCurve = new PlotCurve(mpPlot); - pPlotCurve->setFileName(QFileInfo(mFile).fileName()); + PlotCurve *pPlotCurve = new PlotCurve(QFileInfo(mFile).fileName(), currentVariable, mpPlot); mpPlot->addPlotCurve(pPlotCurve); // read the variable values now currentLine = mpTextStream->readLine(); @@ -291,7 +296,6 @@ void PlotWindow::plot() pPlotCurve->addYAxisValue(QString(values[1]).toDouble()); currentLine = mpTextStream->readLine(); } - pPlotCurve->setTitle(currentVariable); pPlotCurve->setData(pPlotCurve->getXAxisVector(), pPlotCurve->getYAxisVector(), pPlotCurve->getSize()); pPlotCurve->attach(mpPlot); @@ -332,10 +336,8 @@ void PlotWindow::plot() double *vals = read_csv_dataset(csvReader, csvReader->variables[i]); if (vals == NULL) throw NoVariableException(tr("Variable doesnt exist: %1").arg(csvReader->variables[i]).toStdString().c_str()); - PlotCurve *pPlotCurve = new PlotCurve(mpPlot); - pPlotCurve->setFileName(QFileInfo(mFile).fileName()); + PlotCurve *pPlotCurve = new PlotCurve(QFileInfo(mFile).fileName(), csvReader->variables[i], mpPlot); mpPlot->addPlotCurve(pPlotCurve); - pPlotCurve->setTitle(csvReader->variables[i]); for (int i = 0 ; i < csvReader->numsteps ; i++) { pPlotCurve->addXAxisValue(timeVals[i]); @@ -380,10 +382,8 @@ void PlotWindow::plot() { variablesPlotted.append(reader.allInfo[i].name); // create the plot curve for variable - PlotCurve *pPlotCurve = new PlotCurve(mpPlot); - pPlotCurve->setFileName(QFileInfo(mFile).fileName()); + PlotCurve *pPlotCurve = new PlotCurve(QFileInfo(mFile).fileName(), reader.allInfo[i].name, mpPlot); mpPlot->addPlotCurve(pPlotCurve); - pPlotCurve->setTitle(reader.allInfo[i].name); counter++; // read the variable values var = omc_matlab4_find_var(&reader, reader.allInfo[i].name); @@ -476,8 +476,7 @@ void PlotWindow::plotParametric() PlotCurve *pPlotCurve; if (variablesPlotted.size() == 1) { - pPlotCurve = new PlotCurve(mpPlot); - pPlotCurve->setFileName(QFileInfo(mFile).fileName()); + pPlotCurve = new PlotCurve(QFileInfo(mFile).fileName(), yVariable + "(" + xVariable + ")", mpPlot); pPlotCurve->setXVariable(xVariable); pPlotCurve->setYVariable(yVariable); mpPlot->addPlotCurve(pPlotCurve); @@ -497,7 +496,6 @@ void PlotWindow::plotParametric() // when two variables are found plot then plot them if (variablesPlotted.size() == 2) { - pPlotCurve->setTitle(yVariable + "(" + xVariable + ")"); pPlotCurve->setData(pPlotCurve->getXAxisVector(), pPlotCurve->getYAxisVector(), pPlotCurve->getSize()); pPlotCurve->attach(mpPlot); @@ -544,12 +542,10 @@ void PlotWindow::plotParametric() } // create plot curves - PlotCurve *pPlotCurve = new PlotCurve(mpPlot); - pPlotCurve->setFileName(QFileInfo(mFile).fileName()); + PlotCurve *pPlotCurve = new PlotCurve(QFileInfo(mFile).fileName(), yVariable + "(" + xVariable + ")", mpPlot); pPlotCurve->setXVariable(xVariable); pPlotCurve->setYVariable(yVariable); mpPlot->addPlotCurve(pPlotCurve); - pPlotCurve->setTitle(yVariable + "(" + xVariable + ")"); //Assign Values while(!mpTextStream->atEnd()) @@ -583,8 +579,7 @@ void PlotWindow::plotParametric() if(0 != (msg = omc_new_matlab4_reader(mFile.fileName().toStdString().c_str(), &reader))) throw PlotException(msg); - PlotCurve *pPlotCurve = new PlotCurve(mpPlot); - pPlotCurve->setFileName(QFileInfo(mFile).fileName()); + PlotCurve *pPlotCurve = new PlotCurve(QFileInfo(mFile).fileName(), yVariable + "(" + xVariable + ")", mpPlot); pPlotCurve->setXVariable(xVariable); pPlotCurve->setYVariable(yVariable); mpPlot->addPlotCurve(pPlotCurve); @@ -640,7 +635,6 @@ void PlotWindow::plotParametric() pPlotCurve->addYAxisValue(yval); } pPlotCurve->setData(pPlotCurve->getXAxisVector(), pPlotCurve->getYAxisVector(), pPlotCurve->getSize()); - pPlotCurve->setTitle(yVariable + "(" + xVariable + ")"); pPlotCurve->attach(mpPlot); mpPlot->replot(); omc_free_matlab4_reader(&reader); @@ -747,6 +741,8 @@ void PlotWindow::setLegendPosition(QString position) mpPlot->insertLegend(0); mpPlot->setLegend(new Legend(mpPlot)); mpPlot->insertLegend(mpPlot->getLegend(), QwtPlot::TopLegend); +// QwtLegend *pQwtLegend = qobject_cast(mpPlot->legend()); +// pQwtLegend->contentsWidget()->layout()->setAlignment(Qt::AlignTop | Qt::AlignLeft); } else if (position.toLower().compare("bottom") == 0) { @@ -999,3 +995,255 @@ void PlotWindow::setLogY(bool on) } mpPlot->replot(); } + +void PlotWindow::showSetupDialog() +{ + SetupDialog *pSetupDialog = new SetupDialog(this); + pSetupDialog->exec(); +} + +void PlotWindow::showSetupDialog(QString variable) +{ + SetupDialog *pSetupDialog = new SetupDialog(this); + pSetupDialog->selectVariable(variable); + pSetupDialog->exec(); +} + +/*! + \class VariablePageWidget + \brief Represent the attribute of a plot variable. + */ +VariablePageWidget::VariablePageWidget(PlotCurve *pPlotCurve, SetupDialog *pSetupDialog) + : QWidget(pSetupDialog) +{ + mpPlotCurve = pPlotCurve; + // general group box + mpGeneralGroupBox = new QGroupBox(tr("General")); + mpLegendLabel = new QLabel(tr("Legend")); + mpLegendTextBox = new QLineEdit(mpPlotCurve->title().text()); + mpResetLabelButton = new QPushButton(tr("Reset")); + connect(mpResetLabelButton, SIGNAL(clicked()), SLOT(resetLabel())); + mpFileLabel = new QLabel(tr("File")); + mpFileTextBox = new QLabel(mpPlotCurve->getFileName()); + // appearance layout + QGridLayout *pGeneralGroupBoxGridLayout = new QGridLayout; + pGeneralGroupBoxGridLayout->addWidget(mpLegendLabel, 0, 0); + pGeneralGroupBoxGridLayout->addWidget(mpLegendTextBox, 0, 1); + pGeneralGroupBoxGridLayout->addWidget(mpResetLabelButton, 0, 2); + pGeneralGroupBoxGridLayout->addWidget(mpFileLabel, 1, 0); + pGeneralGroupBoxGridLayout->addWidget(mpFileTextBox, 1, 1, 1, 2); + mpGeneralGroupBox->setLayout(pGeneralGroupBoxGridLayout); + // Appearance group box + mpAppearanceGroupBox = new QGroupBox(tr("Appearance")); + mpColorLabel = new QLabel(tr("Color")); + mpPickColorButton = new QPushButton(tr("Pick Color")); + //mpPickColorButton->setAutoDefault(false); + connect(mpPickColorButton, SIGNAL(clicked()), SLOT(pickColor())); + mCurveColor = mpPlotCurve->pen().color(); + setCurvePickColorButtonIcon(); + mpAutomaticColorCheckBox = new QCheckBox(tr("Automatic Color")); + mpAutomaticColorCheckBox->setChecked(!mpPlotCurve->hasCustomColor()); + // pattern + mpPatternLabel = new QLabel(tr("Pattern")); + mpPatternComboBox = new QComboBox; + mpPatternComboBox->addItem("SolidLine", 1); + mpPatternComboBox->addItem("DashLine", 2); + mpPatternComboBox->addItem("DotLine", 3); + mpPatternComboBox->addItem("DashDotLine", 4); + mpPatternComboBox->addItem("DashDotDotLine", 5); + mpPatternComboBox->addItem("Sticks", 6); + mpPatternComboBox->addItem("Steps", 7); + int index = mpPatternComboBox->findData(mpPlotCurve->getCurveStyle()); + if (index != -1) mpPatternComboBox->setCurrentIndex(index); + // thickness + mpThicknessLabel = new QLabel(tr("Thickness")); + mpThicknessSpinBox = new QDoubleSpinBox; + mpThicknessSpinBox->setValue(1); + mpThicknessSpinBox->setSingleStep(1); + mpThicknessSpinBox->setValue(mpPlotCurve->getCurveWidth()); + // hide + mpHideCheckBox = new QCheckBox(tr("Hide")); + mpHideCheckBox->setChecked(!mpPlotCurve->isVisible()); + // appearance layout + QGridLayout *pAppearanceGroupBoxGridLayout = new QGridLayout; + pAppearanceGroupBoxGridLayout->addWidget(mpColorLabel, 0, 0); + pAppearanceGroupBoxGridLayout->addWidget(mpPickColorButton, 0, 1); + pAppearanceGroupBoxGridLayout->addWidget(mpAutomaticColorCheckBox, 0, 2); + pAppearanceGroupBoxGridLayout->addWidget(mpPatternLabel, 1, 0); + pAppearanceGroupBoxGridLayout->addWidget(mpPatternComboBox, 1, 1, 1, 2); + pAppearanceGroupBoxGridLayout->addWidget(mpThicknessLabel, 2, 0); + pAppearanceGroupBoxGridLayout->addWidget(mpThicknessSpinBox, 2, 1, 1, 2); + pAppearanceGroupBoxGridLayout->addWidget(mpHideCheckBox, 3, 0, 1, 3); + mpAppearanceGroupBox->setLayout(pAppearanceGroupBoxGridLayout); + // set layout + QGridLayout *pMainLayout = new QGridLayout; + pMainLayout->setContentsMargins(0, 0, 0, 0); + pMainLayout->addWidget(mpGeneralGroupBox, 0, 0); + pMainLayout->addWidget(mpAppearanceGroupBox, 1, 0); + setLayout(pMainLayout); +} + +void VariablePageWidget::setCurvePickColorButtonIcon() +{ + QPixmap pixmap(QSize(10, 10)); + pixmap.fill(getCurveColor()); + mpPickColorButton->setIcon(pixmap); +} + +void VariablePageWidget::resetLabel() +{ + mpLegendTextBox->setText(mpPlotCurve->getName()); +} + +void VariablePageWidget::pickColor() +{ + QColor color = QColorDialog::getColor(getCurveColor()); + if (!color.isValid()) + return; + + setCurveColor(color); + setCurvePickColorButtonIcon(); + mpAutomaticColorCheckBox->setChecked(false); +} + +/*! + \class SetupDialog + \brief Contains a list of plot variables. Allows user to select the variable and then edit its attributes. + */ +/*! + \param pPlotWindow - pointer to PlotWindow + */ +SetupDialog::SetupDialog(PlotWindow *pPlotWindow) + : QDialog(pPlotWindow, Qt::WindowTitleHint) +{ + setWindowTitle(tr("Plot Setup")); + setAttribute(Qt::WA_DeleteOnClose); + + mpPlotWindow = pPlotWindow; + mpSetupTabWidget = new QTabWidget; + // Variables Tab + mpVariablesTab = new QWidget; + mpVariableLabel = new QLabel(tr("Select a variable, then edit its properties below:")); + // variables list + mpVariablesListWidget = new QListWidget; + mpVariablePagesStackedWidget = new QStackedWidget; + QList plotCurves = mpPlotWindow->getPlot()->getPlotCurvesList(); + foreach (PlotCurve *pPlotCurve, plotCurves) + { + mpVariablePagesStackedWidget->addWidget(new VariablePageWidget(pPlotCurve, this)); + QListWidgetItem *pListItem = new QListWidgetItem(mpVariablesListWidget); + pListItem->setText(pPlotCurve->getName()); + pListItem->setData(Qt::UserRole, pPlotCurve->getNameStructure()); + } + connect(mpVariablesListWidget, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), SLOT(variableSelected(QListWidgetItem*,QListWidgetItem*))); + // Variables Tab Layout + QGridLayout *pVariablesTabGridLayout = new QGridLayout; + pVariablesTabGridLayout->addWidget(mpVariableLabel, 0, 0); + pVariablesTabGridLayout->addWidget(mpVariablesListWidget, 1, 0); + pVariablesTabGridLayout->addWidget(mpVariablePagesStackedWidget, 2, 0); + mpVariablesTab->setLayout(pVariablesTabGridLayout); + // add tabs + mpSetupTabWidget->addTab(mpVariablesTab, tr("Variables")); + // Create the buttons + mpOkButton = new QPushButton(tr("OK")); + mpOkButton->setAutoDefault(true); + connect(mpOkButton, SIGNAL(clicked()), this, SLOT(saveSetup())); + mpApplyButton = new QPushButton(tr("Apply")); + mpApplyButton->setAutoDefault(false); + connect(mpApplyButton, SIGNAL(clicked()), this, SLOT(applySetup())); + mpCancelButton = new QPushButton(tr("Cancel")); + mpCancelButton->setAutoDefault(false); + connect(mpCancelButton, SIGNAL(clicked()), this, SLOT(reject())); + mpButtonBox = new QDialogButtonBox(Qt::Horizontal); + mpButtonBox->addButton(mpOkButton, QDialogButtonBox::ActionRole); + mpButtonBox->addButton(mpApplyButton, QDialogButtonBox::ActionRole); + mpButtonBox->addButton(mpCancelButton, QDialogButtonBox::ActionRole); + // set the main layout + QGridLayout *pMainLayout = new QGridLayout; + pMainLayout->addWidget(mpSetupTabWidget, 0, 0); + pMainLayout->addWidget(mpButtonBox, 1, 0, 1, 1, Qt::AlignRight); + setLayout(pMainLayout); + // select the first variable if its available. + if (mpVariablesListWidget->count() > 0) + { + mpVariablesListWidget->setCurrentRow(0, QItemSelectionModel::Select); + } +} + +void SetupDialog::selectVariable(QString variable) +{ + for (int i = 0 ; i < mpVariablesListWidget->count() ; i++) + { + if (mpVariablesListWidget->item(i)->data(Qt::UserRole).toString().compare(variable) == 0) + { + mpVariablesListWidget->setCurrentRow(i, QItemSelectionModel::ClearAndSelect); + break; + } + } +} + +void SetupDialog::setupPlotCurve(VariablePageWidget *pVariablePageWidget) +{ + if (!pVariablePageWidget) + return; + + PlotCurve *pPlotCurve = pVariablePageWidget->getPlotCurve(); + + /* set the legend title */ + pPlotCurve->setTitle(pVariablePageWidget->getLegendTextBox()->text()); + /* set the curve color title */ + pPlotCurve->setCustomColor(!pVariablePageWidget->getAutomaticColorCheckBox()->isChecked()); + if (pVariablePageWidget->getAutomaticColorCheckBox()->isChecked()) + { + pVariablePageWidget->setCurveColor(pPlotCurve->pen().color()); + pVariablePageWidget->setCurvePickColorButtonIcon(); + } + else + { + QPen pen = pPlotCurve->pen(); + pen.setColor(pVariablePageWidget->getCurveColor()); + pPlotCurve->setPen(pen); + } + /* set the curve style */ + QComboBox *pPatternComboBox = pVariablePageWidget->getPatternComboBox(); + pPlotCurve->setCurveStyle(pPatternComboBox->itemData(pPatternComboBox->currentIndex()).toInt()); + /* set the curve width */ + pPlotCurve->setCurveWidth(pVariablePageWidget->getThicknessSpinBox()->value()); + /* set the curve visibility */ + pPlotCurve->setVisible(!pVariablePageWidget->getHideCheckBox()->isChecked()); + if (pPlotCurve->isVisible()) + { + QwtText text = pPlotCurve->title(); + text.setColor(QColor(Qt::black)); + pPlotCurve->setTitle(text); + } + else + { + QwtText text = pPlotCurve->title(); + text.setColor(QColor(Qt::gray)); + pPlotCurve->setTitle(text); + } +} + +void SetupDialog::variableSelected(QListWidgetItem *current, QListWidgetItem *previous) +{ + if (!current) + current = previous; + + mpVariablePagesStackedWidget->setCurrentIndex(mpVariablesListWidget->row(current)); +} + +void SetupDialog::saveSetup() +{ + applySetup(); + accept(); +} + +void SetupDialog::applySetup() +{ + for (int i = 0 ; i < mpVariablePagesStackedWidget->count() ; i++) + { + setupPlotCurve(qobject_cast(mpVariablePagesStackedWidget->widget(i))); + } + mpPlotWindow->getPlot()->replot(); +} diff --git a/OMPlot/OMPlotGUI/PlotWindow.h b/OMPlot/OMPlotGUI/PlotWindow.h index 3faaaf97329..efe360877c2 100644 --- a/OMPlot/OMPlotGUI/PlotWindow.h +++ b/OMPlot/OMPlotGUI/PlotWindow.h @@ -63,6 +63,7 @@ class PlotWindow : public QMainWindow QToolButton *mpNoGridButton; QToolButton *mpZoomButton; QToolButton *mpPanButton; + QToolButton *mpSetupButton; QTextStream *mpTextStream; QFile mFile; QStringList mVariablesList; @@ -121,6 +122,8 @@ public slots: void fitInView(); void setLogX(bool on); void setLogY(bool on); + void showSetupDialog(); + void showSetupDialog(QString variable); }; //Exception classes @@ -145,38 +148,70 @@ class NoVariableException : public PlotException NoVariableException(const char *varName) : PlotException(varName) {} }; -//Options Class -//class Options : public QDialog -//{ -// Q_OBJECT -//public: -// Options(PlotWindow *pPlotWindow); - -// void setUpForm(); -// void show(); - -// //MainWindow *mpParentMainWindow; -//private: -// QLineEdit *mpTitle; -// QLineEdit *mpXLabel; -// QLineEdit *mpYLabel; - -// QLineEdit *mpXRangeMin; -// QLineEdit *mpXRangeMax; -// QLineEdit *mpYRangeMin; -// QLineEdit *mpYRangeMax; - -// QLabel *mpTitleLabel; - -// QGroupBox *mpLabelGroup; +class SetupDialog; +class VariablePageWidget : public QWidget +{ + Q_OBJECT +private: + PlotCurve *mpPlotCurve; + QGroupBox *mpGeneralGroupBox; + QLabel *mpLegendLabel; + QLineEdit *mpLegendTextBox; + QPushButton *mpResetLabelButton; + QLabel *mpFileLabel; + QLabel *mpFileTextBox; + QGroupBox *mpAppearanceGroupBox; + QLabel *mpColorLabel; + QPushButton *mpPickColorButton; + QColor mCurveColor; + QCheckBox *mpAutomaticColorCheckBox; + QLabel *mpPatternLabel; + QComboBox *mpPatternComboBox; + QLabel *mpThicknessLabel; + QDoubleSpinBox *mpThicknessSpinBox; + QCheckBox *mpHideCheckBox; +public: + VariablePageWidget(PlotCurve *pPlotCurve, SetupDialog *pSetupDialog); + void setCurvePickColorButtonIcon(); + PlotCurve* getPlotCurve() {return mpPlotCurve;} + QLineEdit* getLegendTextBox() {return mpLegendTextBox;} + void setCurveColor(QColor color) {mCurveColor = color;} + QColor getCurveColor() {return mCurveColor;} + QCheckBox* getAutomaticColorCheckBox() {return mpAutomaticColorCheckBox;} + QComboBox* getPatternComboBox() {return mpPatternComboBox;} + QDoubleSpinBox* getThicknessSpinBox() {return mpThicknessSpinBox;} + QCheckBox* getHideCheckBox() {return mpHideCheckBox;} +public slots: + void resetLabel(); + void pickColor(); +}; -// QPushButton *mpOkButton; -// QPushButton *mpCancelButton; +class SetupDialog : public QDialog +{ + Q_OBJECT +private: + PlotWindow *mpPlotWindow; + QTabWidget *mpSetupTabWidget; + + QWidget *mpVariablesTab; + QLabel *mpVariableLabel; + QListWidget *mpVariablesListWidget; + QStackedWidget *mpVariablePagesStackedWidget; + + QPushButton *mpOkButton; + QPushButton *mpApplyButton; + QPushButton *mpCancelButton; + QDialogButtonBox *mpButtonBox; +public: + SetupDialog(PlotWindow *pPlotWindow); + void selectVariable(QString variable); + void setupPlotCurve(VariablePageWidget *pVariablePageWidget); +public slots: + void variableSelected(QListWidgetItem *current, QListWidgetItem *previous); + void saveSetup(); + void applySetup(); +}; -// PlotWindow *mpPlotWindow; -//public slots: -// void edit(); -//}; } #endif // PLOTWINDOW_H