/***************************************************************************
                          qcake.cpp  -  description
                             -------------------
    begin                : Sun Nov  7 16:54:22 CET 2004
    copyright            : (C) 2004 by Harald Krippel
    email                : neuro.harald@surfeu.at
 ***************************************************************************/

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

// Qt includes
#include <qvbox.h>
#include <qaccel.h>
#include <qsettings.h>

// application specific includes
#include "globdefines.h"
#include "kcakeview.h"
#include "qcakedoc.h"
#include "qcake.h"

// Icon Pictures
#include "filenew.xpm"
#include "fileopen.xpm"
#include "filesave.xpm"
#include "crop.xpm"
#include "move.xpm"
#include "redo.xpm"
#include "rotate.xpm"
#include "run.xpm"
#include "stop.xpm"
#include "wizard.xpm"
#include "qcake.xpm"

#include "main.h"
#include "glcontrol.h"


QcakeApp::QcakeApp()
{
  setCaption(tr("QCake " QCAKEVERSION ) );

  printer = new QPrinter;
  doc = NULL;
  view = NULL;
  doc = new QcakeDoc(this);

//  untitledCount=0;
//  pDocList = new QList<QcakeDoc>();
//  pDocList->setAutoDelete(true);

  ///////////////////////////////////////////////////////////////////
  // call inits to invoke all other construction parts
  initView();
  initActions();
  initMenuBar();
  initToolBar();
  initStatusBar();
  readSettings();
//  resize( 1024, 768 );

  viewToolBar->setOn(true);
  viewStatusBar->setOn(true);
  viewFullScreenBar->setOn(false);

    ///////////////////////////////////////////////////////////////////
  // disable actions at startup
  fileClose->setEnabled(false);
  fileSave->setEnabled(false);
  fileSaveAs->setEnabled(false);
  filePrint->setEnabled(false);
  editCut->setEnabled(false);
  editCopy->setEnabled(false);
  editPaste->setEnabled(false);
  toolsStop->setEnabled(false);

}

QcakeApp::~QcakeApp()
{
  delete printer;
}

