/*
    KKSysGuard, the KDE System Guard
   
	Copyright (c) 1999 - 2002 Chris Schlaeger <cs@kde.org>
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of version 2 of the GNU General Public
    License as published by the Free Software Foundation.

    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., 675 Mass Ave, Cambridge, MA 02139, USA.

	KSysGuard is currently maintained by Chris Schlaeger <cs@kde.org>.
	Please do not commit any changes without consulting me first. Thanks!

	$Id: WorkSheet.h,v 1.26 2002/03/03 02:39:14 cschlaeg Exp $
*/

#ifndef _WorkSheet_h_
#define _WorkSheet_h_

#include <qvaluelist.h>
#include <qwidget.h>

#include <SensorDisplay.h>

class QGridLayout;
class QDragEnterEvent;
class QDropEvent;
class QString;
class QDomElement;
class QTextStream;

/**
 * A WorkSheet contains the displays to visualize the sensor results. When
 * creating the WorkSheet you must specify the number of columns. Displays
 * can be added and removed on the fly. The grid layout will handle the
 * layout. The number of columns can not be changed. Displays are added by
 * dragging a sensor from the sensor browser over the WorkSheet.
 */
class WorkSheet : public QWidget, public KSGRD::SensorBoard
{
	Q_OBJECT
public:
	WorkSheet(QWidget* parent);
	WorkSheet(QWidget* parent, uint rows, uint columns, uint interval);
	~WorkSheet();

	bool load(const QString& fN);
	bool save(const QString& fN);

	void cut();
	void copy();
	void paste();

	const QString& getFileName() const
	{
		return (fileName);
	}

	void setFileName(const QString& fN)
	{
		fileName = fN;
		setModified(true);
	}

	bool hasBeenModified() const
	{
		return modified;
	}

	void setName(const QString& _name)
	{
		name = _name;
	}

	KSGRD::SensorDisplay* addDisplay(const QString& hostname,
							  const QString& monitor,
							  const QString& sensorType,
							  const QString& sensorDescr, uint r, uint c);
	void settings();

	void setIsOnTop(bool onTop);
	
public slots:
	void showPopupMenu(KSGRD::SensorDisplay* display);
	void setModified(bool mfd);
	void applyStyle();

signals:
	void sheetModified(QWidget* sheet);

protected:
	void dragEnterEvent(QDragEnterEvent* ev);
	void dropEvent(QDropEvent* ev);
	void customEvent(QCustomEvent* ev);

private:
	void removeDisplay(KSGRD::SensorDisplay* display);

	bool replaceDisplay(uint r, uint c, QDomElement& element);

	void replaceDisplay(uint r, uint c, KSGRD::SensorDisplay* display = 0);

	void collectHosts(QValueList<QString>& l);

	void createGrid(uint r, uint c);

	void resizeGrid(uint r, uint c);

	KSGRD::SensorDisplay* currentDisplay(uint* r = 0, uint* c = 0);

	void fixTabOrder();

	QString currentDisplayAsXML();

	QString fileName;
	QString name;
	bool modified;
	uint rows;
	uint columns;
	QGridLayout* lm;
	/* This two dimensional array stores the pointers to the sensor displays
	 * or if no sensor is present at a position a pointer to a dummy widget.
	 * The size of the array corresponds to the size of the grid layout. */
	KSGRD::SensorDisplay*** displays;
} ;

#endif
