TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musichotkeymanager.cpp
Go to the documentation of this file.
1 #include "musichotkeymanager.h"
3 
4 #include <QStringList>
5 
6 void MusicHotKeyManager::addHotKey(QObject *object)
7 {
8  QGlobalShortcut *shortcut = new QGlobalShortcut(object);
9  m_hotkeys << shortcut;
10 }
11 
12 void MusicHotKeyManager::addHotKey(QObject *object, const char *slot)
13 {
14  QGlobalShortcut *shortcut = new QGlobalShortcut(object);
15  m_hotkeys << shortcut;
16 
17  connect(shortcut, SIGNAL(activated()), object, slot);
18 }
19 
20 void MusicHotKeyManager::addHotKey(QObject *object, const QString &key, const char *slot)
21 {
22  QGlobalShortcut *shortcut = new QGlobalShortcut(key, object);
23  m_hotkeys << shortcut;
24 
25  connect(shortcut, SIGNAL(activated()), object, slot);
26 }
27 
28 void MusicHotKeyManager::setHotKey(int index, const QString &key)
29 {
30  if(index >= m_hotkeys.count())
31  {
32  return;
33  }
34 
35  m_hotkeys[index]->setShortcut(QKeySequence(key));
36 }
37 
38 QObject* MusicHotKeyManager::hotKey(int index)
39 {
40  if(index >= m_hotkeys.count())
41  {
42  return nullptr;
43  }
44 
45  return m_hotkeys[index];
46 }
47 
49 {
51  {
52  key->unsetShortcut();
53  key->setEnabled(false);
54  }
55 }
56 
58 {
60  {
61  key->setEnabled(enabled);
62  }
63 }
64 
65 void MusicHotKeyManager::setEnabled(int index, bool enabled)
66 {
67  if(index >= m_hotkeys.count())
68  {
69  return;
70  }
71 
72  m_hotkeys[index]->setEnabled(enabled);
73 }
74 
76 {
77  if(index >= m_hotkeys.count())
78  {
79  return false;
80  }
81 
82  return m_hotkeys[index]->isEnabled();
83 }
84 
85 QString MusicHotKeyManager::toString(int key, int modifiers)
86 {
87  const QString strModList[] = {"Ctrl", "Shift", "Alt"};
88  const quint32 modList[] = {Qt::ControlModifier, Qt::ShiftModifier, Qt::AltModifier};
89 
90  QString keyStr;
91  for(int i = 0; i < 3; ++i)
92  {
93  if(modifiers & modList[i])
94  {
95  keyStr.append(strModList[i] + "+");
96  }
97  }
98 
99  if(key == Qt::Key_Shift || key == Qt::Key_Control || key == Qt::Key_Alt)
100  {
101  keyStr.chop(1);
102  return keyStr;
103  }
104 
105  return keyStr + QKeySequence(key).toString();
106 }
107 
109 {
110  return m_hotkeys.count();
111 }
112 
114 {
115  QStringList keys;
116  keys << "Ctrl+B" << "Ctrl+Left" << "Ctrl+Right" << "Ctrl+Up"
117  << "Ctrl+Down" << "Ctrl+S" << "Ctrl+I" << "Ctrl+M";
118  return keys;
119 }
120 
121 QStringList MusicHotKeyManager::keys() const
122 {
123  QStringList keys;
125  {
126  keys << key->shortcut().toString();
127  }
128  return keys;
129 }
static constexpr wchar_t key[]
void addHotKey(QObject *object)
QStringList defaultKeys() const
QList< QGlobalShortcut * > m_hotkeys
void setEnabled(bool enabled)
bool isEnabled(int index)
#define qAsConst
Definition: ttkqtglobal.h:53
QObject * hotKey(int index)
QStringList keys() const
The class of the qglobal shortcut.
void setHotKey(int index, const QString &key)
QString toString(int key, int modifiers)