game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
shortcut_cheatsheet.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// shortcut_cheatsheet.cpp
4// ==============================================================================
5// Keyboard shortcut cheat sheet overlay implementation
6// ==============================================================================
7
9#include "shortcut_manager.h"
10#include "theme_manager.h"
11
12#include <QKeyEvent>
13#include <QPainter>
14#include <QPainterPath>
15#include <QShowEvent>
16
18 : QWidget(parent), themeManager_(themeManager ? themeManager : &ThemeManager::instance())
19{
20 setWindowFlags(Qt::Widget);
21 setAttribute(Qt::WA_TranslucentBackground);
22 setFocusPolicy(Qt::StrongFocus);
23}
24
25void ShortcutCheatSheet::showEvent(QShowEvent* event)
26{
27 QWidget::showEvent(event);
28 // Ensure we cover the entire parent
29 if (parentWidget())
30 {
31 setGeometry(parentWidget()->rect());
32 raise();
33 }
34 setFocus();
35}
36
38{
39 Q_UNUSED(event);
40 hide();
41 deleteLater();
42}
43
45{
46 Q_UNUSED(event);
47 hide();
48 deleteLater();
49}
50
51void ShortcutCheatSheet::paintEvent(QPaintEvent* event)
52{
53 Q_UNUSED(event);
54
55 QPainter painter(this);
56 painter.setRenderHint(QPainter::Antialiasing);
57
58 const ThemeColors& c = themeManager_->colors();
59
60 // Semi-transparent dark background covering the entire widget
61 QColor overlayColor = c.windowBackground;
62 overlayColor.setAlpha(220);
63 painter.fillRect(rect(), overlayColor);
64
65 // Gather shortcuts grouped by category
66 auto& mgr = ShortcutManager::instance();
67 QStringList categories = mgr.getCategories();
68
69 // Build category -> list of (shortcut, label) pairs
70 struct Entry
71 {
72 QString shortcut;
73 QString label;
74 };
75 std::map<QString, std::vector<Entry>> grouped;
76 for (const auto& cat : categories)
77 {
78 auto actions = mgr.getActionsByCategory(cat);
79 for (const auto& action : actions)
80 {
81 if (action.currentShortcut.isEmpty())
82 continue;
83 grouped[cat].push_back(
84 {action.currentShortcut.toString(QKeySequence::NativeText), action.label});
85 }
86 }
87
88 // Calculate layout dimensions
89 int columnCount = qMin(static_cast<int>(grouped.size()), 3);
90 if (columnCount == 0)
91 return;
92
93 int padding = 40;
94 int columnGap = 30;
95 int contentWidth = width() - padding * 2;
96 int columnWidth = (contentWidth - columnGap * (columnCount - 1)) / columnCount;
97
98 // Title
99 QFont titleFont;
100 titleFont.setPointSize(themeManager_->scaledFontSize(18));
101 titleFont.setBold(true);
102 painter.setFont(titleFont);
103 painter.setPen(c.textPrimary);
104
105 QString title = "Keyboard Shortcuts";
106 QRect titleRect(0, padding, width(), 40);
107 painter.drawText(titleRect, Qt::AlignHCenter | Qt::AlignVCenter, title);
108
109 // Subtitle
110 QFont subtitleFont;
111 subtitleFont.setPointSize(themeManager_->scaledFontSize(10));
112 painter.setFont(subtitleFont);
113 painter.setPen(c.textMuted);
114 QRect subtitleRect(0, padding + 40, width(), 20);
115 painter.drawText(subtitleRect, Qt::AlignHCenter | Qt::AlignVCenter,
116 "Press any key or click to dismiss");
117
118 // Draw categories in columns
119 QFont categoryFont;
120 categoryFont.setPointSize(themeManager_->scaledFontSize(12));
121 categoryFont.setBold(true);
122
123 QFont shortcutFont;
124 shortcutFont.setPointSize(themeManager_->scaledFontSize(10));
125 shortcutFont.setBold(true);
126
127 QFont labelFont;
128 labelFont.setPointSize(themeManager_->scaledFontSize(10));
129
130 int startY = padding + 80;
131 int col = 0;
132
133 for (const auto& cat : categories)
134 {
135 auto it = grouped.find(cat);
136 if (it == grouped.end() || it->second.empty())
137 continue;
138
139 int x = padding + col * (columnWidth + columnGap);
140 int y = startY;
141
142 // Category header
143 painter.setFont(categoryFont);
144 painter.setPen(c.accent);
145 painter.drawText(x, y, columnWidth, 24, Qt::AlignLeft | Qt::AlignVCenter, cat);
146 y += 30;
147
148 // Separator line under category
149 painter.setPen(QPen(c.border, 1));
150 painter.drawLine(x, y, x + columnWidth, y);
151 y += 10;
152
153 // Entries
154 for (const auto& entry : it->second)
155 {
156 // Shortcut key badge
157 painter.setFont(shortcutFont);
158 QFontMetrics sfm(shortcutFont);
159 int badgeWidth = sfm.horizontalAdvance(entry.shortcut) + 16;
160 int badgeHeight = 24;
161
162 QRect badgeRect(x, y, badgeWidth, badgeHeight);
163 QPainterPath badgePath;
164 badgePath.addRoundedRect(badgeRect, 4, 4);
165 painter.setPen(Qt::NoPen);
166 painter.setBrush(c.inputBackground);
167 painter.drawPath(badgePath);
168
169 painter.setPen(c.textPrimary);
170 painter.drawText(badgeRect, Qt::AlignCenter, entry.shortcut);
171
172 // Label text
173 painter.setFont(labelFont);
174 painter.setPen(c.textSecondary);
175 int labelX = x + badgeWidth + 12;
176 painter.drawText(labelX, y, columnWidth - badgeWidth - 12, badgeHeight,
177 Qt::AlignLeft | Qt::AlignVCenter, entry.label);
178
179 y += badgeHeight + 8;
180 }
181
182 col++;
183 if (col >= columnCount)
184 {
185 col = 0;
186 startY = y + 20;
187 }
188 }
189}
void mousePressEvent(QMouseEvent *event) override
void showEvent(QShowEvent *event) override
ShortcutCheatSheet(QWidget *parent=nullptr, ThemeManager *themeManager=nullptr)
void keyPressEvent(QKeyEvent *event) override
void paintEvent(QPaintEvent *event) override
static ShortcutManager & instance()
Get singleton instance.
QColor inputBackground
QColor textSecondary
QColor textPrimary
QColor windowBackground
QColor textMuted