00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "kalarm.h"
00022 #include "templatedlg.moc"
00023
00024 #include "editdlg.h"
00025 #include "alarmcalendar.h"
00026 #include "alarmresources.h"
00027 #include "eventlistmodel.h"
00028 #include "functions.h"
00029 #include "newalarmaction.h"
00030 #include "templatelistfiltermodel.h"
00031 #include "templatelistview.h"
00032 #include "undo.h"
00033
00034 #include <QPushButton>
00035 #include <QList>
00036 #include <QVBoxLayout>
00037 #include <QHBoxLayout>
00038 #include <QBoxLayout>
00039 #include <QResizeEvent>
00040
00041 #include <klocale.h>
00042 #include <kguiitem.h>
00043 #include <kmessagebox.h>
00044 #include <kdebug.h>
00045 #include <kstandardaction.h>
00046 #include <kactioncollection.h>
00047 #include <kaction.h>
00048 #include <kmenu.h>
00049
00050 using namespace KCal;
00051
00052 static const char TMPL_DIALOG_NAME[] = "TemplateDialog";
00053
00054
00055 TemplateDlg* TemplateDlg::mInstance = 0;
00056
00057
00058 TemplateDlg::TemplateDlg(QWidget* parent)
00059 : KDialog(parent)
00060 {
00061 QWidget* topWidget = new QWidget(this);
00062 setMainWidget(topWidget);
00063 setButtons(Close);
00064 setDefaultButton(Ok);
00065 setModal(false);
00066 setCaption(i18nc("@title:window", "Alarm Templates"));
00067 showButtonSeparator(true);
00068 QBoxLayout* topLayout = new QHBoxLayout(topWidget);
00069 topLayout->setMargin(0);
00070 topLayout->setSpacing(spacingHint());
00071
00072 QBoxLayout* layout = new QVBoxLayout();
00073 layout->setMargin(0);
00074 topLayout->addLayout(layout);
00075 mListFilterModel = new TemplateListFilterModel(EventListModel::templates());
00076 mListFilterModel->setTypeFilter(false);
00077 mListView = new TemplateListView(topWidget);
00078 mListView->setModel(mListFilterModel);
00079 mListView->sortByColumn(TemplateListFilterModel::TemplateNameColumn, Qt::AscendingOrder);
00080 mListView->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
00081 mListView->setWhatsThis(i18nc("@info:whatsthis", "The list of alarm templates"));
00082 mListView->setItemDelegate(new TemplateListDelegate(mListView));
00083 connect(mListView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)), SLOT(slotSelectionChanged()));
00084 layout->addWidget(mListView);
00085
00086 layout = new QVBoxLayout();
00087 layout->setMargin(0);
00088 topLayout->addLayout(layout);
00089 QPushButton* button = new QPushButton(i18nc("@action:button", "New"), topWidget);
00090 mNewAction = new NewAlarmAction(true, i18nc("@action", "New"), this);
00091 button->setMenu(mNewAction->menu());
00092 connect(mNewAction, SIGNAL(selected(EditAlarmDlg::Type)), SLOT(slotNew(EditAlarmDlg::Type)));
00093 button->setWhatsThis(i18nc("@info:whatsthis", "Create a new alarm template"));
00094 layout->addWidget(button);
00095
00096 mEditButton = new QPushButton(i18nc("@action:button", "Edit..."), topWidget);
00097 connect(mEditButton, SIGNAL(clicked()), SLOT(slotEdit()));
00098 mEditButton->setWhatsThis(i18nc("@info:whatsthis", "Edit the currently highlighted alarm template"));
00099 layout->addWidget(mEditButton);
00100
00101 mCopyButton = new QPushButton(i18nc("@action:button", "Copy"), topWidget);
00102 connect(mCopyButton, SIGNAL(clicked()), SLOT(slotCopy()));
00103 mCopyButton->setWhatsThis(i18nc("@info:whatsthis", "Create a new alarm template based on a copy of the currently highlighted template"));
00104 layout->addWidget(mCopyButton);
00105
00106 mDeleteButton = new QPushButton(i18nc("@action:button", "Delete"), topWidget);
00107 connect(mDeleteButton, SIGNAL(clicked()), SLOT(slotDelete()));
00108 mDeleteButton->setWhatsThis(i18nc("@info:whatsthis", "Delete the currently highlighted alarm template"));
00109 layout->addWidget(mDeleteButton);
00110
00111 KActionCollection* actions = new KActionCollection(this);
00112 KAction* act = KStandardAction::selectAll(mListView, SLOT(selectAll()), actions);
00113 topLevelWidget()->addAction(act);
00114 act = KStandardAction::deselect(mListView, SLOT(clearSelection()), actions);
00115 topLevelWidget()->addAction(act);
00116
00117 slotSelectionChanged();
00118
00119 QSize s;
00120 if (KAlarm::readConfigWindowSize(TMPL_DIALOG_NAME, s))
00121 resize(s);
00122 }
00123
00124
00125
00126
00127 TemplateDlg::~TemplateDlg()
00128 {
00129 mInstance = 0;
00130 }
00131
00132
00133
00134
00135 TemplateDlg* TemplateDlg::create(QWidget* parent)
00136 {
00137 if (mInstance)
00138 return 0;
00139 mInstance = new TemplateDlg(parent);
00140 return mInstance;
00141 }
00142
00143
00144
00145
00146 void TemplateDlg::slotNew(EditAlarmDlg::Type type)
00147 {
00148 KAlarm::editNewTemplate(type, mListView);
00149 }
00150
00151
00152
00153
00154
00155 void TemplateDlg::slotCopy()
00156 {
00157 KAEvent* event = mListView->selectedEvent();
00158 if (event)
00159 KAlarm::editNewTemplate(event, mListView);
00160 }
00161
00162
00163
00164
00165
00166 void TemplateDlg::slotEdit()
00167 {
00168 KAEvent* event = mListView->selectedEvent();
00169 if (event)
00170 KAlarm::editTemplate(event, mListView);
00171 }
00172
00173
00174
00175
00176
00177 void TemplateDlg::slotDelete()
00178 {
00179 KAEvent::List events = mListView->selectedEvents();
00180 int n = events.count();
00181 if (KMessageBox::warningContinueCancel(this, i18ncp("@info", "Do you really want to delete the selected alarm template?",
00182 "Do you really want to delete the %1 selected alarm templates?", n),
00183 i18ncp("@title:window", "Delete Alarm Template", "Delete Alarm Templates", n),
00184 KGuiItem(i18nc("@action:button", "&Delete"), "edit-delete"))
00185 != KMessageBox::Continue)
00186 return;
00187
00188 QStringList eventIDs;
00189 Undo::EventList undos;
00190 AlarmCalendar* resources = AlarmCalendar::resources();
00191 for (int i = 0; i < n; ++i)
00192 {
00193 const KAEvent* event = events[i];
00194 eventIDs.append(event->id());
00195 undos.append(*event, resources->resourceForEvent(event->id()));
00196 }
00197 KAlarm::deleteTemplates(eventIDs, this);
00198 Undo::saveDeletes(undos);
00199 }
00200
00201
00202
00203
00204
00205
00206 void TemplateDlg::slotSelectionChanged()
00207 {
00208 AlarmCalendar* resources = AlarmCalendar::resources();
00209 KAEvent::List events = mListView->selectedEvents();
00210 int count = events.count();
00211 bool readOnly = false;
00212 for (int i = 0; i < count; ++i)
00213 {
00214 const KAEvent* event = events[i];
00215 if (resources->eventReadOnly(event->id()))
00216 {
00217 readOnly = true;
00218 break;
00219 }
00220 }
00221 mEditButton->setEnabled(count == 1);
00222 mCopyButton->setEnabled(count == 1);
00223 mDeleteButton->setEnabled(count && !readOnly);
00224 }
00225
00226
00227
00228
00229
00230 void TemplateDlg::resizeEvent(QResizeEvent* re)
00231 {
00232 if (isVisible())
00233 KAlarm::writeConfigWindowSize(TMPL_DIALOG_NAME, re->size());
00234 KDialog::resizeEvent(re);
00235 }