File indexing completed on 2024-05-12 15:59:58

0001 /*
0002  * SPDX-FileCopyrightText: 2019 Boudewijn Rempt <boud@valdyas.org>
0003  * SPDX-FileCopyrightText: 2020 Agata Cacko <cacko.azh@gmail.com>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 #ifndef KISTAGCHOOSERWIDGET_H
0009 #define KISTAGCHOOSERWIDGET_H
0010 
0011 #include <QWidget>
0012 #include "kritaresourcewidgets_export.h"
0013 
0014 #include <KisTag.h>
0015 #include <KisTagModel.h>
0016 
0017 /**
0018  * \brief The KisTagChooserWidget class is responsible for all the logic
0019  *        that the tags combobox has in various resource choosers.
0020  *
0021  * It uses KisTagModel as a model for items in the combobox.
0022  * It is also responsible for the popup for tag removal, renaming and creation
0023  * that appears on the right side of the tag combobox (via KisTagToolButton)
0024  * All the logic for adding and removing tags is done through KisTagModel.
0025  */
0026 class KRITARESOURCEWIDGETS_EXPORT KisTagChooserWidget : public QWidget
0027 {
0028     Q_OBJECT
0029 
0030 public:
0031     explicit KisTagChooserWidget(KisTagModel* model, QString resourceType, QWidget* parent);
0032     ~KisTagChooserWidget() override;
0033 
0034 
0035     /// \brief setCurrentItem sets the tag from the param as the current tag in the combobox
0036     /// \param tag tag url to be set as current in the combobox
0037     /// \return true if successful, false if not successful
0038     void setCurrentItem(const QString &tag);
0039 
0040     /// \brief currentIndex returns the current index in tags combobox
0041     /// \return the index of the current item in tags combobox
0042     int currentIndex() const;
0043 
0044     /// \brief currentlySelectedTag returns the current tag from combobox
0045     /// \see currentIndex
0046     /// \return the tag that is currently selected in the tag combobox
0047     KisTagSP currentlySelectedTag();
0048 
0049     /// \brief selectedTagIsReadOnly checks whether the tag is readonly (generated by Krita)
0050     /// \return true if the tag was generated by Krita, false if it's just a normal tag
0051     bool selectedTagIsReadOnly();
0052 
0053     /// \brief update icon files on loading and theme change
0054     void updateIcons();
0055 
0056 Q_SIGNALS:
0057 
0058     /// \brief sigTagChosen is emitted when the selected tag in the combobox changes due to user interaction or by other means
0059     /// \param tag current tag
0060     void sigTagChosen(const KisTagSP tag);
0061 
0062 public Q_SLOTS:
0063 
0064     /// \brief tagChanged slot for the signal from the combobox that the index changed
0065     /// \param index new index
0066     ///
0067     /// When the index in the combobox changes, for example because of user's interaction,
0068     ///  combobox emits a signal; this method is called when it happens.
0069     void tagChanged(int index);
0070 
0071     /// \brief tagToolCreateNewTag slot for the signal from KisTagToolButton that a new tag needs to be created
0072     /// \param tag tag with the name to be created
0073     /// \return created tag taken from the model, with a valid id
0074     void addTag(const QString &tag);
0075     void addTag(const QString &tag, KoResourceSP resource);
0076     void addTag(KisTagSP tag, KoResourceSP resource);
0077 
0078 private Q_SLOTS:
0079 
0080     /// \brief tagToolRenameCurrentTag slot for the signal from KisTagToolButton that the current tag needs to be renamed
0081     /// \param newName new name for the tag
0082     void tagToolRenameCurrentTag(const QString& tag);
0083 
0084     /// \brief tagToolDeleteCurrentTag slot for the signal from the KisTagToolButton that the current tag needs to be deleted
0085     ///
0086     /// Note that tags are not deleted but just marked inactive in the database.
0087     void tagToolDeleteCurrentTag();
0088 
0089     /// \brief tagToolUndeleteLastTag slot for the signal from the KisTagToolButton that the last deleted tag needs to be undeleted
0090     /// \param tag tag to be undeleted (marked active)
0091     void tagToolUndeleteLastTag(KisTagSP tag);
0092 
0093     /// \brief tagToolContextMenuAboutToShow slot for the signal from the KisTagToolButton that the popup will be shown soon
0094     ///
0095     /// Based on the current tag (if it's readonly or not), the popup looks different, so this function
0096     ///  sets the correct mode on the KisTagToolButton popup.
0097     void tagToolContextMenuAboutToShow();
0098 
0099     /// \brief cacheSelectedTag slot that stores current tag selection.
0100     ///
0101     /// Used to allow restoration of tag even after a model reset. Will store
0102     /// the tag just before model resets.
0103     void cacheSelectedTag();
0104 
0105     /// \brief restoreTagFromCache slot designed to restore a selected tag from previously cached selection.
0106     ///
0107     /// Companion to `cacheSelectedTag`, this method restore the selection after model reset.
0108     void restoreTagFromCache();
0109 
0110     void slotTagModelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> roles);
0111 
0112 private:
0113 
0114 
0115     /// \brief setCurrentIndex sets the current index in the combobox
0116     /// \param index index is the index of the tag in the combobox
0117     void setCurrentIndex(int index);
0118 
0119     enum OverwriteDialogOptions {
0120         Replace,
0121         Undelete,
0122         Cancel
0123     };
0124 
0125     OverwriteDialogOptions overwriteTagDialog(KisTagChooserWidget* parent, bool undelete);
0126 
0127 private:
0128     class Private;
0129     Private* const d;
0130 
0131 };
0132 
0133 #endif // KOTAGCHOOSERWIDGET_H