File indexing completed on 2024-04-28 15:52:01

0001 /*
0002     SPDX-FileCopyrightText: 2012 Fabio D 'Urso <fabiodurso@hotmail.it>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "widgetannottools.h"
0008 #include "editannottooldialog.h"
0009 
0010 #include <KLocalizedString>
0011 #include <QIcon>
0012 
0013 #include <KConfigGroup>
0014 #include <QApplication>
0015 #include <QDialogButtonBox>
0016 #include <QDomDocument>
0017 #include <QDomElement>
0018 #include <QHBoxLayout>
0019 #include <QListWidget>
0020 #include <QListWidgetItem>
0021 #include <QPushButton>
0022 #include <QVBoxLayout>
0023 
0024 #include "pageviewannotator.h"
0025 
0026 // Used to store tools' XML description in m_list's items
0027 static const int ToolXmlRole = Qt::UserRole;
0028 
0029 WidgetAnnotTools::WidgetAnnotTools(QWidget *parent)
0030     : WidgetConfigurationToolsBase(parent)
0031 {
0032 }
0033 
0034 WidgetAnnotTools::~WidgetAnnotTools()
0035 {
0036 }
0037 
0038 /* Before returning the XML strings, this functions updates the id and
0039  * shortcut properties.
0040  * Note: The shortcut is only assigned to the first nine tools */
0041 QStringList WidgetAnnotTools::tools() const
0042 {
0043     QStringList res;
0044 
0045     const int count = m_list->count();
0046     for (int i = 0; i < count; ++i) {
0047         QListWidgetItem *listEntry = m_list->item(i);
0048 
0049         // Parse associated DOM data
0050         QDomDocument doc;
0051         doc.setContent(listEntry->data(ToolXmlRole).value<QString>());
0052 
0053         // Set id
0054         QDomElement toolElement = doc.documentElement();
0055         toolElement.setAttribute(QStringLiteral("id"), i + 1);
0056 
0057         // Remove old shortcut, if any
0058         QDomNode oldShortcut = toolElement.elementsByTagName(QStringLiteral("shortcut")).item(0);
0059         if (oldShortcut.isElement()) {
0060             toolElement.removeChild(oldShortcut);
0061         }
0062 
0063         // Create new shortcut element (only the first 9 tools are assigned a shortcut key)
0064         if (i < 9) {
0065             QDomElement newShortcut = doc.createElement(QStringLiteral("shortcut"));
0066             newShortcut.appendChild(doc.createTextNode(QString::number(i + 1)));
0067             toolElement.appendChild(newShortcut);
0068         }
0069 
0070         // Append to output
0071         res << doc.toString(-1);
0072     }
0073 
0074     return res;
0075 }
0076 
0077 void WidgetAnnotTools::setTools(const QStringList &items)
0078 {
0079     m_list->clear();
0080 
0081     // Parse each string and populate the list widget
0082     for (const QString &toolXml : items) {
0083         QDomDocument entryParser;
0084         if (!entryParser.setContent(toolXml)) {
0085             qWarning() << "Skipping malformed tool XML string";
0086             break;
0087         }
0088 
0089         QDomElement toolElement = entryParser.documentElement();
0090         if (toolElement.tagName() == QLatin1String("tool")) {
0091             // Create list item and attach the source XML string as data
0092             QString itemText = toolElement.attribute(QStringLiteral("name"));
0093             if (itemText.isEmpty()) {
0094                 itemText = PageViewAnnotator::defaultToolName(toolElement);
0095             }
0096             QListWidgetItem *listEntry = new QListWidgetItem(itemText, m_list);
0097             listEntry->setData(ToolXmlRole, QVariant::fromValue(toolXml));
0098             listEntry->setIcon(PageViewAnnotator::makeToolPixmap(toolElement));
0099         }
0100     }
0101 
0102     updateButtons();
0103 }
0104 
0105 void WidgetAnnotTools::slotEdit()
0106 {
0107     QListWidgetItem *listEntry = m_list->currentItem();
0108 
0109     QDomDocument doc;
0110     doc.setContent(listEntry->data(ToolXmlRole).value<QString>());
0111     QDomElement toolElement = doc.documentElement();
0112 
0113     EditAnnotToolDialog t(this, toolElement);
0114 
0115     if (t.exec() != QDialog::Accepted) {
0116         return;
0117     }
0118 
0119     doc = t.toolXml();
0120     toolElement = doc.documentElement();
0121 
0122     QString itemText = t.name();
0123 
0124     // Store name attribute only if the user specified a customized name
0125     if (!itemText.isEmpty()) {
0126         toolElement.setAttribute(QStringLiteral("name"), itemText);
0127     } else {
0128         itemText = PageViewAnnotator::defaultToolName(toolElement);
0129     }
0130 
0131     // Edit list entry and attach XML string as data
0132     listEntry->setText(itemText);
0133     listEntry->setData(ToolXmlRole, QVariant::fromValue(doc.toString(-1)));
0134     listEntry->setIcon(PageViewAnnotator::makeToolPixmap(toolElement));
0135 
0136     // Select and scroll
0137     m_list->setCurrentItem(listEntry);
0138     m_list->scrollToItem(listEntry);
0139     updateButtons();
0140     Q_EMIT changed();
0141 }
0142 
0143 void WidgetAnnotTools::slotAdd()
0144 {
0145     EditAnnotToolDialog t(this);
0146 
0147     if (t.exec() != QDialog::Accepted) {
0148         return;
0149     }
0150 
0151     QDomDocument rootDoc = t.toolXml();
0152     QDomElement toolElement = rootDoc.documentElement();
0153 
0154     QString itemText = t.name();
0155 
0156     // Store name attribute only if the user specified a customized name
0157     if (!itemText.isEmpty()) {
0158         toolElement.setAttribute(QStringLiteral("name"), itemText);
0159     } else {
0160         itemText = PageViewAnnotator::defaultToolName(toolElement);
0161     }
0162 
0163     // Create list entry and attach XML string as data
0164     QListWidgetItem *listEntry = new QListWidgetItem(itemText, m_list);
0165     listEntry->setData(ToolXmlRole, QVariant::fromValue(rootDoc.toString(-1)));
0166     listEntry->setIcon(PageViewAnnotator::makeToolPixmap(toolElement));
0167 
0168     // Select and scroll
0169     m_list->setCurrentItem(listEntry);
0170     m_list->scrollToItem(listEntry);
0171     updateButtons();
0172     Q_EMIT changed();
0173 }