/***************************************************************************
 *   Copyright (C) 2008 by Andreas Silberstorff   *
 *   ml@andreas-silberstorff.de   *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef PLUGINFACTORY_H
#define PLUGINFACTORY_H

#include <KalvaGeneralSettings>
#include <PluginInterface>
#include <PerspectiveInterface>
#include <DeviceInterface>
#include <SchedulerInterface>
#include <ChannellistProviderInterface>

#include <QVariant>
#include <QObject>

class QWidget;
class QTabWidget;
class QStackedWidget;
class KConfigDialog;

/**
 * This class implements kalva extensions through plugins.
 *
 * The PluginManager reads in all plugins
 * and constructs a PluginFactory for each plugin. The factories are held
 * by the manager and can be accessed through PluginManager::factories();
 *
 * @short PluginFactory
 * @author Andreas Silberstorff <ml@andreas-silberstorff.de>
 * @version 1.0.91
 */

namespace KalvaPluginSystem {

class PluginFactory
 : public QObject
{
        Q_OBJECT
public:
	/**
	 * Default constructor.
	 *
	 * Factories are not intended to be constructed other then by the PluginManager.
	 *
	 */
	PluginFactory( QTabWidget *manager, QStackedWidget *mainViewStack, QObject *plugin );

	/**
	 * Destructor
	 */
	virtual ~PluginFactory();

	/**
	 * Provides access to the plugin.
	 */
	inline KalvaPlugin::PluginInterface* plugin() { return m_plugin; }

	/**
	 * Provides access to the perspective, if it is such a plugin.
	 */
	inline KalvaPlugin::PerspectiveInterface* perspective() { return m_perspective; };

	/**
	 * Provides access to the device, if it is such a plugin.
	 */
	inline KalvaPlugin::DeviceInterface* device() { return m_device; };

	/**
	 * Provides access to the scheduler, if it is such a plugin.
	 */
	inline KalvaPlugin::SchedulerInterface* scheduler() { return m_scheduler; };

	/**
	 * Provides access to the scheduler, if it is such a plugin.
	 */
	inline KalvaPlugin::ChannellistProviderInterface* channellistprovider() { return m_channellistprovider; };

	/**
	 * Provides access to the managerTabWidget of kalvaview.
	 */
	inline QTabWidget* managerTabWidget() { return m_managerTabWidget; };

	/**
	 * Provides access to the mainViewStackedWidget of kalvaview.
	 */
	inline QStackedWidget* mainViewStackedWidget() { return m_mainViewStackedWidget; };

	/**
	 * True, if the perspective is enabled.
	 * See setEnabled( bool ).
	 */
	inline bool isEnabled() { return m_enabled; };

public slots:

	/**
	 * enable perspective
	 */
	void setEnabled(bool enabled);

	/**
	 * Informs the plugin when settings changed.
	 */
	virtual void settingsChanged();

	/**
	 * Adds config page(s) for the plugin to the applications config dialog.
	 * The adding of pages is deligated to the plugin.
	 * The implementation here ensures that a plugin adds its config pages
	 * only once to the open KConfigDialog.
	 */
	void addToConfigDialog( KConfigDialog *dialog );

private:

	QTabWidget* m_managerTabWidget;
	QStackedWidget* m_mainViewStackedWidget;
	KConfigDialog* m_dialog;
	QWidget* m_managerWidget;
	QWidget* m_mainViewWidget;
	KalvaPlugin::PluginInterface* m_plugin;
	KalvaPlugin::PerspectiveInterface* m_perspective;
	KalvaPlugin::DeviceInterface* m_device;
	KalvaPlugin::SchedulerInterface* m_scheduler;
	KalvaPlugin::ChannellistProviderInterface* m_channellistprovider;
	bool m_enabled;

	/**
	 * The factory may insert Widgets.
	 */
	void enable();

	/**
	 * The factory may remove Widgets.
	 */
	void disable();

	/**
	 * The factory may insert Widgets.
	 */
	void enablePerspective();

	/**
	 * The factory may remove Widgets.
	 */
	void disablePerspective();

private slots:

	/**
	 * This slot is connected with the manager tab widget's selected-Signal
	 * to make the stackWidget show the main view of the perspective.
	 */
	void perspectiveSelected( int perspectiveId );

};

}

Q_DECLARE_METATYPE(KalvaPluginSystem::PluginFactory*)

#endif // PLUGINFACTORY_H
