dolphin/Source/Core/DolphinQt/Config/Mapping/MappingCommon.cpp
Lioncash fef1b84f0a DolphinQt: Replace QStringLiteral with alternatives where applicable
QStringLiterals generate a buffer so that during runtime there's very
little cost to constructing a QString. However, this also means that
duplicated strings cannot be optimized out into a single entry that gets
referenced everywhere, taking up space in the binary.

Rather than use QStringLiteral(""), we can just use QString{} (the
default constructor) to signify the empty string. This gets rid of an
unnecessary string buffer from being created, saving a tiny bit of
space.

While we're at it, we can just use the character overloads of particular
functions when they're available instead of using a QString overload.
The characters in this case are Latin-1 to begin with, so we can just
specify the characters as QLatin1Char instances to use those overloads.
These will automatically convert to QChar if needed, so this is safe.
2019-07-30 09:06:03 -04:00

112 lines
3.0 KiB
C++

// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include "DolphinQt/Config/Mapping/MappingCommon.h"
#include <tuple>
#include <QApplication>
#include <QPushButton>
#include <QRegExp>
#include <QString>
#include <QTimer>
#include "DolphinQt/QtUtils/BlockUserInputFilter.h"
#include "InputCommon/ControlReference/ControlReference.h"
#include "InputCommon/ControllerInterface/Device.h"
#include "Common/Thread.h"
namespace MappingCommon
{
constexpr int INPUT_DETECT_TIME = 3000;
constexpr int OUTPUT_TEST_TIME = 2000;
QString GetExpressionForControl(const QString& control_name,
const ciface::Core::DeviceQualifier& control_device,
const ciface::Core::DeviceQualifier& default_device, Quote quote)
{
QString expr;
// non-default device
if (control_device != default_device)
{
expr += QString::fromStdString(control_device.ToString());
expr += QLatin1Char{':'};
}
// append the control name
expr += control_name;
if (quote == Quote::On)
{
QRegExp reg(QStringLiteral("[a-zA-Z]+"));
if (!reg.exactMatch(expr))
expr = QStringLiteral("`%1`").arg(expr);
}
return expr;
}
QString DetectExpression(QPushButton* button, ciface::Core::DeviceContainer& device_container,
const std::vector<std::string>& device_strings,
const ciface::Core::DeviceQualifier& default_device, Quote quote)
{
const auto filter = new BlockUserInputFilter(button);
button->installEventFilter(filter);
button->grabKeyboard();
button->grabMouse();
const auto old_text = button->text();
button->setText(QStringLiteral("..."));
// The button text won't be updated if we don't process events here
QApplication::processEvents();
// Avoid that the button press itself is registered as an event
Common::SleepCurrentThread(50);
const auto [device, input] = device_container.DetectInput(INPUT_DETECT_TIME, device_strings);
const auto timer = new QTimer(button);
button->connect(timer, &QTimer::timeout, [button, filter] {
button->releaseMouse();
button->releaseKeyboard();
button->removeEventFilter(filter);
});
// Prevent mappings of "space", "return", or mouse clicks from re-activating detection.
timer->start(500);
button->setText(old_text);
if (!input)
return {};
ciface::Core::DeviceQualifier device_qualifier;
device_qualifier.FromDevice(device.get());
return MappingCommon::GetExpressionForControl(QString::fromStdString(input->GetName()),
device_qualifier, default_device, quote);
}
void TestOutput(QPushButton* button, OutputReference* reference)
{
const auto old_text = button->text();
button->setText(QStringLiteral("..."));
// The button text won't be updated if we don't process events here
QApplication::processEvents();
reference->State(1.0);
Common::SleepCurrentThread(OUTPUT_TEST_TIME);
reference->State(0.0);
button->setText(old_text);
}
} // namespace MappingCommon