const-correctness in dialogxml (dialogs)

This commit is contained in:
2023-01-22 17:12:56 -05:00
parent 455af36c81
commit a416368e0d
4 changed files with 23 additions and 17 deletions

View File

@@ -740,7 +740,7 @@ void cDialog::setBg(short n){
bg = n; bg = n;
} }
short cDialog::getBg() { short cDialog::getBg() const {
return bg; return bg;
} }
@@ -748,7 +748,7 @@ void cDialog::setDefTextClr(sf::Color clr){
defTextClr = clr; defTextClr = clr;
} }
sf::Color cDialog::getDefTextClr() { sf::Color cDialog::getDefTextClr() const {
return defTextClr; return defTextClr;
} }
@@ -767,7 +767,7 @@ void cDialog::untoast() {
this->getControl(currentFocus).triggerFocusHandler(*this, currentFocus, false); this->getControl(currentFocus).triggerFocusHandler(*this, currentFocus, false);
} }
bool cDialog::accepted() { bool cDialog::accepted() const {
return didAccept; return didAccept;
} }
@@ -1057,6 +1057,10 @@ cControl& cDialog::operator[](std::string id){
return getControl(id); return getControl(id);
} }
const cControl& cDialog::operator[](std::string id) const {
return const_cast<cDialog&>(*this).getControl(id);
}
cControl& cDialog::getControl(std::string id) { cControl& cDialog::getControl(std::string id) {
ctrlIter iter = controls.find(id); ctrlIter iter = controls.find(id);
if(iter != controls.end()) return *(iter->second); if(iter != controls.end()) return *(iter->second);
@@ -1073,8 +1077,8 @@ cControl& cDialog::getControl(std::string id) {
throw std::invalid_argument(id + " does not exist in dialog " + fname); throw std::invalid_argument(id + " does not exist in dialog " + fname);
} }
bool cDialog::hasControl(std::string id) { bool cDialog::hasControl(std::string id) const {
ctrlIter iter = controls.find(id); auto iter = controls.find(id);
if(iter != controls.end()) return true; if(iter != controls.end()) return true;
iter = controls.begin(); iter = controls.begin();

View File

@@ -111,7 +111,7 @@ public:
/// @tparam type The result type. /// @tparam type The result type.
/// @throw boost::bad_any_cast if the provided result type is different from the type set by getResult(). /// @throw boost::bad_any_cast if the provided result type is different from the type set by getResult().
/// @return The dialog's result. /// @return The dialog's result.
template<typename type> type getResult(){ template<typename type> type getResult() const {
return boost::any_cast<type>(result); return boost::any_cast<type>(result);
} }
/// Set the result of the dialog. /// Set the result of the dialog.
@@ -134,13 +134,13 @@ public:
void setBg(short n); void setBg(short n);
/// Get the background pattern of the dialog. /// Get the background pattern of the dialog.
/// @return The numeric index of the background pattern. /// @return The numeric index of the background pattern.
short getBg(); short getBg() const;
/// Set the default text colour applied to new dialogs when loading from a file. /// Set the default text colour applied to new dialogs when loading from a file.
/// @param clr The text colour. /// @param clr The text colour.
void setDefTextClr(sf::Color clr); void setDefTextClr(sf::Color clr);
/// Get the default text colour applied to new dialogs when loading from a file. /// Get the default text colour applied to new dialogs when loading from a file.
/// @return The text colour. /// @return The text colour.
sf::Color getDefTextClr(); sf::Color getDefTextClr() const;
/// Set the focused text field. /// Set the focused text field.
/// @param newFocus A pointer to the text field to receive focus. /// @param newFocus A pointer to the text field to receive focus.
/// @param force If true, the change will be forced. /// @param force If true, the change will be forced.
@@ -168,11 +168,11 @@ public:
void untoast(); void untoast();
/// Determine how the dialog exited. /// Determine how the dialog exited.
/// @return the argument passed to toast() when the dialog was closed /// @return the argument passed to toast() when the dialog was closed
bool accepted(); bool accepted() const;
/// Check if a control exists with a given ID. /// Check if a control exists with a given ID.
/// @param id The unique key of the control. /// @param id The unique key of the control.
/// @return true if it exists. /// @return true if it exists.
bool hasControl(std::string id); bool hasControl(std::string id) const;
/// Get a reference to a control. /// Get a reference to a control.
/// @param id The unique key of the control. /// @param id The unique key of the control.
/// @throw std::invalid_argument if the control does not exist. /// @throw std::invalid_argument if the control does not exist.
@@ -180,6 +180,8 @@ public:
cControl& getControl(std::string id); cControl& getControl(std::string id);
/// @copydoc getControl() /// @copydoc getControl()
cControl& operator[](std::string id); cControl& operator[](std::string id);
/// @copydoc getControl()
const cControl& operator[](std::string id) const;
/// Recalculate the dialog's bounding rect. /// Recalculate the dialog's bounding rect.
/// Call this after adding controls to the dialog to ensure that the control is within the bounding rect. /// Call this after adding controls to the dialog to ensure that the control is within the bounding rect.
void recalcRect(); void recalcRect();
@@ -208,7 +210,7 @@ public:
void attachFocusHandlers(std::function<bool(cDialog&,std::string,bool)> handler, std::vector<std::string> controls); void attachFocusHandlers(std::function<bool(cDialog&,std::string,bool)> handler, std::vector<std::string> controls);
/// Get the bounding rect of the dialog. /// Get the bounding rect of the dialog.
/// @return The dialog's bounding rect. /// @return The dialog's bounding rect.
rectangle getBounds() {return winRect;} rectangle getBounds() const {return winRect;}
/// Send keyboard input to the frontmost dialog. /// Send keyboard input to the frontmost dialog.
/// Currently, only text edit fields will respond to this. /// Currently, only text edit fields will respond to this.
/// @return true if there was a dialog opened to send to. /// @return true if there was a dialog opened to send to.

View File

@@ -146,14 +146,14 @@ bool cPictChoice::onSelect(bool losing) {
return true; return true;
} }
pic_num_t cPictChoice::getPicChosen() { pic_num_t cPictChoice::getPicChosen() const {
return dlg.getResult<std::pair<pic_num_t,ePicType>>().first; return dlg.getResult<std::pair<pic_num_t,ePicType>>().first;
} }
ePicType cPictChoice::getPicChosenType() { ePicType cPictChoice::getPicChosenType() const {
return dlg.getResult<std::pair<pic_num_t,ePicType>>().second; return dlg.getResult<std::pair<pic_num_t,ePicType>>().second;
} }
size_t cPictChoice::getSelected() { size_t cPictChoice::getSelected() const {
return cur; return cur;
} }

View File

@@ -68,13 +68,13 @@ public:
bool show(size_t cur_sel); bool show(size_t cur_sel);
/// Get the chosen icon. /// Get the chosen icon.
/// @return The number of the chosen icon. /// @return The number of the chosen icon.
pic_num_t getPicChosen(); pic_num_t getPicChosen() const;
/// Get the chosen icon. /// Get the chosen icon.
/// @return The type of the chosen icon. /// @return The type of the chosen icon.
ePicType getPicChosenType(); ePicType getPicChosenType() const;
/// Get the index of the selected icon in the original list. /// Get the index of the selected icon in the original list.
/// @return The index /// @return The index
size_t getSelected(); size_t getSelected() const;
}; };
#endif #endif