Refactor the shortcut management for plugin macros

When saving a macro, a shortcut is created and the user can change the
shortcut in Options->Keyboard.
When the macro is removed, the shortcut is removed from the
actionManager using the removeShortcut method.
This is way simpler than before, where a number of default shortcuts
where defined and macros took the empty space.

Merge-request: 236
Reviewed-by: Tobias Hunger <tobias.hunger@nokia.com>
This commit is contained in:
Nicolas Arnaud-Cormos
2011-01-24 11:39:42 +01:00
committed by Tobias Hunger
parent f3d7bbff0c
commit 1f1656163c
13 changed files with 59 additions and 169 deletions

View File

@@ -60,12 +60,10 @@ public:
QString version; QString version;
QString fileName; QString fileName;
QList<MacroEvent> events; QList<MacroEvent> events;
int shortcutId;
}; };
Macro::MacroPrivate::MacroPrivate() : Macro::MacroPrivate::MacroPrivate() :
version(Core::Constants::IDE_VERSION_LONG), version(Core::Constants::IDE_VERSION_LONG)
shortcutId(-1)
{ {
} }
@@ -85,7 +83,6 @@ Macro::Macro(const Macro &other):
d->version = other.d->version; d->version = other.d->version;
d->fileName = other.d->fileName; d->fileName = other.d->fileName;
d->events = other.d->events; d->events = other.d->events;
d->shortcutId = other.d->shortcutId;
} }
Macro::~Macro() Macro::~Macro()
@@ -101,7 +98,6 @@ Macro& Macro::operator=(const Macro &other)
d->version = other.d->version; d->version = other.d->version;
d->fileName = other.d->fileName; d->fileName = other.d->fileName;
d->events = other.d->events; d->events = other.d->events;
d->shortcutId = other.d->shortcutId;
return *this; return *this;
} }
@@ -191,16 +187,6 @@ const QList<MacroEvent> &Macro::events() const
return d->events; return d->events;
} }
void Macro::setShortcutId(int id)
{
d->shortcutId = id;
}
int Macro::shortcutId() const
{
return d->shortcutId;
}
bool Macro::isWritable() const bool Macro::isWritable() const
{ {
QFileInfo fileInfo(d->fileName); QFileInfo fileInfo(d->fileName);

View File

@@ -34,6 +34,13 @@
#ifndef MACROSPLUGIN_MACRO_H #ifndef MACROSPLUGIN_MACRO_H
#define MACROSPLUGIN_MACRO_H #define MACROSPLUGIN_MACRO_H
#include <coreplugin/uniqueidmanager.h>
#include <QList>
#include <QString>
#include <QShortcut>
#include "macroevent.h"
#include "macros_global.h" #include "macros_global.h"
#include "macroevent.h" #include "macroevent.h"
@@ -64,9 +71,6 @@ public:
void append(const MacroEvent &event); void append(const MacroEvent &event);
const QList<MacroEvent> &events() const; const QList<MacroEvent> &events() const;
void setShortcutId(int id);
int shortcutId() const;
bool isWritable() const; bool isWritable() const;
private: private:

View File

@@ -114,8 +114,6 @@ public:
QList<IMacroHandler*> handlers; QList<IMacroHandler*> handlers;
QSignalMapper *mapper; QSignalMapper *mapper;
QMap<int, QShortcut *> shortcuts;
int currentId;
ActionMacroHandler *actionHandler; ActionMacroHandler *actionHandler;
TextEditorMacroHandler *textEditorHandler; TextEditorMacroHandler *textEditorHandler;
@@ -124,13 +122,10 @@ public:
void init(); void init();
void appendDirectory(const QString &directory); void appendDirectory(const QString &directory);
void removeDirectory(const QString &directory); void removeDirectory(const QString &directory);
void addMacro(Macro *macro); void addMacro(Macro *macro, QKeySequence ks=QKeySequence());
void removeMacro(const QString &name); void removeMacro(const QString &name);
void changeMacroDescription(Macro *macro, const QString &description); void changeMacroDescription(Macro *macro, const QString &description);
int addShortcut(Macro *macro, int id=-1);
void removeShortcut(Macro *macro);
bool executeMacro(Macro *macro); bool executeMacro(Macro *macro);
void showSaveDialog(); void showSaveDialog();
}; };
@@ -139,8 +134,7 @@ MacroManager::MacroManagerPrivate::MacroManagerPrivate(MacroManager *qq):
q(qq), q(qq),
currentMacro(0), currentMacro(0),
isRecording(false), isRecording(false),
mapper(new QSignalMapper(qq)), mapper(new QSignalMapper(qq))
currentId(0)
{ {
settings.fromSettings(Core::ICore::instance()->settings()); settings.fromSettings(Core::ICore::instance()->settings());
@@ -167,13 +161,13 @@ void MacroManager::MacroManagerPrivate::appendDirectory(const QString &directory
QString fileName = dir.absolutePath()+"/"+name; QString fileName = dir.absolutePath()+"/"+name;
Macro *macro = new Macro; Macro *macro = new Macro;
macro->loadHeader(fileName); macro->loadHeader(fileName);
addMacro(macro);
// Create shortcut // Create shortcut
if (settings.shortcutIds.contains(macro->displayName())) { QKeySequence ks;
int id = settings.shortcutIds.value(macro->displayName()).toInt(); if (settings.shortcuts.contains(macro->displayName()))
addShortcut(macro, id); ks.fromString(settings.shortcuts.value(macro->displayName()).toString());
}
addMacro(macro, ks);
} }
} }
@@ -192,8 +186,22 @@ void MacroManager::MacroManagerPrivate::removeDirectory(const QString &directory
removeMacro(name); removeMacro(name);
} }
void MacroManager::MacroManagerPrivate::addMacro(Macro *macro) void MacroManager::MacroManagerPrivate::addMacro(Macro *macro, QKeySequence ks)
{ {
// Add sortcut
Core::Context context(TextEditor::Constants::C_TEXTEDITOR);
Core::ICore *core = Core::ICore::instance();
Core::ActionManager *am = core->actionManager();
QShortcut *shortcut = new QShortcut(core->mainWindow());
shortcut->setWhatsThis(macro->description());
const QString macroId = QLatin1String(Constants::PREFIX_MACRO) + macro->displayName();
Core::Command *command = am->registerShortcut(shortcut, macroId, context);
if (!ks.isEmpty())
command->setDefaultKeySequence(ks);
connect(shortcut, SIGNAL(activated()), mapper, SLOT(map()));
mapper->setMapping(shortcut, macro->displayName());
// Add macro to the map
macros[macro->displayName()] = macro; macros[macro->displayName()] = macro;
} }
@@ -201,8 +209,13 @@ void MacroManager::MacroManagerPrivate::removeMacro(const QString &name)
{ {
if (!macros.contains(name)) if (!macros.contains(name))
return; return;
// Remove shortcut
Core::ICore *core = Core::ICore::instance();
Core::ActionManager *am = core->actionManager();
am->unregisterShortcut(Core::Id(Constants::PREFIX_MACRO+name));
// Remove macro from the map
Macro *macro = macros.take(name); Macro *macro = macros.take(name);
removeShortcut(macro);
delete macro; delete macro;
} }
@@ -211,52 +224,14 @@ void MacroManager::MacroManagerPrivate::changeMacroDescription(Macro *macro, con
macro->load(); macro->load();
macro->setDescription(description); macro->setDescription(description);
macro->save(macro->fileName()); macro->save(macro->fileName());
}
int MacroManager::MacroManagerPrivate::addShortcut(Macro *macro, int id) // Change shortcut what's this
{ Core::ICore *core = Core::ICore::instance();
if (id==-1) Core::ActionManager *am = core->actionManager();
id=currentId;
QShortcut *shortcut=0;
if (shortcuts.contains(id)) {
shortcut = shortcuts.value(id);
} else {
Core::Context context(TextEditor::Constants::C_TEXTEDITOR);
Core::ICore *core = Core::ICore::instance();
Core::ActionManager *am = core->actionManager();
shortcut = new QShortcut(core->mainWindow());
connect(shortcut, SIGNAL(activated()), mapper, SLOT(map()));
const QString macroId = QLatin1String(Constants::SHORTCUT_MACRO) + QString("%1").arg(id, 2, 10, QLatin1Char('0'));
Core::Command *command = am->registerShortcut(shortcut, macroId, context);
// Add a default shortcut for the 10 first shortcuts Core::Command *command = am->command(Core::Id(Constants::PREFIX_MACRO+macro->displayName()));
if (id < 10) if (command && command->shortcut())
command->setDefaultKeySequence(QKeySequence(QString(tr("Alt+R,Alt+%1").arg(id)))); command->shortcut()->setWhatsThis(description);
shortcuts[id] = shortcut;
}
// Update the current id (first id without shortcut)
while (shortcuts.contains(currentId))
currentId++;
// Assign the shortcut
macro->setShortcutId(id);
shortcut->setWhatsThis(macro->displayName());
mapper->setMapping(shortcut, macro->displayName());
return id;
}
void MacroManager::MacroManagerPrivate::removeShortcut(Macro *macro)
{
// Remove the old shortcut
if (macro->shortcutId() >= 0) {
QShortcut* shortcut = qobject_cast<QShortcut *>(mapper->mapping(macro->displayName()));
shortcut->setWhatsThis("");
if (shortcut)
mapper->removeMappings(shortcut);
if (currentId > macro->shortcutId())
currentId = macro->shortcutId();
macro->setShortcutId(-1);
}
} }
bool MacroManager::MacroManagerPrivate::executeMacro(Macro *macro) bool MacroManager::MacroManagerPrivate::executeMacro(Macro *macro)
@@ -325,9 +300,6 @@ void MacroManager::MacroManagerPrivate::showSaveDialog()
currentMacro->save(fileName); currentMacro->save(fileName);
addMacro(currentMacro); addMacro(currentMacro);
if (dialog.createShortcut())
settings.shortcutIds[dialog.name()] = addShortcut(currentMacro);
if (changed) if (changed)
q->saveSettings(); q->saveSettings();
} }
@@ -458,7 +430,7 @@ void MacroManager::deleteMacro(const QString &name)
if (macro) { if (macro) {
QString fileName = macro->fileName(); QString fileName = macro->fileName();
d->removeMacro(name); d->removeMacro(name);
d->settings.shortcutIds.remove(name); d->settings.shortcuts.remove(name);
QFile::remove(fileName); QFile::remove(fileName);
} }
} }
@@ -483,7 +455,7 @@ MacroManager *MacroManager::instance()
return m_instance; return m_instance;
} }
void MacroManager::changeMacro(const QString &name, const QString &description, bool shortcut) void MacroManager::changeMacro(const QString &name, const QString &description)
{ {
if (!d->macros.contains(name)) if (!d->macros.contains(name))
return; return;
@@ -492,16 +464,4 @@ void MacroManager::changeMacro(const QString &name, const QString &description,
// Change description // Change description
if (macro->description() != description) if (macro->description() != description)
d->changeMacroDescription(macro, description); d->changeMacroDescription(macro, description);
// Change shortcut
if ((macro->shortcutId()==-1 && !shortcut)
|| (macro->shortcutId()>=0 && shortcut))
return;
if (shortcut) {
d->settings.shortcutIds[name] = d->addShortcut(macro);
} else {
d->removeShortcut(macro);
d->settings.shortcutIds.remove(name);
}
} }

View File

@@ -77,7 +77,7 @@ protected:
friend class Internal::MacroOptionsWidget; friend class Internal::MacroOptionsWidget;
void deleteMacro(const QString &name); void deleteMacro(const QString &name);
void changeMacro(const QString &name, const QString &description, bool shortcut); void changeMacro(const QString &name, const QString &description);
void appendDirectory(const QString &directory); void appendDirectory(const QString &directory);
void removeDirectory(const QString &directory); void removeDirectory(const QString &directory);
void setDefaultDirectory(const QString &directory); void setDefaultDirectory(const QString &directory);

View File

@@ -87,8 +87,6 @@ MacroOptionsWidget::MacroOptionsWidget(QWidget *parent) :
this, SLOT(addDirectoy())); this, SLOT(addDirectoy()));
connect(ui->description, SIGNAL(textChanged(QString)), connect(ui->description, SIGNAL(textChanged(QString)),
this, SLOT(changeDescription(QString))); this, SLOT(changeDescription(QString)));
connect(ui->assignShortcut, SIGNAL(toggled(bool)),
this, SLOT(changeShortcut(bool)));
ui->treeWidget->header()->setSortIndicator(0, Qt::AscendingOrder); ui->treeWidget->header()->setSortIndicator(0, Qt::AscendingOrder);
} }
@@ -125,6 +123,7 @@ void MacroOptionsWidget::appendDirectory(const QString &directory, bool isDefaul
Core::ICore *core = Core::ICore::instance(); Core::ICore *core = Core::ICore::instance();
Core::ActionManager *am = core->actionManager(); Core::ActionManager *am = core->actionManager();
QMapIterator<QString, Macro *> it(MacroManager::instance()->macros()); QMapIterator<QString, Macro *> it(MacroManager::instance()->macros());
while (it.hasNext()) { while (it.hasNext()) {
it.next(); it.next();
@@ -135,14 +134,10 @@ void MacroOptionsWidget::appendDirectory(const QString &directory, bool isDefaul
macroItem->setText(1, it.value()->description()); macroItem->setText(1, it.value()->description());
macroItem->setData(0, NAME_ROLE, it.value()->displayName()); macroItem->setData(0, NAME_ROLE, it.value()->displayName());
macroItem->setData(0, WRITE_ROLE, it.value()->isWritable()); macroItem->setData(0, WRITE_ROLE, it.value()->isWritable());
macroItem->setData(0, ID_ROLE, it.value()->shortcutId());
if (it.value()->shortcutId() >= 0) { Core::Command *command = am->command(Core::Id(Constants::PREFIX_MACRO+it.value()->displayName()));
QString textId = QString("%1").arg(it.value()->shortcutId(), 2, 10, QLatin1Char('0')); if (command && command->shortcut())
QString commandId = QLatin1String(Constants::SHORTCUT_MACRO)+textId; macroItem->setText(2, command->shortcut()->key().toString());
QString shortcut = am->command(commandId)->keySequence().toString();
macroItem->setText(2, QString("%1 (%2)").arg(shortcut).arg(commandId));
}
} }
} }
} }
@@ -160,7 +155,6 @@ void MacroOptionsWidget::changeCurrentItem(QTreeWidgetItem *current)
ui->removeButton->setEnabled(false); ui->removeButton->setEnabled(false);
ui->defaultButton->setEnabled(false); ui->defaultButton->setEnabled(false);
ui->description->clear(); ui->description->clear();
ui->assignShortcut->setChecked(false);
ui->macroGroup->setEnabled(false); ui->macroGroup->setEnabled(false);
} }
else if (current->type() == DIRECTORY) { else if (current->type() == DIRECTORY) {
@@ -168,14 +162,12 @@ void MacroOptionsWidget::changeCurrentItem(QTreeWidgetItem *current)
ui->removeButton->setEnabled(!isDefault); ui->removeButton->setEnabled(!isDefault);
ui->defaultButton->setEnabled(!isDefault); ui->defaultButton->setEnabled(!isDefault);
ui->description->clear(); ui->description->clear();
ui->assignShortcut->setChecked(false);
ui->macroGroup->setEnabled(false); ui->macroGroup->setEnabled(false);
} else { } else {
ui->removeButton->setEnabled(true); ui->removeButton->setEnabled(true);
ui->defaultButton->setEnabled(false); ui->defaultButton->setEnabled(false);
ui->description->setText(current->text(1)); ui->description->setText(current->text(1));
ui->description->setEnabled(current->data(0, WRITE_ROLE).toBool()); ui->description->setEnabled(current->data(0, WRITE_ROLE).toBool());
ui->assignShortcut->setChecked(current->data(0, ID_ROLE).toInt() >= 0);
ui->macroGroup->setEnabled(true); ui->macroGroup->setEnabled(true);
} }
changingCurrent = false; changingCurrent = false;
@@ -217,8 +209,7 @@ void MacroOptionsWidget::apply()
QMapIterator<QString, ChangeSet> it(m_macroToChange); QMapIterator<QString, ChangeSet> it(m_macroToChange);
while (it.hasNext()) { while (it.hasNext()) {
it.next(); it.next();
MacroManager::instance()->changeMacro(it.key(), it.value().description, MacroManager::instance()->changeMacro(it.key(), it.value().description);
it.value().shortcut);
} }
// Get list of dir to append or remove // Get list of dir to append or remove
@@ -263,21 +254,6 @@ void MacroOptionsWidget::changeData(QTreeWidgetItem *current, int column, QVaria
font.setItalic(true); font.setItalic(true);
current->setFont(1, font); current->setFont(1, font);
} }
// Change the shortcut
if (column == 2) {
bool shortcut = value.toBool();
m_macroToChange[macroName].shortcut = shortcut;
QFont font = current->font(2);
if (current->data(0, ID_ROLE).toInt() >= 0) {
font.setStrikeOut(!shortcut);
font.setItalic(!shortcut);
}
else {
font.setItalic(shortcut);
current->setText(2, shortcut?tr("create shortcut"):"");
}
current->setFont(2, font);
}
} }
void MacroOptionsWidget::changeDescription(const QString &description) void MacroOptionsWidget::changeDescription(const QString &description)
@@ -287,11 +263,3 @@ void MacroOptionsWidget::changeDescription(const QString &description)
return; return;
changeData(current, 1, description); changeData(current, 1, description);
} }
void MacroOptionsWidget::changeShortcut(bool shortcut)
{
QTreeWidgetItem *current = ui->treeWidget->currentItem();
if (changingCurrent || !current || current->type() == DIRECTORY)
return;
changeData(current, 2, shortcut);
}

