| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/**************************************************************************** |
| 3 |
* * |
| 4 |
* Copyright (c) 2025 Kacper Donat <[email protected]> * |
| 5 |
* * |
| 6 |
* This file is part of FreeCAD. * |
| 7 |
* * |
| 8 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 9 |
* under the terms of the GNU Lesser General Public License as * |
| 10 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 11 |
* License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 16 |
* Lesser General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Lesser General Public * |
| 19 |
* License along with FreeCAD. If not, see * |
| 20 |
* <https://www.gnu.org/licenses/>. * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
#include <QBuffer> |
| 25 |
#include <QPainter> |
| 26 |
|
| 27 |
#include <FCConfig.h> |
| 28 |
|
| 29 |
#include <BitmapFactory.h> |
| 30 |
|
| 31 |
#include "InputHint.h" |
| 32 |
#include "InputHintWidget.h" |
| 33 |
|
| 34 |
namespace |
| 35 |
{ |
| 36 |
constexpr int iconSize = 22; |
| 37 |
constexpr int iconMargin = 2; |
| 38 |
} // namespace |
| 39 |
|
| 40 |
Gui::InputHintWidget::InputHintWidget(QWidget* parent) |
| 41 |
: StatusBarLabel(parent, "InputHintEnabled") |
| 42 |
{ |
| 43 |
setMinimumHeight(iconSize + iconMargin * 2); |
| 44 |
} |
| 45 |
|
| 46 |
void Gui::InputHintWidget::showHints(const std::list<InputHint>& hints) |
| 47 |
{ |
| 48 |
if (hints.empty()) { |
| 49 |
clearHints(); |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
const auto getKeyImage = [this](InputHint::UserInput key) { |
| 54 |
const auto& factory = BitmapFactory(); |
| 55 |
|
| 56 |
QPixmap image = [&] { |
| 57 |
QColor color = palette().text().color(); |
| 58 |
|
| 59 |
if (auto iconPath = getCustomIconPath(key)) { |
| 60 |
return factory.pixmapFromSvg( |
| 61 |
*iconPath, |
| 62 |
QSize(iconSize, iconSize), |
| 63 |
{{0xFFFFFF, color.rgb() & RGB_MASK}} |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
return generateKeyIcon(key, color, iconSize); |
| 68 |
}(); |
| 69 |
|
| 70 |
|
| 71 |
QBuffer buffer; |
| 72 |
image.save(&buffer, "png"); |
| 73 |
|
| 74 |
return QStringLiteral("<img src=\"data:image/png;base64,%1\" height=%2 />") |
| 75 |
.arg(QString::fromLatin1(buffer.data().toBase64())) |
| 76 |
.arg(iconSize); |
| 77 |
}; |
| 78 |
|
| 79 |
const auto getHintHTML = [&](const InputHint& hint) { |
| 80 |
QString message = QStringLiteral("<td valign=bottom>%1</td>").arg(hint.message); |
| 81 |
|
| 82 |
for (const auto& sequence : hint.sequences) { |
| 83 |
QList<QString> keyImages; |
| 84 |
|
| 85 |
for (const auto key : sequence.keys) { |
| 86 |
keyImages.append(getKeyImage(key)); |
| 87 |
} |
| 88 |
|
| 89 |
message = message.arg(keyImages.join(QString {})); |
| 90 |
} |
| 91 |
|
| 92 |
return message; |
| 93 |
}; |
| 94 |
|
| 95 |
QStringList messages; |
| 96 |
for (const auto& hint : hints) { |
| 97 |
messages.append(getHintHTML(hint)); |
| 98 |
} |
| 99 |
|
| 100 |
QString html = QStringLiteral( |
| 101 |
"<table style=\"line-height: %1px\" height=%1>" |
| 102 |
"<tr>%2</tr>" |
| 103 |
"</table>" |
| 104 |
) |
| 105 |
.arg(iconSize + iconMargin * 2); |
| 106 |
|
| 107 |
setText(html.arg(messages.join(QStringLiteral("<td width=10></td>")))); |
| 108 |
} |
| 109 |
|
| 110 |
void Gui::InputHintWidget::clearHints() |
| 111 |
{ |
| 112 |
setText({}); |
| 113 |
} |
| 114 |
|
| 115 |
std::optional<const char*> Gui::InputHintWidget::getCustomIconPath(const InputHint::UserInput key) |
| 116 |
{ |
| 117 |
switch (key) { |
| 118 |
case InputHint::UserInput::MouseLeft: |
| 119 |
return ":/icons/user-input/mouse-left.svg"; |
| 120 |
case InputHint::UserInput::MouseRight: |
| 121 |
return ":/icons/user-input/mouse-right.svg"; |
| 122 |
case InputHint::UserInput::MouseMove: |
| 123 |
return ":/icons/user-input/mouse-move.svg"; |
| 124 |
case InputHint::UserInput::MouseMiddle: |
| 125 |
return ":/icons/user-input/mouse-middle.svg"; |
| 126 |
case InputHint::UserInput::MouseScroll: |
| 127 |
return ":/icons/user-input/mouse-scroll.svg"; |
| 128 |
case InputHint::UserInput::MouseScrollDown: |
| 129 |
return ":/icons/user-input/mouse-scroll-down.svg"; |
| 130 |
case InputHint::UserInput::MouseScrollUp: |
| 131 |
return ":/icons/user-input/mouse-scroll-up.svg"; |
| 132 |
default: |
| 133 |
return std::nullopt; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
QPixmap Gui::InputHintWidget::generateKeyIcon(const InputHint::UserInput key, const QColor color, int height) |
| 138 |
{ |
| 139 |
constexpr int margin = 3; |
| 140 |
constexpr int padding = 4; |
| 141 |
constexpr int radius = 2; |
| 142 |
const int iconSymbolHeight = height - 2 * margin; |
| 143 |
|
| 144 |
const QFont font(QStringLiteral("sans"), 10, QFont::Bold); |
| 145 |
const QFontMetrics fm(font); |
| 146 |
const QString text = inputRepresentation(key); |
| 147 |
const QRect textBoundingRect = fm.tightBoundingRect(text); |
| 148 |
|
| 149 |
const int symbolWidth = std::max(textBoundingRect.width() + padding * 2, iconSymbolHeight); |
| 150 |
|
| 151 |
const QRect keyRect(margin, margin, symbolWidth, iconSymbolHeight); |
| 152 |
|
| 153 |
QPixmap pixmap = BitmapFactory().empty({symbolWidth + margin * 2, height}); |
| 154 |
|
| 155 |
QPainter painter(&pixmap); |
| 156 |
painter.setRenderHint(QPainter::Antialiasing); |
| 157 |
painter.setPen(QPen(color, 2)); |
| 158 |
painter.setFont(font); |
| 159 |
painter.drawRoundedRect(keyRect, radius, radius); |
| 160 |
painter.drawText( |
| 161 |
// adjust the rectangle so it is visually centered |
| 162 |
// this is important for characters that are below baseline |
| 163 |
keyRect.translated(0, -(textBoundingRect.y() + textBoundingRect.height()) / 2), |
| 164 |
Qt::AlignHCenter, |
| 165 |
text |
| 166 |
); |
| 167 |
|
| 168 |
return pixmap; |
| 169 |
} |
| 170 |
|
| 171 |
QString Gui::InputHintWidget::inputRepresentation(const InputHint::UserInput key) |
| 172 |
{ |
| 173 |
using enum InputHint::UserInput; |
| 174 |
|
| 175 |
// clang-format off |
| 176 |
switch (key) { |
| 177 |
// Keyboard Keys |
| 178 |
case KeySpace: return QStringLiteral(" ␣ "); |
| 179 |
case KeyExclam: return QStringLiteral("!"); |
| 180 |
case KeyQuoteDbl: return QStringLiteral("\""); |
| 181 |
case KeyNumberSign: return QStringLiteral("-/+"); |
| 182 |
case KeyDollar: return QStringLiteral("$"); |
| 183 |
case KeyPercent: return QStringLiteral("%"); |
| 184 |
case KeyAmpersand: return QStringLiteral("&"); |
| 185 |
case KeyApostrophe: return QStringLiteral("\'"); |
| 186 |
case KeyParenLeft: return QStringLiteral("("); |
| 187 |
case KeyParenRight: return QStringLiteral(")"); |
| 188 |
case KeyAsterisk: return QStringLiteral("*"); |
| 189 |
case KeyPlus: return QStringLiteral("+"); |
| 190 |
case KeyComma: return QStringLiteral(","); |
| 191 |
case KeyMinus: return QStringLiteral("-"); |
| 192 |
case KeyPeriod: return QStringLiteral("."); |
| 193 |
case KeySlash: return QStringLiteral("/"); |
| 194 |
case Key0: return QStringLiteral("0"); |
| 195 |
case Key1: return QStringLiteral("1"); |
| 196 |
case Key2: return QStringLiteral("2"); |
| 197 |
case Key3: return QStringLiteral("3"); |
| 198 |
case Key4: return QStringLiteral("4"); |
| 199 |
case Key5: return QStringLiteral("5"); |
| 200 |
case Key6: return QStringLiteral("6"); |
| 201 |
case Key7: return QStringLiteral("7"); |
| 202 |
case Key8: return QStringLiteral("8"); |
| 203 |
case Key9: return QStringLiteral("9"); |
| 204 |
case KeyColon: return QStringLiteral(":"); |
| 205 |
case KeySemicolon: return QStringLiteral(";"); |
| 206 |
case KeyLess: return QStringLiteral("<"); |
| 207 |
case KeyEqual: return QStringLiteral("="); |
| 208 |
case KeyGreater: return QStringLiteral(">"); |
| 209 |
case KeyQuestion: return QStringLiteral("?"); |
| 210 |
case KeyAt: return QStringLiteral("@"); |
| 211 |
case KeyA: return QStringLiteral("A"); |
| 212 |
case KeyB: return QStringLiteral("B"); |
| 213 |
case KeyC: return QStringLiteral("C"); |
| 214 |
case KeyD: return QStringLiteral("D"); |
| 215 |
case KeyE: return QStringLiteral("E"); |
| 216 |
case KeyF: return QStringLiteral("F"); |
| 217 |
case KeyG: return QStringLiteral("G"); |
| 218 |
case KeyH: return QStringLiteral("H"); |
| 219 |
case KeyI: return QStringLiteral("I"); |
| 220 |
case KeyJ: return QStringLiteral("J"); |
| 221 |
case KeyK: return QStringLiteral("K"); |
| 222 |
case KeyL: return QStringLiteral("L"); |
| 223 |
case KeyM: return QStringLiteral("M"); |
| 224 |
case KeyN: return QStringLiteral("N"); |
| 225 |
case KeyO: return QStringLiteral("O"); |
| 226 |
case KeyP: return QStringLiteral("P"); |
| 227 |
case KeyQ: return QStringLiteral("Q"); |
| 228 |
case KeyR: return QStringLiteral("R"); |
| 229 |
case KeyS: return QStringLiteral("S"); |
| 230 |
case KeyT: return QStringLiteral("T"); |
| 231 |
case KeyU: return QStringLiteral("U"); |
| 232 |
case KeyV: return QStringLiteral("V"); |
| 233 |
case KeyW: return QStringLiteral("W"); |
| 234 |
case KeyX: return QStringLiteral("X"); |
| 235 |
case KeyY: return QStringLiteral("Y"); |
| 236 |
case KeyZ: return QStringLiteral("Z"); |
| 237 |
case KeyBracketLeft: return QStringLiteral("["); |
| 238 |
case KeyBackslash: return QStringLiteral("\\"); |
| 239 |
case KeyBracketRight: return QStringLiteral("]"); |
| 240 |
case KeyUnderscore: return QStringLiteral("_"); |
| 241 |
case KeyQuoteLeft: return QStringLiteral("\""); |
| 242 |
case KeyBraceLeft: return QStringLiteral("{"); |
| 243 |
case KeyBar: return QStringLiteral("|"); |
| 244 |
case KeyBraceRight: return QStringLiteral("}"); |
| 245 |
case KeyAsciiTilde: return QStringLiteral("~"); |
| 246 |
|
| 247 |
// misc keys |
| 248 |
/*: Keyboard key for Escape */ |
| 249 |
case KeyEscape: return tr("Esc"); |
| 250 |
/*: Keyboard key for Tab */ |
| 251 |
case KeyTab: return tr("Tab ⭾"); |
| 252 |
/*: Keyboard key for Backtab */ |
| 253 |
case KeyBacktab: return tr("Backtab"); |
| 254 |
case KeyBackspace: return QStringLiteral("⌫"); |
| 255 |
case KeyReturn: return QStringLiteral("↵"); |
| 256 |
/*: Keyboard key for numpad Enter */ |
| 257 |
case KeyEnter: return tr("Enter"); |
| 258 |
/*: Keyboard key for Insert */ |
| 259 |
case KeyInsert: return tr("Insert"); |
| 260 |
/*: Keyboard key for Delete */ |
| 261 |
case KeyDelete: return tr("Del"); |
| 262 |
/*: Keyboard key for Pause */ |
| 263 |
case KeyPause: return tr("Pause"); |
| 264 |
/*: Keyboard key for Print */ |
| 265 |
case KeyPrintScr: return tr("Print"); |
| 266 |
/*: Keyboard key for SysReq */ |
| 267 |
case KeySysReq: return tr("SysReq"); |
| 268 |
/*: Keyboard key for Clear */ |
| 269 |
case KeyClear: return tr("Clear"); |
| 270 |
|
| 271 |
// cursor movement |
| 272 |
/*: Keyboard key for Home */ |
| 273 |
case KeyHome: return tr("Home"); |
| 274 |
/*: Keyboard key for End */ |
| 275 |
case KeyEnd: return tr("End"); |
| 276 |
case KeyLeft: return QStringLiteral("←"); |
| 277 |
case KeyUp: return QStringLiteral("↑"); |
| 278 |
case KeyRight: return QStringLiteral("→"); |
| 279 |
case KeyDown: return QStringLiteral("↓"); |
| 280 |
/*: Keyboard key for Page Down */ |
| 281 |
case KeyPageUp: return tr("PgDown"); |
| 282 |
/*: Keyboard key for Page Up */ |
| 283 |
case KeyPageDown: return tr("PgUp"); |
| 284 |
|
| 285 |
// modifiers |
| 286 |
#ifdef FC_OS_MACOSX |
| 287 |
case KeyShift: return QStringLiteral("⇧"); |
| 288 |
case KeyControl: return QStringLiteral("⌘"); |
| 289 |
case KeyMeta: return QStringLiteral("⌃"); |
| 290 |
case KeyAlt: return QStringLiteral("⌥"); |
| 291 |
#else |
| 292 |
/*: Keyboard key for Shift on Windows & Linux */ |
| 293 |
case KeyShift: return tr("⇧ Shift"); |
| 294 |
/*: Keyboard key for Control on Windows & Linux */ |
| 295 |
case KeyControl: return tr("Ctrl"); |
| 296 |
#ifdef FC_OS_WIN32 |
| 297 |
case KeyMeta: return QStringLiteral("⊞ Win"); |
| 298 |
#else |
| 299 |
case KeyMeta: return QStringLiteral("❖ Meta"); |
| 300 |
#endif |
| 301 |
/*: Keyboard key for Alt on Windows & Linux */ |
| 302 |
case KeyAlt: return tr("Alt"); |
| 303 |
#endif |
| 304 |
/*: Keyboard key for Caps Lock */ |
| 305 |
case KeyCapsLock: return tr("Caps Lock"); |
| 306 |
/*: Keyboard key for Num Lock */ |
| 307 |
case KeyNumLock: return tr("Num Lock"); |
| 308 |
/*: Keyboard key for Scroll Lock */ |
| 309 |
case KeyScrollLock: return tr("Scroll Lock"); |
| 310 |
|
| 311 |
// function |
| 312 |
case KeyF1: return QStringLiteral("F1"); |
| 313 |
case KeyF2: return QStringLiteral("F2"); |
| 314 |
case KeyF3: return QStringLiteral("F3"); |
| 315 |
case KeyF4: return QStringLiteral("F4"); |
| 316 |
case KeyF5: return QStringLiteral("F5"); |
| 317 |
case KeyF6: return QStringLiteral("F6"); |
| 318 |
case KeyF7: return QStringLiteral("F7"); |
| 319 |
case KeyF8: return QStringLiteral("F8"); |
| 320 |
case KeyF9: return QStringLiteral("F9"); |
| 321 |
case KeyF10: return QStringLiteral("F10"); |
| 322 |
case KeyF11: return QStringLiteral("F11"); |
| 323 |
case KeyF12: return QStringLiteral("F12"); |
| 324 |
case KeyF13: return QStringLiteral("F13"); |
| 325 |
case KeyF14: return QStringLiteral("F14"); |
| 326 |
case KeyF15: return QStringLiteral("F15"); |
| 327 |
case KeyF16: return QStringLiteral("F16"); |
| 328 |
case KeyF17: return QStringLiteral("F17"); |
| 329 |
case KeyF18: return QStringLiteral("F18"); |
| 330 |
case KeyF19: return QStringLiteral("F19"); |
| 331 |
case KeyF20: return QStringLiteral("F20"); |
| 332 |
case KeyF21: return QStringLiteral("F21"); |
| 333 |
case KeyF22: return QStringLiteral("F22"); |
| 334 |
case KeyF23: return QStringLiteral("F23"); |
| 335 |
case KeyF24: return QStringLiteral("F24"); |
| 336 |
case KeyF25: return QStringLiteral("F25"); |
| 337 |
case KeyF26: return QStringLiteral("F26"); |
| 338 |
case KeyF27: return QStringLiteral("F27"); |
| 339 |
case KeyF28: return QStringLiteral("F28"); |
| 340 |
case KeyF29: return QStringLiteral("F29"); |
| 341 |
case KeyF30: return QStringLiteral("F30"); |
| 342 |
case KeyF31: return QStringLiteral("F31"); |
| 343 |
case KeyF32: return QStringLiteral("F32"); |
| 344 |
case KeyF33: return QStringLiteral("F33"); |
| 345 |
case KeyF34: return QStringLiteral("F34"); |
| 346 |
case KeyF35: return QStringLiteral("F35"); |
| 347 |
|
| 348 |
// numpad |
| 349 |
/*: Keyboard key for numpad 0 */ |
| 350 |
case KeyNum0: return tr("Num0"); |
| 351 |
/*: Keyboard key for numpad 1 */ |
| 352 |
case KeyNum1: return tr("Num1"); |
| 353 |
/*: Keyboard key for numpad 2 */ |
| 354 |
case KeyNum2: return tr("Num2"); |
| 355 |
/*: Keyboard key for numpad 3 */ |
| 356 |
case KeyNum3: return tr("Num3"); |
| 357 |
/*: Keyboard key for numpad 4 */ |
| 358 |
case KeyNum4: return tr("Num4"); |
| 359 |
/*: Keyboard key for numpad 5 */ |
| 360 |
case KeyNum5: return tr("Num5"); |
| 361 |
/*: Keyboard key for numpad 6 */ |
| 362 |
case KeyNum6: return tr("Num6"); |
| 363 |
/*: Keyboard key for numpad 7 */ |
| 364 |
case KeyNum7: return tr("Num7"); |
| 365 |
/*: Keyboard key for numpad 8 */ |
| 366 |
case KeyNum8: return tr("Num8"); |
| 367 |
/*: Keyboard key for numpad 9 */ |
| 368 |
case KeyNum9: return tr("Num9"); |
| 369 |
|
| 370 |
|
| 371 |
default: return QStringLiteral("???"); |
| 372 |
} |
| 373 |
// clang-format on |
| 374 |
} |