From fc0e7683878ac10eb9a4675f61e6443a13ced946 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 21 Dec 2012 10:41:15 +0100 Subject: Implemented new shortcut key system. Fade in/out shortcut tooltips. Close pcpviewer on window close due to commit/nocommit/discard actions. --- client/widgets/altcombobox.cc | 4 +- client/widgets/button.cc | 20 ++++++++- client/widgets/button.h | 1 + client/widgets/checkbox.cc | 10 ++++- client/widgets/checkbox.h | 2 + client/widgets/checkgroupbox.cc | 18 ++++---- client/widgets/combobox.cc | 8 ++-- client/widgets/datetime.cc | 2 +- client/widgets/frame.cc | 2 +- client/widgets/groupbox.cc | 2 +- client/widgets/label.cc | 2 +- client/widgets/lineedit.cc | 7 ++-- client/widgets/listbox.cc | 2 +- client/widgets/metawidget.cc | 2 +- client/widgets/multilist.cc | 5 ++- client/widgets/radiobutton.cc | 5 +++ client/widgets/radiobutton.h | 1 + client/widgets/radiobuttons.cc | 2 +- client/widgets/textedit.cc | 7 ++-- client/widgets/widget.cc | 91 ++++++++++++++++++++++++++++++++++++++++- client/widgets/widget.h | 16 +++++++- client/widgets/window.cc | 16 ++++---- 22 files changed, 183 insertions(+), 42 deletions(-) (limited to 'client/widgets') diff --git a/client/widgets/altcombobox.cc b/client/widgets/altcombobox.cc index 082601a..37df5bf 100644 --- a/client/widgets/altcombobox.cc +++ b/client/widgets/altcombobox.cc @@ -42,9 +42,9 @@ AltComboBox::AltComboBox(QDomNode &node, MacroWindow *macrowindow) : ComboBox(node, macrowindow) { - combobox = (QComboBox*)widget; + combobox = (QComboBox*)getWidget(); frame = new QFrame(); - widget = frame; + setWidget(frame); hideChildren = true; diff --git a/client/widgets/button.cc b/client/widgets/button.cc index 6ef6ac8..06bf594 100644 --- a/client/widgets/button.cc +++ b/client/widgets/button.cc @@ -30,6 +30,7 @@ #include #include "common.h" +#include "debug.h" #include "macrowindow.h" @@ -37,7 +38,7 @@ Button::Button(QDomNode &node, MacroWindow *macrowindow) : Widget(node, macrowindow) { button = new QPushButton(); - widget = button; + setWidget(button); setCommonAttributes(button, node); @@ -61,6 +62,17 @@ Button::Button(QDomNode &node, MacroWindow *macrowindow) connect(this, SIGNAL(act_cancel()), macrowindow, SLOT(cancel())); connect(macrowindow, SIGNAL(macroHasChanged()), this, SLOT(do_enable())); } + + if(elem.attribute("action") == "commit") { + QList lst; + lst << QKeySequence("Ctrl+Enter"); + lst << QKeySequence("Ctrl+Return"); + QAction *a = new QAction(widget_name, this); + a->setShortcuts(lst); + button->addAction(a); + connect(a, SIGNAL(triggered()), + this, SLOT(shortcutActivated())); + } } Button::~Button() @@ -82,3 +94,9 @@ void Button::do_enable() { button->setEnabled(true); } + +void Button::shortcutActivated() +{ + setKeyboardFocus(); + button->click(); +} diff --git a/client/widgets/button.h b/client/widgets/button.h index d9053a6..959ecc3 100644 --- a/client/widgets/button.h +++ b/client/widgets/button.h @@ -63,6 +63,7 @@ public slots: void commit(); void cancel(); void do_enable(); + void shortcutActivated(); signals: void act_commit(); diff --git a/client/widgets/checkbox.cc b/client/widgets/checkbox.cc index 7d81b1a..45f59c6 100644 --- a/client/widgets/checkbox.cc +++ b/client/widgets/checkbox.cc @@ -28,6 +28,8 @@ #include +#include "debug.h" + #include "common.h" #include "luawidget.h" @@ -35,7 +37,7 @@ CheckBox::CheckBox(QDomNode &node, MacroWindow *macrowindow) : Widget(node, macrowindow) { checkbox = new QCheckBox(); - widget = checkbox; + setWidget(checkbox); changedByUser = true; @@ -128,6 +130,12 @@ void CheckBox::setWdgValid(bool valid) checkbox->setPalette(palette); } +void CheckBox::shortcutActivated() +{ + setKeyboardFocus(); + checkbox->click();//setChecked(!checked()); +} + int chk_checked(lua_State *L) { wdg_userdata *wdgu; diff --git a/client/widgets/checkbox.h b/client/widgets/checkbox.h index 5fef7cb..b888bde 100644 --- a/client/widgets/checkbox.h +++ b/client/widgets/checkbox.h @@ -71,6 +71,8 @@ public: public slots: void state_change(int); + void shortcutActivated(); + private: QString truevalue; diff --git a/client/widgets/checkgroupbox.cc b/client/widgets/checkgroupbox.cc index fa662e1..c118e36 100644 --- a/client/widgets/checkgroupbox.cc +++ b/client/widgets/checkgroupbox.cc @@ -37,7 +37,7 @@ CheckGroupBox::CheckGroupBox(QDomNode &node, MacroWindow *macrowindow) : CheckBox(node, macrowindow) { - checkbox = (QCheckBox*)widget; + checkbox = (QCheckBox*)getWidget(); connect(this, SIGNAL(wasChanged()), this, SLOT(cgb_state_change())); QDomElement elem = node.toElement(); @@ -46,33 +46,33 @@ CheckGroupBox::CheckGroupBox(QDomNode &node, MacroWindow *macrowindow) if(type == "framed") { QGroupBox *gb = new QGroupBox(); gb->setTitle(" "); - widget = gb; + setWidget(gb); } else if(type == "simple") { - widget = new QWidget(); + setWidget(new QWidget()); } else { ERROR(checkgroupbox, "Illegal value of attribute 'type'\n"); return; } - setCommonLayout(widget, node); - setCommonAttributes(widget, node); + setCommonLayout(getWidget(), node); + setCommonAttributes(getWidget(), node); - checkbox->setParent(widget); + checkbox->setParent(getWidget()); checkbox->resize(checkbox->sizeHint().width(), 32); checkbox->show(); if(type == "framed") { - widget->setContentsMargins(0, 10, 0, 0); + getWidget()->setContentsMargins(0, 10, 0, 0); checkbox->move(5, -9); checkbox->setAutoFillBackground(true); } if(type == "simple") { - widget->setContentsMargins(checkbox->sizeHint().width(), 0, 0, 0); + getWidget()->setContentsMargins(checkbox->sizeHint().width(), 0, 0, 0); checkbox->move(0, 3); } - addChildren(node, widget->layout()); + addChildren(node, getWidget()->layout()); } CheckGroupBox::~CheckGroupBox() diff --git a/client/widgets/combobox.cc b/client/widgets/combobox.cc index 935f620..53a80e2 100644 --- a/client/widgets/combobox.cc +++ b/client/widgets/combobox.cc @@ -36,6 +36,7 @@ #include #include +#include #include #include "common.h" @@ -63,7 +64,7 @@ ComboBox::ComboBox(QDomNode &node, MacroWindow *macrowindow) : Widget(node, macrowindow) { combobox = new MyQComboBox(); - widget = combobox; + setWidget(combobox); ignoreChangeEvents = false; @@ -215,10 +216,9 @@ void ComboBox::changed() emit eventOnChange(); } -#include bool ComboBox::eventFilter(QObject *obj, QEvent *event) { - if(ignoreChangeEvents == true) return false; + if(ignoreChangeEvents == true) return Widget::eventFilter(obj, event); if(combotype == SELECT) { if(event->type() == QEvent::MouseButtonRelease) { @@ -235,7 +235,7 @@ bool ComboBox::eventFilter(QObject *obj, QEvent *event) } } - return QObject::eventFilter(obj, event); + return Widget::eventFilter(obj, event); } void ComboBox::changeEvent(QEvent *event) diff --git a/client/widgets/datetime.cc b/client/widgets/datetime.cc index f7780e2..be17278 100644 --- a/client/widgets/datetime.cc +++ b/client/widgets/datetime.cc @@ -34,7 +34,7 @@ DateTime::DateTime(QDomNode &node, MacroWindow *macrowindow) : Widget(node, macrowindow) { datetimeedit = new QDateTimeEdit(); - widget = datetimeedit; + setWidget(datetimeedit); changedByUser = true; setCommonAttributes(datetimeedit, node); diff --git a/client/widgets/frame.cc b/client/widgets/frame.cc index 076816a..23f542f 100644 --- a/client/widgets/frame.cc +++ b/client/widgets/frame.cc @@ -36,7 +36,7 @@ Frame::Frame(QDomNode &node, MacroWindow *macrowindow) : Widget(node, macrowindow) { frame = new QFrame(); - widget = frame; + setWidget(frame); setCommonAttributes(frame, node); setCommonLayout(frame, node); diff --git a/client/widgets/groupbox.cc b/client/widgets/groupbox.cc index 0d720ea..ef9a7e8 100644 --- a/client/widgets/groupbox.cc +++ b/client/widgets/groupbox.cc @@ -38,7 +38,7 @@ GroupBox::GroupBox(QDomNode &node, MacroWindow *macrowindow) : Widget(node, macrowindow) { groupbox = new QGroupBox(); - widget = groupbox; + setWidget(groupbox); setCommonAttributes(groupbox, node); setCommonLayout(groupbox, node); diff --git a/client/widgets/label.cc b/client/widgets/label.cc index d9a5814..453d26d 100644 --- a/client/widgets/label.cc +++ b/client/widgets/label.cc @@ -35,7 +35,7 @@ Label::Label(QDomNode &node, MacroWindow *macrowindow) : Widget(node, macrowindow) { label = new QLabel(); - widget = label; + setWidget(label); QDomElement elem = node.toElement(); diff --git a/client/widgets/lineedit.cc b/client/widgets/lineedit.cc index 10f1199..0dfa904 100644 --- a/client/widgets/lineedit.cc +++ b/client/widgets/lineedit.cc @@ -43,7 +43,7 @@ LineEdit::LineEdit(QDomNode &node, MacroWindow *macrowindow) : Widget(node, macrowindow) { lineedit = new QLineEdit(); - widget = lineedit; + setWidget(lineedit); setCommonAttributes(lineedit, node); @@ -109,7 +109,7 @@ void LineEdit::setValue(QString value, QString source) // setInitialValue(value); } -bool LineEdit::eventFilter(QObject *, QEvent *event) +bool LineEdit::eventFilter(QObject *obj, QEvent *event) { if(event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast(event); @@ -119,7 +119,8 @@ bool LineEdit::eventFilter(QObject *, QEvent *event) QCoreApplication::sendEvent(lineedit, &tabevent); } } - return false; + + return Widget::eventFilter(obj, event); } void LineEdit::changeEvent(QEvent *event) diff --git a/client/widgets/listbox.cc b/client/widgets/listbox.cc index 3000f35..a301c30 100644 --- a/client/widgets/listbox.cc +++ b/client/widgets/listbox.cc @@ -75,7 +75,7 @@ ListBox::ListBox(QDomNode &node, MacroWindow *macrowindow) : Widget(node, macrowindow) { listwidget = new QListWidget(); - widget = listwidget; + setWidget(listwidget); valueIsChangingByComputer = false; diff --git a/client/widgets/metawidget.cc b/client/widgets/metawidget.cc index a866445..4516fa1 100644 --- a/client/widgets/metawidget.cc +++ b/client/widgets/metawidget.cc @@ -40,7 +40,7 @@ MetaWidget::MetaWidget(QDomNode &node, MacroWindow *macrowindow) : Widget(node, macrowindow) { frame = new QFrame(); - widget = frame; + setWidget(frame); setCommonAttributes(frame, node); setCommonLayout(frame, node); diff --git a/client/widgets/multilist.cc b/client/widgets/multilist.cc index e261b52..24d3355 100644 --- a/client/widgets/multilist.cc +++ b/client/widgets/multilist.cc @@ -44,7 +44,7 @@ MultiList::MultiList(QDomNode &node, MacroWindow *macrowindow) : Widget(node, macrowindow) { frame = new QFrame(); - widget = frame; + setWidget(frame); hideChildren = true; @@ -223,7 +223,8 @@ bool MultiList::eventFilter(QObject *obj, QEvent *event) QKeyEvent *keyEvent = static_cast(event); if(keyEvent->key() == Qt::Key_Delete) remove(); } - return QObject::eventFilter(obj, event); + + return Widget::eventFilter(obj, event); } void MultiList::setWdgValid(bool valid) diff --git a/client/widgets/radiobutton.cc b/client/widgets/radiobutton.cc index a315577..47930d2 100644 --- a/client/widgets/radiobutton.cc +++ b/client/widgets/radiobutton.cc @@ -50,3 +50,8 @@ QString RadioButton::getValue() { return value; } + +void RadioButton::shortcutActivated() +{ + click(); +} diff --git a/client/widgets/radiobutton.h b/client/widgets/radiobutton.h index 6dbae7e..1dc707e 100644 --- a/client/widgets/radiobutton.h +++ b/client/widgets/radiobutton.h @@ -41,6 +41,7 @@ public: public slots: QString getValue(); + void shortcutActivated(); private: QString value; diff --git a/client/widgets/radiobuttons.cc b/client/widgets/radiobuttons.cc index f8b036f..c2febed 100644 --- a/client/widgets/radiobuttons.cc +++ b/client/widgets/radiobuttons.cc @@ -38,7 +38,7 @@ RadioButtons::RadioButtons(QDomNode &node, MacroWindow *macrowindow) : Widget(node, macrowindow) { frame = new QFrame(); - widget = frame; + setWidget(frame); setCommonAttributes(frame, node); setCommonLayout(frame, node); diff --git a/client/widgets/textedit.cc b/client/widgets/textedit.cc index 96783d9..14cd0ea 100644 --- a/client/widgets/textedit.cc +++ b/client/widgets/textedit.cc @@ -36,7 +36,7 @@ TextEdit::TextEdit(QDomNode &node, MacroWindow *macrowindow) : Widget(node, macrowindow) { textedit = new QPlainTextEdit(); - widget = textedit; + setWidget(textedit); setCommonAttributes(textedit, node); @@ -81,7 +81,7 @@ void TextEdit::setValue(QString value, QString source) // setInitialValue(value); } -bool TextEdit::eventFilter(QObject *, QEvent *event) +bool TextEdit::eventFilter(QObject *obj, QEvent *event) { if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast(event); @@ -91,7 +91,8 @@ bool TextEdit::eventFilter(QObject *, QEvent *event) } if(keyEvent->text() != "") emit wasChanged(); } - return false; + + return Widget::eventFilter(obj, event); } void TextEdit::setWdgValid(bool valid) diff --git a/client/widgets/widget.cc b/client/widgets/widget.cc index a57eb78..eaf1c9c 100644 --- a/client/widgets/widget.cc +++ b/client/widgets/widget.cc @@ -28,10 +28,13 @@ #include "macrowindow.h" #include "luawidget.h" - +#include "macrodrawer.h" #include "../widgets.h" + #include #include +#include +#include #include "debug.h" @@ -69,6 +72,23 @@ Widget::Widget(QDomNode &node, MacroWindow *macrowindow) hasOnInitEvent = elem.hasAttribute("onInit"); onInitEventScript = elem.attribute("onInit", ""); + important = + elem.hasAttribute("important") && elem.attribute("important") == "true"; + + shortcut_label = NULL; + shortcut_action = NULL; + if(elem.hasAttribute("shortcut")) { + QString shortcut = elem.attribute("shortcut", ""); + DEBUG(shortcut, "New shortcut: %s [%s]\n", + widget_name.toStdString().c_str(), + shortcut.toStdString().c_str()); + shortcut_action = new QAction(widget_name, this); + shortcut_action->setShortcut(QKeySequence(shortcut)); + // addAction(a); + connect(shortcut_action, SIGNAL(triggered()), + this, SLOT(shortcutActivated())); + } + is_valid = true; connect(this, SIGNAL(eventOnChange()), @@ -103,6 +123,69 @@ Widget::~Widget() delete widget; widget = NULL; } + + if(shortcut_label) delete shortcut_label; +} + +void Widget::setWidget(QWidget *widget) +{ + this->widget = widget; + if(!this->widget) return; + + if(important) { + this->widget->setProperty("important", QString("true")); + } + + if(shortcut_action) { + this->widget->addAction(shortcut_action); + + QString lbl = shortcut_action->shortcut().toString(); + shortcut_label = new ShortcutTooltip(lbl, macrowindow); + + // Catch shortcut label show/hide and ESC for cancel + macrowindow->drawer->installEventFilter(this); + } +} + +bool Widget::eventFilter(QObject *, QEvent *event) +{ + // The function of this 'p' is to determine if this particular event has + // been used to issue a close action. + // If this is in fact the case then we should not do it gain until a new + // event is received. + // It might be considered a hack, but it works... + static void *p = NULL; + if(p != event) p = NULL; + + if(event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) { + QKeyEvent *kevent = (QKeyEvent*)event; + + if(shortcut_label) { + bool visible = + (kevent->modifiers() & Qt::ControlModifier || + kevent->modifiers() & Qt::AltModifier) && + widget->isVisible(); + + shortcut_label->setShow(visible); + if(visible) { + shortcut_label->move(widget->mapTo(macrowindow, QPoint(-5,-5))); + } + } + + if(event->type() == QEvent::KeyPress && kevent->key() == Qt::Key_Escape) { + if(!p) { // See comment above. + macrowindow->cancel(); + p = event; + } + } + } + + return false; +} + +QWidget *Widget::getWidget() +{ + return this->widget; } QString Widget::name() @@ -311,6 +394,12 @@ bool Widget::hidden() return widget->isHidden(); } +void Widget::shortcutActivated() +{ + DEBUG(shortcut, "Shortcut [%s] activated\n", name().toStdString().c_str()); + setKeyboardFocus(); +} + bool Widget::setKeyboardFocus() { if((widget->focusPolicy() & Qt::TabFocus) != 0) { diff --git a/client/widgets/widget.h b/client/widgets/widget.h index f2bf278..1af20af 100644 --- a/client/widgets/widget.h +++ b/client/widgets/widget.h @@ -33,6 +33,8 @@ #include #include #include +#include +#include "shortcuttooltip.h" #include "lua.h" @@ -130,6 +132,8 @@ public: virtual void setValues(); + bool eventFilter(QObject *obj, QEvent *event); + signals: void wasChanged(); @@ -143,9 +147,12 @@ public slots: void childWasChanged(); void runEventOnChange(bool deep = false); void runEventOnInit(bool deep = false); + virtual void shortcutActivated(); protected: - QWidget *widget; + void setWidget(QWidget *widget); + QWidget *getWidget(); + bool hideChildren; // Implement in subclasses to be able to block commits. @@ -180,7 +187,14 @@ protected: bool hasOnInitEvent; QString onInitEventScript; + QAction *shortcut_action; + QFrame *replwidget; + +private: + QWidget *widget; + bool important; + ShortcutTooltip *shortcut_label; }; /*** diff --git a/client/widgets/window.cc b/client/widgets/window.cc index 7aa6374..6f879a8 100644 --- a/client/widgets/window.cc +++ b/client/widgets/window.cc @@ -33,29 +33,29 @@ Window::Window(QDomNode &node, MacroWindow *macrowindow) : Widget(node, macrowindow) { - widget = new QWidget(NULL); + setWidget(new QWidget(NULL)); - widget->setWindowFlags(Qt::WindowContextHelpButtonHint | + getWidget()->setWindowFlags(Qt::WindowContextHelpButtonHint | Qt::WindowSystemMenuHint); - widget->setWindowIcon(QIcon(":/icons/icon.png")); + getWidget()->setWindowIcon(QIcon(":/icons/icon.png")); - setCommonAttributes(widget, node); - setCommonLayout(widget, node); + setCommonAttributes(getWidget(), node); + setCommonLayout(getWidget(), node); QDomElement elem = node.toElement(); if(elem.hasAttribute("fixed")) { if(elem.attribute("fixed") == "true") { - widget->setFixedSize(widget->width(), widget->height()); + getWidget()->setFixedSize(getWidget()->width(), getWidget()->height()); } } if(elem.hasAttribute("caption")) { - widget->setWindowTitle(elem.attribute("caption")); + getWidget()->setWindowTitle(elem.attribute("caption")); } - addChildren(node, widget->layout()); + addChildren(node, getWidget()->layout()); } Window::~Window() -- cgit v1.2.3