Debian Codegraph @

Source/Core/DolphinQt/Config/Mapping/MappingButton.cpp

raw symbols SCIP
1 // Copyright 2017 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "DolphinQt/Config/Mapping/MappingButton.h"
5
6 #include <QApplication>
7 #include <QFontMetrics>
8 #include <QMouseEvent>
9 #include <QString>
10
11 #include "DolphinQt/Config/Mapping/IOWindow.h"
12 #include "DolphinQt/Config/Mapping/MappingWidget.h"
13 #include "DolphinQt/Config/Mapping/MappingWindow.h"
14
15 #include "InputCommon/ControlReference/ControlReference.h"
16 #include "InputCommon/ControllerEmu/ControllerEmu.h"
17 #include "InputCommon/ControllerInterface/ControllerInterface.h"
18
19 constexpr int SLIDER_TICK_COUNT = 100;
20
21 // Escape ampersands and simplify the text for a short preview
22 static QString RefToDisplayString(ControlReference* ref)
23 {
24 const bool expression_valid =
25 ref->GetParseStatus() != ciface::ExpressionParser::ParseStatus::SyntaxError;
26 QString expression;
27 {
28 const auto lock = ControllerEmu::EmulatedController::GetStateLock();
29 expression = QString::fromStdString(ref->GetExpression());
30 }
31
32 // Split by "`" so that we can give a better preview of control,
33 // without including their device in front of them, which is usually
34 // too long to actually see the control.
35 QStringList controls = expression.split(QLatin1Char{'`'});
36 // Do try to simplify controls if the parsing had failed, as it might create false positives.
37 if (expression_valid)
38 {
39 for (int i = 0; i < controls.size(); i++)
40 {
41 // We have two ` for control so make sure to only consider the odd ones.
42 if (i % 2)
43 {
44 // Use the code from the ControlQualifier instead of duplicating it.
45 ciface::ExpressionParser::ControlQualifier qualifier;
46 qualifier.FromString(controls[i].toStdString());
47 // If the control has got a device specifier/path, add ":" in front of it, to make it clear.
48 controls[i] = qualifier.has_device ? QStringLiteral(":") : QString();
49 controls[i].append(QString::fromStdString(qualifier.control_name));
50 }
51 else
52 {
53 controls[i].remove(QLatin1Char{' '});
54 }
55 }
56 }
57 // Do not re-add "`" to the final string, we don't need to see it.
58 expression = controls.join(QStringLiteral(""));
59
60 expression.remove(QLatin1Char{'\t'});
61 expression.remove(QLatin1Char{'\n'});
62 expression.remove(QLatin1Char{'\r'});
63 expression.replace(QLatin1Char{'&'}, QStringLiteral("&&"));
64
65 return expression;
66 }
67
68 MappingButton::MappingButton(MappingWidget* parent, ControlReference* ref, ControlType control_type)
69 : ElidedButton{RefToDisplayString(ref)}, m_mapping_window{parent->GetParent()},
70 m_reference{ref}, m_control_type{control_type}
71 {
72 if (m_reference->IsInput())
73 {
74 setToolTip(
75 tr("Left-click to detect input.\nMiddle-click to clear.\nRight-click for more options."));
76 }
77 else
78 {
79 setToolTip(tr("Left/Right-click to configure output.\nMiddle-click to clear."));
80 }
81
82 connect(this, &MappingButton::clicked, this, &MappingButton::Clicked);
83
84 connect(parent, &MappingWidget::ConfigChanged, this, &MappingButton::ConfigChanged);
85 connect(this, &MappingButton::ConfigChanged,
86 [this] { setText(RefToDisplayString(m_reference)); });
87 }
88
89 void MappingButton::AdvancedPressed()
90 {
91 m_mapping_window->CancelMapping();
92
93 IOWindow io(m_mapping_window, m_mapping_window->GetController(), m_reference,
94 m_reference->IsInput() ? IOWindow::Type::Input : IOWindow::Type::Output);
95 io.exec();
96
97 ConfigChanged();
98 m_mapping_window->Save();
99 }
100
101 void MappingButton::Clicked()
102 {
103 if (!m_reference->IsInput())
104 {
105 AdvancedPressed();
106 return;
107 }
108
109 m_mapping_window->QueueInputDetection(this);
110 }
111
112 void MappingButton::Clear()
113 {
114 m_reference->range = 100.0 / SLIDER_TICK_COUNT;
115
116 m_reference->SetExpression("");
117 m_mapping_window->GetController()->UpdateSingleControlReference(g_controller_interface,
118 m_reference);
119
120 m_mapping_window->Save();
121
122 m_mapping_window->UnQueueInputDetection(this);
123 }
124
125 void MappingButton::mouseReleaseEvent(QMouseEvent* event)
126 {
127 switch (event->button())
128 {
129 case Qt::MouseButton::MiddleButton:
130 Clear();
131 return;
132 case Qt::MouseButton::RightButton:
133 AdvancedPressed();
134 return;
135 default:
136 QPushButton::mouseReleaseEvent(event);
137 return;
138 }
139 }
140
141 ControlReference* MappingButton::GetControlReference()
142 {
143 return m_reference;
144 }
145
146 auto MappingButton::GetControlType() const -> ControlType
147 {
148 return m_control_type;
149 }