View File

@@ -75,7 +75,6 @@ private:
private slots: private slots:
void changeDescription(const QString &description); void changeDescription(const QString &description);
void changeShortcut(bool shortcut);
private: private:
Ui::MacroOptionsWidget *ui; Ui::MacroOptionsWidget *ui;

View File

@@ -82,7 +82,7 @@
</column> </column>
<column> <column>
<property name="text"> <property name="text">
<string>Shortcut (Command)</string> <string>Shortcut</string>
</property> </property>
</column> </column>
</widget> </widget>
@@ -143,18 +143,6 @@
<item row="0" column="1"> <item row="0" column="1">
<widget class="QLineEdit" name="description"/> <widget class="QLineEdit" name="description"/>
</item> </item>
<item row="1" column="0" colspan="2">
<widget class="QCheckBox" name="assignShortcut">
<property name="toolTip">
<string>Add or remove a shortcut for this macro.
You can change the default shortcut in the
Environment:Keyboard page.</string>
</property>
<property name="text">
<string>Assign a shortcut</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>

View File

@@ -42,7 +42,7 @@ const char * const M_TOOLS_MACRO = "Macros.Tools.Menu";
const char * const START_MACRO = "Macros.StartMacro"; const char * const START_MACRO = "Macros.StartMacro";
const char * const END_MACRO = "Macros.EndMacro"; const char * const END_MACRO = "Macros.EndMacro";
const char * const EXECUTE_LAST_MACRO = "Macros.ExecuteLastMacro"; const char * const EXECUTE_LAST_MACRO = "Macros.ExecuteLastMacro";
const char * const SHORTCUT_MACRO = "Macros.Shortcut-"; const char * const PREFIX_MACRO = "Macros.";
const char * const M_OPTIONS_PAGE = "Macros"; const char * const M_OPTIONS_PAGE = "Macros";
const char * const M_OPTIONS_TR_PAGE = "Macros"; const char * const M_OPTIONS_TR_PAGE = "Macros";

