Source Code (Use browser search to find items of interest.)

Class Index

killustrator'FreeHandTool (./koffice/killustrator/share/FreeHandTool.h:35)

class FreeHandTool : public Tool {
  Q_OBJECT
public:
  FreeHandTool (CommandHistory* history);

  virtual void processEvent (QEvent* e, GDocument* doc, Canvas* canvas);
  virtual void activate (GDocument* doc, Canvas* canvas);
  virtual void deactivate (GDocument*, Canvas*);
  
private:
  GPolyline* line;
  int last;
  bool newObj;
  bool buttonIsDown;
  QList<Coord> points;
};

killustrator'FreeHandTool::FreeHandTool() (./koffice/killustrator/share/FreeHandTool.cc:41)

FreeHandTool::FreeHandTool (CommandHistory* history) : Tool (history) {
  line = 0L;
  last = 0;
  newObj = true;
  buttonIsDown = false;
  points.setAutoDelete (true);
}


killustrator'FreeHandTool::processEvent() (./koffice/killustrator/share/FreeHandTool.cc:49)

void FreeHandTool::processEvent (QEvent* e, GDocument *doc, Canvas* canvas) {
  if (e->type () == 
#if QT_VERSION >= 199
      QEvent::MouseButtonPress
#else
      Event_MouseButtonPress
#endif
      ) {
    QMouseEvent *me = (QMouseEvent *) e;
    if (me->button () != LeftButton)
      return;

    buttonIsDown = true;

    float xpos = me->x (), ypos = me->y ();
    canvas->snapPositionToGrid (xpos, ypos);

    if (line != 0L) {
      // continue creation: add a new segment
      if (last != 0)
	last++;
    }
    else {
      newObj = true;
      
      QList<GObject> olist;
    // look for existing polylines with a point near the mouse pointer
      if (doc->findContainingObjects (xpos, ypos, olist)) {
	QListIterator<GObject> it (olist);
	while (it.current ()) {
	  if (it.current ()->isA ("GPolyline")) {
	    GPolyline* obj = (GPolyline *) it.current ();
	    if ((last = 
		 obj->getNeighbourPoint (Coord (xpos, ypos))) != -1
		&& (last == 0 || last == (int) obj->numOfPoints () - 1)) {
	      line = obj; 
	      newObj = false;
	      if (last != 0)
		// it's not the first point of the line, so update the
		// index
		last += 1;
	      points.clear ();
	      break;
	    } 
	  }
	  ++it;
	}
      }
      if (line == 0L) {
	// no polyline found, create a new one
	line = new GPolyline ();
	line->addPoint (0, Coord (xpos, ypos));
	last = 1;
	newObj = true;
	doc->insertObject (line);
      }
    }
//    line->addPoint (last, Coord (xpos, ypos));
  }
  else if (e->type () == 
#if QT_VERSION >= 199
	   QEvent::MouseMove
#else
	   Event_MouseMove
#endif
	   ) {
    if (line == 0L || !buttonIsDown)
      return;
    QMouseEvent *me = (QMouseEvent *) e;
    float xpos = me->x (), ypos = me->y ();
    canvas->snapPositionToGrid (xpos, ypos);

    Coord np (xpos, ypos);
    Coord oldp = line->getPoint (last > 0 ? last - 1 : 0);
    if (np != oldp) {
      line->addPoint (last, np);
      if (last != 0)
        last++;
      if (! newObj)
        points.append (new Coord (xpos, ypos));
    }
  }
  else if (e->type () == 
#if QT_VERSION >= 199
	   QEvent::MouseButtonRelease
#else
	   Event_MouseButtonRelease
#endif
	   ) {
    buttonIsDown = false;
    if (line == 0L)
      return;
    QMouseEvent *me = (QMouseEvent *) e;
    float xpos = me->x (), ypos = me->y ();
    canvas->snapPositionToGrid (xpos, ypos);

    Coord np (xpos, ypos);
    Coord oldp = line->getPoint (last > 0 ? last - 1 : 0);
    if (np != oldp) {
      line->addPoint (last, np);
      if (last != 0)
        last++;
      if (! newObj)
        points.append (new Coord (xpos, ypos));
    }

    doc->unselectAllObjects ();

    if (last > 0 && line->numOfPoints () >= 3 && 
	  line->getNeighbourPoint (Coord (xpos, ypos)) == 0) {
	// the polyline is closed, so convert it into a polygon
	GPolygon* obj = new GPolygon (line->getPoints ());
	doc->deleteObject (line);
	doc->insertObject (obj);
	doc->setLastObject (obj);
	CreatePolygonCmd *cmd = new CreatePolygonCmd (doc, obj);
	history->addCommand (cmd);
    }
    else {
	doc->setLastObject (line);
	if (newObj) {
	  CreatePolylineCmd *cmd = new CreatePolylineCmd (doc, line);
	  history->addCommand (cmd);
	}
	else {
	  // remove the first point: it's equal to the last point
	  // of the original line
	  points.removeFirst ();
	  if (last != 0)
	    last = last - points.count () + 1;
	  AddLineSegmentCmd *cmd =  
	    new AddLineSegmentCmd (doc, line, last, points);
	  history->addCommand (cmd);
	}
      }
      line = 0L; last = 0;
    }
  else if (e->type () == 
#if QT_VERSION >= 199
	   QEvent::KeyPress
#else
	   Event_KeyPress
#endif
	   ) {
    QKeyEvent *ke = (QKeyEvent *) e;
    if (ke->key () == QT_ESCAPE)
      emit operationDone ();
  }
  return;
}


killustrator'FreeHandTool::activate() (./koffice/killustrator/share/FreeHandTool.cc:200)

void FreeHandTool::activate (GDocument* /*doc*/, Canvas*) {
  buttonIsDown = false;
  emit modeSelected (i18n ("Create FreeHand Line"));
}


killustrator'FreeHandTool::deactivate() (./koffice/killustrator/share/FreeHandTool.cc:205)

void FreeHandTool::deactivate (GDocument*, Canvas*) {
  line = 0L;
  last = 0;
  buttonIsDown = false;
}