/***************************************************************************
                          NewProject.cpp  -  description
                             -------------------
    begin                : ven nov 28 2003
    copyright            : (C) 2003 by Alain Gibaud
    email                : alain.gibaud@univ-valenciennes.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 "NewProject.h"
#include "FNU.h"
#include "PikMain.h"

#include <qvbox.h>
#include <qhbox.h>
#include <qgroupbox.h>
#include <qlabel.h>
#include <qlineedit.h>
#include <qlineedit.h>
#include <qtextedit.h>
#include <qpushbutton.h>
#include <qgrid.h>
#include <qiconset.h>
#include <qdir.h>
#include <qurl.h>
#include <qmessagebox.h>
#include <qvgroupbox.h>
#include <qcombobox.h>
#include <qcheckbox.h>

#include <klocale.h>
#include <kiconloader.h>
#include <kfiledialog.h>
#include <kapplication.h>


#include <iostream>
using namespace std ;


NewProject::NewProject( ) : QMainWindow(0,"new project widget", Qt::WDestructiveClose )
{

   // load standard KDE "open" icon
    KIconLoader loader ;
    QIconSet iconset = loader.loadIcon("fileopen", KIcon::Toolbar) ;
    
    setCaption(i18n("Project creation")) ;
    
    QVBox *vb = new QVBox( this) ;
    vb->setSpacing(8) ;
    vb->setFrameStyle( QFrame::Panel | QFrame::Sunken );
    vb->setLineWidth( 2 );
    vb->setMargin(8) ;
    setCentralWidget(vb) ;

    QVGroupBox *vgb =   new QVGroupBox(i18n("Project informations"), vb) ;
    vgb->setInsideSpacing(8) ;
    
    QGrid *group = new QGrid(3,vgb) ;
    group->setSpacing(10) ;

    new QLabel ( i18n("Name") , group) ;
    project_name = new  QLineEdit(group) ;
    connect(project_name, SIGNAL(textChanged(const QString &)),
    	this, SLOT(slotNameChanged(const QString &))) ;
    new  QHBox(group) ;
    
    new QLabel ( i18n("Directory") , group) ;
    project_dir = new  QLineEdit( group) ;
    QPushButton *bdir = new  QPushButton(iconset,"", group) ;
    connect(bdir,SIGNAL(clicked()), this, SLOT( slotSelectDir())) ;

    new QLabel ( i18n("Type") , group) ;
    project_type = new  QComboBox(false, group) ;
    project_type->insertItem(i18n("Asm")) ;
    project_type->insertItem(i18n("Cpik for p18xxx")) ;
    new  QHBox(group) ;


    
    new QLabel ( i18n("Configure now") , group) ;
    config_now = new QCheckBox( group) ;
    config_now->setChecked(true) ;
    new  QHBox(group) ;

    QHBox *boxbutton = new QHBox(vb) ;
    boxbutton->setSpacing(10) ;
    new QHBox(boxbutton) ;
    
    QPushButton * create = new QPushButton(i18n("Create"), boxbutton) ;
    connect(create, SIGNAL(clicked()), this, SLOT(slotCreate())) ;
    
    QPushButton * cancel = new QPushButton(i18n("Cancel"), boxbutton) ;
    connect(cancel, SIGNAL(clicked()), this, SLOT(slotCancel())) ;
    
    setMinimumWidth(500) ;
    show() ;
}
NewProject::~NewProject()
{
}
/** This slot must be activated from the browse button and update
the project_dir lineedit. */
void NewProject::slotSelectDir()
{
    QString dir(KFileDialog::getExistingDirectory(QString::null,0,
        i18n("Select project directory") )) ;
    if( !dir.isEmpty())
    {
        project_dir->setText(dir) ;
        slotNameChanged(project_name->text()) ;
    }
}

/** No descriptions */
void NewProject::slotCancel()
{
  close(true) ;
}
/** No descriptions */
void NewProject::slotNameChanged(const QString &name)
{
	QString dir(   project_dir->text()  ) ;

	if( dir == "") dir=QDir::homeDirPath() + "/" ;

	project_dir->setText(FNU::path(dir) + "/" + name) ;
}
	
/** No descriptions */
void NewProject::slotCreate()
{
  if( createProjectEntry() && config_now->isChecked() )
  {
    // project entry created, do more configuration.
    PikMain *m = dynamic_cast<PikMain *>(KApplication::kApplication()->mainWidget()) ;
    if(m) m->projectWid()->slotEditProject() ;
  }
  slotCancel() ;
}

/** No descriptions */
bool NewProject::createProjectEntry()
{
  if( project_name->text() == "" || project_dir->text() == "" )
  {
        QMessageBox::warning(this,i18n("Sorry"),
                             i18n("<b>Directory</b> and <b>Project name</b>"
                             "  must be provided"),
                             QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton) ;


       return false;
  }

  QString type ;
  int t =  project_type->currentItem() ;

  if(t == 0) type = "asm" ;
  else if( t == 1 ) type = "C" ;
  else return false;

  emit projectData( project_name->text() , project_dir->text(), type )  ;

  return true ;

}
