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