Sayonara Player
PreferenceInterface.h
1 /* PreferenceInterface.h */
2 
3 /* Copyright (C) 2011-2016 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 
22 
23 #ifndef PREFERENCEINTERFACE_H
24 #define PREFERENCEINTERFACE_H
25 
26 #include <QAction>
27 #include <QByteArray>
28 #include <QLabel>
29 #include <QShowEvent>
30 #include <QCloseEvent>
31 #include <QString>
32 
33 #include "GUI/Helper/SayonaraWidget/SayonaraDialog.h"
34 #include "GUI/Helper/SayonaraWidget/SayonaraWidget.h"
35 
36 
44 class PreferenceAction : public QAction
45 {
46  Q_OBJECT
47 
48 public:
55  PreferenceAction(const QString& text, QWidget* preference_interface) :
56  QAction(nullptr)
57  {
58  this->setText(text + "...");
59  connect(this, &QAction::triggered, preference_interface, &QWidget::show);
60  }
61 
62 };
63 
64 template <typename T>
70 class PreferenceInterface : public T
71 {
72 
73 private:
74  PreferenceAction* _action=nullptr;
75  bool _is_initialized;
76  QByteArray _geometry;
77 
78 protected:
84  virtual void init_ui()=0;
85 
86  template<typename W>
92  void setup_parent(W* widget) {
93 
94  widget->setupUi(widget);
95 
96  _is_initialized = true;
97 
98  widget->language_changed();
99  }
100 
116  void language_changed() override
117  {
118  translate_action();
119 
120  if(!is_ui_initialized()){
121  return;
122  }
123 
124  QString new_name = get_action_name();
125  this->setWindowTitle(new_name);
126  }
127 
128 
133  {
134  QString new_name = this->get_action_name();
135  this->get_action()->setText(new_name + "...");
136  }
137 
138 
139 
140 protected:
141 
146  void showEvent(QShowEvent* e) override
147  {
148  {
149  if(!is_ui_initialized()){
150  init_ui();
151  }
152 
153  T::showEvent(e);
154 
155  if(!_geometry.isEmpty()){
156  this->restoreGeometry(_geometry);
157  }
158  }
159  }
160 
161 
166  void closeEvent(QCloseEvent* e) override
167  {
168  T::closeEvent(e);
169  }
170 
171 
172 public:
173 
178  PreferenceInterface(QWidget* parent=nullptr) :
179  T(parent)
180  {
181  _is_initialized = false;
182  }
183 
184 
189  virtual bool is_ui_initialized() const final
190  {
191  return _is_initialized;
192  }
193 
194 
199  virtual QAction* get_action() final
200  {
201  // action has to be initialized here, because pure
202  // virtual get_action_name should not be called from ctor
203  QString name = get_action_name();
204  if(!_action){
205  _action = new PreferenceAction(name, this);
206  }
207 
208  _action->setText(name + "...");
209  return _action;
210  }
211 
216  virtual QString get_action_name() const=0;
217 
222  virtual void commit()=0;
223 
229  virtual void revert()=0;
230 
231 };
232 
233 #endif // PREFERENCEINTERFACE_H
Template class for implementing preference dialogs and preference widgets.
Definition: PreferenceInterface.h:70
virtual QAction * get_action() final
get action with translated text
Definition: PreferenceInterface.h:199
PreferenceInterface(QWidget *parent=nullptr)
Standard constructor.
Definition: PreferenceInterface.h:178
virtual bool is_ui_initialized() const final
checks if ui has already been initialized.
Definition: PreferenceInterface.h:189
The action, which is used to access the Preference.
Definition: PreferenceInterface.h:44
void closeEvent(QCloseEvent *e) override
closes the widget
Definition: PreferenceInterface.h:166
void showEvent(QShowEvent *e) override
shows the widget and automatically calls init_ui()
Definition: PreferenceInterface.h:146
void setup_parent(W *widget)
Sets up the Preference dialog. After this method, the dialog is "ready to use" This method should be ...
Definition: PreferenceInterface.h:92
void translate_action()
Sets the new translated action name.
Definition: PreferenceInterface.h:132
void language_changed() override
automatically called when language has changed. When overriding this method. Overriding this method s...
Definition: PreferenceInterface.h:116
PreferenceAction(const QString &text, QWidget *preference_interface)
PreferenceAction Create QAction object, which is automatically connected to the show event of the und...
Definition: PreferenceInterface.h:55