]> sigrok.org Git - pulseview.git/blobdiff - pv/prop/double.cpp
Fix random clazy warnings
[pulseview.git] / pv / prop / double.cpp
index 54540bc68ded41c0575c3202726ac383665b5814..f39ae844fc97ce34b4519d4a67546a867e8cdb74 100644 (file)
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <assert.h>
+#include <cassert>
 
+#include <QDebug>
 #include <QDoubleSpinBox>
 
-#include "double.h"
+#include <libsigrokcxx/libsigrokcxx.hpp>
+
+#include "double.hpp"
 
 using boost::optional;
 using std::pair;
@@ -31,65 +33,85 @@ namespace pv {
 namespace prop {
 
 Double::Double(QString name,
+       QString desc,
        int decimals,
        QString suffix,
        optional< pair<double, double> > range,
        optional<double> step,
        Getter getter,
        Setter setter) :
-       Property(name, getter, setter),
-       _decimals(decimals),
-       _suffix(suffix),
-       _range(range),
-       _step(step),
-       _spin_box(NULL)
-{
-}
-
-Double::~Double()
+       Property(name, desc, getter, setter),
+       decimals_(decimals),
+       suffix_(suffix),
+       range_(range),
+       step_(step),
+       spin_box_(nullptr)
 {
 }
 
 QWidget* Double::get_widget(QWidget *parent, bool auto_commit)
 {
-       if (_spin_box)
-               return _spin_box;
+       if (spin_box_)
+               return spin_box_;
+
+       if (!getter_)
+               return nullptr;
+
+       try {
+               Glib::VariantBase variant = getter_();
+               if (!variant.gobj())
+                       return nullptr;
+       } catch (const sigrok::Error &e) {
+               qWarning() << tr("Querying config key %1 resulted in %2").arg(name_, e.what());
+               return nullptr;
+       }
+
+       spin_box_ = new QDoubleSpinBox(parent);
+       spin_box_->setDecimals(decimals_);
+       spin_box_->setSuffix(suffix_);
+       if (range_)
+               spin_box_->setRange(range_->first, range_->second);
+       if (step_)
+               spin_box_->setSingleStep(*step_);
+
+       update_widget();
 
-       if (!_getter)
-               return NULL;
+       if (auto_commit)
+               connect(spin_box_, SIGNAL(valueChanged(double)),
+                       this, SLOT(on_value_changed(double)));
 
-       Glib::VariantBase variant = _getter();
-       if (!variant.gobj())
-               return NULL;
+       return spin_box_;
+}
 
-       double value = Glib::VariantBase::cast_dynamic<Glib::Variant<double>>(
-               variant).get();
+void Double::update_widget()
+{
+       if (!spin_box_)
+               return;
 
-       _spin_box = new QDoubleSpinBox(parent);
-       _spin_box->setDecimals(_decimals);
-       _spin_box->setSuffix(_suffix);
-       if (_range)
-               _spin_box->setRange(_range->first, _range->second);
-       if (_step)
-               _spin_box->setSingleStep(*_step);
+       Glib::VariantBase variant;
 
-       _spin_box->setValue(value);
+       try {
+               variant = getter_();
+       } catch (const sigrok::Error &e) {
+               qWarning() << tr("Querying config key %1 resulted in %2").arg(name_, e.what());
+               return;
+       }
 
-       if (auto_commit)
-               connect(_spin_box, SIGNAL(valueChanged(double)),
-                       this, SLOT(on_value_changed(double)));
+       assert(variant.gobj());
 
-       return _spin_box;
+       double value = Glib::VariantBase::cast_dynamic<Glib::Variant<double>>(
+               variant).get();
+       spin_box_->setValue(value);
 }
 
 void Double::commit()
 {
-       assert(_setter);
+       assert(setter_);
 
-       if (!_spin_box)
+       if (!spin_box_)
                return;
 
-       _setter(Glib::Variant<double>::create(_spin_box->value()));
+       setter_(Glib::Variant<double>::create(spin_box_->value()));
 }
 
 void Double::on_value_changed(double)
@@ -97,5 +119,5 @@ void Double::on_value_changed(double)
        commit();
 }
 
-} // prop
-} // pv
+}  // namespace prop
+}  // namespace pv