//  qpui.h
//
//  Qsas - Plot Widgets Interface File
//
//  Janet Barnes
//  July 2002
//
//  Tony Allen
//  Dec 2007

#ifndef QSAS_PLOT_UI_H_
#define QSAS_PLOT_UI_H_

#include <qsplitter.h>
#include <qstatusbar.h>
#include <qspinbox.h>
#include <qtabwidget.h>
#include <qtabbar.h>
#include <qlayout.h>

#include "qt_version.h"
#include "qslot.h"
#include "QbTimeEditors.h"
#include "IntervalDialog.h"

#if QT_VERSION < 0x040000
#include "qgu.h"
#include "floatspinbox.h"
#include "QbTimeWidgets.h"
#include <qpopupmenu.h>
#else
#include "qgu_qt4.h"
#include "floatspinbox_qt4.h"
#include "QbTimeWidgets_qt4.h"
#endif

#include "PlotWindow.h"

#define QPUI_VERSION  " V_3.0.0 [ Jan 2008 ]"

#define QPUI_CHAR_HEIGHT 130 //0: Default plplot height.

#define QPUI_PAGE_HGAP 0.05
#define QPUI_PAGE_VGAP 0.1
#define QPUI_PAGE_LMARGIN 0.1 //0.2
#define QPUI_PAGE_RMARGIN 0.1
#define QPUI_PAGE_TMARGIN 0.05 //0.03
#define QPUI_PAGE_BMARGIN 0.08 //0.05

#define QPUI_PANEL_LOFFSET 0.2
#define QPUI_PANEL_WIDTH 0.7
#define QPUI_PANEL_TOFFSET 0.05
#define QPUI_PANEL_HEIGHT 0.90

#define QPUI_LINE_WIDTH 1
#define QPUI_TICKS_LENGTH 0.7

#define QPUI_TITLE_CHAR_SIZE_ADD 0.3 //1.0
#define QPUI_TITLE_DISP 1.0 //0.7
#define QPUI_SUBTITLE_CHAR_SIZE_ADD 0.1
#define QPUI_SUBTITLE_DISP 2.0 //1.5
#define QPUI_LABELS_CHAR_SIZE_ADD 0.0
#define QPUI_LABELS_DISP 3.0 //2.0
#define QPUI_SYMBOL_SIZE 1.0
#define QPUI_LEGEND_CHAR_SIZE_ADD 0.0
#define QPUI_LEGEND_BOX_LINE_WIDTH 1
#define QPUI_LEGEND_LINE_LENGTH 0.2
#define QPUI_LEGEND_MAX_CHAR_LENGTH 25
#define QPUI_LEGEND_JUST 0.5
#define QPUI_LEGEND_HEIGHT 0.4
#define QPUI_LEGEND_WIDTH 0.25

#define QPUI_COLOUR_BAR_DISP 1.5
#define QPUI_COLOUR_BAR_WIDTH 2.5
#define QPUI_COLOUR_BAR_HEIGHT 1.0
#define QPUI_COLOUR_BAR_VOFFSET 0.0

#define QPUI_SKIP_LOG_VALUE -1.e31

#define QPUI_LOWEST_COLOUR_INDEX 16
#define QPUI_HIGHEST_COLOUR_INDEX 255
#define QPUI_NCOLOURS_MAX 256

#define QPUI_SPECTROGRAM_LOFFSET 0.1
#define QPUI_SPECTROGRAM_WIDTH 0.7
#define QPUI_SPECTROGRAM_TOFFSET 0.1
#define QPUI_SPECTROGRAM_HEIGHT 0.2

#define QP_LINEAR 0
#define QP_LOG 1

#define QP_FROM_META 0
#define QP_FROM_DATA 1
#define QP_FROM_USER 2

#define NAME_COL 0
#define LINK_COL 1


typedef vector <double> dblBins;

typedef enum QpItemType_e
{
  QP_TRACE=0,
  QP_SPECTROGRAM=1,
  QP_CONST_XT=2,
  QP_CONST_Y=3,
  QP_EVENT=4,
  QP_PANEL=5,
  QP_FRAME=6,
  QP_PAGE=7,
  QP_NONE=8
} QpItemType;

typedef enum QpFrameType_e
{
  QP_TS=0,
  QP_XY=1
  
} QpFrameType;

typedef enum QpTabType_e
{
  QP_DATA_TAB=0,
  QP_TRACE_TAB=1,
  QP_PLOT_TYPE_TAB=2,
  QP_VALUE_TAB=3,
  QP_COLOUR_TAB=4,
  QP_Y_TAB=5,
  QP_LEGEND_TAB=6,
  QP_XT_TAB=7,
  QP_POSITION_TAB=8,
  QP_FRAME_TAB=9,
  QP_TITLE_TAB=10,
  QP_PAGE_TAB=11,
  QP_LAYOUT_TAB=12,
  QP_EVENT_TAB=13,
  QP_IGNORE_TAB=14
  
} QpTabType;

typedef enum QpuiPageSize_e
{
  QPUI_A4=0,
  QPUI_CUSTOM=1,
  QPUI_EXECUTIVE=2,
  QPUI_LEGAL=3,
  QPUI_LETTER=4,
  QPUI_DEFAULT=5
} QpuiPageSize;

typedef enum QpuiPageDPI_e
{
  QPUI_72_DPI=0,
  QPUI_150_DPI=1,
  QPUI_300_DPI=2,
  QPUI_600_DPI=3
} QpuiPageDPI;

typedef enum QpuiUnits_e
{
  QPUI_PIXELS=0,
  QPUI_INCHES=1,
  QPUI_CM=2,
  QPUI_MM=3,
  QPUI_PSPOINTS=4
} QpuiUnits;

typedef enum QpuiReturnStatus_e
{
  QPUI_SUCCESS=0,
  QPUI_FAILURE=1
} QpuiReturnStatus;

typedef enum QpuiLinkStatus_e
{
  QPUI_UNLINKED=0,
  QPUI_LINKED=1
} QpuiLinkStatus;

typedef enum QpuiUpdateStatus_e
{
  QPUI_NO_UPDATES=0,
  QPUI_UPDATED=1,
  QPUI_INCOMPATIBLE=2
} QpuiUpdateStatus;

// typedef enum QpuiPlotDevice_e
// {
//   QPUI_XWINDOW=0,
//   QPUI_GIF=1,
//   QPUI_PS=2,
//   QPUI_NODEVICE=3
// } QpuiPlotDevice;

typedef enum QpuiPageOrientation_e
{
  QPUI_PORTRAIT=0,
  QPUI_LANDSCAPE
} QpuiPageOrientation;

typedef enum QpuiTickMarks_e
{
  QPUI_NOTICKS=0,
  QPUI_IN=1,
  QPUI_OUT=2,
  QPUI_GRID=3
} QpuiTickMarks;

typedef enum QpuiTickLabels_e
{
  QPUI_AUTOMATIC=0,
  QPUI_DECIMAL=1,
  QPUI_EXPONENTIAL=2,
  QPUI_TIME=3
} QpuiTickLabels;


typedef enum QpLineStyle_e
{
	
	QP_NOLINE=0,
	QP_SOLID=1,
	QP_DASHED=2,
	QP_DOTDASH=3,
	QP_DOTTED=4,
	QP_DASHDOTDOTDOT=5
} QpLineStyle;

typedef enum QpuiColours_e
{
  QPUI_WHITE=0,
  QPUI_BLACK=1,
  QPUI_RED=2,
  QPUI_GREEN=3,
  QPUI_BLUE=4,
  QPUI_CYAN=5,
  QPUI_MAGENTA=6,
  QPUI_YELLOW=7,
  QPUI_ORANGE=8,
  QPUI_YELLOWGREEN=9
} QpuiColours;

typedef enum QpuiColourTable_e
{
  QPUI_RAINBOW=0,
  QPUI_RAINBOW_EXT=1,
  QPUI_GRAY_WHITE_LOW=2,
  QPUI_GRAY_WHITE_HIGH=3,
  QPUI_RED_BLUE=4,
  QPUI_INV_RED_BLUE=5,
  QPUI_EARTH=6,
  QPUI_USER_TABLE=7
} QpuiColourTable;

