1 /******************************************************************************
2  *
3  * Project:  OpenCPN
4  * Purpose:  GRIB Plugin Friends
5  * Author:   David Register
6  *
7  ***************************************************************************
8  *   Copyright (C) 2010 by David S. Register   *
9  *                                                                         *
10  *   This program is free software; you can redistribute it and/or modify  *
11  *   it under the terms of the GNU General Public License as published by  *
12  *   the Free Software Foundation; either version 2 of the License, or     *
13  *   (at your option) any later version.                                   *
14  *                                                                         *
15  *   This program is distributed in the hope that it will be useful,       *
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  *   GNU General Public License for more details.                          *
19  *                                                                         *
20  *   You should have received a copy of the GNU General Public License     *
21  *   along with this program; if not, write to the                         *
22  *   Free Software Foundation, Inc.,                                       *
23  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,  USA.         *
24  ***************************************************************************
25  */
26 
27 #ifndef __GRIBUICTRLBAR_H__
28 #define __GRIBUICTRLBAR_H__
29 
30 #include "wx/wxprec.h"
31 
32 #ifndef  WX_PRECOMP
33 #include "wx/wx.h"
34 #endif //precompiled headers
35 #include <wx/fileconf.h>
36 #include <wx/glcanvas.h>
37 
38 #include "GribUIDialogBase.h"
39 #include "CursorData.h"
40 #include "GribSettingsDialog.h"
41 #include "GribRequestDialog.h"
42 #include "GribReader.h"
43 #include "GribRecordSet.h"
44 #include "IsoLine.h"
45 #include "GrabberWin.h"
46 
47 #ifndef PI
48 #define PI        3.1415926535897931160E0      /* pi */
49 #endif
50 
51 class GRIBUICtrlBar;
52 class GRIBUICData;
53 class GRIBFile;
54 class GRIBRecord;
55 class GribRecordTree;
56 class GRIBOverlayFactory;
57 class GribRecordSet;
58 class GribRequestSetting;
59 class GribGrabberWin;
60 class GribSpacerWin;
61 
62 class wxFileConfig;
63 class grib_pi;
64 class wxGraphicsContext;
65 
66 WX_DECLARE_OBJARRAY( GribRecordSet, ArrayOfGribRecordSets );
67 
68 enum ZoneSelection { AUTO_SELECTION, SAVED_SELECTION, START_SELECTION, DRAW_SELECTION, COMPLETE_SELECTION };
69 
70 class GribTimelineRecordSet : public GribRecordSet
71 {
72 public:
73     GribTimelineRecordSet(unsigned int cnt);
74 //    GribTimelineRecordSet(GribRecordSet &GRS1, GribRecordSet &GRS2, double interp_const);
75     ~GribTimelineRecordSet();
76 
77     void ClearCachedData();
78 
79     /* cache isobars here to speed up rendering */
80     wxArrayPtrVoid *m_IsobarArray[Idx_COUNT];
81 };
82 
83 //----------------------------------------------------------------------------------------------------------
84 //    GRIB CtrlBar Specification
85 //----------------------------------------------------------------------------------------------------------
86 class GRIBUICtrlBar: public GRIBUICtrlBarBase {
87 public:
88 
89     GRIBUICtrlBar(wxWindow *parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ,grib_pi *ppi );
90     ~GRIBUICtrlBar();
91 
92     void OpenFile( bool newestFile = false );
93 
94     void ContextMenuItemCallback(int id);
95     void SetFactoryOptions();
96 
97     wxDateTime TimelineTime();
98     GribTimelineRecordSet* GetTimeLineRecordSet(wxDateTime time);
99     void StopPlayBack();
100     void TimelineChanged();
101     void CreateActiveFileFromNames( const wxArrayString &filenames );
102     void PopulateComboDataList();
103     void ComputeBestForecastForNow();
104     void SetViewPort( PlugIn_ViewPort *vp );
105     void SetDataBackGroundColor();
106     void SetTimeLineMax( bool SetValue );
107 	void SetCursorLatLon( double lat, double lon );
108     void UpdateTrackingControl();
109 	void SetDialogsStyleSizePosition( bool force_recompute = false );
110     void SetRequestBitmap( int type );
111     void OnMouseEvent( wxMouseEvent& event );
GetCDataDialog()112     GRIBUICData *GetCDataDialog() { return m_gGRIBUICData; }
InDataPlot(int id)113     bool InDataPlot (int id) { return id > wxID_ANY && id < (int)GribOverlaySettings::GEO_ALTITUDE; }
114     void SetScaledBitmap( double factor );
115     wxBitmap GetScaledBitmap(wxBitmap bitmap, const wxString svgFileName, double scale_factor);
116     void OpenFileFromJSON( wxString json);
117 
118     //
119     double getTimeInterpolatedValue ( int idx, double lon, double lat, wxDateTime t);
120     bool getTimeInterpolatedValues ( double &M, double &A, int idx1, int idx2, double lon, double lat, wxDateTime t);
121 
122 
123     wxWindow *pParent;
124     GribOverlaySettings m_OverlaySettings;
125 
126 	GribTimelineRecordSet *m_pTimelineSet;
127 
128     wxTimer         m_tPlayStop;
129     grib_pi         *pPlugIn;
130     GribRequestSetting  *pReq_Dialog;
131     GRIBFile        *m_bGRIBActiveFile;
132 	bool            m_bDataPlot[GribOverlaySettings::GEO_ALTITUDE];  //only for no altitude parameters
133 	bool            m_CDataIsShown;
134     int             m_ZoneSelAllowed;
135     int             m_old_DialogStyle;
136     double			m_ScaledFactor;
137     void DoZoomToCenter();
138 
139 private:
140     void OnClose( wxCloseEvent& event );
141     void OnSize( wxSizeEvent& event );
142     void OnPaint( wxPaintEvent& event );
143     void OnSettings( wxCommandEvent& event );
144     void OnPlayStop( wxCommandEvent& event );
145     void OnPlayStopTimer( wxTimerEvent & event);
146     void OnMove( wxMoveEvent& event );
147     void OnMenuEvent( wxMenuEvent& event );
148     void MenuAppend( wxMenu *menu, int id, wxString label, wxItemKind kind, wxBitmap bitmap = wxNullBitmap, wxMenu *submenu = NULL );
149     void OnZoomToCenterClick( wxCommandEvent& event );
150     void OnPrev( wxCommandEvent& event );
OnRecordForecast(wxCommandEvent & event)151     void OnRecordForecast( wxCommandEvent& event ) { StopPlayBack(); m_InterpolateMode = false; m_pNowMode = false; TimelineChanged(); }
152     void OnNext( wxCommandEvent& event );
OnNow(wxCommandEvent & event)153     void OnNow( wxCommandEvent& event ) { StopPlayBack(); ComputeBestForecastForNow(); }
154     void OnAltitude( wxCommandEvent& event );
155     void OnOpenFile( wxCommandEvent& event );
156     void OnRequest(  wxCommandEvent& event );
157     void OnCompositeDialog( wxCommandEvent& event );
158 
159     void OnTimeline( wxScrollEvent& event );
160 	void OnShowCursorData( wxCommandEvent& event );
161 
162     wxDateTime MinTime();
163     wxArrayString GetFilesInDirectory();
164     void SetGribTimelineRecordSet(GribTimelineRecordSet *pTimelineSet);
165     int GetNearestIndex(wxDateTime time, int model);
166     int GetNearestValue(wxDateTime time, int model);
167     bool GetGribZoneLimits(GribTimelineRecordSet *timelineSet, double *latmin, double *latmax, double *lonmin, double *lonmax);
168     wxDateTime GetNow();
169     void RestaureSelectionString();
SaveSelectionString()170     void SaveSelectionString()  { m_SelectionIsSaved = true; m_Selection_index = m_cRecordForecast->GetSelection();
171             m_Selection_label = m_cRecordForecast->GetString( m_Selection_index); }
172 
173     //    Data
174 	CursorData        *m_gCursorData;
175     GribGrabberWin    *m_gGrabber;
176     GRIBUICData       *m_gGRIBUICData;
177 
178     PlugIn_ViewPort   *m_vp;
179     int m_lastdatatype;
180 
181     int m_TimeLineHours;
182     int m_FileIntervalIndex;
183     bool m_InterpolateMode;
184     bool m_pNowMode;
185     bool m_HasAltitude;
186 
187     bool             m_SelectionIsSaved;
188     int              m_Selection_index;
189     wxString         m_Selection_label;
190     wxArrayString    m_file_names;   /* selected files */
191     wxString         m_grib_dir;
192 	wxSize           m_DialogsOffset;
193 };
194 
195 //----------------------------------------------------------------------------------------------------------
196 //    GRIBFile Object Specification
197 //----------------------------------------------------------------------------------------------------------
198 class GRIBFile {
199 public:
200 
201     GRIBFile( const wxArrayString & file_names, bool CumRec, bool WaveRec, bool newestFile = false );
202     ~GRIBFile();
203 
IsOK(void)204     bool IsOK( void )
205     {
206         return m_bOK;
207     }
GetFileNames(void)208     wxArrayString &GetFileNames( void )
209     {
210         return m_FileNames;
211     }
GetLastMessage(void)212     wxString GetLastMessage( void )
213     {
214         return m_last_message;
215     }
GetRecordSetArrayPtr(void)216     ArrayOfGribRecordSets *GetRecordSetArrayPtr( void )
217     {
218         return &m_GribRecordSetArray;
219     }
GetRefDateTime(void)220     time_t GetRefDateTime( void )
221     {
222         return m_pRefDateTime;
223     }
224 
GetCounter()225     const unsigned int GetCounter( )
226     {
227         return m_counter;
228     }
229 
230     WX_DEFINE_ARRAY_INT(int, GribIdxArray);
231     GribIdxArray m_GribIdxArray;
232 
233 private:
234     static unsigned int ID;
235 
236     const unsigned int m_counter;
237     bool m_bOK;
238     wxString m_last_message;
239     wxArrayString m_FileNames;
240     GribReader *m_pGribReader;
241     time_t m_pRefDateTime;
242 
243     //    An array of GribRecordSets found in this GRIB file
244     ArrayOfGribRecordSets m_GribRecordSetArray;
245 
246     int m_nGribRecords;
247 };
248 
249 //----------------------------------------------------------------------------------------------------------
250 //    GRIB CursorData Dialog Specification
251 //----------------------------------------------------------------------------------------------------------
252 class GRIBUICData: public GRIBUICDataBase
253 {
254 public:
255 
256     GRIBUICData( GRIBUICtrlBar &parent );
~GRIBUICData()257     ~GRIBUICData() {}
258 
259     // GribGrabberWin      *m_gGrabber;
260     GRIBUICtrlBar       &m_gpparent;
261     CursorData          *m_gCursorData;
262 private:
263     void OnMove( wxMoveEvent& event );
264 };
265 
266 #endif
267