kalarm

templatedlg.cpp

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