typedef enum QpuiSymbolShape_e
{
  QPUI_NOSYMBOL=0,
  QPUI_POINT=1,
  QPUI_PLUS=2,
  QPUI_ASTERISK=3,
  QPUI_CIRCLE=4,
  QPUI_CROSS=5,
  QPUI_SQUARE=6,
  QPUI_TRIANGLE=7,
  QPUI_CIRCLED_CROSS=8,
  QPUI_CIRCLED_POINT=9,
  QPUI_LENS_FLARE=10,
  QPUI_DIAMOND=11,
  QPUI_STAR=12,
  //QPUI_FILLEDTRIANGLE=13, // Not in plplot: square
  QPUI_WIDECROSS=14,
  //QPUI_FILLEDASTERISK=15, // Not in plplot: david star
  QPUI_FILLEDSQUARE=16,
  QPUI_FILLEDCIRCLE=17,
  QPUI_FILLEDSTAR=18
} QpuiSymbolShape;

typedef enum QpuiConnectStyle_e
{
  QPUI_STRAIGHT=0,
  QPUI_HIST=1,
  QPUI_NOCONNECT=2
} QpuiConnectStyle;

typedef enum QpuiObjectType_e
{
  QPUI_DATA_SERIES=0,
  QPUI_TIME_SERIES=1,
  QPUI_TIME_CONSTANT=2,
  QPUI_SCALAR_Y_CONSTANT=3,
  QPUI_SCALAR_X_CONSTANT=4
} QpuiObjectType;

typedef enum QpuiJustification_e
{
  QPUI_HCENTRE=0,
  QPUI_LEFT=1,
  QPUI_RIGHT=2
} QpuiJustification;

typedef enum QpuiLegendJust_e
{
  QPUI_VCENTRE=0,
  QPUI_TOP=1,
  QPUI_BOTTOM=2
} QpuiLegendJust;

typedef enum QpuiCharFont_e
{
  QPUI_NORMAL=0,
  QPUI_ROMAN=1,
  QPUI_ITALIC=2,
  QPUI_SCRIPT=3
} QpuiCharFont;

typedef enum QpuiVectorComp_e
{
  QPUI_VECTOR_X=0,
  QPUI_VECTOR_Y=1,
  QPUI_VECTOR_Z=2,
  QPUI_VECTOR_RMAG=3,
  QPUI_VECTOR_LAT_DEG=4,
  QPUI_VECTOR_THETA_DEG=5,
  QPUI_VECTOR_PHI_DEG=6,
  QPUI_VECTOR_LAT_RAD=7,
  QPUI_VECTOR_THETA_RAD=8,
  QPUI_VECTOR_PHI_RAD=9
} QpuiVectorComp;

typedef enum QpuiTimeIntervalComp_e
{
  QPUI_START_TIME=0,
  QPUI_END_TIME=1,
  QPUI_CENTRE_TIME=2,
  QPUI_START_AND_END_TIME=3
} QpuiTimeIntervalComp;

typedef enum QpuiLegendAlign_e
{
  QPUI_INSIDE_TOP_RIGHT=0,
  QPUI_INSIDE_TOP_LEFT=1,
  QPUI_INSIDE_BOTTOM_RIGHT=2,
  QPUI_INSIDE_BOTTOM_LEFT=3,
  QPUI_ABOVE_TOP_CENTRE=4,
  QPUI_OUTSIDE_TOP_RIGHT=5,
  QPUI_USER_DEFINED=6
} QpuiLegendAlign;

typedef enum QpuiLegendBorder_e
{
  QPUI_NO_BORDER=0,
  QPUI_LINE=1
} QpuiLegendBorder;

typedef enum QpuiLegendBackground_e
{
  QPUI_TRANSPARENT=0,
  QPUI_OPAQUE=1
} QpuiLegendBackground;

typedef enum QpuiPageFlds_e
{
  PAGE_SIZE=0,
  PAGE_UNITS=1,
  PAGE_WIDTH=2,
  PAGE_HEIGHT=3,
  PAGE_ORIENTATION=4,
  XWIN_NUM=5,
  IDENT=6,
  NUM_ROWS=7,
  NUM_COLS=8,
  HGAP=9,
  VGAP=10,
  LMARGIN=11,
  RMARGIN=12,
  TMARGIN=13,
  BMARGIN=14,
  AUTOLAYOUT=15,
  PAGE_DPI=16
} QpuiPageFlds;

const int QpuiPageNumFlds=17;

typedef enum QPanelFlds_e
{
	LOFFSET=0,
	WIDTH=1,
	TOFFSET=2,
	HEIGHT=3,
	ASPECT_RATIO=4,
	PANEL_LINE_WIDTH=5,
	TICKS_LENGTH=6,
	PANEL_LINE_COLOUR=7,
	TOP_AXIS=8,
	BOTTOM_AXIS=9,
	LEFT_AXIS=10,
	RIGHT_AXIS=11,
	TOP_TICKS_LABELS=12,
	BOTTOM_TICKS_LABELS=13,
	LEFT_TICKS_LABELS=14,
	RIGHT_TICKS_LABELS=15,
	TOP_LABELS=16,
	BOTTOM_LABELS=17,
	LEFT_LABELS=18,
	RIGHT_LABELS=19,
	X_IS_ZERO=20,
	Y_IS_ZERO=21,
	AUTOTITLE=22,
	TITLE=23,
	TITLE_CHAR_SIZE=24,
	TITLE_DISP=25,
	SUBTITLE=26,
	SUBTITLE_CHAR_SIZE=27,
	SUBTITLE_DISP=28,
	TITLE_JUST=29,
	TITLE_FONT_FAMILY=30,
	TITLE_COLOUR=31,
	LABELS_CHAR_SIZE=32,
	LABELS_DISP=33,
	LABELS_FONT=34,
	LABELS_COLOUR=35,
	LABELS_ROTATION=36,
	FRAME_TYPE=37,
	TICKS_CHAR_SIZE=38,
	LR_ALTERNATING_AXIS=39,
	LR_ALTERNATING_TICKS_LABELS=40,
	LR_ALTERNATING_LABELS=41,
	RL_ALTERNATING_AXIS=42,
	RL_ALTERNATING_TICKS_LABELS=43,
	RL_ALTERNATING_LABELS=44,
 	TITLE_FONT_STYLE=45,
	TITLE_FONT_WEIGHT=46
} QpPanelFlds;

const int QpuiPanelNumFlds=47;

typedef enum QpAxes_e
{
  X_AXIS=0,
  Y_AXIS=1,
  T_AXIS=2,
  Z_AXIS=3,
  COL_AXIS=4
} QpAxes;

typedef enum QpuiAxisFlds_e
{
   AXIS_AUTOSCALE=0,
   AXIS_MIN=1,
   AXIS_MAX=2,
   AXIS_T_DATA=3,
   AXIS_LINLOG=4,
   AXIS_NUM_MAJOR_TICKS=5,
   AXIS_NUM_MINOR_TICKS=6,
   AXIS_MAJOR_TICKMARKS=7,
   AXIS_MINOR_TICKMARKS=8,
   AXIS_LABEL=9,
   AXIS_AUTOLABEL=10,
   AXIS_LABEL_CHAR_SIZE=11,
   AXIS_LABEL_DISP=13,
   AXIS_LABEL_ROTATION=14,
   AXIS_LABEL_FONT_FAMILY=15,
   AXIS_LABEL_COLOUR=16,
   AXIS_TICK_TIME_FORMAT=17,
   AXIS_TICK_TIME_FORMAT_AUTO=18,
   AXIS_TIME_LABELS_FORMAT=19,
   AXIS_TIME_LABELS_FORMAT_AUTO=20,
   AXIS_LABEL_FONT_STYLE=21,
   AXIS_LABEL_FONT_WEIGHT=22

} QpuiAxisFlds;

const int QpuiAxisNumFlds=23;

