Skip to content

Commit

Permalink
Merge pull request #285 from AdenKoperczak/trim_whitespace_from_inputs
Browse files Browse the repository at this point in the history
Trim white space from inputs
  • Loading branch information
dpaulat authored Oct 14, 2024
2 parents 411c570 + bf3454d commit 620a0db
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
13 changes: 12 additions & 1 deletion scwx-qt/source/scwx/qt/settings/settings_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class SettingsInterface<T>::Impl
double unitScale_ {1};
std::optional<std::string> unitAbbreviation_ {};
bool unitEnabled_ {false};

bool trimmingEnabled_ {false};
};

template<class T>
Expand Down Expand Up @@ -191,8 +193,11 @@ void SettingsInterface<T>::SetEditWidget(QWidget* widget)
p->context_.get(),
[this](const QString& text)
{
QString trimmedText =
p->trimmingEnabled_ ? text.trimmed() : text;

// Map to value if required
std::string value {text.toStdString()};
std::string value {trimmedText.toStdString()};
if (p->mapToValue_ != nullptr)
{
value = p->mapToValue_(value);
Expand Down Expand Up @@ -488,6 +493,12 @@ void SettingsInterface<T>::SetUnit(const double& scale,
p->UpdateUnitLabel();
}

template<class T>
void SettingsInterface<T>::EnableTrimming(bool trimmingEnabled)
{
p->trimmingEnabled_ = trimmingEnabled;
}

template<class T>
template<class U>
void SettingsInterface<T>::Impl::SetWidgetText(U* widget, const T& currentValue)
Expand Down
13 changes: 11 additions & 2 deletions scwx-qt/source/scwx/qt/settings/settings_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,20 @@ class SettingsInterface : public SettingsInterfaceBase
/**
* Sets the unit to be used by this setting.
*
* @param scale The radio of the current unit to the base unit
* @param abbreviation The abreviation to be displayed
* @param scale The ratio of the current unit to the base unit
* @param abbreviation The abbreviation to be displayed
*/
void SetUnit(const double& scale, const std::string& abbreviation);

/**
* Enables or disables whitespace trimming of text input.
* Removes whitespace ('\t', '\n', '\v', '\f', '\r', and ' ') at the
* beginning and end of the string from a QLineEdit.
*
* @param trimmingEnabled If trimming should be enabled.
*/
void EnableTrimming(bool trimmingEnabled = true);

private:
class Impl;
std::unique_ptr<Impl> p;
Expand Down
9 changes: 9 additions & 0 deletions scwx-qt/source/scwx/qt/ui/settings_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,14 +603,17 @@ void SettingsDialogImpl::SetupGeneralTab()
mapboxApiKey_.SetSettingsVariable(generalSettings.mapbox_api_key());
mapboxApiKey_.SetEditWidget(self_->ui->mapboxApiKeyLineEdit);
mapboxApiKey_.SetResetButton(self_->ui->resetMapboxApiKeyButton);
mapboxApiKey_.EnableTrimming();

mapTilerApiKey_.SetSettingsVariable(generalSettings.maptiler_api_key());
mapTilerApiKey_.SetEditWidget(self_->ui->mapTilerApiKeyLineEdit);
mapTilerApiKey_.SetResetButton(self_->ui->resetMapTilerApiKeyButton);
mapTilerApiKey_.EnableTrimming();

customStyleUrl_.SetSettingsVariable(generalSettings.custom_style_url());
customStyleUrl_.SetEditWidget(self_->ui->customMapUrlLineEdit);
customStyleUrl_.SetResetButton(self_->ui->resetCustomMapUrlButton);
customStyleUrl_.EnableTrimming();

customStyleDrawLayer_.SetSettingsVariable(
generalSettings.custom_style_draw_layer());
Expand Down Expand Up @@ -672,10 +675,12 @@ void SettingsDialogImpl::SetupGeneralTab()
nmeaSource_.SetSettingsVariable(generalSettings.nmea_source());
nmeaSource_.SetEditWidget(self_->ui->nmeaSourceLineEdit);
nmeaSource_.SetResetButton(self_->ui->resetNmeaSourceButton);
nmeaSource_.EnableTrimming();

warningsProvider_.SetSettingsVariable(generalSettings.warnings_provider());
warningsProvider_.SetEditWidget(self_->ui->warningsProviderLineEdit);
warningsProvider_.SetResetButton(self_->ui->resetWarningsProviderButton);
warningsProvider_.EnableTrimming();

antiAliasingEnabled_.SetSettingsVariable(
generalSettings.anti_aliasing_enabled());
Expand Down Expand Up @@ -750,6 +755,7 @@ void SettingsDialogImpl::SetupPalettesColorTablesTab()
colorTable.SetSettingsVariable(colorTableVariable);
colorTable.SetEditWidget(lineEdit);
colorTable.SetResetButton(resetButton);
colorTable.EnableTrimming();

colorTableVariable.RegisterValueStagedCallback(
[colorTableType, imageLabel](const std::string& value)
Expand Down Expand Up @@ -873,6 +879,7 @@ void SettingsDialogImpl::SetupAudioTab()
alertAudioSoundFile_.SetSettingsVariable(audioSettings.alert_sound_file());
alertAudioSoundFile_.SetEditWidget(self_->ui->alertAudioSoundLineEdit);
alertAudioSoundFile_.SetResetButton(self_->ui->resetAlertAudioSoundButton);
alertAudioSoundFile_.EnableTrimming();

QObject::connect(
self_->ui->alertAudioSoundSelectButton,
Expand Down Expand Up @@ -1085,6 +1092,7 @@ void SettingsDialogImpl::SetupAudioTab()
alertAudioCounty_.SetSettingsVariable(audioSettings.alert_county());
alertAudioCounty_.SetEditWidget(self_->ui->alertAudioCountyLineEdit);
alertAudioCounty_.SetResetButton(self_->ui->resetAlertAudioCountyButton);
alertAudioCounty_.EnableTrimming();

QObject::connect(self_->ui->alertAudioWFOSelectButton,
&QAbstractButton::clicked,
Expand Down Expand Up @@ -1117,6 +1125,7 @@ void SettingsDialogImpl::SetupAudioTab()
alertAudioWFO_.SetSettingsVariable(audioSettings.alert_wfo());
alertAudioWFO_.SetEditWidget(self_->ui->alertAudioWFOLineEdit);
alertAudioWFO_.SetResetButton(self_->ui->resetAlertAudioWFOButton);
alertAudioWFO_.EnableTrimming();
}

void SettingsDialogImpl::SetupTextTab()
Expand Down
3 changes: 2 additions & 1 deletion scwx-qt/source/scwx/qt/util/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ std::string NormalizeUrl(const std::string& urlString)
std::string normalizedUrl;

// Normalize URL string
QUrl url = QUrl::fromUserInput(QString::fromStdString(urlString));
QString trimmedUrlString = QString::fromStdString(urlString).trimmed();
QUrl url = QUrl::fromUserInput(trimmedUrlString);
if (url.isLocalFile())
{
normalizedUrl = QDir::toNativeSeparators(url.toLocalFile()).toStdString();
Expand Down

0 comments on commit 620a0db

Please sign in to comment.