mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-23 22:29:39 -06:00
Qt: extract ListTabWidget from SettingsWindow
This commit is contained in:
72
Source/Core/DolphinQt2/QtUtils/ListTabWidget.cpp
Normal file
72
Source/Core/DolphinQt2/QtUtils/ListTabWidget.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
// Copyright 2017 Dolphin Emulator Project
|
||||
// Licensed under GPLv2+
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include "DolphinQt2/QtUtils/ListTabWidget.h"
|
||||
|
||||
#include <QHBoxLayout>
|
||||
#include <QListWidget>
|
||||
#include <QScrollBar>
|
||||
#include <QStackedWidget>
|
||||
|
||||
class OverriddenListWidget : public QListWidget
|
||||
{
|
||||
public:
|
||||
// We want this widget to have a fixed width that fits all items, unlike a normal QListWidget
|
||||
// which adds scrollbars and expands/contracts.
|
||||
OverriddenListWidget() { setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred); }
|
||||
QSize sizeHint() const override
|
||||
{
|
||||
int width = sizeHintForColumn(0) + verticalScrollBar()->sizeHint().width() + 2 * frameWidth();
|
||||
int height = QListWidget::sizeHint().height();
|
||||
return {width, height};
|
||||
}
|
||||
};
|
||||
|
||||
ListTabWidget::ListTabWidget()
|
||||
{
|
||||
QHBoxLayout* layout = new QHBoxLayout();
|
||||
layout->setContentsMargins(0, 0, 0, 0);
|
||||
setLayout(layout);
|
||||
|
||||
m_labels = new OverriddenListWidget();
|
||||
layout->addWidget(m_labels);
|
||||
m_labels->setIconSize(QSize(32, 32));
|
||||
m_labels->setMovement(QListView::Static);
|
||||
m_labels->setSpacing(0);
|
||||
|
||||
m_display = new QStackedWidget();
|
||||
layout->addWidget(m_display);
|
||||
|
||||
connect(m_labels, &QListWidget::currentItemChanged, this,
|
||||
[=](QListWidgetItem* current, QListWidgetItem* previous) {
|
||||
if (!current)
|
||||
current = previous;
|
||||
m_display->setCurrentIndex(m_labels->row(current));
|
||||
});
|
||||
}
|
||||
|
||||
int ListTabWidget::addTab(QWidget* page, const QString& label)
|
||||
{
|
||||
QListWidgetItem* button = new QListWidgetItem();
|
||||
button->setText(label);
|
||||
button->setTextAlignment(Qt::AlignVCenter);
|
||||
button->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||||
|
||||
m_labels->addItem(button);
|
||||
if (!m_labels->currentItem())
|
||||
m_labels->setCurrentItem(button);
|
||||
|
||||
return m_display->addWidget(page);
|
||||
}
|
||||
|
||||
void ListTabWidget::setTabIcon(int index, const QIcon& icon)
|
||||
{
|
||||
if (auto* label = m_labels->item(index))
|
||||
label->setIcon(icon);
|
||||
}
|
||||
|
||||
void ListTabWidget::setCurrentIndex(int index)
|
||||
{
|
||||
m_labels->setCurrentRow(index);
|
||||
}
|
23
Source/Core/DolphinQt2/QtUtils/ListTabWidget.h
Normal file
23
Source/Core/DolphinQt2/QtUtils/ListTabWidget.h
Normal file
@ -0,0 +1,23 @@
|
||||
// Copyright 2017 Dolphin Emulator Project
|
||||
// Licensed under GPLv2+
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
class QListWidget;
|
||||
class QStackedWidget;
|
||||
|
||||
class ListTabWidget : public QWidget
|
||||
{
|
||||
public:
|
||||
ListTabWidget();
|
||||
int addTab(QWidget* page, const QString& label);
|
||||
void setTabIcon(int index, const QIcon& icon);
|
||||
void setCurrentIndex(int index);
|
||||
|
||||
private:
|
||||
QListWidget* m_labels;
|
||||
QStackedWidget* m_display;
|
||||
};
|
Reference in New Issue
Block a user