keyBindings.cc 6.8 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
62
63
64
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "optionsWidget.hh"

#include <OpenFlipper/common/GlobalOptions.hh>

#include <OpenFlipper/BasePlugin/BaseInterface.hh>

#include "ShortcutButton.hh"

#include <QMessageBox>

/// get the pluginInfo object corresponding to the given pluginName
int OptionsWidget::getPluginInfo(QString pluginName){
  for (uint i=0; i < plugins_.size(); i++)
    if (plugins_[i].name == pluginName)
      return i;

  return -1;
}

/// doubleclick in the keyTree
void OptionsWidget::keyTreeDoubleClicked(QTreeWidgetItem* _item, int /*col*/){

std::cerr << "set focus\n";

  if (_item == 0 || _item->parent() == 0 || _item->columnCount() < 7)
    return;

  shortcutButton->setFocus(Qt::TabFocusReason);
}

/// user selects a different item in the tree
void OptionsWidget::keyTreeItemChanged( QTreeWidgetItem* current, QTreeWidgetItem* /*previous*/){

  if (current == 0 || current->parent() == 0 || current->columnCount() < 7){
    shortcutBox->setEnabled(false);
    shortcutButton->setText("");
    defaultShortcut->setText("");
  }else{
    shortcutBox->setEnabled(true);

    shortcutButton->setCurrentShortcut( current->text(4).toInt(), (Qt::KeyboardModifiers) current->text(5).toInt() );
    defaultShortcut->setText( current->text(2) );
  }

}

///check if the shortcut exists and add it if not
void OptionsWidget::updateShortcut(){

  if ( keyTree->currentItem() == 0)
    return;

  bool myMultiUse = (bool) keyTree->currentItem()->text(6).toInt();
  QString myNewKey = shortcutButton->text();

   //check if the shortcut already exists
  for (int i=0; i < keyTree->topLevelItemCount(); i++)
    for (int j=0; j < keyTree->topLevelItem(i)->childCount(); j++){

      QTreeWidgetItem* item = keyTree->topLevelItem(i)->child(j);
      QString key = item->text(1);
      bool multiUse = (bool) item->text(6).toInt();

      if (keyTree->currentItem() == item)
        continue;

      if (key == myNewKey && ( !multiUse || !myMultiUse ) ){
        QMessageBox::warning(this, "OpenFlipper", "Could not add Shortcut. Shortcut already assigned.", QMessageBox::Ok);
        return;
      }
    }

  //update the item
  if ( keyTree->currentItem() != 0){
    keyTree->currentItem()->setText( 1, shortcutButton->text() );
    keyTree->currentItem()->setText( 4, QString::number( shortcutButton->key() ) );
    keyTree->currentItem()->setText( 5, QString::number( shortcutButton->modifiers() ) );
  }

  keyTree->setFocus(Qt::TabFocusReason);
  keyTreeItemChanged(keyTree->currentItem(), 0);
}

///check which of the shortcuts changed and inform the core about the change
void OptionsWidget::applyShortcuts(){

  //check all shortcuts
  for (int i=0; i < keyTree->topLevelItemCount(); i++)
    for (int j=0; j < keyTree->topLevelItem(i)->childCount(); j++){

      //check wether the shortcut changed
      QTreeWidgetItem* item = keyTree->topLevelItem(i)->child(j);

      if ( item->text(1) != item->text(7) ){

        QString pluginName = item->parent()->text(0);

        QObject* plugin = 0;

        if (pluginName != "Core"){

          //get the plugin object
          int index = getPluginInfo(pluginName);

          if (index == -1) //if pluginInfo was not found ->skip
            continue;

          plugin = plugins_[index].plugin;
        }

        int bindingID = item->text(3).toInt();
        int key = item->text(4).toInt();

        Qt::KeyboardModifiers modi = (Qt::KeyboardModifiers) item->text(5).toInt();
 
        emit addKeyMapping(key, modi, plugin, bindingID);
      }
    }
}

