diff --git a/Core/Core.cc b/Core/Core.cc index 9ae8981f51e42b368a53905fccace3c31490fb5c..c522f2b194cdda7a2ae2337a13d4167a64ae4f38 100644 --- a/Core/Core.cc +++ b/Core/Core.cc @@ -394,6 +394,9 @@ Core::init() { //register keyBinding for all scripting slots coreWidget_->slotRegisterSlotKeyBindings(); + //get keyAssignments from config files + restoreKeyBindings(); + if ( OpenFlipper::Options::defaultToolboxMode( ) != "" ) coreWidget_->setViewMode( OpenFlipper::Options::defaultToolboxMode() ); else @@ -853,8 +856,11 @@ void Core::slotLogToFile(Logtype _type, QString _message){ return; if (logStream_ == 0){ - //check if a logfile has been specified - if (OpenFlipper::Options::logFile() == "") + //check if a logfile has been specified and if the path is valid + + QFileInfo fi( OpenFlipper::Options::logFile() ); + + if (OpenFlipper::Options::logFile() == "" || !fi.dir().exists() ) OpenFlipper::Options::logFile(QDir::home().absolutePath() + OpenFlipper::Options::dirSeparator() + ".OpenFlipper" + OpenFlipper::Options::dirSeparator() + "OpenFlipper.log"); diff --git a/Core/Core.hh b/Core/Core.hh index cc630f54b2e60d2e4d419414634a2aca41fe9446..ac2ca2f97cdec1c33bd3530669cd5ae544007935 100644 --- a/Core/Core.hh +++ b/Core/Core.hh @@ -441,6 +441,9 @@ private: /// Write Application options to ini file void writeApplicationOptions(INIFile& _ini); + /// Restore key assignments from configs files + void restoreKeyBindings(); + public slots: /// Save the current options to the standard ini file diff --git a/Core/ParseIni.cc b/Core/ParseIni.cc index b2936f2aededd485c3cf2ec99b76ba16d55467d6..c1244dce193aea66f87f0f0606046471229e8344 100644 --- a/Core/ParseIni.cc +++ b/Core/ParseIni.cc @@ -352,6 +352,10 @@ void Core::writeApplicationOptions(INIFile& _ini) { for (int i=0; i < entries.size(); i++) _ini.add_entry("Options","ViewMode" + QString::number(i) ,entries[i]); + //save KeyBindings + if ( OpenFlipper::Options::gui() ) + coreWidget_->saveKeyBindings(_ini); + //============================================================================ // Splash Screen //============================================================================ diff --git a/Core/optionHandling.cc b/Core/optionHandling.cc index 15b8761f68b509461e3bf366333529e8224944ef..08560caa6101f1db93a59681567cabb69f667a45 100644 --- a/Core/optionHandling.cc +++ b/Core/optionHandling.cc @@ -269,4 +269,47 @@ void Core::setupOptions() { } +/// restore key Assignments that were saved in config files +void Core::restoreKeyBindings(){ + + QStringList optionFiles; + + // ============================================================== + // Global ini file in the application directory + // ============================================================== + QFile globalIni(OpenFlipper::Options::applicationDir().absolutePath() + OpenFlipper::Options::dirSeparator() + "OpenFlipper.ini"); + if ( globalIni.exists() ) + optionFiles.push_back(OpenFlipper::Options::applicationDir().absolutePath() + OpenFlipper::Options::dirSeparator() + "OpenFlipper.ini"); + + // ============================================================== + // Local ini file in the users home directory + // ============================================================== + QFile localIni(OpenFlipper::Options::configDirStr() + OpenFlipper::Options::dirSeparator() + "OpenFlipper.ini"); + if ( localIni.exists() ) + optionFiles.push_back(OpenFlipper::Options::configDirStr() + OpenFlipper::Options::dirSeparator() + "OpenFlipper.ini"); + + // Set the previously generated Optionfiles + OpenFlipper::Options::optionFiles(optionFiles); + + + // ============================================================== + // Load Application options from all files available + // ============================================================== + + for ( int i = 0 ; i < (int)optionFiles.size(); ++i) { + + INIFile _ini; + + if ( ! _ini.connect(optionFiles[i],false) ) { + emit log(LOGERR,"Failed to connect to _ini file" + optionFiles[i]); + continue; + } + + if ( OpenFlipper::Options::gui() ) + coreWidget_->loadKeyBindings(_ini); + + _ini.disconnect(); + } +} + //============================================================================= diff --git a/widgets/coreWidget/CoreWidget.hh b/widgets/coreWidget/CoreWidget.hh index 7bf6e4d586c79b0ab6150dfcc2d7609a3a02661e..95c80c06a41e66a62db9c83829463815b2332400 100644 --- a/widgets/coreWidget/CoreWidget.hh +++ b/widgets/coreWidget/CoreWidget.hh @@ -78,6 +78,7 @@ #include +#include struct ViewMode{ QString name; @@ -219,14 +220,35 @@ public: QList< SlotInfo >& coreSlots_; private slots: + /** \brief key registration + * + * internal slot that registers a keyCombination to the sender() who emitted the registerKey() signal + * only after registering keyCombinations plugins receive keyEvents to the corresponding keyCombinations via KeyInterface. + */ void slotRegisterKey(int _key, Qt::KeyboardModifiers _modifiers, QString _description, bool _multiUse = false); public slots: + /** \brief add a new key Mapping + * + * internal function that stores a new keyCombination assignment for an existing keyBinding + */ void slotAddKeyMapping(int _key, Qt::KeyboardModifiers _modifiers, QObject* _plugin, int _keyBindingID); + /** \brief register scripting slots to allow keyBindings + * + * internal function that registers all public slots without parameters of each plugin (or core) + * to the key management system. This allows the assignment of keyBindings for each of these slots. + */ void slotRegisterSlotKeyBindings(); + public: + ///Load key assignments from a given INI file + void loadKeyBindings(INIFile& _ini); + + ///Store current key assignments to a given INI file + void saveKeyBindings(INIFile& _ini); + /** @} */ //=========================================================================== diff --git a/widgets/coreWidget/keyHandling.cc b/widgets/coreWidget/keyHandling.cc index d2a79fd7ddf99e46eb339a4d682ad96c5a94c532..f93bc8d9ff31d787561d978ef4fbd2a643801395 100644 --- a/widgets/coreWidget/keyHandling.cc +++ b/widgets/coreWidget/keyHandling.cc @@ -336,6 +336,115 @@ void CoreWidget::slotAddKeyMapping(int _key, Qt::KeyboardModifiers _modifiers, Q } +///Load key assignments from a given INI file +void CoreWidget::loadKeyBindings(INIFile& _ini){ + + QVector< int > keys; + QVector< int > modifiers; + QStringList pluginNames; + QVector< int > bindingIDs; + + //first load everything from INI file + if ( !_ini.section_exists("KeyBindings") ) + return; + + int keyCount; + if (_ini.get_entry(keyCount,"KeyBindings","KeyCount") ){ + + int key; + int mod; + QString name; + int binding; + + for (int i=0; i < keyCount; i++){ + + if (!_ini.get_entry(key, "KeyBindings","Key" + QString::number(i) ) ) continue; + if (!_ini.get_entry(mod, "KeyBindings","KeyModifiers" + QString::number(i) ) ) continue; + if (!_ini.get_entry(name, "KeyBindings","KeyTarget" + QString::number(i) ) ) continue; + if (!_ini.get_entry(binding, "KeyBindings","KeyBinding" + QString::number(i) ) ) continue; + + keys.push_back( key ); + modifiers.push_back( mod ); + pluginNames.push_back( name ); + bindingIDs.push_back( binding ); + } + } + + //add the keyMapping + for (int i=0; i < keys.count(); i++){ + + //first we need the plugin + QObject* plugin = 0; + + if (pluginNames[i] != "Core" ){ + //search for the plugin + for (uint i=0; i < plugins_.size(); i++) + if (plugins_[i].rpcName == pluginNames[i] ){ + plugin = plugins_[i].plugin; + break; + } + + if (plugin == 0) + continue; //because plugin was not found + } + + slotAddKeyMapping( keys[i], (Qt::KeyboardModifiers) modifiers[i], plugin, bindingIDs[i] ); + } + +} + +///Store current key assignments to a given INI file +void CoreWidget::saveKeyBindings(INIFile& _ini){ + + QVector< int > keys; + QVector< int > modifiers; + QStringList pluginNames; + QVector< int > bindingIDs; + + //first get all keys with custom assignments + KeyMap::iterator it; + for (it=keys_.begin(); it != keys_.end(); ++it){ + + int key = (*it).first.first; + Qt::KeyboardModifiers mod = (*it).first.second; + QObject* plugin = (*it).second.first; + int bindingID = (*it).second.second; + KeyBinding binding = getKeyBinding(plugin, bindingID); + + //check if current key assignment and original assignment differ + if (key != binding.key || mod != binding.modifiers){ + + //get the pluginName + QString name; + + if (plugin == 0) + name = "Core"; + else + name = getRPCName(plugin); + + //store key assignment + keys.push_back( key ); + modifiers.push_back( mod ); + pluginNames.push_back( name ); + bindingIDs.push_back( bindingID ); + } + } + + //finally store everything to INI file + if ( !_ini.section_exists("KeyBindings") ) + _ini.add_section("KeyBindings"); + + _ini.add_entry("KeyBindings","KeyCount", keys.count()); + + for (int i=0; i < keys.count(); i++){ + + _ini.add_entry("KeyBindings","Key" + QString::number(i) , keys[i] ); + _ini.add_entry("KeyBindings","KeyModifiers" + QString::number(i), modifiers[i] ); + _ini.add_entry("KeyBindings","KeyTarget" + QString::number(i) , pluginNames[i] ); + _ini.add_entry("KeyBindings","KeyBinding" + QString::number(i) , bindingIDs[i] ); + } +} + /// if a keyPressEvent belongs to the core this functions is called void CoreWidget::registerCoreKeys() { diff --git a/widgets/optionsWidget/keyBindings.cc b/widgets/optionsWidget/keyBindings.cc index ee13a22015c023a1f12e3f3c3a17aa2e9943fc9f..77e1cacb127312cd9b2bb978258995a13a552464 100644 --- a/widgets/optionsWidget/keyBindings.cc +++ b/widgets/optionsWidget/keyBindings.cc @@ -112,12 +112,48 @@ void OptionsWidget::applyShortcuts(){ int key = item->text(4).toInt(); Qt::KeyboardModifiers modi = (Qt::KeyboardModifiers) item->text(5).toInt(); - + emit addKeyMapping(key, modi, plugin, bindingID); } } } +void OptionsWidget::restoreKeyPresets(){ + + //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); + + QString pluginName = item->parent()->text(0); + int bindingID = item->text(3).toInt(); + + if (pluginName == "Core"){ + + item->setText( 1, item->text(2) ); + item->setText( 4, QString::number(coreKeys_[ bindingID ].key) ); + item->setText( 5, QString::number(coreKeys_[ bindingID ].modifiers) ); + + } else { + + //get the plugin object + int index = getPluginInfo(pluginName); + + if (index == -1) //if pluginInfo was not found ->skip + continue; + + item->setText( 1, item->text(2) ); + item->setText( 4, QString::number(plugins_[index].keys[ bindingID ].key) ); + item->setText( 5, QString::number(plugins_[index].keys[ bindingID ].modifiers) ); + } + } + + keyTree->setFocus(Qt::TabFocusReason); + keyTreeItemChanged(keyTree->currentItem(), 0); +} + /// init the TreeWidget containing the keyBindings void OptionsWidget::initKeyTree(){ @@ -150,7 +186,7 @@ void OptionsWidget::initKeyTree(){ else{ BaseInterface* basePlugin = qobject_cast< BaseInterface * >(plugin); - + if (basePlugin) name = basePlugin->name(); else{ @@ -226,7 +262,7 @@ void OptionsWidget::initKeyTree(){ defaultStr = QKeySequence( defKey + defModi ).toString(); //and add the row - rows << description << keyString << defaultStr << QString::number(bindingID) << QString::number(key) + rows << description << keyString << defaultStr << QString::number(bindingID) << QString::number(key) << QString::number(modifiers) << QString::number(multiUse) << keyString; QTreeWidgetItem* keyItem = new QTreeWidgetItem(parent, rows); diff --git a/widgets/optionsWidget/optionsWidget.cc b/widgets/optionsWidget/optionsWidget.cc index af9158dad8546a7139b7f3527fc07dfe917bee5f..9a85b696755602abc7d91c3b9bd50d0d6415deb5 100644 --- a/widgets/optionsWidget/optionsWidget.cc +++ b/widgets/optionsWidget/optionsWidget.cc @@ -180,7 +180,7 @@ void OptionsWidget::showEvent ( QShowEvent * /*event*/ ) { connect(shortcutButton, SIGNAL(keyChanged()), this, SLOT(updateShortcut()) ); - + connect(presetsButton, SIGNAL(clicked()), this, SLOT(restoreKeyPresets()) ); for (int i = 0 ; i < availDrawModes->count(); ++i ) availDrawModes->item( i )->setCheckState(Qt::Unchecked) ; diff --git a/widgets/optionsWidget/optionsWidget.hh b/widgets/optionsWidget/optionsWidget.hh index 4c3ae567fd696e7e5544700b5746c48dc1b716d8..da39116cf1ffb61f9f9448f953f3a7ac5f453a0f 100644 --- a/widgets/optionsWidget/optionsWidget.hh +++ b/widgets/optionsWidget/optionsWidget.hh @@ -88,6 +88,9 @@ private slots: void slotShowPluginOptions(const QString& _pluginName ); + ///restore keyBinding Presets + void restoreKeyPresets(); + protected: void showEvent ( QShowEvent * event ); diff --git a/widgets/optionsWidget/optionsWidget.ui b/widgets/optionsWidget/optionsWidget.ui index 47184dbc86328255eebeb64993d6a019611d6fb3..8ef75692dbe54aec64bbfccfd1d90a6160842cf2 100644 --- a/widgets/optionsWidget/optionsWidget.ui +++ b/widgets/optionsWidget/optionsWidget.ui @@ -22,7 +22,7 @@ - 2 + 4 true @@ -489,6 +489,36 @@ You can select multiple modes at the same time. + + + + + + Qt::Horizontal + + + + 188 + 23 + + + + + + + + Restore default shortcuts + + + Restore default shortcuts + + + Restore Presets + + + + +