Add new shortcut HUD

This commit is contained in:
Victor Tran 2018-10-10 20:26:54 +11:00
parent 7ca2a12d4b
commit df2e77df3d
No known key found for this signature in database
GPG key ID: FBA10B22D602BAC1
7 changed files with 353 additions and 3 deletions

View file

@ -30,7 +30,8 @@ SOURCES += tvariantanimation.cpp \
tvirtualkeyboard.cpp \ tvirtualkeyboard.cpp \
tcircularspinner.cpp \ tcircularspinner.cpp \
tnotification/tnotification-common.cpp \ tnotification/tnotification-common.cpp \
tapplication.cpp tapplication.cpp \
tshortcuthud.cpp
HEADERS += tvariantanimation.h\ HEADERS += tvariantanimation.h\
the-libs_global.h \ the-libs_global.h \
@ -40,7 +41,8 @@ HEADERS += tvariantanimation.h\
tvirtualkeyboard.h \ tvirtualkeyboard.h \
tcircularspinner.h \ tcircularspinner.h \
tapplication.h \ tapplication.h \
tpromise.h tpromise.h \
tshortcuthud.h
unix { unix {
module.files = qt_thelib.pri module.files = qt_thelib.pri
@ -83,3 +85,6 @@ win32 {
DISTFILES += \ DISTFILES += \
qt_thelib.pri qt_thelib.pri
FORMS += \
tshortcuthud.ui

View file

@ -27,7 +27,7 @@
# define THELIBSSHARED_EXPORT Q_DECL_IMPORT # define THELIBSSHARED_EXPORT Q_DECL_IMPORT
#endif #endif
#define THE_LIBS_API_VERSION 1 #define THE_LIBS_API_VERSION 2
class THELIBSSHARED_EXPORT theLibsGlobal : public QObject { class THELIBSSHARED_EXPORT theLibsGlobal : public QObject {
Q_OBJECT Q_OBJECT

210
lib/tshortcuthud.cpp Normal file
View file

@ -0,0 +1,210 @@
#include "tshortcuthud.h"
#include "ui_tshortcuthud.h"
#include <QLabel>
#include <math.h>
tShortcutHud::tShortcutHud(QWidget *parent) :
QWidget(parent),
ui(new Ui::tShortcutHud)
{
ui->setupUi(this);
this->layout()->setContentsMargins(20 * theLibsGlobal::getDPIScaling(), 1, 20 * theLibsGlobal::getDPIScaling(), 0);
parent->setMouseTracking(true);
parent->installEventFilter(this);
resizeToParent();
}
tShortcutHud::~tShortcutHud()
{
delete ui;
}
void tintImage(QImage &image, QColor tint) {
//bool doPaint = true;
int failNum = 0;
for (int y = 0; y < image.height(); y++) {
for (int x = 0; x < image.width(); x++) {
QColor pixelCol = image.pixelColor(x, y);
//int blue = pixelCol.blue(), green = pixelCol.green(), red = pixelCol.red();
if ((pixelCol.blue() > pixelCol.green() - 10 && pixelCol.blue() < pixelCol.green() + 10) &&
(pixelCol.green() > pixelCol.red() - 10 && pixelCol.green() < pixelCol.red() + 10)) {
} else {
failNum++;
//doPaint = false;
}
}
}
if (failNum < (image.size().width() * image.size().height()) / 8) {
QPainter painter(&image);
painter.setCompositionMode(QPainter::CompositionMode_SourceAtop);
painter.fillRect(0, 0, image.width(), image.height(), tint);
painter.end();
}
}
void tShortcutHud::newShortcut(QShortcut* shortcut, QString shortcutText, ShortcutHudSide side) {
newShortcut(ShortcutGroup(QList<QShortcut*>() << shortcut, shortcutText, side));
}
void tShortcutHud::newShortcut(ShortcutGroup group) {
QBoxLayout* layout = new QBoxLayout(QBoxLayout::LeftToRight);
layout->setSpacing(10 * theLibsGlobal::getDPIScaling());
for (QShortcut* shortcut : group.shortcuts()) {
QKeySequence key = shortcut->key();
QString keyText = key.toString();
connect(shortcut, &QShortcut::activated, [=] {
this->setVisible(true);
this->raise();
});
QBoxLayout* keyLayout = new QBoxLayout(QBoxLayout::LeftToRight);
layout->setSpacing(3 * theLibsGlobal::getDPIScaling());
layout->addLayout(keyLayout);
QStringList keys = keyText.split("+");
for (QString key : keys) {
QLabel* keyLabel = new QLabel();
keyLabel->setPixmap(getKeyIcon(key));
keyLayout->addWidget(keyLabel);
}
}
QLabel* shortcutLabel = new QLabel();
shortcutLabel->setText(group.shortcutText());
layout->addWidget(shortcutLabel);
if (group.side() == Leading) {
ui->leadingLayout->addLayout(layout);
} else {
ui->trailingLayout->addLayout(layout);
}
}
QPixmap tShortcutHud::getKeyIcon(QString key) {
QPixmap squarePx(QSize(16, 16) * theLibsGlobal::getDPIScaling());
squarePx.fill(Qt::transparent);
QPainter sqPainter(&squarePx);
sqPainter.setRenderHint(QPainter::Antialiasing);
sqPainter.setPen(Qt::transparent);
sqPainter.setBrush(this->palette().color(QPalette::WindowText));
sqPainter.drawRoundedRect(QRect(QPoint(0, 0), squarePx.size()), 50, 50, Qt::RelativeSize);
QRect squareIconRect;
squareIconRect.setWidth(12 * theLibsGlobal::getDPIScaling());
squareIconRect.setHeight(12 * theLibsGlobal::getDPIScaling());
squareIconRect.moveCenter(QPoint(8, 8) * theLibsGlobal::getDPIScaling());
if (key == "Left") {
QImage image = QIcon::fromTheme("go-previous").pixmap(squareIconRect.size()).toImage();
tintImage(image, this->palette().color(QPalette::Window));
sqPainter.drawImage(squareIconRect, image);
return squarePx;
} else if (key == "Right") {
QImage image = QIcon::fromTheme("go-next").pixmap(squareIconRect.size()).toImage();
tintImage(image, this->palette().color(QPalette::Window));
sqPainter.drawImage(squareIconRect, image);
return squarePx;
} else {
QFont font = this->font();
QFontMetrics fontMetrics(font);
//font.setPointSizeF(floor(8));
while (QFontMetrics(font).height() > 14 * theLibsGlobal::getDPIScaling()) {
font.setPointSizeF(font.pointSizeF() - 0.5);
}
QSize pixmapSize;
pixmapSize.setHeight(16 * theLibsGlobal::getDPIScaling());
pixmapSize.setWidth(qMax(fontMetrics.width(key) + 6 * theLibsGlobal::getDPIScaling(), 16 * theLibsGlobal::getDPIScaling()));
QPixmap px(pixmapSize);
px.fill(Qt::transparent);
QPainter painter(&px);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::transparent);
painter.setBrush(this->palette().color(QPalette::WindowText));
painter.drawRoundedRect(QRect(QPoint(0, 0), px.size()), 4 * theLibsGlobal::getDPIScaling(), 4 * theLibsGlobal::getDPIScaling());
painter.setFont(font);
painter.setPen(this->palette().color(QPalette::Window));
QRect textRect;
textRect.setHeight(fontMetrics.height());
textRect.setWidth(fontMetrics.width(key));
textRect.moveCenter(QPoint(pixmapSize.width() / 2, pixmapSize.height() / 2));
painter.drawText(textRect, Qt::AlignCenter, key);
painter.end();
return px;
}
}
bool tShortcutHud::eventFilter(QObject* watched, QEvent* event) {
if (watched == this->parent()) {
if (event->type() == QEvent::KeyPress) {
this->setVisible(true);
this->raise();
} else if (event->type() == QEvent::MouseMove) {
this->setVisible(false);
} else if (event->type() == QEvent::Resize) {
resizeToParent();
}
}
return false;
}
void tShortcutHud::resizeToParent() {
this->setFixedHeight(32 * theLibsGlobal::getDPIScaling());
this->setFixedWidth(this->parentWidget()->width());
this->move(0, this->parentWidget()->height() - this->height());
}
void tShortcutHud::paintEvent(QPaintEvent* event) {
QPainter painter(this);
painter.setPen(this->palette().color(QPalette::WindowText));
painter.drawLine(0, 0, this->width(), 0);
}
tShortcutHud::ShortcutGroup::ShortcutGroup() {
}
tShortcutHud::ShortcutGroup::ShortcutGroup(QList<QShortcut*> shortcuts, QString shortcutText, ShortcutHudSide side) {
this->scs = shortcuts;
this->st = shortcutText;
this->sd = side;
}
void tShortcutHud::ShortcutGroup::addShortcut(QShortcut* shortcut) {
scs.append(shortcut);
}
void tShortcutHud::ShortcutGroup::setText(QString text) {
st = text;
}
void tShortcutHud::ShortcutGroup::setSize(ShortcutHudSide side) {
this->sd = side;
}
QList<QShortcut*> tShortcutHud::ShortcutGroup::shortcuts() {
return scs;
}
QString tShortcutHud::ShortcutGroup::shortcutText() {
return st;
}
tShortcutHud::ShortcutHudSide tShortcutHud::ShortcutGroup::side() {
return sd;
}

60
lib/tshortcuthud.h Normal file
View file

@ -0,0 +1,60 @@
#ifndef TSHORTCUTHUD_H
#define TSHORTCUTHUD_H
#include "the-libs_global.h"
#include <QWidget>
#include <QShortcut>
#include <QPaintEvent>
#include <QPainter>
namespace Ui {
class tShortcutHud;
}
class THELIBSSHARED_EXPORT tShortcutHud : public QWidget
{
Q_OBJECT
public:
explicit tShortcutHud(QWidget *parent = nullptr);
~tShortcutHud();
enum ShortcutHudSide {
Leading,
Trailing
};
class ShortcutGroup {
public:
ShortcutGroup();
ShortcutGroup(QList<QShortcut*> shortcuts, QString shortcutText, ShortcutHudSide side = Leading);
void addShortcut(QShortcut* shortcut);
void setText(QString text);
void setSize(ShortcutHudSide side);
QList<QShortcut*> shortcuts();
QString shortcutText();
ShortcutHudSide side();
private:
QList<QShortcut*> scs;
QString st;
ShortcutHudSide sd;
};
public slots:
void newShortcut(QShortcut* shortcut, QString shortcutText, ShortcutHudSide side = Leading);
void newShortcut(ShortcutGroup group);
private:
Ui::tShortcutHud *ui;
QPixmap getKeyIcon(QString key);
void resizeToParent();
void paintEvent(QPaintEvent* paintEvent);
bool eventFilter(QObject* watched, QEvent* event);
};
#endif // TSHORTCUTHUD_H

66
lib/tshortcuthud.ui Normal file
View file

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>tShortcutHud</class>
<widget class="QWidget" name="tShortcutHud">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>23</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<property name="autoFillBackground">
<bool>true</bool>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>20</number>
</property>
<property name="topMargin">
<number>1</number>
</property>
<property name="rightMargin">
<number>20</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<layout class="QHBoxLayout" name="leadingLayout">
<property name="spacing">
<number>20</number>
</property>
</layout>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>353</width>
<height>19</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QHBoxLayout" name="trailingLayout">
<property name="spacing">
<number>20</number>
</property>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View file

@ -103,3 +103,11 @@ void tVirtualKeyboard::setAutoUppercase(bool autoUpperCase) {
} }
#endif #endif
} }
void tVirtualKeyboard::setEnterKeyType(QString type) {
#ifdef T_OS_UNIX_NOT_MAC
if (isKeyboardRunning()) {
keyboardInterface->call(QDBus::NoBlock, "setEnterKeyType", type);
}
#endif
}

View file

@ -30,6 +30,7 @@ public slots:
void setHidden(bool hidden); void setHidden(bool hidden);
void setSensitive(bool sensitive); void setSensitive(bool sensitive);
void setAutoUppercase(bool autoUpperCase); void setAutoUppercase(bool autoUpperCase);
void setEnterKeyType(QString type);
private: private:
tVirtualKeyboard(); tVirtualKeyboard();