File indexing completed on 2024-05-19 04:27:46

0001 /*
0002  *    This file is part of the KDE project
0003  *    SPDX-FileCopyrightText: 2002 Patrick Julien <freak@codepimps.org>
0004  *    SPDX-FileCopyrightText: 2007 Jan Hambrecht <jaham@gmx.net>
0005  *    SPDX-FileCopyrightText: 2007 Sven Langkamp <sven.langkamp@gmail.com>
0006  *    SPDX-FileCopyrightText: 2011 Srikanth Tiyyagura <srikanth.tulasiram@gmail.com>
0007  *    SPDX-FileCopyrightText: 2011 José Luis Vergara <pentalis@gmail.com>
0008  *    SPDX-FileCopyrightText: 2013 Sascha Suelzer <s.suelzer@gmail.com>
0009  *    SPDX-FileCopyrightText: 2020 Agata Cacko <cacko.azh@gmail.com>
0010  *
0011  *    SPDX-License-Identifier: LGPL-2.0-or-later
0012  */
0013 
0014 #include "KisTagToolButton.h"
0015 
0016 #include <QToolButton>
0017 #include <QGridLayout>
0018 
0019 #include <klocalizedstring.h>
0020 
0021 #include <KoIcon.h>
0022 
0023 #include <kis_debug.h>
0024 #include <KisTagModel.h>
0025 
0026 #include "KisResourceItemChooserContextMenu.h"
0027 #include "KisMenuStyleDontCloseOnAlt.h"
0028 
0029 class KisTagToolButton::Private
0030 {
0031 public:
0032     QToolButton *tagToolButton {0};
0033 
0034     QAction *undeleteTagAction {0};
0035     QAction *deleteTagAction {0};
0036     UserInputTagAction *renameTagAction {0};
0037     UserInputTagAction *addTagAction {0};
0038 
0039     KisTagSP undeleteCandidate;
0040     KisTagSP currentTag;
0041 
0042 };
0043 
0044 KisTagToolButton::KisTagToolButton(QWidget* parent)
0045     : QWidget(parent)
0046     , d(new Private())
0047 {
0048     QGridLayout* buttonLayout = new QGridLayout(this);
0049     buttonLayout->setMargin(0);
0050     buttonLayout->setSpacing(0);
0051 
0052     d->tagToolButton = new QToolButton(this);
0053     loadIcon();
0054     d->tagToolButton->setText(i18n("Tag"));
0055     d->tagToolButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
0056     d->tagToolButton->setToolTip(i18nc("@info:tooltip", "<qt>Show the tag box options.</qt>"));
0057     d->tagToolButton->setPopupMode(QToolButton::InstantPopup);
0058     d->tagToolButton->setEnabled(true);
0059 
0060     QMenu* popup = new QMenu(this);
0061 
0062     KisMenuStyleDontCloseOnAlt *menuStyle = new KisMenuStyleDontCloseOnAlt(popup->style());
0063     menuStyle->setParent(popup);
0064     popup->setStyle(menuStyle);
0065 
0066     d->addTagAction = new UserInputTagAction(popup);
0067     d->addTagAction->setPlaceholderText(i18n("New tag"));
0068     d->addTagAction->setIcon(koIcon("document-new"));
0069     d->addTagAction->setCloseParentOnTrigger(true);
0070     popup->addAction(d->addTagAction);
0071 
0072     connect(d->addTagAction, SIGNAL(triggered(QString)), this, SIGNAL(newTagRequested(QString)));
0073 
0074     d->renameTagAction = new UserInputTagAction(popup);
0075     d->renameTagAction->setPlaceholderText(i18n("Rename tag"));
0076     d->renameTagAction->setIcon(koIcon("edit-rename"));
0077     d->renameTagAction->setCloseParentOnTrigger(true);
0078     popup->addAction(d->renameTagAction);
0079 
0080     connect(d->renameTagAction, SIGNAL(triggered(QString)), this, SIGNAL(renamingOfCurrentTagRequested(QString)));
0081 
0082     popup->addSeparator();
0083 
0084     d->deleteTagAction = new QAction(popup);
0085     d->deleteTagAction->setText(i18n("Delete this tag"));
0086     d->deleteTagAction->setIcon(koIcon("edit-delete"));
0087     popup->addAction(d->deleteTagAction);
0088 
0089     connect(d->deleteTagAction, SIGNAL(triggered()), this, SIGNAL(deletionOfCurrentTagRequested()));
0090 
0091     popup->addSeparator();
0092 
0093     d->undeleteTagAction = new QAction(popup);
0094     d->undeleteTagAction->setIcon(koIcon("edit-redo"));
0095     d->undeleteTagAction->setVisible(false);
0096     popup->addAction(d->undeleteTagAction);
0097 
0098     connect(d->undeleteTagAction, SIGNAL(triggered()), this, SLOT(onTagUndeleteClicked()));
0099 
0100     connect(popup, SIGNAL(aboutToShow()), this, SIGNAL(popupMenuAboutToShow()));
0101 
0102     d->tagToolButton->setMenu(popup);
0103     buttonLayout->addWidget(d->tagToolButton);
0104 }
0105 
0106 KisTagToolButton::~KisTagToolButton()
0107 {
0108     delete d;
0109 }
0110 
0111 void KisTagToolButton::readOnlyMode(bool activate)
0112 {
0113     activate = !activate;
0114     d->renameTagAction->setVisible(activate);
0115     d->deleteTagAction->setVisible(activate);
0116 }
0117 
0118 void KisTagToolButton::setUndeletionCandidate(const KisTagSP deletedTag)
0119 {
0120     if (deletedTag.isNull() || deletedTag->name().isEmpty()) {
0121         d->undeleteTagAction->setVisible(false);
0122         d->undeleteCandidate.clear();
0123     } else {
0124         d->undeleteCandidate = deletedTag;
0125         d->undeleteTagAction->setText(i18n("Undelete") +" "+ deletedTag->name());
0126         d->undeleteTagAction->setVisible(true);
0127     }
0128 }
0129 
0130 KisTagSP KisTagToolButton::undeletionCandidate() const
0131 {
0132     return d->undeleteCandidate;
0133 }
0134 
0135 void KisTagToolButton::setCurrentTag(const KisTagSP tag)
0136 {
0137     d->currentTag = tag;
0138     d->deleteTagAction->setProperty("currentTag", QVariant::fromValue<KisTagSP>(tag));
0139 }
0140 
0141 void KisTagToolButton::loadIcon()
0142 {
0143     d->tagToolButton->setIcon(koIcon("bookmarks"));
0144 }
0145 
0146 void KisTagToolButton::onTagUndeleteClicked()
0147 {
0148     emit undeletionOfTagRequested(d->undeleteCandidate);
0149 }
0150 
0151