Changeset - 020e7830b9d2
[Not reviewed]
default
0 5 0
Hasan Yavuz Ă–ZDERYA - 8 years ago 2017-04-30 07:24:07
hy@ozderya.net
save/load symbol setting
5 files changed with 63 insertions and 22 deletions:
0 comments (0 inline, 0 general)
src/plot.cpp
Show inline comments
 
@@ -30,25 +30,25 @@
 

	
 
static const int SYMBOL_SHOW_AT_WIDTH = 5;
 
static const int SYMBOL_SIZE_MAX = 7;
 

	
 
Plot::Plot(QWidget* parent) :
 
    QwtPlot(parent),
 
    zoomer(this->canvas(), false),
 
    sZoomer(this, &zoomer)
 
{
 
    isAutoScaled = true;
 
    symbolSize = 0;
 
    numOfSamples = 1;
 
    showSymbols = ShowSymbolsAuto;
 
    showSymbols = Plot::Auto;
 

	
 
    QObject::connect(&zoomer, &Zoomer::unzoomed, this, &Plot::unzoomed);
 

	
 
    zoomer.setZoomBase();
 
    grid.attach(this);
 
    legend.attach(this);
 

	
 
    showGrid(false);
 
    darkBackground(false);
 

	
 
    snapshotOverlay = NULL;
 

	
 
@@ -243,44 +243,44 @@ void Plot::flashSnapshotOverlay(bool lig
 
    connect(snapshotOverlay, &PlotSnapshotOverlay::done,
 
            [this]()
 
            {
 
                delete snapshotOverlay;
 
                snapshotOverlay = NULL;
 
            });
 
}
 

	
 
void Plot::setSymbols(ShowSymbols shown)
 
{
 
    showSymbols = shown;
 

	
 
    if (showSymbols == ShowSymbolsAuto)
 
    if (showSymbols == Plot::Auto)
 
    {
 
        calcSymbolSize();
 
    }
 
    else if (showSymbols == ShowSymbolsAlways)
 
    else if (showSymbols == Plot::Show)
 
    {
 
        symbolSize = SYMBOL_SIZE_MAX;
 
    }
 
    else
 
    {
 
        symbolSize = 0;
 
    }
 

	
 
    updateSymbols();
 
    replot();
 
}
 

	
 
void Plot::onXScaleChanged()
 
{
 
    if (showSymbols == ShowSymbolsAuto)
 
    if (showSymbols == Plot::Auto)
 
    {
 
        calcSymbolSize();
 
        updateSymbols();
 
    }
 
}
 

	
 
void Plot::calcSymbolSize()
 
{
 
    auto sw = axisWidget(QwtPlot::xBottom);
 
    auto paintDist = sw->scaleDraw()->scaleMap().pDist();
 
    auto scaleDist = sw->scaleDraw()->scaleMap().sDist();
 
    auto fullScaleDist = zoomer.zoomBase().width();
src/plot.h
Show inline comments
 
@@ -24,38 +24,40 @@
 
#include <QList>
 
#include <QAction>
 
#include <qwt_plot.h>
 
#include <qwt_plot_grid.h>
 
#include <qwt_plot_shapeitem.h>
 
#include <qwt_plot_legenditem.h>
 
#include <qwt_plot_textlabel.h>
 

	
 
#include "zoomer.h"
 
#include "scalezoomer.h"
 
#include "plotsnapshotoverlay.h"
 

	
 
enum ShowSymbols
 
{
 
    ShowSymbolsAuto,
 
    ShowSymbolsAlways,
 
    ShowSymbolsNever
 
};
 

	
 
class Plot : public QwtPlot
 
{
 
    Q_OBJECT
 

	
 
    friend class PlotManager;
 

	
 
public:
 
    // Note: this naming is also used as setting values
 
    enum ShowSymbols
 
    {
 
        Auto,
 
        Show,
 
        Hide
 
    };
 
    Q_ENUM(ShowSymbols);
 

	
 
    Plot(QWidget* parent = 0);
 
    ~Plot();
 

	
 
    static QColor makeColor(unsigned int channelIndex);
 

	
 
public slots:
 
    void showGrid(bool show = true);
 
    void showMinorGrid(bool show = true);
 
    void showLegend(bool show = true);
 
    void showDemoIndicator(bool show = true);
 
    void unzoom();
 
    void darkBackground(bool enabled = true);
src/plotmanager.cpp
Show inline comments
 
@@ -9,49 +9,51 @@
 
  (at your option) any later version.
 

	
 
  serialplot is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU General Public License for more details.
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include <QActionGroup>
 
#include <QMetaEnum>
 
#include <QtDebug>
 
#include "qwt_symbol.h"
 

	
 
#include "plot.h"
 
#include "plotmanager.h"
 
#include "utils.h"
 
#include "setting_defines.h"
 

	
 
PlotManager::PlotManager(QWidget* plotArea, ChannelInfoModel* infoModel, QObject *parent) :
 
    QObject(parent),
 
    _plotArea(plotArea),
 
    showGridAction("&Grid", this),
 
    showMinorGridAction("&Minor Grid", this),
 
    unzoomAction("&Unzoom", this),
 
    darkBackgroundAction("&Dark Background", this),
 
    showLegendAction("&Legend", this),
 
    showMultiAction("Multi &Plot", this),
 
    setSymbolsAction("Symbols", this)
 
{
 
    _autoScaled = true;
 
    _yMin = 0;
 
    _yMax = 1;
 
    isDemoShown = false;
 
    _infoModel = infoModel;
 
    _numOfSamples = 1;
 
    showSymbols = ShowSymbolsAuto;
 
    showSymbols = Plot::Auto;
 

	
 
    // initalize layout and single widget
 
    isMulti = false;
 
    scrollArea = NULL;
 
    setupLayout(isMulti);
 
    addPlotWidget();
 

	
 
    // initialize menu actions
 
    showGridAction.setToolTip("Show Grid");
 
    showMinorGridAction.setToolTip("Show Minor Grid");
 
    unzoomAction.setToolTip("Unzoom the Plot");
 
    darkBackgroundAction.setToolTip("Enable Dark Plot Background");
 
@@ -68,45 +70,45 @@ PlotManager::PlotManager(QWidget* plotAr
 
    showLegendAction.setCheckable(true);
 
    showMultiAction.setCheckable(true);
 

	
 
    showGridAction.setChecked(false);
 
    showMinorGridAction.setChecked(false);
 
    darkBackgroundAction.setChecked(false);
 
    showLegendAction.setChecked(true);
 
    showMultiAction.setChecked(false);
 

	
 
    showMinorGridAction.setEnabled(false);
 

	
 
    // setup symbols menu
 
    auto setSymbolAutoAct = setSymbolsMenu.addAction("Show When Zoomed");
 
    setSymbolAutoAct = setSymbolsMenu.addAction("Show When Zoomed");
 
    setSymbolAutoAct->setCheckable(true);
 
    setSymbolAutoAct->setChecked(true);
 
    connect(setSymbolAutoAct, SELECT<bool>::OVERLOAD_OF(&QAction::triggered),
 
            [this](bool checked)
 
            {
 
                if (checked) setSymbols(ShowSymbolsAuto);
 
                if (checked) setSymbols(Plot::Auto);
 
            });
 
    auto setSymbolAlwaysAct = setSymbolsMenu.addAction("Always Show");
 
    setSymbolAlwaysAct = setSymbolsMenu.addAction("Always Show");
 
    setSymbolAlwaysAct->setCheckable(true);
 
    connect(setSymbolAlwaysAct, SELECT<bool>::OVERLOAD_OF(&QAction::triggered),
 
            [this](bool checked)
 
            {
 
                if (checked) setSymbols(ShowSymbolsAlways);
 
                if (checked) setSymbols(Plot::Show);
 
            });
 
    auto setSymbolHideAct = setSymbolsMenu.addAction("Always Hide");
 
    setSymbolHideAct = setSymbolsMenu.addAction("Always Hide");
 
    setSymbolHideAct->setCheckable(true);
 
    connect(setSymbolHideAct, SELECT<bool>::OVERLOAD_OF(&QAction::triggered),
 
            [this](bool checked)
 
            {
 
                if (checked) setSymbols(ShowSymbolsNever);
 
                if (checked) setSymbols(Plot::Hide);
 
            });
 
    setSymbolsAction.setMenu(&setSymbolsMenu);
 

	
 
    // add symbol actions to same group so that they appear as radio buttons
 
    auto group = new QActionGroup(this);
 
    group->addAction(setSymbolAutoAct);
 
    group->addAction(setSymbolAlwaysAct);
 
    group->addAction(setSymbolHideAct);
 

	
 
    connect(&showGridAction, SELECT<bool>::OVERLOAD_OF(&QAction::triggered),
 
            this, &PlotManager::showGrid);
 
    connect(&showGridAction, SELECT<bool>::OVERLOAD_OF(&QAction::triggered),
 
@@ -444,25 +446,25 @@ void PlotManager::unzoom()
 
        plot->unzoom();
 
    }
 
}
 

	
 
void PlotManager::darkBackground(bool enabled)
 
{
 
    for (auto plot : plotWidgets)
 
    {
 
        plot->darkBackground(enabled);
 
    }
 
}
 

	
 
void PlotManager::setSymbols(ShowSymbols shown)
 
void PlotManager::setSymbols(Plot::ShowSymbols shown)
 
{
 
    showSymbols = shown;
 
    for (auto plot : plotWidgets)
 
    {
 
        plot->setSymbols(shown);
 
    }
 
}
 

	
 
void PlotManager::setYAxis(bool autoScaled, double yAxisMin, double yAxisMax)
 
{
 
    _autoScaled = autoScaled;
 
    _yMin = yAxisMin;
 
@@ -515,36 +517,69 @@ void PlotManager::onNumOfSamplesChanged(
 
        if (_xAxisAsIndex) plot->setXAxis(0, value);
 
    }
 
}
 

	
 
void PlotManager::saveSettings(QSettings* settings)
 
{
 
    settings->beginGroup(SettingGroup_Plot);
 
    settings->setValue(SG_Plot_DarkBackground, darkBackgroundAction.isChecked());
 
    settings->setValue(SG_Plot_Grid, showGridAction.isChecked());
 
    settings->setValue(SG_Plot_MinorGrid, showMinorGridAction.isChecked());
 
    settings->setValue(SG_Plot_Legend, showLegendAction.isChecked());
 
    settings->setValue(SG_Plot_MultiPlot, showMultiAction.isChecked());
 
    settings->setValue(SG_Plot_Symbols,
 
                       QMetaEnum::fromType<Plot::ShowSymbols>().valueToKey(showSymbols));
 
    settings->endGroup();
 
}
 

	
 
void PlotManager::loadSettings(QSettings* settings)
 
{
 
    settings->beginGroup(SettingGroup_Plot);
 
    darkBackgroundAction.setChecked(
 
        settings->value(SG_Plot_DarkBackground, darkBackgroundAction.isChecked()).toBool());
 
    darkBackground(darkBackgroundAction.isChecked());
 
    showGridAction.setChecked(
 
        settings->value(SG_Plot_Grid, showGridAction.isChecked()).toBool());
 
    showGrid(showGridAction.isChecked());
 
    showMinorGridAction.setChecked(
 
        settings->value(SG_Plot_MinorGrid, showMinorGridAction.isChecked()).toBool());
 
    showMinorGridAction.setEnabled(showGridAction.isChecked());
 
    showMinorGrid(showMinorGridAction.isChecked());
 
    showLegendAction.setChecked(
 
        settings->value(SG_Plot_Legend, showLegendAction.isChecked()).toBool());
 
    showLegend(showLegendAction.isChecked());
 
    showMultiAction.setChecked(
 
        settings->value(SG_Plot_MultiPlot, showMultiAction.isChecked()).toBool());
 
    setMulti(showMultiAction.isChecked());
 

	
 
    // symbol setting
 
    {
 
        bool ok = false;
 
        QString curStr = QMetaEnum::fromType<Plot::ShowSymbols>().valueToKey(showSymbols);
 
        QString settStr = settings->value(SG_Plot_Symbols, curStr).toString();
 
        Plot::ShowSymbols settValue =
 
            (Plot::ShowSymbols) QMetaEnum::fromType<Plot::ShowSymbols>().\
 
            keyToValue(settStr.toLatin1().constData(), &ok);
 
        if (ok)
 
        {
 
            if (settValue == Plot::Auto)
 
            {
 
                setSymbolAutoAct->setChecked(true);
 
            }
 
            else if (settValue == Plot::Show)
 
            {
 
                setSymbolAlwaysAct->setChecked(true);
 
            }
 
            else
 
            {
 
                setSymbolHideAct->setChecked(true);
 
            }
 
            setSymbols(settValue);
 
        }
 
        else
 
        {
 
            qCritical() << "Invalid symbol setting:" << settStr;
 
        }
 
    }
 

	
 
    settings->endGroup();
 
}
src/plotmanager.h
Show inline comments
 
@@ -82,44 +82,47 @@ private:
 
    QScrollArea* scrollArea;
 
    QList<QwtPlotCurve*> curves;
 
    QList<Plot*> plotWidgets;
 
    ChannelInfoModel* _infoModel;
 
    bool isDemoShown;
 
    bool _autoScaled;
 
    double _yMin;
 
    double _yMax;
 
    bool _xAxisAsIndex;
 
    double _xMin;
 
    double _xMax;
 
    unsigned _numOfSamples;
 
    ShowSymbols showSymbols;
 
    Plot::ShowSymbols showSymbols;
 

	
 
    // menu actions
 
    QAction showGridAction;
 
    QAction showMinorGridAction;
 
    QAction unzoomAction;
 
    QAction darkBackgroundAction;
 
    QAction showLegendAction;
 
    QAction showMultiAction;
 
    QAction setSymbolsAction;
 
    QMenu   setSymbolsMenu;
 
    QMenu setSymbolsMenu;
 
    QAction* setSymbolAutoAct;
 
    QAction* setSymbolAlwaysAct;
 
    QAction* setSymbolHideAct;
 

	
 
    void setupLayout(bool multiPlot);
 
    /// Inserts a new plot widget to the current layout.
 
    Plot* addPlotWidget();
 
    /// Returns the plot widget that given curve is attached to
 
    Plot* plotWidget(unsigned curveIndex);
 
    /// Common part of overloaded `addCurve` functions
 
    void _addCurve(QwtPlotCurve* curve);
 
    void setSymbols(ShowSymbols shown);
 
    void setSymbols(Plot::ShowSymbols shown);
 

	
 
private slots:
 
    void showGrid(bool show = true);
 
    void showMinorGrid(bool show = true);
 
    void showLegend(bool show = true);
 
    void unzoom();
 
    void darkBackground(bool enabled = true);
 

	
 
    void onChannelInfoChanged(const QModelIndex & topLeft,
 
                              const QModelIndex & bottomRight,
 
                              const QVector<int> & roles = QVector<int> ());
 
};
src/setting_defines.h
Show inline comments
 
@@ -78,24 +78,25 @@ const char SG_Channels_Visible[] = "visi
 
const char SG_Plot_NumOfSamples[] = "numOfSamples";
 
const char SG_Plot_IndexAsX[] = "indexAsX";
 
const char SG_Plot_XMax[] = "xMax";
 
const char SG_Plot_XMin[] = "xMin";
 
const char SG_Plot_AutoScale[] = "autoScale";
 
const char SG_Plot_YMax[] = "yMax";
 
const char SG_Plot_YMin[] = "yMin";
 
const char SG_Plot_DarkBackground[] = "darkBackground";
 
const char SG_Plot_Grid[] = "grid";
 
const char SG_Plot_MinorGrid[] = "minorGrid";
 
const char SG_Plot_Legend[] = "legend";
 
const char SG_Plot_MultiPlot[] = "multiPlot";
 
const char SG_Plot_Symbols[] = "symbols";
 

	
 
// command setting keys
 
const char SG_Commands_Command[] = "command";
 
const char SG_Commands_Name[] = "name";
 
const char SG_Commands_Type[] = "type";
 
const char SG_Commands_Data[] = "data";
 

	
 
// record panel settings keys
 
const char SG_Record_AutoIncrement[]    = "autoIncrement";
 
const char SG_Record_RecordPaused[]     = "recordPaused";
 
const char SG_Record_StopOnClose[]      = "stopOnClose";
 
const char SG_Record_Header[]           = "header";
0 comments (0 inline, 0 general)