2017-09-19 20:47:15 +01:00
|
|
|
#include "preferences.h"
|
|
|
|
#include <QFileDialog>
|
2017-10-06 20:48:14 +01:00
|
|
|
#include <QtGamepad/QGamepad>
|
2017-09-19 20:47:15 +01:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
int addDiskItem(QComboBox *disk, const QString & item)
|
|
|
|
{
|
|
|
|
if (!item.isEmpty())
|
|
|
|
{
|
|
|
|
// check if we already have a item with same name
|
|
|
|
const int position = disk->findText(item);
|
|
|
|
if (position != -1)
|
|
|
|
{
|
|
|
|
// and reuse it
|
|
|
|
disk->setCurrentIndex(position);
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// or add a new one
|
|
|
|
const int size = disk->count();
|
|
|
|
disk->insertItem(size, item);
|
|
|
|
disk->setCurrentIndex(size);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkDuplicates(const std::vector<QComboBox *> & disks, const size_t id, const int new_selection)
|
|
|
|
{
|
|
|
|
if (new_selection >= 1)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < disks.size(); ++i)
|
|
|
|
{
|
|
|
|
if (i != id)
|
|
|
|
{
|
|
|
|
const int disk_selection = disks[i]->currentIndex();
|
|
|
|
if (disk_selection == new_selection)
|
|
|
|
{
|
|
|
|
// eject disk
|
|
|
|
disks[i]->setCurrentIndex(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void initialiseDisks(const std::vector<QComboBox *> & disks, const std::vector<QString> & data)
|
|
|
|
{
|
|
|
|
// share the same model for all disks in a group
|
|
|
|
for (size_t i = 1; i < disks.size(); ++i)
|
|
|
|
{
|
|
|
|
disks[i]->setModel(disks[0]->model());
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < disks.size(); ++i)
|
|
|
|
{
|
|
|
|
addDiskItem(disks[i], data[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void fillData(const std::vector<QComboBox *> & disks, std::vector<QString> & data)
|
|
|
|
{
|
|
|
|
data.resize(disks.size());
|
|
|
|
for (size_t i = 0; i < disks.size(); ++i)
|
|
|
|
{
|
|
|
|
if (disks[i]->currentIndex() >= 1)
|
|
|
|
{
|
|
|
|
data[i] = disks[i]->currentText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-10 14:26:40 +01:00
|
|
|
void processSettingsGroup(QSettings & settings, QTreeWidgetItem * item)
|
2017-09-29 21:39:55 +01:00
|
|
|
{
|
|
|
|
QList<QTreeWidgetItem *> items;
|
2017-10-10 14:26:40 +01:00
|
|
|
|
|
|
|
// first the leaves
|
|
|
|
const QStringList children = settings.childKeys();
|
|
|
|
for (const auto & child : children)
|
2017-09-29 21:39:55 +01:00
|
|
|
{
|
2017-10-10 14:26:40 +01:00
|
|
|
QStringList columns;
|
|
|
|
columns.append(child); // the name
|
|
|
|
columns.append(settings.value(child).toString()); // the value
|
|
|
|
|
|
|
|
QTreeWidgetItem * newItem = new QTreeWidgetItem(item, columns);
|
|
|
|
items.append(newItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
// then the subtrees
|
|
|
|
const QStringList groups = settings.childGroups();
|
|
|
|
for (const auto & group : groups)
|
|
|
|
{
|
|
|
|
settings.beginGroup(group);
|
2017-09-29 21:39:55 +01:00
|
|
|
|
|
|
|
QStringList columns;
|
2017-10-10 14:26:40 +01:00
|
|
|
columns.append(group); // the name
|
2017-09-29 21:39:55 +01:00
|
|
|
|
|
|
|
QTreeWidgetItem * newItem = new QTreeWidgetItem(item, columns);
|
2017-10-10 14:26:40 +01:00
|
|
|
processSettingsGroup(settings, newItem); // process subtree
|
2017-09-29 21:39:55 +01:00
|
|
|
items.append(newItem);
|
2017-10-10 14:26:40 +01:00
|
|
|
|
|
|
|
settings.endGroup();
|
2017-09-29 21:39:55 +01:00
|
|
|
}
|
2017-10-10 14:26:40 +01:00
|
|
|
|
2017-09-29 21:39:55 +01:00
|
|
|
item->addChildren(items);
|
|
|
|
}
|
|
|
|
|
2017-09-19 20:47:15 +01:00
|
|
|
}
|
|
|
|
|
2017-09-21 20:31:52 +01:00
|
|
|
Preferences::Preferences(QWidget *parent) :
|
2017-09-19 20:47:15 +01:00
|
|
|
QDialog(parent)
|
|
|
|
{
|
|
|
|
setupUi(this);
|
|
|
|
|
|
|
|
// easier to handle in a vector
|
|
|
|
myDisks.push_back(disk1);
|
|
|
|
myDisks.push_back(disk2);
|
|
|
|
myHDs.push_back(hd1);
|
|
|
|
myHDs.push_back(hd2);
|
2017-09-21 20:31:52 +01:00
|
|
|
}
|
2017-09-19 20:47:15 +01:00
|
|
|
|
2017-10-10 14:26:40 +01:00
|
|
|
void Preferences::setup(const Data & data, QSettings & settings)
|
2017-10-06 20:48:14 +01:00
|
|
|
{
|
|
|
|
populateJoysticks();
|
|
|
|
setData(data);
|
2017-10-10 14:26:40 +01:00
|
|
|
setSettings(settings);
|
2017-10-06 20:48:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Preferences::populateJoysticks()
|
|
|
|
{
|
|
|
|
joystick->clear();
|
|
|
|
const QList<int> gamepads = QGamepadManager::instance()->connectedGamepads();
|
|
|
|
|
|
|
|
joystick->addItem("None");
|
|
|
|
|
|
|
|
for (int id : gamepads)
|
|
|
|
{
|
|
|
|
QGamepad gp(id);
|
|
|
|
QString name = gp.name();
|
|
|
|
if (name.isEmpty())
|
|
|
|
{
|
|
|
|
name = QString::number(id);
|
|
|
|
}
|
|
|
|
joystick->addItem(name, QVariant::fromValue(id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-10 14:26:40 +01:00
|
|
|
void Preferences::setSettings(QSettings & settings)
|
2017-09-29 21:39:55 +01:00
|
|
|
{
|
|
|
|
registryTree->clear();
|
2017-10-10 14:26:40 +01:00
|
|
|
|
|
|
|
QStringList columns;
|
|
|
|
columns.append(QString::fromUtf8("Registry")); // the name
|
|
|
|
columns.append(settings.fileName()); // the value
|
|
|
|
|
|
|
|
QTreeWidgetItem * newItem = new QTreeWidgetItem(registryTree, columns);
|
|
|
|
processSettingsGroup(settings, newItem);
|
|
|
|
|
2017-09-29 21:39:55 +01:00
|
|
|
registryTree->addTopLevelItem(newItem);
|
|
|
|
registryTree->expandAll();
|
|
|
|
}
|
|
|
|
|
2017-09-21 20:31:52 +01:00
|
|
|
void Preferences::setData(const Data & data)
|
|
|
|
{
|
2017-09-19 20:47:15 +01:00
|
|
|
initialiseDisks(myDisks, data.disks);
|
|
|
|
initialiseDisks(myHDs, data.hds);
|
2017-09-26 18:03:02 +01:00
|
|
|
|
|
|
|
apple2Type->setCurrentIndex(data.apple2Type);
|
|
|
|
mouse_4->setChecked(data.mouseInSlot4);
|
|
|
|
cpm_5->setChecked(data.cpmInSlot5);
|
|
|
|
hd_7->setChecked(data.hdInSlot7);
|
|
|
|
|
|
|
|
// synchronise
|
|
|
|
on_hd_7_clicked(data.hdInSlot7);
|
2017-10-06 20:48:14 +01:00
|
|
|
|
|
|
|
joystick->setCurrentText(data.joystick);
|
2017-09-19 20:47:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Preferences::Data Preferences::getData() const
|
|
|
|
{
|
|
|
|
Data data;
|
|
|
|
|
|
|
|
fillData(myDisks, data.disks);
|
|
|
|
fillData(myHDs, data.hds);
|
|
|
|
|
2017-09-26 18:03:02 +01:00
|
|
|
data.apple2Type = apple2Type->currentIndex();
|
|
|
|
data.mouseInSlot4 = mouse_4->isChecked();
|
|
|
|
data.cpmInSlot5 = cpm_5->isChecked();
|
|
|
|
data.hdInSlot7 = hd_7->isChecked();
|
2017-10-06 20:48:14 +01:00
|
|
|
|
|
|
|
if (joystick->currentIndex() >= 1)
|
|
|
|
{
|
|
|
|
const QVariant & device = joystick->itemData(joystick->currentIndex());
|
2017-10-10 14:27:07 +01:00
|
|
|
data.joystick = joystick->currentText();
|
2017-10-06 20:48:14 +01:00
|
|
|
data.joystickId = device.toInt();
|
|
|
|
}
|
2017-10-10 14:27:07 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
data.joystickId = 0;
|
|
|
|
}
|
2017-09-26 18:03:02 +01:00
|
|
|
|
2017-09-19 20:47:15 +01:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preferences::browseDisk(const std::vector<QComboBox *> & disks, const size_t id)
|
|
|
|
{
|
|
|
|
QFileDialog diskFileDialog(this);
|
|
|
|
diskFileDialog.setFileMode(QFileDialog::AnyFile);
|
|
|
|
|
|
|
|
if (disks[id]->currentIndex() >= 1)
|
|
|
|
{
|
|
|
|
diskFileDialog.selectFile(disks[id]->currentText());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (diskFileDialog.exec())
|
|
|
|
{
|
|
|
|
QStringList files = diskFileDialog.selectedFiles();
|
|
|
|
if (files.size() == 1)
|
|
|
|
{
|
|
|
|
const QString & filename = files[0];
|
|
|
|
const int selection = addDiskItem(disks[id], filename);
|
|
|
|
// and now make sure there are no duplicates
|
|
|
|
checkDuplicates(disks, id, selection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preferences::on_disk1_activated(int index)
|
|
|
|
{
|
|
|
|
checkDuplicates(myDisks, 0, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preferences::on_disk2_activated(int index)
|
|
|
|
{
|
|
|
|
checkDuplicates(myDisks, 1, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preferences::on_hd1_activated(int index)
|
|
|
|
{
|
|
|
|
checkDuplicates(myHDs, 0, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preferences::on_hd2_activated(int index)
|
|
|
|
{
|
|
|
|
checkDuplicates(myHDs, 1, index);
|
|
|
|
}
|
|
|
|
|
2017-09-26 18:03:02 +01:00
|
|
|
void Preferences::on_browse_disk1_clicked()
|
2017-09-19 20:47:15 +01:00
|
|
|
{
|
|
|
|
browseDisk(myDisks, 0);
|
|
|
|
}
|
|
|
|
|
2017-09-26 18:03:02 +01:00
|
|
|
void Preferences::on_browse_disk2_clicked()
|
2017-09-19 20:47:15 +01:00
|
|
|
{
|
|
|
|
browseDisk(myDisks, 1);
|
|
|
|
}
|
|
|
|
|
2017-09-26 18:03:02 +01:00
|
|
|
void Preferences::on_browse_hd1_clicked()
|
2017-09-19 20:47:15 +01:00
|
|
|
{
|
|
|
|
browseDisk(myHDs, 0);
|
|
|
|
}
|
|
|
|
|
2017-09-26 18:03:02 +01:00
|
|
|
void Preferences::on_browse_hd2_clicked()
|
2017-09-19 20:47:15 +01:00
|
|
|
{
|
|
|
|
browseDisk(myHDs, 1);
|
|
|
|
}
|
2017-09-26 18:03:02 +01:00
|
|
|
|
|
|
|
void Preferences::on_hd_7_clicked(bool checked)
|
|
|
|
{
|
|
|
|
hd1->setEnabled(checked);
|
|
|
|
hd2->setEnabled(checked);
|
|
|
|
browse_hd1->setEnabled(checked);
|
|
|
|
browse_hd2->setEnabled(checked);
|
|
|
|
}
|