File indexing completed on 2024-05-12 15:56:47

0001 /* This file is part of the KDE project
0002  *
0003  * SPDX-FileCopyrightText: 2006-2007, 2010 Thomas Zander <zander@kde.org>
0004  * SPDX-FileCopyrightText: 2006-2008 Thorsten Zachmann <zachmann@kde.org>
0005  * SPDX-FileCopyrightText: 2011 Jan Hambrecht <jaham@gmx.net>
0006  *
0007  * SPDX-License-Identifier: LGPL-2.0-or-later
0008  */
0009 
0010 #include "KoShapeController.h"
0011 #include "KoShapeControllerBase.h"
0012 #include "KoShapeRegistry.h"
0013 #include "KoDocumentResourceManager.h"
0014 #include "KoShapeManager.h"
0015 #include "KoShapeLayer.h"
0016 #include "KoSelection.h"
0017 #include "commands/KoShapeCreateCommand.h"
0018 #include "commands/KoShapeDeleteCommand.h"
0019 #include "KoCanvasBase.h"
0020 #include "KoShapeConfigWidgetBase.h"
0021 #include "KoShapeFactoryBase.h"
0022 #include "KoShape.h"
0023 #include <KoUnit.h>
0024 
0025 #include <QObject>
0026 
0027 #include <kpagedialog.h>
0028 #include <klocalizedstring.h>
0029 
0030 class KoShapeController::Private
0031 {
0032 public:
0033     Private()
0034         : canvas(0),
0035           shapeController(0)
0036     {
0037     }
0038 
0039     KoCanvasBase *canvas;
0040     KoShapeControllerBase *shapeController;
0041 
0042     KUndo2Command* addShape(KoShape *shape, bool showDialog, KoShapeContainer *parentShape, KUndo2Command *parent) {
0043 
0044         if (canvas) {
0045             if (showDialog && !shape->shapeId().isEmpty()) {
0046                 KoShapeFactoryBase *factory = KoShapeRegistry::instance()->value(shape->shapeId());
0047                 Q_ASSERT(factory);
0048                 qint16 z = 0;
0049                 Q_FOREACH (KoShape *sh, canvas->shapeManager()->shapes()) {
0050                     z = qMax(z, sh->zIndex());
0051                 }
0052                 shape->setZIndex(z + 1);
0053 
0054                 // show config dialog.
0055                 KPageDialog *dialog = new KPageDialog(canvas->canvasWidget());
0056                 dialog->setWindowTitle(i18n("%1 Options", factory->name()));
0057 
0058                 int pageCount = 0;
0059                 QList<KoShapeConfigWidgetBase*> widgets;
0060                 Q_FOREACH (KoShapeConfigWidgetBase* panel, factory->createShapeOptionPanels()) {
0061                     if (! panel->showOnShapeCreate())
0062                         continue;
0063                     panel->open(shape);
0064                     panel->connect(panel, SIGNAL(accept()), dialog, SLOT(accept()));
0065                     widgets.append(panel);
0066                     panel->setResourceManager(canvas->resourceManager());
0067                     panel->setUnit(canvas->unit());
0068                     QString title = panel->windowTitle().isEmpty() ? panel->objectName() : panel->windowTitle();
0069                     dialog->addPage(panel, title);
0070                     pageCount ++;
0071                 }
0072 
0073                 if (pageCount > 0) {
0074                     if (pageCount > 1)
0075                         dialog->setFaceType(KPageDialog::Tabbed);
0076                     if (dialog->exec() != KPageDialog::Accepted) {
0077                         delete dialog;
0078                         return 0;
0079                     }
0080                     Q_FOREACH (KoShapeConfigWidgetBase *widget, widgets)
0081                         widget->save();
0082                 }
0083                 delete dialog;
0084             }
0085         }
0086 
0087         return addShapesDirect({shape}, parentShape, parent);
0088     }
0089 
0090     KUndo2Command* addShapesDirect(const QList<KoShape*> shapes, KoShapeContainer *parentShape, KUndo2Command *parent)
0091     {
0092         KUndo2Command *resultCommand = 0;
0093 
0094         if (!parentShape) {
0095             resultCommand = new KUndo2Command(parent);
0096             parentShape = shapeController->createParentForShapes(shapes, resultCommand);
0097             KUndo2Command *addShapeCommand = new KoShapeCreateCommand(shapeController, shapes, parentShape, resultCommand);
0098             resultCommand->setText(addShapeCommand->text());
0099         } else {
0100             resultCommand = new KoShapeCreateCommand(shapeController, shapes, parentShape, parent);
0101         }
0102 
0103         return resultCommand;
0104     }
0105 };
0106 
0107 KoShapeController::KoShapeController(KoCanvasBase *canvas, KoShapeControllerBase *shapeController)
0108     : d(new Private())
0109 {
0110     d->canvas = canvas;
0111     d->shapeController = shapeController;
0112 }
0113 
0114 KoShapeController::~KoShapeController()
0115 {
0116     delete d;
0117 }
0118 
0119 void KoShapeController::reset()
0120 {
0121     d->canvas = 0;
0122     d->shapeController = 0;
0123 }
0124 
0125 KUndo2Command* KoShapeController::addShape(KoShape *shape, KoShapeContainer *parentShape, KUndo2Command *parent)
0126 {
0127     return d->addShape(shape, true, parentShape, parent);
0128 }
0129 
0130 KUndo2Command* KoShapeController::addShapeDirect(KoShape *shape, KoShapeContainer *parentShape, KUndo2Command *parent)
0131 {
0132     return d->addShapesDirect({shape}, parentShape, parent);
0133 }
0134 
0135 KUndo2Command *KoShapeController::addShapesDirect(const QList<KoShape *> shapes, KoShapeContainer *parentShape, KUndo2Command *parent)
0136 {
0137     return d->addShapesDirect(shapes, parentShape, parent);
0138 }
0139 
0140 KUndo2Command* KoShapeController::removeShape(KoShape *shape, KUndo2Command *parent)
0141 {
0142     return removeShapes({shape}, parent);
0143 }
0144 
0145 KUndo2Command* KoShapeController::removeShapes(const QList<KoShape*> &shapes, KUndo2Command *parent)
0146 {
0147     KUndo2Command *cmd = new KoShapeDeleteCommand(d->shapeController, shapes, parent);
0148     return cmd;
0149 }
0150 
0151 void KoShapeController::setShapeControllerBase(KoShapeControllerBase *shapeController)
0152 {
0153     d->shapeController = shapeController;
0154 }
0155 
0156 QRectF KoShapeController::documentRectInPixels() const
0157 {
0158     return d->shapeController ? d->shapeController->documentRectInPixels() : QRectF(0,0,1920,1080);
0159 }
0160 
0161 qreal KoShapeController::pixelsPerInch() const
0162 {
0163     return d->shapeController ? d->shapeController->pixelsPerInch() : 72.0;
0164 }
0165 
0166 QRectF KoShapeController::documentRect() const
0167 {
0168     return d->shapeController ? d->shapeController->documentRect() : documentRectInPixels();
0169 }
0170 
0171 KoDocumentResourceManager *KoShapeController::resourceManager() const
0172 {
0173     if (!d->shapeController) {
0174         qWarning() << "THIS IS NOT GOOD!";
0175         return 0;
0176     }
0177     return d->shapeController->resourceManager();
0178 }
0179 
0180 KoShapeControllerBase *KoShapeController::documentBase() const
0181 {
0182     return d->shapeController;
0183 }