void QcakeApp::initActions()
{
  QPixmap openIcon, saveIcon, newIcon;
  QPixmap scaleIcon, moveIcon, resetIcon;
  QPixmap rotateIcon, playIcon, stopIcon;
  QPixmap wizardIcon;

  newIcon = QPixmap(filenew);
  openIcon = QPixmap(fileopen);
  saveIcon = QPixmap(filesave);

  scaleIcon = QPixmap(crop_xpm);
  moveIcon = QPixmap(move_xpm);
  resetIcon = QPixmap(redo_xpm);
  
  rotateIcon = QPixmap(rotate_xpm);
  playIcon = QPixmap(run_xpm);
  stopIcon = QPixmap(stop_xpm);

  wizardIcon = QPixmap(wizard_xpm);

  fileNew = new QAction(tr("New File"), newIcon, tr("&New"), QAccel::stringToKey(tr("Ctrl+N")), this);
  fileNew->setStatusTip(tr("Creates a new document"));
  fileNew->setWhatsThis(tr("New File\n\nCreates a new document"));
  connect(fileNew, SIGNAL(activated()), this, SLOT(slotFileNew()));

  fileOpen = new QAction(tr("Open File"), openIcon, tr("&Open..."), 0, this);
  fileOpen->setStatusTip(tr("Opens an existing document"));
  fileOpen->setWhatsThis(tr("Open File\n\nOpens an existing document"));
  connect(fileOpen, SIGNAL(activated()), this, SLOT(slotFileOpen()));

  fileSave = new QAction(tr("Save File"), saveIcon, tr("&Save"), QAccel::stringToKey(tr("Ctrl+S")), this);
  fileSave->setStatusTip(tr("Saves the actual document"));
  fileSave->setWhatsThis(tr("Save File.\n\nSaves the actual document"));
  connect(fileSave, SIGNAL(activated()), this, SLOT(slotFileSave()));

  fileSaveAs = new QAction(tr("Save File As"), tr("Save &as..."), 0, this);
  fileSaveAs->setStatusTip(tr("Saves the actual document under a new filename"));
  fileSaveAs->setWhatsThis(tr("Save As\n\nSaves the actual document under a new filename"));
  connect(fileSaveAs, SIGNAL(activated()), this, SLOT(slotFileSave()));

  fileClose = new QAction(tr("Close File"), tr("&Close"), QAccel::stringToKey(tr("Ctrl+W")), this);
  fileClose->setStatusTip(tr("Closes the actual document"));
  fileClose->setWhatsThis(tr("Close File\n\nCloses the actual document"));
  connect(fileClose, SIGNAL(activated()), this, SLOT(slotFileClose()));

  filePrint = new QAction(tr("Print File"), tr("&Print"), QAccel::stringToKey(tr("Ctrl+P")), this);
  filePrint->setStatusTip(tr("Prints out the actual document"));
  filePrint->setWhatsThis(tr("Print File\n\nPrints out the actual document"));
  connect(filePrint, SIGNAL(activated()), this, SLOT(slotFilePrint()));

  fileQuit = new QAction(tr("Exit"), tr("E&xit"), QAccel::stringToKey(tr("Ctrl+Q")), this);
  fileQuit->setStatusTip(tr("Quits the application"));
  fileQuit->setWhatsThis(tr("Exit\n\nQuits the application"));
//  connect(fileQuit, SIGNAL(activated()), this, SLOT(slotFileQuit()));
  connect(fileQuit, SIGNAL(activated()), this, SLOT(close()));

//  toolsEditCam = new KAction(i18n("Editor Camera"), "wizard", 0, this, SLOT(slotToolsEditCam()), actionCollection(),"tools_editcam");
  toolsEditCam = new QAction(tr("EditCam"), wizardIcon, tr("&EditCam"), 0, this);
  toolsEditCam->setStatusTip(tr("EditorCamera"));
  toolsEditCam->setWhatsThis(tr("Editor Camera\n\nmove around"));
  connect(toolsEditCam, SIGNAL(activated()), this, SLOT(slotToolsEditCam()));

//  toolsTranslate = new KAction(i18n("Translate"), "move", 0, this, SLOT(slotToolsTranslate()), actionCollection(),"tools_translate");
  toolsTranslate = new QAction(tr("Translate"), moveIcon, tr("&Translate"), 0, this);
  toolsTranslate->setStatusTip(tr("Translate"));
  toolsTranslate->setWhatsThis(tr("Translate\n\nTranslate Object"));
  connect(toolsTranslate, SIGNAL(activated()), this, SLOT(slotToolsTranslate()));

//  toolsRotate = new KAction(i18n("Rotate"), "rotate", 0, this, SLOT(slotToolsRotate()), actionCollection(),"tools_rotate");
  toolsRotate = new QAction(tr("Rotate"), rotateIcon, tr("&Rotate"), 0, this);
  toolsRotate->setStatusTip(tr("Rotate"));
  toolsRotate->setWhatsThis(tr("Rotate\n\nRotate Object"));
  connect(toolsRotate, SIGNAL(activated()), this, SLOT(slotToolsRotate()));

//  toolsScale = new KAction(i18n("Scale"), "crop", 0, this, SLOT(slotToolsScale()), actionCollection(),"tools_scale");
  toolsScale = new QAction(tr("Scale"), scaleIcon, tr("&Scale"), 0, this);
  toolsScale->setStatusTip(tr("Scale"));
  toolsScale->setWhatsThis(tr("Scale\n\nScale Object"));
  connect(toolsScale, SIGNAL(activated()), this, SLOT(slotToolsScale()));

//  toolsPlay = new KAction(i18n("Play"), "run", 0, this, SLOT(slotToolsPlay()), actionCollection(),"tools_play");
  toolsPlay = new QAction(tr("Play"), playIcon, tr("&Play"), 0, this);
  toolsPlay->setStatusTip(tr("Play"));
  toolsPlay->setWhatsThis(tr("Play\n\nswitch to Play mode"));
  connect(toolsPlay, SIGNAL(activated()), this, SLOT(slotToolsPlay()));

//  toolsStop = new KAction(i18n("Stop"), "stop", 0, this, SLOT(slotToolsStop()), actionCollection(),"tools_stop");
  toolsStop = new QAction(tr("Stop"), stopIcon, tr("&Stop"), 0, this);
  toolsStop->setStatusTip(tr("Stop"));
  toolsStop->setWhatsThis(tr("Stop\n\nswitch to Stop mode"));
  connect(toolsStop, SIGNAL(activated()), this, SLOT(slotToolsStop()));

//  toolsReset = new KAction(i18n("Reset"), "redo", 0, this, SLOT(slotToolsReset()), actionCollection(),"tools_reset");
  toolsReset = new QAction(tr("Reset"), resetIcon, tr("&Reset"), 0, this);
  toolsReset->setStatusTip(tr("Reset"));
  toolsReset->setWhatsThis(tr("Reset\n\nswitch to Reset mode"));
  connect(toolsReset, SIGNAL(activated()), this, SLOT(slotToolsReset()));
  
  editCut = new QAction(tr("Cut"), tr("Cu&t"), QAccel::stringToKey(tr("Ctrl+X")), this);
  editCut->setStatusTip(tr("Cuts the selected section and puts it to the clipboard"));
  editCut->setWhatsThis(tr("Cut\n\nCuts the selected section and puts it to the clipboard"));
  connect(editCut, SIGNAL(activated()), this, SLOT(slotEditCut()));

  editCopy = new QAction(tr("Copy"), tr("&Copy"), QAccel::stringToKey(tr("Ctrl+C")), this);
  editCopy->setStatusTip(tr("Copies the selected section to the clipboard"));
  editCopy->setWhatsThis(tr("Copy\n\nCopies the selected section to the clipboard"));
  connect(editCopy, SIGNAL(activated()), this, SLOT(slotEditCopy()));

  editUndo = new QAction(tr("Undo"), tr("&Undo"), QAccel::stringToKey(tr("Ctrl+Z")), this);
  editUndo->setStatusTip(tr("Reverts the last editing action"));
  editUndo->setWhatsThis(tr("Undo\n\nReverts the last editing action"));
  connect(editUndo, SIGNAL(activated()), this, SLOT(slotEditUndo()));

  editPaste = new QAction(tr("Paste"), tr("&Paste"), QAccel::stringToKey(tr("Ctrl+V")), this);
  editPaste->setStatusTip(tr("Pastes the clipboard contents to actual position"));
  editPaste->setWhatsThis(tr("Paste\n\nPastes the clipboard contents to actual position"));
  connect(editPaste, SIGNAL(activated()), this, SLOT(slotEditPaste()));

  viewToolBar = new QAction(tr("Toolbar"), tr("Tool&bar"), 0, this, 0, true);
  viewToolBar->setStatusTip(tr("Enables/disables the toolbar"));
  viewToolBar->setWhatsThis(tr("Toolbar\n\nEnables/disables the toolbar"));
  connect(viewToolBar, SIGNAL(toggled(bool)), this, SLOT(slotViewToolBar(bool)));

  viewStatusBar = new QAction(tr("Statusbar"), tr("&Statusbar"), 0, this, 0, true);
  viewStatusBar->setStatusTip(tr("Enables/disables the statusbar"));
  viewStatusBar->setWhatsThis(tr("Statusbar\n\nEnables/disables the statusbar"));
  connect(viewStatusBar, SIGNAL(toggled(bool)), this, SLOT(slotViewStatusBar(bool)));

  viewFullScreenBar = new QAction(tr("FullScreen"), tr("&FullScreen"), 0, this, 0, true);
  viewFullScreenBar->setStatusTip(tr("Enables/disables the statusbar"));
  viewFullScreenBar->setWhatsThis(tr("Statusbar\n\nEnables/disables FullScreen"));
  connect(viewFullScreenBar, SIGNAL(toggled(bool)), this, SLOT(slotViewFullScreenBar(bool)));

  windowNewWindow = new QAction(tr("New Window"), tr("&New Window"), 0, this);
  windowNewWindow->setStatusTip(tr("Opens a new view for the current document"));
  windowNewWindow->setWhatsThis(tr("New Window\n\nOpens a new view for the current document"));
  connect(windowNewWindow, SIGNAL(activated()), this, SLOT(slotWindowNewWindow()));

  windowCascade = new QAction(tr("Cascade"), tr("&Cascade"), 0, this);
  windowCascade->setStatusTip(tr("Cascades all windows"));
  windowCascade->setWhatsThis(tr("Cascade\n\nCascades all windows"));
//  connect(windowCascade, SIGNAL(activated()), pWorkspace, SLOT(cascade()));

  windowTile = new QAction(tr("Tile"), tr("&Tile"), 0, this);
  windowTile->setStatusTip(tr("Tiles all windows"));
  windowTile->setWhatsThis(tr("Tile\n\nTiles all windows"));
//  connect(windowTile, SIGNAL(activated()), pWorkspace, SLOT(tile()));


  windowAction = new QActionGroup(this, 0, false);
  windowAction->insert(windowNewWindow);
  windowAction->insert(windowCascade);
  windowAction->insert(windowTile);

  helpAboutApp = new QAction(tr("About"), tr("&About..."), 0, this);
  helpAboutApp->setStatusTip(tr("About the application"));
  helpAboutApp->setWhatsThis(tr("About\n\nAbout the application"));
  connect(helpAboutApp, SIGNAL(activated()), this, SLOT(slotHelpAbout()));

}

