added shortcuts conflicts detection

This commit is contained in:
Luis Ángel San Martín
2014-07-22 22:02:06 +02:00
parent 2712ef89b2
commit eb095bcdaf
8 changed files with 78 additions and 14 deletions

View File

@ -80,9 +80,15 @@ bool ActionsShortcutsModel::setData(const QModelIndex &index, const QVariant &va
{
if(index.column() == KEYS)
{
actions[index.row()]->setShortcut(value.toString());
ShortcutsManager::getShortcutsManager().saveShortcut(actions[index.row()]);
return true;
ShortcutsManager sm = ShortcutsManager::getShortcutsManager();
if(sm.checkConflicts(value.toString(), actions[index.row()]))
emit conflict(value.toString());
else
{
actions[index.row()]->setShortcut(value.toString());
ShortcutsManager::getShortcutsManager().saveShortcut(actions[index.row()]);
return true;
}
}
return false;
}

View File

@ -28,7 +28,7 @@ public:
KEYS
};
signals:
void conflict(QString);
public slots:
protected:

View File

@ -11,6 +11,7 @@
#include <QPushButton>
#include <QHeaderView>
#include <QLabel>
#include <QMessageBox>
#include "QsLog.h"
@ -59,6 +60,7 @@ EditShortcutsDialog::EditShortcutsDialog(QWidget *parent) :
connect(resetButton,SIGNAL(clicked()),this,SLOT(resetToDefaults()));
connect(actionsGroupsListView->selectionModel(),SIGNAL(currentChanged(QModelIndex,QModelIndex)),this,SLOT(loadShortcuts(QModelIndex,QModelIndex))); //clicked(QModelIndex) doesn't work :S
connect(actionsModel,SIGNAL(conflict(QString)),this,SLOT(processConflict(QString)));
#ifdef Q_OS_MAC
setFixedSize(760,500);
@ -86,3 +88,8 @@ void EditShortcutsDialog::loadShortcuts(const QModelIndex &mi,const QModelIndex
{
actionsModel->addActions(groupsModel->getActions(mi));
}
void EditShortcutsDialog::processConflict(const QString &shortcutInConflict)
{
QMessageBox::warning(this,tr("Shortcut in use"), QString(tr("The shortcut \"%1\" is already assigned to other function")).arg(shortcutInConflict));
}

View File

@ -21,6 +21,7 @@ signals:
public slots:
void resetToDefaults();
void loadShortcuts(const QModelIndex & mi,const QModelIndex &mi2);
void processConflict(const QString & shortcutInConflict);
protected:
QListView * actionsGroupsListView;

View File

@ -93,3 +93,20 @@ void ShortcutsManager::saveShortcut(QAction *action)
return s.setValue(action->data().toString() , action->shortcut().toString());
}
void ShortcutsManager::registerActions(const QList<QAction *> &a)
{
actions = a;
}
bool ShortcutsManager::checkConflicts(const QKeySequence & shortcut, const QAction *dest)
{
foreach(QAction * action, actions)
{
if(action != dest) //if the same shortcut is setted there is no conflict
if(action->shortcut() == shortcut)
return true;
}
return false;
}

View File

@ -16,6 +16,8 @@ class ShortcutsManager
private:
ShortcutsManager();
QMap<QString,QKeySequence> defaultShorcuts;
QList<QAction *> actions; //all actions registered, used for checking conflicts
void initDefaultShorcuts();
public:
static ShortcutsManager & getShortcutsManager()
@ -27,6 +29,8 @@ public:
void resetToDefaults();
QString getShortcut(const QString & name);
void saveShortcut(QAction * action);
void registerActions(const QList<QAction *> & actions);
bool checkConflicts(const QKeySequence &shortcut, const QAction *dest);
};
//ACTION NAMES YACReaderLibrary