1 /*
2     SPDX-FileCopyrightText: 2012 Fabio D 'Urso <fabiodurso@hotmail.it>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "widgetannottools.h"
8 #include "editannottooldialog.h"
9 
10 #include <KLocalizedString>
11 #include <QIcon>
12 
13 #include <KConfigGroup>
14 #include <QApplication>
15 #include <QDialogButtonBox>
16 #include <QDomDocument>
17 #include <QDomElement>
18 #include <QHBoxLayout>
19 #include <QListWidget>
20 #include <QListWidgetItem>
21 #include <QPushButton>
22 #include <QVBoxLayout>
23 
24 #include "pageviewannotator.h"
25 
26 // Used to store tools' XML description in m_list's items
27 static const int ToolXmlRole = Qt::UserRole;
28 
WidgetAnnotTools(QWidget * parent)29 WidgetAnnotTools::WidgetAnnotTools(QWidget *parent)
30     : WidgetConfigurationToolsBase(parent)
31 {
32 }
33 
~WidgetAnnotTools()34 WidgetAnnotTools::~WidgetAnnotTools()
35 {
36 }
37 
38 /* Before returning the XML strings, this functions updates the id and
39  * shortcut properties.
40  * Note: The shortcut is only assigned to the first nine tools */
tools() const41 QStringList WidgetAnnotTools::tools() const
42 {
43     QStringList res;
44 
45     const int count = m_list->count();
46     for (int i = 0; i < count; ++i) {
47         QListWidgetItem *listEntry = m_list->item(i);
48 
49         // Parse associated DOM data
50         QDomDocument doc;
51         doc.setContent(listEntry->data(ToolXmlRole).value<QString>());
52 
53         // Set id
54         QDomElement toolElement = doc.documentElement();
55         toolElement.setAttribute(QStringLiteral("id"), i + 1);
56 
57         // Remove old shortcut, if any
58         QDomNode oldShortcut = toolElement.elementsByTagName(QStringLiteral("shortcut")).item(0);
59         if (oldShortcut.isElement())
60             toolElement.removeChild(oldShortcut);
61 
62         // Create new shortcut element (only the first 9 tools are assigned a shortcut key)
63         if (i < 9) {
64             QDomElement newShortcut = doc.createElement(QStringLiteral("shortcut"));
65             newShortcut.appendChild(doc.createTextNode(QString::number(i + 1)));
66             toolElement.appendChild(newShortcut);
67         }
68 
69         // Append to output
70         res << doc.toString(-1);
71     }
72 
73     return res;
74 }
75 
setTools(const QStringList & items)76 void WidgetAnnotTools::setTools(const QStringList &items)
77 {
78     m_list->clear();
79 
80     // Parse each string and populate the list widget
81     for (const QString &toolXml : items) {
82         QDomDocument entryParser;
83         if (!entryParser.setContent(toolXml)) {
84             qWarning() << "Skipping malformed tool XML string";
85             break;
86         }
87 
88         QDomElement toolElement = entryParser.documentElement();
89         if (toolElement.tagName() == QLatin1String("tool")) {
90             // Create list item and attach the source XML string as data
91             QString itemText = toolElement.attribute(QStringLiteral("name"));
92             if (itemText.isEmpty())
93                 itemText = PageViewAnnotator::defaultToolName(toolElement);
94             QListWidgetItem *listEntry = new QListWidgetItem(itemText, m_list);
95             listEntry->setData(ToolXmlRole, QVariant::fromValue(toolXml));
96             listEntry->setIcon(PageViewAnnotator::makeToolPixmap(toolElement));
97         }
98     }
99 
100     updateButtons();
101 }
102 
slotEdit()103 void WidgetAnnotTools::slotEdit()
104 {
105     QListWidgetItem *listEntry = m_list->currentItem();
106 
107     QDomDocument doc;
108     doc.setContent(listEntry->data(ToolXmlRole).value<QString>());
109     QDomElement toolElement = doc.documentElement();
110 
111     EditAnnotToolDialog t(this, toolElement);
112 
113     if (t.exec() != QDialog::Accepted)
114         return;
115 
116     doc = t.toolXml();
117     toolElement = doc.documentElement();
118 
119     QString itemText = t.name();
120 
121     // Store name attribute only if the user specified a customized name
122     if (!itemText.isEmpty())
123         toolElement.setAttribute(QStringLiteral("name"), itemText);
124     else
125         itemText = PageViewAnnotator::defaultToolName(toolElement);
126 
127     // Edit list entry and attach XML string as data
128     listEntry->setText(itemText);
129     listEntry->setData(ToolXmlRole, QVariant::fromValue(doc.toString(-1)));
130     listEntry->setIcon(PageViewAnnotator::makeToolPixmap(toolElement));
131 
132     // Select and scroll
133     m_list->setCurrentItem(listEntry);
134     m_list->scrollToItem(listEntry);
135     updateButtons();
136     emit changed();
137 }
138 
slotAdd()139 void WidgetAnnotTools::slotAdd()
140 {
141     EditAnnotToolDialog t(this);
142 
143     if (t.exec() != QDialog::Accepted)
144         return;
145 
146     QDomDocument rootDoc = t.toolXml();
147     QDomElement toolElement = rootDoc.documentElement();
148 
149     QString itemText = t.name();
150 
151     // Store name attribute only if the user specified a customized name
152     if (!itemText.isEmpty())
153         toolElement.setAttribute(QStringLiteral("name"), itemText);
154     else
155         itemText = PageViewAnnotator::defaultToolName(toolElement);
156 
157     // Create list entry and attach XML string as data
158     QListWidgetItem *listEntry = new QListWidgetItem(itemText, m_list);
159     listEntry->setData(ToolXmlRole, QVariant::fromValue(rootDoc.toString(-1)));
160     listEntry->setIcon(PageViewAnnotator::makeToolPixmap(toolElement));
161 
162     // Select and scroll
163     m_list->setCurrentItem(listEntry);
164     m_list->scrollToItem(listEntry);
165     updateButtons();
166     emit changed();
167 }
168