typedef enum QpuiLegendFlds_e
{
   SHOW_LEGEND=0,
   LEGEND_BORDER=1,
   LEGEND_NUM_ROWS=4,
   LEGEND_NUM_COLS=5,
   LEGEND_CHAR_SIZE=6,
   LEGEND_MAX_CHAR_LENGTH=7,
   LEGEND_LINE_LENGTH=8,
   LEGEND_JUST=9,
   LEGEND_FONT_FAMILY=10,
   LEGEND_ALIGN=11,
   LEGEND_L_OFFSET=12,
   LEGEND_T_OFFSET=13,
   LEGEND_BACKGROUND=14,
   LEGEND_WIDTH=15,
   LEGEND_HEIGHT=16,
   LEGEND_FONT_STYLE=17,
   LEGEND_FONT_WEIGHT=18
} QpuiLegendFlds;

const int QpuiLegendNumFlds=19;

typedef enum QpuiEventFlds_e
{
   EVENT_LABEL=0,
   EVENT_LABEL_CHAR_SIZE=1,
   EVENT_LABEL_FONT_FAMILY=2,
   EVENT_LABEL_COLOUR=3,
   EVENT_X_DATA=4,
   EVENT_Y_DATA=5,
   EVENT_LABEL_FONT_STYLE=6,
   EVENT_LABEL_FONT_WEIGHT=7
} QpuiEventFlds;

const int QpuiEventNumFlds=8;


typedef enum QpuiTraceFlds_e
{
   TRACE_PLOT_TYPE=0,
   TRACE_YDATA=1,
   TRACE_XDATA=2,
   TRACE_YCOMP=3,
   TRACE_XCOMP=4,
   TRACE_CONNECT_STYLE=5,
   TRACE_LINE_STYLE=6,
   TRACE_LINE_WIDTH=7,
   TRACE_LINE_COLOUR=8,
   TRACE_SYMBOL_SHAPE=9,
   TRACE_SYMBOL_SIZE=10,
   TRACE_SYMBOL_COLOUR=11,
   TRACE_CONSTANT_DATA=12,
   TRACE_TCOMP=13,
   TRACE_LEGEND_TEXT=14,
   TRACE_SHOW_LEGEND=15,
   LEGEND_AUTO_TEXT=16
} QpuiTraceFlds;

typedef enum QpuiDataFlds_e
{
   USE_TIME_AS_SEC=0
} QpuiDataFlds;

const int QpuiTraceNumFlds=16;

typedef enum QpuiSpectroGramFlds_e
{
   SPECTROGRAM_PLOT_TYPE=0,
   SPECTROGRAM_ZDATA=1,
   COLOUR_SCALE_AUTOSCALE=2,
   COLOUR_SCALE_MIN=3,
   COLOUR_SCALE_MAX=4,
   COLOUR_SCALE_LINLOG=5,
   COLOUR_SCALE_SHOW_COLOUR_BAR=6,
   COLOUR_SCALE_COLOUR_BAR_WIDTH=7,
   COLOUR_SCALE_TABLE=8,
   COLOUR_SCALE_COLOUR_BAR_DISP=9,
   COLOUR_SCALE_LABEL=10,
   COLOUR_SCALE_AUTOLABEL=11,
   SPECTROGRAM_YDATA_START=12,
   SPECTROGRAM_XDATA_START=13,
   SPECTROGRAM_XCOMP_START=14,
   SPECTROGRAM_YDATA_END=15,
   SPECTROGRAM_XDATA_END=16,
   SPECTROGRAM_XCOMP_END=17,
   SPECTROGRAM_YCOMP_START=18,
   SPECTROGRAM_YCOMP_END=19,
   SPECTROGRAM_ZCOMP=20,
   COLOUR_SCALE_COLOUR_BAR_HEIGHT=21,
   COLOUR_SCALE_COLOUR_BAR_VOFFSET=22,
   COLOUR_SCALE_TICK_LENGTH=23
} QpuiSpectroGramFlds;

const int QpuiSpectroGramNumFlds=24;

class FontButton: public QPushButton
{
	Q_OBJECT
	
	public:
		FontButton(QWidget* parent=NULL, int family=PL_FCI_SANS, bool italic=false, bool bold=false);
		
		~FontButton();
		
		void getFontParams(PLINT& family, PLINT& style, PLINT& weight);
	
	signals:
		void familyChanged(int);
		void styleChanged(bool);
		void weightChanged(bool);
		
	public slots:
		
		void setSansFont();
		void setSerifFont();
		void setMonotypeFont();
		void setDecorativeFont();
		void setItalic(bool i);
		void checkItalic();
		void setBold(bool b);
		void checkBold();
		
	protected:
		
		void update();
		
		int family;
		bool italic;
		bool bold;
		
#if QT_VERSION >= 0x040000
		QMenu* menu;
		QAction* sansAction;
		QAction* serifAction;
		QAction* monotypeAction;
		QAction* decorativeAction;
		QAction* italicAction;
		QAction* boldAction;
#else
		QPopupMenu* menu;
		int sansId;
		int serifId;
		int monotypeId;
		int decorativeId;
		int italicId;
		int boldId;
		void mouseReleaseEvent(QMouseEvent* e);
#endif
};

// QPUI version 3 classes - AJA Jan 2008

class QpListItem;
class QpListView;
class QpWindow;
class QpPageItem;
class QpFrameItem;
class QpPanelItem;
class QpPlotItem;
class QpTraceItem;
class QpVectorItem;
class QpSpectrumItem;
class QpEventItem;

// QpTab top level class
// ----------------------
class QpTab:  public QWidget
{
	Q_OBJECT

public: 
	QpTab(QTabWidget *tabWidget,  QString name, QpTabType _tabType);
	~QpTab(){;}
	void setSlotColour(bool active);

	QPalette GUIpalette;
	QpTabType TabType;
	
 };

// QpValueReturnTab Class
// ----------------------

class QpValueReturnTab:  public QpTab
{
	Q_OBJECT

public: 
	QpValueReturnTab(QTabWidget *tabWidget,  QString name, QpPageItem * page);

	~QpValueReturnTab(){ killTimer(_timer);}

	QpPageItem*  _page_item;
	QpFrameItem* _frame_item;
	QpPanelItem* _panel_item;

	QPushButton *_rePlot;
	QPushButton *_saveXT;
	QPushButton *_saveY;
	QPushButton *_reset;

	QguWLinput *_xaxis_from_fld;
	QguWLinput *_xaxis_to_fld;
	QbDateTimeWidget * _taxis_from_fld;
	QbDateTimeWidget * _taxis_to_fld;
	QCheckBox *_xaxis_rescale_fld;
	QCheckBox *_xaxis_swap_fld;
	QLabel *_xaxis_save_fld;
	QguWLinput *_xaxis_name_fld;
	QCheckBox *_xaxis_overwrite_fld;

	QguWLinput *_yaxis_from_fld;
	QguWLinput *_yaxis_to_fld;
	QCheckBox *_yaxis_rescale_fld;
	QCheckBox *_yaxis_swap_fld;
	QLabel *_yaxis_save_fld;
	QguWLinput *_yaxis_name_fld;
	QCheckBox *_yaxis_overwrite_fld;
	QLabel *message_lbl;
	QCheckBox *_replot_fld;

	double _x_from;
	double _x_to;
	double _y_from;
	double _y_to;

	vector <double> _x_from_old;
	vector <double>  _x_to_old;
	vector <double>  _y_from_old;
	vector <double>  _y_to_old;
	vector <int>     num_old_frames;
	vector <int>     num_old_panels;
	vector <QpFrameItem*>  _frame_item_old;
	vector <QpPanelItem*>  _panel_item_old;
	vector <int>  _panel_autoscale_old;
	vector <int>  _frame_autoscale_old;
        
        int _timer;

protected:
  virtual void timerEvent(QTimerEvent *e)
  {
    if(parentWidget()!=NULL)
    {
      if(!parentWidget()->isVisible())
        hide();
     }
  }

public slots:
	void catchValueRet(QWidget*);
	void reject(){;}
	void resetValues();
	void rePlot();
	void saveXT();
	void saveY();
	void getValues();
};


class IntervalEditorWindow: public QMainWindow
{
	Q_OBJECT
	public:
		IntervalEditorWindow(QWidget * parent);
		
		void setInterval(const QDOS::Time& t1, const QDOS::Time& t2);
		
		QbTimeIntervalEditor * editor;
		QdTimeInterval_var interval;
		
	signals:
		
