libs/flake

KoShapeController.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002  *
00003  * Copyright (C) 2006-2007 Thomas Zander <zander@kde.org>
00004  * Copyright (C) 2006-2008 Thorsten Zachmann <zachmann@kde.org>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public License
00017  * along with this library; see the file COPYING.LIB.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  */
00021 
00022 #include "KoShapeController.h"
00023 #include "KoShapeControllerBase.h"
00024 #include "KoShapeRegistry.h"
00025 #include "KoShapeManager.h"
00026 #include "KoShapeLayer.h"
00027 #include "KoSelection.h"
00028 #include "commands/KoShapeCreateCommand.h"
00029 #include "commands/KoShapeDeleteCommand.h"
00030 #include "KoCanvasBase.h"
00031 #include "KoShapeConfigWidgetBase.h"
00032 #include "KoShapeConfigFactory.h"
00033 #include "KoShapeFactory.h"
00034 #include "KoShape.h"
00035 
00036 #include <kpagedialog.h>
00037 #include <klocale.h>
00038 
00039 class KoShapeController::Private
00040 {
00041 public:
00042     Private() : canvas(0), shapeController(0) {}
00043     KoCanvasBase *canvas;
00044     KoShapeControllerBase *shapeController;
00045 
00046     QUndoCommand* addShape(KoShape *shape, bool showDialog, QUndoCommand *parent) {
00047         Q_ASSERT(canvas->shapeManager());
00048 
00049         if (showDialog) {
00050             KoShapeFactory *factory = KoShapeRegistry::instance()->value(shape->shapeId());
00051             Q_ASSERT(factory);
00052             int z = 0;
00053             foreach(KoShape *sh, canvas->shapeManager()->shapes())
00054                 z = qMax(z, sh->zIndex());
00055             shape->setZIndex(z + 1);
00056 
00057             // show config dialog.
00058             KPageDialog *dialog = new KPageDialog(canvas->canvasWidget());
00059             dialog->setCaption(i18n("%1 Options", factory->name()));
00060 
00061             int pageCount = 0;
00062             QList<KoShapeConfigFactory*> panels = factory->panelFactories();
00063             qSort(panels.begin(), panels.end(), KoShapeConfigFactory::compare);
00064             QList<KoShapeConfigWidgetBase*> widgets;
00065             foreach(KoShapeConfigFactory *panelFactory, panels) {
00066                 if (! panelFactory->showForShapeId(shape->shapeId()))
00067                     continue;
00068                 KoShapeConfigWidgetBase *widget = panelFactory->createConfigWidget(shape);
00069                 if (widget == 0)
00070                     continue;
00071                 if (! widget->showOnShapeCreate()) {
00072                     delete widget;
00073                     continue;
00074                 }
00075                 widgets.append(widget);
00076                 widget->setResourceProvider(canvas->resourceProvider());
00077                 widget->setUnit(canvas->unit());
00078                 dialog->addPage(widget, panelFactory->name());
00079                 pageCount ++;
00080             }
00081             foreach(KoShapeConfigWidgetBase* panel, factory->createShapeOptionPanels()) {
00082                 if (! panel->showOnShapeCreate())
00083                     continue;
00084                 panel->open(shape);
00085                 widgets.append(panel);
00086                 panel->setResourceProvider(canvas->resourceProvider());
00087                 panel->setUnit(canvas->unit());
00088                 QString title = panel->windowTitle().isEmpty() ? panel->objectName() : panel->windowTitle();
00089                 dialog->addPage(panel, title);
00090                 pageCount ++;
00091             }
00092 
00093             if (pageCount > 0) {
00094                 if (pageCount > 1)
00095                     dialog->setFaceType(KPageDialog::Tabbed);
00096                 if (dialog->exec() != KPageDialog::Accepted) {
00097                     delete dialog;
00098                     return 0;
00099                 }
00100                 foreach(KoShapeConfigWidgetBase *widget, widgets)
00101                     widget->save();
00102             }
00103             delete dialog;
00104         }
00105 
00106         // set the active layer as parent if there is not yet a parent.
00107         if (!shape->parent()) {
00108             shape->setParent(canvas->shapeManager()->selection()->activeLayer());
00109         }
00110 
00111         return new KoShapeCreateCommand(shapeController, shape, parent);
00112     }
00113 };
00114 
00115 KoShapeController::KoShapeController(KoCanvasBase *canvas, KoShapeControllerBase *shapeController)
00116         : d(new Private())
00117 {
00118     d->canvas = canvas;
00119     d->shapeController = shapeController;
00120 }
00121 
00122 KoShapeController::~KoShapeController()
00123 {
00124     delete d;
00125 }
00126 
00127 QUndoCommand* KoShapeController::addShape(KoShape *shape, QUndoCommand *parent)
00128 {
00129     return d->addShape(shape, true, parent);
00130 }
00131 
00132 QUndoCommand* KoShapeController::addShapeDirect(KoShape *shape, QUndoCommand *parent)
00133 {
00134     return d->addShape(shape, false, parent);
00135 }
00136 
00137 QUndoCommand* KoShapeController::removeShape(KoShape *shape, QUndoCommand *parent)
00138 {
00139     return new KoShapeDeleteCommand(d->shapeController, shape, parent);
00140 }
00141 
00142 QUndoCommand* KoShapeController::removeShapes(const QList<KoShape*> &shapes, QUndoCommand *parent)
00143 {
00144     return new KoShapeDeleteCommand(d->shapeController, shapes, parent);
00145 }
00146 
00147 void KoShapeController::setShapeControllerBase(KoShapeControllerBase* shapeControllerBase)
00148 {
00149     d->shapeController = shapeControllerBase;
00150 }
00151 
00152 KoDataCenter * KoShapeController::dataCenter(const QString &dataCenterName)
00153 {
00154     return d->shapeController->dataCenterMap().value(dataCenterName, 0);
00155 }
00156 
00157 QMap<QString, KoDataCenter *> KoShapeController::dataCenterMap()
00158 {
00159     return d->shapeController->dataCenterMap();
00160 }