* 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 <cfloat>
+#include <cmath>
#include <QComboBox>
-#include "enum.h"
+#include "enum.hpp"
-using namespace boost;
-using namespace std;
+using std::abs;
+using std::pair;
+using std::vector;
namespace pv {
namespace prop {
-Enum::Enum(QString name,
- vector<pair<GVariant*, QString> > values,
+Enum::Enum(QString name, QString desc,
+ vector<pair<Glib::VariantBase, QString> > values,
Getter getter, Setter setter) :
- Property(name, getter, setter),
- _values(values),
- _selector(NULL)
+ Property(name, desc, getter, setter),
+ values_(values),
+ selector_(nullptr)
{
}
-Enum::~Enum()
+QWidget* Enum::get_widget(QWidget *parent, bool auto_commit)
{
- for (unsigned int i = 0; i < _values.size(); i++)
- g_variant_unref(_values[i].first);
-}
+ if (selector_)
+ return selector_;
-QWidget* Enum::get_widget(QWidget *parent)
-{
- if (_selector)
- return _selector;
+ if (!getter_)
+ return nullptr;
- GVariant *const value = _getter ? _getter() : NULL;
+ Glib::VariantBase variant = getter_();
+ if (!variant.gobj())
+ return nullptr;
- _selector = new QComboBox(parent);
- for (unsigned int i = 0; i < _values.size(); i++) {
- const pair<GVariant*, QString> &v = _values[i];
- _selector->addItem(v.second, qVariantFromValue((void*)v.first));
- if (value && g_variant_compare(v.first, value) == 0)
- _selector->setCurrentIndex(i);
+ selector_ = new QComboBox(parent);
+ for (unsigned int i = 0; i < values_.size(); i++) {
+ const pair<Glib::VariantBase, QString> &v = values_[i];
+ selector_->addItem(v.second, qVariantFromValue(v.first));
}
- g_variant_unref(value);
+ update_widget();
+
+ if (auto_commit)
+ connect(selector_, SIGNAL(currentIndexChanged(int)),
+ this, SLOT(on_current_item_changed(int)));
+
+ return selector_;
+}
+
+void Enum::update_widget()
+{
+ if (!selector_)
+ return;
- return _selector;
+ Glib::VariantBase variant = getter_();
+ assert(variant.gobj());
+
+ for (unsigned int i = 0; i < values_.size(); i++) {
+ const pair<Glib::VariantBase, QString> &v = values_[i];
+
+ // g_variant_equal() doesn't handle floating point properly
+ if (v.first.is_of_type(Glib::VariantType("d"))) {
+ gdouble a, b;
+ g_variant_get(variant.gobj(), "d", &a);
+ g_variant_get((GVariant*)(v.first.gobj()), "d", &b);
+ if (abs(a - b) <= 2 * DBL_EPSILON)
+ selector_->setCurrentIndex(i);
+ } else {
+ // Check for "(dd)" type and handle it if it's found
+ if (v.first.is_of_type(Glib::VariantType("(dd)"))) {
+ gdouble a1, a2, b1, b2;
+ g_variant_get(variant.gobj(), "(dd)", &a1, &a2);
+ g_variant_get((GVariant*)(v.first.gobj()), "(dd)", &b1, &b2);
+ if ((abs(a1 - b1) <= 2 * DBL_EPSILON) && \
+ (abs(a2 - b2) <= 2 * DBL_EPSILON))
+ selector_->setCurrentIndex(i);
+
+ } else
+ // Handle all other types
+ if (v.first.equal(variant))
+ selector_->setCurrentIndex(i);
+ }
+ }
}
void Enum::commit()
{
- assert(_setter);
+ assert(setter_);
- if (!_selector)
+ if (!selector_)
return;
- const int index = _selector->currentIndex();
+ const int index = selector_->currentIndex();
if (index < 0)
return;
- _setter((GVariant*)_selector->itemData(index).value<void*>());
+ setter_(selector_->itemData(index).value<Glib::VariantBase>());
+}
+
+void Enum::on_current_item_changed(int)
+{
+ commit();
}
-} // prop
-} // pv
+} // namespace prop
+} // namespace pv