keyHandling.cc 12.3 KB
Newer Older
Dirk Wilden's avatar
Dirk Wilden committed
1
2
3
4
5
#include "CoreWidget.hh"

#include <OpenFlipper/BasePlugin/KeyInterface.hh>
#include <OpenFlipper/BasePlugin/BaseInterface.hh>

6
7
#include <OpenFlipper/common/GlobalOptions.hh>

Dirk Wilden's avatar
Dirk Wilden committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//-----------------------------------------------------------------------------

KeyBinding CoreWidget::getKeyBinding(QObject* _plugin, int _keyIndex ){
  if (_plugin == 0)
    return coreKeys_[_keyIndex];

  for (uint i=0; i < plugins_.size(); i++){
    if (plugins_[i].plugin == _plugin)
      return plugins_[i].keys[_keyIndex];
  }

  std::cerr << "ERROR: could not get KeyBinding\n";
  return KeyBinding();
}

/// passes keyPressEvents to either the Core or a Plugin depending on who has registered the key
void CoreWidget::keyPressEvent(QKeyEvent* _e)
{
26
27
  std::cerr << "Key press event in core" << std::endl;

Dirk Wilden's avatar
Dirk Wilden committed
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
  std::pair< int,Qt::KeyboardModifiers > key = std::make_pair(_e->key(), _e->modifiers() );

  //iterate over all assigned keys
  KeyRange range = keys_.equal_range(key);

  KeyMap::iterator it;
  for (it=range.first; it != range.second; ++it){

    QObject* plugin = (*it).second.first;
    KeyBinding binding = getKeyBinding( plugin, (*it).second.second );

    //check if its a core Key
    if (plugin == 0){
      QKeyEvent* mappedEvent = new QKeyEvent(_e->type(),binding.key, binding.modifiers,
                                             _e->text(), _e->isAutoRepeat(), _e->count() );

      coreKeyPressEvent(mappedEvent);

      delete mappedEvent;

      //if the key is multiUse also check other assigned keys
      if (binding.multiUse)
        continue;
      else
        return;
    }

    //it's a plugin key
    KeyInterface* keyPlugin = qobject_cast< KeyInterface * >(plugin);

    if (keyPlugin){
      QKeyEvent* mappedEvent = new QKeyEvent(_e->type(),binding.key, binding.modifiers,
                                             _e->text(), _e->isAutoRepeat(), _e->count() );
      keyPlugin->slotKeyEvent(mappedEvent);
    }

    //if its not a multiUse key we are ready
    if (!binding.multiUse)
      return;
  }
}

//-----------------------------------------------------------------------------

/// passes keyReleaseEvents to either the Core or a Plugin depending on who has registered the key
void CoreWidget::keyReleaseEvent(QKeyEvent* _e) {
74
  std::cerr << "Key release event in core" << std::endl;
Dirk Wilden's avatar
Dirk Wilden committed
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

  if (_e->isAutoRepeat()) return; //consider only "real" release events


  std::pair< int,Qt::KeyboardModifiers > key = std::make_pair(_e->key(), _e->modifiers() );

  //iterate over all assigned keys
  KeyRange range = keys_.equal_range(key);

  KeyMap::iterator it;
  for (it=range.first; it != range.second; ++it){

    QObject* plugin = (*it).second.first;
    KeyBinding binding = getKeyBinding( plugin, (*it).second.second );

    //check if its a core Key
    if (plugin == 0){
      QKeyEvent* mappedEvent = new QKeyEvent(_e->type(),binding.key, binding.modifiers,
                                             _e->text(), _e->isAutoRepeat(), _e->count() );
      coreKeyReleaseEvent(mappedEvent);

      delete mappedEvent;

      //if the key is multiUse also check other assigned keys
      if (binding.multiUse)
        continue;
      else
        return;
    }

    //it's a plugin key
    KeyInterface* keyPlugin = qobject_cast< KeyInterface * >(plugin);

    if (keyPlugin){
      QKeyEvent* mappedEvent = new QKeyEvent(_e->type(),binding.key, binding.modifiers,
                                             _e->text(), _e->isAutoRepeat(), _e->count() );
      keyPlugin->slotKeyEvent(mappedEvent);
    }

    //if its not a multiUse key we are ready
    if (!binding.multiUse)
      return;
  }
}

//-----------------------------------------------------------------------------

