templatemanagementdialog.cpp
00001 /******************************************************************************* 00002 ** 00003 ** Filename : templatemanagementdialog.cpp 00004 ** Created on : 05 June, 2005 00005 ** Copyright : (c) 2005 Till Adam 00006 ** Email : <adam@kde.org> 00007 ** 00008 *******************************************************************************/ 00009 00010 /******************************************************************************* 00011 ** 00012 ** This program is free software; you can redistribute it and/or modify 00013 ** it under the terms of the GNU General Public License as published by 00014 ** the Free Software Foundation; either version 2 of the License, or 00015 ** (at your option) any later version. 00016 ** 00017 ** It is distributed in the hope that it will be useful, but 00018 ** WITHOUT ANY WARRANTY; without even the implied warranty of 00019 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00020 ** General Public License for more details. 00021 ** 00022 ** You should have received a copy of the GNU General Public License 00023 ** along with this program; if not, write to the Free Software 00024 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00025 ** 00026 ** In addition, as a special exception, the copyright holders give 00027 ** permission to link the code of this program with any edition of 00028 ** the TQt library by Trolltech AS, Norway (or with modified versions 00029 ** of TQt that use the same license as TQt), and distribute linked 00030 ** combinations including the two. You must obey the GNU General 00031 ** Public License in all respects for all of the code used other than 00032 ** TQt. If you modify this file, you may extend this exception to 00033 ** your version of the file, but you are not obligated to do so. If 00034 ** you do not wish to do so, delete this exception statement from 00035 ** your version. 00036 ** 00037 *******************************************************************************/ 00038 #include "templatemanagementdialog.h" 00039 00040 #include <tqstringlist.h> 00041 #include <tqtimer.h> 00042 00043 #include <kpushbutton.h> 00044 #include <kinputdialog.h> 00045 #include <klocale.h> 00046 #include <kmessagebox.h> 00047 00048 TemplateManagementDialog::TemplateManagementDialog(TQWidget *parent, const TQStringList &templates ) 00049 :KDialogBase( parent, "template_management_dialog", true, 00050 i18n("Manage Templates"), Ok|Cancel, Ok, true , i18n("Apply Template")), 00051 m_templates( templates ), m_newTemplate( TQString() ), m_changed( false ) 00052 { 00053 m_base = new TemplateManagementDialog_base( this, "template_management_dialog_base" ); 00054 setMainWidget( m_base ); 00055 connect( m_base->m_buttonAdd, TQT_SIGNAL( clicked() ), 00056 TQT_SLOT( slotAddTemplate() ) ); 00057 connect( m_base->m_buttonDelete, TQT_SIGNAL( clicked() ), 00058 TQT_SLOT( slotDeleteTemplate() ) ); 00059 m_base->m_listBox->insertStringList( m_templates ); 00060 connect( m_base->m_listBox, TQT_SIGNAL( selectionChanged( TQListBoxItem * ) ), 00061 TQT_SLOT( slotUpdateDeleteButton( TQListBoxItem * ) ) ); 00062 connect( m_base->m_buttonApply, TQT_SIGNAL( clicked() ), 00063 TQT_SLOT( slotApplyTemplate() ) ); 00064 00065 } 00066 00067 void TemplateManagementDialog::slotAddTemplate() 00068 { 00069 bool ok; 00070 bool duplicate = false; 00071 const TQString newTemplate = KInputDialog::getText( i18n("Template Name"), 00072 i18n("Please enter a name for the new template:"), 00073 i18n("New Template"), &ok ); 00074 if ( newTemplate.isEmpty() || !ok ) return; 00075 if ( m_templates.find( newTemplate) != m_templates.end() ) { 00076 int rc = KMessageBox::warningContinueCancel( this, i18n("A template with that name already exists, do you want to overwrite it?."), i18n("Duplicate Template Name"), i18n("Overwrite")); 00077 if ( rc == KMessageBox::Cancel ) { 00078 TQTimer::singleShot(0, this, TQT_SLOT( slotAddTemplate() ) ); 00079 return; 00080 } 00081 duplicate = true; 00082 } 00083 if ( !duplicate ) { 00084 m_templates.append( newTemplate ); 00085 m_base->m_listBox->clear(); 00086 m_base->m_listBox->insertStringList( m_templates ); 00087 } 00088 m_newTemplate = newTemplate; 00089 m_changed = true; 00090 // From this point on we need to keep the original event around until the user has 00091 // closed the dialog, applying a template would make little sense 00092 m_base->m_buttonApply->setEnabled( false ); 00093 // neither does adding it again 00094 m_base->m_buttonAdd->setEnabled( false ); 00095 } 00096 00097 void TemplateManagementDialog::slotDeleteTemplate() 00098 { 00099 TQListBoxItem *const item = m_base->m_listBox->selectedItem(); 00100 if ( !item ) return; // can't happen (TM) 00101 unsigned int current = m_base->m_listBox->index(item); 00102 m_templates.remove( item->text() ); 00103 m_base->m_listBox->removeItem( m_base->m_listBox->currentItem() ); 00104 m_changed = true; 00105 m_base->m_listBox->setSelected(TQMAX(current -1, 0), true); 00106 } 00107 00108 void TemplateManagementDialog::slotUpdateDeleteButton( TQListBoxItem *item ) 00109 { 00110 m_base->m_buttonDelete->setEnabled( item != 0 ); 00111 } 00112 00113 void TemplateManagementDialog::slotApplyTemplate() 00114 { 00115 // Once the user has applied the current template to the event, it makes no sense to add it again 00116 m_base->m_buttonAdd->setEnabled( false ); 00117 const TQString &cur = m_base->m_listBox->currentText(); 00118 if ( !cur.isEmpty() && cur != m_newTemplate ) 00119 emit loadTemplate( cur ); 00120 } 00121 00122 void TemplateManagementDialog::slotOk() 00123 { 00124 // failure is not an option *cough* 00125 if ( !m_newTemplate.isEmpty() ) 00126 emit saveTemplate( m_newTemplate ); 00127 if ( m_changed ) 00128 emit templatesChanged( m_templates ); 00129 KDialogBase::slotOk(); 00130 } 00131 00132 00133 #include "templatemanagementdialog.moc"