OpenCPN Partial API docs
Loading...
Searching...
No Matches
depth.cpp
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2010 by Jean-Eudes Onfray *
3 * Copyright (C) 2010 by David S. Register *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
17 **************************************************************************/
18
25#include <wx/wxprec.h>
26
27#ifndef WX_PRECOMP
28#include <wx/wx.h>
29#endif
30
31#include "depth.h"
32
33extern int g_iDashDepthUnit;
34
35DashboardInstrument_Depth::DashboardInstrument_Depth(
36 wxWindow* parent, wxWindowID id, wxString title,
37 InstrumentProperties* Properties)
38 : DashboardInstrument(parent, id, title, OCPN_DBP_STC_DPT, Properties) {
39 m_cap_flag.set(OCPN_DBP_STC_TMP);
40 m_max_depth = 0;
41 m_depth = 0;
42 m_depth_unit = getUsrDistanceUnit_Plugin(g_iDashDepthUnit);
43 m_temp = "--";
44 for (int idx = 0; idx < DEPTH_RECORD_COUNT; idx++) {
45 m_array_depth[idx] = 0;
46 }
47}
48
49wxSize DashboardInstrument_Depth::GetSize(int orient, wxSize hint) {
50 InitTitleSize();
51 int w;
52 InitDataTextHeight("15.7 Feet", w);
53
54 wxClientDC dc(this);
55 wxFont f;
56 if (m_Properties) {
57 // Space for bottom(temp)text later.
58 f = m_Properties->m_LabelFont.GetChosenFont();
59 dc.GetTextExtent("20.8 C", &w_label, &h_label, nullptr, nullptr, &f);
60 } else {
61 // Space for bottom(temp)text later.
62 f = g_pFontLabel->GetChosenFont();
63 dc.GetTextExtent("20.8 C", &w_label, &h_label, nullptr, nullptr, &f);
64 }
65
66 // Depth data plot area w-temp
67 int drawHeight =
68 static_cast<int>(m_DataTextHeight + 4 * m_DataTextHeight + h_label +
69 m_DataTextHeight * g_TitleVerticalOffset);
70 InitTitleAndDataPosition(drawHeight);
71 int y_total = GetFullHeight(drawHeight);
72
73 if (orient == wxHORIZONTAL) {
74 return wxSize(wxMax(w + m_DataMargin, DefaultWidth),
75 wxMax(y_total, hint.y));
76 } else {
77 return wxSize(wxMax(hint.x, wxMax(w + m_DataMargin, DefaultWidth)),
78 y_total);
79 }
80}
81
82void DashboardInstrument_Depth::SetData(DASH_CAP st, double data,
83 wxString unit) {
84 if (st == OCPN_DBP_STC_DPT) {
85 m_depth = std::isnan(data) ? 0.0 : data;
86
87 for (int idx = 1; idx < DEPTH_RECORD_COUNT; idx++) {
88 m_array_depth[idx - 1] = m_array_depth[idx];
89 }
90 m_array_depth[DEPTH_RECORD_COUNT - 1] = m_depth;
91 m_depth_unit = unit;
92 } else if (st == OCPN_DBP_STC_TMP) {
93 if (!std::isnan(data)) {
94 m_temp = wxString::Format("%.1f", data) + DEGREE_SIGN + unit;
95 } else {
96 m_temp = "---";
97 }
98 }
99}
100
101void DashboardInstrument_Depth::Draw(wxGCDC* dc) {
102 DrawBackground(dc);
103 DrawForeground(dc);
104}
105
106void DashboardInstrument_Depth::DrawBackground(wxGCDC* dc) {
107 wxSize size = GetClientSize();
108 wxColour cl;
109 if (m_Properties) {
110 dc->SetTextForeground(
111 GetColourSchemeFont(m_Properties->m_LabelFont.GetColour()));
112 } else {
113 if (GetColourSchemeFont(g_pFontSmall->GetColour()) ==
114 GetColourSchemeFont(g_pFontLabel->GetColour())) {
115 GetGlobalColor("DASHL", &cl);
116 dc->SetTextForeground(cl);
117 } else
118 dc->SetTextForeground(GetColourSchemeFont(g_pFontLabel->GetColour()));
119 }
120 wxPen pen;
121 pen.SetStyle(wxPENSTYLE_SOLID);
122 if (m_Properties) {
123 cl = GetColourSchemeFont(m_Properties->m_SmallFont.GetColour());
124 } else {
125 // GetGlobalColor("DASHF", &cl);
126 cl = GetColourSchemeFont(g_pFontSmall->GetColour());
127 }
128 pen.SetColour(cl);
129 pen.SetWidth(1);
130 dc->SetPen(pen);
131
132 int drawHeight = GetDataBottom(size.y) - m_DataTop;
133 InitTitleAndDataPosition(drawHeight);
134
135 m_plotup = m_DataTop + m_DataTextHeight;
136 m_plotdown = m_DataTop + drawHeight - h_label;
137 m_plotheight = m_plotdown - m_plotup;
138
139 dc->DrawLine(3, m_plotup, size.x - 3, m_plotup);
140 dc->DrawLine(3, m_plotdown, size.x - 3, m_plotdown);
141
142#ifdef __WXMSW__
143 pen.SetStyle(wxPENSTYLE_SHORT_DASH);
144#else
145 pen.SetStyle(wxPENSTYLE_DOT);
146 pen.SetWidth(1);
147#endif
148
149 dc->SetPen(pen);
150 dc->DrawLine(3, m_plotup + m_plotheight / 4, size.x - 3,
151 m_plotup + m_plotheight / 4);
152 dc->DrawLine(3, m_plotup + m_plotheight * 2 / 4, size.x - 3,
153 m_plotup + m_plotheight * 2 / 4);
154 dc->DrawLine(3, m_plotup + m_plotheight * 3 / 4, size.x - 3,
155 m_plotup + m_plotheight * 3 / 4);
156 if (m_Properties) {
157 dc->SetFont(m_Properties->m_SmallFont.GetChosenFont());
158 dc->SetTextForeground(
159 GetColourSchemeFont(m_Properties->m_SmallFont.GetColour()));
160 } else {
161 dc->SetFont(g_pFontSmall->GetChosenFont());
162 dc->SetTextForeground(GetColourSchemeFont(g_pFontSmall->GetColour()));
163 }
164 m_max_depth = 0;
165 for (int idx = 0; idx < DEPTH_RECORD_COUNT; idx++) {
166 if (m_array_depth[idx] > m_max_depth) m_max_depth = m_array_depth[idx];
167 }
168 // Increase MaxDepth slightly for nicer display
169 m_max_depth *= 1.2;
170
171 wxString label;
172 label.Printf("%.0f " + m_depth_unit, 0.0);
173 int width, height;
174 wxFont f;
175 if (m_Properties)
176 f = m_Properties->m_SmallFont.GetChosenFont();
177 else
178 f = g_pFontSmall->GetChosenFont();
179 dc->GetTextExtent(label, &width, &height, nullptr, nullptr, &f);
180 dc->DrawText(label, size.x - width - 1, m_plotup - height);
181
182 label.Printf("%.0f " + m_depth_unit, m_max_depth);
183 if (m_Properties)
184 f = m_Properties->m_SmallFont.GetChosenFont();
185 else
186 f = g_pFontSmall->GetChosenFont();
187 dc->GetTextExtent(label, &width, &height, nullptr, nullptr, &f);
188 dc->DrawText(label, size.x - width - 1, m_plotdown);
189}
190
191void DashboardInstrument_Depth::DrawForeground(wxGCDC* dc) {
192 wxSize size = GetClientSize();
193 wxColour cl;
194 if (m_Properties) {
195 cl = GetColourSchemeFont(m_Properties->m_LabelFont.GetColour());
196 } else {
197 if (GetColourSchemeFont(g_pFontSmall->GetColour()) ==
198 GetColourSchemeFont(g_pFontLabel->GetColour()))
199 GetGlobalColor("DASHL", &cl);
200 else
201 cl = GetColourSchemeFont(g_pFontLabel->GetColour());
202 }
203 wxBrush brush;
204 brush.SetStyle(wxBRUSHSTYLE_SOLID);
205 brush.SetColour(cl);
206 dc->SetBrush(brush);
207 dc->SetPen(*wxTRANSPARENT_PEN);
208
209 double ratioH = double(m_plotheight) / m_max_depth;
210 double ratioW = double(size.x - 6) / (DEPTH_RECORD_COUNT - 1);
211 wxPoint points[DEPTH_RECORD_COUNT + 2];
212
213#ifdef __ANDROID__
214 int px = 3;
215 points[0].x = px;
216 points[0].y = m_plotdown;
217
218 for (int idx = 0; idx < DEPTH_RECORD_COUNT - 1; idx++) {
219 points[1].x = points[0].x;
220 if (m_array_depth[idx])
221 points[1].y = m_plotup + m_array_depth[idx] * ratioH;
222 else
223 points[1].y = m_plotdown;
224
225 points[2].x = points[1].x + ratioW;
226 if (m_array_depth[idx + 1])
227 points[2].y = m_plotup + m_array_depth[idx + 1] * ratioH;
228 else
229 points[2].y = m_plotdown;
230
231 points[3].x = points[2].x;
232 points[3].y = m_plotdown;
233 dc->DrawPolygon(4, points);
234
235 points[0].x = points[2].x;
236 points[0].y = m_plotdown;
237 }
238
239#else
240 for (int idx = 0; idx < DEPTH_RECORD_COUNT; idx++) {
241 points[idx].x = static_cast<int>(idx * ratioW + 3);
242 if (m_array_depth[idx])
243 points[idx].y = static_cast<int>(m_plotup + m_array_depth[idx] * ratioH);
244 else
245 points[idx].y = m_plotdown;
246 }
247 points[DEPTH_RECORD_COUNT].x = size.x - 3;
248 points[DEPTH_RECORD_COUNT].y = m_plotdown;
249 points[DEPTH_RECORD_COUNT + 1].x = 3;
250 points[DEPTH_RECORD_COUNT + 1].y = m_plotdown;
251 dc->DrawPolygon(DEPTH_RECORD_COUNT + 2, points);
252#endif
253 if (m_Properties) {
254 dc->SetFont(m_Properties->m_DataFont.GetChosenFont());
255 dc->SetTextForeground(
256 GetColourSchemeFont(m_Properties->m_DataFont.GetColour()));
257 } else {
258 // GetGlobalColor("DASHF", &cl);
259 dc->SetTextForeground(GetColourSchemeFont(g_pFontData->GetColour()));
260 dc->SetFont(g_pFontData->GetChosenFont());
261 }
262 if (m_depth_unit != "-") { // Watchdog
263 wxString s_depth = wxString::Format("%.2f", m_depth);
264 // We want only one decimal but for security not rounded up.
265 s_depth = s_depth.Mid(0, s_depth.length() - 1);
266 dc->DrawText(s_depth + " " + m_depth_unit, 10, m_DataTop);
267 } else
268 dc->DrawText("---", 10, m_DataTop);
269 if (m_Properties)
270 dc->SetFont(m_Properties->m_LabelFont.GetChosenFont());
271 else
272 dc->SetFont(g_pFontLabel->GetChosenFont());
273 dc->DrawText(m_temp, 5, m_plotdown);
274}
Dashboard depth instrument.
DASH_CAP
Definition instrument.h:77
@ OCPN_DBP_STC_DPT
Depth.
Definition instrument.h:92
@ OCPN_DBP_STC_TMP
Water temperature.
Definition instrument.h:93
wxString getUsrDistanceUnit_Plugin(int unit)
Gets display string for user's preferred distance unit.