View File

@@ -41,7 +41,7 @@ static const char GROUP[] = "Macro";
static const char DEFAULT_DIRECTORY[] = "DefaultDirectory"; static const char DEFAULT_DIRECTORY[] = "DefaultDirectory";
static const char SHOW_SAVE_DIALOG[] = "ShowSaveDialog"; static const char SHOW_SAVE_DIALOG[] = "ShowSaveDialog";
static const char DIRECTORIES[] = "Directories"; static const char DIRECTORIES[] = "Directories";
static const char SHORTCUTIDS[] = "ShortcutIds"; static const char SHORTCUTS[] = "Shortcuts";
MacroSettings::MacroSettings(): MacroSettings::MacroSettings():
@@ -55,7 +55,7 @@ void MacroSettings::toSettings(QSettings *s) const
s->setValue(QLatin1String(DEFAULT_DIRECTORY), defaultDirectory); s->setValue(QLatin1String(DEFAULT_DIRECTORY), defaultDirectory);
s->setValue(QLatin1String(SHOW_SAVE_DIALOG), showSaveDialog); s->setValue(QLatin1String(SHOW_SAVE_DIALOG), showSaveDialog);
s->setValue(QLatin1String(DIRECTORIES), directories); s->setValue(QLatin1String(DIRECTORIES), directories);
s->setValue(QLatin1String(SHORTCUTIDS), shortcutIds); s->setValue(QLatin1String(SHORTCUTS), shortcuts);
s->endGroup(); s->endGroup();
} }
@@ -65,14 +65,14 @@ void MacroSettings::fromSettings(QSettings *s)
defaultDirectory = s->value(QLatin1String(DEFAULT_DIRECTORY), QString("")).toString(); defaultDirectory = s->value(QLatin1String(DEFAULT_DIRECTORY), QString("")).toString();
showSaveDialog = s->value(QLatin1String(SHOW_SAVE_DIALOG), false).toBool(); showSaveDialog = s->value(QLatin1String(SHOW_SAVE_DIALOG), false).toBool();
directories = s->value(QLatin1String(DIRECTORIES)).toStringList(); directories = s->value(QLatin1String(DIRECTORIES)).toStringList();
shortcutIds = s->value(QLatin1String(SHORTCUTIDS)).toMap(); shortcuts = s->value(QLatin1String(SHORTCUTS)).toMap();
s->endGroup(); s->endGroup();
} }
bool MacroSettings::equals(const MacroSettings &ms) const bool MacroSettings::equals(const MacroSettings &ms) const
{ {
return defaultDirectory == ms.defaultDirectory && return defaultDirectory == ms.defaultDirectory &&
shortcutIds == ms.shortcutIds && shortcuts == ms.shortcuts &&
directories == ms.directories && directories == ms.directories &&
showSaveDialog == ms.showSaveDialog; showSaveDialog == ms.showSaveDialog;
} }

