ShapeSelector.cpp 3.99 KB
Newer Older
1
/*
2
 * Copyright (C) 2006-2007 Thomas Zander <zander@kde.org>
3
 * Copyright (C) 2006 Thorsten Zachmann <zachmann@kde.org>
4
 *
5 6 7 8
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
9
 *
10 11 12 13
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
14
 *
15 16 17 18
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
19 20
 */

21 22 23 24 25
#include "ShapeSelector.h"
#include "GroupShape.h"
#include "IconShape.h"
#include "TemplateShape.h"
#include "Canvas.h"
26
#include "FolderShape.h"
27 28 29 30 31

#include <KoShapeManager.h>
#include <KoShapeRegistry.h>
#include <KoToolManager.h>
#include <KoShapeFactory.h>
32
#include <KoSelection.h>
33
#include <KoCreateShapesTool.h>
34 35 36
#include <KoCanvasController.h>

#include <QPainter>
Thomas Zander's avatar
Thomas Zander committed
37
#include <QToolTip>
Thomas Zander's avatar
Thomas Zander committed
38
#include <QTimer>
39
#include <klocale.h>
40

41 42
// ************** ShapeSelector ************
ShapeSelector::ShapeSelector(QWidget *parent)
43
: QDockWidget(i18n("Shapes"), parent)
44
{
45
    setObjectName("ShapeSelector");
46
    m_canvas = new Canvas(this);
47
    setWidget(m_canvas);
48
    m_shapeManager = new KoShapeManager(m_canvas);
Thomas Zander's avatar
Thomas Zander committed
49
    setMinimumSize(30, 30);
50
    m_mainFolder = new FolderShape();
51

Thomas Zander's avatar
Thomas Zander committed
52
    QTimer::singleShot(0, this, SLOT(loadShapeTypes()));
53
    connect(m_canvas, SIGNAL(resized(const QSize&)), this, SLOT(setSize(const QSize &)));
Thomas Zander's avatar
Thomas Zander committed
54 55
}

56
ShapeSelector::~ShapeSelector() {
Thomas Zander's avatar
Thomas Zander committed
57 58 59 60
    delete m_shapeManager;
    delete m_canvas;
}

61
void ShapeSelector::loadShapeTypes() {
62
    m_mainFolder->setSize(m_canvas->size());
63

64 65
    foreach(QString id, KoShapeRegistry::instance()->keys()) {
        KoShapeFactory *factory = KoShapeRegistry::instance()->value(id);
66
        bool oneAdded=false;
67
        foreach(KoShapeTemplate shapeTemplate, factory->templates()) {
68 69
            oneAdded=true;
            TemplateShape *shape = new TemplateShape(shapeTemplate);
70
            m_mainFolder->addChild(shape);
71
        }
72
        if(!oneAdded)
73
            m_mainFolder->addChild(new GroupShape(factory));
74
    }
75
    m_shapeManager->add(m_mainFolder);
76 77
}

78
void ShapeSelector::itemSelected() {
79 80
    KoShape *koShape = m_shapeManager->selection()->firstSelectedShape();
    if(koShape == 0)
81
        return;
82 83 84
    IconShape *shape= dynamic_cast<IconShape*> (koShape);
    if(shape == 0)
        return;
85
    KoCanvasController* canvasController = KoToolManager::instance()->activeCanvasController();
86

87
    if(canvasController) {
88 89
        KoCreateShapesTool * tool = KoToolManager::instance()->shapeCreatorTool( canvasController->canvas() );
        shape->visit( tool );
90 91
        KoToolManager::instance()->switchToolRequested(KoCreateShapesTool_ID);
    }
92 93
}

94
void ShapeSelector::add(KoShape *shape) {
95 96 97 98 99 100
    int x=5, y=5; // 5 = gap
    int w = (int) shape->size().width();
    bool ok=true; // lets be optimistic ;)
    do {
        int rowHeight=0;
        ok=true;
Thomas Zander's avatar
Thomas Zander committed
101
        foreach(const KoShape *shape, m_shapeManager->shapes()) {
102 103
            if(shape->position().y() > y || shape->position().y() + shape->size().height() < y)
                continue; // other row.
104 105
            rowHeight = qMax(rowHeight, qRound(shape->size().height()));
            x = qMax(x, qRound(shape->position().x() + shape->size().width()) + 5); // 5=gap
Thomas Zander's avatar
Thomas Zander committed
106
            if(x + w > width()) { // next row
107
                y += rowHeight + 5; // 5 = gap
Thomas Zander's avatar
Thomas Zander committed
108
                x = 5;
109 110 111 112 113 114 115 116 117 118
                ok=false;
                break;
            }
        }
    } while(! ok);
    shape->setPosition(QPointF(x, y));

    m_shapeManager->add(shape);
}

119
void ShapeSelector::setSize(const QSize &size) {
120
    m_mainFolder->setSize(QSizeF(size));
121 122
}

123
#include "ShapeSelector.moc"