/***************************************************************************
 *   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.             *
 ***************************************************************************/
#ifndef GENERICTOOL_H
#define GENERICTOOL_H

#include <qobject.h>

/**
@author Hans Oischinger
Must be inherited by all Tools
 
This class is heavily based on Karbon's vtool class
*/
class GenericTool : public QObject
{
  Q_OBJECT
public:
  GenericTool(QObject *parent = 0, const char *name = 0);

  virtual ~GenericTool();

  /**
   * Called during the tool activation. A tool is supposed to set a mouse cursor and/or
   * the statusbar properly here.
   */
  virtual void activate() {}

  /**
   * Deactivates the tool.
   */
  virtual void deactivate() {}

  /**
   * The name of the tool.
   */
  virtual QString name() const;

  /**
   * The context help of the tool.
   */
  virtual QString contextHelp() const;

  /**
   * The tool icon name.
   */
  virtual QString icon() const { return "help"; }

  virtual QString category() const { return "misc"; }

  virtual QString statusText() const { return ""; }

  /**
   * Position in toolbox. Zero means no preferred position, other
   * values indicate the true position.
   */
  virtual uint priority() const { return 0; }

  /**
   * This function processes every important mouse or keyboard event.
   * It then calls suiting functions like mouseMoved() so deriving tools
   * don't need to directly deal with events themselves.
   */
  bool mouseEvent( QMouseEvent* event, const QPoint& );
  bool keyEvent( QEvent* event );

protected:

  virtual void setCursor() const {}

  /**
   * Mouse button press.
   */
  virtual void mouseButtonPress() {}

  /**
   * Mouse button release. The mouse wasn't moved.
   */
  virtual void mouseButtonRelease() {}

  /**
   * Mouse button double click.
   */
  virtual void mouseButtonDblClick() {}

  /**
   * Mouse move. No mouse button is pressed.
   */
  virtual void mouseMove() {}

  /**
   * Mouse drag.
   */
  virtual void mouseDrag() {}

  /**
   * Mouse button release. The mouse was moved before.
   */
  virtual void mouseDragRelease() {}

  /**
   * Mouse drag with "Shift" key pressed at the same time.
   */
  virtual void mouseDragShiftPressed() {}

  /**
   * Mouse drag with "Ctrl" key pressed at the same time.
   */
  virtual void mouseDragCtrlPressed() {}

  /**
   * "Shift" key released while mouse drag.
   */
  virtual void mouseDragShiftReleased() {}

  /**
   * "Ctrl" key released while mouse drag.
   */
  virtual void mouseDragCtrlReleased() {}

  /**
   * "Arrow" key released up, down, left, right
   */
  virtual void arrowKeyReleased( Qt::Key ) {}

  virtual bool keyReleased( Qt::Key ) { return false; }

  /**
   * Cancels all tool operations. This event is invoked when ESC is pressed.
   */
  virtual void cancel() {}

  /**
   * Cancels the last tool step (if any). This event is invoked when Backspace is pressed.
   */
  virtual void cancelStep() {}

  /**
   * Terminates the current tool drawing (if any). This event is invoked when Enter/Return is pressed.
   */
  virtual void accept() {}

  /**
   * Most tools need to know the first mouse coordinate.
   */
  const QPoint& first() const { return m_firstPoint; }

  const QPoint& last() const { return m_lastPoint; }

  bool shiftPressed() const { return m_shiftPressed; }

  bool ctrlPressed() const { return m_ctrlPressed; }

  bool altPressed() const { return m_altPressed; }

private:
  /**
   * First input mouse coordinate.
   */
  QPoint m_firstPoint;

  /**
   * Last input mouse coordinate.
   */
  QPoint m_lastPoint;

  /**
   * A tool state.
   */
  bool m_mouseButtonIsDown;

  /**
   * A tool state.
   */
  bool m_isDragging;

  /**
   * Indicates if shift is pressed.
   */
  bool m_shiftPressed;

  /**
   * Indicates if ctrl is pressed.
   */
  bool m_ctrlPressed;

  /**
   * Indicates if Alt is pressed.
   */
  bool m_altPressed;
};

#endif