		void changesApplied();
	
	protected slots:
		
		void apply();
		void cancel();
};


// class QpAxisTab
//------------------

class QpAxisTab : public QpTab
{
Q_OBJECT

public:
	QpAxisTab(QTabWidget *tabWidget,  QString name, QpListItem * item, QpTabType _tabType);
	~QpAxisTab(){;}
	// methods to be re-implemented for each axis type
	virtual void setAxisMinMax(){;}
	virtual void setAutoLabelText(){;}
	virtual void setTfromObj(){;}
	void refreshYZRanges();

	
	QpListItem * I_Item;
	QpAxes I_axis;
	
	QpPanelItem * I_Panel;
	QComboBox *_axis_autoscale_fld;
	QLabel* linlog_label;
	QComboBox *_axis_linlog_fld;
	QLabel* t_drop_label;
	QguWLinput *_t_data_fld;
	QguWLinput *_axis_from_xy_fld;
	QguWLinput *_axis_to_xy_fld;
	QPushButton * _axis_time_edit_fld;
	QCheckBox *_axis_y_nudge_fld;
	
	QbDateTimeWidget *_axis_from_time_fld;
	QbDateTimeWidget *_axis_to_time_fld;

	QSpinBox *_axis_major_ticks_num_fld;
	QComboBox *_axis_major_tickmarks_fld;
	QSpinBox *_axis_minor_ticks_num_fld;
	QComboBox *_axis_minor_tickmarks_fld;
	QLineEdit *_label_fld;
	FloatSpinBox *_label_char_size_fld;
	FloatSpinBox *_label_disp_fld;
	QCheckBox *_axis_autolabel_fld;
	FloatSpinBox *_label_rotation_fld;
	FontButton *_label_font_fld;
	QPushButton *_label_colour_fld;
	QPushButton *_event_label_fld;
	QGroupBox *frame_type_groupbox;
	QPushButton *_frame_type_btn;
	
	QLabel* tick_time_fmt_label;
	QLabel* time_labels_fmt_label;
	QLineEdit* tick_time_fmt_fld;
	QLineEdit* time_labels_fmt_fld;
	QCheckBox* tick_time_fmt_auto_fld;
	QCheckBox* time_labels_fmt_auto_fld;
	
	int axis_auto_last;
	QColor _label_colour;
	
	IntervalEditorWindow* editorWindow;

public slots:
	void setAxisAutoScale(int);
	void setAxisLinLog(int);
	void setAxisMin();
	void setAxisMax();
	void setAxisNumMajorTicks(int);
	void setAxisMajorTickMarks(int);
	void setAxisNumMinorTicks(int);
	void setAxisMinorTickMarks(int);
	void setAxisLabel();
	void setAxisLabelCharSize(double);
	void setAxisLabelDisp(double);
	void setAxisAutoLabel(bool);
	void setAxisLabelRotation(double);
// 	void setAxisLabelFont(int);
	void setAxisLabelFamily(int);
	void setAxisLabelStyle(bool);
	void setAxisLabelWeight(bool);
    void setAxisLabelColour();
	void launchFrameTypeSelector();
	void launchIntervalEditor();
	void refreshInterval();

};

// class QpXAxisTab
//------------------

class QpXAxisTab : public QpAxisTab
{
Q_OBJECT

public:
	QpXAxisTab(QTabWidget *tabWidget,  QString name, QpFrameItem * frame);
	~QpXAxisTab(){;}
	void setAxisMinMax();
	void setAutoLabelText();
	
	QpFrameItem * I_Frame;
	
public slots:
    
	void interactiveRescale(double start, double end);

};


// class QpTAxisTab
//------------------

class QpTAxisTab : public QpAxisTab
{
Q_OBJECT

public:
	QpTAxisTab(QTabWidget *tabWidget,  QString name, QpFrameItem * frame);
	~QpTAxisTab(){;}
	void setAxisMinMax();
	void setAutoLabelText();
	void setTfromObj();
	
	QpFrameItem * I_Frame;
	
public slots:
    void setTData();
    
    void setTickTimeFormat(const QString&);
    void setTickTimeFormatAuto(int);
    void setTimeLabelsFormat(const QString&);
    void setTimeLabelsFormatAuto(int);

    void interactiveRescale(double start, double end);
};

// class QpYAxisTab
//------------------

class QpYAxisTab : public QpAxisTab
{
Q_OBJECT

public:
	QpYAxisTab(QTabWidget *tabWidget,  QString name, QpPanelItem * panel);
	~QpYAxisTab(){;}
	void setAxisMinMax();
	void setAutoLabelText();
	
	QpPanelItem * I_Panel;

public slots:
	void updateNudge();
};

// class QpLayoutTab
//------------------

class QpLayoutTab : public QpTab
{
Q_OBJECT
public:
	QpLayoutTab(QTabWidget *tabWidget,  QString name, QpPageItem * _page);
	~QpLayoutTab(){;}
	void updateRowCol(int numFrames);

	QpPageItem * I_Page;
	QSpinBox* _row_num_fld;
	QSpinBox* _col_num_fld;
	QCheckBox* _autolayout_fld;
	FloatSpinBox* _hgap_fld;
	FloatSpinBox* _vgap_fld;
	FloatSpinBox* _lmargin_fld;
	FloatSpinBox* _rmargin_fld;
	FloatSpinBox* _tmargin_fld;
	FloatSpinBox* _bmargin_fld;
	
public slots:
    void setNumRows(int);
    void setNumCols(int);
	void setHGap(double);
	void setVGap(double);
	void setLMargin(double);
	void setRMargin(double);
	void setTMargin(double);
	void setBMargin(double);	
	void setAutoLayout(bool);


};

// class QpPageTab
//------------------

class QpPageTab : public QpTab
{
Q_OBJECT
public:
	QpPageTab(QTabWidget *tabWidget,  QString name, QpPageItem * _page);
	~QpPageTab(){;}

	QpPageItem * I_Page;
	
	QComboBox* _page_size_fld;
	QComboBox* _units_fld;
	QLineEdit* _width_fld;
	QLineEdit* _height_fld;
	QComboBox* _dpi_fld;
	QComboBox* _orientation_fld;
	QLineEdit* _xwin_num_fld;
	QCheckBox* _show_ident_fld;
	int oldUnits;
	
public slots:
	void setPageSize(int);
	void setUnits(int);
	void setDPI(int);
	void setWidth(const QString&);
	void setHeight(const QString&);
	void setOrientation(int);
	void setXwinNum(const QString&);
	void setIdent(bool);
};

class QpEventTab: public QpTab
{
  Q_OBJECT

public:
	QpEventTab( QTabWidget *tabWidget,  QString name, QpEventItem * _event);
	~QpEventTab(){;}

	QpEventItem * I_event;
	QbuLineEdit* _event_label_fld;
	QLabel* _y_label_fld;
	QLabel* _x_label_fld;
	QguWLinput* _y_data_fld;
	QguWLinput* _x_data_fld;
	FloatSpinBox* _char_size_fld;
	FontButton* _event_label_font_fld;
	QPushButton* _event_label_colour_fld;

signals:

public slots:
  void setEventLabel();
  void setEventLabelCharSize(double);
//   void setEventLabelFont(int);
  void setEventLabelFamily(int);
  void setEventLabelStyle(bool);
  void setEventLabelWeight(bool);
  void setEventLabelColour();
  void setYData();
  void setXData();
  void getValues();

};



// class QpDisplayTab
//------------------

class QpDisplayTab : public QpTab
{
	Q_OBJECT
public:
	QpDisplayTab(QTabWidget *tabWidget,  QString name, QpFrameItem * _frame);
	~QpDisplayTab(){;}

	QpFrameItem* I_Frame;
  