void QcakeApp::initMenuBar()
{
  ///////////////////////////////////////////////////////////////////
  // MENUBAR
    for (int i = 0; i < MaxRecentFiles; ++i)
    recentFileIds[i] = -1;

  ///////////////////////////////////////////////////////////////////
  // menuBar entry pFileMenu
  pFileMenu=new QPopupMenu();
  fileNew->addTo(pFileMenu);
  fileOpen->addTo(pFileMenu);
  fileClose->addTo(pFileMenu);
  pFileMenu->insertSeparator();
  fileSave->addTo(pFileMenu);
  fileSaveAs->addTo(pFileMenu);
  pFileMenu->insertSeparator();
  filePrint->addTo(pFileMenu);
  pFileMenu->insertSeparator();
  fileQuit->addTo(pFileMenu);

  ///////////////////////////////////////////////////////////////////
  // menuBar entry editMenu
  pEditMenu=new QPopupMenu();
  editUndo->addTo(pEditMenu);
  pEditMenu->insertSeparator();
  editCut->addTo(pEditMenu);
  editCopy->addTo(pEditMenu);
  editPaste->addTo(pEditMenu);



  ///////////////////////////////////////////////////////////////////
  // menuBar entry viewMenu
  pViewMenu=new QPopupMenu();
  pViewMenu->setCheckable(true);
  viewToolBar->addTo(pViewMenu);
  viewStatusBar->addTo(pViewMenu);
  viewFullScreenBar->addTo(pViewMenu);

  ///////////////////////////////////////////////////////////////////
  // EDIT YOUR APPLICATION SPECIFIC MENUENTRIES HERE
  // menuBar entry viewMenu
  pToolMenu=new QPopupMenu();
  pToolMenu->setCheckable(true);
  toolsEditCam->addTo(pToolMenu);
  toolsTranslate->addTo(pToolMenu);
  toolsRotate->addTo(pToolMenu);
  toolsScale->addTo(pToolMenu);
  toolsPlay->addTo(pToolMenu);
  toolsStop->addTo(pToolMenu);
  toolsReset->addTo(pToolMenu);

  ///////////////////////////////////////////////////////////////////
  // menuBar entry windowMenu
//  pWindowMenu = new QPopupMenu(this);
//  pWindowMenu->setCheckable(true);
//  connect(pWindowMenu, SIGNAL(aboutToShow()), this, SLOT(windowMenuAboutToShow()));

  ///////////////////////////////////////////////////////////////////
  // menuBar entry helpMenu
  pHelpMenu=new QPopupMenu();
  helpAboutApp->addTo(pHelpMenu);
  pHelpMenu->insertSeparator();
  pHelpMenu->insertItem(tr("What's &This"), this, SLOT(whatsThis()), SHIFT+Key_F1);


  menuBar()->insertItem(tr("&File"), pFileMenu);
  menuBar()->insertItem(tr("&Edit"), pEditMenu);
  menuBar()->insertItem(tr("&Tool"), pToolMenu);
  menuBar()->insertItem(tr("&View"), pViewMenu);
//  menuBar()->insertItem(tr("&Window"), pWindowMenu);
  menuBar()->insertItem(tr("&Help"), pHelpMenu);

}

