| 
									
										
										
										
											2015-05-28 23:22:17 +00:00
										 |  |  | #include "LiveFrequencyValidator.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QLocale>
 | 
					
						
							|  |  |  | #include <QString>
 | 
					
						
							|  |  |  | #include <QComboBox>
 | 
					
						
							|  |  |  | #include <QLineEdit>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-07 17:49:45 +00:00
										 |  |  | #include "models/Bands.hpp"
 | 
					
						
							|  |  |  | #include "models/FrequencyList.hpp"
 | 
					
						
							| 
									
										
										
										
											2015-05-28 23:22:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "moc_LiveFrequencyValidator.cpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | LiveFrequencyValidator::LiveFrequencyValidator (QComboBox * combo_box | 
					
						
							|  |  |  |                                                 , Bands const * bands | 
					
						
							| 
									
										
										
										
											2022-10-11 18:30:01 -07:00
										 |  |  |                                                 , FrequencyList_v2_101 const * frequencies | 
					
						
							| 
									
										
										
										
											2016-04-06 17:11:58 +00:00
										 |  |  |                                                 , Frequency const * nominal_frequency | 
					
						
							| 
									
										
										
										
											2015-05-28 23:22:17 +00:00
										 |  |  |                                                 , QWidget * parent) | 
					
						
							|  |  |  |   : QRegExpValidator { | 
					
						
							|  |  |  |       QRegExp {       // frequency in MHz or band
 | 
					
						
							|  |  |  |         bands->data (QModelIndex {}).toString () // out of band string
 | 
					
						
							|  |  |  |           + QString {R"(|((\d{0,6}(\)"}    // or up to 6 digits
 | 
					
						
							|  |  |  |           + QLocale {}.decimalPoint () // (followed by decimal separator
 | 
					
						
							| 
									
										
										
										
											2016-04-06 17:11:58 +00:00
										 |  |  |           + R"(\d{0,2})?)([Mm]{1,2}|([Cc][Mm])))|(\d{0,6}(\)" // followed by up to 2 digits and either 'm' or 'cm' or 'mm' (case insensitive))
 | 
					
						
							| 
									
										
										
										
											2015-05-28 23:22:17 +00:00
										 |  |  |           + QLocale {}.decimalPoint () // or a decimal separator
 | 
					
						
							| 
									
										
										
										
											2016-04-06 17:11:58 +00:00
										 |  |  |           + R"(\d{0,6})?)|(\d{0,3}(\)"        //  followed by up to 6
 | 
					
						
							|  |  |  |                                               //  digits or a decimal number
 | 
					
						
							|  |  |  |           + QLocale {}.decimalPoint () // or a decimal separator
 | 
					
						
							|  |  |  |           + R"(\d{0,6})?[Kk]))"        // followed by a 'k' or 'K'
 | 
					
						
							| 
									
										
										
										
											2015-05-28 23:22:17 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |       , parent | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   , bands_ {bands} | 
					
						
							|  |  |  |   , frequencies_ {frequencies} | 
					
						
							| 
									
										
										
										
											2016-04-06 17:11:58 +00:00
										 |  |  |   , nominal_frequency_ {nominal_frequency} | 
					
						
							| 
									
										
										
										
											2015-05-28 23:22:17 +00:00
										 |  |  |   , combo_box_ {combo_box} | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | auto LiveFrequencyValidator::validate (QString& input, int& pos) const -> State | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   auto state = QRegExpValidator::validate (input, pos); | 
					
						
							|  |  |  |   // by never being Acceptable we force fixup calls on ENTER or
 | 
					
						
							|  |  |  |   // losing focus
 | 
					
						
							|  |  |  |   return Acceptable == state ? Intermediate : state; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void LiveFrequencyValidator::fixup (QString& input) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   QRegExpValidator::fixup (input); | 
					
						
							| 
									
										
										
										
											2015-05-31 11:51:31 +00:00
										 |  |  |   if (!bands_->oob ().startsWith (input)) | 
					
						
							| 
									
										
										
										
											2015-05-28 23:22:17 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |       if (input.contains ('m', Qt::CaseInsensitive)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           input = input.toLower (); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           QVector<QVariant> frequencies; | 
					
						
							| 
									
										
										
										
											2020-09-01 17:32:22 +01:00
										 |  |  |           for (auto const& item : *frequencies_) | 
					
						
							| 
									
										
										
										
											2015-05-28 23:22:17 +00:00
										 |  |  |             { | 
					
						
							| 
									
										
										
										
											2015-05-31 11:51:31 +00:00
										 |  |  |               if (bands_->find (item.frequency_) == input) | 
					
						
							| 
									
										
										
										
											2015-05-28 23:22:17 +00:00
										 |  |  |                 { | 
					
						
							|  |  |  |                   frequencies << item.frequency_; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           if (!frequencies.isEmpty ()) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |               Q_EMIT valid (frequencies.first ().value<Frequency> ()); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |               input = QString {}; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-04-06 17:11:58 +00:00
										 |  |  |       else if (input.contains (QChar {'k'}, Qt::CaseInsensitive)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           // kHz in current MHz input
 | 
					
						
							|  |  |  |           auto f = Radio::frequency (input.remove (QChar {'k'}, Qt::CaseInsensitive), 3); | 
					
						
							|  |  |  |           f += *nominal_frequency_ / 1000000u * 1000000u; | 
					
						
							|  |  |  |           input = bands_->find (f); | 
					
						
							|  |  |  |           Q_EMIT valid (f); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-05-28 23:22:17 +00:00
										 |  |  |       else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           // frequency input
 | 
					
						
							|  |  |  |           auto f = Radio::frequency (input, 6); | 
					
						
							| 
									
										
										
										
											2015-05-31 11:51:31 +00:00
										 |  |  |           input = bands_->find (f); | 
					
						
							| 
									
										
										
										
											2015-05-28 23:22:17 +00:00
										 |  |  |           Q_EMIT valid (f); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-31 11:51:31 +00:00
										 |  |  |       if (bands_->oob () == input) | 
					
						
							| 
									
										
										
										
											2015-05-28 23:22:17 +00:00
										 |  |  |         { | 
					
						
							|  |  |  |           combo_box_->lineEdit ()->setStyleSheet ("QLineEdit {color: yellow; background-color : red;}"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           combo_box_->lineEdit ()->setStyleSheet ({}); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       combo_box_->setCurrentText (input); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |