cutter/src/dialogs/preferences/PreferencesDialog.cpp

118 lines
4.2 KiB
C++
Raw Normal View History

#include "PreferencesDialog.h"
#include "ui_PreferencesDialog.h"
#include "AppearanceOptionsWidget.h"
#include "AsmOptionsWidget.h"
#include "GraphOptionsWidget.h"
#include "DebugOptionsWidget.h"
2019-03-16 12:41:45 +00:00
#include "PluginsOptionsWidget.h"
#include "InitializationFileEditor.h"
#include "AnalysisOptionsWidget.h"
#include "PreferenceCategory.h"
2018-10-17 07:55:53 +00:00
#include "common/Helpers.h"
#include "common/Configuration.h"
2019-03-16 12:41:45 +00:00
#include <QDialogButtonBox>
PreferencesDialog::PreferencesDialog(QWidget *parent)
2021-01-24 14:50:13 +00:00
: QDialog(parent), ui(new Ui::PreferencesDialog)
{
setAttribute(Qt::WA_DeleteOnClose);
ui->setupUi(this);
setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
QList<PreferenceCategory> prefs {
2021-01-24 14:50:13 +00:00
{ tr("Disassembly"),
new AsmOptionsWidget(this),
QIcon(":/img/icons/disas.svg"),
{
{ "Graph", new GraphOptionsWidget(this), QIcon(":/img/icons/graph.svg") },
} },
{ tr("Debug"), new DebugOptionsWidget(this), QIcon(":/img/icons/bug.svg") },
{ tr("Appearance"), new AppearanceOptionsWidget(this), QIcon(":/img/icons/polar.svg") },
{ tr("Plugins"), new PluginsOptionsWidget(this), QIcon(":/img/icons/plugins.svg") },
{ tr("Initialization Script"), new InitializationFileEditor(this),
QIcon(":/img/icons/initialization.svg") },
{ tr("Analysis"), new AnalysisOptionsWidget(this), QIcon(":/img/icons/cog_light.svg") }
};
2021-01-24 14:50:13 +00:00
for (auto &c : prefs) {
c.addItem(*ui->configCategories, *ui->configPanel);
}
2021-01-24 14:50:13 +00:00
connect(ui->configCategories, &QTreeWidget::currentItemChanged, this,
&PreferencesDialog::changePage);
connect(ui->saveButtons, &QDialogButtonBox::accepted, this, &QWidget::close);
QTreeWidgetItem *defitem = ui->configCategories->topLevelItem(0);
ui->configCategories->setCurrentItem(defitem, 0);
2021-01-24 14:50:13 +00:00
connect(Config(), &Configuration::interfaceThemeChanged, this,
&PreferencesDialog::chooseThemeIcons);
chooseThemeIcons();
}
2021-01-24 14:50:13 +00:00
PreferencesDialog::~PreferencesDialog() {}
void PreferencesDialog::showSection(PreferencesDialog::Section section)
{
QTreeWidgetItem *defitem;
2018-03-21 20:32:32 +00:00
switch (section) {
case Section::Appearance:
ui->configPanel->setCurrentIndex(0);
defitem = ui->configCategories->topLevelItem(0);
ui->configCategories->setCurrentItem(defitem, 0);
2018-03-21 20:32:32 +00:00
break;
case Section::Disassembly:
ui->configPanel->setCurrentIndex(1);
defitem = ui->configCategories->topLevelItem(1);
ui->configCategories->setCurrentItem(defitem, 1);
2018-03-21 20:32:32 +00:00
break;
}
}
void PreferencesDialog::changePage(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
if (!current)
current = previous;
int index = current->data(0, Qt::UserRole).toInt();
if (index)
ui->configPanel->setCurrentIndex(index - 1);
}
void PreferencesDialog::chooseThemeIcons()
{
// List of QActions which have alternative icons in different themes
const QList<QPair<QString, QString>> kCategoryIconsNames {
{ QStringLiteral("Debug"), QStringLiteral("bug.svg") },
{ QStringLiteral("Assembly"), QStringLiteral("disas.svg") },
{ QStringLiteral("Graph"), QStringLiteral("graph.svg") },
{ QStringLiteral("Appearance"), QStringLiteral("polar.svg") },
{ QStringLiteral("Plugins"), QStringLiteral("plugins.svg") },
{ QStringLiteral("Initialization Script"), QStringLiteral("initialization.svg") },
{ QStringLiteral("Analysis"), QStringLiteral("cog_light.svg") },
};
2021-01-24 14:50:13 +00:00
QList<QPair<void *, QString>> supportedIconsNames;
foreach (const auto &p, kCategoryIconsNames) {
2021-01-24 14:50:13 +00:00
QList<QTreeWidgetItem *> items =
ui->configCategories->findItems(p.first, Qt::MatchContains | Qt::MatchRecursive, 0);
if (items.isEmpty()) {
continue;
}
2021-01-24 14:50:13 +00:00
supportedIconsNames.append({ items.first(), p.second });
}
// Set the correct icon for the QAction
qhelpers::setThemeIcons(supportedIconsNames, [](void *obj, const QIcon &icon) {
2021-01-24 14:50:13 +00:00
// here we have our setter functor, in this case it is almost "unique" because it specified
// the column in `setIcon` call
static_cast<QTreeWidgetItem *>(obj)->setIcon(0, icon);
});
}