Commit 06856649 authored by Victor Yacovlev's avatar Victor Yacovlev

Reorganized macros menu

parent c5f87139
<?xml version='1.0' encoding='UTF-8'?>
<macros>
<macro title="использовать Чертежник" key="2" last="true">
<command name="InsertImport" text="Чертежник" />
</macros>
<?xml version='1.0' encoding='UTF-8'?>
<macros>
<macro title="использовать Вертун" key="4" last="true">
<command name="InsertImport" text="Вертун" />
</macros>
<?xml version='1.0' encoding='UTF-8'?>
<macros>
<macro title="использовать Рисователь" key="2">
<macro title="использовать Рисователь" key="3" last="true">
<command name="InsertImport" text="Рисователь" />
......
<?xml version='1.0' encoding='UTF-8'?>
<macros>
<macro title="использовать Робот" key="1">
<macro title="использовать Робот" key="1" last="true">
<command name="InsertImport" text="Робот" />
</macro>
<macro title="вверх" key="Up">
......
<?xml version='1.0' encoding='UTF-8'?>
<macros>
<macro title="использовать Водолей" key="3">
<macro title="использовать Водолей" key="5" last="true">
<command name="InsertImport" text="Водолей" />
......
......@@ -52,10 +52,14 @@ void EditorInstance::lock()
if (toggleBreakpoint_)
toggleBreakpoint_->setEnabled(false);
for (int i=0; i<userMacros_.size(); i++) {
userMacros_[i].action->setEnabled(false);
QSharedPointer<Macro> macro = userMacros_[i];
QAction * action = macro->action;
action->setEnabled(false);
}
for (int i=0; i<systemMacros_.size(); i++) {
systemMacros_[i].action->setEnabled(false);
QSharedPointer<Macro> macro = systemMacros_[i];
QAction * action = macro->action;
action->setEnabled(false);
}
}
......@@ -71,10 +75,14 @@ void EditorInstance::unlock()
if (toggleBreakpoint_)
toggleBreakpoint_->setEnabled(true);
for (int i=0; i<userMacros_.size(); i++) {
userMacros_[i].action->setEnabled(true);
QSharedPointer<Macro> macro = userMacros_[i];
QAction * action = macro->action;
action->setEnabled(true);
}
for (int i=0; i<systemMacros_.size(); i++) {
systemMacros_[i].action->setEnabled(true);
QSharedPointer<Macro> macro = systemMacros_[i];
QAction * action = macro->action;
action->setEnabled(true);
}
}
......@@ -204,21 +212,21 @@ void EditorInstance::setLineHighlighted(int lineNo, const QColor &color, quint32
void EditorInstance::disableInsertActions()
{
foreach (Macro m , userMacros_) {
m.action->setEnabled(false);
foreach (QSharedPointer<Macro> m , userMacros_) {
m->action->setEnabled(false);
}
foreach (Macro m , systemMacros_) {
m.action->setEnabled(false);
foreach (QSharedPointer<Macro> m , systemMacros_) {
m->action->setEnabled(false);
}
}
void EditorInstance::enableInsertActions()
{
foreach (Macro m , userMacros_) {
m.action->setEnabled(true);
foreach (QSharedPointer<Macro> m , userMacros_) {
m->action->setEnabled(true);
}
foreach (Macro m , systemMacros_) {
m.action->setEnabled(true);
foreach (QSharedPointer<Macro> m , systemMacros_) {
m->action->setEnabled(true);
}
}
......@@ -296,12 +304,13 @@ void EditorInstance::loadMacros()
foreach (const KPlugin* plugin, actorPlugins) {
ActorInterface * actor = qobject_cast<ActorInterface*>(plugin);
const QString canonicalName = actorCanonicalName<QString>(actor->asciiModuleName());
QString canonicalName = actorCanonicalName<QString>(actor->asciiModuleName());
canonicalName.remove(" ");
const QString actorMacrosFileName = plugin_->myResourcesDir().absoluteFilePath(
"macros-" + analizerName + "-" + canonicalName + ".xml"
);
if (QFile::exists(actorMacrosFileName)) {
systemMacros_.push_back(Macro());
systemMacros_.push_back(QSharedPointer<Macro>(new Macro()));
systemMacros_ += loadFromFile(actorMacrosFileName);
}
}
......@@ -356,66 +365,72 @@ ExtensionSystem::SettingsPtr EditorInstance::mySettings() const
return plugin_->mySettings();
}
static bool operator<(QSharedPointer<Macro> a, QSharedPointer<Macro> b) {
return a->key.unicode() < b->key.unicode();
}
void EditorInstance::updateInsertMenu()
{
loadMacros();
insertMenu_->clear();
const QString escComa = "Esc, ";
for (int i=0; i<systemMacros_.size(); i++) {
Macro m = systemMacros_[i];
if (m.title.isEmpty()) {
QVector< QList<QSharedPointer<Macro> > > groups(2);
for (int i=0; i<systemMacros_.size(); ++i) {
QSharedPointer<Macro> m = systemMacros_[i];
int groupNo = m->showInLastBlock ? 1 : 0;
groups[groupNo].append(m);
}
qSort(groups[1]);
for (int z=0; z<2; ++z) {
QList<QSharedPointer<Macro> > group = groups[z];
if (1==z && groups[1].size() > 0) {
insertMenu_->addSeparator();
}
for (int i=0; i<group.size(); i++) {
QSharedPointer<Macro> m = group[i];
if (m->title.isEmpty()) {
// Separator
systemMacros_[i].action = insertMenu_->addSeparator();
m->action = insertMenu_->addSeparator();
}
else {
m.action = new QAction(m.title, insertMenu_);
systemMacros_[i].action = m.action;
insertMenu_->addAction(m.action);
connect(m.action, SIGNAL(triggered()), this, SLOT(playMacro()));
if (!m.key.isNull()) {
// const QKeySequence ks(escComa+QString(Utils::latinKey(m.key)));
const QKeySequence ks2(escComa + QString(m.key));
m.action->setProperty("fakeShortcut", ks2.toString());
// if (ks == ks2) {
// m.action->setShortcut(ks);
// }
// else {
// const QList<QKeySequence> shortcuts = QList<QKeySequence>() << ks << ks2;
// m.action->setShortcuts(shortcuts);
// }
m->action = new QAction(m->title, insertMenu_);
insertMenu_->addAction(m->action);
connect(m->action, SIGNAL(triggered()), this, SLOT(playMacro()));
if (!m->key.isNull()) {
const QKeySequence ks2(escComa + QString(m->key));
m->action->setProperty("fakeShortcut", ks2.toString());
}
else if (uint(m.extKey) != 0u) {
else if (uint(m->extKey) != 0u) {
QString repr;
if (m.extKey == Qt::Key_Left)
if (m->extKey == Qt::Key_Left)
repr = "Left";
else if (m.extKey == Qt::Key_Right)
else if (m->extKey == Qt::Key_Right)
repr = "Right";
else if (m.extKey == Qt::Key_Up)
else if (m->extKey == Qt::Key_Up)
repr = "Up";
else if (m.extKey == Qt::Key_Down)
else if (m->extKey == Qt::Key_Down)
repr = "Down";
else if (m.extKey == Qt::Key_Space)
else if (m->extKey == Qt::Key_Space)
repr = "Space";
const QKeySequence ks(escComa + repr);
m.action->setShortcut(ks);
m->action->setShortcut(ks);
}
}
}
}
if (!userMacros_.isEmpty())
insertMenu_->addSeparator();
for (int i=0; i<userMacros_.size(); i++) {
Macro m = userMacros_[i];
m.action = new QAction(m.title, insertMenu_);
if (!m.key.isNull()) {
const QKeySequence ks(escComa+QString(Utils::latinKey(m.key)));
const QKeySequence ks2(escComa + QString(m.key));
QSharedPointer<Macro> m = userMacros_[i];
m->action = new QAction(m->title, insertMenu_);
if (!m->key.isNull()) {
const QKeySequence ks(escComa+QString(Utils::latinKey(m->key)));
const QKeySequence ks2(escComa + QString(m->key));
const QList<QKeySequence> shortcuts = QList<QKeySequence>() << ks << ks2;
m.action->setShortcuts(shortcuts);
m->action->setShortcuts(shortcuts);
}
userMacros_[i].action = m.action;
insertMenu_->addAction(m.action);
connect(m.action, SIGNAL(triggered()), this, SLOT(playMacro()));
insertMenu_->addAction(m->action);
connect(m->action, SIGNAL(triggered()), this, SLOT(playMacro()));
}
editMacros_->setEnabled(userMacros_.size() > 0);
Widgets::CyrillicMenu * insMenu = qobject_cast<Widgets::CyrillicMenu*>(insertMenu_);
......@@ -429,15 +444,15 @@ bool EditorInstance::tryEscKeyAction(const QString &text)
if (text.length()!=1 && text.at(0).toLatin1()) {
return false; // workarund required only for non-latin keys
}
const QList<Macro> allMacros = systemMacros_ + userMacros_;
const QList<QSharedPointer<Macro> > allMacros = systemMacros_ + userMacros_;
const QChar ch = text.at(0).toUpper();
const QChar altCh = Utils::cyrillicKey(ch).toUpper();
foreach (const Macro & m, allMacros) {
bool keyMatch = m.key.toUpper() == ch;
bool altKeyMatch = m.key.toUpper() == altCh;
bool enabled = m.action && m.action->isEnabled();
foreach (QSharedPointer<Macro> m, allMacros) {
bool keyMatch = m->key.toUpper() == ch;
bool altKeyMatch = m->key.toUpper() == altCh;
bool enabled = m->action && m->action->isEnabled();
if ( (keyMatch || altKeyMatch) && enabled) {
m.action->trigger();
m->action->trigger();
return true;
}
}
......@@ -470,18 +485,18 @@ void EditorInstance::playMacro()
{
QAction * a = qobject_cast<QAction*>(sender());
Q_CHECK_PTR(a);
Macro m;
QSharedPointer<Macro> m;
bool found = false;
foreach (Macro mm, systemMacros_) {
if (mm.action==a) {
foreach (QSharedPointer<Macro> mm, systemMacros_) {
if (mm->action==a) {
found = true;
m = mm;
break;
}
}
if (!found) {
foreach (Macro mm, userMacros_) {
if (mm.action==a) {
foreach (QSharedPointer<Macro> mm, userMacros_) {
if (mm->action==a) {
found = true;
m = mm;
break;
......@@ -489,9 +504,9 @@ void EditorInstance::playMacro()
}
}
if (found) {
document()->undoStack()->beginMacro(m.title);
for (int i=0; i<m.commands.size(); i++) {
cursor_->evaluateCommand(m.commands[i]);
document()->undoStack()->beginMacro(m->title);
for (int i=0; i<m->commands.size(); i++) {
cursor_->evaluateCommand(m->commands[i]);
}
document()->undoStack()->endMacro();
plane_->updateScrollBars();
......@@ -1061,29 +1076,27 @@ void EditorInstance::toggleRecordMacro(bool on)
cursor_->startRecordMacro();
}
else {
Macro * macro = cursor_->endRecordMacro();
QSharedPointer<Macro> macro = cursor_->endRecordMacro();
if (macro->commands.size() > 0) {
MacroEditor * editor = new MacroEditor(this);
editor->setWindowTitle(tr("New keyboard sequence..."));
QList<Macro> allMacros = systemMacros_ + userMacros_;
QList<QSharedPointer<Macro> > allMacros = systemMacros_ + userMacros_;
QString usedLetters;
QStringList usedNames;
foreach(Macro m, allMacros) {
if (!m.key.isNull()) {
usedLetters.push_back(m.key);
usedNames.push_back(m.title);
foreach(QSharedPointer<Macro> m, allMacros) {
if (!m->key.isNull()) {
usedLetters.push_back(m->key);
usedNames.push_back(m->title);
}
}
editor->setUsedSymbols(usedLetters, usedNames);
editor->setMacro(macro);
if (editor->exec() == QDialog::Accepted) {
Macro newMacro = *macro;
userMacros_.push_back(newMacro);
userMacros_.push_back(macro);
plugin_->updateUserMacros(analizerPlugin_ ? analizerPlugin_->defaultDocumentFileNameSuffix() : QString(), userMacros_, true);
}
editor->deleteLater();
}
delete macro;
}
}
emit recordMacroChanged(on);
}
......
......@@ -195,8 +195,8 @@ private /* fields */:
QAction * separatorAction_;
mutable QAction * toggleBreakpoint_;
QList<Macro> systemMacros_;
QList<Macro> userMacros_;
QList<QSharedPointer<Macro>> systemMacros_;
QList<QSharedPointer<Macro>> userMacros_;
int timerId_;
int autoScrollTimerId_;
......
......@@ -244,7 +244,7 @@ void EditorPlugin::connectGlobalSignalsToEditor(EditorInstance *editor)
}
void EditorPlugin::updateUserMacros(const QString & analizerName, const QList<Macro> &macros, bool rewrite)
void EditorPlugin::updateUserMacros(const QString & analizerName, const QList<QSharedPointer<Macro> > &macros, bool rewrite)
{
if (rewrite) {
QString fileName = analizerName.length() > 0
......
......@@ -80,11 +80,11 @@ protected:
inline void changeCurrentDirectory(const QString & path) {
currentDirectory_ = path;
updateSettings(QStringList());
updateUserMacros(QString(), QList<Macro>(), false);
updateUserMacros(QString(), QList<QSharedPointer<Macro> >(), false);
}
void start();
void stop();
void updateUserMacros(const QString & analizerName, const QList<Macro> & macros, bool rewrite);
void updateUserMacros(const QString & analizerName, const QList<QSharedPointer<Macro> > & macros, bool rewrite);
private:
struct Ed {
inline Ed() { e = 0; a=0; id =-1; }
......
......@@ -14,14 +14,14 @@ QString screenString(QString s)
return s;
}
extern QDomElement dumpMacro(const Macro &m, QDomDocument &document, QDomElement &root)
extern QDomElement dumpMacro(QSharedPointer<Macro> m, QDomDocument &document, QDomElement &root)
{
QDomElement macroElement = document.createElement("macro");
macroElement.setAttribute("title", m.title);
if (!m.key.isNull())
macroElement.setAttribute("key", m.key);
for (int i=0; i<m.commands.size(); i++) {
const KeyCommand & cmd = m.commands[i];
macroElement.setAttribute("title", m->title);
if (!m->key.isNull())
macroElement.setAttribute("key", m->key);
for (int i=0; i<m->commands.size(); i++) {
const KeyCommand & cmd = m->commands[i];
QDomElement commandElement = document.createElement("command");
commandElement.setAttribute("name", dumpKeyCommandType(cmd.type));
if (cmd.text.length() > 0)
......@@ -32,26 +32,28 @@ extern QDomElement dumpMacro(const Macro &m, QDomDocument &document, QDomElement
return macroElement;
}
extern bool loadMacro(const QDomElement &v, Macro &m)
extern bool loadMacro(const QDomElement &v, QSharedPointer<Macro> m)
{
m.action = 0;
m.commands.clear();
m.title = v.attribute("title");
m->action = 0;
m->commands.clear();
m->title = v.attribute("title");
const QString key = v.attribute("key").trimmed();
const QString last = v.attribute("last", "false").toLower().trimmed();
m->showInLastBlock = last=="true" || last=="1";
if (key.length()==1)
m.key = key[0];
m->key = key[0];
else
m.key = QChar(QChar::Null);
m->key = QChar(QChar::Null);
if ( key.toLower() == "up" )
m.extKey = Qt::Key_Up;
m->extKey = Qt::Key_Up;
else if ( key.toLower() == "down" )
m.extKey = Qt::Key_Down;
m->extKey = Qt::Key_Down;
else if ( key.toLower() == "left" )
m.extKey = Qt::Key_Left;
m->extKey = Qt::Key_Left;
else if ( key.toLower() == "right" )
m.extKey = Qt::Key_Right;
m->extKey = Qt::Key_Right;
else if ( key.toLower() == "space" )
m.extKey = Qt::Key_Space;
m->extKey = Qt::Key_Space;
const QDomNodeList commands = v.elementsByTagName("command");
for (int i=0; i<commands.count(); i++) {
......@@ -64,21 +66,21 @@ extern bool loadMacro(const QDomElement &v, Macro &m)
loadKeyCommandType(cmd.attribute("name")),
text
);
m.commands.push_back(command);
m->commands.push_back(command);
}
return m.commands.size() > 0;
return m->commands.size() > 0;
}
extern QList<Macro> loadFromFile(const QString &fileName)
extern QList<QSharedPointer<Macro> > loadFromFile(const QString &fileName)
{
QList<Macro> result;
QList<QSharedPointer<Macro> > result;
QFile f(fileName);
if (f.open(QIODevice::ReadOnly|QIODevice::Text)) {
QDomDocument doc;
doc.setContent(&f);
const QDomNodeList macros = doc.elementsByTagName("macro");
for (int i=0; i<macros.count(); i++) {
Macro macro;
QSharedPointer<Macro> macro(new Macro());
if (loadMacro(macros.at(i).toElement(), macro)) {
result << macro;
}
......@@ -88,13 +90,13 @@ extern QList<Macro> loadFromFile(const QString &fileName)
return result;
}
extern bool saveToFile(const QString &fileName, const QList<Macro> &macros)
extern bool saveToFile(const QString &fileName, const QList<QSharedPointer<Macro> > &macros)
{
QDomDocument document("macros");
QDomElement root = document.createElement("macros");
document.appendChild(root);
for (int i=0; i<macros.size(); i++) {
const Macro & macro = macros[i];
QSharedPointer<Macro> macro = macros[i];
dumpMacro(macro, document, root);
}
QFile f(fileName);
......
......@@ -19,13 +19,14 @@ struct Macro {
Qt::Key extKey;
QList<KeyCommand> commands;
QAction * action;
inline explicit Macro() { action = nullptr; extKey = Qt::Key(0u); }
bool showInLastBlock;
inline explicit Macro() { action = nullptr; extKey = Qt::Key(0u); showInLastBlock = false; }
};
extern QDomElement dumpMacro(const Macro & m, QDomDocument &document, QDomElement &root);
extern bool loadMacro(const QDomElement & v, Macro & m);
extern QList<Macro> loadFromFile(const QString &fileName);
extern bool saveToFile(const QString &fileName, const QList<Macro> & macros);
extern QDomElement dumpMacro(QSharedPointer<Macro> m, QDomDocument &document, QDomElement &root);
extern bool loadMacro(const QDomElement & v, QSharedPointer<Macro> m);
extern QList<QSharedPointer<Macro>> loadFromFile(const QString &fileName);
extern bool saveToFile(const QString &fileName, const QList<QSharedPointer<Macro>> & macros);
}
......
......@@ -52,7 +52,7 @@ void MacroEditor::setUsedSymbols(const QString &symbols, const QStringList & nam
}
}
void MacroEditor::setMacro(Macro *macro)
void MacroEditor::setMacro(QSharedPointer<Macro> macro)
{
static const Qt::ItemFlags Enabled = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
macro_ = macro;
......
......@@ -3,6 +3,8 @@
#include <QDialog>
#include <QTableWidgetItem>
#include <QSharedPointer>
#include "macro.h"
namespace Editor {
......@@ -10,6 +12,7 @@ namespace Ui {
class MacroEditor;
}
class MacroEditor : public QDialog
{
Q_OBJECT
......@@ -18,7 +21,7 @@ public:
explicit MacroEditor(QWidget *parent = 0);
~MacroEditor();
void setUsedSymbols(const QString &symbols, const QStringList & names);
void setMacro(class Macro * macro);
void setMacro(QSharedPointer<Macro> macro);
private slots:
void checkMacroTitle(const QString & title);
......@@ -26,7 +29,7 @@ private slots:
private:
Ui::MacroEditor *ui;
class Macro * macro_;
QSharedPointer<Macro> macro_;
};
......
......@@ -28,8 +28,8 @@ void MacroListEditor::handleItemChanged(QListWidgetItem *current, QListWidgetIte
ui->btnEdit->setEnabled(current != nullptr);
}
void MacroListEditor::initialize(const QList<Macro> &macros,
const QList<Macro> &systemMacros)
void MacroListEditor::initialize(const QList<QSharedPointer<Macro> > &macros,
const QList<QSharedPointer<Macro> > &systemMacros)
{
ui->btnRemove->setEnabled(false);
ui->btnEdit->setEnabled(false);
......@@ -37,11 +37,11 @@ void MacroListEditor::initialize(const QList<Macro> &macros,
macros_ = macros;
systemMacros_ = systemMacros;
for (int i=0; i<macros_.size(); i++) {
Macro & macro = macros_[i];
QSharedPointer<Macro> macro = macros_[i];
QListWidgetItem * item = new QListWidgetItem;
QString text = macro.title.trimmed();
if (!macro.key.isNull()) {
text += QString(" (Esc, ") + macro.key + ")";
QString text = macro->title.trimmed();
if (!macro->key.isNull()) {
text += QString(" (Esc, ") + macro->key + ")";
}
item->setText(text);
ui->listWidget->addItem(item);
......@@ -69,26 +69,26 @@ void MacroListEditor::editMacro()
QListWidgetItem * item = ui->listWidget->currentItem();
int index = ui->listWidget->currentRow();
Macro macro = macros_[index];
QSharedPointer<Macro> macro = macros_[index];
MacroEditor * editor = new MacroEditor(this);
editor->setWindowTitle(tr("Edit recorded keyboard sequence..."));
QList<Macro> allMacros = systemMacros_ + macros_;
QList<QSharedPointer<Macro> > allMacros = systemMacros_ + macros_;
QString usedLetters;
QStringList usedNames;
foreach(Macro m, allMacros) {
if (!m.key.isNull()) {
usedLetters.push_back(m.key);
usedNames.push_back(m.title);
foreach(QSharedPointer<Macro> m, allMacros) {
if (!m->key.isNull()) {
usedLetters.push_back(m->key);
usedNames.push_back(m->title);
}
}
editor->setUsedSymbols(usedLetters, usedNames);
editor->setMacro(&macro);
editor->setMacro(macro);