/***************************************************************************
                          quantaview.h  -  description
                             -------------------
    begin                : ���� 9 13:29:57 EEST 2000
    copyright            : (C) 2000 by Dmitry Poplavsky & Alexander Yakovlev & Eric Laffoon <pdima@users.sourceforge.net,yshurik@linuxfan.com,sequitur@easystreet.com>
                           (C) 2001-2005 Andras Mantia <amantia@kde.org>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef QUANTAVIEW_H
#define QUANTAVIEW_H

// include files for TQt
#include <tqguardedptr.h>
#include <tqwidget.h>
#include <tqptrlist.h>
#include <tqvaluelist.h>
#include <tqtimer.h>

//kde includes
#include <tdemdichildview.h>

#include <dom/dom_node.h>

class QuantaDoc;
class Document;
class ToolbarTabWidget;
class KafkaDocument;
class QuantaPlugin;

class TQSplitter;
class KURL;
class TQGridLayout;

namespace DOM
{
  class Node;
}

namespace KTextEditor
{
  class Mark;
}
class TQSplitter;
class Node;

/** The QuantaView class provides the view widget for the QuantaApp
 * instance.  The View instance inherits TQWidget as a base class and
 * represents the view object of a KTMainWindow. As QuantaView is part
 * of the docuement-view model, it needs a reference to the document
 * object connected with it by the QuantaApp class to manipulate and
 * display the document structure provided by the QuantaDoc class.
 *
 * @author Source Framework Automatically Generated by KDevelop, (c) The KDevelop Team.
 * @version KDevelop version 0.4 code generation */
class QuantaView : public KMdiChildView
{
  TQ_OBJECT
  

public:

  QuantaView(TQWidget *parent = 0, const char *name=0, const TQString &caption = TQString());
  ~QuantaView();

  /** returns true if the view can be removed, false otherwise */
  bool mayRemove();

  /**Adds a Document object to the view. Also creates the VPL widget for the document. */
  void addDocument(Document *document);

  /** Adds a custom widget to the view. */
  void addCustomWidget(TQWidget *widget, const TQString &label);

  /** returns the Document object associated with this view. Returns 0L if the view holds
  a non-Document object */
  Document *document() {return m_document;};

  /**Adds a QuantaPlugin object to the view.*/
  void addPlugin(QuantaPlugin *plugin);

  TQWidget* documentArea() {return m_documentArea;}

   bool saveDocument(const KURL&);
  /** Saves the document if it's modified. Ask the user if their
   @param ask is true. */
  bool saveModified(bool ask = true);
  /** Returns the baseURL of the document. */
  KURL baseURL();

  /** Tells which widget had the focus the more recently */
  int hadLastFocus() {return m_currentFocus;}
  /** Reloads both views ONLY when changes have been made to the Node tree ONLY.
    * Set force to true if you want to reload even if not necessary.*/
  void reloadBothViews(bool force = false);
  /** reload the Kafka view from the Node Tree. Set force to true if you want to reload even if not necessary. */
  void reloadVPLView(bool force = false);
  /** reload the Quanta view from the Node Tree. Set force to true if you want to reload even if not necessary. */
  void reloadSourceView(bool force = false);
  /** Return the curren views layout*/
  int currentViewsLayout() {return m_currentViewsLayout;}

  void reloadLayout();

 /** Called when this view become the active one */
  void activated();
 /** Called when this view lost the active status */
  void deactivated();

  void resizeEvent(TQResizeEvent* e);
 /** Resize the current view */
  void resize(int width, int height);
  /** Redraws the view, resizes the components to their correct size */
  void refreshWindow();

 /** Updates the icon at the name on the view tab */
  void updateTab();

  /** Returns the tab name associated with this view */
  TQString tabName();

  void insertTag( const char *tag);

  /** Insert a new tag by bringing up the TagDialog. */
  void insertNewTag(const TQString &tag, const TQString &attr = TQString(), bool insertInLine = true);
  /**
    * This function take the output of the TagAction, parse it into Nodes and insert it
    * in the Node tree. Then kafka will take care of updating itself from the Node Tree.
    */
  void insertOutputInTheNodeTree(const TQString &str1, const TQString &str2 = TQString(), Node *node = 0L);

  enum ViewFocus {
    SourceFocus = 0,
    VPLFocus
  };

  enum ViewLayout {
    SourceOnly = 0,
    SourceAndVPL,
    VPLOnly
  };


public slots:
  void slotSetSourceLayout();
  void slotSetSourceAndVPLLayout();
  void slotSetVPLOnlyLayout();

  /**
   * Called whenever the KafkaWidget widget get/lost the focus.
   */
  void slotVPLGetFocus(bool focus);

  /**
   * Called whenever the KTextEditor::View widget get the focus.
   */
  void slotSourceGetFocus();

  /**
   * Called when we want to set the Quanta cursor.
   * Record the position until Quanta get the focus again.
   * Useful when we want to set it when quanta doesn't have the focus.
   */
  void slotSetCursorPositionInSource(int col, int line);
 /**
   * Called when an error occured when loading kafka.
   * Pop up the Error reporter dialog.
   */
  void slotVPLLoadingError(Node *node);

  /** Restarts the update timers according to the current settings */
  void reloadUpdateTimers();

private slots:
  void slotSavingCompleted();
  void slotSavingFailed(const TQString& error);

  /**
   * Called to update VPL.
   */
  void VPLUpdateTimerTimeout();

  /**
   * Called to update the source.
   */
  void sourceUpdateTimerTimeout();

signals:
  /** emitted when a file from the template view is dropped on the view */
  void dragInsert(TQDropEvent *);
  /** asks for hiding the preview widget */
  void hidePreview();
  void showProblemsView();
  void cursorPositionChanged();
  void title(const TQString &);
  /** emitted if this view contained an editor and it is closed */
  void documentClosed(const KURL&);

  void eventHappened(const TQString&, const TQString&, const TQString& );

private:
/** Kafka stuff */
  TQValueList<int> m_splitterSizes;
  int m_curCol, m_curLine, m_curOffset;
  DOM::Node curNode;
  bool m_kafkaReloadingEnabled, m_quantaReloadingEnabled;
  TQTimer m_sourceUpdateTimer, m_VPLUpdateTimer;

  TQWidget *m_documentArea;///< the area of the view which can be used to show the source/VPL
  Document *m_document;
  QuantaPlugin *m_plugin;
  TQWidget *m_customWidget; ///<view holds a custom widget, eg. a documentation
  TQGuardedPtr<KafkaDocument> m_kafkaDocument;
  TQSplitter *m_splitter;
  TQGridLayout *m_viewLayout;
  int m_currentViewsLayout; ///< holds the current layout, which can be SourceOnly, VPLOnly or SourceAndVPL
  int m_currentFocus;
  bool m_saveResult;
  bool m_eventLoopStarted;

protected:
  virtual void dropEvent(TQDropEvent *e);
  virtual void dragEnterEvent(TQDragEnterEvent *e);
};

#endif // QUANTAVIEW_H
