okular/part/widgetannottools.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

174 lines
5 KiB
C++
Raw Normal View History

2021-05-24 07:25:56 +00:00
/*
SPDX-FileCopyrightText: 2012 Fabio D 'Urso <fabiodurso@hotmail.it>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "widgetannottools.h"
#include "editannottooldialog.h"
2020-07-08 11:54:37 +00:00
#include <KLocalizedString>
2014-10-06 06:31:17 +00:00
#include <QIcon>
2014-10-17 21:30:54 +00:00
#include <KConfigGroup>
2018-09-01 08:25:57 +00:00
#include <QApplication>
2014-10-17 21:30:54 +00:00
#include <QDialogButtonBox>
2018-08-31 09:23:45 +00:00
#include <QDomDocument>
#include <QDomElement>
#include <QHBoxLayout>
#include <QListWidget>
#include <QListWidgetItem>
2014-10-17 21:30:54 +00:00
#include <QPushButton>
#include <QVBoxLayout>
#include "pageviewannotator.h"
// Used to store tools' XML description in m_list's items
static const int ToolXmlRole = Qt::UserRole;
WidgetAnnotTools::WidgetAnnotTools(QWidget *parent)
: WidgetConfigurationToolsBase(parent)
{
}
WidgetAnnotTools::~WidgetAnnotTools()
{
}
/* Before returning the XML strings, this functions updates the id and
* shortcut properties.
* Note: The shortcut is only assigned to the first nine tools */
QStringList WidgetAnnotTools::tools() const
{
QStringList res;
const int count = m_list->count();
for (int i = 0; i < count; ++i) {
QListWidgetItem *listEntry = m_list->item(i);
// Parse associated DOM data
QDomDocument doc;
doc.setContent(listEntry->data(ToolXmlRole).value<QString>());
// Set id
QDomElement toolElement = doc.documentElement();
2015-10-29 12:37:11 +00:00
toolElement.setAttribute(QStringLiteral("id"), i + 1);
// Remove old shortcut, if any
QDomNode oldShortcut = toolElement.elementsByTagName(QStringLiteral("shortcut")).item(0);
if (oldShortcut.isElement()) {
toolElement.removeChild(oldShortcut);
}
// Create new shortcut element (only the first 9 tools are assigned a shortcut key)
if (i < 9) {
QDomElement newShortcut = doc.createElement(QStringLiteral("shortcut"));
newShortcut.appendChild(doc.createTextNode(QString::number(i + 1)));
toolElement.appendChild(newShortcut);
}
// Append to output
res << doc.toString(-1);
}
return res;
}
void WidgetAnnotTools::setTools(const QStringList &items)
{
m_list->clear();
// Parse each string and populate the list widget
for (const QString &toolXml : items) {
QDomDocument entryParser;
if (!entryParser.setContent(toolXml)) {
2014-09-11 19:12:27 +00:00
qWarning() << "Skipping malformed tool XML string";
break;
}
QDomElement toolElement = entryParser.documentElement();
if (toolElement.tagName() == QLatin1String("tool")) {
// Create list item and attach the source XML string as data
QString itemText = toolElement.attribute(QStringLiteral("name"));
if (itemText.isEmpty()) {
itemText = PageViewAnnotator::defaultToolName(toolElement);
}
QListWidgetItem *listEntry = new QListWidgetItem(itemText, m_list);
2019-09-18 11:41:19 +00:00
listEntry->setData(ToolXmlRole, QVariant::fromValue(toolXml));
listEntry->setIcon(PageViewAnnotator::makeToolPixmap(toolElement));
}
}
updateButtons();
}
2013-05-20 18:10:05 +00:00
void WidgetAnnotTools::slotEdit()
{
2013-05-20 18:10:05 +00:00
QListWidgetItem *listEntry = m_list->currentItem();
QDomDocument doc;
doc.setContent(listEntry->data(ToolXmlRole).value<QString>());
QDomElement toolElement = doc.documentElement();
EditAnnotToolDialog t(this, toolElement);
if (t.exec() != QDialog::Accepted) {
return;
}
doc = t.toolXml();
toolElement = doc.documentElement();
QString itemText = t.name();
// Store name attribute only if the user specified a customized name
if (!itemText.isEmpty()) {
toolElement.setAttribute(QStringLiteral("name"), itemText);
} else {
itemText = PageViewAnnotator::defaultToolName(toolElement);
}
2013-05-18 20:23:04 +00:00
// Edit list entry and attach XML string as data
listEntry->setText(itemText);
2019-09-18 11:41:19 +00:00
listEntry->setData(ToolXmlRole, QVariant::fromValue(doc.toString(-1)));
listEntry->setIcon(PageViewAnnotator::makeToolPixmap(toolElement));
// Select and scroll
m_list->setCurrentItem(listEntry);
m_list->scrollToItem(listEntry);
updateButtons();
Q_EMIT changed();
}
2013-05-20 17:57:35 +00:00
void WidgetAnnotTools::slotAdd()
{
EditAnnotToolDialog t(this);
if (t.exec() != QDialog::Accepted) {
return;
}
QDomDocument rootDoc = t.toolXml();
QDomElement toolElement = rootDoc.documentElement();
QString itemText = t.name();
// Store name attribute only if the user specified a customized name
if (!itemText.isEmpty()) {
toolElement.setAttribute(QStringLiteral("name"), itemText);
} else {
itemText = PageViewAnnotator::defaultToolName(toolElement);
}
// Create list entry and attach XML string as data
QListWidgetItem *listEntry = new QListWidgetItem(itemText, m_list);
2019-09-18 11:41:19 +00:00
listEntry->setData(ToolXmlRole, QVariant::fromValue(rootDoc.toString(-1)));
listEntry->setIcon(PageViewAnnotator::makeToolPixmap(toolElement));
// Select and scroll
m_list->setCurrentItem(listEntry);
m_list->scrollToItem(listEntry);
updateButtons();
Q_EMIT changed();
}