]> sigrok.org Git - pulseview.git/blobdiff - pv/prop/string.cpp
Fix random clazy warnings
[pulseview.git] / pv / prop / string.cpp
index b9de789b90298042104bd0b60b2ea94e8bd45668..b82b496eed42175f7361cd792a4ab8196036eee7 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 <QLineEdit>
 #include <QSpinBox>
 
-#include "string.h"
+#include <libsigrokcxx/libsigrokcxx.hpp>
 
-using namespace std;
-using namespace boost;
+#include "string.hpp"
+
+using std::string;
+
+using Glib::ustring;
 
 namespace pv {
 namespace prop {
 
 String::String(QString name,
+       QString desc,
        Getter getter,
        Setter setter) :
-       Property(name, getter, setter),
-       _line_edit(NULL)
+       Property(name, desc, getter, setter),
+       line_edit_(nullptr)
 {
 }
 
-QWidget* String::get_widget(QWidget *parent)
+QWidget* String::get_widget(QWidget *parent, bool auto_commit)
 {
-       if (_line_edit)
-               return _line_edit;
+       if (line_edit_)
+               return line_edit_;
 
-       _line_edit = new QLineEdit(parent);
+       if (!getter_)
+               return nullptr;
 
-       GVariant *const value = _getter ? _getter() : NULL;
-       if (value) {
-               _line_edit->setText(QString(
-                       g_variant_get_string(value, NULL)));
-               g_variant_unref(value);
+       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;
        }
 
-       return _line_edit;
+       line_edit_ = new QLineEdit(parent);
+
+       update_widget();
+
+       if (auto_commit)
+               connect(line_edit_, SIGNAL(textEdited(const QString&)),
+                       this, SLOT(on_text_edited(const QString&)));
+
+       return line_edit_;
+}
+
+void String::update_widget()
+{
+       if (!line_edit_)
+               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());
+
+       string value = Glib::VariantBase::cast_dynamic<Glib::Variant<ustring>>(
+               variant).get();
+
+       line_edit_->setText(QString::fromStdString(value));
 }
 
 void String::commit()
 {
-       assert(_setter);
+       assert(setter_);
 
-       if (!_line_edit)
+       if (!line_edit_)
                return;
 
-       QByteArray ba = _line_edit->text().toLocal8Bit();
-       _setter(g_variant_new_string(ba.data()));
+       QByteArray ba = line_edit_->text().toLocal8Bit();
+       setter_(Glib::Variant<ustring>::create(ba.data()));
+}
+
+void String::on_text_edited(const QString&)
+{
+       commit();
 }
 
-} // prop
-} // pv
+}  // namespace prop
+}  // namespace pv