/// init the TreeWidget containing the keyBindings
void OptionsWidget::initKeyTree(){

  keyTree->clear();

  keyTree->setEditTriggers(QAbstractItemView::NoEditTriggers);

  keyTree->setColumnCount ( 8 );

  QStringList headerdata;
  headerdata << "Action" << "Shortcut" << "Default" << "keyIndex" << "currentKey" << "currentModi" << "MultiUse" << "initShortcut";
  keyTree->setHeaderLabels(headerdata);

  //fill the keyTree
  std::map< QString, QTreeWidgetItem* > parentMap;

  InverseKeyMap::iterator it;
  for (it=keys_.begin(); it != keys_.end(); ++it){

    QObject* plugin = (*it).first.first;
    int bindingID = (*it).first.second;
    int key = (*it).second.first;
    Qt::KeyboardModifiers modifiers = (*it).second.second;

    //get plugin name
    QString name;

    if (plugin == 0) //parent is the core
      name = "Core";
    else{

      BaseInterface* basePlugin = qobject_cast< BaseInterface * >(plugin);
  
      if (basePlugin)
        name = basePlugin->name();
      else{
        //name not found so skip it
        continue;
      }
    }

    //get corresponding the pluginInfo object
    int i = -1;

    if (name != "Core"){
      i = getPluginInfo(name);

      if (i == -1) //if pluginInfo was not found ->skip
        continue;
    }

    //is the toplevel item already there?
    if ( parentMap.find(name) == parentMap.end() ){
      parentMap[name] = new QTreeWidgetItem(keyTree, QStringList(name));
      keyTree->addTopLevelItem( parentMap[name] );
    }

    QTreeWidgetItem* parent = parentMap[name];

    //get the default settings
    QString               description;
    bool                  multiUse;
    int                   defKey;
    Qt::KeyboardModifiers defModi;

    if (name == "Core"){

      description = coreKeys_[bindingID].description;
      multiUse = coreKeys_[bindingID].multiUse;
      defKey = coreKeys_[bindingID].key;
      defModi = coreKeys_[bindingID].modifiers;

    } else {

      description = plugins_[i].keys[bindingID].description;
      multiUse = plugins_[i].keys[bindingID].multiUse;
      defKey = plugins_[i].keys[bindingID].key;
      defModi = plugins_[i].keys[bindingID].modifiers;
    }

    QStringList rows;

    //setup the strings for the shortcuts
    QString keyString;

    if (key == Qt::Key_AltGr || key == Qt::Key_Alt || key == Qt::Key_Control || key == Qt::Key_Shift || key == Qt::Key_Meta){
      keyString = QKeySequence( modifiers ).toString();
      keyString = keyString.left(keyString.size()-1);
    }else
      keyString = QKeySequence( key + modifiers ).toString();

    QString defaultStr;

    if (defKey == Qt::Key_AltGr || defKey == Qt::Key_Alt || defKey == Qt::Key_Control || defKey == Qt::Key_Shift || defKey == Qt::Key_Meta){
      defaultStr = QKeySequence( defModi ).toString();
      defaultStr = defaultStr.left(defaultStr.size()-1);
    }else
      defaultStr = QKeySequence( defKey + defModi ).toString();

    //and add the row
    rows << description << keyString << defaultStr << QString::number(bindingID) << QString::number(key) 
         << QString::number(modifiers) << QString::number(multiUse) << keyString;

    QTreeWidgetItem* keyItem = new QTreeWidgetItem(parent, rows);

    parent->addChild( keyItem );
  }


  keyTree->setColumnWidth(0,350);
  keyTree->setColumnHidden(3, true);
  keyTree->setColumnHidden(4, true);
  keyTree->setColumnHidden(5, true);
  keyTree->setColumnHidden(6, true);
  keyTree->setColumnHidden(7, true);
}