View File

@@ -58,7 +58,7 @@ public:
QString defaultDirectory; QString defaultDirectory;
QStringList directories; QStringList directories;
QMap<QString, QVariant> shortcutIds; QMap<QString, QVariant> shortcuts;
bool showSaveDialog; bool showSaveDialog;
}; };

View File

@@ -67,8 +67,3 @@ bool SaveDialog::hideSaveDialog() const
{ {
return ui->hideSaveDialog->isChecked(); return ui->hideSaveDialog->isChecked();
} }
bool SaveDialog::createShortcut() const
{
return ui->createShortcut->isChecked();
}

View File

@@ -54,7 +54,6 @@ public:
QString name() const; QString name() const;
QString description() const; QString description() const;
bool hideSaveDialog() const; bool hideSaveDialog() const;
bool createShortcut() const;
private: private:
Ui::SaveDialog *ui; Ui::SaveDialog *ui;

View File

@@ -7,7 +7,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>219</width> <width>219</width>
<height>153</height> <height>116</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@@ -51,14 +51,7 @@
<item row="4" column="0" colspan="2"> <item row="4" column="0" colspan="2">
<widget class="QDialogButtonBox" name="buttonBox"> <widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons"> <property name="standardButtons">
<set>QDialogButtonBox::Ok</set> <set>QDialogButtonBox::Cancel|QDialogButtonBox::Save</set>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<widget class="QCheckBox" name="createShortcut">
<property name="text">
<string>Create a shortcut</string>
</property> </property>
</widget> </widget>
</item> </item>
@@ -67,8 +60,6 @@
<tabstops> <tabstops>
<tabstop>name</tabstop> <tabstop>name</tabstop>
<tabstop>description</tabstop> <tabstop>description</tabstop>
<tabstop>createShortcut</tabstop>
<tabstop>buttonBox</tabstop>
<tabstop>hideSaveDialog</tabstop> <tabstop>hideSaveDialog</tabstop>
</tabstops> </tabstops>
<resources/> <resources/>