Subversion Repositories Code-Repo

Compare Revisions

Ignore whitespace Rev 349 → Rev 350

/Misc Projects/PcMarlinInterface/MacroController.cpp
9,6 → 9,7
btnImport = new QPushButton("&Import");
btnAddMacro = new QPushButton("&Add");
btnRemoveMacro = new QPushButton("&Remove");
btnClear = new QPushButton("&Clear");
currKeyBindInfo = QPair<QPushButton*,int>(NULL, 0);
 
mainLayout = new QGridLayout();
20,6 → 21,7
ioLayout = new QHBoxLayout();
ioLayout->addWidget(btnAddMacro);
ioLayout->addWidget(btnRemoveMacro);
ioLayout->addWidget(btnClear);
ioLayout->addWidget(btnExport);
ioLayout->addWidget(btnImport);
ioLayout->addStretch();
33,10 → 35,14
 
connect(btnAddMacro, SIGNAL(clicked()), this, SLOT(Macro_AddEntry()));
connect(btnRemoveMacro, SIGNAL(clicked()), this, SLOT(Macro_RemoveEntry()));
connect(btnClear, SIGNAL(clicked()), this, SLOT(Macro_Clear()));
connect(btnExport, SIGNAL(clicked()), this, SLOT(Macro_WriteToFile()));
connect(btnImport, SIGNAL(clicked()), this, SLOT(Macro_ReadFromFile()));
connect(sigmapTransmit, SIGNAL(mapped(QWidget*)), this, SLOT(Macro_InitTransmit(QWidget*)));
connect(sigmapKeybind, SIGNAL(mapped(int)), this, SLOT(Macro_KeybindPrompt(int)));
 
// Register global event process for keyboard shortcut handling
qApp->installEventFilter(this);
}
 
MacroController::~MacroController()
67,18 → 73,36
 
void MacroController::Macro_InitTransmit(QWidget *t)
{
QTextEdit *text = qobject_cast<QTextEdit*>(t);
emit Macro_TransmitText(text->toPlainText());
if (connected) {
QTextEdit *text = qobject_cast<QTextEdit*>(t);
emit Macro_TransmitText(text->toPlainText());
}
}
 
void MacroController::Macro_KeybindPrompt(int id)
{
QPushButton *btn = qobject_cast<QPushButton*>(sigmapKeybind->mapping(id));
 
// Check to make sure we arn't processing another key first
if (currKeyBindInfo.first != NULL) {
currKeyBindInfo.first->setText("Hotkey: None");
currKeyBindInfo.first->setDown(false);
currKeyBindInfo.first->removeEventFilter(this);
}
 
// Mark and save button as waiting for key sequence
btn->setDown(true);
btn->setText("Waiting for Key..");
currKeyBindInfo = QPair<QPushButton*,int>(btn, id);
 
// Steal all following keyboard events
// Remove current keybinding for this macro
int index = registeredKeyMacroIDs.indexOf(id);
if (index >= 0) {
registeredKeySequences.removeAt(index);
registeredKeyMacroIDs.removeAt(index);
}
 
// Inspect all following keyboard events
btn->installEventFilter(this);
}
 
88,8 → 112,9
 
// Create new layout/widgets
QLineEdit *lineEdit = new QLineEdit(QString("Macro %1").arg(macroCount));
lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
lineEdit->setAlignment(Qt::AlignCenter | Qt::AlignTop);
lineEdit->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
lineEdit->setMinimumWidth(100);
lineEdit->setAlignment(Qt::AlignCenter);
macroNameList.append(lineEdit);
 
QTextEdit *textEdit = new QTextEdit();
101,10 → 126,9
textEdit->setTabChangesFocus(true);
macroValueList.append(textEdit);
 
QPushButton *keyButton = new QPushButton("Key: None");
QPushButton *keyButton = new QPushButton("Hotkey: None");
keyButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
macroBtnKeyList.append(keyButton);
macroKeybindList.append(QKeySequence(Qt::Key_unknown));
 
QPushButton *pushButton = new QPushButton("Send Macro");
pushButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
165,8 → 189,13
macroValueList.pop_back();
macroBtnSendList.pop_back();
macroBtnKeyList.pop_back();
macroKeybindList.pop_back();
 
int index = registeredKeyMacroIDs.indexOf(macroCount-1);
if (index >= 0) {
registeredKeySequences.removeAt(index);
registeredKeyMacroIDs.removeAt(index);
}
 
macroCount--;
 
QDockWidget *parent = qobject_cast<QDockWidget*>(this->parent());
188,6 → 217,9
QFileInfo fileInfo = QFileInfo(file);
lastKnownFilePath = fileInfo.absolutePath();
 
if (file.size() != 0)
QFile::remove(file);
 
QFile inputFile(file);
if (!inputFile.open(QIODevice::ReadWrite | QIODevice::Text)) return;
 
204,6 → 236,13
stream.writeAttribute("Name", macroNameList[i]->text());
stream.writeTextElement("Value", macroValueList[i]->toPlainText());
 
int index = registeredKeyMacroIDs.indexOf(i);
if (index >= 0) {
stream.writeTextElement("Keybinding", registeredKeySequences[index].toString());
} else {
stream.writeTextElement("Keybinding", "");
}
 
stream.writeEndElement(); // Macro Entry
}
 
223,6 → 262,8
 
