Commit 1f165616 authored by Nicolas Arnaud-Cormos's avatar Nicolas Arnaud-Cormos Committed by Tobias Hunger
Browse files

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: default avatarTobias Hunger <tobias.hunger@nokia.com>
parent f3d7bbff
......@@ -60,12 +60,10 @@ public:
QString version;
QString fileName;
QList<MacroEvent> events;
int shortcutId;
};
Macro::MacroPrivate::MacroPrivate() :
version(Core::Constants::IDE_VERSION_LONG),
shortcutId(-1)
version(Core::Constants::IDE_VERSION_LONG)
{
}
......@@ -85,7 +83,6 @@ Macro::Macro(const Macro &other):
d->version = other.d->version;
d->fileName = other.d->fileName;
d->events = other.d->events;
d->shortcutId = other.d->shortcutId;
}
Macro::~Macro()
......@@ -101,7 +98,6 @@ Macro& Macro::operator=(const Macro &other)
d->version = other.d->version;
d->fileName = other.d->fileName;
d->events = other.d->events;
d->shortcutId = other.d->shortcutId;
return *this;
}
......@@ -191,16 +187,6 @@ const QList<MacroEvent> &Macro::events() const
return d->events;
}
void Macro::setShortcutId(int id)
{
d->shortcutId = id;
}
int Macro::shortcutId() const
{
return d->shortcutId;
}
bool Macro::isWritable() const
{
QFileInfo fileInfo(d->fileName);
......
......@@ -34,6 +34,13 @@
#ifndef 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 "macroevent.h"
......@@ -64,9 +71,6 @@ public:
void append(const MacroEvent &event);
const QList<MacroEvent> &events() const;
void setShortcutId(int id);
int shortcutId() const;
bool isWritable() const;
private:
......
......@@ -114,8 +114,6 @@ public:
QList<IMacroHandler*> handlers;
QSignalMapper *mapper;
QMap<int, QShortcut *> shortcuts;
int currentId;
ActionMacroHandler *actionHandler;
TextEditorMacroHandler *textEditorHandler;
......@@ -124,13 +122,10 @@ public:
void init();
void appendDirectory(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 changeMacroDescription(Macro *macro, const QString &description);
int addShortcut(Macro *macro, int id=-1);
void removeShortcut(Macro *macro);
bool executeMacro(Macro *macro);
void showSaveDialog();
};
......@@ -139,8 +134,7 @@ MacroManager::MacroManagerPrivate::MacroManagerPrivate(MacroManager *qq):
q(qq),
currentMacro(0),
isRecording(false),
mapper(new QSignalMapper(qq)),
currentId(0)
mapper(new QSignalMapper(qq))
{
settings.fromSettings(Core::ICore::instance()->settings());
......@@ -167,13 +161,13 @@ void MacroManager::MacroManagerPrivate::appendDirectory(const QString &directory
QString fileName = dir.absolutePath()+"/"+name;
Macro *macro = new Macro;
macro->loadHeader(fileName);
addMacro(macro);
// Create shortcut
if (settings.shortcutIds.contains(macro->displayName())) {
int id = settings.shortcutIds.value(macro->displayName()).toInt();
addShortcut(macro, id);
}
QKeySequence ks;
if (settings.shortcuts.contains(macro->displayName()))
ks.fromString(settings.shortcuts.value(macro->displayName()).toString());
addMacro(macro, ks);
}
}
......@@ -192,8 +186,22 @@ void MacroManager::MacroManagerPrivate::removeDirectory(const QString &directory
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;
}
......@@ -201,8 +209,13 @@ void MacroManager::MacroManagerPrivate::removeMacro(const QString &name)
{
if (!macros.contains(name))
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);
removeShortcut(macro);
delete macro;
}
......@@ -211,52 +224,14 @@ void MacroManager::MacroManagerPrivate::changeMacroDescription(Macro *macro, con
macro->load();
macro->setDescription(description);
macro->save(macro->fileName());
}
int MacroManager::MacroManagerPrivate::addShortcut(Macro *macro, int id)
{
if (id==-1)
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
if (id < 10)
command->setDefaultKeySequence(QKeySequence(QString(tr("Alt+R,Alt+%1").arg(id))));
shortcuts[id] = shortcut;
}
// Update the current id (first id without shortcut)
while (shortcuts.contains(currentId))
currentId++;
// Change shortcut what's this
Core::ICore *core = Core::ICore::instance();
Core::ActionManager *am = core->actionManager();
// 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);
}
Core::Command *command = am->command(Core::Id(Constants::PREFIX_MACRO+macro->displayName()));
if (command && command->shortcut())
command->shortcut()->setWhatsThis(description);
}
bool MacroManager::MacroManagerPrivate::executeMacro(Macro *macro)
......@@ -325,9 +300,6 @@ void MacroManager::MacroManagerPrivate::showSaveDialog()
currentMacro->save(fileName);
addMacro(currentMacro);
if (dialog.createShortcut())
settings.shortcutIds[dialog.name()] = addShortcut(currentMacro);
if (changed)
q->saveSettings();
}
......@@ -458,7 +430,7 @@ void MacroManager::deleteMacro(const QString &name)
if (macro) {
QString fileName = macro->fileName();
d->removeMacro(name);
d->settings.shortcutIds.remove(name);
d->settings.shortcuts.remove(name);
QFile::remove(fileName);
}
}
......@@ -483,7 +455,7 @@ MacroManager *MacroManager::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))
return;
......@@ -492,16 +464,4 @@ void MacroManager::changeMacro(const QString &name, const QString &description,
// Change description
if (macro->description() != 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);
}
}
......@@ -77,7 +77,7 @@ protected:
friend class Internal::MacroOptionsWidget;
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 removeDirectory(const QString &directory);
void setDefaultDirectory(const QString &directory);
......
......@@ -87,8 +87,6 @@ MacroOptionsWidget::MacroOptionsWidget(QWidget *parent) :
this, SLOT(addDirectoy()));
connect(ui->description, SIGNAL(textChanged(QString)),
this, SLOT(changeDescription(QString)));
connect(ui->assignShortcut, SIGNAL(toggled(bool)),
this, SLOT(changeShortcut(bool)));
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::ActionManager *am = core->actionManager();
QMapIterator<QString, Macro *> it(MacroManager::instance()->macros());
while (it.hasNext()) {
it.next();
......@@ -135,14 +134,10 @@ void MacroOptionsWidget::appendDirectory(const QString &directory, bool isDefaul
macroItem->setText(1, it.value()->description());
macroItem->setData(0, NAME_ROLE, it.value()->displayName());
macroItem->setData(0, WRITE_ROLE, it.value()->isWritable());
macroItem->setData(0, ID_ROLE, it.value()->shortcutId());
if (it.value()->shortcutId() >= 0) {
QString textId = QString("%1").arg(it.value()->shortcutId(), 2, 10, QLatin1Char('0'));
QString commandId = QLatin1String(Constants::SHORTCUT_MACRO)+textId;
QString shortcut = am->command(commandId)->keySequence().toString();
macroItem->setText(2, QString("%1 (%2)").arg(shortcut).arg(commandId));
}
Core::Command *command = am->command(Core::Id(Constants::PREFIX_MACRO+it.value()->displayName()));
if (command && command->shortcut())
macroItem->setText(2, command->shortcut()->key().toString());
}
}
}
......@@ -160,7 +155,6 @@ void MacroOptionsWidget::changeCurrentItem(QTreeWidgetItem *current)
ui->removeButton->setEnabled(false);
ui->defaultButton->setEnabled(false);
ui->description->clear();
ui->assignShortcut->setChecked(false);
ui->macroGroup->setEnabled(false);
}
else if (current->type() == DIRECTORY) {
......@@ -168,14 +162,12 @@ void MacroOptionsWidget::changeCurrentItem(QTreeWidgetItem *current)
ui->removeButton->setEnabled(!isDefault);
ui->defaultButton->setEnabled(!isDefault);
ui->description->clear();
ui->assignShortcut->setChecked(false);
ui->macroGroup->setEnabled(false);
} else {
ui->removeButton->setEnabled(true);
ui->defaultButton->setEnabled(false);
ui->description->setText(current->text(1));
ui->description->setEnabled(current->data(0, WRITE_ROLE).toBool());
ui->assignShortcut->setChecked(current->data(0, ID_ROLE).toInt() >= 0);
ui->macroGroup->setEnabled(true);
}
changingCurrent = false;
......@@ -217,8 +209,7 @@ void MacroOptionsWidget::apply()
QMapIterator<QString, ChangeSet> it(m_macroToChange);
while (it.hasNext()) {
it.next();
MacroManager::instance()->changeMacro(it.key(), it.value().description,
it.value().shortcut);
MacroManager::instance()->changeMacro(it.key(), it.value().description);
}
// Get list of dir to append or remove
......@@ -263,21 +254,6 @@ void MacroOptionsWidget::changeData(QTreeWidgetItem *current, int column, QVaria
font.setItalic(true);
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)
......@@ -287,11 +263,3 @@ void MacroOptionsWidget::changeDescription(const QString &description)
return;
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);
}
......@@ -75,7 +75,6 @@ private:
private slots:
void changeDescription(const QString &description);
void changeShortcut(bool shortcut);
private:
Ui::MacroOptionsWidget *ui;
......
......@@ -82,7 +82,7 @@
</column>
<column>
<property name="text">
<string>Shortcut (Command)</string>
<string>Shortcut</string>
</property>
</column>
</widget>
......@@ -143,18 +143,6 @@
<item row="0" column="1">
<widget class="QLineEdit" name="description"/>
</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>
</widget>
</item>
......
......@@ -42,7 +42,7 @@ const char * const M_TOOLS_MACRO = "Macros.Tools.Menu";
const char * const START_MACRO = "Macros.StartMacro";
const char * const END_MACRO = "Macros.EndMacro";
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_TR_PAGE = "Macros";
......
......@@ -41,7 +41,7 @@ static const char GROUP[] = "Macro";
static const char DEFAULT_DIRECTORY[] = "DefaultDirectory";
static const char SHOW_SAVE_DIALOG[] = "ShowSaveDialog";
static const char DIRECTORIES[] = "Directories";
static const char SHORTCUTIDS[] = "ShortcutIds";
static const char SHORTCUTS[] = "Shortcuts";
MacroSettings::MacroSettings():
......@@ -55,7 +55,7 @@ void MacroSettings::toSettings(QSettings *s) const
s->setValue(QLatin1String(DEFAULT_DIRECTORY), defaultDirectory);
s->setValue(QLatin1String(SHOW_SAVE_DIALOG), showSaveDialog);
s->setValue(QLatin1String(DIRECTORIES), directories);
s->setValue(QLatin1String(SHORTCUTIDS), shortcutIds);
s->setValue(QLatin1String(SHORTCUTS), shortcuts);
s->endGroup();
}
......@@ -65,14 +65,14 @@ void MacroSettings::fromSettings(QSettings *s)
defaultDirectory = s->value(QLatin1String(DEFAULT_DIRECTORY), QString("")).toString();
showSaveDialog = s->value(QLatin1String(SHOW_SAVE_DIALOG), false).toBool();
directories = s->value(QLatin1String(DIRECTORIES)).toStringList();
shortcutIds = s->value(QLatin1String(SHORTCUTIDS)).toMap();
shortcuts = s->value(QLatin1String(SHORTCUTS)).toMap();
s->endGroup();
}
bool MacroSettings::equals(const MacroSettings &ms) const
{
return defaultDirectory == ms.defaultDirectory &&
shortcutIds == ms.shortcutIds &&
shortcuts == ms.shortcuts &&
directories == ms.directories &&
showSaveDialog == ms.showSaveDialog;
}
......@@ -58,7 +58,7 @@ public:
QString defaultDirectory;
QStringList directories;
QMap<QString, QVariant> shortcutIds;
QMap<QString, QVariant> shortcuts;
bool showSaveDialog;
};
......
......@@ -67,8 +67,3 @@ bool SaveDialog::hideSaveDialog() const
{
return ui->hideSaveDialog->isChecked();
}
bool SaveDialog::createShortcut() const
{
return ui->createShortcut->isChecked();
}
......@@ -54,7 +54,6 @@ public:
QString name() const;
QString description() const;
bool hideSaveDialog() const;
bool createShortcut() const;
private:
Ui::SaveDialog *ui;
......
......@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>219</width>
<height>153</height>
<height>116</height>
</rect>
</property>
<property name="windowTitle">
......@@ -51,14 +51,7 @@
<item row="4" column="0" colspan="2">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<widget class="QCheckBox" name="createShortcut">
<property name="text">
<string>Create a shortcut</string>
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Save</set>
</property>
</widget>
</item>
......@@ -67,8 +60,6 @@
<tabstops>
<tabstop>name</tabstop>
<tabstop>description</tabstop>
<tabstop>createShortcut</tabstop>
<tabstop>buttonBox</tabstop>
<tabstop>hideSaveDialog</tabstop>
</tabstops>
<resources/>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment