File indexing completed on 2024-05-12 05:14:58
0001 /* 0002 * templatepickdlg.cpp - dialog to choose an alarm template 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 "templatepickdlg.h" 0010 0011 #include "templatelistview.h" 0012 #include "resources/datamodel.h" 0013 #include "resources/eventmodel.h" 0014 #include "lib/config.h" 0015 #include "lib/shellprocess.h" 0016 0017 #include <KLocalizedString> 0018 0019 #include <QVBoxLayout> 0020 #include <QResizeEvent> 0021 #include <QDialogButtonBox> 0022 #include <QPushButton> 0023 0024 static const char TMPL_PICK_DIALOG_NAME[] = "TemplatePickDialog"; 0025 0026 0027 TemplatePickDlg::TemplatePickDlg(KAEvent::Action type, QWidget* parent) 0028 : QDialog(parent) 0029 { 0030 QWidget* topWidget = new QWidget(this); 0031 auto mainLayout = new QVBoxLayout; 0032 setLayout(mainLayout); 0033 mainLayout->addWidget(topWidget); 0034 setWindowTitle(i18nc("@title:window", "Choose Alarm Template")); 0035 QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel); 0036 mainLayout->addWidget(buttonBox); 0037 mOkButton = buttonBox->button(QDialogButtonBox::Ok); 0038 mOkButton->setDefault(true); 0039 mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0040 connect(buttonBox, &QDialogButtonBox::accepted, this, &TemplatePickDlg::accept); 0041 connect(buttonBox, &QDialogButtonBox::rejected, this, &TemplatePickDlg::reject); 0042 auto topLayout = new QVBoxLayout(topWidget); 0043 topLayout->setContentsMargins(0, 0, 0, 0); 0044 0045 // Display the list of templates, but exclude command alarms if in kiosk mode. 0046 KAEvent::Action shown = KAEvent::Action::All; 0047 if (!ShellProcess::authorised()) 0048 { 0049 type = static_cast<KAEvent::Action>(type & ~KAEvent::Action::Command); 0050 shown = static_cast<KAEvent::Action>(shown & ~KAEvent::Action::Command); 0051 } 0052 mListFilterModel = DataModel::createTemplateListModel(this); 0053 mListFilterModel->setAlarmActionsEnabled(type); 0054 mListFilterModel->setAlarmActionFilter(shown); 0055 mListView = new TemplateListView(topWidget); 0056 mainLayout->addWidget(mListView); 0057 mListView->setModel(mListFilterModel); 0058 mListView->sortByColumn(TemplateListModel::TemplateNameColumn, Qt::AscendingOrder); 0059 mListView->setSelectionMode(QAbstractItemView::SingleSelection); 0060 mListView->setWhatsThis(i18nc("@info:whatsthis", "Select a template to base the new alarm on.")); 0061 connect(mListView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &TemplatePickDlg::slotSelectionChanged); 0062 // Require a real double click (even if KDE is in single-click mode) to accept the selection 0063 connect(mListView, &TemplateListView::doubleClicked, this, &TemplatePickDlg::slotDoubleClick); 0064 topLayout->addWidget(mListView); 0065 0066 slotSelectionChanged(); // enable or disable the OK button 0067 0068 QSize s; 0069 if (Config::readWindowSize(TMPL_PICK_DIALOG_NAME, s)) 0070 resize(s); 0071 } 0072 0073 /****************************************************************************** 0074 * Return the currently selected alarm template, or invalid if none. 0075 */ 0076 KAEvent TemplatePickDlg::selectedTemplate() const 0077 { 0078 return mListView->selectedEvent(); 0079 } 0080 0081 /****************************************************************************** 0082 * Called when the template selection changes. 0083 * Enable/disable the OK button depending on whether anything is selected. 0084 */ 0085 void TemplatePickDlg::slotSelectionChanged() 0086 { 0087 bool enable = !mListView->selectionModel()->selectedRows().isEmpty(); 0088 if (enable) 0089 enable = mListView->model()->flags(mListView->selectedIndex()) & Qt::ItemIsEnabled; 0090 mOkButton->setEnabled(enable); 0091 } 0092 0093 /****************************************************************************** 0094 * Called when the user double clicks to accept a selection. 0095 * Ignore if the double click is on a disabled item. 0096 */ 0097 void TemplatePickDlg::slotDoubleClick(const QModelIndex& ix) 0098 { 0099 if ((mListFilterModel->flags(ix) & (Qt::ItemIsEnabled | Qt::ItemIsSelectable)) == (Qt::ItemIsEnabled | Qt::ItemIsSelectable)) 0100 accept(); 0101 } 0102 0103 /****************************************************************************** 0104 * Called when the dialog's size has changed. 0105 * Records the new size in the config file. 0106 */ 0107 void TemplatePickDlg::resizeEvent(QResizeEvent* re) 0108 { 0109 if (isVisible()) 0110 Config::writeWindowSize(TMPL_PICK_DIALOG_NAME, re->size()); 0111 QDialog::resizeEvent(re); 0112 } 0113 0114 #include "moc_templatepickdlg.cpp" 0115 0116 // vim: et sw=4: