/***************************************************************************
 *   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 "canvaswidget.h"

// Core
#include "core/timeserver.h"
#include "core/direktorinstance.h"
#include "core/elementdatabase.h"

// Elements
#include "elements/documentelement.h"
#include "elements/vectorelement.h"
#include "elements/animateelement.h"
#include "elements/elementtreeiterator.h"
#include "elements/vectorpath.h"
#include "elements/animatedlengthattr.h"
#include "elements/lengthattribute.h"
#include "elements/elementselection.h"

// Qt
#include <qpixmap.h>

// KDE
#include <kdebug.h>
#include <kcursor.h>

#include <kcanvas/KCanvas.h>
#include <kcanvas/device/KRenderingDevice.h>
#include <kcanvas/device/KRenderingDeviceFactory.h>
#include <kcanvas/device/KRenderingPaintServerSolid.h>
#include <kcanvas/KCanvasMatrix.h>
#include <kcanvas/targets/qt/KCanvasTargetQt.h>

#define ZOOM_FACTOR 1.2

CanvasWidget::CanvasWidget(QWidget *parent, const char *name)
    : QWidget(parent, name, WNoAutoErase|WStaticContents)
{
  setBackgroundMode(Qt::NoBackground);

  m_canvas = 0;
  initKCanvas();
}

CanvasWidget::~CanvasWidget()
{
  delete m_canvas;
}

KCanvas *CanvasWidget::getCanvas() const
{
  return m_canvas;
}

void CanvasWidget::paintEvent(QPaintEvent *event)
{
  m_canvas->draw(event->rect());
}

void CanvasWidget::resizeEvent(QResizeEvent *event)
{
  // Our widget size has already changed, resize & update the canvas.
  resizeView(event->size().width(), event->size().height());
}

void CanvasWidget::reset()
{
  kdDebug() << k_funcinfo << endl;
  m_canvas->reset();
  // FIXME: Reset Zoom and pan here?
}

void CanvasWidget::initKCanvas()
{
  KCanvasTargetQt *targetQt = new KCanvasTargetQt(width(), height());

  // Request a rendering device...
  QString deviceLib("agg");

  KRenderingDevice *device = KRenderingDeviceFactory::self()->request(deviceLib, targetQt);
  if(!device)
    return;

  m_canvas = new KCanvas(targetQt, device);

  init(m_canvas, targetQt);

  targetQt->init(this, device->isBuffered());
  device->init(targetQt);
  m_canvas->setBackgroundColor(QColor(qRgba(255, 255, 255, 255)));
}

KRenderingDevice * CanvasWidget::getDevice()
{
  return m_canvas->renderingDevice();
}

#include "canvaswidget.moc"
