1 /*
2  *  Copyright (C) 2014-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "guilib/GUIDialog.h"
12 #include "settings/SettingControl.h"
13 #include "settings/lib/ISettingCallback.h"
14 #include "threads/Timer.h"
15 #include "utils/ILocalizer.h"
16 
17 #include <set>
18 #include <vector>
19 
20 #define CONTROL_SETTINGS_LABEL 2
21 #define CONTROL_SETTINGS_DESCRIPTION 6
22 
23 #define CONTROL_SETTINGS_OKAY_BUTTON 28
24 #define CONTROL_SETTINGS_CANCEL_BUTTON 29
25 #define CONTROL_SETTINGS_CUSTOM_BUTTON 30
26 
27 #define CONTROL_SETTINGS_CUSTOM 100
28 
29 #define CONTROL_SETTINGS_START_BUTTONS -100
30 #define CONTROL_SETTINGS_START_CONTROL -80
31 
32 #define SETTINGS_RESET_SETTING_ID "settings.reset"
33 #define SETTINGS_EMPTY_CATEGORY_ID "categories.empty"
34 
35 class CGUIControl;
36 class CGUIControlBaseSetting;
37 class CGUIImage;
38 class CGUISpinControlEx;
39 class CGUIEditControl;
40 class CGUIButtonControl;
41 class CGUIRadioButtonControl;
42 class CGUISettingsSliderControl;
43 class CGUILabelControl;
44 
45 class CSetting;
46 class CSettingAction;
47 class CSettingCategory;
48 class CSettingGroup;
49 class CSettingSection;
50 
51 class CVariant;
52 
53 class ISetting;
54 
55 typedef std::shared_ptr<CGUIControlBaseSetting> BaseSettingControlPtr;
56 
57 class CGUIDialogSettingsBase : public CGUIDialog,
58                                public CSettingControlCreator,
59                                public ILocalizer,
60                                protected ITimerCallback,
61                                protected ISettingCallback
62 {
63 public:
64   CGUIDialogSettingsBase(int windowId, const std::string& xmlFile);
65   ~CGUIDialogSettingsBase() override;
66 
67   // specializations of CGUIControl
68   bool OnMessage(CGUIMessage& message) override;
69   bool OnAction(const CAction& action) override;
70   bool OnBack(int actionID) override;
71   void DoProcess(unsigned int currentTime, CDirtyRegionList& dirtyregions) override;
72 
IsConfirmed()73   virtual bool IsConfirmed() const { return m_confirmed; }
74 
75   // implementation of ILocalizer
Localize(std::uint32_t code)76   std::string Localize(std::uint32_t code) const override { return GetLocalizedString(code); }
77 
78 protected:
79   // specializations of CGUIWindow
80   void OnInitWindow() override;
81 
82   // implementations of ITimerCallback
83   void OnTimeout() override;
84 
85   // implementations of ISettingCallback
86   void OnSettingChanged(const std::shared_ptr<const CSetting>& setting) override;
87   void OnSettingPropertyChanged(const std::shared_ptr<const CSetting>& setting,
88                                 const char* propertyName) override;
89 
90   // new virtual methods
AllowResettingSettings()91   virtual bool AllowResettingSettings() const { return true; }
GetSettingLevel()92   virtual int GetSettingLevel() const { return 0; }
93   virtual std::shared_ptr<CSettingSection> GetSection() = 0;
94   virtual std::shared_ptr<CSetting> GetSetting(const std::string& settingId) = 0;
GetDelayMs()95   virtual unsigned int GetDelayMs() const { return 1500; }
96   virtual std::string GetLocalizedString(uint32_t labelId) const;
97 
OnOkay()98   virtual bool OnOkay()
99   {
100     m_confirmed = true;
101     return true;
102   }
OnCancel()103   virtual void OnCancel() {}
104 
105   virtual void SetupView();
106   virtual std::set<std::string> CreateSettings();
107   virtual void UpdateSettings();
108 
109   /*!
110     \brief Get the name for the setting entry
111 
112     Used as virtual to allow related settings dialog to give a std::string name of the setting.
113     If not used on own dialog class it handle the string from int CSetting::GetLabel(),
114     This must also be used if on related dialog no special entry is wanted.
115 
116     \param pSetting Base settings class which need the name
117     \return Name used on settings dialog
118    */
119   virtual std::string GetSettingsLabel(const std::shared_ptr<ISetting>& pSetting);
120 
121   virtual CGUIControl* AddSetting(const std::shared_ptr<CSetting>& pSetting,
122                                   float width,
123                                   int& iControlID);
124   virtual CGUIControl* AddSettingControl(CGUIControl* pControl,
125                                          BaseSettingControlPtr pSettingControl,
126                                          float width,
127                                          int& iControlID);
128 
129   virtual void SetupControls(bool createSettings = true);
130   virtual void FreeControls();
131   virtual void DeleteControls();
132   virtual void FreeSettingsControls();
133 
134   virtual void SetHeading(const CVariant& label);
135   virtual void SetDescription(const CVariant& label);
136 
137   virtual void OnResetSettings();
138 
139   /*!
140     \brief A setting control has been interacted with by the user
141 
142     This method is called when the user manually interacts (clicks,
143     edits) with a setting control. It contains handling for both
144     delayed and undelayed settings and either starts the delay timer
145     or triggers the setting change which, on success, results in a
146     callback to OnSettingChanged().
147 
148     \param pSettingControl Setting control that has been interacted with
149    */
150   virtual void OnClick(const BaseSettingControlPtr& pSettingControl);
151 
152   void UpdateSettingControl(const std::string& settingId, bool updateDisplayOnly = false);
153   void UpdateSettingControl(const BaseSettingControlPtr& pSettingControl,
154                             bool updateDisplayOnly = false);
155   void SetControlLabel(int controlId, const CVariant& label);
156 
157   BaseSettingControlPtr GetSettingControl(const std::string& setting);
158   BaseSettingControlPtr GetSettingControl(int controlId);
159 
160   CGUIControl* AddSeparator(float width, int& iControlID);
161   CGUIControl* AddGroupLabel(const std::shared_ptr<CSettingGroup>& group,
162                              float width,
163                              int& iControlID);
164 
165   std::vector<std::shared_ptr<CSettingCategory>> m_categories;
166   std::vector<BaseSettingControlPtr> m_settingControls;
167 
168   int m_iSetting;
169   int m_iCategory;
170   std::shared_ptr<CSettingAction> m_resetSetting;
171   std::shared_ptr<CSettingCategory> m_dummyCategory;
172 
173   CGUISpinControlEx* m_pOriginalSpin;
174   CGUISettingsSliderControl* m_pOriginalSlider;
175   CGUIRadioButtonControl* m_pOriginalRadioButton;
176   CGUIButtonControl* m_pOriginalCategoryButton;
177   CGUIButtonControl* m_pOriginalButton;
178   CGUIEditControl* m_pOriginalEdit;
179   CGUIImage* m_pOriginalImage;
180   CGUILabelControl* m_pOriginalGroupTitle;
181   bool m_newOriginalEdit;
182 
183   BaseSettingControlPtr
184       m_delayedSetting; ///< Current delayed setting \sa CBaseSettingControl::SetDelayed()
185   CTimer m_delayedTimer; ///< Delayed setting timer
186 
187   bool m_confirmed;
188   int m_focusedControl, m_fadedControl;
189 };
190