Changeset - 6c0c96841b44
[Not reviewed]
default
0 2 0
Hasan Yavuz Ă–ZDERYA - 9 years ago 2016-09-27 08:35:48
hy@ozderya.net
ask confirmation to change the number of samples if its too big
2 files changed with 67 insertions and 1 deletions:
0 comments (0 inline, 0 general)
src/plotcontrolpanel.cpp
Show inline comments
 
@@ -9,56 +9,64 @@
 
  (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 <QVariant>
 
#include <QMessageBox>
 
#include <QCheckBox>
 

	
 
#include <math.h>
 

	
 
#include "plotcontrolpanel.h"
 
#include "ui_plotcontrolpanel.h"
 
#include "setting_defines.h"
 

	
 
/// Confirm if #samples is being set to a value greater than this
 
const int NUMSAMPLES_CONFIRM_AT = 10000;
 

	
 
/// Used for scale range selection combobox
 
struct Range
 
{
 
    double rmin;
 
    double rmax;
 
};
 

	
 
Q_DECLARE_METATYPE(Range);
 

	
 
PlotControlPanel::PlotControlPanel(QWidget *parent) :
 
    QWidget(parent),
 
    ui(new Ui::PlotControlPanel)
 
{
 
    ui->setupUi(this);
 

	
 
    warnNumOfSamples = true;    // TODO: load from settings
 
    _numOfSamples = ui->spNumOfSamples->value();
 

	
 
    // set limits for axis limit boxes
 
    ui->spYmin->setRange((-1) * std::numeric_limits<double>::max(),
 
                         std::numeric_limits<double>::max());
 

	
 
    ui->spYmax->setRange((-1) * std::numeric_limits<double>::max(),
 
                         std::numeric_limits<double>::max());
 

	
 
    // connect signals
 
    connect(ui->spNumOfSamples, SIGNAL(valueChanged(int)),
 
            this, SIGNAL(numOfSamplesChanged(int)));
 
            this, SLOT(onNumOfSamples(int)));
 

	
 
    connect(ui->cbAutoScale, &QCheckBox::toggled,
 
            this, &PlotControlPanel::onAutoScaleChecked);
 

	
 
    connect(ui->spYmax, SIGNAL(valueChanged(double)),
 
            this, SLOT(onYScaleChanged()));
 

	
 
    connect(ui->spYmin, SIGNAL(valueChanged(double)),
 
            this, SLOT(onYScaleChanged()));
 

	
 
    // init scale range preset list
 
    for (int nbits = 8; nbits <= 24; nbits++) // signed binary formats
 
@@ -87,24 +95,73 @@ PlotControlPanel::PlotControlPanel(QWidg
 
}
 

	
 
PlotControlPanel::~PlotControlPanel()
 
{
 
    delete ui;
 
}
 

	
 
unsigned PlotControlPanel::numOfSamples()
 
{
 
    return ui->spNumOfSamples->value();
 
}
 

	
 
void PlotControlPanel::onNumOfSamples(int value)
 
{
 
    if (warnNumOfSamples && value > NUMSAMPLES_CONFIRM_AT)
 
    {
 
        // ask confirmation
 
        if (!askNSConfirmation(value))
 
        {
 
            // revert to old value
 
            disconnect(ui->spNumOfSamples, SIGNAL(valueChanged(int)),
 
                       this, SLOT(onNumOfSamples(int)));
 

	
 
            ui->spNumOfSamples->setValue(_numOfSamples);
 

	
 
            connect(ui->spNumOfSamples, SIGNAL(valueChanged(int)),
 
                    this, SLOT(onNumOfSamples(int)));
 

	
 
            return;
 
        }
 
    }
 

	
 
    _numOfSamples = value;
 
    emit numOfSamplesChanged(value);
 
}
 

	
 
bool PlotControlPanel::askNSConfirmation(int value)
 
{
 
    auto text = tr("Setting number of samples to a too big value "
 
                   "(>%1) can seriously impact the performance of "
 
                   "the application and cause freezes. Are you sure you "
 
                   "want to change the number of samples to %2?")
 
        .arg(QString::number(NUMSAMPLES_CONFIRM_AT), QString::number(value));
 

	
 
    // TODO: parent the mainwindow
 
    QMessageBox mb(QMessageBox::Warning,
 
                   tr("Confirm Number of Samples"),
 
                   text,
 
                   QMessageBox::Apply | QMessageBox::Cancel);
 

	
 
    auto cb = new QCheckBox("Don't show this again.");
 
    connect(cb, &QCheckBox::stateChanged, [this](int state)
 
            {
 
                warnNumOfSamples = (state == Qt::Unchecked);
 
            });
 

	
 
    mb.setCheckBox(cb);
 

	
 
    return mb.exec() == QMessageBox::Apply;
 
}
 

	
 
void PlotControlPanel::onAutoScaleChecked(bool checked)
 
{
 
    if (checked)
 
    {
 
        ui->lYmin->setEnabled(false);
 
        ui->lYmax->setEnabled(false);
 
        ui->spYmin->setEnabled(false);
 
        ui->spYmax->setEnabled(false);
 

	
 
        emit scaleChanged(true); // autoscale
 
    }
 
    else
src/plotcontrolpanel.h
Show inline comments
 
@@ -46,19 +46,28 @@ public:
 
    /// Stores plot settings into a `QSettings`
 
    void saveSettings(QSettings* settings);
 
    /// Loads plot settings from a `QSettings`.
 
    void loadSettings(QSettings* settings);
 

	
 
signals:
 
    void numOfSamplesChanged(int value);
 
    void scaleChanged(bool autoScaled, double yMin = 0, double yMax = 1);
 

	
 
private:
 
    Ui::PlotControlPanel *ui;
 

	
 
    /// Holds 'number of samples' after the confirmation
 
    unsigned _numOfSamples;
 
    /// User can disable this setting in the checkbox
 
    bool warnNumOfSamples;
 

	
 
    /// Show a confirmation dialog before setting #samples to a big value
 
    bool askNSConfirmation(int value);
 

	
 
private slots:
 
    void onNumOfSamples(int value);
 
    void onAutoScaleChecked(bool checked);
 
    void onYScaleChanged();
 
    void onRangeSelected();
 
};
 

	
 
#endif // PLOTCONTROLPANEL_H
0 comments (0 inline, 0 general)