From dec741233dc709950fe542bcd4f69b254b33eb80 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 15 Aug 2011 17:31:22 +0300 Subject: Move setupitem class to widgets directory. --- src/CMakeLists.txt | 4 +- src/Makefile.am | 4 +- src/gui/setup_chat.cpp | 2 +- src/gui/setup_other.cpp | 3 +- src/gui/setup_perfomance.cpp | 2 +- src/gui/setup_players.cpp | 2 +- src/gui/setupitem.cpp | 507 ------------------------------------- src/gui/setupitem.h | 244 ------------------ src/gui/widgets/setupitem.cpp | 507 +++++++++++++++++++++++++++++++++++++ src/gui/widgets/setupitem.h | 244 ++++++++++++++++++ src/gui/widgets/setuptabscroll.cpp | 3 +- 11 files changed, 760 insertions(+), 762 deletions(-) delete mode 100644 src/gui/setupitem.cpp delete mode 100644 src/gui/setupitem.h create mode 100644 src/gui/widgets/setupitem.cpp create mode 100644 src/gui/widgets/setupitem.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fba255c1c..413a72f65 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -181,6 +181,8 @@ SET(SRCS gui/widgets/resizegrip.h gui/widgets/scrollarea.cpp gui/widgets/scrollarea.h + gui/widgets/setupitem.cpp + gui/widgets/setupitem.h gui/widgets/setuptab.cpp gui/widgets/setuptab.h gui/widgets/setuptabscroll.cpp @@ -299,8 +301,6 @@ SET(SRCS gui/serverdialog.h gui/setup.cpp gui/setup.h - gui/setupitem.cpp - gui/setupitem.h gui/setup_audio.cpp gui/setup_audio.h gui/setup_colors.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 697575b97..57bb4e620 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -184,6 +184,8 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \ gui/widgets/resizegrip.h \ gui/widgets/scrollarea.cpp \ gui/widgets/scrollarea.h \ + gui/widgets/setupitem.cpp \ + gui/widgets/setupitem.h \ gui/widgets/setuptab.cpp \ gui/widgets/setuptab.h \ gui/widgets/setuptabscroll.cpp \ @@ -302,8 +304,6 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \ gui/serverdialog.h \ gui/setup.cpp \ gui/setup.h \ - gui/setupitem.cpp \ - gui/setupitem.h \ gui/setup_audio.cpp \ gui/setup_audio.h \ gui/setup_colors.cpp \ diff --git a/src/gui/setup_chat.cpp b/src/gui/setup_chat.cpp index 7ce44a552..722a3f8b2 100644 --- a/src/gui/setup_chat.cpp +++ b/src/gui/setup_chat.cpp @@ -25,7 +25,6 @@ #include "gui/chatwindow.h" #include "gui/editdialog.h" -#include "gui/setupitem.h" #include "gui/widgets/button.h" #include "gui/widgets/chattab.h" @@ -34,6 +33,7 @@ #include "gui/widgets/label.h" #include "gui/widgets/layouthelper.h" #include "gui/widgets/scrollarea.h" +#include "gui/widgets/setupitem.h" #include "configuration.h" #include "localplayer.h" diff --git a/src/gui/setup_other.cpp b/src/gui/setup_other.cpp index 3b603a8ee..b405f5045 100644 --- a/src/gui/setup_other.cpp +++ b/src/gui/setup_other.cpp @@ -23,10 +23,9 @@ #include "gui/setup_other.h" -#include "gui/setupitem.h" - #include "gui/widgets/layouthelper.h" #include "gui/widgets/scrollarea.h" +#include "gui/widgets/setupitem.h" #include "configuration.h" #include "logger.h" diff --git a/src/gui/setup_perfomance.cpp b/src/gui/setup_perfomance.cpp index 583f2909a..dfb119b91 100644 --- a/src/gui/setup_perfomance.cpp +++ b/src/gui/setup_perfomance.cpp @@ -25,7 +25,6 @@ #include "gui/chatwindow.h" #include "gui/editdialog.h" -#include "gui/setupitem.h" #include "gui/widgets/button.h" #include "gui/widgets/chattab.h" @@ -34,6 +33,7 @@ #include "gui/widgets/label.h" #include "gui/widgets/layouthelper.h" #include "gui/widgets/scrollarea.h" +#include "gui/widgets/setupitem.h" #include "configuration.h" #include "localplayer.h" diff --git a/src/gui/setup_players.cpp b/src/gui/setup_players.cpp index acc6b3d3c..78c77f6f1 100644 --- a/src/gui/setup_players.cpp +++ b/src/gui/setup_players.cpp @@ -25,7 +25,6 @@ #include "gui/chatwindow.h" #include "gui/editdialog.h" -#include "gui/setupitem.h" #include "gui/widgets/button.h" #include "gui/widgets/chattab.h" @@ -34,6 +33,7 @@ #include "gui/widgets/label.h" #include "gui/widgets/layouthelper.h" #include "gui/widgets/scrollarea.h" +#include "gui/widgets/setupitem.h" #include "configuration.h" #include "localplayer.h" diff --git a/src/gui/setupitem.cpp b/src/gui/setupitem.cpp deleted file mode 100644 index defdb4005..000000000 --- a/src/gui/setupitem.cpp +++ /dev/null @@ -1,507 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2011 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "gui/setupitem.h" - -#include "configuration.h" -#include "main.h" -#include "logger.h" - -#include "gui/editdialog.h" - -#include "gui/widgets/button.h" -#include "gui/widgets/checkbox.h" -#include "gui/widgets/horizontcontainer.h" -#include "gui/widgets/inttextfield.h" -#include "gui/widgets/label.h" -#include "gui/widgets/layouthelper.h" -#include "gui/widgets/tabbedarea.h" -#include "gui/widgets/textfield.h" -#include "gui/widgets/vertcontainer.h" - -#include "utils/dtor.h" -#include "utils/gettext.h" - - -SetupItem::SetupItem(std::string text, std::string description, - std::string keyName, SetupTabScroll *parent, - std::string eventName, bool mainConfig) : - mText(text), - mDescription(description), - mKeyName(keyName), - mParent(parent), - mEventName(eventName), - mMainConfig(mainConfig), - mUseDefault(false), - mValue(""), - mDefault(""), - mWidget(0), - mValueType(VBOOL) -{ -} - -SetupItem::SetupItem(std::string text, std::string description, - std::string keyName, SetupTabScroll *parent, - std::string eventName, std::string def, bool mainConfig) : - mText(text), - mDescription(description), - mKeyName(keyName), - mParent(parent), - mEventName(eventName), - mMainConfig(mainConfig), - mUseDefault(true), - mValue(""), - mDefault(def), - mWidget(0), - mValueType(VBOOL) -{ -} - -SetupItem::~SetupItem() -{ - -} - -Configuration *SetupItem::getConfig() -{ - if (mMainConfig) - return &config; - else - return &serverConfig; -} - -void SetupItem::load() -{ - Configuration *cfg = getConfig(); - if (mUseDefault) - { - mValue = cfg->getValue(mKeyName, mDefault); - } - else - { - switch (mValueType) - { - case VBOOL: - if (cfg->getBoolValue(mKeyName)) - mValue = "1"; - else - mValue = "0"; - break; - case VSTR: - default: - mValue = cfg->getStringValue(mKeyName); - break; - case VINT: - mValue = toString(cfg->getIntValue(mKeyName)); - break; - case VNONE: - break; - } - } -} - -void SetupItem::save() -{ - Configuration *cfg = getConfig(); - cfg->setValue(mKeyName, mValue); -} - -std::string SetupItem::getActionEventId() -{ - if (!mWidget) - return ""; - - return mWidget->getActionEventId(); -} - -void SetupItem::action(const gcn::ActionEvent &event) -{ - if (!mWidget) - return; - - if (event.getId() == mWidget->getActionEventId()) - action(); -} - -void SetupItem::action() -{ - fromWidget(); -} - -void SetupItem::apply(std::string eventName A_UNUSED) -{ - save(); -} - -void SetupItem::cancel(std::string eventName A_UNUSED) -{ - load(); - toWidget(); -} - -void SetupItem::externalUpdated(std::string eventName A_UNUSED) -{ - load(); - toWidget(); -} - -SetupItemCheckBox::SetupItemCheckBox(std::string text, std::string description, - std::string keyName, - SetupTabScroll *parent, - std::string eventName, bool mainConfig) : - SetupItem(text, description, keyName, parent, eventName, mainConfig) -{ - createControls(); -} - -SetupItemCheckBox::SetupItemCheckBox(std::string text, std::string description, - std::string keyName, - SetupTabScroll *parent, - std::string eventName, std::string def, - bool mainConfig) : - SetupItem(text, description, keyName, parent, eventName, def, mainConfig) -{ - createControls(); -} - -SetupItemCheckBox::~SetupItemCheckBox() -{ - mWidget = 0; -} - -void SetupItemCheckBox::createControls() -{ - load(); - mCheckBox = new CheckBox(mText, mValue != "0", mParent, mEventName); - mWidget = mCheckBox; - mParent->getContainer()->add(mWidget); - mParent->addControl(this); - mParent->addActionListener(this); - mWidget->addActionListener(this); -} - -void SetupItemCheckBox::fromWidget() -{ - if (!mCheckBox) - return; - - if (mCheckBox->isSelected()) - mValue = "1"; - else - mValue = "0"; -} - -void SetupItemCheckBox::toWidget() -{ - if (!mCheckBox) - return; - - mCheckBox->setSelected(mValue != "0"); -} - - -SetupItemTextField::SetupItemTextField(std::string text, - std::string description, - std::string keyName, - SetupTabScroll *parent, - std::string eventName, - bool mainConfig) : - SetupItem(text, description, keyName, parent, eventName, mainConfig), - mHorizont(0), - mLabel(0), - mTextField(0), - mButton(0), - mEditDialog(0) -{ - mValueType = VSTR; - createControls(); -} - -SetupItemTextField::SetupItemTextField(std::string text, - std::string description, - std::string keyName, - SetupTabScroll *parent, - std::string eventName, std::string def, - bool mainConfig) : - SetupItem(text, description, keyName, parent, eventName, def, mainConfig), - mHorizont(0), - mLabel(0), - mTextField(0), - mButton(0), - mEditDialog(0) -{ - mValueType = VSTR; - createControls(); -} - -SetupItemTextField::~SetupItemTextField() -{ - mHorizont = 0; - mWidget = 0; - mTextField = 0; - mLabel = 0; - mButton = 0; -} - -void SetupItemTextField::createControls() -{ - load(); - mHorizont = new HorizontContainer(32, 2); - - mLabel = new Label(mText); - mTextField = new TextField(mValue, true, mParent, mEventName); - mButton = new Button(_("Edit"), mEventName + "_EDIT", mParent); - mWidget = mTextField; - mTextField->setWidth(200); - mHorizont->add(mLabel); - mHorizont->add(mTextField); - mHorizont->add(mButton); - - mParent->getContainer()->add(mHorizont, true, 4); - mParent->addControl(this); - mParent->addControl(this, mEventName + "_EDIT"); - mParent->addControl(this, mEventName + "_EDIT_OK"); - mParent->addActionListener(this); - mWidget->addActionListener(this); - mButton->addActionListener(this); -} - -void SetupItemTextField::fromWidget() -{ - if (!mTextField) - return; - - mValue = mTextField->getText(); -} - -void SetupItemTextField::toWidget() -{ - if (!mTextField) - return; - - mTextField->setText(mValue); -} - -void SetupItemTextField::action(const gcn::ActionEvent &event) -{ - if (!mTextField) - return; - - if (event.getId() == mWidget->getActionEventId()) - { - fromWidget(); - } - else if (event.getId() == mEventName + "_EDIT") - { - mEditDialog = new EditDialog(mText, mTextField->getText(), - mEventName + "_EDIT_OK"); - mEditDialog->addActionListener(this); - } - else if (event.getId() == mEventName + "_EDIT_OK") - { - mTextField->setText(mEditDialog->getMsg()); - mEditDialog = 0; - } -} - -void SetupItemTextField::apply(std::string eventName) -{ - if (eventName != mEventName) - return; - - fromWidget(); - save(); -} - - -SetupItemIntTextField::SetupItemIntTextField(std::string text, - std::string description, - std::string keyName, - SetupTabScroll *parent, - std::string eventName, - int min, int max, - bool mainConfig) : - SetupItem(text, description, keyName, parent, eventName, mainConfig), - mHorizont(0), - mLabel(0), - mTextField(0), - mButton(0), - mMin(min), - mMax(max), - mEditDialog(0) -{ - mValueType = VSTR; - createControls(); -} - -SetupItemIntTextField::SetupItemIntTextField(std::string text, - std::string description, - std::string keyName, - SetupTabScroll *parent, - std::string eventName, - int min, int max, - std::string def, - bool mainConfig) : - SetupItem(text, description, keyName, parent, eventName, def, mainConfig), - mHorizont(0), - mLabel(0), - mTextField(0), - mButton(0), - mMin(min), - mMax(max), - mEditDialog(0) -{ - mValueType = VSTR; - createControls(); -} - -SetupItemIntTextField::~SetupItemIntTextField() -{ - mHorizont = 0; - mWidget = 0; - mTextField = 0; - mLabel = 0; - mButton = 0; -} - -void SetupItemIntTextField::createControls() -{ - load(); - mHorizont = new HorizontContainer(32, 2); - - mLabel = new Label(mText); - mTextField = new IntTextField(atoi(mValue.c_str()), mMin, mMax, true, 30); - mTextField->setActionEventId(mEventName); - mTextField->addActionListener(mParent); - - mButton = new Button(_("Edit"), mEventName + "_EDIT", mParent); - mWidget = mTextField; - mTextField->setWidth(50); - mHorizont->add(mLabel); - mHorizont->add(mTextField); - mHorizont->add(mButton); - - mParent->getContainer()->add(mHorizont, true, 4); - mParent->addControl(this); - mParent->addControl(this, mEventName + "_EDIT"); - mParent->addControl(this, mEventName + "_EDIT_OK"); - mParent->addActionListener(this); - mWidget->addActionListener(this); - mButton->addActionListener(this); -} - -void SetupItemIntTextField::fromWidget() -{ - if (!mTextField) - return; - - mValue = mTextField->getText(); -} - -void SetupItemIntTextField::toWidget() -{ - if (!mTextField) - return; - - mTextField->setText(mValue); -} - -void SetupItemIntTextField::action(const gcn::ActionEvent &event) -{ - if (!mTextField) - return; - - if (event.getId() == mWidget->getActionEventId()) - { - fromWidget(); - } - else if (event.getId() == mEventName + "_EDIT") - { - mEditDialog = new EditDialog(mText, mTextField->getText(), - mEventName + "_EDIT_OK"); - mEditDialog->addActionListener(this); - } - else if (event.getId() == mEventName + "_EDIT_OK") - { - mTextField->setValue(atoi(mEditDialog->getMsg().c_str())); - mEditDialog = 0; - } -} - -void SetupItemIntTextField::apply(std::string eventName) -{ - if (eventName != mEventName) - return; - - fromWidget(); - save(); -} - -SetupItemLabel::SetupItemLabel(std::string text, std::string description, - SetupTabScroll *parent, bool separator) : - SetupItem(text, description, "", parent, "", "", true), - mLabel(0), - mIsSeparator(separator) -{ - mValueType = VNONE; - createControls(); -} - -SetupItemLabel::~SetupItemLabel() -{ - mWidget = 0; - mLabel = 0; -} - -void SetupItemLabel::createControls() -{ - if (mIsSeparator) - { - const std::string str = " \342\200\225\342\200\225\342\200\225" - "\342\200\225\342\200\225 "; - mLabel = new Label(str + mText + str); - } - else - { - mLabel = new Label(mText); - } - - mWidget = mLabel; - mParent->getContainer()->add(mWidget); - mParent->addControl(this); - mParent->addActionListener(this); - mWidget->addActionListener(this); -} - -void SetupItemLabel::fromWidget() -{ -} - -void SetupItemLabel::toWidget() -{ -} - -void SetupItemLabel::action(const gcn::ActionEvent &event A_UNUSED) -{ -} - -void SetupItemLabel::apply(std::string eventName A_UNUSED) -{ -} diff --git a/src/gui/setupitem.h b/src/gui/setupitem.h deleted file mode 100644 index 3396edea3..000000000 --- a/src/gui/setupitem.h +++ /dev/null @@ -1,244 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2011 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef SETUPITEM_H -#define SETUPITEM_H - -#include "gui/widgets/tabbedarea.h" - -#include "guichanfwd.h" - -#include "gui/widgets/setuptabscroll.h" -#include "gui/widgets/window.h" - -#include -#include - -#include - -#ifdef __GNUC__ -#define A_UNUSED __attribute__ ((unused)) -#else -#define A_UNUSED -#endif - -class CheckBox; -class Configuration; -class ContainerPlacer; -class EditDialog; -class HorizontContainer; -class IntTextField; -class Label; -class TextField; - -class SetupItem : public gcn::ActionListener -{ - public: - enum - { - VBOOL = 0, - VSTR, - VINT, - VNONE - }; - - SetupItem(std::string text, std::string description, - std::string keyName, SetupTabScroll *parent, - std::string eventName, bool mainConfig); - - SetupItem(std::string text, std::string description, - std::string keyName, SetupTabScroll *parent, - std::string eventName, std::string def, bool mainConfig); - - ~SetupItem(); - - void load(); - - void save(); - - virtual void fromWidget() = 0; - - virtual void toWidget() = 0; - - void setWidget(gcn::Widget *widget) - { mWidget = widget; } - - gcn::Widget *getWidget() - { return mWidget; } - - Configuration *getConfig(); - - virtual std::string getActionEventId(); - - virtual void action(const gcn::ActionEvent &event); - - virtual void action(); - - virtual void apply(std::string eventName); - - virtual void cancel(std::string eventName); - - virtual void externalUpdated(std::string eventName); -// virtual int add(ContainerPlacer &place, int x, int y, int width); - - bool isMainConfig() - { return mMainConfig; } - - protected: - std::string mText; - - std::string mDescription; - - std::string mKeyName; - - SetupTabScroll *mParent; - - std::string mEventName; - - bool mMainConfig; - - bool mUseDefault; - - std::string mValue; - - std::string mDefault; - - gcn::Widget *mWidget; - - std::list mTempWidgets; - - int mValueType; -}; - -class SetupItemCheckBox : public SetupItem -{ - public: - SetupItemCheckBox(std::string text, std::string description, - std::string keyName, SetupTabScroll *parent, - std::string eventName, bool mainConfig = true); - - SetupItemCheckBox(std::string text, std::string description, - std::string keyName, SetupTabScroll *parent, - std::string eventName, std::string def, - bool mainConfig = true); - - ~SetupItemCheckBox(); - - void createControls(); - - void fromWidget(); - - void toWidget(); - - protected: - CheckBox *mCheckBox; -}; - -class SetupItemTextField : public SetupItem -{ - public: - SetupItemTextField(std::string text, std::string description, - std::string keyName, SetupTabScroll *parent, - std::string eventName, bool mainConfig = true); - - SetupItemTextField(std::string text, std::string description, - std::string keyName, SetupTabScroll *parent, - std::string eventName, std::string def, - bool mainConfig = true); - - ~SetupItemTextField(); - - void createControls(); - - void fromWidget(); - - void toWidget(); - - void action(const gcn::ActionEvent &event); - - void apply(std::string eventName); - - protected: - HorizontContainer *mHorizont; - Label *mLabel; - TextField *mTextField; - Button *mButton; - EditDialog *mEditDialog; -}; - -class SetupItemIntTextField : public SetupItem -{ - public: - SetupItemIntTextField(std::string text, std::string description, - std::string keyName, SetupTabScroll *parent, - std::string eventName, int min, int max, - bool mainConfig = true); - - SetupItemIntTextField(std::string text, std::string description, - std::string keyName, SetupTabScroll *parent, - std::string eventName, int min, int max, - std::string def, bool mainConfig = true); - - ~SetupItemIntTextField(); - - void createControls(); - - void fromWidget(); - - void toWidget(); - - void action(const gcn::ActionEvent &event); - - void apply(std::string eventName); - - protected: - HorizontContainer *mHorizont; - Label *mLabel; - IntTextField *mTextField; - Button *mButton; - int mMin; - int mMax; - EditDialog *mEditDialog; -}; - -class SetupItemLabel : public SetupItem -{ - public: - SetupItemLabel(std::string text, std::string description, - SetupTabScroll *parent, bool separator = true); - - ~SetupItemLabel(); - - void createControls(); - - void fromWidget(); - - void toWidget(); - - void action(const gcn::ActionEvent &event); - - void apply(std::string eventName); - - protected: - Label *mLabel; - bool mIsSeparator; -}; - -#endif diff --git a/src/gui/widgets/setupitem.cpp b/src/gui/widgets/setupitem.cpp new file mode 100644 index 000000000..fd2c937dc --- /dev/null +++ b/src/gui/widgets/setupitem.cpp @@ -0,0 +1,507 @@ +/* + * The ManaPlus Client + * Copyright (C) 2011 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "gui/widgets/setupitem.h" + +#include "configuration.h" +#include "main.h" +#include "logger.h" + +#include "gui/editdialog.h" + +#include "gui/widgets/button.h" +#include "gui/widgets/checkbox.h" +#include "gui/widgets/horizontcontainer.h" +#include "gui/widgets/inttextfield.h" +#include "gui/widgets/label.h" +#include "gui/widgets/layouthelper.h" +#include "gui/widgets/tabbedarea.h" +#include "gui/widgets/textfield.h" +#include "gui/widgets/vertcontainer.h" + +#include "utils/dtor.h" +#include "utils/gettext.h" + + +SetupItem::SetupItem(std::string text, std::string description, + std::string keyName, SetupTabScroll *parent, + std::string eventName, bool mainConfig) : + mText(text), + mDescription(description), + mKeyName(keyName), + mParent(parent), + mEventName(eventName), + mMainConfig(mainConfig), + mUseDefault(false), + mValue(""), + mDefault(""), + mWidget(0), + mValueType(VBOOL) +{ +} + +SetupItem::SetupItem(std::string text, std::string description, + std::string keyName, SetupTabScroll *parent, + std::string eventName, std::string def, bool mainConfig) : + mText(text), + mDescription(description), + mKeyName(keyName), + mParent(parent), + mEventName(eventName), + mMainConfig(mainConfig), + mUseDefault(true), + mValue(""), + mDefault(def), + mWidget(0), + mValueType(VBOOL) +{ +} + +SetupItem::~SetupItem() +{ + +} + +Configuration *SetupItem::getConfig() +{ + if (mMainConfig) + return &config; + else + return &serverConfig; +} + +void SetupItem::load() +{ + Configuration *cfg = getConfig(); + if (mUseDefault) + { + mValue = cfg->getValue(mKeyName, mDefault); + } + else + { + switch (mValueType) + { + case VBOOL: + if (cfg->getBoolValue(mKeyName)) + mValue = "1"; + else + mValue = "0"; + break; + case VSTR: + default: + mValue = cfg->getStringValue(mKeyName); + break; + case VINT: + mValue = toString(cfg->getIntValue(mKeyName)); + break; + case VNONE: + break; + } + } +} + +void SetupItem::save() +{ + Configuration *cfg = getConfig(); + cfg->setValue(mKeyName, mValue); +} + +std::string SetupItem::getActionEventId() +{ + if (!mWidget) + return ""; + + return mWidget->getActionEventId(); +} + +void SetupItem::action(const gcn::ActionEvent &event) +{ + if (!mWidget) + return; + + if (event.getId() == mWidget->getActionEventId()) + action(); +} + +void SetupItem::action() +{ + fromWidget(); +} + +void SetupItem::apply(std::string eventName A_UNUSED) +{ + save(); +} + +void SetupItem::cancel(std::string eventName A_UNUSED) +{ + load(); + toWidget(); +} + +void SetupItem::externalUpdated(std::string eventName A_UNUSED) +{ + load(); + toWidget(); +} + +SetupItemCheckBox::SetupItemCheckBox(std::string text, std::string description, + std::string keyName, + SetupTabScroll *parent, + std::string eventName, bool mainConfig) : + SetupItem(text, description, keyName, parent, eventName, mainConfig) +{ + createControls(); +} + +SetupItemCheckBox::SetupItemCheckBox(std::string text, std::string description, + std::string keyName, + SetupTabScroll *parent, + std::string eventName, std::string def, + bool mainConfig) : + SetupItem(text, description, keyName, parent, eventName, def, mainConfig) +{ + createControls(); +} + +SetupItemCheckBox::~SetupItemCheckBox() +{ + mWidget = 0; +} + +void SetupItemCheckBox::createControls() +{ + load(); + mCheckBox = new CheckBox(mText, mValue != "0", mParent, mEventName); + mWidget = mCheckBox; + mParent->getContainer()->add(mWidget); + mParent->addControl(this); + mParent->addActionListener(this); + mWidget->addActionListener(this); +} + +void SetupItemCheckBox::fromWidget() +{ + if (!mCheckBox) + return; + + if (mCheckBox->isSelected()) + mValue = "1"; + else + mValue = "0"; +} + +void SetupItemCheckBox::toWidget() +{ + if (!mCheckBox) + return; + + mCheckBox->setSelected(mValue != "0"); +} + + +SetupItemTextField::SetupItemTextField(std::string text, + std::string description, + std::string keyName, + SetupTabScroll *parent, + std::string eventName, + bool mainConfig) : + SetupItem(text, description, keyName, parent, eventName, mainConfig), + mHorizont(0), + mLabel(0), + mTextField(0), + mButton(0), + mEditDialog(0) +{ + mValueType = VSTR; + createControls(); +} + +SetupItemTextField::SetupItemTextField(std::string text, + std::string description, + std::string keyName, + SetupTabScroll *parent, + std::string eventName, std::string def, + bool mainConfig) : + SetupItem(text, description, keyName, parent, eventName, def, mainConfig), + mHorizont(0), + mLabel(0), + mTextField(0), + mButton(0), + mEditDialog(0) +{ + mValueType = VSTR; + createControls(); +} + +SetupItemTextField::~SetupItemTextField() +{ + mHorizont = 0; + mWidget = 0; + mTextField = 0; + mLabel = 0; + mButton = 0; +} + +void SetupItemTextField::createControls() +{ + load(); + mHorizont = new HorizontContainer(32, 2); + + mLabel = new Label(mText); + mTextField = new TextField(mValue, true, mParent, mEventName); + mButton = new Button(_("Edit"), mEventName + "_EDIT", mParent); + mWidget = mTextField; + mTextField->setWidth(200); + mHorizont->add(mLabel); + mHorizont->add(mTextField); + mHorizont->add(mButton); + + mParent->getContainer()->add(mHorizont, true, 4); + mParent->addControl(this); + mParent->addControl(this, mEventName + "_EDIT"); + mParent->addControl(this, mEventName + "_EDIT_OK"); + mParent->addActionListener(this); + mWidget->addActionListener(this); + mButton->addActionListener(this); +} + +void SetupItemTextField::fromWidget() +{ + if (!mTextField) + return; + + mValue = mTextField->getText(); +} + +void SetupItemTextField::toWidget() +{ + if (!mTextField) + return; + + mTextField->setText(mValue); +} + +void SetupItemTextField::action(const gcn::ActionEvent &event) +{ + if (!mTextField) + return; + + if (event.getId() == mWidget->getActionEventId()) + { + fromWidget(); + } + else if (event.getId() == mEventName + "_EDIT") + { + mEditDialog = new EditDialog(mText, mTextField->getText(), + mEventName + "_EDIT_OK"); + mEditDialog->addActionListener(this); + } + else if (event.getId() == mEventName + "_EDIT_OK") + { + mTextField->setText(mEditDialog->getMsg()); + mEditDialog = 0; + } +} + +void SetupItemTextField::apply(std::string eventName) +{ + if (eventName != mEventName) + return; + + fromWidget(); + save(); +} + + +SetupItemIntTextField::SetupItemIntTextField(std::string text, + std::string description, + std::string keyName, + SetupTabScroll *parent, + std::string eventName, + int min, int max, + bool mainConfig) : + SetupItem(text, description, keyName, parent, eventName, mainConfig), + mHorizont(0), + mLabel(0), + mTextField(0), + mButton(0), + mMin(min), + mMax(max), + mEditDialog(0) +{ + mValueType = VSTR; + createControls(); +} + +SetupItemIntTextField::SetupItemIntTextField(std::string text, + std::string description, + std::string keyName, + SetupTabScroll *parent, + std::string eventName, + int min, int max, + std::string def, + bool mainConfig) : + SetupItem(text, description, keyName, parent, eventName, def, mainConfig), + mHorizont(0), + mLabel(0), + mTextField(0), + mButton(0), + mMin(min), + mMax(max), + mEditDialog(0) +{ + mValueType = VSTR; + createControls(); +} + +SetupItemIntTextField::~SetupItemIntTextField() +{ + mHorizont = 0; + mWidget = 0; + mTextField = 0; + mLabel = 0; + mButton = 0; +} + +void SetupItemIntTextField::createControls() +{ + load(); + mHorizont = new HorizontContainer(32, 2); + + mLabel = new Label(mText); + mTextField = new IntTextField(atoi(mValue.c_str()), mMin, mMax, true, 30); + mTextField->setActionEventId(mEventName); + mTextField->addActionListener(mParent); + + mButton = new Button(_("Edit"), mEventName + "_EDIT", mParent); + mWidget = mTextField; + mTextField->setWidth(50); + mHorizont->add(mLabel); + mHorizont->add(mTextField); + mHorizont->add(mButton); + + mParent->getContainer()->add(mHorizont, true, 4); + mParent->addControl(this); + mParent->addControl(this, mEventName + "_EDIT"); + mParent->addControl(this, mEventName + "_EDIT_OK"); + mParent->addActionListener(this); + mWidget->addActionListener(this); + mButton->addActionListener(this); +} + +void SetupItemIntTextField::fromWidget() +{ + if (!mTextField) + return; + + mValue = mTextField->getText(); +} + +void SetupItemIntTextField::toWidget() +{ + if (!mTextField) + return; + + mTextField->setText(mValue); +} + +void SetupItemIntTextField::action(const gcn::ActionEvent &event) +{ + if (!mTextField) + return; + + if (event.getId() == mWidget->getActionEventId()) + { + fromWidget(); + } + else if (event.getId() == mEventName + "_EDIT") + { + mEditDialog = new EditDialog(mText, mTextField->getText(), + mEventName + "_EDIT_OK"); + mEditDialog->addActionListener(this); + } + else if (event.getId() == mEventName + "_EDIT_OK") + { + mTextField->setValue(atoi(mEditDialog->getMsg().c_str())); + mEditDialog = 0; + } +} + +void SetupItemIntTextField::apply(std::string eventName) +{ + if (eventName != mEventName) + return; + + fromWidget(); + save(); +} + +SetupItemLabel::SetupItemLabel(std::string text, std::string description, + SetupTabScroll *parent, bool separator) : + SetupItem(text, description, "", parent, "", "", true), + mLabel(0), + mIsSeparator(separator) +{ + mValueType = VNONE; + createControls(); +} + +SetupItemLabel::~SetupItemLabel() +{ + mWidget = 0; + mLabel = 0; +} + +void SetupItemLabel::createControls() +{ + if (mIsSeparator) + { + const std::string str = " \342\200\225\342\200\225\342\200\225" + "\342\200\225\342\200\225 "; + mLabel = new Label(str + mText + str); + } + else + { + mLabel = new Label(mText); + } + + mWidget = mLabel; + mParent->getContainer()->add(mWidget); + mParent->addControl(this); + mParent->addActionListener(this); + mWidget->addActionListener(this); +} + +void SetupItemLabel::fromWidget() +{ +} + +void SetupItemLabel::toWidget() +{ +} + +void SetupItemLabel::action(const gcn::ActionEvent &event A_UNUSED) +{ +} + +void SetupItemLabel::apply(std::string eventName A_UNUSED) +{ +} diff --git a/src/gui/widgets/setupitem.h b/src/gui/widgets/setupitem.h new file mode 100644 index 000000000..3396edea3 --- /dev/null +++ b/src/gui/widgets/setupitem.h @@ -0,0 +1,244 @@ +/* + * The ManaPlus Client + * Copyright (C) 2011 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef SETUPITEM_H +#define SETUPITEM_H + +#include "gui/widgets/tabbedarea.h" + +#include "guichanfwd.h" + +#include "gui/widgets/setuptabscroll.h" +#include "gui/widgets/window.h" + +#include +#include + +#include + +#ifdef __GNUC__ +#define A_UNUSED __attribute__ ((unused)) +#else +#define A_UNUSED +#endif + +class CheckBox; +class Configuration; +class ContainerPlacer; +class EditDialog; +class HorizontContainer; +class IntTextField; +class Label; +class TextField; + +class SetupItem : public gcn::ActionListener +{ + public: + enum + { + VBOOL = 0, + VSTR, + VINT, + VNONE + }; + + SetupItem(std::string text, std::string description, + std::string keyName, SetupTabScroll *parent, + std::string eventName, bool mainConfig); + + SetupItem(std::string text, std::string description, + std::string keyName, SetupTabScroll *parent, + std::string eventName, std::string def, bool mainConfig); + + ~SetupItem(); + + void load(); + + void save(); + + virtual void fromWidget() = 0; + + virtual void toWidget() = 0; + + void setWidget(gcn::Widget *widget) + { mWidget = widget; } + + gcn::Widget *getWidget() + { return mWidget; } + + Configuration *getConfig(); + + virtual std::string getActionEventId(); + + virtual void action(const gcn::ActionEvent &event); + + virtual void action(); + + virtual void apply(std::string eventName); + + virtual void cancel(std::string eventName); + + virtual void externalUpdated(std::string eventName); +// virtual int add(ContainerPlacer &place, int x, int y, int width); + + bool isMainConfig() + { return mMainConfig; } + + protected: + std::string mText; + + std::string mDescription; + + std::string mKeyName; + + SetupTabScroll *mParent; + + std::string mEventName; + + bool mMainConfig; + + bool mUseDefault; + + std::string mValue; + + std::string mDefault; + + gcn::Widget *mWidget; + + std::list mTempWidgets; + + int mValueType; +}; + +class SetupItemCheckBox : public SetupItem +{ + public: + SetupItemCheckBox(std::string text, std::string description, + std::string keyName, SetupTabScroll *parent, + std::string eventName, bool mainConfig = true); + + SetupItemCheckBox(std::string text, std::string description, + std::string keyName, SetupTabScroll *parent, + std::string eventName, std::string def, + bool mainConfig = true); + + ~SetupItemCheckBox(); + + void createControls(); + + void fromWidget(); + + void toWidget(); + + protected: + CheckBox *mCheckBox; +}; + +class SetupItemTextField : public SetupItem +{ + public: + SetupItemTextField(std::string text, std::string description, + std::string keyName, SetupTabScroll *parent, + std::string eventName, bool mainConfig = true); + + SetupItemTextField(std::string text, std::string description, + std::string keyName, SetupTabScroll *parent, + std::string eventName, std::string def, + bool mainConfig = true); + + ~SetupItemTextField(); + + void createControls(); + + void fromWidget(); + + void toWidget(); + + void action(const gcn::ActionEvent &event); + + void apply(std::string eventName); + + protected: + HorizontContainer *mHorizont; + Label *mLabel; + TextField *mTextField; + Button *mButton; + EditDialog *mEditDialog; +}; + +class SetupItemIntTextField : public SetupItem +{ + public: + SetupItemIntTextField(std::string text, std::string description, + std::string keyName, SetupTabScroll *parent, + std::string eventName, int min, int max, + bool mainConfig = true); + + SetupItemIntTextField(std::string text, std::string description, + std::string keyName, SetupTabScroll *parent, + std::string eventName, int min, int max, + std::string def, bool mainConfig = true); + + ~SetupItemIntTextField(); + + void createControls(); + + void fromWidget(); + + void toWidget(); + + void action(const gcn::ActionEvent &event); + + void apply(std::string eventName); + + protected: + HorizontContainer *mHorizont; + Label *mLabel; + IntTextField *mTextField; + Button *mButton; + int mMin; + int mMax; + EditDialog *mEditDialog; +}; + +class SetupItemLabel : public SetupItem +{ + public: + SetupItemLabel(std::string text, std::string description, + SetupTabScroll *parent, bool separator = true); + + ~SetupItemLabel(); + + void createControls(); + + void fromWidget(); + + void toWidget(); + + void action(const gcn::ActionEvent &event); + + void apply(std::string eventName); + + protected: + Label *mLabel; + bool mIsSeparator; +}; + +#endif diff --git a/src/gui/widgets/setuptabscroll.cpp b/src/gui/widgets/setuptabscroll.cpp index 805c0c8c7..30bb66469 100644 --- a/src/gui/widgets/setuptabscroll.cpp +++ b/src/gui/widgets/setuptabscroll.cpp @@ -20,10 +20,9 @@ #include "gui/widgets/setuptabscroll.h" -#include "gui/setupitem.h" - #include "gui/widgets/layouthelper.h" #include "gui/widgets/scrollarea.h" +#include "gui/widgets/setupitem.h" #include "gui/widgets/vertcontainer.h" #include "debug.h" -- cgit v1.2.3-60-g2f50