void QcakeApp::initToolBar()
{
  ///////////////////////////////////////////////////////////////////
  // TOOLBAR
  fileToolbar = new QToolBar(this, "file operations");
  fileNew->addTo(fileToolbar);
  fileOpen->addTo(fileToolbar);
  fileSave->addTo(fileToolbar);
  fileToolbar->addSeparator();
  QWhatsThis::whatsThisButton(fileToolbar);

  ///////////////////////////////////////////////////////////////////
  // TOOLS TOOLBAR
  toolsToolbar = new QToolBar(this, "tool operations");
  toolsEditCam->addTo(toolsToolbar);
  toolsTranslate->addTo(toolsToolbar);
  toolsRotate->addTo(toolsToolbar);
  toolsScale->addTo(toolsToolbar);
  toolsPlay->addTo(toolsToolbar);
  toolsStop->addTo(toolsToolbar);
  toolsReset->addTo(toolsToolbar);
}

void QcakeApp::initStatusBar()
{
  ///////////////////////////////////////////////////////////////////
  //STATUSBAR
  statusBar()->message(tr("Ready."));
}

void QcakeApp::initView()
{ 
  ////////////////////////////////////////////////////////////////////
  // set the main widget here
  view = new KCakeView(this);
  doc->addView(view);
  view->setFrameStyle( QFrame::StyledPanel | QFrame::Sunken );
  setCentralWidget(view);


}


