mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 04:30:05 +00:00 
			
		
		
		
	Support editing integer arrays in settings interface (font sizes)
This commit is contained in:
		
							parent
							
								
									b2dbcfefb0
								
							
						
					
					
						commit
						9b5b841903
					
				
					 4 changed files with 110 additions and 23 deletions
				
			
		|  | @ -3,6 +3,8 @@ | |||
| #include <scwx/qt/settings/settings_interface.hpp> | ||||
| #include <scwx/qt/settings/settings_variable.hpp> | ||||
| 
 | ||||
| #include <boost/tokenizer.hpp> | ||||
| #include <fmt/ranges.h> | ||||
| #include <QAbstractButton> | ||||
| #include <QCheckBox> | ||||
| #include <QComboBox> | ||||
|  | @ -41,8 +43,8 @@ public: | |||
|    QWidget*                 editWidget_ {nullptr}; | ||||
|    QAbstractButton*         resetButton_ {nullptr}; | ||||
| 
 | ||||
|    std::function<T(const T&)> mapFromValue_ {nullptr}; | ||||
|    std::function<T(const T&)> mapToValue_ {nullptr}; | ||||
|    std::function<std::string(const T&)> mapFromValue_ {nullptr}; | ||||
|    std::function<T(const std::string&)> mapToValue_ {nullptr}; | ||||
| }; | ||||
| 
 | ||||
| template<class T> | ||||
|  | @ -96,6 +98,52 @@ void SettingsInterface<T>::SetEditWidget(QWidget* widget) | |||
|                              p->stagedValid_ = p->variable_->StageValue(value); | ||||
|                              p->UpdateResetButton(); | ||||
| 
 | ||||
|                              // TODO: Display invalid status
 | ||||
|                           }); | ||||
|       } | ||||
|       else if constexpr (std::is_same_v<T, std::vector<std::int64_t>>) | ||||
|       { | ||||
|          // If the line is edited (not programatically changed), stage the new
 | ||||
|          // value
 | ||||
|          QObject::connect( | ||||
|             lineEdit, | ||||
|             &QLineEdit::textEdited, | ||||
|             p->context_.get(), | ||||
|             [this](const QString& text) | ||||
|             { | ||||
|                // Map to value if required
 | ||||
|                T value {}; | ||||
|                if (p->mapToValue_ != nullptr) | ||||
|                { | ||||
|                   // User-defined map to value
 | ||||
|                   value = p->mapToValue_(text.toStdString()); | ||||
|                } | ||||
|                else | ||||
|                { | ||||
|                   // Tokenize string to parse each element
 | ||||
|                   const std::string str {text.toStdString()}; | ||||
|                   boost::tokenizer  tokens(str); | ||||
|                   for (auto it = tokens.begin(); it != tokens.end(); ++it) | ||||
|                   { | ||||
|                      try | ||||
|                      { | ||||
|                         // Good value
 | ||||
|                         value.push_back( | ||||
|                            static_cast<T::value_type>(std::stoll(*it))); | ||||
|                      } | ||||
|                      catch (const std::exception&) | ||||
|                      { | ||||
|                         // Error value
 | ||||
|                         value.push_back( | ||||
|                            std::numeric_limits<T::value_type>::min()); | ||||
|                      } | ||||
|                   } | ||||
|                } | ||||
| 
 | ||||
|                // Attempt to stage the value
 | ||||
|                p->stagedValid_ = p->variable_->StageValue(value); | ||||
|                p->UpdateResetButton(); | ||||
| 
 | ||||
|                // TODO: Display invalid status
 | ||||
|             }); | ||||
|       } | ||||
|  | @ -228,14 +276,14 @@ void SettingsInterface<T>::SetResetButton(QAbstractButton* button) | |||
| 
 | ||||
| template<class T> | ||||
| void SettingsInterface<T>::SetMapFromValueFunction( | ||||
|    std::function<T(const T&)> function) | ||||
|    std::function<std::string(const T&)> function) | ||||
| { | ||||
|    p->mapFromValue_ = function; | ||||
| } | ||||
| 
 | ||||
