/***************************************************************************
 *   Copyright (C) 2005-2006 by Anne-Marie Mahfouf   *
 *   annemarie.mahfouf@free.fr   *
 *                                                                         *
 *   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.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/


#include "kalcul.h"
#include "settings.h"
#include "timer.h"

#include <QtGui/QPrinter>
#include <QtGui/QPrintDialog>

#include <kconfigdialog.h>
#include <kdebug.h>
#include <kglobal.h>
#include <klocale.h>
#include <kmenubar.h>
#include <kstatusbar.h>
#include <kicon.h>
#include <kaction.h>
#include <kstandardaction.h>
#include <kactioncollection.h>
#include <QApplication>

Kalcul::Kalcul()
    : KXmlGuiWindow(),
      m_view(new KalculView(this)),
      m_printer(0)
{
    // accept dnd
    setAcceptDrops(true);

    // tell the KXmlGuiWindow that this is indeed the main widget
    setCentralWidget(m_view);

    // and a status bar
    statusBar( )->insertItem("   ",IDS_USER,   0);
    statusBar()->show();

    // then, setup our actions
    setupActions();

    // apply the saved mainwindow settings, if any, and ask the mainwindow
    // to automatically save settings if changed: window size, toolbar
    // position, icon size, etc.
    setAutoSaveSettings();

    // allow the view to change the statusbar and caption
    connect(m_view, SIGNAL(signalChangeStatusbar(const QString&, int)),
            this,   SLOT(changeStatusbar(const QString&, int)));
    connect(m_view, SIGNAL(signalChangeCaption(const QString&)),
            this,   SLOT(changeCaption(const QString&)));
    changeStatusbar( i18n("User: %1", Settings::nameLine() ), IDS_USER);
}

Kalcul::~Kalcul()
{
}

void Kalcul::setupActions()
{
    KStandardAction::quit(qApp, SLOT(quit()), actionCollection());

    exerciseDialogAction = new KAction(i18n("Exercise Dialog"), actionCollection());
	actionCollection()->addAction("exercise_dialog", exerciseDialogAction );
    exerciseDialogAction->setIcon(KIcon("system-run"));
    connect(exerciseDialogAction, SIGNAL(triggered(bool)), this, SLOT(showExerciseDialog()));

    levelEditorAction = new KAction(i18n("Level Editor"), actionCollection());
	actionCollection()->addAction("level_editor", levelEditorAction );
    levelEditorAction->setIcon(KIcon("document-properties"));
    connect(levelEditorAction, SIGNAL(triggered(bool)), this, SLOT(showLevelEditor()));

    KStandardAction::preferences(this, SLOT(optionsPreferences()), actionCollection());

    setupGUI();
}

void Kalcul::optionsPreferences()
{
    if ( KConfigDialog::showDialog( "settings" ) )  {
            return;
    }
    KConfigDialog* dialog = new KConfigDialog( this, "settings",  Settings::self() );
    QWidget *generalSettingsDlg = new QWidget;
    ui_general.setupUi(generalSettingsDlg);
    dialog->addPage(generalSettingsDlg, i18n("General"), "kalcul");
    dialog->addPage(new Timer(), i18n("Timer"), "chronometer");
    QWidget *advancedSettingsDlg = new QWidget;
    ui_advanced.setupUi(advancedSettingsDlg);
    dialog->addPage(advancedSettingsDlg, i18n("Advanced"), "preferences-other");
    //dialog->addPage(new missing(), i18n("Sequence"), "apply");
    connect(dialog, SIGNAL(settingsChanged(const QString &)), this, SLOT(settingsChanged()));
    dialog->show();
}

void Kalcul::changeStatusbar(const QString& text, int id)
{
    kDebug() << "in status " << endl;
    // display the text on the statusbar
    statusBar()->changeItem(text, id);
}

void Kalcul::changeCaption(const QString& text)
{
    // display the text on the caption
    setCaption(text);
}


void Kalcul::practiceAddition()
{
    m_view->setPracticeMode(Practice_Addition);
    Settings::setOperation(Practice_Addition);
}

void Kalcul::practiceSubtraction()
{
    m_view->setPracticeMode(Practice_Subtraction);
    Settings::setOperation(Practice_Subtraction);
}

void Kalcul::practiceMultiplication()
{
    m_view->setPracticeMode(Practice_Multiplication);
    Settings::setOperation(Practice_Multiplication);
}

void Kalcul::practiceDivision()
{
    m_view->setPracticeMode(Practice_Division);
    Settings::setOperation(Practice_Division);
}

void Kalcul::showExerciseDialog()
{
    KDialog *exerciseDialog=new KDialog( this );
    exerciseDialog->setCaption( i18n( "foo" ) );
    exerciseDialog->setButtons( KDialog::Ok | KDialog::Cancel | KDialog::Apply );
    exerciseDialog->setDefaultButton( KDialog::Ok );
    QWidget *exerciseDlg = new QWidget;
    ui_exercisechooser.setupUi(exerciseDlg);
    exerciseDialog->setMainWidget(exerciseDlg);
    //TODO: can this be done with less connects?
    /*connect(exerciseDialog, SIGNAL(signalPracticeAddition()), this, SLOT(practiceAddition()));
    connect(exerciseDialog, SIGNAL(signalPracticeSubtraction()), this, SLOT(practiceSubtraction()));
    connect(exerciseDialog, SIGNAL(signalPracticeMultiplication()), this, SLOT(practiceMultiplication()));
    connect(exerciseDialog, SIGNAL(signalPracticeDivision()), this, SLOT(practiceDivision()));

    connect(exerciseDialog, SIGNAL(setLevel(const QString&)), m_view, SLOT(setLevel(const QString&)));*/
    exerciseDialog->show();
}

void Kalcul::showLevelEditor()
{
    QDialog *levelEditor = new QDialog;
    ui_leveleditor.setupUi(levelEditor);
    levelEditor->show();
}

void Kalcul::settingsChanged()
{
    m_view->settingsChanged();
}

#include "kalcul.moc"

