templatedlg.cpp
00001 /* 00002 * templatedlg.cpp - dialogue to create, edit and delete alarm templates 00003 * Program: kalarm 00004 * Copyright © 2004-2006 by David Jarvie <software@astrojar.org.uk> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License along 00017 * with this program; if not, write to the Free Software Foundation, Inc., 00018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "kalarm.h" 00022 00023 #include <tqlayout.h> 00024 #include <tqpushbutton.h> 00025 #include <tqwhatsthis.h> 00026 00027 #include <tdelocale.h> 00028 #include <kguiitem.h> 00029 #include <tdemessagebox.h> 00030 #include <tdeaccel.h> 00031 #include <kdebug.h> 00032 00033 #include "editdlg.h" 00034 #include "alarmcalendar.h" 00035 #include "functions.h" 00036 #include "templatelistview.h" 00037 #include "undo.h" 00038 #include "templatedlg.moc" 00039 00040 static const char TMPL_DIALOG_NAME[] = "TemplateDialog"; 00041 00042 00043 TemplateDlg* TemplateDlg::mInstance = 0; 00044 00045 00046 TemplateDlg::TemplateDlg(TQWidget* parent, const char* name) 00047 : KDialogBase(KDialogBase::Plain, i18n("Alarm Templates"), Close, Ok, parent, name, false, true) 00048 { 00049 TQWidget* topWidget = plainPage(); 00050 TQBoxLayout* topLayout = new TQHBoxLayout(topWidget); 00051 topLayout->setSpacing(spacingHint()); 00052 00053 TQBoxLayout* layout = new TQVBoxLayout(topLayout); 00054 mTemplateList = new TemplateListView(true, i18n("The list of alarm templates"), topWidget); 00055 mTemplateList->setSelectionMode(TQListView::Extended); 00056 mTemplateList->setSizePolicy(TQSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Expanding)); 00057 connect(mTemplateList, TQT_SIGNAL(selectionChanged()), TQT_SLOT(slotSelectionChanged())); 00058 layout->addWidget(mTemplateList); 00059 00060 layout = new TQVBoxLayout(topLayout); 00061 TQPushButton* button = new TQPushButton(i18n("&New..."), topWidget); 00062 connect(button, TQT_SIGNAL(clicked()), TQT_SLOT(slotNew())); 00063 TQWhatsThis::add(button, i18n("Create a new alarm template")); 00064 layout->addWidget(button); 00065 00066 mEditButton = new TQPushButton(i18n("&Edit..."), topWidget); 00067 connect(mEditButton, TQT_SIGNAL(clicked()), TQT_SLOT(slotEdit())); 00068 TQWhatsThis::add(mEditButton, i18n("Edit the currently highlighted alarm template")); 00069 layout->addWidget(mEditButton); 00070 00071 mCopyButton = new TQPushButton(i18n("Co&py"), topWidget); 00072 connect(mCopyButton, TQT_SIGNAL(clicked()), TQT_SLOT(slotCopy())); 00073 TQWhatsThis::add(mCopyButton, 00074 i18n("Create a new alarm template based on a copy of the currently highlighted template")); 00075 layout->addWidget(mCopyButton); 00076 00077 mDeleteButton = new TQPushButton(i18n("&Delete"), topWidget); 00078 connect(mDeleteButton, TQT_SIGNAL(clicked()), TQT_SLOT(slotDelete())); 00079 TQWhatsThis::add(mDeleteButton, i18n("Delete the currently highlighted alarm template")); 00080 layout->addWidget(mDeleteButton); 00081 00082 TDEAccel* accel = new TDEAccel(this); 00083 accel->insert(TDEStdAccel::SelectAll, TQT_TQOBJECT(mTemplateList), TQT_SLOT(slotSelectAll())); 00084 accel->insert(TDEStdAccel::Deselect, TQT_TQOBJECT(mTemplateList), TQT_SLOT(slotDeselect())); 00085 accel->readSettings(); 00086 00087 mTemplateList->refresh(); 00088 slotSelectionChanged(); // enable/disable buttons as appropriate 00089 00090 TQSize s; 00091 if (KAlarm::readConfigWindowSize(TMPL_DIALOG_NAME, s)) 00092 resize(s); 00093 } 00094 00095 /****************************************************************************** 00096 * Destructor. 00097 */ 00098 TemplateDlg::~TemplateDlg() 00099 { 00100 mInstance = 0; 00101 } 00102 00103 /****************************************************************************** 00104 * Create an instance, if none already exists. 00105 */ 00106 TemplateDlg* TemplateDlg::create(TQWidget* parent, const char* name) 00107 { 00108 if (mInstance) 00109 return 0; 00110 mInstance = new TemplateDlg(parent, name); 00111 return mInstance; 00112 } 00113 00114 /****************************************************************************** 00115 * Called when the New Template button is clicked to create a new template 00116 * based on the currently selected alarm. 00117 */ 00118 void TemplateDlg::slotNew() 00119 { 00120 createTemplate(0, this, mTemplateList); 00121 } 00122 00123 /****************************************************************************** 00124 * Called when the Copy button is clicked to edit a copy of an existing alarm, 00125 * to add to the list. 00126 */ 00127 void TemplateDlg::slotCopy() 00128 { 00129 TemplateListViewItem* item = mTemplateList->selectedItem(); 00130 if (item) 00131 { 00132 KAEvent event = item->event(); 00133 createTemplate(&event, mTemplateList); 00134 } 00135 } 00136 00137 /****************************************************************************** 00138 * Create a new template. 00139 * If 'event' is non-zero, base the new template on an existing event or template. 00140 */ 00141 void TemplateDlg::createTemplate(const KAEvent* event, TQWidget* parent, TemplateListView* view) 00142 { 00143 EditAlarmDlg editDlg(true, i18n("New Alarm Template"), parent, 0, event); 00144 if (editDlg.exec() == TQDialog::Accepted) 00145 { 00146 KAEvent event; 00147 editDlg.getEvent(event); 00148 00149 // Add the template to the displayed lists and to the calendar file 00150 KAlarm::addTemplate(event, view, &editDlg); 00151 Undo::saveAdd(event); 00152 } 00153 } 00154 00155 /****************************************************************************** 00156 * Called when the Modify button is clicked to edit the currently highlighted 00157 * alarm in the list. 00158 */ 00159 void TemplateDlg::slotEdit() 00160 { 00161 TemplateListViewItem* item = mTemplateList->selectedItem(); 00162 if (item) 00163 { 00164 KAEvent event = item->event(); 00165 EditAlarmDlg editDlg(true, i18n("Edit Alarm Template"), this, 0, &event); 00166 if (editDlg.exec() == TQDialog::Accepted) 00167 { 00168 KAEvent newEvent; 00169 editDlg.getEvent(newEvent); 00170 TQString id = event.id(); 00171 newEvent.setEventID(id); 00172 00173 // Update the event in the displays and in the calendar file 00174 KAlarm::updateTemplate(newEvent, mTemplateList, &editDlg); 00175 Undo::saveEdit(event, newEvent); 00176 } 00177 } 00178 } 00179 00180 /****************************************************************************** 00181 * Called when the Delete button is clicked to delete the currently highlighted 00182 * alarms in the list. 00183 */ 00184 void TemplateDlg::slotDelete() 00185 { 00186 TQValueList<EventListViewItemBase*> items = mTemplateList->selectedItems(); 00187 int n = items.count(); 00188 if (KMessageBox::warningContinueCancel(this, i18n("Do you really want to delete the selected alarm template?", 00189 "Do you really want to delete the %n selected alarm templates?", n), 00190 i18n("Delete Alarm Template", "Delete Alarm Templates", n), KGuiItem(i18n("&Delete"), "edit-delete")) 00191 != KMessageBox::Continue) 00192 return; 00193 00194 int warnErr = 0; 00195 KAlarm::UpdateStatus status = KAlarm::UPDATE_OK; 00196 TQValueList<KAEvent> events; 00197 AlarmCalendar::templateCalendar()->startUpdate(); // prevent multiple saves of the calendar until we're finished 00198 for (TQValueList<EventListViewItemBase*>::Iterator it = items.begin(); it != items.end(); ++it) 00199 { 00200 TemplateListViewItem* item = (TemplateListViewItem*)(*it); 00201 events.append(item->event()); 00202 KAlarm::UpdateStatus st = KAlarm::deleteTemplate(item->event()); 00203 if (st != KAlarm::UPDATE_OK) 00204 { 00205 status = st; 00206 ++warnErr; 00207 } 00208 } 00209 if (!AlarmCalendar::templateCalendar()->endUpdate()) // save the calendar now 00210 { 00211 status = KAlarm::SAVE_FAILED; 00212 warnErr = items.count(); 00213 } 00214 Undo::saveDeletes(events); 00215 if (warnErr) 00216 displayUpdateError(this, status, KAlarm::ERR_TEMPLATE, warnErr); 00217 } 00218 00219 /****************************************************************************** 00220 * Called when the group of items selected changes. 00221 * Enable/disable the buttons depending on whether/how many templates are 00222 * currently highlighted. 00223 */ 00224 void TemplateDlg::slotSelectionChanged() 00225 { 00226 int count = mTemplateList->selectedCount(); 00227 mEditButton->setEnabled(count == 1); 00228 mCopyButton->setEnabled(count == 1); 00229 mDeleteButton->setEnabled(count); 00230 } 00231 00232 /****************************************************************************** 00233 * Called when the dialog's size has changed. 00234 * Records the new size in the config file. 00235 */ 00236 void TemplateDlg::resizeEvent(TQResizeEvent* re) 00237 { 00238 if (isVisible()) 00239 KAlarm::writeConfigWindowSize(TMPL_DIALOG_NAME, re->size()); 00240 KDialog::resizeEvent(re); 00241 }