Skip to content

Commit

Permalink
Merge pull request #5490 from ligfx/qtcontrollergridlayout
Browse files Browse the repository at this point in the history
DolphinQt2: use GridLayout for ControllersWindow
  • Loading branch information
shuffle2 committed Jun 6, 2017
2 parents b8f2e24 + 0cc8834 commit 10e54c8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 61 deletions.
121 changes: 64 additions & 57 deletions Source/Core/DolphinQt2/Config/ControllersWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <QApplication>
#include <QBoxLayout>
#include <QCheckBox>
#include <QComboBox>
#include <QDialog>
#include <QDialogButtonBox>
#include <QFormLayout>
#include <QGridLayout>
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QPushButton>
#include <QRadioButton>
#include <QSpacerItem>
#include <QScreen>
#include <QVBoxLayout>

#include <unordered_map>
Expand Down Expand Up @@ -86,16 +87,15 @@ ControllersWindow::ControllersWindow(QWidget* parent) : QDialog(parent)
void ControllersWindow::CreateGamecubeLayout()
{
m_gc_box = new QGroupBox(tr("GameCube Controllers"));
m_gc_layout = new QFormLayout();
m_gc_layout = new QGridLayout();
m_gc_layout->setVerticalSpacing(7);
m_gc_layout->setColumnStretch(1, 1);

for (size_t i = 0; i < m_gc_groups.size(); i++)
{
auto* gc_label = new QLabel(tr("Controller %1").arg(i + 1));
auto* gc_box = m_gc_controller_boxes[i] = new QComboBox();
auto* gc_button = m_gc_buttons[i] = new QPushButton(tr("Configure"));
auto* gc_group = m_gc_groups[i] = new QHBoxLayout();

gc_box->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
gc_button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

for (const auto& item :
{tr("None"), tr("Standard Controller"), tr("GameCube Adapter for Wii U"),
Expand All @@ -104,47 +104,57 @@ void ControllersWindow::CreateGamecubeLayout()
gc_box->addItem(item);
}

gc_group->addItem(new QSpacerItem(42, 1));
gc_group->addWidget(gc_box);
gc_group->addWidget(gc_button);

m_gc_layout->addRow(tr("Controller %1").arg(i + 1), gc_group);
int controller_row = m_gc_layout->rowCount();
m_gc_layout->addWidget(gc_label, controller_row, 0);
m_gc_layout->addWidget(gc_box, controller_row, 1);
m_gc_layout->addWidget(gc_button, controller_row, 2);
}
m_gc_box->setLayout(m_gc_layout);
}

static QHBoxLayout* CreateSubItem(QWidget* label, QWidget* widget)
static int GetRadioButtonIndicatorWidth()
{
QHBoxLayout* hbox = new QHBoxLayout();
hbox->addItem(new QSpacerItem(25, 1));

if (label != nullptr)
hbox->addWidget(label);
const QStyle* style = QApplication::style();
QStyleOptionButton opt;

if (widget != nullptr)
hbox->addWidget(widget);
// TODO: why does the macOS style act different? Is it because of the magic with
// Cocoa widgets it does behind the scenes?
if (style->objectName() == QStringLiteral("macintosh"))
return style->subElementRect(QStyle::SE_RadioButtonIndicator, &opt).width();

return hbox;
return style->subElementRect(QStyle::SE_RadioButtonContents, &opt).left();
}

static QHBoxLayout* CreateSubItem(QWidget* label, QLayoutItem* item)
static int GetLayoutHorizontalSpacing(const QGridLayout* layout)
{
QHBoxLayout* hbox = new QHBoxLayout();
hbox->addItem(new QSpacerItem(25, 1));

if (label != nullptr)
hbox->addWidget(label);

if (item != nullptr)
hbox->addItem(item);

return hbox;
// TODO: shouldn't layout->horizontalSpacing() do all this? Why does it return -1?
int hspacing = layout->horizontalSpacing();
if (hspacing >= 0)
return hspacing;

// According to docs, this is the fallback if horizontalSpacing() isn't set.
auto style = layout->parentWidget()->style();
hspacing = style->pixelMetric(QStyle::PM_LayoutHorizontalSpacing);
if (hspacing >= 0)
return hspacing;

// Docs claim this is deprecated, but on macOS with Qt 5.8 this is the only one that actually
// works.
float pixel_ratio = QGuiApplication::primaryScreen()->devicePixelRatio();
hspacing = pixel_ratio * style->pixelMetric(QStyle::PM_DefaultLayoutSpacing);
if (hspacing >= 0)
return hspacing;

// Ripped from qtbase/src/widgets/styles/qcommonstyle.cpp
return pixel_ratio * 6;
}

void ControllersWindow::CreateWiimoteLayout()
{
m_wiimote_layout = new QGridLayout();
m_wiimote_box = new QGroupBox(tr("Wii Remotes"));
m_wiimote_layout = new QFormLayout();
m_wiimote_box->setLayout(m_wiimote_layout);

m_wiimote_passthrough = new QRadioButton(tr("Use Bluetooth Passthrough"));
m_wiimote_sync = new QPushButton(tr("Sync"));
m_wiimote_reset = new QPushButton(tr("Reset"));
Expand All @@ -156,52 +166,49 @@ void ControllersWindow::CreateWiimoteLayout()
m_wiimote_real_balance_board = new QCheckBox(tr("Real Balance Board"));
m_wiimote_speaker_data = new QCheckBox(tr("Enable Speaker Data"));

m_wiimote_layout->setLabelAlignment(Qt::AlignLeft);
m_wiimote_layout->setVerticalSpacing(7);
m_wiimote_layout->setColumnMinimumWidth(0, GetRadioButtonIndicatorWidth() -
GetLayoutHorizontalSpacing(m_wiimote_layout));
m_wiimote_layout->setColumnStretch(2, 1);

// Passthrough BT
m_wiimote_layout->addWidget(m_wiimote_passthrough, m_wiimote_layout->rowCount(), 0, 1, -1);

m_wiimote_layout->addRow(m_wiimote_passthrough);

m_wiimote_sync->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
m_wiimote_reset->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
m_wiimote_refresh->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
int sync_row = m_wiimote_layout->rowCount();
m_wiimote_layout->addWidget(m_wiimote_pt_labels[0], sync_row, 1, 1, 2);
m_wiimote_layout->addWidget(m_wiimote_sync, sync_row, 3);

m_wiimote_layout->addRow(CreateSubItem(m_wiimote_pt_labels[0], m_wiimote_sync));
m_wiimote_layout->addRow(CreateSubItem(m_wiimote_pt_labels[1], m_wiimote_reset));
int reset_row = m_wiimote_layout->rowCount();
m_wiimote_layout->addWidget(m_wiimote_pt_labels[1], reset_row, 1, 1, 2);
m_wiimote_layout->addWidget(m_wiimote_reset, reset_row, 3);

// Emulated BT
m_wiimote_layout->addRow(m_wiimote_emu);
m_wiimote_layout->addWidget(m_wiimote_emu, m_wiimote_layout->rowCount(), 0, 1, -1);

for (size_t i = 0; i < m_wiimote_groups.size(); i++)
{
auto* wm_label = m_wiimote_labels[i] = new QLabel(tr("Wii Remote %1").arg(i + 1));
auto* wm_box = m_wiimote_boxes[i] = new QComboBox();
auto* wm_button = m_wiimote_buttons[i] = new QPushButton(tr("Configure"));
auto* wm_group = m_wiimote_groups[i] = new QHBoxLayout();

for (const auto& item :
{tr("None"), tr("Emulated Wii Remote"), tr("Real Wii Remote"), tr("Hybrid Wii Remote")})
{
wm_box->addItem(item);
}

wm_button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
wm_box->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

wm_group->addItem(new QSpacerItem(25, 1));
wm_group->addWidget(wm_label);
wm_group->addItem(new QSpacerItem(10, 1));
wm_group->addWidget(wm_box);
wm_group->addWidget(wm_button);

m_wiimote_layout->addRow(wm_group);
int wm_row = m_wiimote_layout->rowCount();
m_wiimote_layout->addWidget(wm_label, wm_row, 1);
m_wiimote_layout->addWidget(wm_box, wm_row, 2);
m_wiimote_layout->addWidget(wm_button, wm_row, 3);
}

m_wiimote_layout->addRow(CreateSubItem(m_wiimote_continuous_scanning, m_wiimote_refresh));
m_wiimote_layout->addRow(CreateSubItem(nullptr, m_wiimote_real_balance_board));
m_wiimote_layout->addRow(CreateSubItem(m_wiimote_speaker_data, new QSpacerItem(1, 35)));
int continuous_scanning_row = m_wiimote_layout->rowCount();
m_wiimote_layout->addWidget(m_wiimote_continuous_scanning, continuous_scanning_row, 1, 1, 2);
m_wiimote_layout->addWidget(m_wiimote_refresh, continuous_scanning_row, 3);

m_wiimote_box->setLayout(m_wiimote_layout);
m_wiimote_layout->addWidget(m_wiimote_real_balance_board, m_wiimote_layout->rowCount(), 1, 1, -1);
m_wiimote_layout->addWidget(m_wiimote_speaker_data, m_wiimote_layout->rowCount(), 1, 1, -1);
}

void ControllersWindow::CreateAdvancedLayout()
Expand Down
7 changes: 3 additions & 4 deletions Source/Core/DolphinQt2/Config/ControllersWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ class QDialogButtonBox;
class QCheckBox;
class QComboBox;
class QHBoxLayout;
class QFormLayout;
class QGridLayout;
class QGroupBox;
class QLabel;
class QVBoxLayout;
class QPushButton;
class QRadioButton;
class QSpacerItem;

class ControllersWindow final : public QDialog
{
Expand Down Expand Up @@ -54,15 +53,15 @@ class ControllersWindow final : public QDialog
// Gamecube
std::array<MappingWindow*, 4> m_gc_mappings;
QGroupBox* m_gc_box;
QFormLayout* m_gc_layout;
QGridLayout* m_gc_layout;
std::array<QComboBox*, 4> m_gc_controller_boxes;
std::array<QPushButton*, 4> m_gc_buttons;
std::array<QHBoxLayout*, 4> m_gc_groups;

// Wii Remote
std::array<MappingWindow*, 4> m_wiimote_mappings;
QGroupBox* m_wiimote_box;
QFormLayout* m_wiimote_layout;
QGridLayout* m_wiimote_layout;
std::array<QLabel*, 4> m_wiimote_labels;
std::array<QComboBox*, 4> m_wiimote_boxes;
std::array<QPushButton*, 4> m_wiimote_buttons;
Expand Down

0 comments on commit 10e54c8

Please sign in to comment.