	FloatSpinBox* _line_width_fld;
	QPushButton* _line_colour_fld;
	FloatSpinBox* _ticks_length_fld;
	FloatSpinBox* _ticks_char_size_fld;
	QCheckBox* _show_top_axis_fld;
	QCheckBox* _show_bottom_axis_fld;
	QCheckBox* _show_left_axis_fld;
	QCheckBox* _show_right_axis_fld;
	QCheckBox* _show_lr_alternating_axis_fld;
	QCheckBox* _show_rl_alternating_axis_fld;
	QCheckBox* _show_top_ticks_labels_fld;
	QCheckBox* _show_bottom_ticks_labels_fld;
	QCheckBox* _show_left_ticks_labels_fld;
	QCheckBox* _show_right_ticks_labels_fld;
	QCheckBox* _show_lr_alternating_ticks_labels_fld;
	QCheckBox* _show_rl_alternating_ticks_labels_fld;
	QCheckBox* _show_top_labels_fld;
	QCheckBox* _show_bottom_labels_fld;
	QCheckBox* _show_left_labels_fld;
	QCheckBox* _show_right_labels_fld;
	QCheckBox* _show_lr_alternating_labels_fld;
	QCheckBox* _show_rl_alternating_labels_fld;
	QCheckBox* _show_xiszero_fld;
	QCheckBox* _show_yiszero_fld;
	
public slots:
    void setLineWidth(double);
	void setLineColour();
	void setTicksLength(double);
	void setTicksCharSize(double);
	void setTopAxis(bool);
	void setBottomAxis(bool);
	void setLeftAxis(bool);
	void setRightAxis(bool);
	void setLRAlternatingAxis(bool);
	void setRLAlternatingAxis(bool);
	void setTopTicksLabels(bool);
	void setBottomTicksLabels(bool);
	void setLeftTicksLabels(bool);
	void setRightTicksLabels(bool);
	void setLRAlternatingTicksLabels(bool);
	void setRLAlternatingTicksLabels(bool);
	void setTopLabels(bool);
	void setBottomLabels(bool);
	void setLeftLabels(bool);
	void setRightLabels(bool);
	void setLRAlternatingLabels(bool);
	void setRLAlternatingLabels(bool);
	void setYIsZero(bool);
	void setXIsZero(bool);

};

// class QpPropertiesTab
// --------------------------

class QpPropertiesTab : public QpTab
{
	Q_OBJECT
public:
	QpPropertiesTab(QTabWidget *tabWidget,  QString name, QpFrameItem * _frame);
	~QpPropertiesTab(){;}

	QpFrameItem* I_Frame;
	QLabel* _autolayout_lbl;
	FloatSpinBox* _loffset_fld;
	FloatSpinBox* _width_fld;
	FloatSpinBox* _toffset_fld;
	FloatSpinBox* _height_fld;
	QCheckBox* _aspect_ratio_fld;
	QComboBox* _frame_type_fld;
	float _aspect_ratio;
		
public slots:
	void setLOffset(double);
	void setTOffset(double);
	void setWidth(double);
	void setHeight(double);
	void setAspectRatio(bool);


};

// class QpTitleTab
// -----------------

class QpTitleTab : public QpTab
{
	Q_OBJECT
public:
	QpTitleTab(QTabWidget *tabWidget,  QString name, QpFrameItem * _frame);
	~QpTitleTab(){;}

	QpFrameItem* I_Frame;
	QCheckBox* _autotitle_fld;
	QLineEdit* _title_fld;
	FloatSpinBox* _title_char_size_fld;
	FloatSpinBox* _title_disp_fld;
	QLineEdit* _subtitle_fld;
	FloatSpinBox* _subtitle_char_size_fld;
	FloatSpinBox* _subtitle_disp_fld;
	QComboBox* _title_just_fld;
	FontButton* _title_font_fld;
	QPushButton* _title_colour_fld;
					
public slots:
	void setAutoTitle(bool);
	void setTitle(const QString &);
	void setTitleCharSize(double);
	void setTitleDisp(double);
	void setSubTitle(const QString &);
	void setSubTitleCharSize(double);
	void setSubTitleDisp(double);
	void setTitleJust(int);
// 	void setTitleFont(int);
	void setTitleFamily(int);
	void setTitleStyle(bool);
	void setTitleWeight(bool);
	void setTitleColour();
	
};



// class QpDataTab
//------------------

class QpDataTab : public QpTab
{
Q_OBJECT
public:

	QpDataTab(QTabWidget *tabWidget,  QString name, QpPlotItem * _plot, QpItemType plotType);
	~QpDataTab(){;}
	void setDataTypeTitle();
	
	QpPlotItem * I_Plot;
	QpItemType I_PlotType;
	
	QLabel* _y_label_fld;
	QguWLinput* _y_data_fld;
	QLabel* _x_label_fld;
	QguWLinput* _x_data_fld;
	QLineEdit *_legend_text_fld;
	QCheckBox* _show_legend_fld;
	QCheckBox* _auto_legend_fld;
   
	// for Spectrum
	QCheckBox* _XYtoggle;
	QguWLinput* _z_data_fld;
	QguWLinput* _y_start_data_fld;
	QguWLinput* _y_end_data_fld;
	QLabel* _x_start_label_fld;
	QguWLinput* _x_start_data_fld;
	QLabel* _x_end_label_fld;
	QguWLinput* _x_end_data_fld;
	QCheckBox *gap_toggle;
	QCheckBox *timeAsSec_toggle;
	QGroupBox *data_type_groupbox;
	QString old_BaseName;
	QString old_Comp;
	QString old_BaseXName;
	QString old_XComp;

public slots:
	void setYData();
	void setXData();
	void setLegendText(const QString &);
	void setAutoLegend(bool);
	void setShowLegend();
	void useTimeAsSec(bool);
	void setYTextOnly();
   
	// for Spectrum
	void setZData();
	void gap_toggle_changed();
	void setZTextOnly();
	void setXYswap();
	void setYStartData();
	void setYEndData();
	void setXStartData();
	void setXEndData();
	void launchTypeSelector();
    
};


// class QpTraceTab
//------------------

class QpTraceTab : public QpTab
{
	Q_OBJECT
public:
	QpTraceTab(QTabWidget *tabWidget,  QString name, QpTraceItem * _trace);
	~QpTraceTab(){;}   
   
	QpTraceItem * I_Trace;
  
	QComboBox* _line_style_fld;
	FloatSpinBox* _line_width_fld;
	QComboBox* _connect_style_fld;
	QPushButton* _line_colour_fld;
	QComboBox* _symbol_shape_fld;
	FloatSpinBox* _symbol_size_fld;
	QPushButton* _symbol_colour_fld;
	void setLineColour(QColor c);
	void setSymbolColour(QColor c); 
	
public slots:
	void setLineStyle(int choice);
	void setLineWidth(double choice);
	void setConnectStyle(int choice);
	void setLineColour();
	void setSymbolShape(int choice);
	void setSymbolSize(double choice);
	void setSymbolColour(); 


};


// class QpLegendTab
//------------------

class QpLegendTab : public QpTab
{
	Q_OBJECT
public:
	QpLegendTab(QTabWidget *tabWidget,  QString name, QpPanelItem * panel);
	~QpLegendTab(){;}   
	void updateNumTraces();
	
	QpPanelItem * I_Panel;
	QCheckBox *_show_legend_fld;
	QComboBox *_legend_border_fld;
	QComboBox *_legend_background_fld;
	QSpinBox *_legend_num_rows_fld;
	QSpinBox *_legend_num_cols_fld;
	FloatSpinBox *_legend_width_fld;
	FloatSpinBox *_legend_height_fld;
	QSpinBox *_legend_max_char_length_fld;
	FloatSpinBox *_legend_line_length_fld;
	FloatSpinBox *_legend_char_size_fld;
	QComboBox *_legend_just_fld;
	FontButton *_legend_font_fld;
	QComboBox *_legend_align_fld;
	FloatSpinBox *_legend_loffset_fld;
	FloatSpinBox *_legend_toffset_fld;
// 	QWidget* legend_box_widget;
	QGroupBox* legend_box_groupbox;
	QGroupBox* legend_text_groupbox;
	QGroupBox* legend_position_groupbox;
	
public slots:
	void setShowLegend(bool);
	void setLegendBorder(int);
	void setLegendBackground(int);
	void setLegendNumRows(int);
	void setLegendNumCols(int);
	void setLegendWidth(double);
	void setLegendHeight(double);
	void setLegendMaxCharLength(int);
	void setLegendLineLength(double);
	void setLegendCharSize(double);
	void setLegendJust(int);
// 	void setLegendFont(int);
	void setLegendFamily(int);
	void setLegendStyle(bool);
	void setLegendWeight(bool);
	void setLegendAlign(int);
	void setLegendLoffset(double);
	void setLegendToffset(double);
	
};


