Qt: Add help text section to settings dialog

This commit is contained in:
Connor McLaughlin 2020-03-22 13:16:56 +10:00
parent d5c76bf7d5
commit 4629cdfffc
3 changed files with 66 additions and 1 deletions

View file

@ -9,6 +9,8 @@
#include "qthostinterface.h" #include "qthostinterface.h"
#include <QtWidgets/QTextEdit> #include <QtWidgets/QTextEdit>
static constexpr char DEFAULT_SETTING_HELP_TEXT[] = "Mouse over an option for additional information.";
SettingsDialog::SettingsDialog(QtHostInterface* host_interface, QWidget* parent /* = nullptr */) SettingsDialog::SettingsDialog(QtHostInterface* host_interface, QWidget* parent /* = nullptr */)
: QDialog(parent), m_host_interface(host_interface) : QDialog(parent), m_host_interface(host_interface)
{ {
@ -33,6 +35,8 @@ SettingsDialog::SettingsDialog(QtHostInterface* host_interface, QWidget* parent
m_ui.settingsCategory->setCurrentRow(0); m_ui.settingsCategory->setCurrentRow(0);
m_ui.settingsContainer->setCurrentIndex(0); m_ui.settingsContainer->setCurrentIndex(0);
connect(m_ui.settingsCategory, &QListWidget::currentRowChanged, this, &SettingsDialog::onCategoryCurrentRowChanged); connect(m_ui.settingsCategory, &QListWidget::currentRowChanged, this, &SettingsDialog::onCategoryCurrentRowChanged);
m_ui.helpText->setText(tr(DEFAULT_SETTING_HELP_TEXT));
} }
SettingsDialog::~SettingsDialog() = default; SettingsDialog::~SettingsDialog() = default;
@ -49,3 +53,44 @@ void SettingsDialog::onCategoryCurrentRowChanged(int row)
{ {
m_ui.settingsContainer->setCurrentIndex(row); m_ui.settingsContainer->setCurrentIndex(row);
} }
void SettingsDialog::registerWidgetHelp(QObject* object, const char* title, const char* recommended_value,
const char* text)
{
// construct rich text with formatted description
QString full_text;
full_text += "<table width='100%' cellpadding='0' cellspacing='0'><tr><td><strong>";
full_text += tr(title);
full_text += "</strong></td><td align='right'><strong>";
full_text += tr("Recommended Value");
full_text += ": </strong>";
full_text += recommended_value;
full_text += "</td></table><hr>";
full_text += text;
m_widget_help_text_map[object] = std::move(full_text);
object->installEventFilter(this);
}
bool SettingsDialog::eventFilter(QObject* object, QEvent* event)
{
if (event->type() == QEvent::Enter)
{
auto iter = m_widget_help_text_map.constFind(object);
if (iter != m_widget_help_text_map.end())
{
m_current_help_widget = object;
m_ui.helpText->setText(iter.value());
}
}
else if (event->type() == QEvent::Leave)
{
if (m_current_help_widget)
{
m_current_help_widget = nullptr;
m_ui.helpText->setText(tr(DEFAULT_SETTING_HELP_TEXT));
}
}
return QDialog::eventFilter(object, event);
}

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "ui_settingsdialog.h" #include "ui_settingsdialog.h"
#include <QtCore/QMap>
#include <QtWidgets/QDialog> #include <QtWidgets/QDialog>
class QtHostInterface; class QtHostInterface;
@ -13,7 +14,7 @@ class PortSettingsWidget;
class GPUSettingsWidget; class GPUSettingsWidget;
class AudioSettingsWidget; class AudioSettingsWidget;
class SettingsDialog : public QDialog class SettingsDialog final : public QDialog
{ {
Q_OBJECT Q_OBJECT
@ -41,6 +42,9 @@ public:
GPUSettingsWidget* getGPUSettingsWidget() const { return m_gpu_settings; } GPUSettingsWidget* getGPUSettingsWidget() const { return m_gpu_settings; }
AudioSettingsWidget* getAudioSettingsWidget() const { return m_audio_settings; } AudioSettingsWidget* getAudioSettingsWidget() const { return m_audio_settings; }
void registerWidgetHelp(QObject* object, const char* title, const char* recommended_value, const char* text);
bool eventFilter(QObject* object, QEvent* event) override;
public Q_SLOTS: public Q_SLOTS:
void setCategory(Category category); void setCategory(Category category);
@ -59,4 +63,7 @@ private:
PortSettingsWidget* m_port_settings = nullptr; PortSettingsWidget* m_port_settings = nullptr;
GPUSettingsWidget* m_gpu_settings = nullptr; GPUSettingsWidget* m_gpu_settings = nullptr;
AudioSettingsWidget* m_audio_settings = nullptr; AudioSettingsWidget* m_audio_settings = nullptr;
QObject* m_current_help_widget = nullptr;
QMap<QObject*, QString> m_widget_help_text_map;
}; };

View file

@ -115,6 +115,19 @@
</widget> </widget>
</item> </item>
<item row="1" column="0" colspan="2"> <item row="1" column="0" colspan="2">
<widget class="QTextEdit" name="helpText">
<property name="readOnly">
<bool>true</bool>
</property>
<property name="minimumSize">
<size>
<width>16777215</width>
<height>80</height>
</size>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<widget class="QDialogButtonBox" name="buttonBox"> <widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>