2007-12-01 10:47:15 +00:00
/*
2011-03-02 00:16:40 +00:00
Copyright 2006 - 2011 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-05 01:13:14 +00:00
# include "elementscategoryeditor.h"
2009-04-03 19:30:25 +00:00
# include "elementscollection.h"
2007-04-05 01:13:14 +00:00
# include "elementscategory.h"
2007-04-13 11:50:16 +00:00
# include "nameslistwidget.h"
2007-12-23 01:15:16 +00:00
# include "qet.h"
2009-04-03 19:30:25 +00:00
# include "qetapp.h"
# include "qfilenameedit.h"
2009-08-09 16:02:14 +00:00
# include "qetmessagebox.h"
2007-04-05 01:13:14 +00:00
/**
Constructeur fournissant un dialogue d ' edition de categorie .
@ param category_path Chemin de la categorie a editer ou de la categorie parente en cas de creation
@ param edit booleen a true pour le mode edition , a false pour le mode creation
@ param parent QWidget parent du dialogue
*/
2009-04-03 19:30:25 +00:00
ElementsCategoryEditor : : ElementsCategoryEditor ( const ElementsLocation & category_path , bool edit , QWidget * parent ) :
QDialog ( parent ) ,
mode_edit ( edit )
{
2007-04-05 01:13:14 +00:00
// dialogue basique
buildDialog ( ) ;
2009-04-03 19:30:25 +00:00
// recupere la categorie a editer
ElementsCollectionItem * category_item = QETApp : : collectionItem ( category_path ) ;
if ( category_item ) category_item = category_item - > toCategory ( ) ;
if ( ! category_item | | ! category_item - > isCategory ( ) ) {
2009-08-09 16:02:14 +00:00
QET : : MessageBox : : warning (
2009-04-03 19:30:25 +00:00
this ,
tr ( " Cat \351 gorie inexistante " , " message box title " ) ,
tr ( " La cat \351 gorie demand \351 e n'existe pas. Abandon. " , " message box content " )
) ;
return ;
} else {
category = category_item - > toPureCategory ( ) ;
}
2007-04-05 01:13:14 +00:00
if ( mode_edit ) {
2009-04-03 19:30:25 +00:00
setWindowTitle ( tr ( " \311 diter une cat \351 gorie " , " window title " ) ) ;
2007-04-05 01:13:14 +00:00
connect ( buttons , SIGNAL ( accepted ( ) ) , this , SLOT ( acceptUpdate ( ) ) ) ;
// edition de categorie = affichage des noms deja existants
2007-04-13 15:48:00 +00:00
names_list - > setNames ( category - > categoryNames ( ) ) ;
2009-04-03 19:30:25 +00:00
internal_name_ - > setText ( category - > pathName ( ) ) ;
internal_name_ - > setReadOnly ( true ) ;
2007-04-05 01:13:14 +00:00
} else {
2009-04-03 19:30:25 +00:00
setWindowTitle ( tr ( " Cr \351 er une nouvelle cat \351 gorie " , " window title " ) ) ;
2007-04-05 01:13:14 +00:00
connect ( buttons , SIGNAL ( accepted ( ) ) , this , SLOT ( acceptCreation ( ) ) ) ;
// nouvelle categorie = une ligne pre-machee
2007-04-13 15:48:00 +00:00
NamesList cat_names ;
2009-04-03 19:30:25 +00:00
cat_names . addName ( QLocale : : system ( ) . name ( ) . left ( 2 ) , tr ( " Nom de la nouvelle cat \351 gorie " , " default name when creating a new category " ) ) ;
2007-04-07 02:37:57 +00:00
names_list - > setNames ( cat_names ) ;
2007-04-05 01:13:14 +00:00
}
2007-06-30 17:41:07 +00:00
// gestion de la lecture seule
if ( ! category - > isWritable ( ) ) {
2009-08-09 16:02:14 +00:00
QET : : MessageBox : : warning (
2007-06-30 17:41:07 +00:00
this ,
2009-04-03 19:30:25 +00:00
tr ( " \311 dition en lecture seule " , " message box title " ) ,
tr ( " Vous n'avez pas les privil \350 ges n \351 cessaires pour modifier cette cat \351 gorie. Elle sera donc ouverte en lecture seule. " , " message box content " )
2007-06-30 17:41:07 +00:00
) ;
names_list - > setReadOnly ( true ) ;
2009-04-03 19:30:25 +00:00
internal_name_ - > setReadOnly ( true ) ;
2007-06-30 17:41:07 +00:00
}
2007-04-05 01:13:14 +00:00
}
/**
Destructeur
*/
ElementsCategoryEditor : : ~ ElementsCategoryEditor ( ) {
}
/**
Bases du dialogue de creation / edition
*/
void ElementsCategoryEditor : : buildDialog ( ) {
QVBoxLayout * editor_layout = new QVBoxLayout ( ) ;
setLayout ( editor_layout ) ;
2007-04-13 11:50:16 +00:00
names_list = new NamesListWidget ( ) ;
2009-04-03 19:30:25 +00:00
internal_name_label_ = new QLabel ( tr ( " Nom interne : " ) ) ;
internal_name_ = new QFileNameEdit ( ) ;
2007-04-05 18:02:40 +00:00
2007-04-05 01:13:14 +00:00
buttons = new QDialogButtonBox ( QDialogButtonBox : : Ok | QDialogButtonBox : : Cancel ) ;
connect ( buttons , SIGNAL ( rejected ( ) ) , this , SLOT ( reject ( ) ) ) ;
2009-04-03 19:30:25 +00:00
QHBoxLayout * internal_name_layout = new QHBoxLayout ( ) ;
internal_name_layout - > addWidget ( internal_name_label_ ) ;
internal_name_layout - > addWidget ( internal_name_ ) ;
editor_layout - > addLayout ( internal_name_layout ) ;
2007-04-05 01:13:14 +00:00
editor_layout - > addWidget ( new QLabel ( tr ( " Vous pouvez sp \351 cifier un nom par langue pour la cat \351 gorie. " ) ) ) ;
2007-04-07 02:37:57 +00:00
editor_layout - > addWidget ( names_list ) ;
2007-04-05 01:13:14 +00:00
editor_layout - > addWidget ( buttons ) ;
}
/**
Valide les donnees entrees par l ' utilisateur lors d ' une creation de
categorie
*/
void ElementsCategoryEditor : : acceptCreation ( ) {
2007-06-30 17:41:07 +00:00
if ( ! category - > isWritable ( ) ) QDialog : : accept ( ) ;
2007-04-05 01:13:14 +00:00
// il doit y avoir au moins un nom
2007-04-07 02:37:57 +00:00
if ( ! names_list - > checkOneName ( ) ) return ;
2007-04-05 01:13:14 +00:00
2009-04-03 19:30:25 +00:00
// exige un nom de dossier de la part de l'utilisateur
if ( ! internal_name_ - > isValid ( ) ) {
2009-08-09 16:02:14 +00:00
QET : : MessageBox : : critical (
2009-04-03 19:30:25 +00:00
this ,
tr ( " Nom interne manquant " , " message box title " ) ,
tr ( " Vous devez sp \351 cifier un nom interne. " , " message box content " )
) ;
return ;
}
QString dirname = internal_name_ - > text ( ) ;
// verifie que le nom interne n'est pas deja pris
if ( category - > category ( dirname ) ) {
2009-08-09 16:02:14 +00:00
QET : : MessageBox : : critical (
2009-04-03 19:30:25 +00:00
this ,
tr ( " Nom interne d \351 j \340 utilis \351 " , " message box title " ) ,
tr (
" Le nom interne que vous avez choisi est d \351 j \340 utilis \351 "
" par une cat \351 gorie existante. Veuillez en choisir un autre. " ,
" message box content "
)
) ;
return ;
}
// cree la nouvelle categorie
ElementsCategory * new_category = category - > createCategory ( dirname ) ;
if ( ! new_category ) {
2009-08-09 16:02:14 +00:00
QET : : MessageBox : : critical (
2009-04-03 19:30:25 +00:00
this ,
tr ( " Erreur " , " message box title " ) ,
tr ( " Impossible de cr \351 er la cat \351 gorie " , " message box content " )
) ;
return ;
}
2007-04-05 01:13:14 +00:00
// chargement des noms
2007-04-13 15:48:00 +00:00
NamesList names = names_list - > names ( ) ;
foreach ( QString lang , names . langs ( ) ) {
2009-04-03 19:30:25 +00:00
new_category - > addName ( lang , names [ lang ] ) ;
2007-04-05 01:13:14 +00:00
}
2009-04-03 19:30:25 +00:00
// ecriture de la
if ( ! new_category - > write ( ) ) {
2009-08-09 16:02:14 +00:00
QET : : MessageBox : : critical (
2009-04-03 19:30:25 +00:00
this ,
tr ( " Erreur " , " message box title " ) ,
tr ( " Impossible d'enregistrer la cat \351 gorie " , " message box content " )
) ;
return ;
}
2007-04-05 01:13:14 +00:00
QDialog : : accept ( ) ;
}
/**
Valide les donnees entrees par l ' utilisateur lors d ' une modification de
categorie
*/
void ElementsCategoryEditor : : acceptUpdate ( ) {
2009-04-03 19:30:25 +00:00
2007-06-30 17:41:07 +00:00
if ( ! category - > isWritable ( ) ) QDialog : : accept ( ) ;
2007-04-05 18:02:40 +00:00
// il doit y avoir au moins un nom
2007-04-07 02:37:57 +00:00
if ( ! names_list - > checkOneName ( ) ) return ;
2007-04-05 18:02:40 +00:00
// chargement des noms
category - > clearNames ( ) ;
2007-04-13 15:48:00 +00:00
NamesList names = names_list - > names ( ) ;
foreach ( QString lang , names . langs ( ) ) {
2007-04-07 02:37:57 +00:00
category - > addName ( lang , names [ lang ] ) ;
2007-04-05 18:02:40 +00:00
}
category - > write ( ) ;
QDialog : : accept ( ) ;
}