File indexing completed on 2024-04-28 15:52:01
0001 /* 0002 SPDX-FileCopyrightText: 2015 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "widgetdrawingtools.h" 0008 0009 #include "editdrawingtooldialog.h" 0010 0011 #include <KLocalizedString> 0012 #include <KMessageBox> 0013 0014 #include <QDebug> 0015 #include <QDomElement> 0016 #include <QListWidgetItem> 0017 #include <QPainter> 0018 0019 // Used to store tools' XML description in m_list's items 0020 static const int ToolXmlRole = Qt::UserRole; 0021 0022 static QPixmap colorDecorationFromToolDescription(const QString &toolDescription) 0023 { 0024 QDomDocument doc; 0025 doc.setContent(toolDescription, true); 0026 const QDomElement toolElement = doc.documentElement(); 0027 const QDomElement engineElement = toolElement.elementsByTagName(QStringLiteral("engine")).at(0).toElement(); 0028 const QDomElement annotationElement = engineElement.elementsByTagName(QStringLiteral("annotation")).at(0).toElement(); 0029 0030 QPixmap pm(50, 20); 0031 pm.fill(QColor(annotationElement.attribute(QStringLiteral("color")))); 0032 0033 QPainter p(&pm); 0034 p.setPen(Qt::black); 0035 p.drawRect(QRect(0, 0, pm.width() - 1, pm.height() - 1)); 0036 0037 return pm; 0038 } 0039 0040 WidgetDrawingTools::WidgetDrawingTools(QWidget *parent) 0041 : WidgetConfigurationToolsBase(parent) 0042 { 0043 } 0044 0045 WidgetDrawingTools::~WidgetDrawingTools() 0046 { 0047 } 0048 0049 QStringList WidgetDrawingTools::tools() const 0050 { 0051 QStringList res; 0052 0053 const int count = m_list->count(); 0054 for (int i = 0; i < count; ++i) { 0055 QListWidgetItem *listEntry = m_list->item(i); 0056 0057 // Parse associated DOM data 0058 QDomDocument doc; 0059 doc.setContent(listEntry->data(ToolXmlRole).value<QString>()); 0060 0061 // Append to output 0062 res << doc.toString(-1); 0063 } 0064 0065 return res; 0066 } 0067 0068 void WidgetDrawingTools::setTools(const QStringList &items) 0069 { 0070 m_list->clear(); 0071 0072 // Parse each string and populate the list widget 0073 for (const QString &toolXml : items) { 0074 QDomDocument entryParser; 0075 if (!entryParser.setContent(toolXml)) { 0076 qWarning() << "Skipping malformed tool XML string"; 0077 break; 0078 } 0079 0080 const QDomElement toolElement = entryParser.documentElement(); 0081 if (toolElement.tagName() == QLatin1String("tool")) { 0082 const QString name = toolElement.attribute(QStringLiteral("name")); 0083 QString itemText; 0084 if (toolElement.attribute(QStringLiteral("default"), QStringLiteral("false")) == QLatin1String("true")) { 0085 itemText = i18n(name.toLatin1().constData()); 0086 } else { 0087 itemText = name; 0088 } 0089 0090 QListWidgetItem *listEntry = new QListWidgetItem(itemText, m_list); 0091 listEntry->setData(ToolXmlRole, QVariant::fromValue(toolXml)); 0092 listEntry->setData(Qt::DecorationRole, colorDecorationFromToolDescription(toolXml)); 0093 } 0094 } 0095 0096 updateButtons(); 0097 } 0098 0099 QString WidgetDrawingTools::defaultName() const 0100 { 0101 int nameIndex = 1; 0102 bool freeNameFound = false; 0103 QString candidateName; 0104 while (!freeNameFound) { 0105 candidateName = i18n("Default Drawing Tool #%1", nameIndex); 0106 int i = 0; 0107 for (; i < m_list->count(); ++i) { 0108 QListWidgetItem *listEntry = m_list->item(i); 0109 if (candidateName == listEntry->text()) { 0110 break; 0111 } 0112 } 0113 freeNameFound = i == m_list->count(); 0114 ++nameIndex; 0115 } 0116 return candidateName; 0117 } 0118 0119 void WidgetDrawingTools::slotAdd() 0120 { 0121 EditDrawingToolDialog dlg(QDomElement(), this); 0122 0123 if (dlg.exec() != QDialog::Accepted) { 0124 return; 0125 } 0126 0127 const QDomDocument rootDoc = dlg.toolXml(); 0128 QDomElement toolElement = rootDoc.documentElement(); 0129 0130 QString itemText = dlg.name().trimmed(); 0131 0132 if (itemText.isEmpty()) { 0133 itemText = defaultName(); 0134 } 0135 0136 for (int i = 0; i < m_list->count(); ++i) { 0137 QListWidgetItem *listEntry = m_list->item(i); 0138 if (itemText == listEntry->text()) { 0139 KMessageBox::information(this, i18n("There's already a tool with that name. Using a default one"), i18n("Duplicated Name")); 0140 itemText = defaultName(); 0141 break; 0142 } 0143 } 0144 0145 // Store name attribute only if the user specified a customized name 0146 toolElement.setAttribute(QStringLiteral("name"), itemText); 0147 0148 // Create list entry and attach XML string as data 0149 const QString toolXml = rootDoc.toString(-1); 0150 QListWidgetItem *listEntry = new QListWidgetItem(itemText, m_list); 0151 listEntry->setData(ToolXmlRole, QVariant::fromValue(toolXml)); 0152 listEntry->setData(Qt::DecorationRole, colorDecorationFromToolDescription(toolXml)); 0153 0154 // Select and scroll 0155 m_list->setCurrentItem(listEntry); 0156 m_list->scrollToItem(listEntry); 0157 updateButtons(); 0158 Q_EMIT changed(); 0159 } 0160 0161 void WidgetDrawingTools::slotEdit() 0162 { 0163 QListWidgetItem *listEntry = m_list->currentItem(); 0164 0165 QDomDocument doc; 0166 doc.setContent(listEntry->data(ToolXmlRole).value<QString>()); 0167 QDomElement toolElement = doc.documentElement(); 0168 0169 EditDrawingToolDialog dlg(toolElement, this); 0170 0171 if (dlg.exec() != QDialog::Accepted) { 0172 return; 0173 } 0174 0175 doc = dlg.toolXml(); 0176 toolElement = doc.documentElement(); 0177 0178 QString itemText = dlg.name(); 0179 0180 for (int i = 0; i < m_list->count(); ++i) { 0181 QListWidgetItem *auxListEntry = m_list->item(i); 0182 if (itemText == auxListEntry->text() && auxListEntry != listEntry) { 0183 KMessageBox::information(this, i18n("There's already a tool with that name. Using a default one"), i18n("Duplicated Name")); 0184 itemText = defaultName(); 0185 break; 0186 } 0187 } 0188 0189 // Store name attribute only if the user specified a customized name 0190 toolElement.setAttribute(QStringLiteral("name"), itemText); 0191 0192 // Edit list entry and attach XML string as data 0193 const QString toolXml = doc.toString(-1); 0194 listEntry->setText(itemText); 0195 listEntry->setData(ToolXmlRole, QVariant::fromValue(toolXml)); 0196 listEntry->setData(Qt::DecorationRole, colorDecorationFromToolDescription(toolXml)); 0197 0198 // Select and scroll 0199 m_list->setCurrentItem(listEntry); 0200 m_list->scrollToItem(listEntry); 0201 updateButtons(); 0202 Q_EMIT changed(); 0203 }