F:/KPlato/koffice/libs/flake/KoCreateShapeStrategy.cpp

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002  * Copyright (C) 2006 Thomas Zander <zander@kde.org>
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this library; see the file COPYING.LIB.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "KoCreateShapeStrategy.h"
00021 #include "KoCreateShapesTool.h"
00022 #include "KoShapeControllerBase.h"
00023 #include "KoShapeRegistry.h"
00024 #include "KoShapeManager.h"
00025 #include "KoCommand.h"
00026 #include "KoCanvasBase.h"
00027 #include "KoShapeConfigWidgetBase.h"
00028 #include "KoShapeConfigFactory.h"
00029 
00030 #include <KoProperties.h>
00031 
00032 #include <kpagedialog.h>
00033 #include <klocale.h>
00034 
00035 KoCreateShapeStrategy::KoCreateShapeStrategy( KoCreateShapesTool *tool, KoCanvasBase *canvas, const QPointF &clicked)
00036 : KoShapeRubberSelectStrategy(tool, canvas, clicked)
00037 , m_tool(tool)
00038 {
00039 }
00040 
00041 KCommand* KoCreateShapeStrategy::createCommand() {
00042     KoCreateShapesTool *parent = static_cast<KoCreateShapesTool*>(m_parent);
00043     KoShapeFactory *factory = KoShapeRegistry::instance()->get(parent->shapeId());
00044     if(! factory) {
00045         kWarning(30001) << "Application requested a shape that is not registered '" <<
00046             static_cast<KoCreateShapesTool*>(m_parent)->shapeId() << "'" << endl;
00047         return 0;
00048     }
00049     const KoProperties *props = parent->shapeProperties();
00050     KoShape *shape;
00051     if(props)
00052         shape = factory->createShape(props);
00053     else
00054         shape = factory->createDefaultShape();
00055     QRectF rect = selectRect();
00056     shape->setPosition(rect.topLeft());
00057     QSizeF newSize = rect.size();
00058     // if the user has dragged when creating the shape,
00059     // resize the shape to the dragged size
00060     if(newSize.width() > 1.0 && newSize.height() > 1.0) 
00061         shape->resize(newSize);
00062 
00063     Q_ASSERT(m_canvas->shapeManager());
00064     int z=0;
00065     foreach(KoShape *sh, m_canvas->shapeManager()->shapesAt(shape->boundingRect()))
00066         z = qMax(z, sh->zIndex());
00067     shape->setZIndex(z+1);
00068 
00069     // show config dialog.
00070     KPageDialog *dialog = new KPageDialog(m_canvas->canvasWidget());
00071     dialog->setCaption(i18n("%1 Options", factory->name()));
00072 
00073     int pageCount = 0;
00074     QList<KoShapeConfigFactory*> panels = factory->panelFactories();
00075     qSort(panels.begin(), panels.end(), KoShapeConfigFactory::compare);
00076     QList<KoShapeConfigWidgetBase*> widgets;
00077     foreach (KoShapeConfigFactory *panelFactory, panels) {
00078         if(! panelFactory->showForShapeId(parent->shapeId()))
00079             continue;
00080         KoShapeConfigWidgetBase *widget = panelFactory->createConfigWidget(shape);
00081         if(widget == 0)
00082             continue;
00083         widgets.append(widget);
00084         widget->setUnit(m_canvas->unit());
00085         dialog->addPage(widget, panelFactory->name());
00086         pageCount ++;
00087     }
00088     foreach(KoShapeConfigWidgetBase* panel, factory->createShapeOptionPanels()) {
00089         panel->open(shape);
00090         widgets.append(panel);
00091         panel->setUnit(m_canvas->unit());
00092         dialog->addPage(panel, panel->objectName());
00093         pageCount ++;
00094     }
00095 
00096     if(pageCount > 0) {
00097         if(pageCount > 1)
00098             dialog->setFaceType(KPageDialog::Tabbed);
00099         if(dialog->exec() != KPageDialog::Accepted) {
00100             delete dialog;
00101             return 0;
00102         }
00103         foreach(KoShapeConfigWidgetBase *widget, widgets) {
00104             widget->save();
00105             // TODO action;
00106         }
00107     }
00108     delete dialog;
00109 
00110     KoSelection *selection = m_canvas->shapeManager()->selection();
00111     selection->deselectAll();
00112     selection->select(shape);
00113 
00114     Q_ASSERT(m_tool->controller() /*controller was set on parent tool*/);
00115     KoShapeCreateCommand *cmd = new KoShapeCreateCommand(m_tool->controller(), shape);
00116     cmd->execute();
00117     return cmd;
00118 }
00119 
00120 void KoCreateShapeStrategy::finishInteraction( Qt::KeyboardModifiers modifiers ) {
00121     Q_UNUSED( modifiers );
00122     m_canvas->updateCanvas(selectRect());
00123 }

Généré le Wed Nov 22 23:40:59 2006 pour KPlato par  doxygen 1.5.1-p1