/***************************************************************************
 *   Copyright (C) 2008 by Andreas Silberstorff   *
 *   taki@Rivendell   *
 *                                                                         *
 *   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.             *
 ***************************************************************************/

#include "kalvaview.h"
#include "settings.h"
#include "pluginmanager.h"

#include <KLocale>
#include <KConfigGroup>
#include <KConfigDialog>
#include <KGlobal>
#include <KDialog>
#include <KDebug>

#include <QtGui>
#include <QGridLayout>
#include <QTabWidget>
#include <QSplitter>
#include <QStackedWidget>
#include <QRect>
#include <QSize>
#include <QSizePolicy>

// Constructor
// Constructs the main view of kalva. The main view consists of a QTabWidget called manager
// and a QStackedWidget called mainViewStack.
// Perspectives have to provide a Widget for the manager and a Widget for the mainViewStack.
// see also AbstractPerspective
kalvaview::kalvaview ( QWidget *parent ) : QWidget( parent )
{
	m_managerSize = Optimized;

	setWindowTitle( i18n( "Kalva" ) );
	setAutoFillBackground( true );
	createWidgets();
	layoutWidgets();

	setupPerspectives();

	settingsChanged();
}

// cleanup at application shutdown
// delete all active perspectives
kalvaview::~kalvaview()
{
}

// Widget creation
// Here the widgets are created that will be the root of
// the widgets the perspectives have to provide.
void kalvaview::createWidgets()
{
	m_manager = new QTabWidget( this );
	m_manager->setTabPosition( QTabWidget::West );

	m_mainViewStack = new QStackedWidget( this );

	m_splitter = new QSplitter( this );
	m_splitter->addWidget( m_manager );
        m_splitter->addWidget( m_mainViewStack );

	m_resizer = new QPushButton ( ">" );
	QFont font = m_resizer->font();
	font.setPointSize( 7 );
	m_resizer->setFont( font );
	m_resizer->setFixedSize( 18, 18 );
	connect( m_resizer, SIGNAL( clicked() ),
                     SLOT( resizeManager() ) );
}

// layout definitions for the widgets
void kalvaview::layoutWidgets()
{
	QGridLayout* grid  = new QGridLayout( this );

 	grid->addWidget( m_splitter, 1, 0, 10, 10 );
 	grid->addWidget( m_resizer, 0, 0, 1, 1 );

 	grid->setMargin( KDialog::spacingHint() );
 	grid->setSpacing( KDialog::spacingHint() );

        setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
	setMinimumSize( minimumSizeHint() );
	setMaximumSize( maximumSizeHint() );
}

QSize kalvaview::minimumSizeHint() const
{
	int x = Settings::max_size_width();
	int y = Settings::max_size_height();
	return QSize(x, y);
}

QSize kalvaview::maximumSizeHint()
{
	int x = Settings::max_size_width();
	int y = Settings::max_size_height();
	return QSize(x, y);
}

void kalvaview::setupPerspectives()
{
	KalvaPluginSystem::PluginManager::instance().setTabWidget( m_manager );
	KalvaPluginSystem::PluginManager::instance().setMainViewStackedWidget( m_mainViewStack );

// TODO delete when settings can be stored! ...only set enabled on loading of kalvaview untill storing of settings is working!!!
	KalvaPluginSystem::PluginManager::instance().perspectives();
}

void kalvaview::resizeManager()
{
	kDebug() << "Perspective::resizeManager()";

	QList<int> managerOptimized;
	managerOptimized << 1;
	managerOptimized << 1;

	QList<int> managerMaximized;
	managerMaximized << 1;
	managerMaximized << 0;

	QList<int> managerMinimized;
	managerMinimized << 0;
	managerMinimized << 1;

	switch (m_managerSize)
	{
		case (Minimized):
		{
			kDebug() << "manager is Minimized, set to managerOptimized";
			m_splitter->setSizes( managerOptimized );
			m_managerSize = Optimized;
			m_resizer->setText( ">" );
			break;
		}
		case (Optimized):
		{
			kDebug() << "manager is Optimized, set to managerMaximized";
			m_splitter->setSizes( managerMaximized );
			m_managerSize = Maximized;
			m_resizer->setText( "<" );
			break;
		}
		case (Maximized):
		{
			kDebug() << "manager is Maximized, set to managerMinimized";
			m_splitter->setSizes( managerMinimized );
			m_managerSize = Minimized;
			m_resizer->setText( "|" );
			break;
		}
	}
}

void kalvaview::addToConfigDialog( KConfigDialog *dialog )
{
	kDebug() << "addConfigPageTo()";
        KalvaPluginSystem::PluginManager::instance().addToConfigDialog( dialog );
}

void kalvaview::settingsChanged()
{
	KalvaPluginSystem::PluginManager::instance().settingsChanged();
}

#include "kalvaview.moc"
