Warning, file /pim/kalarm/src/templatedlg.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *  templatedlg.cpp  -  dialog to create, edit and delete alarm templates
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2004-2023 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "templatedlg.h"
0010 
0011 #include "functions.h"
0012 #include "newalarmaction.h"
0013 #include "templatelistview.h"
0014 #include "undo.h"
0015 #include "resources/datamodel.h"
0016 #include "resources/eventmodel.h"
0017 #include "resources/resources.h"
0018 #include "lib/config.h"
0019 #include "lib/messagebox.h"
0020 #include "lib/shellprocess.h"
0021 
0022 #include <KLocalizedString>
0023 #include <KGuiItem>
0024 #include <KStandardAction>
0025 #include <KActionCollection>
0026 #include <KSeparator>
0027 
0028 #include <QAction>
0029 #include <QBoxLayout>
0030 #include <QHBoxLayout>
0031 #include <QPushButton>
0032 #include <QResizeEvent>
0033 #include <QVBoxLayout>
0034 #include <QMenu>
0035 
0036 using namespace KCal;
0037 
0038 static const char TMPL_DIALOG_NAME[] = "TemplateDialog";
0039 
0040 
0041 TemplateDlg* TemplateDlg::mInstance = nullptr;
0042 
0043 
0044 TemplateDlg::TemplateDlg(QWidget* parent)
0045     : QDialog(parent)
0046 {
0047     setModal(false);
0048     setWindowTitle(i18nc("@title:window", "Alarm Templates"));
0049 
0050     QBoxLayout* topLayout = new QVBoxLayout(this);
0051 
0052     QBoxLayout* hlayout = new QHBoxLayout();
0053     topLayout->addLayout(hlayout);
0054 
0055     QBoxLayout* layout = new QVBoxLayout();
0056     layout->setContentsMargins(0, 0, 0, 0);
0057     hlayout->addLayout(layout);
0058     mListFilterModel = DataModel::createTemplateListModel(this);
0059     if (!ShellProcess::authorised())
0060         mListFilterModel->setAlarmActionFilter(static_cast<KAEvent::Action>(KAEvent::Action::All & ~KAEvent::Action::Command));
0061     mListView = new TemplateListView(this);
0062     mListView->setModel(mListFilterModel);
0063     mListView->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
0064     mListView->setWhatsThis(i18nc("@info:whatsthis", "The list of alarm templates"));
0065     mListView->setItemDelegate(new TemplateListDelegate(mListView));
0066     connect(mListView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &TemplateDlg::slotSelectionChanged);
0067     layout->addWidget(mListView);
0068 
0069     layout = new QVBoxLayout();
0070     layout->setContentsMargins(0, 0, 0, 0);
0071     hlayout->addLayout(layout);
0072     QPushButton* button = new QPushButton(i18nc("@action:button", "New"));
0073     mNewAction = new NewAlarmAction(true, i18nc("@action", "New"), this);
0074     button->setMenu(mNewAction->menu());
0075     connect(mNewAction, &NewAlarmAction::selected, this, &TemplateDlg::slotNew);
0076     button->setToolTip(i18nc("@info:tooltip", "Create a new alarm template"));
0077     button->setWhatsThis(i18nc("@info:whatsthis", "Create a new alarm template"));
0078     layout->addWidget(button);
0079 
0080     mEditButton = new QPushButton(i18nc("@action:button", "Edit..."));
0081     connect(mEditButton, &QPushButton::clicked, this, &TemplateDlg::slotEdit);
0082     mEditButton->setToolTip(i18nc("@info:tooltip", "Edit the selected alarm template"));
0083     mEditButton->setWhatsThis(i18nc("@info:whatsthis", "Edit the currently highlighted alarm template"));
0084     layout->addWidget(mEditButton);
0085 
0086     mCopyButton = new QPushButton(i18nc("@action:button", "Copy"));
0087     connect(mCopyButton, &QPushButton::clicked, this, &TemplateDlg::slotCopy);
0088     mCopyButton->setToolTip(i18nc("@info:tooltip", "Create a new alarm template based on the selected template"));
0089     mCopyButton->setWhatsThis(i18nc("@info:whatsthis", "Create a new alarm template based on a copy of the currently highlighted template"));
0090     layout->addWidget(mCopyButton);
0091 
0092     mDeleteButton = new QPushButton(i18nc("@action:button", "Delete"));
0093     connect(mDeleteButton, &QPushButton::clicked, this, &TemplateDlg::slotDelete);
0094     mDeleteButton->setToolTip(i18nc("@info:tooltip", "Delete the selected alarm templates"));
0095     mDeleteButton->setWhatsThis(i18nc("@info:whatsthis", "Delete the currently highlighted alarm templates"));
0096     layout->addWidget(mDeleteButton);
0097 
0098     layout->addStretch();
0099 
0100     topLayout->addWidget(new KSeparator(Qt::Horizontal, this));
0101 
0102     auto buttonBox = new QDialogButtonBox(this);
0103     buttonBox->addButton(QDialogButtonBox::Close);
0104     connect(buttonBox, &QDialogButtonBox::rejected,
0105             this, &QDialog::close);
0106     topLayout->addWidget(buttonBox);
0107 
0108     KActionCollection* actions = new KActionCollection(this);   //NOLINT(clang-analyzer-cplusplus.NewDeleteLeaks)
0109     QAction* act = KStandardAction::selectAll(mListView, &QTreeView::selectAll, actions);
0110     topLevelWidget()->addAction(act);
0111     act = KStandardAction::deselect(mListView, &QAbstractItemView::clearSelection, actions);
0112     topLevelWidget()->addAction(act);
0113     slotSelectionChanged();          // enable/disable buttons as appropriate
0114 
0115     QSize s;
0116     if (Config::readWindowSize(TMPL_DIALOG_NAME, s))
0117         resize(s);
0118 }
0119 
0120 /******************************************************************************
0121 * Destructor.
0122 */
0123 TemplateDlg::~TemplateDlg()
0124 {
0125     mInstance = nullptr;
0126 }
0127 
0128 /******************************************************************************
0129 * Create an instance, if none already exists.
0130 */
0131 TemplateDlg* TemplateDlg::create(QWidget* parent)
0132 {
0133     if (mInstance)
0134         return nullptr;
0135     mInstance = new TemplateDlg(parent);
0136     return mInstance;
0137 }
0138 
0139 /******************************************************************************
0140 * Called when the New Template button is clicked to create a new template.
0141 */
0142 void TemplateDlg::slotNew(EditAlarmDlg::Type type)
0143 {
0144     KAlarm::editNewTemplate(type, mListView);
0145 }
0146 
0147 /******************************************************************************
0148 * Called when the Copy button is clicked to edit a copy of an existing alarm,
0149 * to add to the list.
0150 */
0151 void TemplateDlg::slotCopy()
0152 {
0153     KAEvent event = mListView->selectedEvent();
0154     if (event.isValid())
0155         KAlarm::editNewTemplate(event, mListView);
0156 }
0157 
0158 /******************************************************************************
0159 * Called when the Modify button is clicked to edit the currently highlighted
0160 * alarm in the list.
0161 */
0162 void TemplateDlg::slotEdit()
0163 {
0164     KAEvent event = mListView->selectedEvent();
0165     if (event.isValid())
0166         KAlarm::editTemplate(event, mListView);
0167 }
0168 
0169 /******************************************************************************
0170 * Called when the Delete button is clicked to delete the currently highlighted
0171 * alarms in the list.
0172 */
0173 void TemplateDlg::slotDelete()
0174 {
0175     QList<KAEvent> events = mListView->selectedEvents();
0176     int n = events.count();
0177     if (KAMessageBox::warningContinueCancel(this, i18ncp("@info", "Do you really want to delete the selected alarm template?",
0178                                                          "Do you really want to delete the %1 selected alarm templates?", n),
0179                                             i18ncp("@title:window", "Delete Alarm Template", "Delete Alarm Templates", n),
0180                                             KGuiItem(i18nc("@action:button", "&Delete"), QStringLiteral("edit-delete")))
0181             != KMessageBox::Continue)
0182         return;
0183 
0184     KAEvent::List delEvents;
0185     delEvents.reserve(n);
0186     Undo::EventList undos;
0187     undos.reserve(n);
0188     for (int i = 0;  i < n;  ++i)
0189     {
0190         KAEvent* event = &events[i];
0191         delEvents.append(event);
0192         undos.append(*event, Resources::resourceForEvent(event->id()));
0193     }
0194     KAlarm::deleteTemplates(delEvents, this);
0195     Undo::saveDeletes(undos);
0196 }
0197 
0198 /******************************************************************************
0199 * Called when the group of items selected changes.
0200 * Enable/disable the buttons depending on whether/how many templates are
0201 * currently highlighted.
0202 */
0203 void TemplateDlg::slotSelectionChanged()
0204 {
0205     QList<KAEvent> events = mListView->selectedEvents();
0206     int count = events.count();
0207     bool readOnly = false;
0208     for (int i = 0;  i < count;  ++i)
0209     {
0210         const KAEvent* event = &events[i];
0211         if (KAlarm::eventReadOnly(event->id()))
0212         {
0213             readOnly = true;
0214             break;
0215         }
0216     }
0217     mEditButton->setEnabled(count == 1);
0218     mCopyButton->setEnabled(count == 1);
0219     mDeleteButton->setEnabled(count && !readOnly);
0220 }
0221 
0222 /******************************************************************************
0223 * Called when the dialog's size has changed.
0224 * Records the new size in the config file.
0225 */
0226 void TemplateDlg::resizeEvent(QResizeEvent* re)
0227 {
0228     if (isVisible())
0229         Config::writeWindowSize(TMPL_DIALOG_NAME, re->size());
0230     QDialog::resizeEvent(re);
0231 }
0232 
0233 #include "moc_templatedlg.cpp"
0234 
0235 // vim: et sw=4: