Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let cursor keys and PgUp/PgDown work as key combination on VCButtons #1584

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ui/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ add_library(${module_name}
virtualconsole/vcmatrixproperties.cpp virtualconsole/vcmatrixproperties.h virtualconsole/vcmatrixproperties.ui
virtualconsole/vcproperties.cpp virtualconsole/vcproperties.h virtualconsole/vcproperties.ui
virtualconsole/vcpropertieseditor.cpp virtualconsole/vcpropertieseditor.h
virtualconsole/vcscrollarea.cpp virtualconsole/vcscrollarea.h
virtualconsole/vcslider.cpp virtualconsole/vcslider.h
virtualconsole/vcsliderproperties.cpp virtualconsole/vcsliderproperties.h virtualconsole/vcsliderproperties.ui
virtualconsole/vcsoloframe.cpp virtualconsole/vcsoloframe.h
Expand Down
2 changes: 2 additions & 0 deletions ui/src/src.pro
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ HEADERS += virtualconsole/addvcbuttonmatrix.h \
virtualconsole/vcmatrixproperties.h \
virtualconsole/vcproperties.h \
virtualconsole/vcpropertieseditor.h \
virtualconsole/vcscrollarea.h \
virtualconsole/vcslider.h \
virtualconsole/vcsliderproperties.h \
virtualconsole/vcsoloframe.h \
Expand Down Expand Up @@ -337,6 +338,7 @@ SOURCES += virtualconsole/addvcbuttonmatrix.cpp \
virtualconsole/vcmatrixproperties.cpp \
virtualconsole/vcproperties.cpp \
virtualconsole/vcpropertieseditor.cpp \
virtualconsole/vcscrollarea.cpp \
virtualconsole/vcslider.cpp \
virtualconsole/vcsliderproperties.cpp \
virtualconsole/vcsoloframe.cpp \
Expand Down
48 changes: 48 additions & 0 deletions ui/src/virtualconsole/vcscrollarea.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "vcscrollarea.h"
#include "qevent.h"

#include <QDebug>

VCScrollArea::VCScrollArea(QWidget *parent) : QScrollArea(parent) {
// nothing more right now
}

void VCScrollArea::keyPressEvent(QKeyEvent *ev) {
unsigned int keyCode = ev->key() | ev->modifiers();
QList<unsigned int>::const_iterator it = std::find(m_overriddenKeyCodes.constBegin(), m_overriddenKeyCodes.constEnd(), keyCode);
if (it != m_overriddenKeyCodes.constEnd())
{
ev->ignore();
}
else
{
QScrollArea::keyPressEvent(ev);
}
}

bool VCScrollArea::needsOverride(const QKeySequence& ks) {
return ks == QKeySequence::MoveToPreviousPage
|| ks == QKeySequence::MoveToNextPage
|| ks == QKeySequence(Qt::Key_Up)
|| ks == QKeySequence(Qt::Key_Down)
|| ks == QKeySequence(Qt::Key_Left)
|| ks == QKeySequence(Qt::Key_Right);
}

void VCScrollArea::setKeyOverrides(const QList<QKeySequence> &keyOverrides) {
m_overriddenKeyCodes.clear();
for (QList<QKeySequence>::const_iterator it = keyOverrides.constBegin();
it != keyOverrides.constEnd(); ++it)
{
if (needsOverride(*it))
{
// all our key sequences have only one key
unsigned int keyCode = (*it)[0];
m_overriddenKeyCodes.append(keyCode);
}
}
}

void VCScrollArea::clearKeyOverrides() {
m_overriddenKeyCodes.clear();
}
26 changes: 26 additions & 0 deletions ui/src/virtualconsole/vcscrollarea.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef VCSCROLLAREA_H
#define VCSCROLLAREA_H

#include <QObject>
#include <QScrollArea>
#include <QWidget>

class VCScrollArea : public QScrollArea
{
Q_OBJECT
public:
explicit VCScrollArea(QWidget *parent = nullptr);

static bool needsOverride(const QKeySequence& keySequence);

void setKeyOverrides(const QList<QKeySequence> &keyOverrides);
void clearKeyOverrides();

protected:
void keyPressEvent(QKeyEvent *) override;

private:
QList<unsigned int> m_overriddenKeyCodes;
};

#endif // VCSCROLLAREA_H
25 changes: 24 additions & 1 deletion ui/src/virtualconsole/virtualconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1583,7 +1583,7 @@ void VirtualConsole::initContents()
{
Q_ASSERT(layout() != NULL);

m_scrollArea = new QScrollArea(this);
m_scrollArea = new VCScrollArea(this);
m_contentsLayout->addWidget(m_scrollArea);
m_scrollArea->setAlignment(Qt::AlignCenter);
m_scrollArea->setWidgetResizable(false);
Expand Down Expand Up @@ -1661,6 +1661,8 @@ bool VirtualConsole::liveEdit() const

void VirtualConsole::enableEdit()
{
disableKeyOverrides();

// Allow editing and adding in design mode
m_toolsSettingsAction->setEnabled(true);
m_editActionGroup->setEnabled(true);
Expand Down Expand Up @@ -1759,6 +1761,8 @@ void VirtualConsole::disableEdit()
m_stackingRaiseAction->setShortcut(QKeySequence());
m_stackingLowerAction->setShortcut(QKeySequence());

enableKeyOverrides();

// Hide toolbar; there's nothing usable there in operate mode
m_toolbar->hide();

Expand All @@ -1768,6 +1772,25 @@ void VirtualConsole::disableEdit()
m_contents->setFocus();
}

void VirtualConsole::enableKeyOverrides() {
QList<QKeySequence> keySequencesToOverride;

for (QHash<quint32, VCWidget *>::const_iterator it = m_widgetsMap.constBegin();
it != m_widgetsMap.constEnd(); ++it)
{
VCButton *btn = qobject_cast<VCButton*>(*it);
if (btn)
{
keySequencesToOverride.append(btn->keySequence());
}
}
m_scrollArea->setKeyOverrides(keySequencesToOverride);
}

void VirtualConsole::disableKeyOverrides() {
m_scrollArea->clearKeyOverrides();
}

void VirtualConsole::slotModeChanged(Doc::Mode mode)
{
if (mode == Doc::Operate)
Expand Down
8 changes: 6 additions & 2 deletions ui/src/virtualconsole/virtualconsole.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <QFrame>
#include <QList>

#include "vcscrollarea.h"
#include "vcproperties.h"
#include "doc.h"

Expand All @@ -34,7 +35,7 @@ class QXmlStreamWriter;
class VirtualConsole;
class QActionGroup;
class QVBoxLayout;
class QScrollArea;
class VCScrollArea;
class VCDockArea;
class QKeyEvent;
class QToolBar;
Expand Down Expand Up @@ -336,13 +337,16 @@ public slots:

protected:
QVBoxLayout* m_contentsLayout;
QScrollArea* m_scrollArea;
VCScrollArea* m_scrollArea;
VCFrame* m_contents;
QHash <quint32, VCWidget *> m_widgetsMap;

/*********************************************************************
* Key press handler
*********************************************************************/
private:
void enableKeyOverrides();
void disableKeyOverrides();

protected:
/** Handler for keyboard key presse events */
Expand Down
Loading