Sayonara Player
Shortcut.h
1 /* Shortcut.h */
2 
3 /* Copyright (C) 2011-2020 Michael Lugmair (Lucio Carreras)
4  *
5  * This file is part of sayonara player
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef SHORTCUT_H
22 #define SHORTCUT_H
23 
24 #include "ShortcutIdentifier.h"
25 #include "Utils/Pimpl.h"
26 
27 #include <QShortcut>
28 
29 #define ShortcutHandlerPrivate private
30 
31 class QKeySequence;
32 class QWidget;
41 class Shortcut
42 {
43  private:
44  PIMPL(Shortcut)
45 
46  Shortcut();
47 
54  QList<QShortcut*> initQtShortcut(QWidget* parent, Qt::ShortcutContext context);
55 
56 
57  friend class ShortcutHandler;
58  ShortcutHandlerPrivate:
59  void setQtShortcuts(const QList<QShortcut*>& qshortcuts);
60  void removeQtShortcut(QShortcut* qshortcut);
61  QList<QShortcut*> qtShortcuts() const;
62 
63 
64  public:
71  Shortcut(ShortcutIdentifier identifier, const QString& defaultShortcut);
72 
79  Shortcut(ShortcutIdentifier identifier, const QStringList& defaultShortcuts);
80 
85  Shortcut(const Shortcut& other);
86 
87  Shortcut& operator=(const Shortcut& other);
88 
89  ~Shortcut();
90 
95  static Shortcut getInvalid();
96 
101  void changeShortcut(const QStringList& shortcuts);
102 
107  QString name() const;
108 
113  QStringList defaultShortcut() const;
114 
120  QKeySequence sequence() const;
121 
126  const QStringList& shortcuts() const;
127 
132  ShortcutIdentifier identifier() const;
133  QString databaseKey() const;
134 
139  bool isValid() const;
140 
141  template<typename T>
147  void connect(QWidget* parent, T func, Qt::ShortcutContext context=Qt::WindowShortcut)
148  {
149  QList<QShortcut*> shortcuts = initQtShortcut(parent, context);
150  for(QShortcut* sc : shortcuts)
151  {
152  parent->connect(sc, &QShortcut::activated, func);
153  }
154  }
155 
156 
163  void connect(QWidget* parent, QObject* receiver, const char* slot, Qt::ShortcutContext context=Qt::WindowShortcut);
164 };
165 
166 #endif // SHORTCUT_H
Shortcut::Shortcut
Shortcut(const Shortcut &other)
Copy constructor.
Shortcut::getInvalid
static Shortcut getInvalid()
get a raw and invalid shortcut. This function is used instead of the default constructor
Shortcut::shortcuts
const QStringList & shortcuts() const
get a human-readable list of mapped shortcuts
Shortcut::Shortcut
Shortcut(ShortcutIdentifier identifier, const QStringList &defaultShortcuts)
Shortcut.
Shortcut::Shortcut
Shortcut(ShortcutIdentifier identifier, const QString &defaultShortcut)
Shortcut.
ShortcutHandler
A singleton class for retrieving shortcuts.
Definition: ShortcutHandler.h:42
QList< QShortcut * >
Shortcut::isValid
bool isValid() const
Check if the shortcut is valid or if it was retrieved via getInvalid()
Shortcut::defaultShortcut
QStringList defaultShortcut() const
get a human-readable list of mapped default shortcuts
Shortcut::sequences
QList< QKeySequence > sequences() const
get a list key squences mapped to this shortcut
Shortcut::name
QString name() const
get the human-readable name of the shortcut
Shortcut::connect
void connect(QWidget *parent, QObject *receiver, const char *slot, Qt::ShortcutContext context=Qt::WindowShortcut)
create a qt shortcut for a widget
Shortcut::connect
void connect(QWidget *parent, T func, Qt::ShortcutContext context=Qt::WindowShortcut)
create a qt shortcut for a widget
Definition: Shortcut.h:147
Shortcut::changeShortcut
void changeShortcut(const QStringList &shortcuts)
Shortcut
A single shortcut managed by ShortcutHandler. This class holds information about the default shortcut...
Definition: Shortcut.h:42
Shortcut::identifier
ShortcutIdentifier identifier() const
get the unique identifier