1 /**
2 @file 	gui_utilities.cpp
3 @author Lime Microsystems (www.limemicro.com)
4 @brief 	Implementation of common functions used by all panels
5 */
6 #include "lms7002_gui_utilities.h"
7 #include <wx/defs.h>
8 #include <wx/panel.h>
9 #include <wx/combobox.h>
10 #include <wx/checkbox.h>
11 #include <wx/radiobox.h>
12 #include <wx/stattext.h>
13 #include <wx/msgdlg.h>
14 #include "numericSlider.h"
15 #include <wx/spinctrl.h>
16 #include <wx/object.h>
17 #include <wx/tooltip.h>
18 #include <lms7_device.h>
19 
20 using namespace lime;
21 
UpdateControlsByMap(wxPanel * panel,lms_device_t * lmsControl,const std::map<wxWindow *,LMS7Parameter> & wndId2param)22 void LMS7002_WXGUI::UpdateControlsByMap(wxPanel* panel, lms_device_t* lmsControl, const std::map<wxWindow*, LMS7Parameter> &wndId2param)
23 {
24     if (panel == nullptr || lmsControl == nullptr)
25         return;
26     panel->Freeze();
27 
28     wxObject *wnd;
29     uint16_t value = 0;
30     wxClassInfo *wndClass;
31     wxClassInfo *cmbInfo = wxClassInfo::FindClass(_("wxComboBox"));
32     wxClassInfo *chkInfo = wxClassInfo::FindClass(_("wxCheckBox"));
33     wxClassInfo *rgrInfo = wxClassInfo::FindClass(_("wxRadioBox"));
34     wxClassInfo *numericSliderInfo = wxClassInfo::FindClass(_("NumericSlider"));
35     wxClassInfo *spinCtrlInfo = wxClassInfo::FindClass(_("wxSpinCtrl"));
36     wxClassInfo *labelInfo = wxClassInfo::FindClass(_("wxStaticText"));
37     wxClassInfo *radioBtnInfo = wxClassInfo::FindClass(_("wxRadioButton"));
38 
39     for (auto idParam : wndId2param)
40     {
41         wnd = idParam.first;
42         if (wnd == nullptr)
43             continue;
44         wndClass = wnd->GetClassInfo();
45 
46         LMS7_Device* lms = (LMS7_Device*)lmsControl;
47         value = lms->ReadParam(idParam.second, -1, true);
48         //cast window to specific control, to set value, or set selection
49         if (wndClass->IsKindOf(cmbInfo))
50         {
51             wxComboBox *box = wxStaticCast(wnd, wxComboBox);
52             if (box->GetCount() <= value)
53             {
54                 wxString str;
55                 str = wxString::Format(_("combobox value(%i) is out of range [0-%u]"), value, box->GetCount() - 1);
56                 //wxMessageBox(str, "WARNING!");
57                 value = 0;
58             }
59             box->SetSelection(value);
60         }
61         else if (wndClass->IsKindOf(chkInfo))
62         {
63             wxStaticCast(wnd, wxCheckBox)->SetValue(value);
64         }
65         else if (wndClass->IsKindOf(rgrInfo))
66         {
67             wxRadioBox *box = wxStaticCast(wnd, wxRadioBox);
68             if (box->GetCount() <= value)
69             {
70                 wxString str;
71                 str = wxString::Format(_("radiogroup value(%i) is out of range [0-%u]"), value, box->GetCount() - 1);
72                 //wxMessageBox(str, "WARNING!");
73                 continue;
74             }
75             box->SetSelection(value);
76         }
77         else if (wndClass->IsKindOf(labelInfo))
78         {
79             wxStaticCast(wnd, wxStaticText)->SetLabel(wxString::Format(_("%i"), value));
80         }
81         else if (wndClass->IsKindOf(numericSliderInfo))
82         {
83             wxStaticCast(wnd, NumericSlider)->SetValue(value);
84         }
85         else if (wndClass->IsKindOf(spinCtrlInfo))
86         {
87             wxStaticCast(wnd, wxSpinCtrl)->SetValue(value);
88         }
89         else if (wndClass->IsKindOf(radioBtnInfo))
90         {
91             wxStaticCast(wnd, wxRadioButton)->SetValue(value);
92         }
93         else
94         {
95             wxString str;
96 #ifndef NDEBUG
97             str = wxString::Format(_("Unhandled control class type. className=%s, was assigned address %04X"), wndClass->GetClassName(), idParam.second.address);
98             wxMessageBox(str, "ERROR!");
99 #endif
100         }
101     }
102     panel->Thaw();
103 }
104 
index2value(int index,const indexValueMap & pairs)105 int LMS7002_WXGUI::index2value(int index, const indexValueMap &pairs)
106 {
107     for (size_t i = 0; i < pairs.size(); ++i)
108         if (index == pairs[i].first)
109             return pairs[i].second;
110     return 0;
111 }
112 
value2index(int value,const indexValueMap & pairs)113 int LMS7002_WXGUI::value2index(int value, const indexValueMap &pairs)
114 {
115     for (size_t i = 0; i < pairs.size(); ++i)
116         if (value == pairs[i].second)
117             return pairs[i].first;
118     return 0;
119 }
120 
121 /** @brief Changes given wxWidget controls tooltips to parameter descriptions
122     @param wndId2Param wxWidgets controls and LMS parameters pairs
123     @param replace Replace all tooltips with new ones, or keep old ones and just add missing ones
124 */
UpdateTooltips(const std::map<wxWindow *,LMS7Parameter> & wndId2param,bool replace)125 void LMS7002_WXGUI::UpdateTooltips(const std::map<wxWindow*, LMS7Parameter> &wndId2param, bool replace)
126 {
127     wxString sttip = _("");
128     std::map<wxWindow*, LMS7Parameter>::const_iterator iter;
129     for (iter = wndId2param.begin(); iter != wndId2param.end(); ++iter)
130     {
131         wxToolTip *ttip = NULL;
132         ttip = iter->first->GetToolTip();
133         if (ttip)
134             sttip = ttip->GetTip();
135         else
136             sttip = _("");
137 
138         if (replace || sttip.length() == 0)
139             sttip = wxString::From8BitData(iter->second.tooltip);
140 
141         if (sttip.length() != 0)
142             sttip += _("\n");
143 
144         int bitCount = iter->second.msb - iter->second.lsb +1;
145         if (bitCount == 1)
146             sttip += wxString::Format(_("0x%.4X[%i]"), iter->second.address, iter->second.lsb);
147         else
148             sttip += wxString::Format(_("0x%.4X[%i:%i]"), iter->second.address, iter->second.msb, iter->second.lsb);
149         if(iter->first->IsKindOf(wxClassInfo::FindClass(_("NumericSlider")))) //set tooltip is not virtual method, need to cast
150             (reinterpret_cast<NumericSlider*>(iter->first))->SetToolTip(sttip + wxString::From8BitData(iter->second.name));
151         else
152             iter->first->SetToolTip(sttip + wxString::From8BitData(iter->second.name));
153     }
154 }
155