#ifndef QGPYCONSOLE_H
#define QGPYCONSOLE_H

#include <QTextEdit>

#include "PythonQt.h"

class QMimeData;

class QCompleter;
class QAction;
class QgPyEditDialog;

class QgPyConsole : public QTextEdit
{
    Q_OBJECT

public:
    QgPyConsole(QWidget* parent=0, const PythonQtObjectPtr* context=0, Qt::WindowFlags i= 0);
    ~QgPyConsole();

    void setContext(const PythonQtObjectPtr* context);

public Q_SLOTS:
  void dropFile(const QMimeData*);

  //! execute current line
  void executeLine(bool storeOnly);
  void executeCode(const QString& code);
  void executeFile(const QString& fname);

  //! derived key press event
  void keyPressEvent (QKeyEvent * e);

  //! output from console
  void consoleMessage(const QString & message);

  //! get history
  QStringList history() { return _history; }

  //! set history
  void setHistory(const QStringList& h) { _history = h; _historyPosition = 0; }

  //! clear the console
  void clear();

  //! overridden to control which characters a user may delete
  virtual void cut();

  //! output redirection
  void stdOut(const QString& s);
  //! output redirection
  void stdErr(const QString& s);

  void insertCompletion(const QString&);

  //! Appends a newline and command prompt at the end of the document.
  void appendCommandPrompt(bool storeOnly = false);

public:
  //! returns true if python cerr had an error
  bool hadError() { return _hadError; }

  //! returns true if python cerr had an error
  void clearError() {
    _hadError = false;
  }

protected:
  //! handle the pressing of tab
  void handleTabCompletion();

  //! Returns the position of the command prompt
  int commandPromptPosition();

  //! Returns if deletion is allowed at the current cursor
  //! (with and without selected text)
  bool verifySelectionBeforeDeletion();

  //! Sets the current font
  void setCurrentFont(const QColor& color = QColor(0,0,0), bool bold = false);

  //! change the history according to _historyPos
  void changeHistory();

  //! flush output that was not yet printed
  void flushStdOut();

  void contextMenuEvent(QContextMenuEvent *e);

private:

  PythonQtObjectPtr _context;

  QStringList _history;
  int         _historyPosition;

  QString _clickedAnchor;
  QString _storageKey;
  QString _commandPrompt;

  QString _currentMultiLineCode;

  QString _stdOut;
  QString _stdErr;

  QTextCharFormat _defaultTextCharacterFormat;
  QCompleter* _completer;
  QAction* actEditor;
  QgPyEditDialog* dlgEdit;

  bool _hadError;

};

#endif // QGPYCONSOLE_H
