OpenMW
apps/opencs/view/settings/settingwidget.hpp
Go to the documentation of this file.
00001 #ifndef SETTINGWIDGET_HPP
00002 #define SETTINGWIDGET_HPP
00003 
00004 #include <QLabel>
00005 #include <QCheckBox>
00006 #include <QSpinBox>
00007 #include <QLineEdit>
00008 #include <QRadioButton>
00009 #include <QComboBox>
00010 #include <QListWidget>
00011 #include <QGroupBox>
00012 #include <QHBoxLayout>
00013 #include <QVBoxLayout>
00014 
00015 #include "abstractwidget.hpp"
00016 
00017 namespace CSVSettings
00018 {
00019 
00021     template <typename T1>
00022     class SettingWidget : public AbstractWidget
00023     {
00024 
00025         T1 *mWidget;
00026 
00027     public:
00028 
00029         explicit SettingWidget (WidgetDef &def, QLayout *layout, QWidget* parent = 0)
00030             : AbstractWidget (layout, parent), mWidget (new T1 (parent))
00031         {
00032             mWidget->setText(def.caption);
00033             build (mWidget, def, true);
00034             mWidget->setChecked(def.isDefault);
00035 
00036             connect (mWidget, SIGNAL (toggled (bool)),
00037                      this, SLOT (slotUpdateItem (bool)));
00038         }
00039 
00040         QWidget *widget()   { return mWidget; }
00041 
00042     private:
00043 
00044         void updateWidget (const QString &value)
00045         {
00046             if ( value == mWidget->objectName() && !mWidget->isChecked() )
00047                 mWidget->setChecked (true);
00048         }
00049     };
00050 
00052     template <>
00053     class SettingWidget <QSpinBox>: public AbstractWidget
00054     {
00055 
00056         QSpinBox *mWidget;
00057 
00058     public:
00059 
00060         SettingWidget (WidgetDef &def, QLayout *layout, QWidget *parent = 0)
00061             : AbstractWidget (layout, parent), mWidget (new QSpinBox (parent))
00062         {
00063             def.caption += tr(" (%1 to %2)").arg(def.minMax->left).arg(def.minMax->right);
00064 
00065             mWidget->setMaximum     (def.minMax->right.toInt());
00066             mWidget->setMinimum     (def.minMax->left.toInt());
00067             mWidget->setValue       (def.value.toInt());
00068 
00069             build (mWidget, def);
00070 
00071             connect (mWidget, SIGNAL (valueChanged (int)),
00072                      this, SLOT (slotUpdateItem (int)));
00073 
00074             mWidget->setAlignment (getAlignment(def.valueAlignment));
00075 
00076 
00077         }
00078 
00079         QWidget *widget()   { return mWidget; }
00080 
00081     private:
00082 
00083         void updateWidget (const QString &value)
00084         {
00085             int intVal = value.toInt();
00086 
00087             if (intVal >= mWidget->minimum() && intVal <= mWidget->maximum() && intVal != mWidget->value())
00088                 mWidget->setValue (intVal);
00089         }
00090 
00091     signals:
00092 
00093     };
00094 
00096     template <>
00097     class SettingWidget <QComboBox>: public CSVSettings::AbstractWidget
00098     {
00099 
00100         QComboBox *mWidget;
00101 
00102 
00103     public:
00104 
00105         explicit SettingWidget(WidgetDef &def, QLayout *layout, QWidget *parent = 0)
00106             : AbstractWidget (layout, parent), mWidget (new QComboBox (parent))
00107         {
00108             int i = 0;
00109 
00110             foreach (QString item, *(def.valueList))
00111             {
00112                 mWidget->addItem (item);
00113 
00114                 if (item == def.value)
00115                     mWidget->setCurrentIndex(i);
00116 
00117                 i++;
00118             }
00119 
00120             build (mWidget, def);
00121 
00122             connect (mWidget, SIGNAL (currentIndexChanged (const QString &)),
00123                      this, SLOT (slotUpdateItem (const QString &)));
00124 
00125             //center the combo box items
00126             mWidget->setEditable (true);
00127             mWidget->lineEdit()->setReadOnly (true);
00128             mWidget->lineEdit()->setAlignment (getAlignment(def.valueAlignment));
00129 
00130             QFlags<Qt::AlignmentFlag> alignment = mWidget->lineEdit()->alignment();
00131 
00132             for (int j = 0; j < mWidget->count(); j++)
00133                 mWidget->setItemData (j, QVariant(alignment), Qt::TextAlignmentRole);
00134         }
00135 
00136         QWidget *widget()   { return mWidget; }
00137 
00138     private:
00139 
00140         void updateWidget (const QString &value)
00141         {
00142             if (mWidget->currentText() != value)
00143                 mWidget->setCurrentIndex(mWidget->findText(value));
00144         }
00145 
00146     };
00147 
00149     template <>
00150     class SettingWidget <QLineEdit>: public CSVSettings::AbstractWidget
00151     {
00152 
00153         QLineEdit *mWidget;
00154 
00155     public:
00156 
00157         explicit SettingWidget(WidgetDef &def, QLayout *layout, QWidget *parent = 0)
00158             : AbstractWidget (layout, parent), mWidget (new QLineEdit (parent))
00159         {
00160             if (!def.inputMask.isEmpty())
00161                 mWidget->setInputMask (def.inputMask);
00162 
00163             mWidget->setText (def.value);
00164 
00165             build (mWidget, def);
00166 
00167             connect (mWidget, SIGNAL (textChanged (const QString &)),
00168                      this, SLOT (slotUpdateItem (const QString &)));
00169 
00170             mWidget->setAlignment (getAlignment(def.valueAlignment));
00171         }
00172 
00173         QWidget *widget()   { return mWidget; }
00174 
00175         void updateWidget (const QString &value)
00176         {
00177             if (mWidget->text() != value)
00178             mWidget->setText(value);
00179         }
00180     };
00181 
00184     template <>
00185     class SettingWidget <QListWidget>: public CSVSettings::AbstractWidget
00186     {
00187 
00188         QListWidget *mWidget;
00189 
00190     public:
00191 
00192         explicit SettingWidget(WidgetDef &def, QLayout *layout, QWidget *parent = 0 )
00193             : AbstractWidget (layout, parent), mWidget (new QListWidget (parent))
00194         {
00195             int i = 0;
00196 
00197             foreach (QString item, *(def.valueList))
00198             {
00199                 mWidget->addItem (item);
00200 
00201                 if (item == def.value) {}
00202                 i++;
00203             }
00204             build (mWidget, def);
00205         }
00206 
00207         QWidget *widget()   { return mWidget; }
00208 
00209     private:
00210         void updateWidget (const QString &value) {}
00211     };
00212 
00213 }
00214 #endif // SETTINGWIDGET_HPP