File indexing completed on 2024-05-12 05:07:59
0001 /* 0002 SPDX-FileCopyrightText: 2006-2018 Thomas Baumgart <tbaumgart@kde.org> 0003 SPDX-FileCopyrightText: 2017 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com> 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #ifndef KMYMONEYSELECTOR_H 0008 #define KMYMONEYSELECTOR_H 0009 0010 // ---------------------------------------------------------------------------- 0011 // QT Includes 0012 0013 #include <QWidget> 0014 #include <QTreeWidget> 0015 0016 // ---------------------------------------------------------------------------- 0017 // KDE Includes 0018 0019 // ---------------------------------------------------------------------------- 0020 // Project Includes 0021 0022 #include "kmm_base_widgets_export.h" 0023 0024 /** 0025 * This class implements a general selector for id based objects. It is based 0026 * on a tree view. Using this widget, one can select one or multiple 0027 * items depending on the mode of operation and the set of items 0028 * selected to be displayed. (see setSelectionMode() ). 0029 * 0030 * - Single selection mode\n 0031 * In this mode the widget allows to select a single entry out of 0032 * the set of displayed items. 0033 * 0034 * - Multi selection mode\n 0035 * In this mode, the widget allows to select one or more entries 0036 * out of the set of displayed items. Selection is performed 0037 * by marking the item in the view. 0038 */ 0039 class KMyMoneySelectorPrivate; 0040 class KMM_BASE_WIDGETS_EXPORT KMyMoneySelector : public QWidget 0041 { 0042 Q_OBJECT 0043 Q_DISABLE_COPY(KMyMoneySelector) 0044 0045 Q_PROPERTY(QStringList selectedItems READ selectedItems DESIGNABLE false STORED false) 0046 public: 0047 explicit KMyMoneySelector(QWidget* parent = nullptr, Qt::WindowFlags flags = {}); 0048 virtual ~KMyMoneySelector(); 0049 0050 /** 0051 * This method sets the mode of operation of this widget. 0052 * Supported values are @p QListView::Single and @p QListView::Multi. 0053 * 0054 * @param mode @p QListView::Single selects single selection mode and 0055 * @p QListView::Multi selects multi selection mode 0056 * 0057 * @note When the widget is created, it defaults to QListView::Single. 0058 * Any items loaded into the widget will be cleared if the mode changes. 0059 * Changing the selection mode also changes the type of the items 0060 * created through newItem(). You should therefore set the selection mode 0061 * before you create items. 0062 */ 0063 void setSelectionMode(const QTreeWidget::SelectionMode mode); 0064 0065 /** 0066 * returns the selection mode of the widget. 0067 * 0068 * @sa setSelectionMode() 0069 */ 0070 QTreeWidget::SelectionMode selectionMode() const; 0071 0072 /** 0073 * This method returns the list of selected item ids. If 0074 * no item is selected, the list is empty. The list is cleared 0075 * when the method is called. 0076 * 0077 * @param list reference to id list 0078 */ 0079 void selectedItems(QStringList& list) const; 0080 0081 /** 0082 * Convenience method for above method. Requires more resources. 0083 * Provided only for backward compatibility. 0084 * 0085 * @todo Deprecated after 1.0 0086 */ 0087 QStringList selectedItems() const; 0088 0089 /** 0090 * This method returns the list of all item ids. 0091 * The list is cleared when the method is called. 0092 * 0093 * @param list reference to id list 0094 */ 0095 void itemList(QStringList& list) const; 0096 0097 /** 0098 * Convenience method for above method. Requires more resources. 0099 * Provided only for backward compatibility. 0100 * 0101 * @todo Deprecated after 1.0 0102 */ 0103 QStringList itemList() const; 0104 0105 /** 0106 * This method returns an information if all items 0107 * currently shown are selected or not. 0108 * 0109 * @retval true All items shown are selected 0110 * @retval false Not all items are selected 0111 * 0112 * @note If the selection mode is set to Single, this 0113 * method always returns false. 0114 */ 0115 bool allItemsSelected() const; 0116 0117 /** 0118 * This method sets the current selected item and marks the 0119 * checkbox according to @p state in multi-selection-mode. 0120 * 0121 * @param id id of account 0122 * @param state state of checkbox in multi-selection-mode 0123 * @p true checked 0124 * @p false not checked (default) 0125 */ 0126 void setSelected(const QString& id, const bool state = false); 0127 0128 /** 0129 * Return a pointer to the QTreeWidget object 0130 */ 0131 QTreeWidget* listView() const; 0132 0133 /** 0134 * This method selects/deselects all items that 0135 * are currently in the item list according 0136 * to the parameter @p state. 0137 * 0138 * @param state select items if @p true, deselect otherwise 0139 */ 0140 void selectAllItems(const bool state); 0141 0142 /** 0143 * This method selects/deselects all items that 0144 * are currently in this object's item list AND are present in the supplied 0145 * @p itemList of items to select, according to the @p state. 0146 * 0147 * @param itemList of item ids to apply @p state to 0148 * @param state select items if @p true, deselect otherwise 0149 */ 0150 void selectItems(const QStringList& itemList, const bool state); 0151 0152 /** 0153 * Protect an entry from selection. Protection is controlled by 0154 * the parameter @p protect. 0155 * 0156 * @param itemId id of item for which to modify the protection 0157 * @param protect if true, the entry specified by @p accId cannot be 0158 * selected. If false, it can be selected. Defaults to @p true. 0159 */ 0160 void protectItem(const QString& itemId, const bool protect = true); 0161 0162 /** 0163 * This method removes an item with a given id from the list. 0164 * 0165 * @param id QString containing id of item to be removed 0166 */ 0167 void removeItem(const QString& id); 0168 0169 /** 0170 * This method creates a new top level KMyMoneyTreeWidgetItem object in the list view. 0171 * The type can be influenced with the @a type argument. It defaults 0172 * to QCheckListItem::RadioButtonController. If @a id is empty, the item is not 0173 * selectable. It will be shown 'opened' (see QListViewItem::setOpen()) 0174 * 0175 * @return pointer to newly created object 0176 */ 0177 QTreeWidgetItem* newItem(const QString& name, const QString& key, const QString& id); 0178 QTreeWidgetItem* newItem(const QString& name, const QString& key); 0179 QTreeWidgetItem* newItem(const QString& name); 0180 0181 /** 0182 * Same as above, but create the item following the item pointed to by @c after. 0183 * If @c after is 0, then behave as previous method 0184 */ 0185 QTreeWidgetItem* newItem(const QString& name, QTreeWidgetItem* after, const QString& key, const QString& id); 0186 QTreeWidgetItem* newItem(const QString& name, QTreeWidgetItem* after, const QString& key); 0187 QTreeWidgetItem* newItem(const QString& name, QTreeWidgetItem* after); 0188 0189 /** 0190 * This method creates a new selectable object depending on the 0191 * selection mode. This is either a QTreeWidgetItem for single 0192 * selection mode or a KMyMoneyTreeWidgetItem for multi selection mode 0193 * 0194 * @note The new item will be the first one in the selection 0195 * 0196 * @param parent pointer to parent item 0197 * @param name the displayed name 0198 * @param key string to be used for completion. If empty defaults to @a name 0199 * @param id the id used to identify the objects 0200 * 0201 * @return pointer to newly created object 0202 */ 0203 QTreeWidgetItem* newItem(QTreeWidgetItem* parent, const QString& name, const QString& key, const QString& id); 0204 0205 /** 0206 * This method creates a new selectable object depending on the 0207 * selection mode. This is either a QTreeWidgetItem for single 0208 * selection mode or a KMyMoneyTreeWidgetItem for multi selection mode. 0209 * In contrast to the above method, the parent is always the view. 0210 * 0211 * @note The new item will be the first one in the selection 0212 * 0213 * @param name the displayed name 0214 * @param key string to be used for completion. If empty defaults to @a name 0215 * @param id the id used to identify the objects 0216 * 0217 * @return pointer to newly created object 0218 */ 0219 QTreeWidgetItem* newTopItem(const QString& name, const QString& key, const QString& id); 0220 0221 /** 0222 * This method checks if a given @a item matches the given regular expression @a exp. 0223 * 0224 * @param exp const reference to a regular expression object 0225 * @param item pointer to QListViewItem 0226 * 0227 * @retval true item matches 0228 * @retval false item does not match 0229 */ 0230 virtual bool match(const QRegularExpression& exp, QTreeWidgetItem* item) const; 0231 0232 /** 0233 * This method returns a pointer to the QListViewItem with the id @a id. 0234 * If such an item is not contained in the list, @a 0 will be returned. 0235 * 0236 * @param id id to be used to find a QListViewItem pointer for 0237 */ 0238 QTreeWidgetItem* item(const QString& id) const; 0239 0240 /** 0241 * This method returns, if any of the items in the selector contains 0242 * the text @a txt. 0243 * 0244 * @param txt const reference to string to be looked for 0245 * @retval true exact match found 0246 * @retval false no match found 0247 */ 0248 virtual bool contains(const QString& txt) const; 0249 0250 /** 0251 * Clears all items of the selector and the associated listview. 0252 */ 0253 virtual void clear(); 0254 0255 /** 0256 * Sets the give item's selectable state. 0257 */ 0258 void setSelectable(QTreeWidgetItem *item, bool selectable); 0259 0260 public Q_SLOTS: 0261 /** 0262 * This slot selects all items that are currently in 0263 * the item list of the widget. 0264 */ 0265 void slotSelectAllItems(); 0266 0267 /** 0268 * This slot deselects all items that are currently in 0269 * the item list of the widget. 0270 */ 0271 void slotDeselectAllItems(); 0272 0273 Q_SIGNALS: 0274 void stateChanged(); 0275 0276 void itemSelected(const QString& id); 0277 0278 protected: 0279 /** 0280 * Helper method for selectedItems() to traverse the tree. 0281 * 0282 * @param list list of selected ids 0283 * @param item pointer to item to start with 0284 */ 0285 void selectedItems(QStringList& list, QTreeWidgetItem* item) const; 0286 0287 /** 0288 * Helper method for allItemsSelected() to traverse the tree. 0289 * 0290 * @param item pointer to item to start with 0291 */ 0292 bool allItemsSelected(const QTreeWidgetItem *item) const; 0293 0294 /** 0295 * This is a helper method for selectAllItems(). 0296 * 0297 * @param item pointer to item to start with 0298 * @param state selection state (@a true = selected, @a false = not selected) 0299 */ 0300 void selectAllSubItems(QTreeWidgetItem* item, const bool state); 0301 0302 /** 0303 * This is a helper method for selectItems(). 0304 * 0305 * @param item pointer to item to start with 0306 * @param itemList list of ids to be selected 0307 * @param state selection state (@a true = selected, @a false = not selected) 0308 */ 0309 void selectSubItems(QTreeWidgetItem* item, const QStringList& itemList, const bool state); 0310 0311 public Q_SLOTS: 0312 /** 0313 * Hide all listview items that do not match the regular expression @a exp. 0314 * This method returns the number of visible items 0315 * 0316 * @param exp const reference to QRegularExpression that an item must match to stay visible 0317 * 0318 * @return number of visible items 0319 */ 0320 int slotMakeCompletion(const QRegularExpression& exp); 0321 0322 /** 0323 * This is an overloaded member function, provided for convenience. It behaves essentially like the above function. 0324 * 0325 * @param txt contains the pattern for a QRegularExpression 0326 */ 0327 int slotMakeCompletion(const QString& txt); 0328 0329 0330 protected Q_SLOTS: 0331 /** 0332 * This slot is connected to the QTreeWidget executed signal 0333 */ 0334 void slotItemSelected(QTreeWidgetItem *it_v); 0335 0336 /** 0337 * This slot processes the right mouse button press on a list view item. 0338 * 0339 * @param item pointer to the item where the mouse was pressed 0340 * @param col the column of the tree (unused here) 0341 */ 0342 void slotItemPressed(QTreeWidgetItem* item, int col); 0343 0344 protected: 0345 KMyMoneySelectorPrivate * const d_ptr; 0346 KMyMoneySelector(KMyMoneySelectorPrivate &dd, QWidget* parent = nullptr, Qt::WindowFlags flags = {}); 0347 0348 private: 0349 Q_DECLARE_PRIVATE(KMyMoneySelector) 0350 }; 0351 0352 #endif