File indexing completed on 2024-04-28 15:39:43

0001 // SPDX-FileCopyrightText: 2016 Tobias Leupold <tl at stonemx dot de>
0002 // SPDX-FileCopyrightText: 2021 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
0003 //
0004 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 
0006 // Local includes
0007 #include "AreaTagSelectDialog.h"
0008 
0009 #include "CompletableLineEdit.h"
0010 #include "Dialog.h"
0011 #include "ListSelect.h"
0012 #include "ResizableFrame.h"
0013 
0014 // KDE includes
0015 #include <KLocalizedString>
0016 #include <kcompletion_version.h>
0017 
0018 // Qt includes
0019 #include <QGridLayout>
0020 #include <QLabel>
0021 #include <QLineEdit>
0022 #include <QMenu>
0023 #include <QPainter>
0024 
0025 AnnotationDialog::AreaTagSelectDialog::AreaTagSelectDialog(AnnotationDialog::ResizableFrame *area,
0026                                                            ListSelect *ls,
0027                                                            QPixmap areaImage,
0028                                                            Dialog *dialog)
0029     : QDialog(area)
0030     , m_area(area)
0031     , m_listSelect(ls)
0032     , m_dialog(dialog)
0033     , m_usedTags(dialog->positionedTags(ls->category()))
0034 {
0035     setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
0036     setAttribute(Qt::WA_TranslucentBackground);
0037     setModal(true);
0038 
0039     QGridLayout *mainLayout = new QGridLayout(this);
0040 
0041     m_areaImageLabel = new QLabel();
0042     m_areaImageLabel->setAlignment(Qt::AlignTop);
0043     m_areaImageLabel->setPixmap(areaImage);
0044     // span 2 rows:
0045     mainLayout->addWidget(m_areaImageLabel, 0, 0, 2, 1);
0046 
0047     CompletableLineEdit *tagSelect = new CompletableLineEdit(ls, this);
0048     ls->connectLineEdit(tagSelect);
0049     tagSelect->setAlignment(Qt::AlignTop);
0050     mainLayout->addWidget(tagSelect, 0, 1);
0051 
0052     m_messageLabel = new QLabel();
0053     m_messageLabel->setWordWrap(true);
0054     mainLayout->addWidget(m_messageLabel, 1, 1);
0055     m_messageLabel->hide();
0056 
0057     QMenu *tagMenu = new QMenu();
0058     m_area->addTagActions(tagMenu);
0059     mainLayout->addWidget(tagMenu, 2, 0, 1, 2);
0060     connect(tagMenu, &QMenu::triggered, this, &QDialog::accept);
0061 
0062 #if KCOMPLETION_VERSION >= QT_VERSION_CHECK(5, 81, 0)
0063     connect(tagSelect, &KLineEdit::returnKeyPressed, this, &AreaTagSelectDialog::slotSetTag);
0064 #else
0065     connect(tagSelect, &KLineEdit::returnPressed, this, &AreaTagSelectDialog::slotSetTag);
0066 #endif
0067     connect(tagSelect, &QLineEdit::textChanged, this, &AreaTagSelectDialog::slotValidateTag);
0068     connect(this, &QDialog::finished, this, &AreaTagSelectDialog::slotFinished);
0069 }
0070 
0071 void AnnotationDialog::AreaTagSelectDialog::slotSetTag(const QString &tag)
0072 {
0073     QString enteredText = tag.trimmed();
0074     if (m_dialog->positionableTagAvailable(m_listSelect->category(), enteredText)) {
0075         const auto currentTagData = m_area->tagData();
0076         // was there already a tag associated?
0077         if (!currentTagData.first.isEmpty()) {
0078             // Deselect the tag
0079             m_dialog->listSelectForCategory(currentTagData.first)->deselectTag(currentTagData.second);
0080             m_area->removeTagData();
0081         }
0082         m_area->setTagData(m_listSelect->category(), enteredText);
0083         this->accept();
0084     }
0085 }
0086 
0087 void AnnotationDialog::AreaTagSelectDialog::slotValidateTag(const QString &tag)
0088 {
0089     if (m_usedTags.contains(tag.trimmed())) {
0090         m_messageLabel->show();
0091         m_messageLabel->setText(i18n("Tag is already used for another area"));
0092         adjustSize();
0093     } else {
0094         m_messageLabel->clear();
0095         adjustSize();
0096     }
0097 }
0098 
0099 void AnnotationDialog::AreaTagSelectDialog::slotFinished()
0100 {
0101     // remove filter from listSelect
0102     m_listSelect->showOnlyItemsMatching(QString());
0103 }
0104 
0105 void AnnotationDialog::AreaTagSelectDialog::paintEvent(QPaintEvent *)
0106 {
0107     QColor backgroundColor = palette().base().color();
0108     backgroundColor.setAlpha(160);
0109     QPainter painter(this);
0110     painter.fillRect(rect(), backgroundColor);
0111 }
0112 
0113 void AnnotationDialog::AreaTagSelectDialog::moveToArea(QPoint areaTopLeft)
0114 {
0115     move(areaTopLeft - (m_areaImageLabel->mapToGlobal(QPoint(0, 0)) - mapToGlobal(QPoint(0, 0))));
0116 }
0117 
0118 // vi:expandtab:tabstop=4 shiftwidth=4:
0119 
0120 #include "moc_AreaTagSelectDialog.cpp"