korganizer
templatemanagementdialog.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "templatemanagementdialog.h"
00039
00040 #include <qstringlist.h>
00041 #include <qtimer.h>
00042
00043 #include <kpushbutton.h>
00044 #include <kinputdialog.h>
00045 #include <klocale.h>
00046 #include <kmessagebox.h>
00047
00048 TemplateManagementDialog::TemplateManagementDialog(QWidget *parent, const QStringList &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( QString::null ), 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, SIGNAL( clicked() ),
00056 SLOT( slotAddTemplate() ) );
00057 connect( m_base->m_buttonDelete, SIGNAL( clicked() ),
00058 SLOT( slotDeleteTemplate() ) );
00059 m_base->m_listBox->insertStringList( m_templates );
00060 connect( m_base->m_listBox, SIGNAL( selectionChanged( QListBoxItem * ) ),
00061 SLOT( slotUpdateDeleteButton( QListBoxItem * ) ) );
00062 connect( m_base->m_buttonApply, SIGNAL( clicked() ),
00063 SLOT( slotApplyTemplate() ) );
00064
00065 }
00066
00067 void TemplateManagementDialog::slotAddTemplate()
00068 {
00069 bool ok;
00070 bool duplicate = false;
00071 const QString 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 QTimer::singleShot(0, this, 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
00091
00092 m_base->m_buttonApply->setEnabled( false );
00093
00094 m_base->m_buttonAdd->setEnabled( false );
00095 }
00096
00097 void TemplateManagementDialog::slotDeleteTemplate()
00098 {
00099 QListBoxItem *const item = m_base->m_listBox->selectedItem();
00100 if ( !item ) return;
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(QMAX(current -1, 0), true);
00106 }
00107
00108 void TemplateManagementDialog::slotUpdateDeleteButton( QListBoxItem *item )
00109 {
00110 m_base->m_buttonDelete->setEnabled( item != 0 );
00111 }
00112
00113 void TemplateManagementDialog::slotApplyTemplate()
00114 {
00115
00116 m_base->m_buttonAdd->setEnabled( false );
00117 const QString &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
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"
|