/***************************************************************************
                          ConfigCenter.cpp  -  description
                             -------------------
    begin                : mer oct 13 2004
    copyright            : (C) 2004 by Gibaud Alain
    email                : alain.gibaud@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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "ConfigCenter.h"
#include "ConfigWidget.h"
#include "AsmConfigWidget.h"
#include "PrgConfigWidget.h"
#include "GlobalConfigWidget.h"
#include "PikMain.h"

#include <qlistview.h>
#include <qwidgetstack.h>
#include <qsplitter.h>
#include <qtabwidget.h>
#include <qpushbutton.h>
#include <qhbox.h>
#include <qvbox.h>
#include <qmainwindow.h>
#include <qtoolbox.h>
#include <qheader.h>
#include <qlabel.h>
#include <qfont.h>
#include <qcolor.h>
#include <qvaluelist.h>

#include <klocale.h>

ConfigCenter::ConfigCenter( PikMain *main) :
    QMainWindow(0, 0, Qt::WDestructiveClose )
{

  // splitter
  splitv =  new QSplitter(this) ;
  splitv->setHandleWidth(5) ;
  splitv->setOrientation(Horizontal) ;
  splitv->setOpaqueResize(true) ;

  setCentralWidget(splitv) ;
  // add control tree
  QVBox *treebox = new QVBox(splitv) ;
  confView =    new  QListView(treebox) ;
  int firstcol = confView->addColumn(i18n("Configure"),200) ;
  confView->setColumnWidthMode(firstcol,QListView::Manual) ;
  treebox->setMinimumWidth(165) ;

  confView->setRootIsDecorated(false) ;
  confView->setItemMargin(8) ;
  confView->header()->hide() ;
  confView->setSelectionMode( /*QListView::NoSelection */QListView::Single) ;
  confView->setSorting(-1) ;  // no sort
  connect( confView , SIGNAL(currentChanged(QListViewItem *)), this, SLOT(raiseConfigWidget(QListViewItem *))) ;

  QVBox *vb = new QVBox(splitv) ;
  vb->setSpacing(7) ;
  vb->setMargin(5) ;

  QHBox *boxtitle = new QHBox(vb) ;
  image = new QLabel("",boxtitle) ;
  title = new QLabel("",boxtitle) ;
  new QHBox(boxtitle);

  title->setAlignment(Qt::AlignCenter) ;
  QFont f = title->font() ;
  f.setPointSize(14) ;
  title->setFont(f) ;
  title->setPaletteForegroundColor(QColor(0x7D,0x70,0x70)) ;


  wstack = new QWidgetStack(vb) ;

  //new QVBox(vb) ;
  //  Buttons
  QHBox *hboxbutton = new QHBox(vb) ;
  hboxbutton->setSpacing(20) ;

  QHBox *space = new QHBox(hboxbutton) ;
  okButton = new QPushButton(i18n("Ok"),hboxbutton) ;
  applyButton  = new QPushButton(i18n("Apply"),hboxbutton) ;
  cancelButton = new QPushButton(i18n("Quit"),hboxbutton) ;
  hboxbutton->setStretchFactor(space,1) ;
  hboxbutton->setStretchFactor(cancelButton,0) ;
  hboxbutton->setStretchFactor(applyButton,0) ;
  hboxbutton->setStretchFactor(okButton,0) ;

  connect(cancelButton,SIGNAL(clicked()) , this, SLOT(slotClose())) ;
  connect(okButton,SIGNAL(clicked()) , this, SLOT(slotOk())) ;
  connect(applyButton,SIGNAL(clicked()) , this, SLOT(slotApply())) ;


  // plug programmer configurators
  QValueVector<ProgrammerBase *>::iterator
  cf= main->programmersList().begin() ,
      cfend = main->programmersList().end() ;

  for( ; cf != cfend ; ++cf)
  {
    ConfigWidget *cw = (*cf)->configWidget() ;
    // some programmers doesn't need configurator
    if( cw) plugConfigWidget(cw) ;
  }

  plugConfigWidget(new AsmConfigWidget()) ;
  plugConfigWidget(new GlobalConfigWidget(main)) ;

  QListViewItem *first = confView->firstChild() ;
  raiseConfigWidget(first) ;

  setSizePolicy( QSizePolicy::Maximum,QSizePolicy::Maximum ); 
//	setFixedSize(sizeHint()) ;

  show() ;
}

ConfigCenter::~ConfigCenter()
{}

/** Insert a new config widget */
void ConfigCenter::plugConfigWidget(ConfigWidget *cw)
{

  int id = wstack->addWidget(cw) ;
  // wstack->raiseWidget(id) ;

  QListViewItem *conf =   new QListViewItem(confView,cw->configName(),QString::number(id)) ;
  conf->setPixmap(0, cw->icon() ) ;
  title->setText(cw->configTitle()) ;
  image->setPixmap(  cw->icon() ) ;
}
/** No descriptions */
void ConfigCenter::slotClose()
{
  close(true) ;
}
/** No descriptions */
void ConfigCenter::slotOk()
{
  if(wstack->visibleWidget())
  {
    ConfigWidget *ac =  dynamic_cast<ConfigWidget *>(wstack->visibleWidget()) ;
    if( ac) ac->doApply();
    slotClose() ;
  }
}
/** No descriptions */
void ConfigCenter::slotApply()
{
  if(wstack->visibleWidget())
  {
    ConfigWidget *ac =  dynamic_cast<ConfigWidget *>(wstack->visibleWidget()) ;
    if( ac) ac->doApply();
  }
}
/** No descriptions */
void ConfigCenter::raiseConfigWidget(QListViewItem *item)
{
  if( item == 0 ) return ;

  int id = item->text(1).toInt() ;
  item->setSelected(true) ;
  confView->setCurrentItem(item);

  wstack->raiseWidget(id) ;
  ConfigWidget *ac =  dynamic_cast<ConfigWidget *>(wstack->visibleWidget()) ;
  title->setText(ac->configTitle()) ;
  image->setPixmap(  ac->icon() ) ;
}