| template<class T> | ||||
| void SettingsInterface<T>::SetMapToValueFunction( | ||||
|    std::function<T(const T&)> function) | ||||
|    std::function<T(const std::string&)> function) | ||||
| { | ||||
|    p->mapToValue_ = function; | ||||
| } | ||||
|  | @ -266,6 +314,19 @@ void SettingsInterface<T>::Impl::UpdateEditWidget() | |||
|             lineEdit->setText(QString::fromStdString(currentValue)); | ||||
|          } | ||||
|       } | ||||
|       else if constexpr (std::is_same_v<T, std::vector<std::int64_t>>) | ||||
|       { | ||||
|          if (mapFromValue_ != nullptr) | ||||
|          { | ||||
|             lineEdit->setText( | ||||
|                QString::fromStdString(mapFromValue_(currentValue))); | ||||
|          } | ||||
|          else | ||||
|          { | ||||
|             lineEdit->setText(QString::fromStdString( | ||||
|                fmt::format("{}", fmt::join(currentValue, ", ")))); | ||||
|          } | ||||
|       } | ||||
|    } | ||||
|    else if (QCheckBox* checkBox = dynamic_cast<QCheckBox*>(editWidget_)) | ||||
|    { | ||||
|  |  | |||
|  | @ -60,7 +60,7 @@ public: | |||
|     * | ||||
|     * @param function Map from settings value function | ||||
|     */ | ||||
|    void SetMapFromValueFunction(std::function<T(const T&)> function); | ||||
|    void SetMapFromValueFunction(std::function<std::string(const T&)> function); | ||||
| 
 | ||||
|    /**
 | ||||
|     * If the edit widget displays a different value than what is stored in the | ||||
|  | @ -69,7 +69,7 @@ public: | |||
|     * | ||||
|     * @param function Map to settings value function | ||||
|     */ | ||||
|    void SetMapToValueFunction(std::function<T(const T&)> function); | ||||
|    void SetMapToValueFunction(std::function<T(const std::string&)> function); | ||||
| 
 | ||||
| private: | ||||
|    class Impl; | ||||
|  |  | |||
|  | @ -56,8 +56,9 @@ public: | |||
|    SettingsDialog* self_; | ||||
| 
 | ||||
|    settings::SettingsInterface<std::string>               defaultRadarSite_ {}; | ||||
|    settings::SettingsInterface<int64_t>     gridWidth_ {}; | ||||
|    settings::SettingsInterface<int64_t>     gridHeight_ {}; | ||||
|    settings::SettingsInterface<std::vector<std::int64_t>> fontSizes_ {}; | ||||
|    settings::SettingsInterface<std::int64_t>              gridWidth_ {}; | ||||
|    settings::SettingsInterface<std::int64_t>              gridHeight_ {}; | ||||
|    settings::SettingsInterface<std::string>               mapboxApiKey_ {}; | ||||
|    settings::SettingsInterface<bool>                      debugEnabled_ {}; | ||||
| 
 | ||||
|  | @ -150,6 +151,10 @@ void SettingsDialogImpl::SetupGeneralTab() | |||
|    defaultRadarSite_.SetEditWidget(self_->ui->radarSiteComboBox); | ||||
|    defaultRadarSite_.SetResetButton(self_->ui->resetRadarSiteButton); | ||||
| 
 | ||||
|    fontSizes_.SetSettingsVariable(generalSettings.font_sizes()); | ||||
|    fontSizes_.SetEditWidget(self_->ui->fontSizesLineEdit); | ||||
|    fontSizes_.SetResetButton(self_->ui->resetFontSizesButton); | ||||
| 
 | ||||
|    gridWidth_.SetSettingsVariable(generalSettings.grid_width()); | ||||
|    gridWidth_.SetEditWidget(self_->ui->gridWidthSpinBox); | ||||
|    gridWidth_.SetResetButton(self_->ui->resetGridWidthButton); | ||||
|  |  | |||
|  | @ -116,17 +116,17 @@ | |||
|                </property> | ||||
|               </widget> | ||||
|              </item> | ||||
|              <item row="2" column="0"> | ||||
|              <item row="3" column="0"> | ||||
|               <widget class="QLabel" name="label_3"> | ||||
|                <property name="text"> | ||||
|                 <string>Grid Height</string> | ||||
|                </property> | ||||
|               </widget> | ||||
|              </item> | ||||
|              <item row="2" column="1"> | ||||
|              <item row="3" column="2"> | ||||
|               <widget class="QSpinBox" name="gridHeightSpinBox"/> | ||||
|              </item> | ||||
|              <item row="1" column="2"> | ||||
|              <item row="2" column="3"> | ||||
|               <widget class="QToolButton" name="resetGridWidthButton"> | ||||
|                <property name="text"> | ||||
|                 <string>...</string> | ||||
|  | @ -137,10 +137,7 @@ | |||
|                </property> | ||||
|               </widget> | ||||
|              </item> | ||||
|              <item row="3" column="1"> | ||||
|               <widget class="QLineEdit" name="mapboxApiKeyLineEdit"/> | ||||
|              </item> | ||||
|              <item row="3" column="2"> | ||||
|              <item row="4" column="3"> | ||||
|               <widget class="QToolButton" name="resetMapboxApiKeyButton"> | ||||
|                <property name="text"> | ||||
|                 <string>...</string> | ||||
|  | @ -151,7 +148,7 @@ | |||
|                </property> | ||||
|               </widget> | ||||
|              </item> | ||||
|              <item row="0" column="2"> | ||||
|              <item row="0" column="3"> | ||||
|               <widget class="QToolButton" name="resetRadarSiteButton"> | ||||
|                <property name="text"> | ||||
|                 <string>...</string> | ||||
|  | @ -162,13 +159,13 @@ | |||
|                </property> | ||||
|               </widget> | ||||
|              </item> | ||||
|              <item row="1" column="1"> | ||||
|              <item row="2" column="2"> | ||||
|               <widget class="QSpinBox" name="gridWidthSpinBox"/> | ||||
|              </item> | ||||
|              <item row="0" column="1"> | ||||
|              <item row="0" column="2"> | ||||
|               <widget class="QComboBox" name="radarSiteComboBox"/> | ||||
|              </item> | ||||
|              <item row="2" column="2"> | ||||
|              <item row="3" column="3"> | ||||
|               <widget class="QToolButton" name="resetGridHeightButton"> | ||||
|                <property name="text"> | ||||
|                 <string>...</string> | ||||
|  | @ -179,20 +176,44 @@ | |||
|                </property> | ||||
|               </widget> | ||||
|              </item> | ||||
|              <item row="3" column="0"> | ||||
|              <item row="4" column="0"> | ||||
|               <widget class="QLabel" name="label_4"> | ||||
|                <property name="text"> | ||||
|                 <string>Mapbox API Key</string> | ||||
|                </property> | ||||
|               </widget> | ||||
|              </item> | ||||
|              <item row="4" column="2"> | ||||
|               <widget class="QLineEdit" name="mapboxApiKeyLineEdit"/> | ||||
|              </item> | ||||
|              <item row="1" column="0"> | ||||
|               <widget class="QLabel" name="label_5"> | ||||
|                <property name="text"> | ||||
|                 <string>Font Sizes</string> | ||||
|                </property> | ||||
|               </widget> | ||||
|              </item> | ||||
|              <item row="2" column="0"> | ||||
|               <widget class="QLabel" name="label_2"> | ||||
|                <property name="text"> | ||||
|                 <string>Grid Width</string> | ||||
|                </property> | ||||
|               </widget> | ||||
|              </item> | ||||
|              <item row="1" column="2"> | ||||
|               <widget class="QLineEdit" name="fontSizesLineEdit"/> | ||||
|              </item> | ||||
|              <item row="1" column="3"> | ||||
|               <widget class="QToolButton" name="resetFontSizesButton"> | ||||
|                <property name="text"> | ||||
|                 <string>...</string> | ||||
|                </property> | ||||
|                <property name="icon"> | ||||
|                 <iconset resource="../../../../scwx-qt.qrc"> | ||||
|                  <normaloff>:/res/icons/font-awesome-6/rotate-left-solid.svg</normaloff>:/res/icons/font-awesome-6/rotate-left-solid.svg</iconset> | ||||
|                </property> | ||||
|               </widget> | ||||
|              </item> | ||||
|             </layout> | ||||
|            </widget> | ||||
|           </item> | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat