Merge pull request #5503 from ligfx/qtelidedbutton

DolphinQt2: add ElidedButton for controller mapping buttons
This commit is contained in:
Leo Lam 2017-06-03 12:47:18 +02:00 committed by GitHub
commit ccccb8463d
8 changed files with 74 additions and 8 deletions

View File

@ -39,6 +39,7 @@ set(SRCS
GameList/GameListModel.cpp GameList/GameListModel.cpp
GameList/GameTracker.cpp GameList/GameTracker.cpp
GameList/ListProxyModel.cpp GameList/ListProxyModel.cpp
QtUtils/ElidedButton.cpp
Settings/GeneralPane.cpp Settings/GeneralPane.cpp
Settings/InterfacePane.cpp Settings/InterfacePane.cpp
) )

View File

@ -17,9 +17,8 @@
#include "InputCommon/ControllerInterface/ControllerInterface.h" #include "InputCommon/ControllerInterface/ControllerInterface.h"
MappingButton::MappingButton(MappingWidget* widget, ControlReference* ref) MappingButton::MappingButton(MappingWidget* widget, ControlReference* ref)
: QPushButton(QString::fromStdString(ref->expression)), m_parent(widget), m_reference(ref) : ElidedButton(QString::fromStdString(ref->expression)), m_parent(widget), m_reference(ref)
{ {
setText(QString::fromStdString(m_reference->expression));
Connect(); Connect();
} }

View File

@ -4,16 +4,14 @@
#pragma once #pragma once
#include <QPoint> #include "DolphinQt2/QtUtils/ElidedButton.h"
#include <QPushButton>
#include <QString>
class ControlReference; class ControlReference;
class MappingWidget; class MappingWidget;
class QEvent; class QEvent;
class QMouseEvent; class QMouseEvent;
class MappingButton : public QPushButton class MappingButton : public ElidedButton
{ {
public: public:
MappingButton(MappingWidget* widget, ControlReference* ref); MappingButton(MappingWidget* widget, ControlReference* ref);

View File

@ -49,6 +49,7 @@ QGroupBox* MappingWidget::CreateGroupBox(const QString& name, ControllerEmu::Con
for (auto& control : group->controls) for (auto& control : group->controls)
{ {
auto* button = new MappingButton(this, control->control_ref.get()); auto* button = new MappingButton(this, control->control_ref.get());
button->setMinimumWidth(125);
button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
form_layout->addRow(QString::fromStdString(control->name), button); form_layout->addRow(QString::fromStdString(control->name), button);
m_buttons.push_back(button); m_buttons.push_back(button);

View File

@ -154,6 +154,7 @@
<ClCompile Include="Main.cpp" /> <ClCompile Include="Main.cpp" />
<ClCompile Include="MainWindow.cpp" /> <ClCompile Include="MainWindow.cpp" />
<ClCompile Include="MenuBar.cpp" /> <ClCompile Include="MenuBar.cpp" />
<ClCompile Include="QtUtils\ElidedButton.cpp" />
<ClCompile Include="RenderWidget.cpp" /> <ClCompile Include="RenderWidget.cpp" />
<ClCompile Include="Resources.cpp" /> <ClCompile Include="Resources.cpp" />
<ClCompile Include="Settings.cpp" /> <ClCompile Include="Settings.cpp" />
@ -254,4 +255,4 @@
<Message Text="Copy: @(BinaryFiles) -&gt; $(BinaryOutputDir)" Importance="High" /> <Message Text="Copy: @(BinaryFiles) -&gt; $(BinaryOutputDir)" Importance="High" />
<Copy SourceFiles="@(BinaryFiles)" DestinationFolder="$(BinaryOutputDir)" /> <Copy SourceFiles="@(BinaryFiles)" DestinationFolder="$(BinaryOutputDir)" />
</Target> </Target>
</Project> </Project>

View File

@ -116,6 +116,9 @@
<ClCompile Include="Config\Mapping\WiimoteEmuExtension.cpp" /> <ClCompile Include="Config\Mapping\WiimoteEmuExtension.cpp" />
<ClCompile Include="Config\Mapping\WiimoteEmuGeneral.cpp" /> <ClCompile Include="Config\Mapping\WiimoteEmuGeneral.cpp" />
<ClCompile Include="Config\Mapping\WiimoteEmuMotionControl.cpp" /> <ClCompile Include="Config\Mapping\WiimoteEmuMotionControl.cpp" />
<ClCompile Include="QtUtils\ElidedButton.cpp">
<Filter>QtUtils</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<QtMoc Include="MainWindow.h" /> <QtMoc Include="MainWindow.h" />
@ -177,6 +180,9 @@
<Filter Include="Config"> <Filter Include="Config">
<UniqueIdentifier>{42f8a963-563e-420d-8aca-5761657dcff5}</UniqueIdentifier> <UniqueIdentifier>{42f8a963-563e-420d-8aca-5761657dcff5}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="QtUtils">
<UniqueIdentifier>{fb4b5691-df66-4984-af40-fcc2b37f3622}</UniqueIdentifier>
</Filter>
<Filter Include="Settings"> <Filter Include="Settings">
<UniqueIdentifier>{d2a31121-7903-4a66-9b42-9358e92d36ff}</UniqueIdentifier> <UniqueIdentifier>{d2a31121-7903-4a66-9b42-9358e92d36ff}</UniqueIdentifier>
</Filter> </Filter>
@ -184,4 +190,4 @@
<ItemGroup> <ItemGroup>
<ClInclude Include="Resources.h" /> <ClInclude Include="Resources.h" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,40 @@
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include "DolphinQt2/QtUtils/ElidedButton.h"
#include <QFontMetrics>
#include <QStyleOptionButton>
#include <QStylePainter>
ElidedButton::ElidedButton(const QString& text, Qt::TextElideMode elide_mode)
: QPushButton(text, nullptr), m_elide_mode{elide_mode}
{
}
Qt::TextElideMode ElidedButton::elideMode() const
{
return m_elide_mode;
}
void ElidedButton::setElideMode(Qt::TextElideMode elide_mode)
{
if (elide_mode == m_elide_mode)
return;
m_elide_mode = elide_mode;
repaint();
}
void ElidedButton::paintEvent(QPaintEvent* event)
{
QStyleOptionButton option;
initStyleOption(&option);
option.text = fontMetrics().elidedText(
text(), m_elide_mode,
style()->subElementRect(QStyle::SE_PushButtonContents, &option, this).width());
QStylePainter{this}.drawControl(QStyle::CE_PushButton, option);
}

View File

@ -0,0 +1,20 @@
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#pragma once
#include <QPushButton>
class ElidedButton : public QPushButton
{
public:
ElidedButton(const QString& text = QStringLiteral(""),
Qt::TextElideMode elide_mode = Qt::ElideRight);
Qt::TextElideMode elideMode() const;
void setElideMode(Qt::TextElideMode elide_mode);
private:
void paintEvent(QPaintEvent* event);
Qt::TextElideMode m_elide_mode;
};