|
| 1 | +/* Path Option |
| 2 | + * |
| 3 | + * From: https://github.com/PokemonAutomation/ |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +#include <QHBoxLayout> |
| 8 | +#include <QLabel> |
| 9 | +#include <QLineEdit> |
| 10 | +#include <QPushButton> |
| 11 | +#include <QFileDialog> |
| 12 | +#include "PathWidget.h" |
| 13 | + |
| 14 | +namespace PokemonAutomation{ |
| 15 | + |
| 16 | + |
| 17 | +ConfigWidget* PathCell::make_QtWidget(QWidget& parent){ |
| 18 | + return new PathCellWidget(parent, *this); |
| 19 | +} |
| 20 | + |
| 21 | +ConfigWidget* PathOption::make_QtWidget(QWidget& parent){ |
| 22 | + return new PathOptionWidget(parent, *this); |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +PathCellWidget::~PathCellWidget(){ |
| 28 | + m_value.remove_listener(*this); |
| 29 | +} |
| 30 | +PathCellWidget::PathCellWidget(QWidget& parent, PathCell& value) |
| 31 | + : QWidget(&parent) |
| 32 | + , ConfigWidget(value, *this) |
| 33 | + , m_value(value) |
| 34 | +{ |
| 35 | + QHBoxLayout* layout = new QHBoxLayout(this); |
| 36 | + layout->setContentsMargins(0, 0, 0, 0); |
| 37 | + |
| 38 | + m_line_edit = new QLineEdit(QString::fromStdString(value), this); |
| 39 | + m_line_edit->setPlaceholderText(QString::fromStdString(value.placeholder_text())); |
| 40 | + m_line_edit->setReadOnly(value.lock_mode() == LockMode::READ_ONLY || m_value.is_locked()); |
| 41 | + layout->addWidget(m_line_edit, 1); |
| 42 | + |
| 43 | + m_browse_button = new QPushButton("Browse...", this); |
| 44 | + m_browse_button->setEnabled(value.lock_mode() != LockMode::READ_ONLY && !m_value.is_locked()); |
| 45 | + layout->addWidget(m_browse_button); |
| 46 | + |
| 47 | + connect( |
| 48 | + m_line_edit, &QLineEdit::editingFinished, |
| 49 | + this, [this](){ |
| 50 | + m_value.set(m_line_edit->text().toStdString()); |
| 51 | + } |
| 52 | + ); |
| 53 | + |
| 54 | + connect( |
| 55 | + m_browse_button, &QPushButton::clicked, |
| 56 | + this, [this](){ |
| 57 | + browse_file(); |
| 58 | + } |
| 59 | + ); |
| 60 | + |
| 61 | + m_value.add_listener(*this); |
| 62 | +} |
| 63 | + |
| 64 | +void PathCellWidget::browse_file(){ |
| 65 | + QString current_path = m_line_edit->text(); |
| 66 | + QString file_path = QFileDialog::getOpenFileName( |
| 67 | + this, |
| 68 | + "Select File", |
| 69 | + current_path, |
| 70 | + QString::fromStdString(m_value.filter_string()) |
| 71 | + ); |
| 72 | + |
| 73 | + if (!file_path.isEmpty()){ |
| 74 | + m_line_edit->setText(file_path); |
| 75 | + m_value.set(file_path.toStdString()); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +void PathCellWidget::update_value(){ |
| 80 | + m_line_edit->setText(QString::fromStdString(m_value)); |
| 81 | +} |
| 82 | +void PathCellWidget::on_config_value_changed(void* object){ |
| 83 | + QMetaObject::invokeMethod(this, [this]{ |
| 84 | + update_value(); |
| 85 | + }, Qt::QueuedConnection); |
| 86 | +} |
| 87 | +void PathCellWidget::on_config_visibility_changed(){ |
| 88 | + this->setEnabled(true); |
| 89 | + QMetaObject::invokeMethod(this, [this]{ |
| 90 | + const ConfigOptionState visibility = m_value.visibility(); |
| 91 | + m_line_edit->setReadOnly(visibility != ConfigOptionState::ENABLED || |
| 92 | + m_value.lock_mode() == LockMode::READ_ONLY || m_value.is_locked()); |
| 93 | + m_browse_button->setEnabled(visibility == ConfigOptionState::ENABLED && |
| 94 | + m_value.lock_mode() != LockMode::READ_ONLY && !m_value.is_locked()); |
| 95 | + switch (visibility){ |
| 96 | + case ConfigOptionState::ENABLED: |
| 97 | + case ConfigOptionState::DISABLED: |
| 98 | + this->setVisible(true); |
| 99 | + break; |
| 100 | + case ConfigOptionState::HIDDEN: |
| 101 | + this->setVisible(false); |
| 102 | + break; |
| 103 | + } |
| 104 | + }, Qt::QueuedConnection); |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +PathOptionWidget::~PathOptionWidget(){ |
| 111 | + m_value.remove_listener(*this); |
| 112 | +} |
| 113 | +PathOptionWidget::PathOptionWidget(QWidget& parent, PathOption& value) |
| 114 | + : QWidget(&parent) |
| 115 | + , ConfigWidget(value, *this) |
| 116 | + , m_value(value) |
| 117 | +{ |
| 118 | + QHBoxLayout* layout = new QHBoxLayout(this); |
| 119 | + layout->setContentsMargins(0, 0, 0, 0); |
| 120 | + QLabel* text = new QLabel(QString::fromStdString(value.label()), this); |
| 121 | + text->setWordWrap(true); |
| 122 | + layout->addWidget(text, 1); |
| 123 | + |
| 124 | + m_line_edit = new QLineEdit(QString::fromStdString(m_value), this); |
| 125 | + m_line_edit->setPlaceholderText(QString::fromStdString(value.placeholder_text())); |
| 126 | + m_line_edit->setReadOnly(value.lock_mode() == LockMode::READ_ONLY); |
| 127 | + layout->addWidget(m_line_edit, 1); |
| 128 | + |
| 129 | + m_browse_button = new QPushButton("Browse...", this); |
| 130 | + m_browse_button->setEnabled(value.lock_mode() != LockMode::READ_ONLY); |
| 131 | + layout->addWidget(m_browse_button); |
| 132 | + |
| 133 | + connect( |
| 134 | + m_line_edit, &QLineEdit::editingFinished, |
| 135 | + this, [this](){ |
| 136 | + m_value.set(m_line_edit->text().toStdString()); |
| 137 | + } |
| 138 | + ); |
| 139 | + |
| 140 | + connect( |
| 141 | + m_browse_button, &QPushButton::clicked, |
| 142 | + this, [this](){ |
| 143 | + browse_file(); |
| 144 | + } |
| 145 | + ); |
| 146 | + |
| 147 | + m_value.add_listener(*this); |
| 148 | +} |
| 149 | + |
| 150 | +void PathOptionWidget::browse_file(){ |
| 151 | + QString current_path = m_line_edit->text(); |
| 152 | + QString file_path = QFileDialog::getOpenFileName( |
| 153 | + this, |
| 154 | + "Select File", |
| 155 | + current_path, |
| 156 | + QString::fromStdString(m_value.filter_string()) |
| 157 | + ); |
| 158 | + |
| 159 | + if (!file_path.isEmpty()){ |
| 160 | + m_line_edit->setText(file_path); |
| 161 | + m_value.set(file_path.toStdString()); |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +void PathOptionWidget::update_value(){ |
| 166 | + m_line_edit->setText(QString::fromStdString(m_value)); |
| 167 | +} |
| 168 | +void PathOptionWidget::on_config_value_changed(void* object){ |
| 169 | + QMetaObject::invokeMethod(m_line_edit, [this]{ |
| 170 | + update_value(); |
| 171 | + }, Qt::QueuedConnection); |
| 172 | +} |
| 173 | +void PathOptionWidget::on_config_visibility_changed(){ |
| 174 | + this->setEnabled(true); |
| 175 | + QMetaObject::invokeMethod(this, [this]{ |
| 176 | + const ConfigOptionState visibility = m_value.visibility(); |
| 177 | + m_line_edit->setReadOnly(visibility != ConfigOptionState::ENABLED || |
| 178 | + m_value.lock_mode() == LockMode::READ_ONLY || m_value.is_locked()); |
| 179 | + m_browse_button->setEnabled(visibility == ConfigOptionState::ENABLED && |
| 180 | + m_value.lock_mode() != LockMode::READ_ONLY && !m_value.is_locked()); |
| 181 | + switch (visibility){ |
| 182 | + case ConfigOptionState::ENABLED: |
| 183 | + case ConfigOptionState::DISABLED: |
| 184 | + this->setVisible(true); |
| 185 | + break; |
| 186 | + case ConfigOptionState::HIDDEN: |
| 187 | + this->setVisible(false); |
| 188 | + break; |
| 189 | + } |
| 190 | + }, Qt::QueuedConnection); |
| 191 | +} |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | +} |
0 commit comments