/***************************************************************************
                          keduca.cpp  -  description
                             -------------------
    begin                : mié may 23 20:36:15 CEST 2001
    copyright            : (C) 2001 by Javier Campos
    email                : javi_cms@terra.es
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "keduca.h"
#include "keduca.moc"

#include "keducaprefs.h"

#include <klocale.h>
#include <kstandarddirs.h>
#include <kfiledialog.h>
#include <kedittoolbar.h>
#include <kkeydialog.h>

#include <qwidget.h>
#include <qlayout.h>
#include <qlabel.h>

Keduca::Keduca( QWidget* parent, const char *name, WFlags f )
    : KMainWindow(parent, name, f),
      _mainView(0)
{
    init();
    initMenuBar();
    configRead();
}

Keduca::~Keduca()
{
    delete _recentFiles;
}

/** Init graphical interface */
void Keduca::init()
{
    QWidget *mainview = new QWidget(this);
    this->setBackgroundColor(white);
    QBoxLayout* layout_1 = new QBoxLayout( mainview, QBoxLayout::TopToBottom, 5, 5, NULL );
    layout_1->addStrut( 0 );
    QLabel *mainLabel = new QLabel(mainview, "mainLabel");
    mainLabel->setGeometry(0, 0, 256, 156);
    mainLabel->setBackgroundColor(white);
    mainLabel->setAlignment( AlignHCenter|AlignVCenter );
    mainLabel->setPixmap( QPixmap( locate("appdata","pics/keduca_init.png") ) );
    layout_1->addWidget( mainLabel );
    mainview->show();

    this->setCentralWidget(mainview);
    this->setCaption( i18n("Main Window") );
}

/** Init menu bar settings */
void Keduca::initMenuBar()
{
    KStdAction::open(this, SLOT( slotFileOpen() ), actionCollection());
    _recentFiles = KStdAction::openRecent(this, SLOT ( slotFileOpenURL(const KURL &)), actionCollection());
//	KStdAction::print(this, SLOT( slotMenuPrint() ), actionCollection());
    KStdAction::close(this, SLOT( slotFileClose() ), actionCollection());
    KStdAction::quit(this, SLOT( close() ), actionCollection());

    (void)new KAction(i18n("&Create/Modify..."), "control", 0, this, SLOT( slotControlDoc() ), actionCollection(), "control_doc");

    KStdAction::showToolbar(this, SLOT( slotToggleToolBar()), actionCollection());
    KStdAction::keyBindings(this, SLOT( slotConfigureKeys() ), actionCollection());
    KStdAction::configureToolbars(this, SLOT( slotConfigureToolbar() ), actionCollection());
    KStdAction::preferences(this, SLOT( slotConfigure()), actionCollection());
    createGUI( "keducaui.rc" );
}

/** Open new file */
void Keduca::slotFileOpen()
{
    KURL url = KFileDialog::getOpenURL( QString::null, "application/x-edu", this,  i18n("Open Educa File") );
    if( !url.isEmpty() )
        slotFileOpenURL( url );
}

/** Open a file */
void Keduca::slotFileOpenURL( const KURL &url )
{
    _mainView = new KEducaView(this,"mainview");
    if( !_mainView->openURL(url) ) { delete _mainView; return; }
    _recentFiles->addURL( url );
    _mainView->show();
    this->setCentralWidget(_mainView);
}

/** Close actual document */
void Keduca::slotFileClose()
{
    if( _mainView ) delete _mainView;
    init();
}

/** Start control documents */
void Keduca::slotControlDoc()
{
    KControlDoc *pepe = new KControlDoc();
    pepe->show();
    if (_mainView != 0)
        pepe->slotFileOpenURL(_mainView->getKeducaFile()->getCurrentURL());
    else
        pepe->startChooser();
}

/** Close the window */
bool Keduca::queryClose()
{
    configWrite();
    return( TRUE );
}

/** Read settings */
void Keduca::configRead()
{
    KConfig *config = KGlobal::config();

    config->setGroup( "keduca" );
    this->applyMainWindowSettings( config, "keduca" );
    _recentFiles->loadEntries(config);
}

/** Write settings */
void Keduca::configWrite()
{
    KConfig *config = KGlobal::config();

    config->setGroup( "keduca" );
    this->saveMainWindowSettings( config, "keduca" );
    _recentFiles->saveEntries(config);
    config->sync();
}

/** Toggle toolbar */
void Keduca::slotToggleToolBar()
{
    if(toolBar()->isVisible())
        toolBar()->hide();
    else
        toolBar()->show();
}

/** Configure keys */
void Keduca::slotConfigureKeys()
{
    KKeyDialog::configureKeys( actionCollection(), xmlFile() );
}

/** Configure toolbar */
void Keduca::slotConfigureToolbar()
{
    KEditToolbar edit( guiFactory(), this );
    if( edit.exec() )
        createGUI( "keducaui.rc" );
}

/** Configure keduca */
void Keduca::slotConfigure()
{
    KEducaPrefs pref;
    pref.exec();
}