class QpColourscaleTab : public QpTab
{
Q_OBJECT
public:
	QpColourscaleTab(QTabWidget *tabWidget,  QString name, QpSpectrumItem * _spectro);
	~QpColourscaleTab(){;}

	QpSpectrumItem * I_Spectrum;
  
	QComboBox* _colourscale_auto_fld;
	QComboBox* _colourscale_linlog_fld;
	QguWLinput* _colourscale_min_fld;
	QguWLinput* _colourscale_max_fld;
	QCheckBox* _colourscale_show_colour_bar_fld;
	FloatSpinBox* _colourscale_colour_bar_width_fld;
	FloatSpinBox* _colourscale_colour_bar_height_fld;
	FloatSpinBox* _colourscale_colour_bar_voffset_fld;
	FloatSpinBox* _colourscale_colour_bar_disp_fld;
	FloatSpinBox* _colourscale_ticklength_fld;
	QComboBox* _colourscale_table_fld;
	QLineEdit* _colourscale_label_fld;
	QCheckBox* _colourscale_autolabel_fld;
	
	int colour_auto_last;


public slots:
	void setColourScaleMin(const QString&);
	void setColourScaleMax(const QString&);
	void setColourScaleLabel(const QString&);
	void setColourscaleMinMax();
	void setColourScaleAutoLabel();
	void setAutoScale(int);
	void setLinLog(int);
	void setColourScaleAutoLabelToggle(bool On);
	void setColourbarWidth(double width);
	void setColourbarVoffset(double offset);
	void setColourbarHeight(double height);
	void setColourbarDisplacement(double disp);
	void setColourtable(int choice);
	void setShowColourBar(bool On);
	void setTicksLength(double);
};


// class QpListItem
//------------------

class QpListItem : public QguListItem
{

public:
	QpListItem(QpListView *parent=0,  QString txt="", QpListItem *after=0, QguListType type=ListTypeGroup, QString WL="");
	QpListItem(QpListItem *parent=0,  QString txt="", QpListItem *after=0, QguListType type=ListTypeGroup, QString WL="");
	~QpListItem(){;}
	int numberOfChildren()const;
	virtual void showTabs(){;}	
	QpListItem* lastChild() const;
	QpPageItem *pageItem();
	virtual void setDefaultOptions(){;}
	QpListView * I_ListView;
	QpListItem* parent();
	virtual QpFrameType frameType(){return QP_TS;}
	virtual void updateAxes(){;}
	
	QpItemType I_Type;
	QTabWidget *tabWidget;
	
	bool nameLock;


};

// class QpPageItem
//------------------

class QpPageItem : public QObject, public QpListItem
{
	Q_OBJECT

public:
	QpPageItem(QpListView *parent=0,  QString name="", QpPageItem *after=0);
	~QpPageItem();
	QpuiReturnStatus plot(PlotWindow* win);
	void countColourTables();
	void set_colour_table(const QpuiColourTable& colour_table=QPUI_RAINBOW);
	void setLayout();
	void showTabs();
	void setFramesAuto(bool active);
	void setDefaultOptions();
	PlotWindow* newWindow(double width, double height, int* id,  int resolution=72, QPrinter::PageSize pageSize=QPrinter::A4, QPrinter::Orientation orientation=QPrinter::Portrait);
	PlotWindow* focusWindow(int id, double width=-1., double height=-1., int resolution=72, QPrinter::PageSize pageSize=QPrinter::A4, QPrinter::Orientation orientation=QPrinter::Portrait);
	void clearWindows();
	void rename(QString newName);
	int panelsInColumn(int colno);
	
	int _print_id;
	string _filename;
	
	QString name;

	bool containsSpectrum();
	
	/*static */double _conversion_factors[5];
	static double _width_in_inches[6];
	static double _height_in_inches[6];
   
	QpValueReturnTab* ValueReturnTab;
	QpPageTab* PageTab;
	QpLayoutTab* LayoutTab;

#if QT_VERSION < 0x040000
	std::list<PlotWindow*> PlotWindows;
#else
	QList<PlotWindow*> PlotWindows;
#endif
	
	friend class QpFrameItem;
	friend class QpPanelItem;
	
	public slots:
		void displayIntervalWidget(double x, double y, int pos_x, int pos_y);
};

//  class QpFrameItem
// ------------------

class QpFrameItem : public QpListItem
{

public:
	QpFrameItem(QpPageItem *parent=0, QString txt="", QpFrameItem *after=0,  QpFrameType frame_type=QP_TS);
	~QpFrameItem();
	string getTitle();
	void showTabs();
	bool getXmetaRange(double& min, double& max);
	bool getXRange(double& min, double& max);
	void getYObjects();
	int numberInStack();
	QpuiReturnStatus plot(PlotWindow* win);
	void postPlot();
	QpFrameType frameType(){return _frameType;}
	void setPanelProperties(double loffset, double width, double toffset, double height);
	int panelNum(QpPanelItem *panelItem);
	void changeFrame(int frameType);
	bool containsSpectrum();
	void updateAxes();
	void setDefaultOptions();
	
	QpPropertiesTab* PropertiesTab;
	QpDisplayTab*   DisplayTab;
	QpTitleTab*     TitleTab;
	QpAxisTab * xtAxisTab; // pointer to x or t tab
	QpXAxisTab * xAxisTab;
	QpTAxisTab * tAxisTab;

	QpPageItem * I_Page;
	QColor _line_colour;
	QColor _title_colour;
	QpFrameType _frameType;


};

//  class QpPanelItem
// ------------------

class QpPanelItem : public QpListItem
{

public:
	QpPanelItem(QpFrameItem *parent=0,  QString txt="", QpPanelItem *after=0);
	~QpPanelItem();
   	void changePlotType(QpPlotItem *item, QpItemType plotType);
	void showTabs();
	int  getStackNumber();
	bool getXmetaRange(double& panel_xmin, double& panel_xmax);
	int  getXmetaIsLog();
	bool getXRange(double& panel_xmin, double& panel_xmax);
	void getYObjects();
	bool getYmetaRange(double& ymin, double& ymax);
	bool getYRange(double& _ymin, double& _ymax);
	void getLabel(const QpAxes& axis, string& label);
	int numberTraces();
	QpuiReturnStatus plot(double _height, double _width, double _toffset, double _loffset, PlotWindow* win);
	void postPlot(bool show_top_axis, bool show_bottom_axis);
	void prePlot();
	string getTitle();
	QpFrameType frameType(){return I_Frame->frameType();}
	void updateYZRange();
	void updateAxes();
	void setDefaultOptions();

	QpLegendTab* LegendTab;
	QpYAxisTab * yAxisTab;
		
	QpFrameItem * I_Frame;
	int   _legend_entry;
	float _legend_cell_width;
	float _legend_cell_height;

};

//  class QpPlotItem
// ------------------

class QpPlotItem : public QpListItem
{

public:
	QpPlotItem(QpPanelItem *parent=0,  QString txt="", QpPlotItem *after=0, QString WL="");
	virtual ~QpPlotItem();
	virtual void changeFrameType(int frameType){;}
	QpFrameType frameType();
	virtual	QpuiReturnStatus plot(){;}
	virtual void replaceData(QString obj_name){;}
	virtual void replaceComponent(QString comp){;}
	virtual void replaceXData(QString obj_name){;}
	virtual void replaceXComponent(QString comp){;}
	virtual void clearObj_id(){;}
	virtual int getXmetaIsLog(){;}
	virtual int getYmetaIsLog(){;}
	virtual bool getXmetaRange(double& min, double& max){;}
	virtual bool getYmetaRange(double&, double&){;}
	virtual bool getXRange(double& min, double& max){;}
	virtual bool getYRange(double& ymin, double& ymax){;}
	virtual void get_dobjs(){;}
	void setLinks(list <QpPlotItem*> linkList);
	void removeFromLinks();
	void removeLink(QpPlotItem* plot_item);

	QpPanelItem * I_Panel;
	QpDataTab * DataTab;
		
