Sayonara Player
PreferenceInterface.h
1 /* PreferenceInterface.h */
2 
3 /* Copyright (C) 2011-2017 Lucio Carreras
4  *
5  * This file is part of sayonara player
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef PREFERENCEINTERFACE_H
22 #define PREFERENCEINTERFACE_H
23 
24 #include <QAction>
25 #include <QByteArray>
26 
27 #include "GUI/Helper/SayonaraWidget/SayonaraDialog.h"
28 #include "GUI/Helper/SayonaraWidget/SayonaraWidget.h"
29 
30 
38 class PreferenceAction : public QAction
39 {
40  Q_OBJECT
41 
42 public:
49  PreferenceAction(const QString& text, QWidget* preference_interface);
50 };
51 
52 template <typename T>
58 class PreferenceInterface : public T
59 {
60 private:
61  PreferenceAction* _action=nullptr;
62  bool _is_initialized;
63  QByteArray _geometry;
64 
65 protected:
71  virtual void init_ui()=0;
72 
73 
77  virtual void retranslate_ui()=0;
78 
79 
80  template<typename W, typename UiClass>
86  void setup_parent(W* widget, UiClass** ui) {
87  *ui = new UiClass();
88  (*ui)->setupUi(widget);
89 
90  _is_initialized = true;
91 
92  widget->language_changed();
93  }
94 
110  virtual void language_changed() override final
111  {
112  translate_action();
113 
114  if(!is_ui_initialized()){
115  return;
116  }
117 
118  QString new_name = get_action_name();
119  this->setWindowTitle(new_name);
120 
121  retranslate_ui();
122  }
123 
124 
129  {
130  QString new_name = this->get_action_name();
131  this->get_action()->setText(new_name + "...");
132  }
133 
134 
135 protected:
136 
141  void showEvent(QShowEvent* e) override
142  {
143  {
144  if(!is_ui_initialized()){
145  init_ui();
146  }
147 
148  T::showEvent(e);
149 
150  if(!_geometry.isEmpty()){
151  this->restoreGeometry(_geometry);
152  }
153  }
154  }
155 
156 
161  void closeEvent(QCloseEvent* e) override
162  {
163  T::closeEvent(e);
164  }
165 
166 
167 public:
168 
173  PreferenceInterface(QWidget* parent=nullptr) :
174  T(parent)
175  {
176  _is_initialized = false;
177  }
178 
179 
180 
185  virtual bool is_ui_initialized() const final
186  {
187  return _is_initialized;
188  }
189 
190 
195  virtual QAction* get_action() final
196  {
197  // action has to be initialized here, because pure
198  // virtual get_action_name should not be called from ctor
199  QString name = get_action_name();
200  if(!_action){
201  _action = new PreferenceAction(name, this);
202  }
203 
204  _action->setText(name + "...");
205  return _action;
206  }
207 
208 
209 
214  virtual QString get_action_name() const=0;
215 
216 
217 
222  virtual void commit()=0;
223 
229  virtual void revert()=0;
230 };
231 
232 #endif // PREFERENCEINTERFACE_H
Template class for implementing preference dialogs and preference widgets.
Definition: PreferenceInterface.h:58
virtual QAction * get_action() final
get action with translated text
Definition: PreferenceInterface.h:195
PreferenceInterface(QWidget *parent=nullptr)
Standard constructor.
Definition: PreferenceInterface.h:173
virtual bool is_ui_initialized() const final
checks if ui has already been initialized.
Definition: PreferenceInterface.h:185
The action, which is used to access the Preference.
Definition: PreferenceInterface.h:38
virtual void language_changed() override final
automatically called when language has changed. When overriding this method. Overriding this method s...
Definition: PreferenceInterface.h:110
void closeEvent(QCloseEvent *e) override
closes the widget
Definition: PreferenceInterface.h:161
void showEvent(QShowEvent *e) override
shows the widget and automatically calls init_ui()
Definition: PreferenceInterface.h:141
void setup_parent(W *widget, UiClass **ui)
Sets up the Preference dialog. After this method, the dialog is "ready to use" This method should be ...
Definition: PreferenceInterface.h:86
void translate_action()
Sets the new translated action name.
Definition: PreferenceInterface.h:128
PreferenceAction(const QString &text, QWidget *preference_interface)
PreferenceAction Create QAction object, which is automatically connected to the show event of the und...