File: statusbarmanager.cpp

package info (click to toggle)
openterface-qt 0.1.0%2Bds-1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 1,444 kB
  • sloc: cpp: 9,552; sh: 127; python: 57; ansic: 4; makefile: 4
file content (135 lines) | stat: -rw-r--r-- 4,342 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "statusbarmanager.h"
#include <QPainter>
#include <QSvgRenderer>

StatusBarManager::StatusBarManager(QStatusBar *statusBar, QObject *parent)
    : QObject(parent), m_statusBar(statusBar)
{
    iconColor = QPalette().color(QPalette::WindowText);
    m_statusWidget = new StatusWidget(m_statusBar);
    m_statusBar->addPermanentWidget(m_statusWidget);
    initStatusBar();
}

void StatusBarManager::initStatusBar()
{
    mouseLabel = new QLabel(m_statusBar);
    mouseLocationLabel = new QLabel(QString("(0,0)"), m_statusBar);
    mouseLocationLabel->setFixedWidth(80);

    QWidget *mouseContainer = new QWidget(m_statusBar);
    QHBoxLayout *mouseLayout = new QHBoxLayout(mouseContainer);

    mouseLayout->setContentsMargins(0, 0, 0, 0);
    mouseLayout->addWidget(mouseLabel);
    mouseLayout->addWidget(mouseLocationLabel);
    m_statusBar->addWidget(mouseContainer);

    keyPressedLabel = new QLabel(m_statusBar);
    keyLabel = new QLabel(m_statusBar);
    keyLabel->setFixedWidth(120);

    QWidget *keyContainer = new QWidget(m_statusBar);
    QHBoxLayout *keyLayout = new QHBoxLayout(keyContainer);
    keyLayout->setContentsMargins(0, 0, 0, 0);
    keyLayout->addWidget(keyPressedLabel);
    keyLayout->addWidget(keyLabel);
    m_statusBar->addWidget(keyContainer);

    onLastKeyPressed("");
    updateIconColor();
}

void StatusBarManager::onLastKeyPressed(const QString& key)
{
    updateKeyboardIcon(key);
    keyLabel->setText(key);
}

void StatusBarManager::onLastMouseLocation(const QPoint& location, const QString& mouseEvent)
{
    QString svgPath;
    if (mouseEvent == "L") {
        svgPath = ":/images/mouse-left-button.svg";
    } else if (mouseEvent == "R") {
        svgPath = ":/images/mouse-right-button.svg";
    } else if (mouseEvent == "M") {
        svgPath = ":/images/mouse-middle-button.svg";
    } else {
        svgPath = ":/images/mouse-default.svg";
    }

    QPixmap pixmap = recolorSvg(svgPath, iconColor, QSize(12, 12));
    mouseLabel->setPixmap(pixmap);

    int capture_width = m_statusWidget->getCaptureWidth() > 5000 ? 0 : m_statusWidget->getCaptureWidth();
    int capture_height = m_statusWidget->getCaptureHeight() > 5000 ? 0 : m_statusWidget->getCaptureHeight();
    
    int mouse_x = static_cast<int>(location.x() / 4096.0 * capture_width);
    int mouse_y = static_cast<int>(location.y() / 4096.0 * capture_height);

    mouseLocationLabel->setText(QString("(%1,%2)").arg(mouse_x).arg(mouse_y));
}

void StatusBarManager::setConnectedPort(const QString& port, const int& baudrate)
{
    m_currentPort = port;
    m_statusWidget->setConnectedPort(port, baudrate);
}

void StatusBarManager::setStatusUpdate(const QString& status)
{
    m_statusWidget->setStatusUpdate(status);
}

void StatusBarManager::setInputResolution(int width, int height, float fps)
{
    m_statusWidget->setInputResolution(width, height, fps);
}

void StatusBarManager::setCaptureResolution(int width, int height, int fps)
{
    m_statusWidget->setCaptureResolution(width, height, fps);
}

QPixmap StatusBarManager::recolorSvg(const QString &svgPath, const QColor &color, const QSize &size)
{
    QSvgRenderer svgRenderer(svgPath);
    QPixmap pixmap(size);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    svgRenderer.render(&painter);

    QPixmap colorOverlay(size);
    colorOverlay.fill(color);

    painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
    painter.drawPixmap(0, 0, colorOverlay);

    return pixmap;
}

QColor StatusBarManager::getContrastingColor(const QColor &color)
{
    double luminance = (0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue()) / 255;
    return luminance > 0.5 ? QColor(0, 0, 0) : QColor(255, 255, 255);
}

void StatusBarManager::setTargetUsbConnected(bool isConnected)
{
    m_statusWidget->setTargetUsbConnected(isConnected);
}

void StatusBarManager::updateIconColor()
{
    iconColor = getContrastingColor(m_statusBar->palette().color(QPalette::Window));
    updateKeyboardIcon(keyLabel->text());
    onLastMouseLocation(QPoint(0, 0), "");
}

void StatusBarManager::updateKeyboardIcon(const QString& key)
{
    QString svgPath = key.isEmpty() ? ":/images/keyboard.svg" : ":/images/keyboard-pressed.svg";
    QPixmap pixmap = recolorSvg(svgPath, iconColor, QSize(18, 18));
    keyPressedLabel->setPixmap(pixmap);
}