#include "pirmacro.h"
-#include "macros/pirmacrocommanditem.h"
+#include "pirmacrocommanditem.h"
+#include "pirmacropack.h"
+#include "mainwindow.h"
+
+#include <QSettings>
+
+#include <iostream>
bool PIRMacro::macroRunning = false;
PIRMacro::PIRMacro(
- QTreeWidgetItem *parent,
- QString n,
- char k)
+ PIRMacroPack *parent,
+ QString name,
+ char k,
+ unsigned int b,
+ MainWindow *mw)
: QTreeWidgetItem(parent),
- keyMapping(k)
+ key(k),
+ buttonID(b),
+ mainWindow(mw)
{
- setText(0, n);
+ setText(0, name);
+
+ if (key)
+ {
+ parent->registerKey(key, this);
+ }
+
+ if (buttonID)
+ {
+ parent->registerButton(buttonID, this);
+ }
+}
+
+
+PIRMacro::~PIRMacro()
+{
+ PIRMacroPack *mp = dynamic_cast<PIRMacroPack*> (QTreeWidgetItem::parent());
+
+ if (key)
+ {
+ mp->eraseKey(key, this);
+ }
+
+ if (buttonID)
+ {
+ mp->eraseButton(buttonID, this);
+ }
+
+ currentCommand = commands.begin();
+ while (currentCommand != commands.end())
+ {
+ delete *currentCommand;
+ commands.pop_front();
+ currentCommand = commands.begin();
+ }
}
}
-char PIRMacro::getKeyMapping()
+void PIRMacro::setName(
+ QString name)
{
- return keyMapping;
+ setText(0, name);
}
+char PIRMacro::getKey()
+{
+ return key;
+}
+
+
+void PIRMacro::setKey(
+ char k)
+{
+ key = k;
+}
+
+
+unsigned int PIRMacro::getButtonID()
+{
+ return buttonID;
+}
+
+
+void PIRMacro::setButtonID(
+ unsigned int id)
+{
+ buttonID = id;
+}
+
void PIRMacro::appendCommand(
PIRMacroCommandItem *c)
{
}
+bool PIRMacro::deleteCommand(
+ int index)
+{
+ CommandSequence::iterator i = commands.begin();
+ int count = 0;
+
+ while (i != commands.end())
+ {
+ if (index == count)
+ {
+ delete (*i);
+ commands.erase(i);
+ return true;
+ }
+ ++count;
+ ++i;
+ }
+
+ return false;
+}
+
+
+bool PIRMacro::moveUp(
+ int index)
+{
+ CommandSequence::iterator i = commands.begin();
+ int count = 0;
+ CommandSequence::iterator previ = commands.begin();
+
+ while (i != commands.end())
+ {
+ if (index == count)
+ {
+ PIRMacroCommandItem *mci = *i;
+ commands.erase(i);
+ commands.insert(previ, mci);
+ return true;
+ }
+
+ previ = i;
+ ++count;
+ ++i;
+ }
+
+ return false;
+}
+
+
+bool PIRMacro::moveDown(
+ int index)
+{
+ CommandSequence::iterator i = commands.begin();
+ CommandSequence::iterator nexti = commands.begin();
+ int count = 0;
+
+ while (i != commands.end())
+ {
+ ++nexti;
+ if ((index == count) && nexti != commands.end())
+ {
+ PIRMacroCommandItem *mci = *i;
+ commands.erase(i);
+ ++nexti;
+ commands.insert(nexti, mci);
+ return true;
+ }
+
+ ++count;
+ ++i;
+ }
+
+ return false;
+}
+
+
void PIRMacro::populateList(
QListWidget *lw)
{
while (i != commands.end())
{
- lw->addItem(*i);
+ lw->addItem(new QListWidgetItem((*i)->getName()));
++i;
}
}
// Start running the list of commands:
macroRunning = true;
+ // Take note of the current keyset id:
+ preMacroKeysetID = mainWindow->getCurrentKeyset();
+
connect(
*currentCommand,
SIGNAL(commandCompleted()),
(*currentCommand)->executeCommand();
- ++currentCommand;
-
return true;
}
void PIRMacro::startNextCommand()
{
+ disconnect(
+ *currentCommand,
+ SIGNAL(commandCompleted()),
+ 0,
+ 0);
+
+ ++currentCommand;
+
// Are we done?
if (currentCommand == commands.end())
{
// We are done.
macroRunning = false;
+ mainWindow->updateKeysetSelection(preMacroKeysetID);
emit macroCompleted();
return;
}
SLOT(startNextCommand()));
(*currentCommand)->executeCommand();
+}
- ++currentCommand;
+
+void PIRMacro::storeSettings(
+ QSettings &settings)
+{
+ settings.setValue("macroName", text(0));
+ settings.setValue("macroKey", key);
+ settings.setValue("macroButtonID", buttonID);
+
+ CommandSequence::const_iterator i = commands.begin();
+
+ int index = 0;
+ while (i != commands.end())
+ {
+ (*i)->storeSettings(settings, index);
+ ++index;
+ ++i;
+ }
+}
+
+
+QString PIRMacro::getCommandType(
+ int index)
+{
+ CommandSequence::iterator i = commands.begin();
+
+ // Advance 'index' steps into the sequence:
+ while (i != commands.end() && index)
+ {
+ ++i;
+ --index;
+ }
+
+ if (i != commands.end())
+ {
+ return (*i)->getTypeString();
+ }
+
+ return "";
+}
+
+
+QString PIRMacro::getCommandName(
+ int index)
+{
+ CommandSequence::iterator i = commands.begin();
+
+ while (i != commands.end() && index)
+ {
+ ++i;
+ --index;
+ }
+
+ if (i != commands.end())
+ {
+ return (*i)->getName();
+ }
+
+ return "";
}