]> sigrok.org Git - pulseview.git/blobdiff - pv/prop/bool.cpp
Fix #1035 by checking for exceptions when accessing config
[pulseview.git] / pv / prop / bool.cpp
index ac142fd440403c6b015c441f6e6b1d13ae3923aa..0cd1604612661730348acd099580f7ac02947725 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 <QCheckBox>
+#include <QDebug>
 
-#include "bool.h"
+#include <libsigrokcxx/libsigrokcxx.hpp>
+
+#include "bool.hpp"
 
 namespace pv {
 namespace prop {
 
-Bool::Bool(QString name, Getter getter, Setter setter) :
-       Property(name, getter, setter),
-       _check_box(NULL)
-{
-}
-
-Bool::~Bool()
+Bool::Bool(QString name, QString desc, Getter getter, Setter setter) :
+       Property(name, desc, getter, setter),
+       check_box_(nullptr)
 {
 }
 
 QWidget* Bool::get_widget(QWidget *parent, bool auto_commit)
 {
-       if (_check_box)
-               return _check_box;
-
-       _check_box = new QCheckBox(name(), parent);
+       if (check_box_)
+               return check_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;
+       }
 
-       GVariant *const value = _getter ? _getter() : NULL;
+       check_box_ = new QCheckBox(name(), parent);
+       check_box_->setToolTip(desc());
 
-       if (value) {
-               _check_box->setCheckState(g_variant_get_boolean(value) ?
-                       Qt::Checked : Qt::Unchecked);
-               g_variant_unref(value);
-       }
+       update_widget();
 
        if (auto_commit)
-               connect(_check_box, SIGNAL(stateChanged(int)),
+               connect(check_box_, SIGNAL(stateChanged(int)),
                        this, SLOT(on_state_changed(int)));
 
-       return _check_box;
+       return check_box_;
 }
 
 bool Bool::labeled_widget() const
@@ -64,15 +69,35 @@ bool Bool::labeled_widget() const
        return true;
 }
 
+void Bool::update_widget()
+{
+       if (!check_box_)
+               return;
+
+       Glib::VariantBase variant;
+
+       try {
+               variant = getter_();
+       } catch (const sigrok::Error &e) {
+               qWarning() << tr("Querying config key %1 resulted in %2").arg(name_, e.what());
+               return;
+       }
+
+       assert(variant.gobj());
+       bool value = Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(
+               variant).get();
+
+       check_box_->setCheckState(value ? Qt::Checked : Qt::Unchecked);
+}
+
 void Bool::commit()
 {
-       assert(_setter);
+       assert(setter_);
 
-       if (!_check_box)
+       if (!check_box_)
                return;
 
-       _setter(g_variant_new_boolean(
-               _check_box->checkState() == Qt::Checked));
+       setter_(Glib::Variant<bool>::create(check_box_->checkState() == Qt::Checked));
 }
 
 void Bool::on_state_changed(int)
@@ -80,5 +105,5 @@ void Bool::on_state_changed(int)
        commit();
 }
 
-} // prop
-} // pv
+}  // namespace prop
+}  // namespace pv