void QcakeApp::createClient(QcakeDoc* doc)
{
//  KCakeView* w = new KCakeView(doc, pWorkspace,0,WDestructiveClose);
  KCakeView* w = new KCakeView(pWorkspace);
  w->installEventFilter(this);
  doc->addView(w);
  if ( pWorkspace->windowList().isEmpty() ) // show the very first window in maximized mode
    w->showMaximized();
  else
    w->show();
}

void QcakeApp::openDocumentFile(const char* file)
{
  statusBar()->message(tr("Opening file..."));

  doc->openDocument( file);
  doc->setURL(file);

  statusBar()->message(tr("Ready."));
}

bool QcakeApp::queryExit()
{
  int exit=QMessageBox::information(this, tr("Quit..."),
                                    tr("Do your really want to quit?"),
                                    QMessageBox::Ok, QMessageBox::Cancel);

  if (exit==1)
  {

  }
  else
  {

  };

  return (exit==1);
}

bool QcakeApp::eventFilter(QObject* object, QEvent* event)
{
  if((event->type() == QEvent::Close)&&((QcakeApp*)object!=this))
  {
    QCloseEvent* e=(QCloseEvent*)event;
//    KCakeView* pView=(KCakeView*)object;
//    QcakeDoc* pDoc=pView->getDocument();
//    if(pDoc->canCloseFrame(pView))
//    {
//      pDoc->removeView(pView);
//      if(!pDoc->firstView())
//        pDocList->remove(pDoc);
//
//      e->accept();
//    }
//    else
      e->ignore();
  }
  return QWidget::eventFilter( object, event );    // standard event processing
}

/////////////////////////////////////////////////////////////////////
// SLOT IMPLEMENTATION
/////////////////////////////////////////////////////////////////////