if (file.size() == 0) return;
 
Macro_Clear();
 
// If file was selected, save directory for next time
QFileInfo fileInfo = QFileInfo(file);
lastKnownFilePath = fileInfo.absolutePath();
248,7 → 289,7
 
// Parse element <Macro_Entry>
if (stream.name() == "Macro_Entry") {
QString name, value;
QString name, value, keybinding;
 
// Read and save attribute value
QXmlStreamAttributes attr = stream.attributes();
266,6 → 307,10
stream.readNext();
value = stream.text().toString();
}
if (stream.name() == "Keybinding") {
stream.readNext();
keybinding = stream.text().toString();
}
}
stream.readNext();
}
276,6 → 321,11
 
macroNameList[counter]->setText(name);
macroValueList[counter]->setText(value);
if (keybinding != "") {
registeredKeySequences.append(QKeySequence(keybinding));
registeredKeyMacroIDs.append(counter);
macroBtnKeyList[counter]->setText("Hotkey: " + keybinding);
}
counter++;
}
}
282,21 → 332,52
}
}
 
void MacroController::Macro_Clear()
{
for (int i = 0; i < macroCount; i++) {
macroNameList[i]->setText(QString("Macro %1").arg(i+1));
macroValueList[i]->clear();
macroBtnKeyList[i]->setText("Hotkey: None");
}
registeredKeyMacroIDs.clear();
registeredKeySequences.clear();
}
 
bool MacroController::eventFilter(QObject *obj, QEvent *event)
{
// Only process keyboard events
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyevent = static_cast<QKeyEvent*>(event);
QKeySequence seq = keyevent->modifiers() + keyevent->key();
if ((keyevent->key() >= 0x21 && keyevent->key() <= 0x2F) ||
(keyevent->key() >= 0x3A && keyevent->key() <= 0x40) ||
(keyevent->key() >= 0x5E && keyevent->key() <= 0x7E) ) {
seq = keyevent->key();
}
 
if (connected) {
// First check if key sequence matches any saved ones
if (!registeredKeySequences.isEmpty()) {
int index = registeredKeySequences.indexOf(seq);
if (index >= 0) {
Macro_InitTransmit(macroValueList[registeredKeyMacroIDs[index]]);
return true;
}
}
}
 
// Then save key sequence if needed
if (currKeyBindInfo.first != NULL) {
// Ignore modifier keys
// Ignore modifier keys and locks
if (keyevent->key() == Qt::Key_Shift || keyevent->key() == Qt::Key_Control ||
keyevent->key() == Qt::Key_Meta || keyevent->key() == Qt::Key_Alt)
keyevent->key() == Qt::Key_Meta || keyevent->key() == Qt::Key_Alt ||
keyevent->key() == Qt::Key_AltGr || keyevent->key() == Qt::Key_CapsLock ||
keyevent->key() == Qt::Key_NumLock || keyevent->key() == Qt::Key_ScrollLock)
return true;
 
// Reset on ESC key
if (keyevent->key() == Qt::Key_Escape) {
macroKeybindList[currKeyBindInfo.second] = Qt::Key_unknown;
currKeyBindInfo.first->setText("Key: None");
currKeyBindInfo.first->setText("Hotkey: None");
currKeyBindInfo.first->setDown(false);
currKeyBindInfo.first->removeEventFilter(this);
currKeyBindInfo = QPair<QPushButton*, int>(NULL, 0);
303,15 → 384,17
return true;
}
 
// Otherwise save key sequence
QKeySequence seq = keyevent->modifiers() + keyevent->key();
macroKeybindList[currKeyBindInfo.second] = seq;
currKeyBindInfo.first->setText(seq.toString());
currKeyBindInfo.first->setDown(false);
currKeyBindInfo.first->removeEventFilter(this);
currKeyBindInfo = QPair<QPushButton*, int>(NULL, 0);
// Otherwise save key sequence if it doesnt already exist
if (!registeredKeySequences.contains(seq)) {
registeredKeySequences.append(seq);
registeredKeyMacroIDs.append(currKeyBindInfo.second);
currKeyBindInfo.first->setText("Hotkey: " + seq.toString());
currKeyBindInfo.first->setDown(false);
currKeyBindInfo.first->removeEventFilter(this);
currKeyBindInfo = QPair<QPushButton*, int>(NULL, 0);
return true;
}
}
return true;
}
 
return QWidget::eventFilter(obj, event);
/Misc Projects/PcMarlinInterface/MacroController.h
24,6 → 24,7
void Macro_RemoveEntry(void);
void Macro_WriteToFile(void);
void Macro_ReadFromFile(void);
void Macro_Clear(void);
 
signals:
void Macro_TransmitText(QString string);
40,13 → 41,16
QPushButton *btnImport;
QPushButton *btnAddMacro;
QPushButton *btnRemoveMacro;
QPushButton *btnClear;
 
QList<QLineEdit*> macroNameList;
QList<QTextEdit*> macroValueList;
QList<QPushButton*> macroBtnSendList;
QList<QPushButton*> macroBtnKeyList;
QList<QKeySequence> macroKeybindList;
 
QList<QKeySequence> registeredKeySequences;
QList<int> registeredKeyMacroIDs;
 
QGridLayout *mainLayout;
QHBoxLayout *ioLayout;
QSignalMapper *sigmapTransmit;