2007-12-01 10:47:15 +00:00
/*
2009-04-03 19:30:25 +00:00
Copyright 2006 - 2009 Xavier Guerrin
2007-12-01 10:47:15 +00:00
This file is part of QElectroTech .
QElectroTech 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 .
QElectroTech 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 QElectroTech . If not , see < http : //www.gnu.org/licenses/>.
*/
2007-04-04 02:13:14 +00:00
# include "newelementwizard.h"
2009-04-03 19:30:25 +00:00
# include "elementscategory.h"
2007-04-04 02:13:14 +00:00
# include "elementscategorieswidget.h"
2007-04-06 02:47:25 +00:00
# include "elementscategorieslist.h"
2007-04-13 11:50:16 +00:00
# include "nameslistwidget.h"
2007-06-30 17:41:07 +00:00
# include "orientationsetwidget.h"
2007-08-18 22:59:10 +00:00
# include "hotspoteditor.h"
2007-04-07 19:25:38 +00:00
# include "element.h"
2007-08-25 03:43:05 +00:00
# include "qetelementeditor.h"
2007-12-23 01:15:16 +00:00
# include "qet.h"
2009-04-03 19:30:25 +00:00
# include "qetapp.h"
# include "elementscollectionitem.h"
# include "qfilenameedit.h"
2007-04-04 02:13:14 +00:00
2007-04-07 19:25:38 +00:00
/**
Constructeur
@ param parent QWidget parent de ce dialogue
@ param f flags pour le dialogue
*/
2009-04-03 19:30:25 +00:00
NewElementWizard : : NewElementWizard ( QWidget * parent , Qt : : WindowFlags f ) :
QWizard ( parent , f ) ,
chosen_category ( 0 )
{
2007-09-07 08:42:36 +00:00
setPixmap ( LogoPixmap , QPixmap ( " :/ico/qelectrotech.png " ) . scaled ( 64 , 64 , Qt : : KeepAspectRatio , Qt : : SmoothTransformation ) ) ;
2009-04-03 19:30:25 +00:00
setWindowTitle ( tr ( " Cr \351 er un nouvel \351 l \351 ment : Assistant " , " window title " ) ) ;
2007-09-07 08:42:36 +00:00
setButtonText ( QWizard : : NextButton , tr ( " &Suivant > " ) ) ;
addPage ( buildStep1 ( ) ) ;
addPage ( buildStep2 ( ) ) ;
addPage ( buildStep3 ( ) ) ;
addPage ( buildStep4 ( ) ) ;
addPage ( buildStep5 ( ) ) ;
setFixedSize ( 705 , 325 ) ;
2007-04-04 02:13:14 +00:00
}
2007-04-07 19:25:38 +00:00
/**
Destructeur
*/
2007-04-04 02:13:14 +00:00
NewElementWizard : : ~ NewElementWizard ( ) {
}
2009-04-03 19:30:25 +00:00
/**
@ return la categorie parente selectionnee , ou 0 si celle - ci n ' a pas encore
ete choisie .
*/
ElementsCategory * NewElementWizard : : selectedCategory ( ) const {
return ( chosen_category ) ;
}
/**
@ param category Categorie d ' elements dans laquelle le nouvel element sera
place
@ return true si ce choix est possible et a ete pris en compte , false sinon
*/
bool NewElementWizard : : preselectCategory ( ElementsCategory * category ) {
// verifie si la categorie est utilisable
if ( ! category | | ! category - > exists ( ) | | ! category - > isWritable ( ) ) {
return ( false ) ;
}
// selectionne la categorie ainsi demandee dans la liste
if ( categories_list - > elementsCategoriesList ( ) . selectLocation ( category - > location ( ) ) ) {
chosen_category = category ;
return ( true ) ;
}
return ( false ) ;
}
2007-04-06 02:47:25 +00:00
/**
Met en place l ' etape 1 : Categorie
*/
2007-09-07 08:42:36 +00:00
QWizardPage * NewElementWizard : : buildStep1 ( ) {
QWizardPage * page = new QWizardPage ( ) ;
page - > setProperty ( " WizardState " , Category ) ;
2009-04-03 19:30:25 +00:00
page - > setTitle ( tr ( " \311 tape 1/5 : Cat \351 gorie parente " , " wizard page title " ) ) ;
page - > setSubTitle ( tr ( " S \351 lectionnez une cat \351 gorie dans laquelle enregistrer le nouvel \351 l \351 ment. " , " wizard page subtitle " ) ) ;
2007-09-07 08:42:36 +00:00
QVBoxLayout * layout = new QVBoxLayout ( ) ;
2007-04-06 02:47:25 +00:00
categories_list = new ElementsCategoriesWidget ( ) ;
2007-09-07 08:42:36 +00:00
layout - > addWidget ( categories_list ) ;
page - > setLayout ( layout ) ;
return ( page ) ;
2007-04-06 02:47:25 +00:00
}
/**
Met en place l ' etape 2 : Nom du fichier
*/
2007-09-07 08:42:36 +00:00
QWizardPage * NewElementWizard : : buildStep2 ( ) {
QWizardPage * page = new QWizardPage ( ) ;
page - > setProperty ( " WizardState " , Filename ) ;
2009-04-03 19:30:25 +00:00
page - > setTitle ( tr ( " \311 tape 2/5 : Nom du fichier " , " wizard page title " ) ) ;
page - > setSubTitle ( tr ( " Indiquez le nom du fichier dans lequel enregistrer le nouvel \351 l \351 ment. " , " wizard page subtitle " ) ) ;
2007-09-07 08:42:36 +00:00
QVBoxLayout * layout = new QVBoxLayout ( ) ;
2009-04-03 19:30:25 +00:00
qle_filename = new QFileNameEdit ( tr ( " nouvel_element " ) ) ;
2007-09-07 08:42:36 +00:00
qle_filename - > selectAll ( ) ;
2007-04-07 19:25:38 +00:00
QLabel * explication2 = new QLabel ( tr ( " Vous n' \352 tes pas oblig \351 de pr \351 ciser l'extension *.elmt. Elle sera ajout \351 e automatiquement. " ) ) ;
explication2 - > setAlignment ( Qt : : AlignJustify | Qt : : AlignVCenter ) ;
explication2 - > setWordWrap ( true ) ;
2007-09-07 08:42:36 +00:00
layout - > addWidget ( qle_filename ) ;
layout - > addWidget ( explication2 ) ;
layout - > addSpacing ( 100 ) ;
page - > setLayout ( layout ) ;
return ( page ) ;
2007-04-06 02:47:25 +00:00
}
/**
Met en place l ' etape 3 : Noms de l ' element
*/
2007-09-07 08:42:36 +00:00
QWizardPage * NewElementWizard : : buildStep3 ( ) {
QWizardPage * page = new QWizardPage ( ) ;
page - > setProperty ( " WizardState " , Names ) ;
2009-04-03 19:30:25 +00:00
page - > setTitle ( tr ( " \311 tape 3/5 : Noms de l' \351 l \351 ment " , " wizard page title " ) ) ;
page - > setSubTitle ( tr ( " Indiquez le ou les noms de l' \351 l \351 ment. " , " wizard page subtitle " ) ) ;
2007-09-07 08:42:36 +00:00
QVBoxLayout * layout = new QVBoxLayout ( ) ;
2007-04-13 11:50:16 +00:00
element_names = new NamesListWidget ( ) ;
2007-04-13 15:48:00 +00:00
NamesList hash_name ;
2009-04-03 19:30:25 +00:00
hash_name . addName ( QLocale : : system ( ) . name ( ) . left ( 2 ) , tr ( " Nom du nouvel \351 l \351 ment " , " default name when creating a new element " ) ) ;
2007-04-07 02:37:57 +00:00
element_names - > setNames ( hash_name ) ;
2007-09-07 08:42:36 +00:00
layout - > addWidget ( element_names ) ;
page - > setLayout ( layout ) ;
return ( page ) ;
2007-04-06 02:47:25 +00:00
}
/**
Met en place l ' etape 4 : Dimensions et Hotspot
*/
2007-09-07 08:42:36 +00:00
QWizardPage * NewElementWizard : : buildStep4 ( ) {
QWizardPage * page = new QWizardPage ( ) ;
page - > setProperty ( " WizardState " , Dimensions ) ;
2009-04-03 19:30:25 +00:00
page - > setTitle ( tr ( " \311 tape 4/5 : Dimensions et point de saisie " , " wizard page title " ) ) ;
page - > setSubTitle ( tr ( " Saisissez les dimensions du nouvel \351 l \351 ment ainsi que la position du hotspot (point de saisie de l' \351 l \351 ment \340 la souris) en consid \351 rant que l' \351 l \351 ment est dans son orientation par d \351 faut. " , " wizard page subtitle " ) ) ;
2007-09-07 08:42:36 +00:00
QVBoxLayout * layout = new QVBoxLayout ( ) ;
2007-08-18 22:59:10 +00:00
hotspot_editor = new HotspotEditor ( ) ;
2007-09-07 08:42:36 +00:00
layout - > addWidget ( hotspot_editor , 0 ) ;
layout - > setSpacing ( 0 ) ;
page - > setLayout ( layout ) ;
return ( page ) ;
2007-04-06 02:47:25 +00:00
}
/**
Met en place l ' etape 5 : orientations possibles pour le nouvel element
*/
2007-09-07 08:42:36 +00:00
QWizardPage * NewElementWizard : : buildStep5 ( ) {
QWizardPage * page = new QWizardPage ( ) ;
page - > setProperty ( " WizardState " , Orientations ) ;
2009-04-03 19:30:25 +00:00
page - > setTitle ( tr ( " \311 tape 5/5 : Orientations " , " wizard page title " ) ) ;
page - > setSubTitle ( tr ( " Indiquez les orientations possibles pour le nouvel \351 l \351 ment. " , " wizard page subtitle " ) ) ;
2007-09-07 08:42:36 +00:00
QVBoxLayout * layout = new QVBoxLayout ( ) ;
2007-04-07 19:25:38 +00:00
2007-06-30 17:41:07 +00:00
orientation_set = new OrientationSetWidget ( ) ;
2007-09-07 08:42:36 +00:00
layout - > addWidget ( orientation_set ) ;
layout - > addSpacing ( 25 ) ;
2007-04-07 19:25:38 +00:00
2007-09-07 08:42:36 +00:00
page - > setLayout ( layout ) ;
return ( page ) ;
}
/// @return true si l'etape en cours est validee, false sinon
bool NewElementWizard : : validateCurrentPage ( ) {
WizardState wizard_state = static_cast < WizardState > ( currentPage ( ) - > property ( " WizardState " ) . toInt ( ) ) ;
if ( wizard_state = = Category ) return ( validStep1 ( ) ) ;
else if ( wizard_state = = Filename ) return ( validStep2 ( ) ) ;
else if ( wizard_state = = Names ) return ( element_names - > checkOneName ( ) ) ; // il doit y avoir au moins un nom
else if ( wizard_state = = Orientations ) {
createNewElement ( ) ;
return ( true ) ;
}
else return ( true ) ;
2007-04-06 02:47:25 +00:00
}
2007-04-07 19:25:38 +00:00
/**
Valide l ' etape 1
@ return true si l ' etape est validee , false sinon
*/
2007-04-06 02:47:25 +00:00
bool NewElementWizard : : validStep1 ( ) {
// il doit y avoir une categorie selectionnee
2009-04-03 19:30:25 +00:00
bool step1_ok = false ;
ElementsLocation selected_location = categories_list - > elementsCategoriesList ( ) . selectedLocation ( ) ;
if ( ElementsCollectionItem * collection_item = QETApp : : collectionItem ( selected_location , false ) ) {
if ( collection_item - > isCategory ( ) ) {
chosen_category = qobject_cast < ElementsCategory * > ( collection_item ) ;
step1_ok = chosen_category ;
}
}
2007-04-06 02:47:25 +00:00
if ( ! step1_ok ) {
QMessageBox : : critical (
this ,
2009-04-03 19:30:25 +00:00
tr ( " Erreur " , " message box title " ) ,
tr ( " Vous devez s \351 lectionner une cat \351 gorie. " , " message box content " )
2007-04-06 02:47:25 +00:00
) ;
}
return ( step1_ok ) ;
}
2007-04-07 19:25:38 +00:00
/**
Valide l ' etape 2
@ return true si l ' etape est validee , false sinon
*/
2007-04-06 02:47:25 +00:00
bool NewElementWizard : : validStep2 ( ) {
2009-04-03 19:30:25 +00:00
// il doit y avoir une categorie selectionnee
if ( ! chosen_category ) return ( false ) ;
2007-04-06 02:47:25 +00:00
QString file_name = qle_filename - > text ( ) ;
// un nom doit avoir ete entre
2007-10-21 12:53:57 +00:00
if ( file_name . isEmpty ( ) ) {
2007-04-06 02:47:25 +00:00
QMessageBox : : critical (
this ,
2009-04-03 19:30:25 +00:00
tr ( " Erreur " , " message box title " ) ,
tr ( " Vous devez entrer un nom de fichier " , " message box content " )
2007-04-06 02:47:25 +00:00
) ;
return ( false ) ;
}
2007-04-07 02:37:57 +00:00
if ( ! file_name . endsWith ( " .elmt " ) ) file_name + = " .elmt " ;
2007-12-23 01:15:16 +00:00
// le nom de fichier contient peut etre des caracteres interdits
if ( QET : : containsForbiddenCharacters ( file_name ) ) {
QMessageBox : : critical (
this ,
2009-04-03 19:30:25 +00:00
tr ( " Erreur " , " message box title " ) ,
tr ( " Merci de ne pas utiliser les caract \350 res suivants : \\ / : * ? \" < > | " , " message box content " )
2007-12-23 01:15:16 +00:00
) ;
return ( false ) ;
}
2007-04-06 02:47:25 +00:00
// le fichier existe peut etre deja
2009-04-03 19:30:25 +00:00
if ( chosen_category - > element ( file_name ) ) {
2007-04-06 02:47:25 +00:00
QMessageBox : : StandardButton answer = QMessageBox : : question (
this ,
" \311 craser le fichier ? " ,
" Le fichier existe d \351 j \340 . Souhaitez-vous l' \351 craser ? " ,
QMessageBox : : Yes | QMessageBox : : No | QMessageBox : : Cancel ,
QMessageBox : : No
) ;
return ( answer = = QMessageBox : : Yes ) ;
2007-04-04 02:13:14 +00:00
}
2007-04-06 02:47:25 +00:00
2009-04-03 19:30:25 +00:00
chosen_file = file_name ;
2007-04-06 02:47:25 +00:00
return ( true ) ;
2007-04-04 02:13:14 +00:00
}
2007-04-06 02:47:25 +00:00
2007-04-07 19:25:38 +00:00
/**
Cree le nouvel element
*/
void NewElementWizard : : createNewElement ( ) {
2007-08-25 03:43:05 +00:00
QETElementEditor * edit_new_element = new QETElementEditor ( parentWidget ( ) ) ;
2007-08-18 22:59:10 +00:00
edit_new_element - > setSize ( hotspot_editor - > elementSize ( ) ) ;
edit_new_element - > setHotspot ( hotspot_editor - > hotspot ( ) ) ;
2007-06-30 17:41:07 +00:00
edit_new_element - > setNames ( element_names - > names ( ) ) ;
edit_new_element - > setOrientations ( orientation_set - > orientationSet ( ) ) ;
2009-04-03 19:30:25 +00:00
ElementsLocation new_element_location = chosen_category - > location ( ) ;
new_element_location . addToPath ( chosen_file ) ;
edit_new_element - > setLocation ( new_element_location ) ;
2007-06-30 17:41:07 +00:00
edit_new_element - > show ( ) ;
2007-04-07 19:25:38 +00:00
}