/***************************************************************************
 *   Copyright (C) 2004 by Hans Oischinger                                 *
 *   hans.oischinger@kdemail.net                                           *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/
#include "direktorinstance.h"

#include "timeserver.h"
#include "documentbuilder.h"
#include "mainwindow.h"
#include "elementdatabase.h"

// Elements
#include "elements/documentelement.h"
#include "elements/elementtreeiterator.h"
#include "elements/lengthattribute.h"
#include "elements/animatedlengthattr.h"

// Visitors
#include "visitors/vdrawelements.h"

// Tools
#include "tools/toolmanager.h"

// KDE
#include <kurl.h>
#include <kdebug.h>
#include <kcommand.h>

Kast* Kast::m_instance = 0;

Kast* Kast::self()
{
  if (m_instance == 0)
    m_instance = new Kast();
  return m_instance;
}

Kast::Kast(QObject *parent, const char *name)
    : QObject(parent, name),
    m_mainwin(0),
    m_docWidget(0),
    m_cmdHistory(0),
    m_recordAnimation(0),
    m_guiReady(false),
    m_styleTarget(ST_FILL)
{
  init();
}

Kast::~Kast()
{
  delete m_toolManager;
  delete m_timeServer;
  delete m_elemDB;
}

void Kast::init( )
{
  m_timeServer = new TimeServer();
  m_elemDB = new ElementDatabase();
  m_toolManager = new ToolManager();
}

void Kast::setupGUI( MainWindow *win, KActionCollection *actionCollection )
{
  if (m_mainwin)
  {
    kdError() << k_funcinfo << "[ERROR] Mainwindow already exists" << endl;
  }
  m_mainwin = win;
  m_docWidget = m_mainwin->getDocWidget();
  m_cmdHistory = new KCommandHistory( actionCollection, true );
  m_toolManager->setDefaultTool();
  m_guiReady = true;
}

bool Kast::openURL( const KURL& url )
{
  // Clear cmd History
  m_cmdHistory->clear();
  // forward request to Element DB
  return m_elemDB->openURL(url);
}

void Kast::addCommand( KCommand* command, bool execute )
{
  m_cmdHistory->addCommand( command, execute );
}

void Kast::toggleRecordAnimation( )
{
  m_recordAnimation = !m_recordAnimation;
}

void Kast::setStyleTarget( STYLETARGET t )
{
  m_styleTarget = t;
  emit(styleTargetChanged());
}

#include "direktorinstance.moc"
