/* view.cc */

#include "view.h"
#include "prop.h" // ListField

#include "QtGui"

#if QT_VERSION >= 0x040500
   #define QT_4_5
#endif

MyView::MyView (QWidget* parent)
  : QGraphicsView (parent)
{
  scene = new QGraphicsScene;
  scene->setSceneRect (0, 0, 800, 600);
  this->setScene (scene);

  // scene->addText ("Hello, world!");
  // scene->addRect (QRectF (1, 1, 100, 80), QPen (Qt::red), QBrush (Qt::lightGray));

  /* background */

  const int n = 16;
  QBitmap texture (n, n);
  texture.clear ();

  QPainter painter (& texture);
  painter.drawLine (0, 0, n-1, 0);
  painter.drawLine (0, 0, 0, n-1);

  QBrush brush (QColor ("cornflowerblue"));
  brush.setTexture (texture);
  scene->setBackgroundBrush (brush);

  /* plane */

  plane = new Area;
  plane->initializeTopLevel ();
  plane->setRect (10, 10, 630, 470);
  scene->addItem (plane);
}

void MyView::addArea (Area * a)
{
   plane->addArea (a);
   #ifndef QT_4_5
      scene->addItem (a);
   #endif
}

/* ---------------------------------------------------------------------- */

void MyView::example ()
{
    Area * a = new Area;
    a->setPen (QPen (Qt::green));
    a->setBrush (Qt::yellow);
    a->setPos (10, 10);
    a->setTitle ("Area 1");
    a->setToolTip ("first area");

    ListField * f = new ListField;
    f->setName ("size");
    f->setValue ("small");
    f->list << "small" << "medium" << "large";
    a->addField (f);

    Field * f2 = new Field;
    f2->setName ("color");
    f2->setValue ("blue");
    a->addField (f2);

    Source * a_source = new Source;
    a_source->setPos (90, 0);
    a->addSource (a_source);

    Target * a_target = new Target;
    a->addTarget (a_target);

    Area * b = new Area;
    b->setPen (QPen (Qt::red));
    b->setBrush (QColor ("cornflowerblue"));
    b->setTitle ("Area 2");
    b->setToolTip ("second area");
    b->setPos (320, 10);

    Target * b_target = new Target;
    b->addTarget (b_target);

    Source * b_source = new Source;
    b_source->setPos (90, 0);
    b->addSource (b_source);

    Connection * c = new Connection;
    c->setSource (a_source);
    c->setTarget (b_target);

    #if 0
    Connection * c2 = new Connection;
    c2->setSource (b_source);
    c2->setTarget (a_target);
    #endif

    Area * s = new Area;
    s->setTitle ("orange");
    s->setPen (QPen (Qt::red));
    s->setBrush (QColor ("orange"));
    s->setToolTip ("Subitem");
    s->setRect (10, 10, 80, 20);
    a->addArea (s);

    Area * t = new Area;
    t->setTitle ("orange 2");
    t->setPen (QPen (Qt::red));
    t->setBrush (QColor ("orange"));
    t->setToolTip ("Subitem 2");
    t->setRect (10, 10, 80, 20);
    b->addArea (t);

    #if 0
    Area * u = new Area;
    u->setTitle ("lime");
    u->setPen (QPen (Qt::red));
    u->setBrush (QColor ("lime"));
    u->setToolTip ("Subitem 3");
    u->setRect (10, 40, 80, 20);
    b->addArea (u);

    Area * v = new Area;
    v->setTitle ("green");
    v->setPen (QPen (Qt::red));
    v->setBrush (QColor ("green"));
    v->setToolTip ("Subitem 3");
    v->setRect (10, 70, 80, 20);
    b->addArea (v);
    #endif

    this->addArea (a);
    this->addArea (b);
    this->centerOn (a);
}

/* ---------------------------------------------------------------------- */