2015-05-04 19:44:03 -04:00
|
|
|
#include "FrequencyDialog.h"
|
|
|
|
|
2015-05-09 23:13:35 -04:00
|
|
|
#include "wx/clipbrd.h"
|
2015-05-10 01:07:48 -04:00
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include "CubicSDR.h"
|
2015-05-09 23:13:35 -04:00
|
|
|
|
2015-06-07 21:42:23 -04:00
|
|
|
wxBEGIN_EVENT_TABLE(FrequencyDialog, wxDialog)
|
|
|
|
EVT_CHAR_HOOK(FrequencyDialog::OnChar)
|
2015-05-09 23:13:35 -04:00
|
|
|
wxEND_EVENT_TABLE()
|
2015-05-04 19:44:03 -04:00
|
|
|
|
2015-05-10 20:00:48 -04:00
|
|
|
FrequencyDialog::FrequencyDialog(wxWindow * parent, wxWindowID id, const wxString & title, DemodulatorInstance *demod, const wxPoint & position,
|
|
|
|
const wxSize & size, long style) :
|
|
|
|
wxDialog(parent, id, title, position, size, style) {
|
2015-05-10 01:39:10 -04:00
|
|
|
wxString freqStr;
|
|
|
|
activeDemod = demod;
|
|
|
|
|
|
|
|
if (activeDemod) {
|
|
|
|
freqStr = frequencyToStr(activeDemod->getFrequency());
|
|
|
|
} else {
|
|
|
|
freqStr = frequencyToStr(wxGetApp().getFrequency());
|
|
|
|
}
|
2015-05-04 19:44:03 -04:00
|
|
|
|
2015-05-10 20:00:48 -04:00
|
|
|
dialogText = new wxTextCtrl(this, wxID_FREQ_INPUT, freqStr, wxPoint(6, 1), wxSize(size.GetWidth() - 20, size.GetHeight() - 70),
|
|
|
|
wxTE_PROCESS_ENTER);
|
2015-05-04 19:44:03 -04:00
|
|
|
dialogText->SetFont(wxFont(20, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD));
|
|
|
|
|
|
|
|
Centre();
|
2015-05-04 23:03:55 -04:00
|
|
|
|
|
|
|
dialogText->SetSelection(-1, -1);
|
2015-05-04 19:44:03 -04:00
|
|
|
}
|
|
|
|
|
2015-05-09 23:13:35 -04:00
|
|
|
std::string& FrequencyDialog::filterChars(std::string& s, const std::string& allowed) {
|
|
|
|
s.erase(remove_if(s.begin(), s.end(), [&allowed](const char& c) {
|
|
|
|
return allowed.find(c) == std::string::npos;
|
|
|
|
}), s.end());
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-05-10 01:07:48 -04:00
|
|
|
std::string FrequencyDialog::frequencyToStr(long long freq) {
|
|
|
|
long double freqTemp;
|
|
|
|
|
|
|
|
freqTemp = freq;
|
|
|
|
std::string suffix("");
|
|
|
|
std::stringstream freqStr;
|
|
|
|
|
|
|
|
if (freqTemp >= 1.0e9) {
|
|
|
|
freqTemp /= 1.0e9;
|
|
|
|
freqStr << std::setprecision(10);
|
|
|
|
suffix = std::string("GHz");
|
|
|
|
} else if (freqTemp >= 1.0e6) {
|
|
|
|
freqTemp /= 1.0e6;
|
|
|
|
freqStr << std::setprecision(7);
|
|
|
|
suffix = std::string("MHz");
|
|
|
|
} else if (freqTemp >= 1.0e3) {
|
|
|
|
freqTemp /= 1.0e3;
|
|
|
|
freqStr << std::setprecision(4);
|
|
|
|
suffix = std::string("KHz");
|
|
|
|
}
|
|
|
|
|
|
|
|
freqStr << freqTemp;
|
|
|
|
freqStr << suffix;
|
|
|
|
|
|
|
|
return freqStr.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
long long FrequencyDialog::strToFrequency(std::string freqStr) {
|
2015-07-06 23:15:18 -04:00
|
|
|
std::string filterStr = filterChars(freqStr, std::string("0123456789.MKGHmkgh"));
|
2015-05-10 01:07:48 -04:00
|
|
|
|
|
|
|
int numLen = filterStr.find_first_not_of("0123456789.");
|
|
|
|
|
|
|
|
if (numLen == std::string::npos) {
|
|
|
|
numLen = freqStr.length();
|
|
|
|
}
|
|
|
|
|
2015-05-10 01:39:10 -04:00
|
|
|
std::string numPartStr = freqStr.substr(0, numLen);
|
2015-05-10 01:07:48 -04:00
|
|
|
std::string suffixStr = freqStr.substr(numLen);
|
|
|
|
|
|
|
|
std::stringstream numPartStream;
|
|
|
|
numPartStream.str(numPartStr);
|
|
|
|
|
|
|
|
long double freqTemp = 0;
|
|
|
|
|
|
|
|
numPartStream >> freqTemp;
|
|
|
|
|
|
|
|
if (suffixStr.length()) {
|
|
|
|
if (suffixStr.find_first_of("Gg") != std::string::npos) {
|
|
|
|
freqTemp *= 1.0e9;
|
|
|
|
} else if (suffixStr.find_first_of("Mm") != std::string::npos) {
|
|
|
|
freqTemp *= 1.0e6;
|
|
|
|
} else if (suffixStr.find_first_of("Kk") != std::string::npos) {
|
|
|
|
freqTemp *= 1.0e3;
|
2015-07-06 23:15:18 -04:00
|
|
|
} else if (suffixStr.find_first_of("Hh") != std::string::npos) {
|
|
|
|
// ...
|
2015-05-10 01:07:48 -04:00
|
|
|
}
|
2015-07-06 23:15:18 -04:00
|
|
|
} else if (numPartStr.find_first_of(".") != std::string::npos || freqTemp <= 3000) {
|
2015-05-10 01:07:48 -04:00
|
|
|
freqTemp *= 1.0e6;
|
|
|
|
}
|
|
|
|
|
2015-05-10 01:39:10 -04:00
|
|
|
return (long long) freqTemp;
|
2015-05-10 01:07:48 -04:00
|
|
|
}
|
|
|
|
|
2015-05-09 23:13:35 -04:00
|
|
|
void FrequencyDialog::OnChar(wxKeyEvent& event) {
|
2015-06-07 21:42:23 -04:00
|
|
|
int c = event.GetKeyCode();
|
2015-05-10 01:07:48 -04:00
|
|
|
long long freq;
|
2015-05-09 23:13:35 -04:00
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case WXK_RETURN:
|
|
|
|
case WXK_NUMPAD_ENTER:
|
|
|
|
// Do Stuff
|
2015-05-10 01:07:48 -04:00
|
|
|
freq = strToFrequency(dialogText->GetValue().ToStdString());
|
2015-05-10 01:39:10 -04:00
|
|
|
if (activeDemod) {
|
|
|
|
activeDemod->setTracking(true);
|
|
|
|
activeDemod->setFollow(true);
|
|
|
|
activeDemod->setFrequency(freq);
|
|
|
|
activeDemod->updateLabel(freq);
|
|
|
|
} else {
|
|
|
|
wxGetApp().setFrequency(freq);
|
|
|
|
}
|
2015-05-09 23:13:35 -04:00
|
|
|
Close();
|
|
|
|
break;
|
|
|
|
case WXK_ESCAPE:
|
|
|
|
Close();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string allowed("0123456789.MKGHZmkghz");
|
|
|
|
|
2015-05-10 20:00:48 -04:00
|
|
|
if (allowed.find_first_of(c) != std::string::npos || c == WXK_DELETE || c == WXK_BACK || c == WXK_NUMPAD_DECIMAL
|
|
|
|
|| (c >= WXK_NUMPAD0 && c <= WXK_NUMPAD9)) {
|
2015-06-07 21:42:23 -04:00
|
|
|
#ifdef __linux__
|
|
|
|
dialogText->OnChar(event);
|
|
|
|
event.Skip();
|
|
|
|
#else
|
2015-05-09 23:13:35 -04:00
|
|
|
event.DoAllowNextEvent();
|
2015-06-07 21:42:23 -04:00
|
|
|
#endif
|
2015-05-09 23:13:35 -04:00
|
|
|
} else if (event.ControlDown() && c == 'V') {
|
|
|
|
// Alter clipboard contents to remove unwanted chars
|
|
|
|
wxTheClipboard->Open();
|
|
|
|
wxTextDataObject data;
|
|
|
|
wxTheClipboard->GetData(data);
|
|
|
|
std::string clipText = data.GetText().ToStdString();
|
2015-05-10 01:07:48 -04:00
|
|
|
std::string pasteText = filterChars(clipText, std::string(allowed));
|
2015-05-09 23:13:35 -04:00
|
|
|
wxTheClipboard->SetData(new wxTextDataObject(pasteText));
|
|
|
|
wxTheClipboard->Close();
|
|
|
|
event.Skip();
|
|
|
|
} else if (c == WXK_RIGHT || c == WXK_LEFT || event.ControlDown()) {
|
|
|
|
event.Skip();
|
|
|
|
}
|
2015-05-04 19:44:03 -04:00
|
|
|
}
|