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

0001 /*
0002  * SPDX-FileCopyrightText: 2018 Boudewijn Rempt <boud@valdyas.org>
0003  * SPDX-FileCopyrightText: 2020 Agata Cacko <cacko.azh@gmail.com>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 #include "TagActions.h"
0008 
0009 #include <QDebug>
0010 #include <QLabel>
0011 #include <QGridLayout>
0012 
0013 #include <KoIcon.h>
0014 #include <klocalizedstring.h>
0015 #include <KoResource.h>
0016 
0017 #include <KisPopupSelfActivatingLineEdit.h>
0018 #include <KisTag.h>
0019 
0020 #include "kis_debug.h"
0021 
0022 // ############ Simple Existing Tag Action ##############
0023 
0024 SimpleExistingTagAction::SimpleExistingTagAction(KoResourceSP resource, KisTagSP tag, QObject* parent)
0025     : QAction(parent)
0026     , m_resource(resource)
0027     , m_tag(tag)
0028 {
0029     if (tag) {
0030         setText(tag->name());
0031     }
0032     connect (this, SIGNAL(triggered()),
0033              this, SLOT(onTriggered()));
0034 }
0035 
0036 SimpleExistingTagAction::~SimpleExistingTagAction()
0037 {
0038 }
0039 
0040 void SimpleExistingTagAction::onTriggered()
0041 {
0042     if (!m_tag) return;
0043     emit triggered(m_tag, m_resource);
0044 }
0045 
0046 
0047 // ############ Line Edit ##############
0048 
0049 LineEditAction::LineEditAction(QObject* parent)
0050     : QWidgetAction(parent)
0051     , m_closeParentOnTrigger(false)
0052 {
0053     QWidget* pWidget = new QWidget (0);
0054     QHBoxLayout* pLayout = new QHBoxLayout();
0055     m_label = new QLabel(0);
0056     m_editBox = new KisPopupSelfActivatingLineEdit(0);
0057     m_editBox->setClearButtonEnabled(true);
0058     m_AddButton = new QPushButton();
0059     m_AddButton->setIcon(koIcon("list-add"));
0060     pLayout->addWidget(m_label);
0061     pLayout->addWidget(m_editBox);
0062     pLayout->addWidget(m_AddButton);
0063     pWidget->setLayout(pLayout);
0064     setDefaultWidget(pWidget);
0065 
0066     connect(m_editBox, &QLineEdit::returnPressed, this, &LineEditAction::slotActionTriggered);
0067     connect(m_AddButton, &QPushButton::clicked, this, &LineEditAction::slotActionTriggered);
0068 
0069 }
0070 
0071 LineEditAction::~LineEditAction()
0072 {
0073 }
0074 
0075 void LineEditAction::setIcon(const QIcon &icon)
0076 {
0077     QPixmap pixmap = QPixmap(icon.pixmap(16,16));
0078     m_label->setPixmap(pixmap);
0079 }
0080 
0081 void LineEditAction::setCloseParentOnTrigger(bool closeParent)
0082 {
0083     m_closeParentOnTrigger = closeParent;
0084 }
0085 
0086 bool LineEditAction::closeParentOnTrigger()
0087 {
0088     return m_closeParentOnTrigger;
0089 }
0090 
0091 
0092 void LineEditAction::slotActionTriggered()
0093 {
0094     setData(userText());
0095     QAction::trigger();
0096     onTriggered();
0097     if (!m_editBox->text().isEmpty()) {
0098         if (m_closeParentOnTrigger) {
0099             this->parentWidget()->close();
0100             m_editBox->clearFocus();
0101             m_editBox->clear();
0102         }
0103     }
0104 }
0105 
0106 void LineEditAction::setPlaceholderText(const QString& clickMessage)
0107 {
0108     m_editBox->setPlaceholderText(clickMessage);
0109 }
0110 
0111 void LineEditAction::setText(const QString& text)
0112 {
0113     m_editBox->setText(text);
0114 }
0115 
0116 void LineEditAction::setVisible(bool showAction)
0117 {
0118     QLayout* currentLayout = defaultWidget()->layout();
0119 
0120     this->QAction::setVisible(showAction);
0121 
0122     for(int i=0;i<currentLayout->count();i++) {
0123         currentLayout->itemAt(i)->widget()->setVisible(showAction);
0124     }
0125     defaultWidget()->setVisible(showAction);
0126 }
0127 
0128 QString LineEditAction::userText()
0129 {
0130     return m_editBox->text();
0131 }
0132 
0133 // ############ New Tag ##############
0134 
0135 UserInputTagAction::UserInputTagAction(QObject* parent)
0136     : LineEditAction(parent)
0137 {
0138     setIcon(koIcon("document-new"));
0139     setPlaceholderText(i18n("New tag"));
0140     setCloseParentOnTrigger(true);
0141 }
0142 
0143 UserInputTagAction::~UserInputTagAction()
0144 {
0145 }
0146 
0147 void UserInputTagAction::onTriggered()
0148 {
0149     emit triggered(userText());
0150 }
0151 
0152 // ############ New Tag Resource ##############
0153 
0154 NewTagResourceAction::NewTagResourceAction(KoResourceSP resource, QObject *parent)
0155     : LineEditAction(parent)
0156 {
0157     setIcon(koIcon("document-new"));
0158     setPlaceholderText(i18n("New tag"));
0159     setCloseParentOnTrigger(true);
0160     m_resource = resource;
0161 }
0162 
0163 NewTagResourceAction::~NewTagResourceAction()
0164 {
0165 }
0166 
0167 void NewTagResourceAction::setResource(KoResourceSP resource)
0168 {
0169     m_resource = resource;
0170 }
0171 
0172 void NewTagResourceAction::onTriggered()
0173 {
0174     emit triggered(userText(), m_resource);
0175 }
0176 
0177 // ############ TAG COMPARER ##############
0178 
0179 
0180 CompareWithOtherTagFunctor::CompareWithOtherTagFunctor(KisTagSP referenceTag)
0181 {
0182     m_referenceTag = referenceTag;
0183 }
0184 
0185 bool CompareWithOtherTagFunctor::operator()(KisTagSP otherTag)
0186 {
0187 //    ENTER_FUNCTION() << "refTag: " << (m_referenceTag.isNull() ? "null" : m_referenceTag->name())
0188 //                     << " other: " << (otherTag.isNull() ? "null" : otherTag->name())
0189 //                     << " result = " << (!otherTag.isNull() && !m_referenceTag.isNull() && otherTag->url() == m_referenceTag->url());
0190     return !otherTag.isNull() && !m_referenceTag.isNull() && otherTag->url() == m_referenceTag->url();
0191 }
0192 
0193 void CompareWithOtherTagFunctor::setReferenceTag(KisTagSP referenceTag) {
0194     m_referenceTag = referenceTag;
0195 }
0196 
0197 KisTagSP CompareWithOtherTagFunctor::referenceTag() {
0198     return m_referenceTag;
0199 }
0200