F:/KPlato/koffice/libs/kofficeui/KoShapeSelector.cpp

Aller à la documentation de ce fichier.
00001 /*
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 "KoShapeSelector.h"
00021 
00022 #include <KoShapeManager.h>
00023 #include <KoPointerEvent.h>
00024 #include <KoShapeRegistry.h>
00025 #include <KoToolManager.h>
00026 #include <KoShapeFactory.h>
00027 #include <KoShape.h>
00028 #include <KoShapeContainer.h>
00029 #include <KoInteractionTool.h>
00030 #include <KoShapeMoveStrategy.h>
00031 #include <KoCreateShapesTool.h>
00032 #include <KoShapeController.h>
00033 #include <KoCanvasController.h>
00034 
00035 #include <QKeyEvent>
00036 #include <QPainter>
00037 #include <QMouseEvent>
00038 #include <QHelpEvent>
00039 #include <QPointF>
00040 #include <QToolTip>
00041 #include <QTimer>
00042 
00043 #include <kdebug.h>
00044 #include <kicon.h>
00045 #include <klocale.h>
00046 
00047 // ******** IconShape *********
00049 class IconShape : public KoShape {
00050 public:
00051     IconShape(const QString &icon) {
00052         m_icon = KIcon(icon).pixmap(22);
00053         resize(m_icon.size());
00054     }
00055 
00056     virtual void visit(KoShapeController *tool) = 0;
00057     virtual QString toolTip() = 0;
00058 
00059     void paint(QPainter &painter, const KoViewConverter &converter) {
00060         Q_UNUSED(converter);
00061         painter.drawPixmap(QRect( QPoint(0,0), m_icon.size()), m_icon);
00062     }
00063 
00064 private:
00065     QPixmap m_icon;
00066 };
00067 
00068 
00069 // ******** TemplateShape *********
00071 class TemplateShape : public IconShape {
00072 public:
00073     TemplateShape(KoShapeTemplate shapeTemplate) : IconShape(shapeTemplate.icon) {
00074         m_shapeTemplate = shapeTemplate;
00075     }
00076 
00077     void visit(KoShapeController *tool) {
00078         tool->setShapeId(m_shapeTemplate.id);
00079         tool->setShapeProperties(m_shapeTemplate.properties);
00080     }
00081 
00082     QString toolTip() {
00083         return m_shapeTemplate.toolTip;
00084     }
00085 
00086 private:
00087     KoShapeTemplate m_shapeTemplate;
00088 };
00089 
00090 
00091 // ******** GroupShape *********
00093 class GroupShape : public IconShape {
00094 public:
00095     GroupShape(KoShapeFactory *shapeFactory) : IconShape(shapeFactory->icon()) {
00096         m_shapeFactory = shapeFactory;
00097     }
00098 
00099     void visit(KoShapeController *tool) {
00100         tool->setShapeId(m_shapeFactory->shapeId());
00101         tool->setShapeProperties(0);
00102     }
00103 
00104     QString toolTip() {
00105         return m_shapeFactory->toolTip();
00106     }
00107 
00108 private:
00109     KoShapeFactory *m_shapeFactory;
00110 };
00111 
00112 
00113 // ******* MoveTool *********
00115 class MoveTool : public KoInteractionTool {
00116 public:
00117     MoveTool(KoCanvasBase *canvas) : KoInteractionTool(canvas) {};
00118     void mousePressEvent( KoPointerEvent *event ) {
00119         KoShape *clickedShape = m_canvas->shapeManager()->shapeAt(event->point);
00120         repaintDecorations();
00121         m_canvas->shapeManager()->selection()->deselectAll();
00122         if(clickedShape) {
00123             m_canvas->shapeManager()->selection()->select(clickedShape);
00124         }
00125         m_currentStrategy = new KoShapeMoveStrategy(this, m_canvas, event->point);
00126     }
00127     void mouseMoveEvent (KoPointerEvent *event) {
00128         KoInteractionTool::mouseMoveEvent(event);
00129     }
00130 };
00131 
00132 
00133 // ************** KoShapeSelector ************
00134 KoShapeSelector::KoShapeSelector(QWidget *parent)
00135 : QDockWidget(i18n("Shapes"), parent)
00136 {
00137     setObjectName("ShapeSelector");
00138     m_canvas = new Canvas(this);
00139     setWidget(m_canvas);
00140     m_shapeManager = new KoShapeManager(m_canvas);
00141     setMinimumSize(30, 200);
00142     setFeatures(DockWidgetMovable|DockWidgetFloatable);
00143 
00144     QTimer::singleShot(0, this, SLOT(loadShapeTypes()));
00145 }
00146 
00147 KoShapeSelector::~KoShapeSelector() {
00148     delete m_shapeManager;
00149     delete m_canvas;
00150 }
00151 
00152 void KoShapeSelector::loadShapeTypes() {
00153     foreach(KoID id, KoShapeRegistry::instance()->listKeys()) {
00154         KoShapeFactory *factory = KoShapeRegistry::instance()->get(id);
00155         bool oneAdded=false;
00156         foreach(KoShapeTemplate shapeTemplate, factory->templates()) {
00157             oneAdded=true;
00158             TemplateShape *shape = new TemplateShape(shapeTemplate);
00159             add(shape);
00160         }
00161         if(!oneAdded)
00162             add(new GroupShape(factory));
00163     }
00164 
00165     connect(m_shapeManager->selection(), SIGNAL(selectionChanged()), this, SLOT(itemSelected()));
00166 }
00167 
00168 void KoShapeSelector::itemSelected() {
00169     QList<KoShape*> allSelected = m_shapeManager->selection()->selectedShapes().toList();
00170     if(allSelected.isEmpty())
00171         return;
00172     IconShape *shape= static_cast<IconShape*> (allSelected.first());
00173     KoCanvasController* canvasController = KoToolManager::instance()->activeCanvasController();
00174 
00175     if(canvasController) {
00176         KoShapeController* controller = KoToolManager::instance()->shapeController(canvasController->canvas());
00177         shape->visit(controller);
00178         KoToolManager::instance()->switchToolRequested(KoCreateShapesTool_ID);
00179     }
00180 }
00181 
00182 void KoShapeSelector::add(KoShape *shape) {
00183     int x=5, y=5; // 5 = gap
00184     int w = (int) shape->size().width();
00185     bool ok=true; // lets be optimistic ;)
00186     do {
00187         int rowHeight=0;
00188         ok=true;
00189         foreach(const KoShape *shape, m_shapeManager->shapes()) {
00190             if(shape->position().y() > y || shape->position().y() + shape->size().height() < y)
00191                 continue; // other row.
00192             rowHeight = qMax(rowHeight, qRound(shape->size().height()));
00193             x = qMax(x, qRound(shape->position().x() + shape->size().width()) + 5); // 5=gap
00194             if(x + w > width()) { // next row
00195                 y += rowHeight + 5; // 5 = gap
00196                 x = 5;
00197                 ok=false;
00198                 break;
00199             }
00200         }
00201     } while(! ok);
00202     shape->setPosition(QPointF(x, y));
00203 
00204     m_shapeManager->add(shape);
00205 }
00206 
00207 
00208 // ************ DummyViewConverter **********
00209 QPointF KoShapeSelector::DummyViewConverter::documentToView (const QPointF &documentPoint) const {
00210     return documentPoint;
00211 }
00212 
00213 QPointF KoShapeSelector::DummyViewConverter::viewToDocument (const QPointF &viewPoint) const {
00214     return viewPoint;
00215 }
00216 
00217 QRectF KoShapeSelector::DummyViewConverter::documentToView (const QRectF &documentRect) const {
00218     return documentRect;
00219 }
00220 
00221 QRectF KoShapeSelector::DummyViewConverter::viewToDocument (const QRectF &viewRect) const {
00222     return viewRect;
00223 }
00224 
00225 void KoShapeSelector::DummyViewConverter::zoom (double *zoomX, double *zoomY) const {
00226     *zoomX = 1.0;
00227     *zoomY = 1.0;
00228 }
00229 
00230 double KoShapeSelector::DummyViewConverter::documentToViewX (double documentX) const {
00231     return documentX;
00232 }
00233 
00234 double KoShapeSelector::DummyViewConverter::documentToViewY (double documentY) const {
00235     return documentY;
00236 }
00237 
00238 double KoShapeSelector::DummyViewConverter::viewToDocumentX (double viewX) const {
00239     return viewX;
00240 }
00241 
00242 double KoShapeSelector::DummyViewConverter::viewToDocumentY (double viewY) const {
00243     return viewY;
00244 }
00245 
00246 
00247 // ********* Canvas **********
00248 KoShapeSelector::Canvas::Canvas(KoShapeSelector *parent)
00249 : QWidget(parent), m_parent(parent)
00250 {
00251     m_toolProxy = KoToolManager::instance()->toolProxy();
00252     m_tool = new MoveTool(this);
00253 
00254     setAutoFillBackground(true);
00255     setBackgroundRole(QPalette::Base);
00256 }
00257 
00258 void KoShapeSelector::Canvas::gridSize (double *horizontal, double *vertical) const {
00259     Q_UNUSED(horizontal);
00260     Q_UNUSED(vertical);
00261 }
00262 
00263 void KoShapeSelector::Canvas::updateCanvas (const QRectF &rc) {
00264     QRect rect = rc.toRect();
00265     rect.adjust(-2, -2, 2, 2); // grow for to anti-aliasing
00266     update(rect);
00267 }
00268 
00269 void  KoShapeSelector::Canvas::addCommand (KCommand *command, bool execute) {
00270     if(execute)
00271         command->execute();
00272     delete command;
00273 }
00274 
00275 // event handlers
00276 void KoShapeSelector::Canvas::mouseMoveEvent(QMouseEvent *e) {
00277     KoPointerEvent ev(e, QPointF( viewConverter()->viewToDocument(e->pos()) ));
00278     m_tool->mouseMoveEvent( &ev );
00279 }
00280 
00281 void KoShapeSelector::Canvas::mousePressEvent(QMouseEvent *e) {
00282     KoPointerEvent ev(e, QPointF( viewConverter()->viewToDocument(e->pos()) ));
00283     m_tool->mousePressEvent( &ev );
00284 }
00285 
00286 void KoShapeSelector::Canvas::mouseReleaseEvent(QMouseEvent *e) {
00287     KoPointerEvent ev(e, QPointF( viewConverter()->viewToDocument(e->pos()) ));
00288     m_tool->mouseReleaseEvent( &ev );
00289 }
00290 
00291 void KoShapeSelector::Canvas::keyReleaseEvent (QKeyEvent *e) {
00292     m_tool->keyReleaseEvent(e);
00293 }
00294 
00295 void KoShapeSelector::Canvas::keyPressEvent( QKeyEvent *e ) {
00296     m_tool->keyPressEvent(e);
00297 }
00298 
00299 void KoShapeSelector::Canvas::paintEvent(QPaintEvent * e) {
00300     QPainter painter( this );
00301     painter.setRenderHint(QPainter::Antialiasing);
00302     painter.setClipRect(e->rect());
00303 
00304     m_parent->m_shapeManager->paint( painter, *(viewConverter()), false );
00305     painter.end();
00306 }
00307 
00308 bool KoShapeSelector::Canvas::event(QEvent *e) {
00309     if (e->type() == QEvent::ToolTip) {
00310         QHelpEvent *helpEvent = static_cast<QHelpEvent *>(e);
00311 
00312         const QPointF pos(helpEvent->x(), helpEvent->y());
00313         KoShape *shape = m_parent->m_shapeManager->shapeAt(pos);
00314         if(shape) {
00315             IconShape *is = static_cast<IconShape*> (shape);
00316             QToolTip::showText(helpEvent->globalPos(), is->toolTip());
00317         }
00318         else
00319             QToolTip::showText(helpEvent->globalPos(), "");
00320     }
00321     return QWidget::event(e);
00322 }
00323 
00324 #include "KoShapeSelector.moc"

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