MDIApplication Example

#include <QtitanDef.h>

#include "sketchdocument.h"
#include "sketchobject.h"
#include "sketchrepresentation.h"

/* SketchDocumentPrivate */
class SketchDocumentPrivate
{
public:
    SketchDocumentPrivate(SketchDocument* p);
public:
    void init();
    void clear();
    GeomGrid* getGrid() const;
public:
    QList<GeomObject*> m_objects;
    QList<GeomObject*> m_selectObjects;
    GeomObject* m_highlightObject;
    double m_width;
    double m_height;
    bool m_bModified;
protected:
    SketchDocument* m_parent;
};

SketchDocumentPrivate::SketchDocumentPrivate(SketchDocument* p)
{
    m_parent = p;
    m_width = 0.0;
    m_height = 0.0;
    m_bModified = false;
    m_highlightObject = Q_NULL;
}

void SketchDocumentPrivate::init()
{
    GeomGrid* pObject = new GeomGrid;
    pObject->setVisible(true);
    m_parent->addObject(pObject);
}

void SketchDocumentPrivate::clear()
{
    for (int i = m_objects.size()-1; i >= 0; i--)
    {
        GeomObject* object = m_objects[i];
        m_objects.removeAt(i);
        delete object;
    }
}

GeomGrid* SketchDocumentPrivate::getGrid() const
{
    for (int i = 0, count = m_objects.size(); count > i; i++)
    {
        if (GeomGrid* pGrid = dynamic_cast<GeomGrid*>(m_objects[i]))
            return pGrid;
    }
    return Q_NULL;
}

#undef THIS
#define THIS this->m_pimpl

/* SketchDocument */
SketchDocument::SketchDocument(QObject* p)
   : QObject(p)
{
    m_pimpl = new SketchDocumentPrivate(this);
    m_pimpl->init();
}

SketchDocument::~SketchDocument()
{
    m_pimpl->clear();
    delete m_pimpl;
}

void SketchDocument::addObject(GeomObject* pObject)
{
    m_pimpl->m_objects.append(pObject);
    pObject->updateObject();

    if (!dynamic_cast<GeomGrid*>(pObject))
        setModified(true);
}

void SketchDocument::setModified(bool bModified)
{
    m_pimpl->m_bModified = bModified;
    emit contentsChanged();
}

bool SketchDocument::isModified() const
{
    return m_pimpl->m_bModified;
}

void SketchDocument::setVisibleGrid(bool bShow)
{
    if (GeomGrid* pGrid = m_pimpl->getGrid())
        pGrid->setVisible(bShow);
}

void SketchDocument::setStepGrid(double step)
{
    if (GeomGrid* pGrid = m_pimpl->getGrid())
        pGrid->setStepGrid(step);
}

double SketchDocument::getStepGrid() const
{
    if (GeomGrid* pGrid = m_pimpl->getGrid())
        return pGrid->getStepGrid();
    return 0.0;
}

GeomObject* SketchDocument::highlightObject() const
{
    return m_pimpl->m_highlightObject;
}

void SketchDocument::setHighlightObject(GeomObject* object)
{
    if (m_pimpl->m_highlightObject == object)
        return;

    if (m_pimpl->m_highlightObject)
        m_pimpl->m_highlightObject->setHighlight(false);

    if (object)
        object->setHighlight(true);

    m_pimpl->m_highlightObject = object;
}

const QList<GeomObject*> SketchDocument::selectObjects() const
{
    return m_pimpl->m_selectObjects;
}

void SketchDocument::setSelectObject(GeomObject* object, bool bMultiSelect)
{
    if (!object || !bMultiSelect)
    {
        for (int i = m_pimpl->m_selectObjects.size()-1; i >= 0; i--)
        {
            m_pimpl->m_selectObjects[i]->setSelect(false);
            m_pimpl->m_selectObjects.removeAt(i);
        }
        if (!object)
            return;
    }

    bool find = false;
    for (int j = 0, countJ = m_pimpl->m_selectObjects.size(); countJ > j && !find; j++)
        find = m_pimpl->m_selectObjects[j] == object;

    if (find)
    {
        object->setSelect(false);
        m_pimpl->m_selectObjects.removeOne(object);
    }
    else
    {
        object->setSelect(true);
        m_pimpl->m_selectObjects.append(object);
    }
}

void SketchDocument::setSize(double w, double h)
{
    m_pimpl->m_width = w;
    m_pimpl->m_height = h;
    updateObjectSize();
}

const QList<GeomObject*>& SketchDocument::getObjects() const
{
    return m_pimpl->m_objects;
}

void SketchDocument::updateObjectSize()
{
    if (GeomGrid* grid = m_pimpl->getGrid())
        grid->setSize(m_pimpl->m_width, THIS->m_height);
}

#undef THIS