00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00059
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
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
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() );
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 }