void QcakeApp::slotFileNew()
{
  statusBar()->message(tr("Creating new file..."));

/*  
  if(!doc->modified)
  {
     // here saving wasn't successful

  }
  else
  {
    doc->newDocument();
    setCaption(doc->URL().fileName(), false);
  }
*/
    doc->newDocument();

  fileClose->setEnabled(true);
  fileSave->setEnabled(true);
  fileSaveAs->setEnabled(true);

  statusBar()->message(tr("Ready."));
}

void QcakeApp::slotFileOpen()
{
  statusBar()->message(tr("Opening file..."));

  QString fileName = QFileDialog::getOpenFileName(0,"*.xml (*.xml)",this);
  if (!fileName.isEmpty())
  {
     openDocumentFile(fileName);
     recentFiles.remove(fileName);
     recentFiles.push_front(fileName);
     updateRecentFileItems();
     
     fileClose->setEnabled(true);
     fileSave->setEnabled(true);
     fileSaveAs->setEnabled(true);
  }
  statusBar()->message(tr("Ready."));
}


void QcakeApp::slotFileSave()
{
  statusBar()->message(tr("Saving file..."));
	
  doc->saveDocument(doc->URL());

  statusBar()->message(tr("Ready."));
}

void QcakeApp::slotFileSaveAs()
{
  statusBar()->message(tr("Saving file under new filename..."));
  QString fn = QFileDialog::getSaveFileName(0, 0, this);
  if (!fn.isEmpty())
  {
    KCakeView* m = (KCakeView*)pWorkspace->activeWindow();
    if( m )
    {
      QcakeDoc* doc = m->getDocument();
      if(!doc->saveDocument(fn))
      {
         QMessageBox::critical (this, tr("I/O Error !"), tr("Could not save the current document !"));
         return;
      }
      doc->changedViewList();
    }
  }
  statusBar()->message(tr("Ready."));
}

void QcakeApp::slotFileClose()
{
  statusBar()->message(tr("Closing file..."));
	
  KCakeView* m = (KCakeView*)pWorkspace->activeWindow();
  if( m )
  {
    QcakeDoc* doc=m->getDocument();
    doc->closeDocument();
    
    fileClose->setEnabled(false);
    fileSave->setEnabled(false);
    fileSaveAs->setEnabled(false);
  }

  statusBar()->message(tr("Ready."));
}

void QcakeApp::slotFilePrint()
{
  statusBar()->message(tr("Printing..."));
	
  KCakeView* m = (KCakeView*) pWorkspace->activeWindow();
  if ( m )
    m->print( printer );

  statusBar()->message(tr("Ready."));
}
/*
void QcakeApp::slotFileQuit()
{ 
  statusBar()->message(tr("Exiting application..."));

  writeSettings();
  ///////////////////////////////////////////////////////////////////
  // exits the Application
//  if(doc->isModified())
//  {
//    if(queryExit())
//    {
//      qApp->quit();
//    }
//    else
//    {
//
//    };
//  }
//  else
//  {
    qApp->quit();
//  };

  statusBar()->message(tr("Ready."));
}
*/

void QcakeApp::closeEvent(QCloseEvent *event)
{
//    if (maybeSave()) {
        writeSettings();
        event->accept();
//    } else {
//        event->ignore();
//    }
}

void QcakeApp::slotEditUndo()
{
  statusBar()->message(tr("Reverting last action..."));
	
  KCakeView* m = (KCakeView*) pWorkspace->activeWindow();
  if ( m )
//   m->undo();

  statusBar()->message(tr("Ready."));
}

void QcakeApp::slotEditCut()
{
  statusBar()->message(tr("Cutting selection..."));
	
  KCakeView* m = (KCakeView*) pWorkspace->activeWindow();
  if ( m )
//  m->cut();	

  statusBar()->message(tr("Ready."));
}

void QcakeApp::slotEditCopy()
{
  statusBar()->message(tr("Copying selection to clipboard..."));

  KCakeView* m = (KCakeView*) pWorkspace->activeWindow();
  if ( m )
//  m->copy();

  statusBar()->message(tr("Ready."));
}