	int with;
	double missing_value;
	list <QpPlotItem*> linkedPlots;
   
};

//  class QpTraceItem
// ------------------

class QpTraceItem : public QpPlotItem
{

public:
	QpTraceItem(QpPanelItem *parent=0,  QString txt="", QpPlotItem *after=0, QString WL="", QpItemType plotType=QP_TRACE);
	~QpTraceItem();
	int getXmetaIsLog();
	int getYmetaIsLog();
	bool getXmetaRange(double& min, double& max);
	bool getYmetaRange(double& min, double& max);
	bool getXRange(double& min, double& max);
	bool getYRange(double& ymin, double& ymax);
	void getLabel(const QpAxes& axis, string& label);
	void getLegendText(string& legend_text);
	string getTitle();
	void getName(string& name);
	void changeFrameType(int frameType);
	void showTabs();
	QpuiReturnStatus plotLegend();
	QpuiReturnStatus plot();
	void replaceData(QString obj_name);
	void replaceComponent(QString comp);
	void replaceXData(QString obj_name);
	void replaceXComponent(QString comp);
	void refreshTabs();
	void clearObj_id();
	void updateAxes();
	void get_dobjs();
	void setDefaultOptions();

	QpTraceTab * TraceTab;

	QdObject_var _ydo;
	QdObject_var _xdo;
	
	int _x_obj_id_getXRange;
	int _y_obj_id_getYRange;
	double _x_range_min; // min used last y range
	double _x_range_max;   // max used last y range
	double _x_from; // min from data for autoscale
	double _x_to;   // max from data for autoscale
	double _y_from; // min from data for autoscale
	double _y_to;   // max from data for autoscale
	
	QColor _line_colour;
	QColor C1_colour;
	QColor C2_colour;
	QColor C3_colour;
	QColor C4_colour;
	QColor R_colour;
	QColor X_colour;
	QColor Y_colour;
	QColor Z_colour;
	QColor T_colour;
	QColor P_colour;
	QColor L_colour;
	QColor _symbol_colour;
	bool userLineColour;
	bool userSymbolColour;

   
public slots:

};


//  class QpEventItem
// ------------------

class QpEventItem : public QpPlotItem
{

public:
	QpEventItem(QpPanelItem *parent=0,  QString name="", QpPlotItem *after=0);
	~QpEventItem();
	QpuiReturnStatus plot();
	void showTabs();
	
	QpEventTab *eventTab;
	QColor _event_label_colour;
   
public slots:

};


//  class QpSpectrumItem
// ---------------------

class QpSpectrumItem : public QpPlotItem
{

public:
	QpSpectrumItem(QpPanelItem *parent=0,  QString txt="", QpPlotItem *after=0, QString WL="");
	~QpSpectrumItem();
	void getLabel(const QpAxes axis, string& label);
	int  getXData(QdObject_var xdo, QdObject_var xdo_end);
	int  getXData(QdObject_var xdo, QdObject_var xdo_plus, QdObject_var xdo_minus);
	int  getXData(QdObject_var xdo);
	void refreshX();
	int  getYData(QdObject_var ydo, QdObject_var ydo_end);
	int  getYData(QdObject_var ydo, QdObject_var ydo_plus, QdObject_var ydo_minus);
	int  getYData(QdObject_var ydo);
	int  isZConformal(int nx_len, int ny_len);
	string getTitle();
	void getName(string& name);
	int  getXmetaIsLog();
	int  getYmetaIsLog();
	bool getXRange(double&, double&);
	bool getXmetaRange(double&, double&);
	bool getYmetaRange(double&, double&);
	bool getYRange(double& ymin, double& ymax);
	bool getZRange(double &zmin, double &zmax);
	bool getZmetaRange(double& zmin,double& zmax);
	void updateAxes();
	void get_dobjs();
	void setDefaultOptions();

	QdObject_var getZDO();
	int  fill_X_Data();
	int  fill_Y_Data();
	int  fill_Z_Data(int XtoY, bool inPlot=true);
	void changeFrameType(int frameType);
	void showTabs();
	QpuiReturnStatus plot();
	void replaceData(QString obj_name);
	void replaceComponent(QString comp);
	void refreshTabs();
	void clearObj_id();

	QpColourscaleTab* ColourscaleTab;

	QdObject_var _zdo;
	int _z_obj_id;
	vector <dblBins> _z_data_all;
	QdObject_var _ydo;
	int _y_start_obj_id;
	vector <dblBins> _y_start_all;
	int _y_end_obj_id;
	vector <dblBins> _y_end_all;
	QdObject_var _xdo;
	int _x_start_obj_id;
	vector <double> _x_start_all;
	int _x_end_obj_id;
	vector <double> _x_end_all;
	QString y_data_start_str;
	QString y_data_end_str;
	QString x_data_start_str;
	QString x_data_end_str;
	int _nx_data;
	int _ny_data;
	bool XYtoggle_in_fill_x;
	bool XYtoggle_in_fill_y;
	int zdo_id_getZRange;
	int ydo_id_getYRange;
	int xdo_id_getXRange;
	
	double x_range_min;
	double x_range_max;	
	double x_range_min_z;
	double x_range_max_z;	
	double y_range_min_z;
	double y_range_max_z;	
	double x_min_data;
	double x_max_data;	
	double y_min_data;
	double y_max_data;	
	double z_min_data;
	double z_max_data;
	
public slots:
	
};

// class QpListView
//----------------

Vostream& operator<<(Vostream&, QpListView&);
Vistream& operator>>(Vistream&, QpListView&);

class QpListView : public QguListView
{
      Q_OBJECT
public:
  
	QpListView(QWidget *parent, QpWindow* window, const char *name=0);
	~QpListView(){;}

	void setPageOption(int option, int iChoice=0, float fChoice=0.0, QString sChoice ="", bool bChoice =false, QColor cChoice = QColor(0, 0, 0));
	void setFrameOption(int option, int iChoice=0, float fChoice=0.0, QString sChoice ="", bool bChoice =false, QColor cChoice = QColor(0, 0, 0));
	void setTraceOption(int option, int iChoice=0, float fChoice=0.0, QString sChoice ="", bool bChoice =false, QColor cChoice = QColor(0, 0, 0));
	void setEventOption(int option, int iChoice=0, float fChoice=0.0, QString sChoice ="", bool bChoice =false, QColor cChoice = QColor(0, 0, 0));
	void setSpectrumOption(int option, int iChoice=0, float fChoice=0.0, QString sChoice ="", bool bChoice =false, QColor cChoice = QColor(0, 0, 0));
	void setLegendOption(int option, int iChoice=0, float fChoice=0.0, QString sChoice ="", bool bChoice =false, QColor cChoice = QColor(0, 0, 0));
	void setAxisOption(int option, int axis=T_AXIS, int iChoice=0, float fChoice=0.0, QString sChoice ="", bool bChoice =false, QColor cChoice = QColor(0, 0, 0));
	void setPlotOption(int option,  bool bChoice =false);
	QString nextFrameName();
	QString nextPanelName();
	QpListItem* lastChild() const;
	QpListItem * getDropTarget(QpListItem *dropAt, const string type);
	void insertDragItem(QguListItem *dropAt, QguListItem *item);
	void insertEntries(QguListItem *dropAt, QguListItem *item);
	QpListItem * makePlotItem(QpListItem * target, QString obj_name);
	QpPlotItem * makePlot(QpListItem * target, QpItemType plotType, QpFrameType frameType=QP_TS, QString WL="");
	QpFrameItem* makeXYFrame(QpPageItem* pageItem);
	QpFrameItem* makeTSFrame(QpPageItem* pageItem);
	QpPageItem* makePage();
	QpPanelItem* makePanel(QpFrameItem* frameItem);
	QpTraceItem* makeTrace(QpPanelItem* panelItem, QString WL);
	QpTraceItem* makeConstXTrace(QpPanelItem* panelItem, QString WL);
	QpTraceItem* makeConstYTrace(QpPanelItem* panelItem, QString WL);
	QpSpectrumItem* makeSpectrum(QpPanelItem* panelItem, QString WL);
	QpEventItem* makeEvent(QpPanelItem* panelItem);
	void makeMultiTraceItem(vector <int> &nTraces, vector <QpTraceItem *> &stack, QpFrameType frameType);
	void plot();
	bool reNameItem(QpListItem *item);
	void selectAll();
	void setSelectedList(QguListItem *item);
	void makeEmptyItem(QpItemType type);
	bool findPItem(QpListItem* item);
	void deleteItem(QpListItem* item);
	void selectSomething();
	void removeLink(QpPlotItem* plot_item);
	void linkTraceItems(vector <QpTraceItem *> stack, bool onceOnly);
	void raisePlotWindow();
// 	int _plot_device;
	QTabWidget *tabWidget;
	vector<QpPageItem*> _plot_page_list;
	QpTabType I_LastTab;
	bool isLink;
	//char* vectorPlot;
	//char* array3dPlot;
	int LinkNumber;
	
