ShortcutButton.cc 1.92 KB
Newer Older
Dirk Wilden's avatar
Dirk Wilden committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include "ShortcutButton.hh"

#include <iostream>

///Constructor
ShortcutButton::ShortcutButton(QWidget* _parent) : QPushButton(_parent)
{
  key_ = -1;
  setCheckable(true);
}

///set the current shortcut
void ShortcutButton::setCurrentShortcut(int _key, Qt::KeyboardModifiers _modifiers)
{
  key_ = _key;
  modifiers_ = _modifiers;

  showCurrentShortcut();
}

///get current key
int ShortcutButton::key()
{
  return key_;
}

///get current modifiers
Qt::KeyboardModifiers ShortcutButton::modifiers()
{
  return modifiers_;
}

///key press
void ShortcutButton::keyPressEvent(QKeyEvent* event)
{
  if (firstPress_){
    oldKey_ = key_;
    oldModifiers_ = modifiers_;
  }

  firstPress_ = false;

  key_ = event->key();
  modifiers_ = event->modifiers();
}

///key release
void ShortcutButton::keyReleaseEvent(QKeyEvent* /*event*/)
{
  showCurrentShortcut();
  emit keyChanged();
}

///show the current shortcut
void ShortcutButton::showCurrentShortcut()
{

  firstPress_ = true;

  QString keyString;

Dirk Wilden's avatar
Dirk Wilden committed
62 63 64
  if (key_ == -1){
    keyString = "unassigned";
  }else if (key_ == Qt::Key_AltGr || key_ == Qt::Key_Alt || key_ == Qt::Key_Control || key_ == Qt::Key_Shift || key_ == Qt::Key_Meta){
Dirk Wilden's avatar
Dirk Wilden committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    keyString = QKeySequence( modifiers_ ).toString();
    keyString = keyString.left(keyString.size()-1);
  }else
    keyString = QKeySequence( key_ + modifiers_ ).toString();

  setText(keyString);
}

void ShortcutButton::focusInEvent(QFocusEvent* event){
  setChecked(true);
  setText("");
  QPushButton::focusInEvent(event);
}

void ShortcutButton::focusOutEvent(QFocusEvent* event){
  setChecked(false);

  key_ = oldKey_;
  modifiers_ = oldModifiers_;

  showCurrentShortcut();
  QPushButton::focusOutEvent(event);
}

void ShortcutButton::mousePressEvent(QMouseEvent* event){
  checked_ = isChecked();
  QPushButton::mousePressEvent(event);
}

void ShortcutButton::mouseReleaseEvent(QMouseEvent* event){
  QPushButton::mouseReleaseEvent(event);
  setChecked( checked_ );
}