/// Register a key to the core or a plugin
void CoreWidget::slotRegisterKey(int _key, Qt::KeyboardModifiers _modifiers, QString _description, bool _multiUse){

  //first check if the key is already registered by the coreWidget
  bool found = false;
  bool multi = false;
  for (uint i=0; i < coreKeys_.size(); i++)
    if (coreKeys_[i].key == _key && coreKeys_[i].modifiers == _modifiers){
      found = true;
      multi = coreKeys_[i].multiUse;
      break;
    }

  //then check if the key is already registered by a different plugin
  if (!found)
    for (uint i=0; i < plugins_.size(); i++)
      for (int k=0; k < plugins_[i].keys.count(); k++)
        if (plugins_[i].keys[k].key == _key
        && plugins_[i].keys[k].modifiers == _modifiers){
          found = true;
          multi = plugins_[i].keys[k].multiUse;
          break;
        }

  if (found)
    emit log(LOGERR, "Key already registered elsewhere.");

  //check if its a key for the core
  if (sender() == this){
    KeyBinding kb;
    kb.key = _key;
    kb.modifiers = _modifiers;
    kb.description = _description;
    kb.multiUse = multi || _multiUse;

    if (multi && !_multiUse)
      log(LOGWARN, "Key registered as multiUse key.");

    coreKeys_.push_back( kb );

    keys_.insert( std::make_pair( std::make_pair(_key, _modifiers) , std::make_pair ((QObject*)0, coreKeys_.size()-1 ) )) ;
    invKeys_.insert( std::make_pair( std::make_pair ((QObject*)0, coreKeys_.size()-1 ) , std::make_pair(_key, _modifiers) ) );
    return;
  }

  //find plugin
 PluginInfo* pluginInfo = 0;

  for (uint i=0; i < plugins_.size(); i++)
    if (plugins_[i].plugin == sender())
      pluginInfo = &plugins_[i];

  if (pluginInfo == 0){
    emit log(LOGERR, "Unable to register key. Plugin not found!");
    return;
  }

  KeyBinding kb;
  kb.key = _key;
  kb.modifiers = _modifiers;
  kb.description = _description;
  kb.multiUse = multi || _multiUse;

  if (multi && !_multiUse)
    log(LOGWARN, "Key registered as multiUse key.");

  pluginInfo->keys.append( kb );

  keys_.insert( std::make_pair( std::make_pair(_key, _modifiers) , std::make_pair(pluginInfo->plugin, pluginInfo->keys.size()-1) ) );
  invKeys_.insert( std::make_pair( std::make_pair(pluginInfo->plugin, pluginInfo->keys.size()-1) , std::make_pair(_key, _modifiers) ) );
}

/// add a new keyMapping (keyBindingID is the id of the keyBinding in the pluginInfo of _plugin :)
void CoreWidget::slotAddKeyMapping(int _key, Qt::KeyboardModifiers _modifiers, QObject* _plugin, int _keyBindingID){

  std::pair< int,Qt::KeyboardModifiers > keyCombi = std::make_pair(_key, _modifiers );
  std::pair< int,Qt::KeyboardModifiers > oldCombi;
  std::pair< QObject*, int > oldTarget;

  bool replace = false;

  //and check if the key is already assigned without multiUse
  KeyMap::iterator it;
  for (it=keys_.begin(); it != keys_.end(); ++it){

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

    //check if its the keyBinding we want to map/replace
    if (plugin == _plugin && bindingID == _keyBindingID){
      replace = true;
      oldCombi = (*it).first;
      oldTarget = (*it).second;
      continue;
    }

    //check if the mapping is conflicting with other mappings
    if (_key == key && _modifiers == modifiers ){

      if (!binding.multiUse){
        if (plugin == 0)
          emit log(LOGERR, "Could not add key mapping. Key already assigned to the core.");
        else{
          BaseInterface* basePlugin = qobject_cast< BaseInterface * >(plugin);
229

Dirk Wilden's avatar
Dirk Wilden committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
          if (basePlugin)
            emit log(LOGERR, "Could not add key mapping. Key already assigned to " + basePlugin->name() );
          else
            emit log(LOGERR, "Could not add key mapping. Key already assigned to an unknown plugin.");
        }
        return;
      }
    }
  }

  KeyBinding keyBinding = getKeyBinding(_plugin, _keyBindingID);

  //check if new binding doesn't allow multiUse but other assignments for the key exist
  if (!keyBinding.multiUse)
    if ( (replace && keys_.count(keyCombi) > 1)  || (!replace && keys_.count(keyCombi) > 0) ){
      emit log(LOGERR, "Could not add (single usage) key mapping. Key already assigned.");
      return;
    }

  if (replace){
    keys_.erase(oldCombi);
    invKeys_.erase(oldTarget);
  }

254
255
256
257
  //now we can add the mapping
  keys_.insert   ( std::make_pair( keyCombi , std::make_pair(_plugin, _keyBindingID) ));
  invKeys_.insert( std::make_pair( std::make_pair(_plugin, _keyBindingID),  keyCombi ));

Dirk Wilden's avatar
Dirk Wilden committed
258
259
}

