File indexing completed on 2024-12-22 04:09:14

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2007 Thomas Zander <zander@kde.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "PathToolOptionWidget.h"
0008 #include "KoPathTool.h"
0009 #include <QAction>
0010 
0011 #include <KoPathShape.h>
0012 #include <KoParameterShape.h>
0013 #include <KoShapeConfigWidgetBase.h>
0014 #include <QVBoxLayout>
0015 #include <KoCanvasBase.h>
0016 #include <KoShapeRegistry.h>
0017 #include <KoShapeFactoryBase.h>
0018 #include <KoUnit.h>
0019 #include "kis_assert.h"
0020 
0021 PathToolOptionWidget::PathToolOptionWidget(KoPathTool *tool, QWidget *parent)
0022     : QWidget(parent),
0023       m_currentShape(0),
0024       m_currentPanel(0),
0025       m_canvas(tool->canvas())
0026 
0027 {
0028     widget.setupUi(this);
0029     widget.corner->setDefaultAction(tool->action("pathpoint-corner"));
0030     widget.smooth->setDefaultAction(tool->action("pathpoint-smooth"));
0031     widget.symmetric->setDefaultAction(tool->action("pathpoint-symmetric"));
0032     widget.lineSegment->setDefaultAction(tool->action("pathsegment-line"));
0033     widget.curveSegment->setDefaultAction(tool->action("pathsegment-curve"));
0034     widget.linePoint->setDefaultAction(tool->action("pathpoint-line"));
0035     widget.curvePoint->setDefaultAction(tool->action("pathpoint-curve"));
0036     widget.addPoint->setDefaultAction(tool->action("pathpoint-insert"));
0037     widget.removePoint->setDefaultAction(tool->action("pathpoint-remove"));
0038     widget.breakPoint->setDefaultAction(tool->action("path-break-point"));
0039     widget.breakSegment->setDefaultAction(tool->action("path-break-segment"));
0040     widget.joinSegment->setDefaultAction(tool->action("pathpoint-join"));
0041     widget.mergePoints->setDefaultAction(tool->action("pathpoint-merge"));
0042 
0043     widget.wdgShapeProperties->setVisible(false);
0044     widget.lineShapeProperties->setVisible(false);
0045 
0046     connect(widget.convertToPath, SIGNAL(released()), tool->action("convert-to-path"), SLOT(trigger()));
0047 }
0048 
0049 PathToolOptionWidget::~PathToolOptionWidget()
0050 {
0051 }
0052 
0053 void PathToolOptionWidget::setSelectionType(int type)
0054 {
0055     const bool plain = type & PlainPath;
0056     if (plain)
0057         widget.stackedWidget->setCurrentIndex(0);
0058     else
0059         widget.stackedWidget->setCurrentIndex(1);
0060 }
0061 
0062 QString shapeIdFromShape(KoPathShape *pathShape)
0063 {
0064     if (!pathShape) return QString();
0065 
0066     QString shapeId = pathShape->pathShapeId();
0067 
0068     KoParameterShape *paramShape = dynamic_cast<KoParameterShape *>(pathShape);
0069     if (paramShape && !paramShape->isParametricShape()) {
0070         shapeId = paramShape->shapeId();
0071     }
0072 
0073     return shapeId;
0074 }
0075 
0076 void PathToolOptionWidget::setCurrentShape(KoPathShape *pathShape)
0077 {
0078     const QString newShapeId = shapeIdFromShape(pathShape);
0079     if (pathShape == m_currentShape && m_currentShapeId == newShapeId) return;
0080 
0081     if (m_currentShape) {
0082         m_currentShape = 0;
0083         if (m_currentPanel) {
0084             m_currentPanel->deleteLater();
0085             m_currentPanel = 0;
0086             m_currentShapeId.clear();
0087         }
0088     }
0089 
0090     if (pathShape) {
0091         m_currentShape = pathShape;
0092         m_currentShapeId = newShapeId;
0093 
0094         KoShapeFactoryBase *factory = KoShapeRegistry::instance()->value(m_currentShapeId);
0095         KIS_SAFE_ASSERT_RECOVER_RETURN(factory);
0096 
0097         QList<KoShapeConfigWidgetBase*> panels = factory->createShapeOptionPanels();
0098         if (!panels.isEmpty()) {
0099             KoShapeConfigWidgetBase *activePanel = 0;
0100 
0101             Q_FOREACH (KoShapeConfigWidgetBase *panel, panels) {
0102                 if (!activePanel && panel->showOnShapeSelect()) {
0103                     activePanel = panel;
0104                 } else {
0105                     delete panel;
0106                 }
0107             }
0108 
0109             if (activePanel) {
0110                 KIS_ASSERT_RECOVER_RETURN(m_canvas);
0111                 m_currentPanel = activePanel;
0112                 m_currentPanel->setUnit(m_canvas->unit());
0113 
0114                 QLayout *baseLayout = widget.wdgShapeProperties->layout();
0115                 QVBoxLayout *layout = dynamic_cast<QVBoxLayout*>(widget.wdgShapeProperties->layout());
0116 
0117                 if (!layout) {
0118                     KIS_SAFE_ASSERT_RECOVER(!baseLayout) {
0119                         delete baseLayout;
0120                     }
0121                     layout = new QVBoxLayout(widget.wdgShapeProperties);
0122                 }
0123 
0124 
0125                 KIS_ASSERT_RECOVER_RETURN(widget.wdgShapeProperties->layout());
0126                 layout->addWidget(m_currentPanel);
0127                 connect(m_currentPanel, SIGNAL(propertyChanged()), SLOT(slotShapePropertyChanged()));
0128                 m_currentPanel->open(m_currentShape);
0129             }
0130         }
0131     }
0132 
0133     widget.wdgShapeProperties->setVisible(m_currentPanel);
0134     widget.lineShapeProperties->setVisible(m_currentPanel);
0135 }
0136 
0137 void PathToolOptionWidget::slotShapePropertyChanged()
0138 {
0139     KIS_SAFE_ASSERT_RECOVER_RETURN(m_currentPanel);
0140 
0141     KUndo2Command *command = m_currentPanel->createCommand();
0142     if (command) {
0143         m_canvas->addCommand(command);
0144     }
0145 }
0146 
0147 void PathToolOptionWidget::showEvent(QShowEvent *event)
0148 {
0149     emit sigRequestUpdateActions();
0150     QWidget::showEvent(event);
0151 }