QSettingsDialog  1.0.0
A Qt library to easily create a settings dialog for user configurable settings
qsettingsdialogwidget.h
1 #ifndef QSETTINGSDIALOGWIDGET_H
2 #define QSETTINGSDIALOGWIDGET_H
3 
4 #include "qsettingsdialog_global.h"
5 #include "qsettingswidget.h"
6 #include <QWidget>
7 #include <QDialog>
8 #include <QIcon>
9 class QPushButton;
10 
12 class QSETTINGSDIALOGSHARED_EXPORT QSettingsDialogWidgetBase : public QSettingsWidget<QWidget>
13 {
14 public:
16  QSettingsDialogWidgetBase(QWidget *parent = nullptr);
17 
19  virtual QString buttonText() const = 0;
21  virtual QIcon buttonIcon() const;
23  virtual void showDialog() = 0;
24 
25 protected:
27  void showEvent(QShowEvent *event) final;
28 
30  static void wrapInDialog(QDialog *dialog, QWidget *element, bool fixedSize = false);
31 
32 private:
33  QPushButton *btn;
34 };
35 
37 template<typename TSettingsWidget>
39 {
40 public:
42  QSettingsDialogWidget(QWidget *parent = nullptr) :
44  dialog(nullptr),
45  widget(new TSettingsWidget(this))
46  {
47  if(this->widget->inherits("QDialog"))
48  this->dialog = (QDialog*)this->widget;
49  else {
50  this->dialog = new QDialog(this);
51  wrapInDialog(this->dialog, this->widget);
52  }
53  }
54 
55  // QSettingsWidgetBase interface
56  bool hasValueChanged() const final {
57  return this->widget->hasValueChanged();
58  }
59  void resetValueChanged() final {
60  this->widget->resetValueChanged();
61  }
62  void setValue(const QVariant &value) final {
63  this->widget->setValue(value);
64  }
65  QVariant getValue() const final {
66  return this->widget->getValue();
67  }
68  void resetValue() final {
69  this->widget->resetValue();
70  }
71  bool searchExpression(const QRegularExpression &regex) final {
72  return this->widget->searchExpression(regex);
73  }
74 
75  // QSettingsDialogWidgetBase interface
76  QString buttonText() const override {
77  return this->widget->windowTitle();
78  }
79  QIcon buttonIcon() const override {
80  return this->widget->windowIcon();
81  }
82  void showDialog() final {
83  this->dialog->open();
84  }
85 
86 private:
87  QDialog *dialog;
88  TSettingsWidget *widget;
89 };
90 
91 #endif // QSETTINGSDIALOGWIDGET_H
QIcon buttonIcon() const override
Returns the icon to be shown in the dialog button.
Definition: qsettingsdialogwidget.h:79
void resetValue() final
Resets the edit widgets "value".
Definition: qsettingsdialogwidget.h:68
void resetValueChanged() final
Resets the value changed status to unchanged.
Definition: qsettingsdialogwidget.h:59
QString buttonText() const override
Returns the text to be shown in the dialog button.
Definition: qsettingsdialogwidget.h:76
QVariant getValue() const final
Gets the edit widgets "value".
Definition: qsettingsdialogwidget.h:65
A generic class to easily create a dialog widget from a normal one.
Definition: qsettingsdialogwidget.h:38
The base class for dialog based edit widgets in the settings dialog.
Definition: qsettingsdialogwidget.h:12
bool hasValueChanged() const final
Will be called to determine, whether the value has changed or not.
Definition: qsettingsdialogwidget.h:56
bool searchExpression(const QRegularExpression &regex) final
Will be called to search for a specific expression inside the edit widget.
Definition: qsettingsdialogwidget.h:71
QSettingsDialogWidget(QWidget *parent=nullptr)
Creates a new dialog widget with a parent.
Definition: qsettingsdialogwidget.h:42
void setValue(const QVariant &value) final
Sets the edit widgets "value".
Definition: qsettingsdialogwidget.h:62
void showDialog() final
Will be called to show the dialog.
Definition: qsettingsdialogwidget.h:82
Generic base class for settings widgets.
Definition: qsettingswidget.h:47