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 set_qt_shortcuts(const QList<QShortcut*>& qshortcuts);
61  void remove_qt_shortcut(QShortcut* qshortcut);
62  QList<QShortcut*> qt_shortcuts() const;
63 
64 
65  public:
72  Shortcut(ShortcutIdentifier identifier, const QString& default_shortcut);
73 
80  Shortcut(ShortcutIdentifier identifier, const QStringList& default_shortcuts);
81 
86  Shortcut(const Shortcut& other);
87 
88  Shortcut& operator=(const Shortcut& other);
89 
90  ~Shortcut();
91 
96  static Shortcut getInvalid();
97 
102  void change_shortcut(const QStringList& shortcuts);
103 
108  QString name() const;
109 
114  QStringList default_shorcut() const;
115 
121  QKeySequence sequence() const;
122 
127  const QStringList& shortcuts() const;
128 
133  ShortcutIdentifier identifier() const;
134  QString db_key() const;
135 
140  bool is_valid() const;
141 
142  template<typename T>
148  void connect(QWidget* parent, T func, Qt::ShortcutContext context=Qt::WindowShortcut)
149  {
150  QList<QShortcut*> shortcuts = init_qt_shortcut(parent, context);
151  for(QShortcut* sc : shortcuts)
152  {
153  parent->connect(sc, &QShortcut::activated, func);
154  }
155  }
156 
157 
164  void connect(QWidget* parent, QObject* receiver, const char* slot, Qt::ShortcutContext context=Qt::WindowShortcut);
165 };
166 
167 #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:148
Definition: EngineUtils.h:33