keyBindings.cc

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 6733 $                                                         *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2009-08-05 09:32:32 +0200 (Mi, 05. Aug 2009) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 #include "optionsWidget.hh"
00044 
00045 #include <OpenFlipper/common/GlobalOptions.hh>
00046 
00047 #include <OpenFlipper/BasePlugin/BaseInterface.hh>
00048 
00049 #include "ShortcutButton.hh"
00050 
00051 #include <QMessageBox>
00052 
00054 int OptionsWidget::getPluginInfo(QString pluginName){
00055   for (uint i=0; i < plugins_.size(); i++)
00056     if (plugins_[i].name == pluginName)
00057       return i;
00058 
00059   return -1;
00060 }
00061 
00063 void OptionsWidget::keyTreeDoubleClicked(QTreeWidgetItem* _item, int /*col*/){
00064 
00065 std::cerr << "set focus\n";
00066 
00067   if (_item == 0 || _item->parent() == 0 || _item->columnCount() < 7)
00068     return;
00069 
00070   shortcutButton->setFocus(Qt::TabFocusReason);
00071 }
00072 
00074 void OptionsWidget::keyTreeItemChanged( QTreeWidgetItem* current, QTreeWidgetItem* /*previous*/){
00075 
00076   if (current == 0 || current->parent() == 0 || current->columnCount() < 7){
00077     shortcutBox->setEnabled(false);
00078     shortcutButton->setText("");
00079     defaultShortcut->setText("");
00080   }else{
00081     shortcutBox->setEnabled(true);
00082 
00083     shortcutButton->setCurrentShortcut( current->text(4).toInt(), (Qt::KeyboardModifiers) current->text(5).toInt() );
00084     defaultShortcut->setText( current->text(2) );
00085   }
00086 
00087 }
00088 
00090 void OptionsWidget::updateShortcut(){
00091 
00092   if ( keyTree->currentItem() == 0)
00093     return;
00094 
00095   bool myMultiUse = (bool) keyTree->currentItem()->text(6).toInt();
00096   QString myNewKey = shortcutButton->text();
00097 
00098    //check if the shortcut already exists
00099   for (int i=0; i < keyTree->topLevelItemCount(); i++)
00100     for (int j=0; j < keyTree->topLevelItem(i)->childCount(); j++){
00101 
00102       QTreeWidgetItem* item = keyTree->topLevelItem(i)->child(j);
00103       QString key = item->text(1);
00104       bool multiUse = (bool) item->text(6).toInt();
00105 
00106       if (keyTree->currentItem() == item)
00107         continue;
00108 
00109       if (key == myNewKey && ( !multiUse || !myMultiUse ) ){
00110         QMessageBox::warning(this, tr("OpenFlipper"), tr("Could not add Shortcut. Shortcut already assigned."), QMessageBox::Ok);
00111         return;
00112       }
00113     }
00114 
00115   //update the item
00116   if ( keyTree->currentItem() != 0){
00117     keyTree->currentItem()->setText( 1, shortcutButton->text() );
00118     keyTree->currentItem()->setText( 4, QString::number( shortcutButton->key() ) );
00119     keyTree->currentItem()->setText( 5, QString::number( shortcutButton->modifiers() ) );
00120   }
00121 
00122   keyTree->setFocus(Qt::TabFocusReason);
00123   keyTreeItemChanged(keyTree->currentItem(), 0);
00124 }
00125 
00127 void OptionsWidget::applyShortcuts(){
00128 
00129   //check all shortcuts
00130   for (int i=0; i < keyTree->topLevelItemCount(); i++)
00131     for (int j=0; j < keyTree->topLevelItem(i)->childCount(); j++){
00132 
00133       //check wether the shortcut changed
00134       QTreeWidgetItem* item = keyTree->topLevelItem(i)->child(j);
00135 
00136       if ( item->text(1) != item->text(7) ){
00137 
00138         QString pluginName = item->parent()->text(0);
00139 
00140         QObject* plugin = 0;
00141 
00142         if (pluginName != "Core"){
00143 
00144           //get the plugin object
00145           int index = getPluginInfo(pluginName);
00146 
00147           if (index == -1) //if pluginInfo was not found ->skip
00148             continue;
00149 
00150           plugin = plugins_[index].plugin;
00151         }
00152 
00153         int bindingID = item->text(3).toInt();
00154         int key = item->text(4).toInt();
00155 
00156         Qt::KeyboardModifiers modi = (Qt::KeyboardModifiers) item->text(5).toInt();
00157 
00158         emit addKeyMapping(key, modi, plugin, bindingID);
00159       }
00160     }
00161 }
00162 
00163 void OptionsWidget::restoreKeyPresets(){
00164 
00165    //check if the shortcut already exists
00166   for (int i=0; i < keyTree->topLevelItemCount(); i++)
00167     for (int j=0; j < keyTree->topLevelItem(i)->childCount(); j++){
00168 
00169       QTreeWidgetItem* item = keyTree->topLevelItem(i)->child(j);
00170       QString key = item->text(1);
00171 
00172       QString pluginName = item->parent()->text(0);
00173       int bindingID = item->text(3).toInt();
00174 
00175       if (pluginName == "Core"){
00176 
00177         item->setText( 1, item->text(2) );
00178         item->setText( 4, QString::number(coreKeys_[ bindingID ].key) );
00179         item->setText( 5, QString::number(coreKeys_[ bindingID ].modifiers) );
00180 
00181       } else {
00182 
00183         //get the plugin object
00184         int index = getPluginInfo(pluginName);
00185 
00186         if (index == -1) //if pluginInfo was not found ->skip
00187           continue;
00188 
00189         item->setText( 1, item->text(2) );
00190         item->setText( 4, QString::number(plugins_[index].keys[ bindingID ].key) );
00191         item->setText( 5, QString::number(plugins_[index].keys[ bindingID ].modifiers) );
00192       }
00193     }
00194 
00195   keyTree->setFocus(Qt::TabFocusReason);
00196   keyTreeItemChanged(keyTree->currentItem(), 0);
00197 }
00198 
00200 void OptionsWidget::initKeyTree(){
00201 
00202   keyTree->clear();
00203 
00204   keyTree->setEditTriggers(QAbstractItemView::NoEditTriggers);
00205 
00206   keyTree->setColumnCount ( 8 );
00207 
00208   QStringList headerdata;
00209   headerdata << "Action" << "Shortcut" << "Default" << "keyIndex" << "currentKey" << "currentModi" << "MultiUse" << "initShortcut";
00210   keyTree->setHeaderLabels(headerdata);
00211 
00212   //fill the keyTree
00213   std::map< QString, QTreeWidgetItem* > parentMap;
00214 
00215   InverseKeyMap::iterator it;
00216   for (it=keys_.begin(); it != keys_.end(); ++it){
00217 
00218     QObject* plugin = (*it).first.first;
00219     int bindingID = (*it).first.second;
00220     int key = (*it).second.first;
00221     Qt::KeyboardModifiers modifiers = (*it).second.second;
00222 
00223     //get plugin name
00224     QString name;
00225 
00226     if (plugin == 0) //parent is the core
00227       name = "Core";
00228     else{
00229 
00230       BaseInterface* basePlugin = qobject_cast< BaseInterface * >(plugin);
00231 
00232       if (basePlugin)
00233         name = basePlugin->name();
00234       else{
00235         //name not found so skip it
00236         continue;
00237       }
00238     }
00239 
00240     //get corresponding the pluginInfo object
00241     int i = -1;
00242 
00243     if (name != "Core"){
00244       i = getPluginInfo(name);
00245 
00246       if (i == -1) //if pluginInfo was not found ->skip
00247         continue;
00248     }
00249 
00250     //is the toplevel item already there?
00251     if ( parentMap.find(name) == parentMap.end() ){
00252       parentMap[name] = new QTreeWidgetItem(keyTree, QStringList(name));
00253       keyTree->addTopLevelItem( parentMap[name] );
00254     }
00255 
00256     QTreeWidgetItem* parent = parentMap[name];
00257 
00258     //get the default settings
00259     QString               description;
00260     bool                  multiUse;
00261     bool                  slot;
00262     int                   defKey;
00263     Qt::KeyboardModifiers defModi;
00264 
00265     if (name == "Core"){
00266 
00267       description = coreKeys_[bindingID].description;
00268       multiUse = coreKeys_[bindingID].multiUse;
00269       slot = coreKeys_[bindingID].slot;
00270       defKey = coreKeys_[bindingID].key;
00271       defModi = coreKeys_[bindingID].modifiers;
00272 
00273     } else {
00274 
00275       description = plugins_[i].keys[bindingID].description;
00276       multiUse = plugins_[i].keys[bindingID].multiUse;
00277       slot = plugins_[i].keys[bindingID].slot;
00278       defKey = plugins_[i].keys[bindingID].key;
00279       defModi = plugins_[i].keys[bindingID].modifiers;
00280     }
00281 
00282     QStringList rows;
00283 
00284     //setup the strings for the shortcuts
00285     QString keyString;
00286 
00287     if (key == -1 && modifiers == 0){
00288       keyString = "unassigned";
00289     }else if (key == Qt::Key_AltGr || key == Qt::Key_Alt || key == Qt::Key_Control || key == Qt::Key_Shift || key == Qt::Key_Meta){
00290       keyString = QKeySequence( modifiers ).toString();
00291       keyString = keyString.left(keyString.size()-1);
00292     }else
00293       keyString = QKeySequence( key + modifiers ).toString();
00294 
00295     QString defaultStr;
00296 
00297     if (defKey == -1 && defModi == 0){
00298       defaultStr = "unassigned";
00299     }else if (defKey == Qt::Key_AltGr || defKey == Qt::Key_Alt || defKey == Qt::Key_Control
00300            || defKey == Qt::Key_Shift || defKey == Qt::Key_Meta){
00301       defaultStr = QKeySequence( defModi ).toString();
00302       defaultStr = defaultStr.left(defaultStr.size()-1);
00303     }else
00304       defaultStr = QKeySequence( defKey + defModi ).toString();
00305 
00306     //and add the row
00307     rows << description << keyString << defaultStr << QString::number(bindingID) << QString::number(key)
00308          << QString::number(modifiers) << QString::number(multiUse) << keyString;
00309 
00310     QTreeWidgetItem* keyItem = new QTreeWidgetItem(parent, rows);
00311 
00312     parent->addChild( keyItem );
00313   }
00314 
00315 
00316   keyTree->setColumnWidth(0,350);
00317   keyTree->setColumnHidden(3, true);
00318   keyTree->setColumnHidden(4, true);
00319   keyTree->setColumnHidden(5, true);
00320   keyTree->setColumnHidden(6, true);
00321   keyTree->setColumnHidden(7, true);
00322 }

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .