File indexing completed on 2024-04-28 15:51:43
0001 /* 0002 SPDX-FileCopyrightText: 2015 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "editdrawingtooldialog.h" 0008 0009 #include <KColorButton> 0010 #include <KLineEdit> 0011 #include <KLocalizedString> 0012 0013 #include <QDialogButtonBox> 0014 #include <QLabel> 0015 #include <QPushButton> 0016 #include <QSpinBox> 0017 #include <QVBoxLayout> 0018 0019 EditDrawingToolDialog::EditDrawingToolDialog(const QDomElement &initialState, QWidget *parent) 0020 : QDialog(parent) 0021 { 0022 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); 0023 buttonBox->setObjectName(QStringLiteral("buttonbox")); 0024 QVBoxLayout *mainLayout = new QVBoxLayout(this); 0025 0026 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok); 0027 okButton->setDefault(true); 0028 okButton->setShortcut(Qt::CTRL | Qt::Key_Return); // NOLINT(bugprone-suspicious-enum-usage) 0029 connect(buttonBox, &QDialogButtonBox::accepted, this, &EditDrawingToolDialog::accept); 0030 connect(buttonBox, &QDialogButtonBox::rejected, this, &EditDrawingToolDialog::reject); 0031 okButton->setDefault(true); 0032 0033 QWidget *widget = new QWidget(this); 0034 QGridLayout *widgetLayout = new QGridLayout(widget); 0035 0036 mainLayout->addWidget(widget); 0037 mainLayout->addWidget(buttonBox); 0038 0039 m_name = new KLineEdit(widget); 0040 m_name->setObjectName(QStringLiteral("name")); 0041 mainLayout->addWidget(m_name); 0042 0043 QLabel *tmplabel = new QLabel(i18n("&Name:"), widget); 0044 mainLayout->addWidget(tmplabel); 0045 tmplabel->setBuddy(m_name); 0046 0047 widgetLayout->addWidget(tmplabel, 0, 0, Qt::AlignRight); 0048 widgetLayout->addWidget(m_name, 0, 1); 0049 0050 tmplabel = new QLabel(i18n("Color:"), widget); 0051 widgetLayout->addWidget(tmplabel, 1, 0, Qt::AlignRight); 0052 0053 m_colorBn = new KColorButton(this); 0054 m_colorBn->setObjectName(QStringLiteral("colorbutton")); 0055 widgetLayout->addWidget(m_colorBn, 1, 1, Qt::AlignRight); 0056 0057 tmplabel = new QLabel(i18n("&Pen Width:"), widget); 0058 widgetLayout->addWidget(tmplabel, 2, 0, Qt::AlignRight); 0059 0060 m_penWidth = new QSpinBox(widget); 0061 m_penWidth->setObjectName(QStringLiteral("penWidth")); 0062 m_penWidth->setRange(0, 50); 0063 m_penWidth->setSuffix(i18nc("Suffix for the pen width, eg '10 px'", " px")); 0064 tmplabel->setBuddy(m_penWidth); 0065 widgetLayout->addWidget(m_penWidth, 2, 1); 0066 0067 tmplabel = new QLabel(i18n("&Opacity:"), widget); 0068 widgetLayout->addWidget(tmplabel, 3, 0, Qt::AlignRight); 0069 0070 m_opacity = new QSpinBox(widget); 0071 m_opacity->setObjectName(QStringLiteral("opacity")); 0072 m_opacity->setRange(0, 100); 0073 m_opacity->setSuffix(i18nc("Suffix for the opacity level, eg '80 %'", " %")); 0074 tmplabel->setBuddy(m_opacity); 0075 widgetLayout->addWidget(m_opacity, 3, 1); 0076 0077 if (initialState.isNull()) { 0078 setWindowTitle(i18n("Create drawing tool")); 0079 m_colorBn->setColor(Qt::black); 0080 m_penWidth->setValue(2); 0081 m_opacity->setValue(100); 0082 } else { 0083 setWindowTitle(i18n("Edit drawing tool")); 0084 loadTool(initialState); 0085 } 0086 0087 m_name->setFocus(); 0088 } 0089 0090 EditDrawingToolDialog::~EditDrawingToolDialog() 0091 { 0092 } 0093 0094 QString EditDrawingToolDialog::name() const 0095 { 0096 return m_name->text(); 0097 } 0098 0099 QDomDocument EditDrawingToolDialog::toolXml() const 0100 { 0101 QDomDocument doc; 0102 QDomElement toolElement = doc.createElement(QStringLiteral("tool")); 0103 QDomElement engineElement = doc.createElement(QStringLiteral("engine")); 0104 QDomElement annotationElement = doc.createElement(QStringLiteral("annotation")); 0105 doc.appendChild(toolElement); 0106 toolElement.appendChild(engineElement); 0107 engineElement.appendChild(annotationElement); 0108 0109 const QString color = m_colorBn->color().name(); 0110 const double opacity = m_opacity->value() / 100.0; 0111 0112 engineElement.setAttribute(QStringLiteral("color"), color); 0113 0114 annotationElement.setAttribute(QStringLiteral("type"), QStringLiteral("Ink")); 0115 annotationElement.setAttribute(QStringLiteral("color"), color); 0116 annotationElement.setAttribute(QStringLiteral("width"), QString::number(m_penWidth->value())); 0117 0118 if (opacity != 1.0) { 0119 annotationElement.setAttribute(QStringLiteral("opacity"), QString::number(opacity)); 0120 } 0121 0122 return doc; 0123 } 0124 0125 void EditDrawingToolDialog::loadTool(const QDomElement &toolElement) 0126 { 0127 const QDomElement engineElement = toolElement.elementsByTagName(QStringLiteral("engine")).item(0).toElement(); 0128 const QDomElement annotationElement = engineElement.elementsByTagName(QStringLiteral("annotation")).item(0).toElement(); 0129 0130 if (annotationElement.hasAttribute(QStringLiteral("color"))) { 0131 m_colorBn->setColor(QColor(annotationElement.attribute(QStringLiteral("color")))); 0132 } 0133 0134 m_penWidth->setValue(annotationElement.attribute(QStringLiteral("width"), QStringLiteral("2")).toInt()); 0135 m_opacity->setValue(annotationElement.attribute(QStringLiteral("opacity"), QStringLiteral("1.0")).toDouble() * 100); 0136 0137 if (toolElement.attribute(QStringLiteral("default"), QStringLiteral("false")) == QLatin1String("true")) { 0138 m_name->setText(i18n(toolElement.attribute(QStringLiteral("name")).toLatin1().constData())); 0139 } else { 0140 m_name->setText(toolElement.attribute(QStringLiteral("name"))); 0141 } 0142 }