1 /***************************************************************************
2  * SPDX-FileCopyrightText: 2021 S. MANKOWSKI stephane@mankowski.fr
3  * SPDX-FileCopyrightText: 2021 G. DE BURE support@mankowski.fr
4  * SPDX-License-Identifier: GPL-3.0-or-later
5  ***************************************************************************/
6 #ifndef SKGSHOW_H
7 #define SKGSHOW_H
8 /** @file
9  * A widget to select what to show.
10  *
11  * @author Stephane MANKOWSKI / Guillaume DE BURE
12  */
13 
14 #include <qmap.h>
15 #include <qtimer.h>
16 #include <qtoolbutton.h>
17 
18 #include "skgbasegui_export.h"
19 
20 class QMenu;
21 class QAction;
22 class QActionGroup;
23 
24 /**
25  * This file is a widget to select what to show
26  */
27 class SKGBASEGUI_EXPORT SKGShow : public QToolButton
28 {
29     Q_OBJECT
30 
31     /**
32      * Mode
33      */
34     Q_PROPERTY(OperatorMode mode READ getMode WRITE setMode NOTIFY modified)
35 
36     /**
37      * Display Title
38      */
39     Q_PROPERTY(bool displayTitle READ getDisplayTitle WRITE setDisplayTitle NOTIFY modified)
40 public:
41     /**
42      * This enumerate defines type of operator
43      */
44     enum OperatorMode {AND,   /**< AND*/
45                        OR      /**< OR*/
46                       };
47 
48     /**
49      * This enumerate defines type of operator
50      */
51     Q_ENUM(OperatorMode)
52 
53     /**
54      * Default Constructor
55      * @param iParent the parent
56      */
57     explicit SKGShow(QWidget* iParent);
58 
59     /**
60      * Default Destructor
61      */
62     ~SKGShow() override;
63 
64     /**
65      * Get the current state
66      * @return a string containing all activated item identifiers (separated by ;)
67      */
68     virtual QString getState();
69 
70     /**
71      * Set the current state
72      * @param iState a string containing all activated item identifiers (separated by ;)
73      */
74     virtual void setState(const QString& iState);
75 
76     /**
77      * Get the current mode
78      * @return the mode
79      */
80     virtual OperatorMode getMode();
81 
82     /**
83      * Set the current mode
84      * @param iMode the mode
85      */
86     virtual void setMode(OperatorMode iMode);
87 
88     /**
89      * Set the default state
90      * @param iState a string containing all activated item identifiers (separated by ;)
91      */
92     virtual void setDefaultState(const QString& iState);
93 
94     /**
95      * Get the current display mode
96      * @return true of false
97      */
98     virtual bool getDisplayTitle();
99 
100     /**
101      * Set the current display mode
102      * @param iDisplay true if you want to see the selected filter of false
103      */
104     virtual void setDisplayTitle(bool iDisplay);
105 
106     /**
107      * Get the current where clause
108      * @return a where clause string
109      */
110     virtual QString getWhereClause() const;
111 
112     /**
113      * Remove all items
114      */
115     virtual void clear();
116 
117     /**
118      * @brief Get the number of items
119      *
120      * @return the number of items
121      **/
122     virtual int count();
123 
124     /**
125      * @brief Get the action for an identifier
126      *
127      * @param iIdentifier unique identifier of the item
128      * @return the action
129      **/
130     virtual QAction* getAction(const QString& iIdentifier) const;
131 
132     /**
133      * @brief Add an item to the menu
134      *
135      * @param iIdentifier unique identifier of the item
136      * @param iText text
137      * @param iIcon icon Defaults to "".
138      * @param iWhereClose icon Defaults to "".
139      * @param iListIdToCheckWhenChecked list of item identifiers (separated by ;) to check when checked Defaults to "".
140      * @param iListIdToUncheckWhenChecked list of item identifiers (separated by ;) to uncheck when unchecked Defaults to "".
141      * @param iListIdToCheckWhenUnchecked list of item identifiers (separated by ;) to check when checked Defaults to "".
142      * @param iListIdToUncheckWhenUnchecked list of item identifiers (separated by ;) to uncheck when unchecked Defaults to "".
143      * @param iShortcut the associated shortcut.
144      * @return the index of the new item
145      **/
146     virtual int addItem(const QString& iIdentifier, const QString& iText, const QString& iIcon = QString(),
147                         const QString& iWhereClose = QString(),
148                         const QString& iListIdToCheckWhenChecked = QString(),
149                         const QString& iListIdToUncheckWhenChecked = QString(),
150                         const QString& iListIdToCheckWhenUnchecked = QString(),
151                         const QString& iListIdToUncheckWhenUnchecked = QString(),
152                         const QKeySequence& iShortcut = QKeySequence());
153 
154     /**
155      * @brief Add a period item to the menu
156      *
157      * @param iIdentifier unique identifier of the item
158      * @return the index of the new item
159      **/
160     virtual int addPeriodItem(const QString& iIdentifier);
161 
162     /**
163      * @brief Add an item to the menu
164      *
165      * @param iIdentifier unique identifier of the item
166      * @param iText text
167      * @param iIcon icon Defaults to "".
168      * @param iWhereClose icon Defaults to "".
169      * @param iGroup the group of actions.
170      * @param iShortcut the associated shortcut.
171      * @return the index of the new item
172      **/
173     virtual int addGroupedItem(const QString& iIdentifier,
174                                const QString& iText,
175                                const QString& iIcon = QString(),
176                                const QString& iWhereClose = QString(),
177                                const QString& iGroup = QString(),
178                                const QKeySequence& iShortcut = QKeySequence());
179 
180     /**
181      * @brief Set the list of items to check when iIndex is checked
182      *
183      * @param iIndex index of the item (@see addItem)
184      * @param iIds list of item identifiers (separated by ;)
185      **/
186     virtual void setListIdToCheckWhenChecked(int iIndex, const QString& iIds);
187 
188     /**
189      * @brief Set the list of items to uncheck when iIndex is checked
190      *
191      * @param iIndex index of the item (@see addItem)
192      * @param iIds list of item identifiers (separated by ;)
193      **/
194     virtual void setListIdToUncheckWhenChecked(int iIndex, const QString& iIds);
195 
196     /**
197      * @brief Set the list of items to check when iIndex is unchecked
198      *
199      * @param iIndex index of the item (@see addItem)
200      * @param iIds list of item identifiers (separated by ;)
201      **/
202     virtual void setListIdToCheckWhenUnchecked(int iIndex, const QString& iIds);
203 
204     /**
205      * @brief Set the list of items to uncheck when iIndex is unchecked
206      *
207      * @param iIndex index of the item (@see addItem)
208      * @param iIds list of item identifiers (separated by ;)
209      **/
210     virtual void setListIdToUncheckWhenUnchecked(int iIndex, const QString& iIds);
211 
212     /**
213      * @brief Add a separator
214      *
215      * @return void
216      **/
217     virtual void addSeparator();
218 
219 Q_SIGNALS:
220     /**
221      * @brief Emitted when an item is changed
222      *
223      * @return void
224      **/
225     void stateChanged();
226 
227     /**
228      * This signal is launched when the error is modified
229      */
230     void modified();
231 
232 private Q_SLOTS:
233     /**
234      * @brief trigger
235      **/
236     void trigger();
237 
238     /**
239      * @brief trigger
240      **/
241     void triggerRefreshOnly();
242 
243 private:
244     Q_DISABLE_COPY(SKGShow)
245 
246     QString getTitle() const;
247     void refreshTitle();
248 
249     QMenu* m_menu;
250     QTimer m_timer;
251     QString m_defaultState;
252     OperatorMode m_mode;
253     bool m_inTrigger;
254     bool m_displayTitle;
255 
256     QList<QAction*> m_actions;
257     QStringList m_icons;
258     QMap<QAction*, QString> m_check_to_check;
259     QMap<QAction*, QString> m_uncheck_to_check;
260     QMap<QAction*, QString> m_check_to_uncheck;
261     QMap<QAction*, QString> m_uncheck_to_uncheck;
262     QMap<QAction*, QString> m_whereclause;
263     QMap<QString, QActionGroup*> m_groups;
264 };
265 
266 #endif  // SKGSHOW_H
267