Sayonara Player
Shortcut.h
1 /* Shortcut.h */
2 
3 /* Copyright (C) 2011-2019 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 
44  private:
45  PIMPL(Shortcut)
46 
47  Shortcut();
48 
55  QList<QShortcut*> init_qt_shortcut(QWidget* parent, Qt::ShortcutContext context);
56 
57 
58  friend class ShortcutHandler;
59  ShortcutHandlerPrivate:
60  void add_qt_shortcuts(const QList<QShortcut*>& shortcuts);
61 
62 
63  public:
70  Shortcut(ShortcutIdentifier identifier, const QString& default_shortcut);
71 
78  Shortcut(ShortcutIdentifier identifier, const QStringList& default_shortcuts);
79 
84  Shortcut(const Shortcut& other);
85 
86  Shortcut& operator=(const Shortcut& other);
87 
88  ~Shortcut();
89 
94  static Shortcut getInvalid();
95 
100  void change_shortcut(const QStringList& shortcuts);
101 
106  QString name() const;
107 
112  QStringList default_shorcut() const;
113 
119  QKeySequence sequence() const;
120 
125  const QStringList& shortcuts() const;
126 
131  ShortcutIdentifier identifier() const;
132  QString identifier_string() const;
133 
138  bool is_valid() const;
139 
140  template<typename T>
146  void connect(QWidget* parent, T func, Qt::ShortcutContext context=Qt::WindowShortcut)
147  {
148  QList<QShortcut*> shortcuts = init_qt_shortcut(parent, context);
149  for(QShortcut* sc : shortcuts)
150  {
151  parent->connect(sc, &QShortcut::activated, func);
152  }
153  }
154 
155 
162  void connect(QWidget* parent, QObject* receiver, const char* slot, Qt::ShortcutContext context=Qt::WindowShortcut);
163 };
164 
165 #endif // SHORTCUT_H
A singleton class for retrieving shortcuts.
Definition: ShortcutHandler.h:40
static Shortcut getInvalid()
get a raw and invalid shortcut. This function is used instead of the default constructor
bool is_valid() const
Check if the shortcut is valid or if it was retrieved via getInvalid()
A single shortcut managed by ShortcutHandler. This class holds information about the default shortcut...
Definition: Shortcut.h:41
QList< QKeySequence > sequences() const
get a list key squences mapped to this shortcut
QString name() const
get the human-readable name of the shortcut
ShortcutIdentifier identifier() const
get the unique identifier
const QStringList & shortcuts() const
get a human-readable list of mapped shortcuts
void change_shortcut(const QStringList &shortcuts)
QStringList default_shorcut() const
get a human-readable list of mapped default shortcuts
void connect(QWidget *parent, T func, Qt::ShortcutContext context=Qt::WindowShortcut)
create a qt shortcut for a widget
Definition: Shortcut.h:146