1 #include "shortcut.hpp" 2 3 #include <cassert> 4 5 #include <QAction> 6 #include <QWidget> 7 8 #include <components/debug/debuglog.hpp> 9 10 #include "state.hpp" 11 #include "shortcutmanager.hpp" 12 13 namespace CSMPrefs 14 { Shortcut(const std::string & name,QWidget * parent)15 Shortcut::Shortcut(const std::string& name, QWidget* parent) 16 : QObject(parent) 17 , mEnabled(true) 18 , mName(name) 19 , mModName("") 20 , mSecondaryMode(SM_Ignore) 21 , mModifier(0) 22 , mCurrentPos(0) 23 , mLastPos(0) 24 , mActivationStatus(AS_Inactive) 25 , mModifierStatus(false) 26 , mAction(nullptr) 27 { 28 assert (parent); 29 30 State::get().getShortcutManager().addShortcut(this); 31 State::get().getShortcutManager().getSequence(name, mSequence); 32 } 33 Shortcut(const std::string & name,const std::string & modName,QWidget * parent)34 Shortcut::Shortcut(const std::string& name, const std::string& modName, QWidget* parent) 35 : QObject(parent) 36 , mEnabled(true) 37 , mName(name) 38 , mModName(modName) 39 , mSecondaryMode(SM_Ignore) 40 , mModifier(0) 41 , mCurrentPos(0) 42 , mLastPos(0) 43 , mActivationStatus(AS_Inactive) 44 , mModifierStatus(false) 45 , mAction(nullptr) 46 { 47 assert (parent); 48 49 State::get().getShortcutManager().addShortcut(this); 50 State::get().getShortcutManager().getSequence(name, mSequence); 51 State::get().getShortcutManager().getModifier(modName, mModifier); 52 } 53 Shortcut(const std::string & name,const std::string & modName,SecondaryMode secMode,QWidget * parent)54 Shortcut::Shortcut(const std::string& name, const std::string& modName, SecondaryMode secMode, QWidget* parent) 55 : QObject(parent) 56 , mEnabled(true) 57 , mName(name) 58 , mModName(modName) 59 , mSecondaryMode(secMode) 60 , mModifier(0) 61 , mCurrentPos(0) 62 , mLastPos(0) 63 , mActivationStatus(AS_Inactive) 64 , mModifierStatus(false) 65 , mAction(nullptr) 66 { 67 assert (parent); 68 69 State::get().getShortcutManager().addShortcut(this); 70 State::get().getShortcutManager().getSequence(name, mSequence); 71 State::get().getShortcutManager().getModifier(modName, mModifier); 72 } 73 ~Shortcut()74 Shortcut::~Shortcut() 75 { 76 try 77 { 78 State::get().getShortcutManager().removeShortcut(this); 79 } 80 catch(const std::exception& e) 81 { 82 Log(Debug::Error) << "Error in the destructor: " << e.what(); 83 } 84 } 85 isEnabled() const86 bool Shortcut::isEnabled() const 87 { 88 return mEnabled; 89 } 90 getName() const91 const std::string& Shortcut::getName() const 92 { 93 return mName; 94 } 95 getModifierName() const96 const std::string& Shortcut::getModifierName() const 97 { 98 return mModName; 99 } 100 getSecondaryMode() const101 Shortcut::SecondaryMode Shortcut::getSecondaryMode() const 102 { 103 return mSecondaryMode; 104 } 105 getSequence() const106 const QKeySequence& Shortcut::getSequence() const 107 { 108 return mSequence; 109 } 110 getModifier() const111 int Shortcut::getModifier() const 112 { 113 return mModifier; 114 } 115 getPosition() const116 int Shortcut::getPosition() const 117 { 118 return mCurrentPos; 119 } 120 getLastPosition() const121 int Shortcut::getLastPosition() const 122 { 123 return mLastPos; 124 } 125 getActivationStatus() const126 Shortcut::ActivationStatus Shortcut::getActivationStatus() const 127 { 128 return mActivationStatus; 129 } 130 getModifierStatus() const131 bool Shortcut::getModifierStatus() const 132 { 133 return mModifierStatus; 134 } 135 enable(bool state)136 void Shortcut::enable(bool state) 137 { 138 mEnabled = state; 139 } 140 setSequence(const QKeySequence & sequence)141 void Shortcut::setSequence(const QKeySequence& sequence) 142 { 143 mSequence = sequence; 144 mCurrentPos = 0; 145 mLastPos = sequence.count() - 1; 146 147 if (mAction) 148 { 149 mAction->setText(mActionText + "\t" + State::get().getShortcutManager().convertToString(mSequence).data()); 150 } 151 } 152 setModifier(int modifier)153 void Shortcut::setModifier(int modifier) 154 { 155 mModifier = modifier; 156 } 157 setPosition(int pos)158 void Shortcut::setPosition(int pos) 159 { 160 mCurrentPos = pos; 161 } 162 setActivationStatus(ActivationStatus status)163 void Shortcut::setActivationStatus(ActivationStatus status) 164 { 165 mActivationStatus = status; 166 } 167 setModifierStatus(bool status)168 void Shortcut::setModifierStatus(bool status) 169 { 170 mModifierStatus = status; 171 } 172 associateAction(QAction * action)173 void Shortcut::associateAction(QAction* action) 174 { 175 if (mAction) 176 { 177 mAction->setText(mActionText); 178 179 disconnect(this, SIGNAL(activated()), mAction, SLOT(trigger())); 180 disconnect(mAction, SIGNAL(destroyed()), this, SLOT(actionDeleted())); 181 } 182 183 mAction = action; 184 185 if (mAction) 186 { 187 mActionText = mAction->text(); 188 mAction->setText(mActionText + "\t" + State::get().getShortcutManager().convertToString(mSequence).data()); 189 190 connect(this, SIGNAL(activated()), mAction, SLOT(trigger())); 191 connect(mAction, SIGNAL(destroyed()), this, SLOT(actionDeleted())); 192 } 193 } 194 signalActivated(bool state)195 void Shortcut::signalActivated(bool state) 196 { 197 emit activated(state); 198 } 199 signalActivated()200 void Shortcut::signalActivated() 201 { 202 emit activated(); 203 } 204 signalSecondary(bool state)205 void Shortcut::signalSecondary(bool state) 206 { 207 emit secondary(state); 208 } signalSecondary()209 void Shortcut::signalSecondary() 210 { 211 emit secondary(); 212 } 213 toString() const214 QString Shortcut::toString() const 215 { 216 return QString(State::get().getShortcutManager().convertToString(mSequence, mModifier).data()); 217 } 218 actionDeleted()219 void Shortcut::actionDeleted() 220 { 221 mAction = nullptr; 222 } 223 } 224