diff --git a/doomsday/libappfw/include/de/VariableLineEditWidget b/doomsday/libappfw/include/de/VariableLineEditWidget new file mode 100644 index 0000000000..80629677a4 --- /dev/null +++ b/doomsday/libappfw/include/de/VariableLineEditWidget @@ -0,0 +1,2 @@ +#include "widgets/variablelineeditwidget.h" + diff --git a/doomsday/libappfw/include/de/widgets/variablelineeditwidget.h b/doomsday/libappfw/include/de/widgets/variablelineeditwidget.h new file mode 100644 index 0000000000..8055832215 --- /dev/null +++ b/doomsday/libappfw/include/de/widgets/variablelineeditwidget.h @@ -0,0 +1,56 @@ +/** @file variablelineeditwidget.h Edits the text of a variable. + * + * @authors Copyright (c) 2014 Jaakko Keränen + * + * @par License + * LGPL: http://www.gnu.org/licenses/lgpl.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at your + * option) any later version. This program 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 Lesser + * General Public License for more details. You should have received a copy of + * the GNU Lesser General Public License along with this program; if not, see: + * http://www.gnu.org/licenses + */ + +#ifndef LIBAPPFW_VARIABLELINEEDITWIDGET_H +#define LIBAPPFW_VARIABLELINEEDITWIDGET_H + +#include + +#include "../LineEditWidget" + +namespace de { + +/** + * Widget for editing the value of a text variable. + */ +class LIBAPPFW_PUBLIC VariableLineEditWidget : public LineEditWidget +{ + Q_OBJECT + +public: + /// Thrown when the variable is gone and someone tries to access it. @ingroup errors + DENG2_ERROR(VariableMissingError); + +public: + VariableLineEditWidget(Variable &variable, String const &name = ""); + + Variable &variable() const; + +public slots: + void updateFromVariable(); + +protected slots: + void setVariableFromWidget(); + +private: + DENG2_PRIVATE(d) +}; + +} // namespace de + +#endif // LIBAPPFW_VARIABLELINEEDITWIDGET_H diff --git a/doomsday/libappfw/libappfw.pro b/doomsday/libappfw/libappfw.pro index ad5d220b13..64fa4cf804 100644 --- a/doomsday/libappfw/libappfw.pro +++ b/doomsday/libappfw/libappfw.pro @@ -81,6 +81,7 @@ publicHeaders(root, \ include/de/WindowSystem \ include/de/WindowTransform \ include/de/VariableChoiceWidget \ + include/de/VariableLineEditWidget \ include/de/VariableToggleWidget \ include/de/VRConfig \ \ @@ -170,6 +171,7 @@ publicHeaders(widgets, \ include/de/widgets/tabwidget.h \ include/de/widgets/togglewidget.h \ include/de/widgets/variablechoicewidget.h \ + include/de/widgets/variablelineeditwidget.h \ include/de/widgets/variabletogglewidget.h \ ) @@ -231,6 +233,7 @@ SOURCES += \ src/widgets/tabwidget.cpp \ src/widgets/togglewidget.cpp \ src/widgets/variablechoicewidget.cpp \ + src/widgets/variablelineeditwidget.cpp \ src/widgets/variabletogglewidget.cpp \ src/windowsystem.cpp \ src/windowtransform.cpp diff --git a/doomsday/libappfw/src/widgets/variablelineeditwidget.cpp b/doomsday/libappfw/src/widgets/variablelineeditwidget.cpp new file mode 100644 index 0000000000..8365c47eca --- /dev/null +++ b/doomsday/libappfw/src/widgets/variablelineeditwidget.cpp @@ -0,0 +1,103 @@ +/** @file variablelineeditwidget.cpp Edits the text of a variable. + * + * @authors Copyright (c) 2014 Jaakko Keränen + * + * @par License + * LGPL: http://www.gnu.org/licenses/lgpl.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at your + * option) any later version. This program 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 Lesser + * General Public License for more details. You should have received a copy of + * the GNU Lesser General Public License along with this program; if not, see: + * http://www.gnu.org/licenses + */ + +#include "de/VariableLineEditWidget" + +#include + +namespace de { + +DENG2_PIMPL(VariableLineEditWidget), +DENG2_OBSERVES(Variable, Deletion), +DENG2_OBSERVES(Variable, Change ) +{ + Variable *var; + + Instance(Public *i, Variable &variable) : Base(i), var(&variable) + { + updateFromVariable(); + + var->audienceForDeletion() += this; + var->audienceForChange() += this; + } + + ~Instance() + { + if(var) + { + var->audienceForDeletion() -= this; + var->audienceForChange() -= this; + } + } + + void updateFromVariable() + { + if(!var) return; + + self.setText(var->value()); + } + + void setVariableFromWidget() + { + if(!var) return; + + var->audienceForChange() -= this; + var->set(TextValue(self.text())); + var->audienceForChange() += this; + } + + void variableValueChanged(Variable &, Value const &) + { + updateFromVariable(); + } + + void variableBeingDeleted(Variable &) + { + var = 0; + self.disable(); + } +}; + +VariableLineEditWidget::VariableLineEditWidget(Variable &variable, String const &name) + : LineEditWidget(name), d(new Instance(this, variable)) +{ + connect(this, SIGNAL(editorContentChanged()), + this, SLOT(setVariableFromWidget())); +} + +Variable &VariableLineEditWidget::variable() const +{ + if(!d->var) + { + throw VariableMissingError("VariableLineEditWidget::variable", + "Widget is not associated with a variable"); + } + return *d->var; +} + +void VariableLineEditWidget::updateFromVariable() +{ + d->updateFromVariable(); +} + +void VariableLineEditWidget::setVariableFromWidget() +{ + d->setVariableFromWidget(); +} + +} // namespace de