source: trunk/MagicSoft/Cosy/gui/MGCosy.cc@ 1275

Last change on this file since 1275 was 1275, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 17.9 KB
Line 
1#include "MGCosy.h"
2
3#include <iostream.h>
4
5#include "msgqueue.h"
6#include "coord.h"
7
8#include <TROOT.h>
9
10#include <TGTab.h> // TGTab
11#include <TGMenu.h> // TGPopupMenu
12#include <TGButton.h> // TGButton
13#include <TSystem.h> // gSystem
14#include <TGLabel.h> // TGLabel
15#include <TGListBox.h> // TGListBox
16#include <TG3DLine.h> // TGHorizontal3DLine (TGSplitter)
17#include <TGFrame.h> // TGGroupFrame
18#include <TApplication.h> // gApplication
19
20//#include "timer.h" // Timer
21
22#include "MCosy.h"
23#include "MGList.h"
24#include "MGAccuracy.h"
25#include "MGCoordinates.h"
26#include "MGSkyPosition.h"
27#include "MGVelocity.h"
28
29#include "SlaStars.h"
30
31ClassImp(MGCosy);
32
33#define IDM_EXIT 1
34#define IDM_TEXT 2
35
36#define kPB_POSITION 0x1001
37#define kPB_TRACK 0x1002
38#define kPB_STOP 0x1003
39#define kPB_CALCALTAZ 0x1004
40#define kPB_POLARIS 0x1005
41#define kPB_START 0x1006
42
43#define kEF_A 0x1010
44#define kEF_B 0x1011
45
46void MGCosy::CreateMenu()
47{
48 fLayMenuBar = new TGLayoutHints (kLHintsNormal | kLHintsExpandX);
49 fLayMenuItem = new TGLayoutHints (kLHintsNormal, 0, 4, 0, 0);
50
51 //
52 // crate the menu bar
53 //
54 TGPopupMenu *fFileMenu = new TGPopupMenu(gClient->GetRoot());
55 fFileMenu->AddEntry("Exit", IDM_EXIT);
56 fFileMenu->Associate(this);
57 fList->Add(fFileMenu);
58
59 //
60 // the button messages are handled by main frame (this)
61 //
62 TGMenuBar *fMenuBar = new TGMenuBar(this, 1, 1, kHorizontalFrame);
63 fMenuBar->AddPopup("File", fFileMenu, fLayMenuItem);
64 AddFrame(fMenuBar, fLayMenuBar);
65 fList->Add(fMenuBar);
66
67 //
68 // Seperator beyond menubar
69 //
70
71 TGHorizontal3DLine *fLineSep = new TGHorizontal3DLine(this);
72 AddFrame(fLineSep, fLayMenuBar);
73 fList->Add(fLineSep);
74}
75
76void MGCosy::CreateLabel(TGCompositeFrame *f)
77{
78 const int x = 180;
79 const int y = 25;
80
81 TGLabel *l;
82
83 l = new TGLabel(f, "SE-Az:");
84 l->Move(x-60, y);
85 fList->Add(l);
86
87 l = new TGLabel(f, "SE-Zd1:");
88 l->Move(x-60, y+20);
89 fList->Add(l);
90
91 l = new TGLabel(f, "SE-Zd2:");
92 l->Move(x-60, y+40);
93 fList->Add(l);
94
95
96 fLabel1 = new TGLabel*[3];
97 fLabel1[0] = new TGLabel(f, "00000"); // Max: 16384
98 fLabel1[1] = new TGLabel(f, "000"); // Max: 256
99 fLabel1[2] = new TGLabel(f, "000"); // Max: 256
100 fLabel1[0]->SetTextJustify(kTextRight);
101 fLabel1[1]->SetTextJustify(kTextRight);
102 fLabel1[2]->SetTextJustify(kTextRight);
103 fLabel1[0]->Move(x, y);
104 fLabel1[1]->Move(x+43, y);
105 fLabel1[2]->Move(x+70, y);
106 fList->Add(fLabel1[0]);
107 fList->Add(fLabel1[1]);
108 fList->Add(fLabel1[2]);
109 //f->AddFrame(fLabel1[0]);
110 //f->AddFrame(fLabel1[1]);
111 //f->AddFrame(fLabel1[2]);
112
113 fLabel2 = new TGLabel*[3];
114 fLabel2[0] = new TGLabel(f, "00000");
115 fLabel2[1] = new TGLabel(f, "000");
116 fLabel2[2] = new TGLabel(f, "000");
117 fLabel2[0]->SetTextJustify(kTextRight);
118 fLabel2[1]->SetTextJustify(kTextRight);
119 fLabel2[2]->SetTextJustify(kTextRight);
120 fLabel2[0]->Move(x, y+20);
121 fLabel2[1]->Move(x+43, y+20);
122 fLabel2[2]->Move(x+70, y+20);
123 fList->Add(fLabel2[0]);
124 fList->Add(fLabel2[1]);
125 fList->Add(fLabel2[2]);
126 //f->AddFrame(fLabel2[0]);
127 //f->AddFrame(fLabel2[1]);
128 //f->AddFrame(fLabel2[2]);
129
130 fLabel3 = new TGLabel*[3];
131 fLabel3[0] = new TGLabel(f, "00000");
132 fLabel3[1] = new TGLabel(f, "000");
133 fLabel3[2] = new TGLabel(f, "000");
134 fLabel3[0]->SetTextJustify(kTextRight);
135 fLabel3[1]->SetTextJustify(kTextRight);
136 fLabel3[2]->SetTextJustify(kTextRight);
137 fLabel3[0]->Move(x, y+40);
138 fLabel3[1]->Move(x+43, y+40);
139 fLabel3[2]->Move(x+70, y+40);
140 fList->Add(fLabel3[0]);
141 fList->Add(fLabel3[1]);
142 fList->Add(fLabel3[2]);
143 //f->AddFrame(fLabel3[0]);
144 //f->AddFrame(fLabel3[1]);
145 //f->AddFrame(fLabel3[2]);
146
147 l = new TGLabel(f, "Offset-Zd:");
148 l->Move(x-60, y+80);
149 fList->Add(l);
150
151 l = new TGLabel(f, "Offset-Az:");
152 l->Move(x-60, y+100);
153 fList->Add(l);
154
155 fOffsetZd = new TGLabel(f, "0000000");
156 fOffsetAz = new TGLabel(f, "0000000");
157 fOffsetZd->SetTextJustify(kTextRight);
158 fOffsetAz->SetTextJustify(kTextRight);
159 fOffsetZd->Move(x, y+80);
160 fOffsetAz->Move(x, y+100);
161 fList->Add(fOffsetZd);
162 fList->Add(fOffsetAz);
163
164
165 fError = new TGLabel(f, "Error");
166 fMoving = new TGLabel(f, "Moving");
167 fTracking = new TGLabel(f, "Tracking");
168 fStopping = new TGLabel(f, "Stopping");
169 fStopped = new TGLabel(f, "Stopped");
170
171
172 ULong_t color;
173
174 gClient->GetColorByName("Red", color);
175 fError->SetBackgroundColor(color);
176 gClient->GetColorByName("LightBlue", color);
177 fMoving->SetBackgroundColor(color);
178 gClient->GetColorByName("Blue", color);
179 fTracking->SetBackgroundColor(color);
180 gClient->GetColorByName("Orange", color);
181 fStopping->SetBackgroundColor(color);
182 gClient->GetColorByName("Green", color);
183 fStopped->SetBackgroundColor(color);
184
185 fError ->Move(10, 25);
186 fMoving ->Move(10, 25+20);
187 fTracking->Move(10, 25+40);
188 fStopping->Move(10, 25+60);
189 fStopped ->Move(10, 25+80);
190
191 fError ->Resize(60, 20);
192 fMoving ->Resize(60, 20);
193 fTracking->Resize(60, 20);
194 fStopping->Resize(60, 20);
195 fStopped ->Resize(60, 20);
196
197 fList->Add(fError);
198 fList->Add(fMoving);
199 fList->Add(fTracking);
200 fList->Add(fStopping);
201 fList->Add(fStopped);
202}
203
204void MGCosy::CreateTabs(TGTab *fTab)
205{
206 TGCompositeFrame *tf1 = fTab->AddTab("Position Zd/Az");
207 TGCompositeFrame *tf2 = fTab->AddTab("Track Ra/Dec");
208 TGCompositeFrame *tf3 = fTab->AddTab("Demo Mode");
209
210 fCZdAz = new MGCoordinates(tf1, kETypeZdAz);
211 fCZdAz->Move(27, 105);
212 fList->Add(fCZdAz);
213
214 fCRaDec = new MGCoordinates(tf2, kETypeRaDec);
215 fCRaDec->Move(27, 105);
216 fList->Add(fCRaDec);
217
218
219 const int x=15;
220 const int y=12;
221 const int h=16;
222
223 TGLabel *l1 = new TGLabel(tf1, "Move the telescope to a position given in");
224 TGLabel *l2 = new TGLabel(tf1, "local coordinates. The given coordinates");
225 TGLabel *l3 = new TGLabel(tf1, "are pointing coordinates, which means that the");
226 TGLabel *l4 = new TGLabel(tf1, "coordinates are corrected for the bending of");
227 TGLabel *l5 = new TGLabel(tf1, "the telescope.");
228 l1->Move(x, y);
229 l2->Move(x, y+h);
230 l3->Move(x, y+2*h);
231 l4->Move(x, y+3*h);
232 l5->Move(x, y+4*h);
233 fList->Add(l1);
234 fList->Add(l2);
235 fList->Add(l3);
236 fList->Add(l4);
237 fList->Add(l5);
238
239 l1 = new TGLabel(tf2, "Track a position given in sky coordinates.");
240 l2 = new TGLabel(tf2, "Right Ascension and declination must be given");
241 l3 = new TGLabel(tf2, "in the FK5, J2000 coordinate system");
242 l1->Move(x, y);
243 l2->Move(x, y+h);
244 l3->Move(x, y+2*h);
245 fList->Add(l1);
246 fList->Add(l2);
247 fList->Add(l3);
248
249 //
250 // light green: 3 light red: 2 blue?: 2
251 // dark green: 8 dark red: 50 dark blue?: 1
252
253 //
254 // blue: 0-7, 9, 10,11, 14, 15, 18, 19, 22, 23, 27, 31, 33-39, 41-43
255 // 46, 47
256 // green: 8, 12,13, 16, 17, 20, 21, 24, 25, 26, 28, 29, 30, 40, 44
257 // 45, 48
258 // gray: 31, 113-
259 // red: 164, 192, 224, 232
260 //
261 TGTextButton *but;
262
263 but= new TGTextButton(this, "Calc Zd/Az", kPB_CALCALTAZ);
264 but->Resize(80, 25);
265 but->Move(37, 257);
266 but->SetToolTipText("Calculate Zd/Az corresponding to Ra/Dec.");
267 fList->Add(but);
268
269 but = new TGTextButton(this, "Set Polaris", kPB_POLARIS);
270 but->Resize(80, 25);
271 but->Move(37, 288);
272 but->SetToolTipText("Set the actual position as the position of Polaris.");
273 fList->Add(but);
274
275 ULong_t color;
276
277 but = new TGTextButton(this, "Start", kPB_START);
278 gClient->GetColorByName("Green", color);
279 but->SetBackgroundColor(color);
280 but->Move(147, 275);
281 but->Resize(62, 25);
282 but->SetToolTipText("Start a telescope movement.");
283 fList->Add(but);
284
285 but = new TGTextButton(this, "Stop", kPB_STOP);
286
287 gClient->GetColorByName("Red", color);
288 but->SetBackgroundColor(color);
289 but->Move(212, 275);
290 but->Resize(62, 25);
291 but->SetToolTipText("Stop any movement of telescope.");
292 fList->Add(but);
293
294}
295
296MGCosy::MGCosy(MsgQueue *q, const TGWindow *p, UInt_t w, UInt_t h)
297 : TGMainFrame(p, w, h), fQueue(q)
298{
299 fList = new MGList;
300
301 cout << "MGCosy: Creating Menu" << endl;
302 CreateMenu();
303
304 cout << "MGCosy: Creating TGCompositeFrame" << endl;
305 TGCompositeFrame *f = new TGCompositeFrame(this, 0, 0);
306 f->SetLayoutManager(new TGMatrixLayout(f, 3, 2, 10));
307 fList->Add(f);
308
309 cout << "MGCosy: Creating TGTab" << endl;
310 fTab = new TGTab(f, 300, 300);
311 fList->Add(fTab);
312
313 cout << "MGCosy: Creating MGSkyPosition" << endl;
314 fSkyPosition = new MGSkyPosition(f, 300);
315 cout << "MGCosy: Creating MGAccuracy" << endl;
316 fAccuracy = new MGAccuracy (f, 300);
317 cout << "MGCosy: Creating MGVelocity" << endl;
318 fVelocity = new MGVelocity (f, "Velocity [\"/min]", 300);
319// fOffset = new MGVelocity (f, "Offset se-re [']", 300);
320
321 fList->Add(fSkyPosition);
322 fList->Add(fAccuracy);
323 fList->Add(fVelocity);
324// fList->Add(fOffset);
325
326 cout << "MGCosy: Creating TGGroupFrame" << endl;
327 TGGroupFrame *frame = new TGGroupFrame(f, "Status");
328 frame->Resize(300, 300);
329 fList->Add(frame);
330
331 cout << "MGCosy: Creating TGListBox" << endl;
332 fLog = new TGListBox(f, -1, kSunkenFrame); //kSunkenFrame|kDoubleBorder,
333 fLog->Resize(300, 300);
334 fLog->ChangeBackground(TGFrame::GetBlackPixel());
335 fList->Add(fLog);
336
337 //
338 // Layout the window, tile by tile...
339 //
340 f->AddFrame(fTab);
341 f->AddFrame(fSkyPosition);
342 f->AddFrame(fAccuracy);
343 f->AddFrame(frame);
344 f->AddFrame(fVelocity);
345 f->AddFrame(fLog);
346
347 AddFrame(f, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 0, 0, 1, 0));
348
349 //
350 // FIXME!
351 //
352 cout << "MGCosy: Creating Tabs" << endl;
353 CreateTabs(fTab);
354 cout << "MGCosy: Creating Label" << endl;
355 CreateLabel(frame);
356
357 //
358 // Map the window, set up the layout, etc.
359 //
360 cout << "MGCosy: Setting Size" << endl;
361 const Int_t w = 940;
362 const Int_t h = 660;
363 SetWMSizeHints(w, h, w, h, 10, 10); // set the smallest and biggest size of the Main frame
364
365 SetWindowName("Cosy Main Window");
366 SetIconName("Cosy");
367
368 cout << "MGCosy: Map Window" << endl;
369 MapSubwindows();
370 Resize(GetDefaultSize());
371 MapWindow();
372}
373
374// ======================================================================
375
376MGCosy::~MGCosy()
377{
378 cout << "MGCosy::~MGCosy called." << endl;
379
380 delete fLayMenuBar;
381 delete fLayMenuItem;
382
383 cout << "Deleting MGCosy::fList" << endl;
384
385 delete fList;
386
387 cout << "MGCosy::~MGCosy done." << endl;
388}
389// ======================================================================
390void MGCosy::EnableLabel(TGLabel *label, Bool_t stat)
391{
392 stat ? label->MapWindow() : label->UnmapWindow();
393
394 /*
395 TGGC *fRedTextGC(TGButton::GetDefaultGC())
396 // Set foreground color in graphics context for drawing of
397 // TGlabel and TGButtons with text in red.
398 ULong_t red;
399 gClient->GetColorByName("red", red);
400 fRedTextGC.SetForeground(red);
401 */
402}
403
404void MGCosy::UpdateOffset(ZdAz &off)
405{
406 static Int_t zd=~0;
407 static Int_t az=~0;
408
409 char text[21];
410
411 if (zd!=(Int_t)off.Zd())
412 {
413 zd = (Int_t)off.Zd();
414 sprintf(text, "%d", zd);
415 fOffsetZd->SetText(new TGString(text));
416 }
417 if (az!=(Int_t)off.Az())
418 {
419 az = (Int_t)off.Az();
420 sprintf(text, "%d", az);
421 fOffsetAz->SetText(new TGString(text));
422 }
423}
424
425void MGCosy::Update(ZdAz pos, ZdAz acc, ZdAz vel, ZdAz off, UInt_t stat)
426{
427 fSkyPosition->Update(pos);
428 fAccuracy->Update(pos, acc);
429 fVelocity->Update(vel);
430 UpdateOffset(off);
431
432#define kError 0x01
433#define kMoving 0x02
434#define kTracking 0x04
435#define kStopping 0x08
436#define kStopped 0x10
437
438 EnableLabel(fError, stat&kError);
439 EnableLabel(fMoving, stat&kMoving);
440 EnableLabel(fTracking, stat&kTracking);
441 EnableLabel(fStopping, stat&kStopping);
442 EnableLabel(fStopped, stat&kStopped);
443
444 stat&kTracking ? fAccuracy->MapWindow() : fAccuracy->UnmapWindow();
445 stat&kTracking ? fVelocity->MapWindow() : fVelocity->UnmapWindow();
446 // stat&kTracking ? fOffset->MapWindow() : fOffset->UnmapWindow();
447
448 if (!fLog->TestBit(kHasChanged))
449 return;
450
451 fLog->MapSubwindows();
452 fLog->Layout();
453 fLog->ResetBit(kHasChanged);
454}
455// ======================================================================
456
457void MGCosy::CloseWindow()
458{
459 // Got close message for this MainFrame. Calls parent CloseWindow()
460 // (which destroys the window) and terminate the application.
461 // The close message is generated by the window manager when its close
462 // window menu item is selected.
463
464 // gSystem->ExitLoop();
465 // gSystem->DispatchOneEvent(kTRUE);
466
467 // TGMainFrame::CloseWindow();
468 cout << "Closing window - waiting until all nodes are stopped." << endl;
469 fQueue->PostMsg(WM_QUIT, 0, 0);
470 cout << "Closing window - done." << endl;
471 // gApplication->Terminate(0);
472}
473
474void MGCosy::StartTrack()
475{
476 cout << "Start tracking." << endl;
477
478 XY xy = fCRaDec->GetCoordinates();
479 RaDec dest(xy.X()*15., xy.Y()); // xy.X() [h]->[ø]
480
481 cout << dest.Ra() << "h " << dest.Dec() << "\xb0" << endl;
482
483 fQueue->PostMsg(WM_TRACK, &dest, sizeof(dest));
484
485 cout << "PostMsg (WM_Track) returned." << endl;
486}
487
488void MGCosy::StartPos()
489{
490 cout << "Start positioning." << endl;
491
492 XY xy = fCZdAz->GetCoordinates();
493 ZdAz dest(xy.X(), xy.Y());
494
495 cout << dest.Zd() << kDEG << " " << dest.Az() << kDEG << endl;
496
497 fQueue->PostMsg(WM_POSITION, &dest, sizeof(dest));
498
499 cout << "PostMsg (WM_Position) returned." << endl;
500}
501
502//
503// ************************** For demo purpose **********************
504//
505#include <TRandom.h>
506class MDemo : public MThread
507{
508private:
509 MsgQueue *fQueue;
510 TRandom fRand;
511
512public:
513 MDemo() : MThread(false) {}
514
515 void SetQueue(MsgQueue *q) { fQueue = q; }
516
517 virtual void *Thread()
518 {
519 while (1)
520 {
521 Timer tm;
522 tm.Now();
523
524 Float_t h = 2.+tm.H()+(8.+tm.M())/60.;
525 RaDec dest(h*15, 130);
526
527 cout << dest.Ra()/15 << "h " << dest.Dec() << "°" << endl;
528
529 fQueue->PostMsg(WM_TRACK, &dest, sizeof(dest));
530
531 int i = 0;
532 while (!HasStopFlag() && i++<130) // 2.5min
533 usleep(1000000);
534 if (HasStopFlag())
535 break;
536
537 //fQueue->PostMsg(WM_STOP, 0, 0);
538
539 ZdAz dest1(fRand.Integer(56)+5, fRand.Integer(360));
540
541 cout << "Demo: Zd=" << dest1.Zd() << "° Az=" << dest1.Az() << "°" << endl;
542
543 fQueue->PostMsg(WM_POSITION, &dest1, sizeof(dest1));
544
545 i = 0;
546 while (!HasStopFlag() && i++<30) // 30s
547 usleep(1000000);
548 if (HasStopFlag())
549 break;
550
551 //ZdAz dest2(5, 30);
552 //fQueue->PostMsg(WM_POSITION, &dest2, sizeof(dest2));
553 /*
554 i = 0;
555 while (!HasStopFlag() && i++<30) // 30s
556 usleep(1000000);
557 */
558 //if (HasStopFlag())
559 // break;
560 }
561 cout << "Demo Thread: done." << endl;
562 return NULL;
563 }
564};
565
566MDemo demo;
567
568void MGCosy::StartDemo()
569{
570 cout << "Start Demo." << endl;
571
572 demo.SetQueue(fQueue);
573 demo.Start();
574
575 cout << "PostMsg (WM_Demo) returned." << endl;
576}
577
578void StopDemo()
579{
580 cout << "Stopping demo." << endl;
581 demo.Stop();
582}
583//
584// ******************************************************************
585//
586
587Bool_t MGCosy::ProcessMessage(Long_t msg, Long_t mp1, Long_t mp2)
588{
589 //
590 // This processing is serialized!
591 //
592 switch (GET_MSG(msg))
593 {
594 case kC_COMMAND:
595 switch (GET_SUBMSG(msg))
596 {
597 case kCM_TAB:
598 //cout << "Tab: " << mp1 << endl;
599 return kTRUE;
600
601 case kCM_BUTTON:
602
603 switch (mp1)
604 {
605 /*
606 case kPB_POSITION:
607 Start(0);
608 return kTRUE;
609
610 case kPB_TRACK:
611 Start(1);
612 return kTRUE;
613 */
614 case kPB_START:
615 switch (fTab->GetCurrent())
616 {
617 case 0:
618 StartPos();
619 return kTRUE;
620 case 1:
621 StartTrack();
622 return kTRUE;
623 case 2:
624 StartDemo();
625 return kTRUE;
626 }
627 return kTRUE;
628
629 case kPB_STOP:
630 cout << "Sending stop movement msg." << endl;
631 StopDemo();
632 fQueue->PostMsg(WM_STOP, 0, 0);
633
634 cout << "PostMsg (WM_Stop) returned." << endl;
635 return kTRUE;
636
637 case kPB_CALCALTAZ:
638 {
639 SlaStars sla;
640 sla.SetMjd2Now();
641
642 XY xy = fCRaDec->GetCoordinates();
643 RaDec rd(xy.X()*15., xy.Y());
644
645 cout << "Ra/Dec: " << rd.Ra() << kDEG << " " << rd.Dec() << kDEG << endl;
646 ZdAz aa=sla.CalcZdAz(rd*kDeg2Rad)*kRad2Deg;
647 cout << "Zd/Az: " << aa.Zd() << kDEG << " " << aa.Az() << kDEG << endl;
648 }
649 return kTRUE;
650
651 case kPB_POLARIS:
652 fQueue->PostMsg(WM_POLARIS, 0, 0);
653 return kTRUE;
654
655 default:
656 return kTRUE;
657 }
658 return kTRUE;
659
660 case kCM_MENU:
661
662 switch (mp1)
663 {
664 case IDM_EXIT:
665 cout << "IDM_EXIT: Posting WM_QUIT." << endl;
666 fQueue->PostMsg(WM_QUIT, 0, 0);
667 cout << "IDM_EXIT: WM_QUIT done." << endl;
668 //cout << "Idm_Exit." << endl;
669 //CloseWindow();
670 return kTRUE;
671 }
672 return kTRUE;
673 }
674 }
675
676 //
677 // Start a thread to process the message.
678 //
679 // fQueue->PostMsg(msg, mp1, mp2);
680 return kTRUE;
681
682}
Note: See TracBrowser for help on using the repository browser.