/***************************************************************************
 *                                                                         *
 *   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 FRAMES_H
#define FRAMES_H

#define BORDERWIDTH 2
#define COLORS 10
#define DOCK_WIDTH 24

#include "resources.h"
#include "kdevmonconfig.h"

#include <qframe.h>
#include <qevent.h>
#include <qcolor.h>
#include <qtooltip.h>
#include <Qt3Support/Q3MemArray>
#include <ksystemtrayicon.h>


class BpsFrame;
class RateTooltip;
class GraphPixmap;
class QPixmap;
class QBitmap;
class KRootPixmap;

/** class for a frame that shows a graph of two specified values.
 * (for example the values can be the in/out rate from a network device).
 */
class MainFrame : public QFrame
{
	Q_OBJECT
public:
	MainFrame( QWidget *parent=0);
	~MainFrame();
	
	void initFromConfig();
	
	void updateDisplay( double rateIn, double RateOut );
	void clear();
	QString getTooltipText( const QPoint& point ) const;

protected:
	void paintEvent( QPaintEvent * );
	void resizeEvent( QResizeEvent * );
	bool event(QEvent *event);
	
private:
	Q3MemArray<double> m_rateInLifo;
	Q3MemArray<double> m_rateOutLifo;
	int m_currentIndex;
	
	
	QWidget *m_rootPixmap;
	GraphPixmap *m_graphPixmap;
	RateTooltip *tooltip;
	KdevmonConfig *config;
};


class DockFrame : public KSystemTrayIcon
{
	Q_OBJECT
public:
	DockFrame( QWidget *parent);
	~DockFrame();
	
	void initFromConfig();
	
	void updateDisplay( double rateIn, double RateOut );
	void clear();
	void setShowText( bool show );


protected:
	void paintEvent( QPaintEvent * );
	void resizeEvent( QResizeEvent * );
	
private:
	Q3MemArray<double> m_rateInLifo;
	Q3MemArray<double> m_rateOutLifo;
	int m_currentIndex;
	GraphPixmap *m_graphPixmap;
	KdevmonConfig *config;

};


class GraphPixmap : public QObject
{
public:	
	GraphPixmap();
	
	void initFromConfig();
	void drawGraph( const Q3MemArray<double>&, const Q3MemArray<double>&, int );
	void setSize( int w, int h );
	const QPixmap &getPixmap() const { return *m_graphPixmap; }
	
private:
	QPixmap *m_graphPixmap;
	QBitmap *m_mask;
	QColor m_colorInArray[COLORS], m_colorOutArray[COLORS];
	Q3MemArray<double> m_lastRateInArray, m_lastRateOutArray;
	int m_lastIndex;
	int getGridRowHeight();
};


/** class for a frame that simply draws a text in a specified color
 */
class BpsFrame : public QFrame {
public:	
	/** constructor, only sets font */
	BpsFrame( QWidget *parent);
	~BpsFrame();
	
	/** draws text */
	void drawText( const QString& text, const QColor& color);
	/** change background color */
	void setPixmapColor( const QColor& color );
protected:
	/** resizes font */
	void resizeEvent( QResizeEvent * );
	void paintEvent( QPaintEvent * );
private:
	QFont font;
	QColor pixmapColor;
	QPixmap *pixmap;
 	QString text;
	QColor color;
};


/** very simple class for a frame that only draws a bar.
 * Placeholder for a more complex class
 */
class BarFrame : public QFrame {
public:
	/** constructor, sets up pixmap on which the circle is drawn */
	BarFrame( QWidget *parent );
	~BarFrame();

	void initFromConfig();
	void updatePixmap( double rateIn, double rateOut );
	void clear();
protected:
	/** resizes pixmap */
	void resizeEvent( QResizeEvent * );
	void paintEvent( QPaintEvent * );
private:
	QPixmap *pixmap;
	QColor
	pixmapColor,
	colorIn,
	colorOut;
	Common::GraphPosition graphPosition;
	KdevmonConfig * config;
	double lastPercentIn, lastPercentOut;
	double percentIn, percentOut;
};

class Tools
{
public:
	static QString double2bit( double value );
	static QString double2byte( double value );
};

#endif
