File indexing completed on 2024-12-08 11:06:04

0001 // clang-format off
0002 /*
0003  * KDiff3 - Text Diff And Merge Tool
0004  *
0005  * SPDX-FileCopyrightText: 2008 Valentin Rusu kde at rusu.info
0006  * SPDX-FileCopyrightText: 2018-2020 Michael Reeves reeves.87@gmail.com
0007  * SPDX-License-Identifier: GPL-2.0-or-later
0008  */
0009 // clang-format on
0010 
0011 #ifndef GUIUTILS_H
0012 #define GUIUTILS_H
0013 
0014 #include "defmac.h"
0015 
0016 #include <type_traits>
0017 
0018 #include <KActionCollection>
0019 #include <QObject>
0020 
0021 namespace GuiUtils {
0022 /*
0023     Use std::enable_if since compilers don't disambiguate overloads based on return type alone.
0024     Further more KToggleAction is actually a subclass of QAction which would leave the call
0025     ambiguous even as a parameter.
0026 */
0027 template <class T, class Receiver, class Func>
0028 inline typename std::enable_if_t<std::is_same_v<T, QAction>, QAction>* createAction(
0029     const QString& text,
0030     const Receiver receiver,
0031     const Func slot,
0032     KActionCollection* ac,
0033     const QString& actionName)
0034 {
0035     assert(ac != nullptr);
0036     QAction* theAction;
0037 
0038     theAction = ac->addAction(actionName);
0039     theAction->setText(text);
0040     chk_connect_a(theAction, &QAction::triggered, receiver, slot);
0041     return theAction;
0042 }
0043 
0044 template <class T, class Receiver, class Func>
0045 inline typename std::enable_if_t<std::is_same_v<T, KToggleAction>, KToggleAction>* createAction(
0046     const QString& text,
0047     const Receiver receiver,
0048     const Func slot,
0049     KActionCollection* ac,
0050     const QString& actionName)
0051 {
0052     assert(ac != nullptr);
0053     KToggleAction* theAction = new KToggleAction(ac);
0054     ac->addAction(actionName, theAction);
0055     theAction->setText(text);
0056     chk_connect_a(theAction, &KToggleAction::triggered, receiver, slot);
0057     return theAction;
0058 }
0059 
0060 template <class T, class Receiver, class Func>
0061 T* createAction(
0062     const QString& text,
0063     const QKeySequence& shortcut,
0064     Receiver receiver,
0065     Func slot,
0066     KActionCollection* ac,
0067     const QString& actionName)
0068 {
0069     T* theAction = createAction<T, Receiver, Func>(text, receiver, slot, ac, actionName);
0070     ac->setDefaultShortcut(theAction, shortcut);
0071     return theAction;
0072 }
0073 template <class T, class Receiver, class Func>
0074 T* createAction(
0075     const QString& text,
0076     const QIcon& icon,
0077     Receiver receiver,
0078     Func slot,
0079     KActionCollection* ac,
0080     const QString& actionName)
0081 {
0082     T* theAction = createAction<T, Receiver, Func>(text, receiver, slot, ac, actionName);
0083     theAction->setIcon(icon);
0084     return theAction;
0085 }
0086 template <class T, class Receiver, class Func>
0087 T* createAction(
0088     const QString& text,
0089     const QIcon& icon,
0090     const QString& iconText,
0091     Receiver receiver,
0092     Func slot,
0093     KActionCollection* ac,
0094     const QString& actionName)
0095 {
0096     T* theAction = createAction<T, Receiver, Func>(text, receiver, slot, ac, actionName);
0097     theAction->setIcon(icon);
0098     theAction->setIconText(iconText);
0099     return theAction;
0100 }
0101 template <class T, class Receiver, class Func>
0102 T* createAction(
0103     const QString& text,
0104     const QIcon& icon,
0105     const QKeySequence& shortcut,
0106     Receiver receiver,
0107     Func slot,
0108     KActionCollection* ac,
0109     const QString& actionName)
0110 {
0111     T* theAction = createAction<T, Receiver, Func>(text, shortcut, receiver, slot, ac, actionName);
0112     theAction->setIcon(icon);
0113     return theAction;
0114 }
0115 template <class T, class Receiver, class Func>
0116 T* createAction(
0117     const QString& text,
0118     const QIcon& icon,
0119     const QString& iconText,
0120     const QKeySequence& shortcut,
0121     Receiver receiver,
0122     Func slot,
0123     KActionCollection* ac,
0124     const QString& actionName)
0125 {
0126     T* theAction = createAction<T, Receiver, Func>(text, shortcut, receiver, slot, ac, actionName);
0127     theAction->setIcon(icon);
0128     theAction->setIconText(iconText);
0129     return theAction;
0130 }
0131 
0132 //Allow actions to be created without connecting them immediately.
0133 
0134 template <class T>
0135 inline typename std::enable_if<std::is_same<T, QAction>::value, QAction>::type* createAction(
0136     const QString& text,
0137     KActionCollection* ac,
0138     const QString& actionName)
0139 {
0140     assert(ac != nullptr);
0141     QAction* theAction;
0142 
0143     theAction = ac->addAction(actionName);
0144     theAction->setText(text);
0145 
0146     return theAction;
0147 }
0148 
0149 template <class T>
0150 inline typename std::enable_if<std::is_same<T, KToggleAction>::value, KToggleAction>::type* createAction(
0151     const QString& text,
0152     KActionCollection* ac,
0153     const QString& actionName)
0154 {
0155     assert(ac != nullptr);
0156     KToggleAction* theAction = new KToggleAction(ac);
0157     ac->addAction(actionName, theAction);
0158     theAction->setText(text);
0159     return theAction;
0160 }
0161 
0162 template <class T>
0163 T* createAction(
0164     const QString& text,
0165     const QKeySequence& shortcut,
0166     KActionCollection* ac,
0167     const QString& actionName)
0168 {
0169     T* theAction = createAction<T>(text, ac, actionName);
0170     ac->setDefaultShortcut(theAction, shortcut);
0171     return theAction;
0172 }
0173 
0174 template <class T>
0175 T* createAction(
0176     const QString& text,
0177     const QIcon& icon,
0178     KActionCollection* ac,
0179     const QString& actionName)
0180 {
0181     T* theAction = createAction<T>(text, ac, actionName);
0182     theAction->setIcon(icon);
0183     return theAction;
0184 }
0185 
0186 template <class T>
0187 T* createAction(
0188     const QString& text,
0189     const QIcon& icon,
0190     const QString& iconText,
0191     KActionCollection* ac,
0192     const QString& actionName)
0193 {
0194     T* theAction = createAction<T>(text, ac, actionName);
0195     theAction->setIcon(icon);
0196     theAction->setIconText(iconText);
0197     return theAction;
0198 }
0199 
0200 template <class T>
0201 T* createAction(
0202     const QString& text,
0203     const QIcon& icon,
0204     const QKeySequence& shortcut,
0205     KActionCollection* ac,
0206     const QString& actionName)
0207 {
0208     T* theAction = createAction<T>(text, shortcut, ac, actionName);
0209     theAction->setIcon(icon);
0210     return theAction;
0211 }
0212 
0213 template <class T>
0214 T* createAction(
0215     const QString& text,
0216     const QIcon& icon,
0217     const QString& iconText,
0218     const QKeySequence& shortcut,
0219     KActionCollection* ac,
0220     const QString& actionName)
0221 {
0222     T* theAction = createAction<T>(text, shortcut, ac, actionName);
0223     theAction->setIcon(icon);
0224     theAction->setIconText(iconText);
0225     return theAction;
0226 }
0227 
0228 } //namespace GuiUtils
0229 
0230 #endif