summaryrefslogtreecommitdiff
path: root/lib-apps-common/inc/Ux/SelectView.h
blob: ed0915f9630218faf64202149e2b0f85c8e6cb03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * Copyright 2017 Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://floralicense.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef UX_SELECT_VIEW_H
#define UX_SELECT_VIEW_H

#include "Ui/View.h"
#include "Ux/MultiSelector.h"
#include "Ux/SelectTypes.h"

#include <vector>

namespace Ui
{
	class GenItem;
}

namespace Ux
{
	class SelectItem;

	/**
	 * @brief Base class for a view that support single and multiple selection modes.
	 */
	class EXPORT_API SelectView : public Ui::View
	{
	public:
		/**
		 * @brief Translatable strings table for view elements.
		 */
		struct Strings
		{
			const char *selectAll;      /**< "Select all" text. */
			const char *deselectAll;    /**< "Deselect all" text. */
			const char *buttonDone;     /**< "Done" button text. */
			const char *buttonCancel;   /**< "Cancel" button text. */
			const char *titleDefault;   /**< Title for #SelectNone mode. */
			const char *titleSingle;    /**< Title for #SelectSingle mode */
			const char *titleMulti;     /**< Title for #SelectMulti mode */
			const char *titleWithCount; /**< Title for #SelectMulti mode with selection count.
											 Can contain one integer format specifier. */
			const char *titleWithLimit; /**< Title for #SelectMulti mode with limit.
											 Can contain two integer format specifiers. */
			const char *popupLimit;     /**< Selection limit reached popup text.
											 Can contain one integer format specifiers. */
		};

		/**
		 * @brief Called when item's "checked" state changed in #SelectMulti mode.
		 * @param[in]   item            Changed item
		 * @param[in]   isChecked       Whether item is checked
		 * @param[in]   isMultiChecked  Whether item is being checked via "Select All"
		 * @return Whether item's state should be changed.
		 */
		typedef std::function<bool(SelectItem *item, bool isChecked, bool isMultiChecked)> CheckCallback;

		/**
		 * @brief Called when selection limit is reached.
		 * @return Whether limit popup should be shown.
		 */
		typedef std::function<bool()> LimitCallback;

		/**
		 * @brief Array of selectable items.
		 */
		typedef std::vector<SelectItem *> SelectItems;

		SelectView();
		virtual ~SelectView() override;

		/**
		 * @return View selection mode.
		 */
		SelectMode getSelectMode() const;

		/**
		 * @return Current selection limit.
		 */
		size_t getSelectLimit() const;

		/**
		 * @return Current selected items count.
		 */
		size_t getSelectCount() const;

		/**
		 * @return Selectable items managed by the view.
		 */
		const SelectItems &getSelectItems() const;

		/**
		 * @return Whether all items are selected.
		 */
		bool isMaxSelected() const;

		/**
		 * @brief Set translatable strings for the view.
		 * @remark Should be called before create().
		 * @param[in]   strings    Translatable strings table
		 */
		void setStrings(const Strings &strings);

		/**
		 * @brief Set selection mode.
		 * @param[in]   selectMode  Selection mode
		 */
		void setSelectMode(SelectMode selectMode);

		/**
		 * @brief Set item selection limit.
		 * @param[in]   selectLimit     Maximum selectable items count
		 */
		void setSelectLimit(size_t selectLimit);

		/**
		 * @brief Enable active Done button despite number of selections.
		 * @param[in]   isAllowed   True - Done button should be enabled, otherwise false
		 */
		void setEmptyResultAllowed(bool isAllowed);

		/**
		 * @brief Set selection callback.
		 * @param[in]   callback    Selection callback
		 */
		void setSelectCallback(SelectCallback callback);

		/**
		 * @brief Set cancel callback.
		 * @param[in]   callback    Cancel callback
		 */
		void setCancelCallback(CancelCallback callback);

		/**
		 * @brief Set item check callback.
		 * @param[in]   callback    Check callback
		 */
		void setCheckCallback(CheckCallback callback);

		/**
		 * @brief Set limit callback.
		 * @param[in]   callback    Limit callback
		 */
		void setLimitCallback(LimitCallback callback);

	protected:
		/**
		 * @brief Creates "Done" and "Cancel" buttons in #SelectMulti mode.
		 * @see View::onPageAttached()
		 */
		virtual void onPageAttached(Ui::NavigatorPage *page) override;

		/**
		 * @brief Calls cancel callback if it exists.
		 * @see View::onBackPressed()
		 */
		virtual bool onBackPressed() override;

		/**
		 * @brief Called when title was changed.
		 * @param[in]   title  Title
		 */
		virtual void onTitleChanged(const char *title);

		/**
		 * @brief Called when selection mode was changed.
		 * @param[in]   selectMode  New selection mode
		 */
		virtual void onSelectModeChanged(SelectMode selectMode) { }

		/**
		 * @brief Called when selection limit was changed.
		 * @param[in]   selectLimit New selection limit
		 */
		virtual void onSelectLimitChanged(size_t selectLimit) { }

		/**
		 * @brief Called when selection count was changed.
		 * @param[in]   selectCount New selection count
		 */
		virtual void onSelectCountChanged(size_t selectCount) { }

		/**
		 * @return Done button.
		 */
		virtual Evas_Object *createDoneButton();

		/**
		 * @return Cancel button.
		 */
		virtual Evas_Object *createCancelButton();

		/**
		 * @return MultiSelector control.
		 */
		virtual Ux::MultiSelector *createMultiSelector() = 0;

		/**
		 * @brief Add selectable item to be managed by the view.
		 * @param[in]   item    Item to add
		 */
		void addSelectItem(SelectItem *item);

		/**
		 * @brief Remove selectable item.
		 * @param[in]   item    Item to remove
		 */
		void removeSelectItem(SelectItem *item);

		/**
		 * @return MultiSelector
		 */
		Ui::ControlPtr getMultiSelector();

	private:
		friend class SelectItem;

		enum CountChange
		{
			CountIncrement,
			CountDecrement
		};

		size_t getSelectMax() const;
		bool isLimitReached() const;

		void updatePageTitle();
		void updatePageButtons();
		void updateDoneButtonState();
		void updateMultiSelector();
		void updateMultiSelectorState();

		void updateTotalCount(CountChange change, SelectItem *item);
		void updateTotalSelectCount(CountChange change, SelectItem *item);

		void updateVisibleCount(CountChange change, SelectItem *item);
		void updateVisibleSelectCount(CountChange change, SelectItem *item);

		void createPageButtons();
		void destroyPageButtons();

		void onItemExcluded(SelectItem *item, bool isExcluded);
		void onItemVisibilityChanged(SelectItem *item, bool isVisible);
		void onItemSelected(SelectItem *item);
		bool onItemChecked(SelectItem *item, bool isChecked);
		bool onMultiSelectorChanged(MultiSelector::State state);

		void onDonePressed(Evas_Object *button, void *eventInfo);
		void onCancelPressed(Evas_Object *button, void *eventInfo);
		void onLimitReached();

		Ui::ControlPtr m_MultiSelector;
		SelectItems m_Items;

		Evas_Object *m_DoneButton;
		Evas_Object *m_CancelButton;

		bool m_IsMultiChecking;
		size_t m_TotalCount;
		size_t m_TotalSelectCount;
		size_t m_VisibleCount;
		size_t m_VisibleSelectCount;
		size_t m_SelectLimit;
		bool m_IsEmptyResultAllowed;

		SelectMode     m_SelectMode;
		SelectCallback m_OnSelected;
		CheckCallback  m_OnChecked;
		CancelCallback m_OnCanceled;
		LimitCallback  m_OnLimitReached;

		Strings m_Strings;
	};
}

#endif /* UX_SELECT_VIEW_H */