void QcakeApp::slotEditPaste()
{
  statusBar()->message(tr("Inserting clipboard contents..."));
	
  KCakeView* m = (KCakeView*) pWorkspace->activeWindow();
  if ( m )
//   m->paste();

  statusBar()->message(tr("Ready."));
}


void QcakeApp::slotViewToolBar(bool toggle)
{
  statusBar()->message(tr("Toggle toolbar..."));
  ///////////////////////////////////////////////////////////////////
  // turn Toolbar on or off
   if (toggle== false)
  {
    fileToolbar->hide();
    toolsToolbar->hide();
  }
  else
  {
    fileToolbar->show();
    toolsToolbar->show();
  };

 statusBar()->message(tr("Ready."));
}

void QcakeApp::slotViewStatusBar(bool toggle)
{
  statusBar()->message(tr("Toggle statusbar..."));
  ///////////////////////////////////////////////////////////////////
  //turn Statusbar on or off
  
  if (toggle == false)
  {
    statusBar()->hide();
  }
  else
  {
    statusBar()->show();
  }

  statusBar()->message(tr("Ready."));
}

void QcakeApp::slotViewFullScreenBar(bool toggle)
{
  if (toggle == false)
  {
      viewToolBar->setOn(true);
      viewStatusBar->setOn(true);
      this->showNormal ();
  }
  else
  {
      viewToolBar->setOn(false);
      viewStatusBar->setOn(false);
      this->showFullScreen ();
  }  
  statusBar()->message(tr("Ready."));
}

void QcakeApp::slotWindowNewWindow()
{
  statusBar()->message(tr("Opening new document view..."));
	
  KCakeView* m = (KCakeView*) pWorkspace->activeWindow();
  if ( m ){
    QcakeDoc* doc = m->getDocument();
    createClient(doc);
  }

  statusBar()->message(tr("Ready."));
}

void QcakeApp::slotHelpAbout()
{
  QPixmap wizardIcon, abouticon;

  abouticon = QPixmap(qcake);
 /*
  QMessageBox::about(this,tr("About..."),
                     tr("QCake\n Version " QCAKEVERSION
                     "\n(c) 2004-2005 by"
                     "\n    Krippel Harald"
                     "\n    Roland Schwartz"
                     "\n    Harald Leibrecht"
                     "\n    David Kuderer"
                     "\n    Macus Anderle"));
 */
  QMessageBox mb( this,"Application Name");
  mb.setIconPixmap(abouticon); 
  mb.exec();
}

void QcakeApp::slotStatusHelpMsg(const QString &text)
{
  ///////////////////////////////////////////////////////////////////
  // change status message of whole statusbar temporary (text, msec)
  statusBar()->message(text, 2000);
}

void QcakeApp::windowMenuAboutToShow()
{
  pWindowMenu->clear();	
	windowNewWindow->addTo(pWindowMenu);
	windowCascade->addTo(pWindowMenu);
	windowTile->addTo(pWindowMenu);
	
  if ( pWorkspace->windowList().isEmpty() )
  {
    windowAction->setEnabled(false);
  }
  else
  {
    windowAction->setEnabled(true);
  }

  pWindowMenu->insertSeparator();

  QWidgetList windows = pWorkspace->windowList();
  for ( int i = 0; i < int(windows.count()); ++i )
  {
    int id = pWindowMenu->insertItem(QString("&%1 ").arg(i+1)+windows.at(i)->caption(), this, SLOT( windowMenuActivated( int ) ) );
    pWindowMenu->setItemParameter( id, i );
    pWindowMenu->setItemChecked( id, pWorkspace->activeWindow() == windows.at(i) );
  }
}

void QcakeApp::windowMenuActivated( int id )
{
  QWidget* w = pWorkspace->windowList().at( id );
  if ( w )
    w->setFocus();
}

void QcakeApp::slotToolsEditCam()
{
  statusBar()->message(tr("move around the Editor Camera "));

  g_mouse_mode=3;

  statusBar()->message(tr("Ready."));
}

