kalarm

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 <qlayout.h>
00024 #include <qpushbutton.h>
00025 #include <qwhatsthis.h>
00026 
00027 #include <klocale.h>
00028 #include <kguiitem.h>
00029 #include <kmessagebox.h>
00030 #include <kaccel.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(QWidget* parent, const char* name)
00047     : KDialogBase(KDialogBase::Plain, i18n("Alarm Templates"), Close, Ok, parent, name, false, true)
00048 {
00049     QWidget* topWidget = plainPage();
00050     QBoxLayout* topLayout = new QHBoxLayout(topWidget);
00051     topLayout->setSpacing(spacingHint());
00052 
00053     QBoxLayout* layout = new QVBoxLayout(topLayout);
00054     mTemplateList = new TemplateListView(true, i18n("The list of alarm templates"), topWidget);
00055     mTemplateList->setSelectionMode(QListView::Extended);
00056     mTemplateList->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
00057     connect(mTemplateList, SIGNAL(selectionChanged()), SLOT(slotSelectionChanged()));
00058     layout->addWidget(mTemplateList);
00059 
00060     layout = new QVBoxLayout(topLayout);
00061     QPushButton* button = new QPushButton(i18n("&New..."), topWidget);
00062     button->setFixedSize(button->sizeHint());
00063     connect(button, SIGNAL(clicked()), SLOT(slotNew()));
00064     QWhatsThis::add(button, i18n("Create a new alarm template"));
00065     layout->addWidget(button);
00066 
00067     mEditButton = new QPushButton(i18n("&Edit..."), topWidget);
00068     mEditButton->setFixedSize(mEditButton->sizeHint());
00069     connect(mEditButton, SIGNAL(clicked()), SLOT(slotEdit()));
00070     QWhatsThis::add(mEditButton, i18n("Edit the currently highlighted alarm template"));
00071     layout->addWidget(mEditButton);
00072 
00073     mCopyButton = new QPushButton(i18n("Co&py"), topWidget);
00074     mCopyButton->setFixedSize(mCopyButton->sizeHint());
00075     connect(mCopyButton, SIGNAL(clicked()), SLOT(slotCopy()));
00076     QWhatsThis::add(mCopyButton,
00077           i18n("Create a new alarm template based on a copy of the currently highlighted template"));
00078     layout->addWidget(mCopyButton);
00079 
00080     mDeleteButton = new QPushButton(i18n("&Delete"), topWidget);
00081     mDeleteButton->setFixedSize(mDeleteButton->sizeHint());
00082     connect(mDeleteButton, SIGNAL(clicked()), SLOT(slotDelete()));
00083     QWhatsThis::add(mDeleteButton, i18n("Delete the currently highlighted alarm template"));
00084     layout->addWidget(mDeleteButton);
00085 
00086     KAccel* accel = new KAccel(this);
00087     accel->insert(KStdAccel::SelectAll, mTemplateList, SLOT(slotSelectAll()));
00088     accel->insert(KStdAccel::Deselect, mTemplateList, SLOT(slotDeselect()));
00089     accel->readSettings();
00090 
00091     mTemplateList->refresh();
00092     slotSelectionChanged();          // enable/disable buttons as appropriate
00093 
00094     QSize s;
00095     if (KAlarm::readConfigWindowSize(TMPL_DIALOG_NAME, s))
00096         resize(s);
00097 }
00098 
00099 /******************************************************************************
00100 *  Destructor.
00101 */
00102 TemplateDlg::~TemplateDlg()
00103 {
00104     mInstance = 0;
00105 }
00106 
00107 /******************************************************************************
00108 *  Create an instance, if none already exists.
00109 */
00110 TemplateDlg* TemplateDlg::create(QWidget* parent, const char* name)
00111 {
00112     if (mInstance)
00113         return 0;
00114     mInstance = new TemplateDlg(parent, name);
00115     return mInstance;
00116 }
00117 
00118 /******************************************************************************
00119 *  Called when the New Template button is clicked to create a new template
00120 *  based on the currently selected alarm.
00121 */
00122 void TemplateDlg::slotNew()
00123 {
00124     createTemplate(0, this, mTemplateList);
00125 }
00126 
00127 /******************************************************************************
00128 *  Called when the Copy button is clicked to edit a copy of an existing alarm,
00129 *  to add to the list.
00130 */
00131 void TemplateDlg::slotCopy()
00132 {
00133     TemplateListViewItem* item = mTemplateList->selectedItem();
00134     if (item)
00135     {
00136         KAEvent event = item->event();
00137         createTemplate(&event, mTemplateList);
00138     }
00139 }
00140 
00141 /******************************************************************************
00142 *  Create a new template.
00143 *  If 'event' is non-zero, base the new template on an existing event or template.
00144 */
00145 void TemplateDlg::createTemplate(const KAEvent* event, QWidget* parent, TemplateListView* view)
00146 {
00147     EditAlarmDlg editDlg(true, i18n("New Alarm Template"), parent, 0, event);
00148     if (editDlg.exec() == QDialog::Accepted)
00149     {
00150         KAEvent event;
00151         editDlg.getEvent(event);
00152 
00153         // Add the template to the displayed lists and to the calendar file
00154         KAlarm::addTemplate(event, view, &editDlg);
00155         Undo::saveAdd(event);
00156     }
00157 }
00158 
00159 /******************************************************************************
00160 *  Called when the Modify button is clicked to edit the currently highlighted
00161 *  alarm in the list.
00162 */
00163 void TemplateDlg::slotEdit()
00164 {
00165     TemplateListViewItem* item = mTemplateList->selectedItem();
00166     if (item)
00167     {
00168         KAEvent event = item->event();
00169         EditAlarmDlg editDlg(true, i18n("Edit Alarm Template"), this, 0, &event);
00170         if (editDlg.exec() == QDialog::Accepted)
00171         {
00172             KAEvent newEvent;
00173             editDlg.getEvent(newEvent);
00174             QString id = event.id();
00175             newEvent.setEventID(id);
00176 
00177             // Update the event in the displays and in the calendar file
00178             KAlarm::updateTemplate(newEvent, mTemplateList, &editDlg);
00179             Undo::saveEdit(event, newEvent);
00180         }
00181     }
00182 }
00183 
00184 /******************************************************************************
00185 *  Called when the Delete button is clicked to delete the currently highlighted
00186 *  alarms in the list.
00187 */
00188 void TemplateDlg::slotDelete()
00189 {
00190     QValueList<EventListViewItemBase*> items = mTemplateList->selectedItems();
00191     int n = items.count();
00192     if (KMessageBox::warningContinueCancel(this, i18n("Do you really want to delete the selected alarm template?",
00193                                                       "Do you really want to delete the %n selected alarm templates?", n),
00194                                            i18n("Delete Alarm Template", "Delete Alarm Templates", n), KGuiItem(i18n("&Delete"), "editdelete"))
00195             != KMessageBox::Continue)
00196         return;
00197 
00198     int warnErr = 0;
00199     KAlarm::UpdateStatus status = KAlarm::UPDATE_OK;
00200     QValueList<KAEvent> events;
00201     AlarmCalendar::templateCalendar()->startUpdate();    // prevent multiple saves of the calendar until we're finished
00202     for (QValueList<EventListViewItemBase*>::Iterator it = items.begin();  it != items.end();  ++it)
00203     {
00204         TemplateListViewItem* item = (TemplateListViewItem*)(*it);
00205         events.append(item->event());
00206         KAlarm::UpdateStatus st = KAlarm::deleteTemplate(item->event());
00207         if (st != KAlarm::UPDATE_OK)
00208         {
00209             status = st;
00210             ++warnErr;
00211         }
00212     }
00213     if (!AlarmCalendar::templateCalendar()->endUpdate())    // save the calendar now
00214     {
00215         status = KAlarm::SAVE_FAILED;
00216         warnErr = items.count();
00217     }
00218     Undo::saveDeletes(events);
00219     if (warnErr)
00220         displayUpdateError(this, status, KAlarm::ERR_TEMPLATE, warnErr);
00221 }
00222 
00223 /******************************************************************************
00224 * Called when the group of items selected changes.
00225 * Enable/disable the buttons depending on whether/how many templates are
00226 * currently highlighted.
00227 */
00228 void TemplateDlg::slotSelectionChanged()
00229 {
00230     int count = mTemplateList->selectedCount();
00231     mEditButton->setEnabled(count == 1);
00232     mCopyButton->setEnabled(count == 1);
00233     mDeleteButton->setEnabled(count);
00234 }
00235 
00236 /******************************************************************************
00237 *  Called when the dialog's size has changed.
00238 *  Records the new size in the config file.
00239 */
00240 void TemplateDlg::resizeEvent(QResizeEvent* re)
00241 {
00242     if (isVisible())
00243         KAlarm::writeConfigWindowSize(TMPL_DIALOG_NAME, re->size());
00244     KDialog::resizeEvent(re);
00245 }
KDE Home | KDE Accessibility Home | Description of Access Keys