260
/// if a keyPressEvent belongs to the core this functions is called
Jan Möbius's avatar
Jan Möbius committed
261
void CoreWidget::registerCoreKeys() {
262
263
264
265
266

  //register keys for coreWidget
  connect(this , SIGNAL( registerKey(int, Qt::KeyboardModifiers, QString) ),
          this , SLOT(slotRegisterKey(int, Qt::KeyboardModifiers, QString)) );

Jan Möbius's avatar
Jan Möbius committed
267
  emit registerKey(Qt::Key_Print  , Qt::NoModifier, "Create Snapshot");
268
269
  emit registerKey(Qt::Key_S      , Qt::ControlModifier, "Save Object");
  emit registerKey(Qt::Key_O      , Qt::ControlModifier, "Open Object");
Jan Möbius's avatar
Jan Möbius committed
270
271
272
  emit registerKey(Qt::Key_L      , Qt::ControlModifier, "Show/Hide Logger");
  emit registerKey(Qt::Key_T      , Qt::ControlModifier, "Show/Hide Toolbox");
  emit registerKey(Qt::Key_F      , Qt::ControlModifier, "Toggle Fullscreen");
273
274
  emit registerKey(Qt::Key_E      , Qt::ControlModifier | Qt::AltModifier, "Adjust Eye distance for stereo mode");
  emit registerKey(Qt::Key_F      , Qt::ControlModifier | Qt::AltModifier, "Adjust Focal distance for stereo mode");
275
  emit registerKey(Qt::Key_Escape , Qt::NoModifier, "Switch to last action mode ( Move,Picking,Light or Info Mode)");
276
  emit registerKey(Qt::Key_Space  , Qt::NoModifier, "Toggle between multiview and single view");
277
278
279

}

Dirk Wilden's avatar
Dirk Wilden committed
280
281
282
/// if a keyPressEvent belongs to the core this functions is called
void CoreWidget::coreKeyPressEvent  (QKeyEvent* _e){

Jan Möbius's avatar
Jan Möbius committed
283
  if (_e->modifiers() & Qt::ControlModifier ) {
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
    if (_e->modifiers() & Qt::AltModifier )

      if ( OpenFlipper::Options::stereo() ) {
        switch (_e->key()) {
          // Stereo seeing: eye distance
          case Qt::Key_E:
          {
            bool ok(false);

            double val = QInputDialog::getDouble( this, "Eye Dist", "Eye Dist:",
                                                  examiner_widgets_[0]->eyeDistance(),
                                                  0.0, 100.0, 10,
                                                  &ok);

            if (ok) {
              for ( uint i = 0 ; i < examiner_widgets_.size(); ++i)
                examiner_widgets_[i]->setEyeDistance(val);
            }

            break;
          }

          // Stereo setting: focal length
          case Qt::Key_F:
          {
            bool ok(false);

            double val = QInputDialog::getDouble( this, "Focal Dist", "Focal Dist:",
                                                  examiner_widgets_[0]->focalDistance(),
                                                  0.0, 100.0, 10,
                                                  &ok);
            if (ok) {
              for ( uint i = 0 ; i < examiner_widgets_.size(); ++i)
                examiner_widgets_[i]->setFocalDistance(val);
            }

            break;
          }

          default:
            break;
        }

      } else
        emit log(LOGWARN,"Activate stereo mode to change stereo settings");

    else
      switch (_e->key()) {
Dirk Wilden's avatar
Dirk Wilden committed
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
        case Qt::Key_F :
            toggleFullscreen();
          return;

        case Qt::Key_L :
            toggleLogger();
          return;

        case Qt::Key_T :
            toggleToolbox();
          return;

        case Qt::Key_O :
          loadMenu();

        case Qt::Key_S :
          saveMenu();

        default:
          return;
352
353
      }

Dirk Wilden's avatar
Dirk Wilden committed
354
355
  }

Jan Möbius's avatar
Jan Möbius committed
356
357
358
359
360
  switch (_e->key()) {
    case Qt::Key_Escape:
      for ( uint i = 0 ; i < examiner_widgets_.size(); ++i)
        examiner_widgets_[i]->actionMode(examiner_widgets_[i]->lastActionMode());
      break;
Dirk Wilden's avatar
Dirk Wilden committed
361

Jan Möbius's avatar
Jan Möbius committed
362
363
364
365
    case Qt::Key_Print:
      std::cerr << "Todo : On Print Screen, create a snapshot for all viewers" << std::endl;
      break;

366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
    case Qt::Key_Space:
      if ( OpenFlipper::Options::multiView() ) {
        if ( examiner_widgets_[1]->isHidden() ) {

          for ( uint i = 1 ; i < OpenFlipper::Options::examinerWidgets() ; ++i )
            examiner_widgets_[i]->show();

        } else {

          for ( uint i = 1 ; i < OpenFlipper::Options::examinerWidgets() ; ++i )
            examiner_widgets_[i]->hide();

        }
      }

      break;

    // This should never be reached!
Jan Möbius's avatar
Jan Möbius committed
384
385
    default:
      return;
Dirk Wilden's avatar
Dirk Wilden committed
386
387
388
389
390
391
392
  }
}

/// if a keyReleaseEvent belongs to the core this functions is called
void CoreWidget::coreKeyReleaseEvent(QKeyEvent* /*_e*/){

}