void QcakeApp::slotToolsTranslate()
{
  statusBar()->message(tr("Translate Mouse-Mode"));

  g_mouse_mode=0;

  statusBar()->message(tr("Ready."));
}
void QcakeApp::slotToolsRotate()
{
  statusBar()->message(tr("Rotate Mouse-Mode"));

  g_mouse_mode=1;

  statusBar()->message(tr("Ready."));
}
void QcakeApp::slotToolsScale()
{
  statusBar()->message(tr("Scale Mouse-Mode"));

  g_mouse_mode=2;

  statusBar()->message(tr("Ready."));
}

void QcakeApp::slotToolsPlay()
{
  int a;

  statusBar()->message(tr("Play-Mode"));

  // compile the spl program
  for(a=0;a<MAX_SPL;a++)
  {
    if(gl_spl_obj[a] != NULL)
    {
      gl_spl_obj[a]->compile();
    }
  }

  g_playmode = 1;
  toolsStop->setEnabled(true);
  toolsPlay->setEnabled(false);

  statusBar()->message(tr("Ready."));
}

void QcakeApp::slotToolsStop()
{
  statusBar()->message(tr("Play-Stop"));

  g_playmode = 0;
  toolsPlay->setEnabled(true);
  toolsStop->setEnabled(false);
  if(g_gl != NULL)
  {
    g_gl->StopSound();
  }
  statusBar()->message(tr("Ready."));
}

void QcakeApp::slotToolsReset()
{
  statusBar()->message(tr("Reset-Mode"));

  g_playmode_reset=1;

      if (g_tmpItem != NULL && g_gl != NULL)    // only it exist !!
      {
         g_gl->switchScene(g_tmpItem);
      }

  statusBar()->message(tr("Ready."));
}

void QcakeApp::writeSettings()
{
    QSettings settings;
    settings.setPath("qcake.org", "Game-Designer");
    settings.beginGroup("/Designer");
    settings.writeEntry("/geometry/x", x());
    settings.writeEntry("/geometry/y", y());
    settings.writeEntry("/geometry/width", width());
    settings.writeEntry("/geometry/height", height());
    settings.writeEntry("/recentFiles", recentFiles);
    settings.endGroup();
}

void QcakeApp::readSettings()
{
    QSettings settings;
    settings.setPath("qcake.org", "Game-Designer");
    settings.beginGroup("/Designer");

    int x = settings.readNumEntry("/geometry/x", 200);
    int y = settings.readNumEntry("/geometry/y", 200);
    int w = settings.readNumEntry("/geometry/width", 400);
    int h = settings.readNumEntry("/geometry/height", 400);
    move(x, y);
    resize(w, h);

    recentFiles = settings.readListEntry("/recentFiles");
    updateRecentFileItems();

    settings.endGroup();
}

QString QcakeApp::strippedName(const QString &fullFileName)
{
    return QFileInfo(fullFileName).fileName();
}

void QcakeApp::updateRecentFileItems()
{
    while ((int)recentFiles.size() > MaxRecentFiles)
        recentFiles.pop_back();

    for (int i = 0; i < (int)recentFiles.size(); ++i) {
        QString text = tr("&%1 %2")
                       .arg(i + 1)
                       .arg(strippedName(recentFiles[i]));
        if (recentFileIds[i] == -1) {
            if (i == 0)
                pFileMenu->insertSeparator(pFileMenu->count() - 2);
            recentFileIds[i] =
                    pFileMenu->insertItem(text, this,
                                         SLOT(openRecentFile(int)),
                                         0, -1,
                                         pFileMenu->count() - 2);
            pFileMenu->setItemParameter(recentFileIds[i], i);
        } else {
            pFileMenu->changeItem(recentFileIds[i], text);
        }
    }
}


void QcakeApp::openRecentFile(int param)
{
//    if (maybeSave())
//        loadFile(recentFiles[param]);
  QString fileName = recentFiles[param];
  if (!fileName.isEmpty())
  {
     openDocumentFile(fileName);
     recentFiles.remove(fileName);
     recentFiles.push_front(fileName);
     updateRecentFileItems();

     fileClose->setEnabled(true);
     fileSave->setEnabled(true);
     fileSaveAs->setEnabled(true);
  }
}