	friend class QpPageItem;
	friend class QpFrameItem;
	friend class QpPanelItem;
	friend class QpTraceItem;
	friend class QpSpectrumItem;
	friend class QpWindow;

protected:
	virtual void contentsMouseReleaseEvent(QMouseEvent *event);
	virtual void contentsMouseDoubleClickEvent( QMouseEvent *event );
	void keyPressEvent(QKeyEvent * e );


signals:
	void selectionModified();
	void status_msg(const QString&);
	void pageLayoutChanged(QpPageItem*);
  
public slots:
	void setPlotType(int);
	void setFrameType(int);
	void deleteOneItem();
	void deleteSelectedItems();
	void dropEvent(QDropEvent *event);
	void dragEnterEvent(QDragEnterEvent *event);
	void moveItemDown();
	void moveItemUp();
	void emitStatusMsg(const QString& str){ emit status_msg(str);}
	void plotCurrentItem();
	void plotSelectedItems();
	void setDefaultSelectedItems();
	void setDefaultCurrentItem();
	virtual void rightButtonPressEvent(Q_LIST_VIEW_ITEM *, const QPoint &,int);
	void renameSelectedItem();
	void setPageLayout(QpPageItem *pageItem);
	void refreshWLitems();
	void updateOnSelection();
	void makeTraceItem();
	void makeSpectrumItem();
	void makeConstXTraceItem();
	void makeConstYTraceItem();
	void makeEventItem();
	void makePanelItem();
	void makeTSFrameItem();
	void makeXYFrameItem();
	void makePageItem();
	void linkPlots();
	void unlinkPlots();
	void makeVector_RXYZ_Item(QpFrameType frameType=QP_TS);
	void makeVector_XYZ_Item(QpFrameType frameType=QP_TS);
	void makeVector_R_TP_Item(QpFrameType frameType=QP_TS);
	void makeVector_R_LP_Item(QpFrameType frameType=QP_TS);
	void makeVector_R_XYZ_Item(QpFrameType frameType=QP_TS);
	void makeVector_R_X_Y_Z_Item(QpFrameType frameType=QP_TS);
	void makeVector_X_Y_Z_Item(QpFrameType frameType=QP_TS);
	void makeVector_R_T_P_Item(QpFrameType frameType=QP_TS);
	void makeVector_R_L_P_Item(QpFrameType frameType=QP_TS);
	void makeC1234_Item();
	void makeC1_2_3_4_Item();
	void make2Panel_Item();
	void make3Panel_Item();
	void make4Panel_Item();
	
	void paste();

};


// PGPLOT lower level routines from the grpckg
//---------------------------------------------
extern "C" void grsize_(int*,float*,float*,float*,float*,float*,float*);
extern "C" void grxrgb_(float*,float*,float*,float*,float*,float*);
extern "C" void grchsz_(int*,float*,float*,float*,float*);

// Function generating the dash styles
// ----------------------
void setDashStyle(QpLineStyle style);

// Function getting the character height in normalized device coordinates
// ----------------------
PLFLT getCharHeight();

int getPGPColourIndex(const QColor&, bool reset=false);
PLINT symbolIndexConversion(int index);
double SafeLog10(double);
string SafePGPString(const string&);
float roundf(float,short);

void print_iden();

// Plot Utility calls
//------------------
QpItemType QpRemapPlotType(int _type);


class QpuiColourScale
{
public:
  bool _autoscale;
  bool _recalc_range;
  double _min;
  double _max;
  double _missing_value;
  int _linlog;
  bool _show_colour_bar;
  float _colour_bar_width;
  int _colour_table;
  float _colour_bar_disp;
  string _label;
  bool _autolabel;
  float _colour_bar_height;
  float _colour_bar_voffset;

};

// class  QpWindow
// -----------------------

class QpWindow : public QguWindow
{
  Q_OBJECT
 
  QWidget*    _top_widget;
  QGridLayout* _top_gl;
  QSplitter * _top_splitter;
  QpListView* _plot_listview;
  QStatusBar* _status_bar;
  QMenuBar *_menu_bar;
 
public slots:
	void status(const QString& str) {_status_bar->message(str,2000);};
	void doPlot();
	void deleteAllItems();

	void launchManPlot();
	void launchManual();
	void launchVersion();

signals:
  void WLmodified();

public:
  QpWindow( QWidget *parent=0);
  ~QpWindow(){;}
  void save(Vostream&);
  void restore(Vistream&,bool replace=true);
  void restoreV22(Vistream& v_is, bool replace);
  void resetDefaultBtn();

  QMenuBar* menu_bar(){ return _menu_bar;}

  int _releaseVersion;
  int _majorVersion;
  int _minorVersion;

protected:
  void keyPressEvent(QKeyEvent * e );
  QPushButton *plotBtn;
//   QPushButton *printBtn;
  QPushButton *dismissBtn;
};



void QPUImakeWindow(QWidget*,const char*);
void QpuiShow();
extern QpWindow *QPUI_widget;
QdObject_var makeIndexObject(QdObject_var zdo, int indx);


Vostream& operator<<(Vostream&, QpListView&);
Vistream& operator>>(Vistream&, QpListView&);

Vostream& operator<<(Vostream&, QpListItem&);
Vistream& operator>>(Vistream&, QpListItem&);

Vostream& operator<<(Vostream&, QpPageItem&);
Vistream& operator>>(Vistream&, QpPageItem&);

Vostream& operator<<(Vostream&, QpFrameItem&);
Vistream& operator>>(Vistream&, QpFrameItem&);

Vostream& operator<<(Vostream&, QpPanelItem&);
Vistream& operator>>(Vistream&, QpPanelItem&);

Vostream& operator<<(Vostream&, QpSpectrumItem&);
Vistream& operator>>(Vistream&, QpSpectrumItem&);

Vostream& operator<<(Vostream&, QpTraceItem&);
Vistream& operator>>(Vistream&, QpTraceItem&);

Vostream& operator<<(Vostream&, QpEventItem&);
Vistream& operator>>(Vistream&, QpEventItem&);

Vostream& operator<<(Vostream&, QpAxisTab&);
Vistream& operator>>(Vistream&, QpAxisTab&);

// interface to old (V2.2 and earlier) savesets

void restoreListview(Vistream& v_is, QpListView *lv);
void restoreItem(Vistream& v_is, QpListItem* item);
void restorePageItem(Vistream& v_is, QpPageItem* page);
bool restoreTSFrameItem(Vistream& v_is, QpFrameItem *frame);
void restoreXYFrameItem(Vistream& v_is, QpFrameItem *frame);
void restorePanelItem(Vistream& v_is, QpFrameItem *frame, QpPanelItem *panel);
void restoreAxis(Vistream& v_is, QpAxisTab *axis, bool keep, bool isXY);
void restoreFrameTabs(Vistream& v_is, QpFrameItem *frame);
void restoreLegend(Vistream& v_is, QpPanelItem *panel);
void restoreTSstack(Vistream& v_is, QpFrameItem *frame, QpPanelItem *panel);
void restoreXYstack(Vistream& v_is, QpFrameItem *frame, QpPanelItem *panel);
void restoreTrace(Vistream& v_is, QpTraceItem *trace);
void restoreEvent(Vistream& v_is, QpEventItem *event);
void restoreSpectroGram(Vistream& v_is, QpSpectrumItem *spectrum);

# endif // QSAS_PLOT_UI_H_
