Warning, file /pim/kalarm/src/templatemenuaction.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 * templatemenuaction.cpp - menu action to select a template 0003 * Program: kalarm 0004 * SPDX-FileCopyrightText: 2005-2022 David Jarvie <djarvie@kde.org> 0005 * 0006 * SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #include "templatemenuaction.h" 0010 0011 #include "functions.h" 0012 #include "resourcescalendar.h" 0013 #include "kalarmcalendar/kaevent.h" 0014 0015 #include <QMenu> 0016 0017 0018 TemplateMenuAction::TemplateMenuAction(const QIcon& icon, const QString& label, QObject* parent) 0019 : KActionMenu(icon, label, parent) 0020 { 0021 setPopupMode(QToolButton::InstantPopup); 0022 connect(menu(), &QMenu::aboutToShow, this, &TemplateMenuAction::slotInitMenu); 0023 connect(menu(), &QMenu::triggered, this, &TemplateMenuAction::slotSelected); 0024 } 0025 0026 /****************************************************************************** 0027 * Called when the New From Template action is clicked. 0028 * Creates a popup menu listing all alarm templates, in sorted name order. 0029 */ 0030 void TemplateMenuAction::slotInitMenu() 0031 { 0032 QMenu* m = menu(); 0033 m->clear(); 0034 mOriginalTexts.clear(); 0035 0036 // Compile a sorted list of template names 0037 QStringList sorted; 0038 const QList<KAEvent> templates = KAlarm::templateList(); 0039 for (const KAEvent& templ : templates) 0040 { 0041 const QString name = templ.name(); 0042 int j = 0; 0043 for (int jend = sorted.count(); 0044 j < jend && QString::localeAwareCompare(name, sorted[j]) > 0; 0045 ++j) ; 0046 sorted.insert(j, name); 0047 } 0048 0049 for (const QString& name : std::as_const(sorted)) 0050 { 0051 QAction* act = m->addAction(name); 0052 mOriginalTexts[act] = name; // keep original text, since action text has shortcuts added 0053 } 0054 } 0055 0056 /****************************************************************************** 0057 * Called when a template is selected from the New From Template popup menu. 0058 * Executes a New Alarm dialog, preset from the selected template. 0059 */ 0060 void TemplateMenuAction::slotSelected(QAction* action) 0061 { 0062 QMap<QAction*, QString>::ConstIterator it = mOriginalTexts.constFind(action); 0063 if (it == mOriginalTexts.constEnd() || it.value().isEmpty()) 0064 return; 0065 KAEvent templ = ResourcesCalendar::templateEvent(it.value()); 0066 templ.setName(QString()); // don't preset the new alarm with the template's name 0067 Q_EMIT selected(templ); 0068 } 0069 0070 #include "moc_templatemenuaction.cpp" 0071 0072 // vim: et sw=4: