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/widget.cc | 91 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-) (limited to 'client/widgets/widget.cc') 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) { -- cgit v1.2.3