Skip to content

Commit

Permalink
Qt: Add swap memory card button
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek authored and refractionpcsx2 committed Jun 7, 2022
1 parent 547b2fc commit fed3ea5
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
38 changes: 35 additions & 3 deletions pcsx2-qt/Settings/MemoryCardSettingsWidget.cpp
Expand Up @@ -52,8 +52,7 @@ MemoryCardSettingsWidget::MemoryCardSettingsWidget(SettingsDialog* dialog, QWidg
SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.autoEject, "EmuCore", "McdEnableEjection", true);
SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.automaticManagement, "EmuCore", "McdFolderAutoManage", true);

for (u32 i = 0; i < static_cast<u32>(m_slots.size()); i++)
createSlotWidgets(&m_slots[i], i);
setupAdditionalUi();

m_ui.cardList->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_ui.cardList, &MemoryCardListWidget::itemSelectionChanged, this, &MemoryCardSettingsWidget::updateCardActions);
Expand Down Expand Up @@ -85,6 +84,20 @@ bool MemoryCardSettingsWidget::eventFilter(QObject* watched, QEvent* event)
return QWidget::eventFilter(watched, event);
}

void MemoryCardSettingsWidget::setupAdditionalUi()
{
for (u32 i = 0; i < static_cast<u32>(m_slots.size()); i++)
createSlotWidgets(&m_slots[i], i);

// button to swap memory cards
QToolButton* swap_button = new QToolButton(m_ui.portGroupBox);
swap_button->setIcon(QIcon::fromTheme("arrow-left-right-line"));
swap_button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
swap_button->setToolTip(tr("Swap Memory Cards"));
connect(swap_button, &QToolButton::clicked, this, &MemoryCardSettingsWidget::swapCards);
static_cast<QGridLayout*>(m_ui.portGroupBox->layout())->addWidget(swap_button, 0, 1);
}

void MemoryCardSettingsWidget::createSlotWidgets(SlotGroup* port, u32 slot)
{
port->root = new QWidget(m_ui.portGroupBox);
Expand All @@ -97,6 +110,7 @@ void MemoryCardSettingsWidget::createSlotWidgets(SlotGroup* port, u32 slot)
port->eject = new QToolButton(port->root);
port->eject->setIcon(QIcon::fromTheme("eject-line"));
port->eject->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
port->eject->setToolTip(tr("Eject Memory Card"));
connect(port->eject, &QToolButton::clicked, this, [this, slot]() { ejectSlot(slot); });

port->slot = new MemoryCardSlotWidget(port->root);
Expand All @@ -112,7 +126,7 @@ void MemoryCardSettingsWidget::createSlotWidgets(SlotGroup* port, u32 slot)
vert_layout->addWidget(port->enable, 0);
vert_layout->addLayout(bottom_layout, 1);

static_cast<QGridLayout*>(m_ui.portGroupBox->layout())->addWidget(port->root, 0, slot);
static_cast<QGridLayout*>(m_ui.portGroupBox->layout())->addWidget(port->root, 0, (slot != 0) ? 2 : 0);
}

void MemoryCardSettingsWidget::autoSizeUI()
Expand Down Expand Up @@ -296,6 +310,24 @@ void MemoryCardSettingsWidget::refresh()
updateCardActions();
}

void MemoryCardSettingsWidget::swapCards()
{
const std::string card_1_key(getSlotFilenameKey(0));
const std::string card_2_key(getSlotFilenameKey(1));
std::optional<std::string> card_1_name(m_dialog->getStringValue("MemoryCards", card_1_key.c_str(), std::nullopt));
std::optional<std::string> card_2_name(m_dialog->getStringValue("MemoryCards", card_2_key.c_str(), std::nullopt));
if (!card_1_name.has_value() || card_1_name->empty() ||
!card_2_name.has_value() || card_2_name->empty())
{
QMessageBox::critical(QtUtils::GetRootWidget(this), tr("Error"), tr("Both ports must have a card selected to swap."));
return;
}

m_dialog->setStringSettingValue("MemoryCards", card_1_key.c_str(), card_2_name->c_str());
m_dialog->setStringSettingValue("MemoryCards", card_2_key.c_str(), card_1_name->c_str());
refresh();
}

static QString getSizeSummary(const AvailableMcdInfo& mcd)
{
if (mcd.type == MemoryCardType::File)
Expand Down
2 changes: 2 additions & 0 deletions pcsx2-qt/Settings/MemoryCardSettingsWidget.h
Expand Up @@ -88,6 +88,7 @@ class MemoryCardSettingsWidget : public QWidget
private Q_SLOTS:
void listContextMenuRequested(const QPoint& pos);
void refresh();
void swapCards();

private:
struct SlotGroup
Expand All @@ -99,6 +100,7 @@ private Q_SLOTS:
};

void createSlotWidgets(SlotGroup* port, u32 slot);
void setupAdditionalUi();
void autoSizeUI();

void tryInsertCard(u32 slot, const QString& newCard);
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions pcsx2-qt/resources/resources.qrc
Expand Up @@ -2,6 +2,8 @@
<qresource>
<file>icons/address-book-new-22.png</file>
<file>icons/applications-system-24.png</file>
<file>icons/black/index.theme</file>
<file>icons/black/svg/arrow-left-right-line.svg</file>
<file>icons/black/svg/artboard-2-line.svg</file>
<file>icons/black/svg/book-open-line.svg</file>
<file>icons/black/svg/brush-line.svg</file>
Expand Down Expand Up @@ -42,14 +44,15 @@
<file>icons/black/svg/tv-2-line.svg</file>
<file>icons/black/svg/volume-up-line.svg</file>
<file>icons/black/svg/window-2-line.svg</file>
<file>icons/black/index.theme</file>
<file>icons/discord.png</file>
<file>icons/logo.png</file>
<file>icons/media-optical-24.png</file>
<file>icons/media-optical-gear-24.png</file>
<file>icons/media-optical.png</file>
<file>icons/QT.png</file>
<file>icons/update.png</file>
<file>icons/white/index.theme</file>
<file>icons/white/svg/arrow-left-right-line.svg</file>
<file>icons/white/svg/artboard-2-line.svg</file>
<file>icons/white/svg/book-open-line.svg</file>
<file>icons/white/svg/brush-line.svg</file>
Expand Down Expand Up @@ -90,7 +93,6 @@
<file>icons/white/svg/tv-2-line.svg</file>
<file>icons/white/svg/volume-up-line.svg</file>
<file>icons/white/svg/window-2-line.svg</file>
<file>icons/white/index.theme</file>
<file>images/dualshock-2.png</file>
</qresource>
</RCC>
</RCC>

0 comments on commit fed3ea5

Please sign in to comment.