]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/include/wxReadingPriorityPanel.cpp
90965cbb72382f008c35810166f6ec707b1ebd7a
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / include / wxReadingPriorityPanel.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        Test_ReadingPriorityFrm.cpp
3 // Purpose:     
4 // Author:      
5 // Modified by: 
6 // Created:     
7 // RCS-ID:      
8 // Copyright:   (C)2003 
9 // Licence:     wxWindows
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ----------------------------------------------------------------------------
13 // headers
14 // ----------------------------------------------------------------------------
15 #include "wx/wxprec.h"
16 #include <wx/thread.h>
17
18
19 #ifndef WX_PRECOMP
20         #include "wx/wx.h"
21 #endif
22
23 #include "wxReadingPriorityPanel.h"
24
25
26 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
29 PriorityLoop::PriorityLoop(){
30         _vecIdImage                     =       NULL;
31         _loadingSerie           =       false;
32         _forceReadRegion        =       false;
33         _i_forceReadReagion =   -1;
34         _needToChange       =   false;
35         _deltaImg                       =       0;
36 }
37 // ----------------------------------------------------------------------------
38 PriorityLoop::~PriorityLoop(){
39         if (_vecIdImage!=NULL) {
40                 free(_vecIdImage);
41         }
42 }
43 // ----------------------------------------------------------------------------
44 void PriorityLoop::StopLoop(){
45         _loadingSerie = false;
46 }
47 // ----------------------------------------------------------------------------
48 bool PriorityLoop::IsLoop(){
49         return _loadingSerie;
50 }
51 // ----------------------------------------------------------------------------
52 void PriorityLoop::ForceEndLoopRegion(){
53         _forceReadRegion=true;
54 }
55
56 // ----------------------------------------------------------------------------
57 void PriorityLoop::InitLoop(){
58         _loadingSerie = true;
59         NeedToChange();
60         Create_Empty_Vector();
61         InitConfiguration();
62         _counterReading = 0;
63 }
64
65 // ----------------------------------------------------------------------------
66 void PriorityLoop::Iteration(){
67
68         NeedToChange();
69
70         if (_forceReadRegion==false){
71                 int ii,i=Get_i_image();
72                 for (ii=i-_deltaImg;ii<=i+_deltaImg;ii++){
73                         if (_Action_i( ii )==true){
74                                 _counterReading++;
75                         }
76                 }
77         } else {
78                 _loadingSerie=_ForceEndLoopRegion();
79         }
80
81         if (_counterReading==_maxImages){
82                 _loadingSerie=false;
83         }
84
85 }
86
87 // ----------------------------------------------------------------------------
88 bool PriorityLoop::_ForceEndLoopRegion (){
89         bool ok=true;
90         if (_i_forceReadReagion==-1){
91                 _i_forceReadReagion=_start;
92         }
93         _Action_i( _i_forceReadReagion );
94         _i_forceReadReagion++;
95         if (_i_forceReadReagion>_end){
96                 ok=false;
97         }
98         return ok;
99 }
100 // ----------------------------------------------------------------------------
101 void PriorityLoop::Create_Empty_Vector(){
102         if (_vecIdImage!=NULL) {
103                 free(_vecIdImage);
104         }
105         _vecIdImage =  (bool*)malloc(sizeof(bool)*(_maxImages+1) );
106         int i;
107         for (i=0; i<=_maxImages;i++){
108                 _vecIdImage[i]=false;
109         }       
110
111 }
112
113 // ----------------------------------------------------------------------------
114 void PriorityLoop::SetStartEndActualMax(int start,int end, int actual, int deltaImg,int maxImages){
115         _tstart         = start;
116         _tend           = end;
117         _tactual        = actual;
118         _tdeltaImg      = deltaImg;
119         _tmaxImages     = maxImages;
120         _needToChange = true;
121 }
122 // ----------------------------------------------------------------------------
123 void PriorityLoop::NeedToChange(){
124         if (_needToChange==true){
125                 _needToChange   = false;
126                 _start                  = _tstart;
127                 _end                    = _tend;
128                 _actual                 = _tactual;
129                 _deltaImg               = _tdeltaImg;
130                 _maxImages              = _tmaxImages;
131                 InitConfiguration();
132         }
133 }
134
135 // ----------------------------------------------------------------------------
136 void PriorityLoop::InitConfiguration(){
137         _forceReadRegion=false;
138         int i;
139         for (i=0;i<PriorityLoadImage_MAX_P;i++){
140                 _dg[i]=false;
141                 _counterActual[i]=0;
142         }
143         _priorityType=0;
144         _priority=9;
145 }
146
147 // ----------------------------------------------------------------------------
148 bool PriorityLoop::_Action_i( int i ){
149         bool ok=false;
150         if (i!=-1) {
151                 if ((i>=0) && (i<_maxImages)){
152                         if (_vecIdImage[i]==false ) {
153                                 Action_i(i);
154                                 _vecIdImage[i]=true;
155                                 ok=true;
156                         }
157                 }
158         }
159         return ok;
160 }
161
162 // ----------------------------------------------------------------------------
163 void PriorityLoop::Action_i( int i ){
164 }
165
166 // ----------------------------------------------------------------------------
167 int PriorityLoop::Get_i_image(){
168         int p=4;
169
170         int i=-1;
171         while ((i==-1) || (p==4) ){
172                 if (_priorityType==0){
173                         if ((_priority<=9) && (_priority>=5))   { p=0; }
174                         if ((_priority<=4) && (_priority>=2))   { p=1; }
175                         if ((_priority<=1) && (_priority>=0))   { p=2; }
176                 }
177                 if (_priorityType==1){
178                         if ((_priority<=9) && (_priority>=4))   { p=1; }
179                         if ((_priority<=3) && (_priority>=2))   { p=2; }
180                         if ((_priority<=1) && (_priority>=0))   { p=3; }
181                 }
182                 if (_priorityType==2){
183                         if ((_priority<=9) && (_priority>=3))   { p=2; }
184                         if ((_priority<=2) && (_priority>=0))   { p=3; }
185                 }
186                 if (_priorityType==3){
187                         if ((_priority<=9) && (_priority>=0))   { p=3; }
188                 }
189                 if (_priorityType==4){
190                         p=4;
191                 }
192                 _priority--;
193                 if (_priority==-1) {_priority=9;}
194
195                 if (p==0) { i=Geti(p,5,_start,_actual           ,_actual                ,_end);                 }
196                 if (p==1) { i=Geti(p,1,_start,_actual           ,_actual                ,_end);                 }
197                 if (p==2) { i=Geti(p,5,0         ,_start                ,_end                   ,_maxImages);   }
198                 if (p==3) { i=Geti(p,1,0         ,_start                ,_end                   ,_maxImages);   }
199                 if (p==4) { i=Geti(p,1,0         ,_maxImages    ,_maxImages             ,_maxImages);   }
200
201                 if ((_priorityType==0) && (p==0) && (i==-1)) { _priorityType++; _priority=9; }
202                 if ((_priorityType==1) && (p==1) && (i==-1)) { _priorityType++; _priority=9; }
203                 if ((_priorityType==2) && (p==2) && (i==-1)) { _priorityType++; _priority=9; }
204                 if ((_priorityType==3) && (p==3) && (i==-1)) { _priorityType++; _priority=9; }
205         }
206         return i;
207 }
208
209 // ----------------------------------------------------------------------------
210
211 int PriorityLoop::Geti(int id,int step,int start,int actualg,int actuald,int end){
212         int tempig=0;
213         int tempid=0;
214         int i=-2;
215         while (i==-2){
216                 if (_dg[id]==true) { _counterActual[id] = _counterActual[id]+step; }
217                 tempig=actualg - _counterActual[id];
218                 tempid=actuald + _counterActual[id];
219                 if (_dg[id]==true){
220                         if (tempig>=start) { 
221                                 if (_vecIdImage[tempig]==false ) {
222                                         i=tempig;
223                                 }
224                         }
225                 }
226                 if (_dg[id]==false){
227                         if (tempid<=end) { 
228                                 if (_vecIdImage[tempid]==false ) {
229                                         i=tempid;
230                                 }
231                         }
232                 }
233                 if ((tempig<start) && (tempid>end)){
234                         i=-1;
235                 }
236                 _dg[id]=!_dg[id];
237         }
238         return i;
239 }
240 // ----------------------------------------------------------------------------
241 void PriorityLoop::WaitUntil_i_isDone(int i){
242         WaitUntil_segment_isDone(i-_deltaImg,i+_deltaImg,false);
243 }
244 // ----------------------------------------------------------------------------
245 void PriorityLoop::WaitUntil_segment_isDone(int start, int end,bool forceEndLoopRegion){
246         if (forceEndLoopRegion==true) { ForceEndLoopRegion(); }
247         if (_loadingSerie==true){
248                 int s=start,e=end;
249                 if (s<0){
250                         s=0;
251                 }
252                 if (e>_maxImages-1){
253                         e=_maxImages-1;
254                 }
255                 bool ok=false;
256                 while (ok==false){
257                         int i;
258                         ok=true;
259                         for (i=s;i<=e;i++){
260                                 ok = ok && _vecIdImage[i];
261                         }
262                         if (ok==false) { 
263                                 RefreshForce();
264                                 wxThread::Sleep(100); 
265                         }
266                 }
267         }
268 }
269
270 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
272 // ----------------------------------------------------------------------------
273
274 // ----------------------------------------------------------------------------
275 PriorityLoadImageBar::PriorityLoadImageBar(marFilesBase *marfilesbase, BarLoading *bar){
276         _marfilesbase=marfilesbase;
277         _bar=bar;
278 }
279 // ----------------------------------------------------------------------------
280 PriorityLoadImageBar::~PriorityLoadImageBar(){
281 }
282 // ----------------------------------------------------------------------------
283 void PriorityLoadImageBar::Action_i(int i){
284
285         if (_bar!=NULL){
286
287 // EED 09 Oct 2007 
288                 _bar->SetI(i);
289
290                 #if defined(WIN32)
291 //                      _bar->Refresh(false);
292                         //_bar->Paint();
293 //                      Sleep(1);
294                 #else
295 //                      _bar->Refresh(false);
296 //                      _bar->Update();
297 //                      _bar->Paint();
298 //                      usleep(1000);
299                 #endif
300         }
301
302
303         if (_marfilesbase!=NULL){
304                 _marfilesbase->loadImage(i);
305                 vtkImageData *imagedata = _marfilesbase->getVolume()->castVtk();
306                 imagedata->Update();
307         }
308 }
309 // ----------------------------------------------------------------------------
310 void PriorityLoadImageBar::RefreshForce(){
311         _bar->RefreshForce();
312 }
313
314 // ----------------------------------------------------------------------------
315 // ----------------------------------------------------------------------------
316 // ----------------------------------------------------------------------------
317
318 IMPLEMENT_CLASS(BarLoading, wxPanel)
319 BEGIN_EVENT_TABLE(BarLoading, wxPanel)
320          EVT_PAINT(BarLoading::OnPaint)
321 END_EVENT_TABLE()
322
323 static wxMutex *s_mutexProtectingTheGlobalData=NULL;
324
325
326 // ----------------------------------------------------------------------------
327 BarLoading::BarLoading(wxWindow *parent, int w, int h) 
328  :wxPanel(parent,-1,wxDefaultPosition,wxDefaultSize,wxTAB_TRAVERSAL)
329 {
330         _w=w;
331         _h=h;
332         SetSize(_w,_h);
333         SetWindowStyle(wxNO_FULL_REPAINT_ON_RESIZE);
334         _bitmap = new wxBitmap(_w,_h);
335         _flag_bitmap=true;
336         _flag2_bitmap=true;
337
338         if (s_mutexProtectingTheGlobalData!=NULL)
339         {
340                 delete s_mutexProtectingTheGlobalData;
341                 s_mutexProtectingTheGlobalData=NULL;
342         }
343         s_mutexProtectingTheGlobalData = new wxMutex();
344         Reset();
345 }
346
347 // ----------------------------------------------------------------------------
348 BarLoading::~BarLoading()
349 {
350 }
351 // ----------------------------------------------------------------------------
352 void BarLoading::SetI(int i)
353 {
354
355 s_mutexProtectingTheGlobalData->Lock();
356
357         _flag_bitmap=false;
358
359         while (_flag2_bitmap==false){
360                 //sleep force
361 #if defined(WIN32)
362                 Sleep(1);
363 #else
364 //              usleep(1000);
365 #endif
366         }
367
368 /*
369         wxMemoryDC temp_dc;
370         temp_dc.SelectObject( *_bitmap );
371         int x = (_w*i)/_sizeData;
372         int d = (_w/_sizeData)+1;
373         temp_dc.SetBrush(wxBrush( wxColour(0,0,255),wxSOLID  ));
374         temp_dc.SetPen(wxPen( wxColour(0,0,255),1,wxSOLID  ));
375         temp_dc.DrawRectangle(x,0,d,_h);
376 */
377
378        lstInt.push_back(i);
379
380 s_mutexProtectingTheGlobalData->Unlock();
381         _flag_bitmap=true;
382
383 }
384 // ----------------------------------------------------------------------------
385 void BarLoading::RefreshForce()
386 {
387 //      Refresh();
388 //      Update();
389 }
390 // ----------------------------------------------------------------------------
391 void BarLoading::SetSizeData(int sizeData)
392 {
393    _sizeData=sizeData;
394 }
395 // ----------------------------------------------------------------------------
396 void BarLoading::Reset()
397 {
398         if (_flag_bitmap==true){
399                 wxMemoryDC temp_dc;
400                 temp_dc.SelectObject( *_bitmap );
401                 temp_dc.SetBrush(wxBrush( wxColour(255,0,0),wxSOLID  ));
402                 temp_dc.SetPen(wxPen( wxColour(255,0,0),1,wxSOLID  ));
403                 temp_dc.DrawRectangle(0,0,_w,_h);
404                 lstInt.clear();
405         }
406 }
407
408
409 // ----------------------------------------------------------------------------
410 void BarLoading::Paint( )
411 {
412   _flag2_bitmap=false;
413   wxMutexLocker lock(*s_mutexProtectingTheGlobalData);
414
415
416         if (_flag_bitmap==true){
417                 wxMemoryDC temp_dc;
418                 temp_dc.SelectObject( *_bitmap );
419         
420         int i,ii,size=lstInt.size();
421         for (ii=0;ii<size; ii++)
422         {
423                 i=lstInt[ii];
424                 int x = (_w*i)/_sizeData;
425                 int d = (_w/_sizeData)+1;
426                 temp_dc.SetBrush(wxBrush( wxColour(0,0,255),wxSOLID  ));
427                 temp_dc.SetPen(wxPen( wxColour(0,0,255),1,wxSOLID  ));
428                 temp_dc.DrawRectangle(x,0,d,_h);
429         }
430         lstInt.clear();
431
432
433
434
435                 wxPaintDC dc( this );
436                 dc.Blit(0,0, _w, _h, &temp_dc, 0, 0);
437         }
438         _flag2_bitmap=true;//SIL//POR QUE TANTAS VECES LO MISMO??????
439         _flag2_bitmap=true;
440         _flag2_bitmap=true;
441         _flag2_bitmap=true;
442         _flag2_bitmap=true;
443         _flag2_bitmap=true;
444
445
446 }
447
448
449 // ----------------------------------------------------------------------------
450 void BarLoading::OnPaint( wxPaintEvent &WXUNUSED(event) )
451 {
452         Paint();
453 }
454
455
456 // ----------------------------------------------------------------------------
457 // ----------------------------------------------------------------------------
458 // ----------------------------------------------------------------------------
459 // ----------------------------------------------------------------------------
460 /* EED Borrame
461 IMPLEMENT_CLASS(BarRange, wxPanel)
462 BEGIN_EVENT_TABLE(BarRange, wxPanel)
463          EVT_PAINT(BarRange::OnPaint)
464 END_EVENT_TABLE()
465
466
467 BarRange::BarRange(wxWindow *parent, int w, int h)
468  :wxPanel(parent,-1,wxDefaultPosition,wxDefaultSize,wxTAB_TRAVERSAL)
469 {
470         _w=w;
471         _h=h;
472         SetSize(_w,_h);
473         SetWindowStyle(wxNO_FULL_REPAINT_ON_RESIZE);
474         _bitmap = new wxBitmap(_w,_h);
475
476         _start = 0;
477         _max   = 100;
478         _end   = _max;
479 }
480 // ----------------------------------------------------------------------------
481 BarRange::~BarRange()
482 {
483 }
484 // ----------------------------------------------------------------------------
485 void BarRange::SetStart(int i)
486 {
487         _start=i;
488 }
489 // ----------------------------------------------------------------------------
490 void BarRange::SetActual(int i)
491 {
492         _actual=i;
493 }
494 // ----------------------------------------------------------------------------
495 void BarRange::SetEnd(int i)
496 {
497         _end=i;
498 }
499 // ----------------------------------------------------------------------------
500 void BarRange::SetMax(int i)
501 {
502         _max=i;
503 }
504 // ----------------------------------------------------------------------------
505 void BarRange::OnPaint( wxPaintEvent &WXUNUSED(event) )
506 {
507
508         RefreshView();
509
510     wxMemoryDC temp_dc;
511         temp_dc.SelectObject( *_bitmap );
512         wxPaintDC dc( this );
513         dc.Blit(0,0, _w, _h, &temp_dc, 0, 0);
514
515 }
516 // ----------------------------------------------------------------------------
517
518 void BarRange::RefreshView()
519 {
520         wxPoint points[3];
521
522         int px1=(_w*_start)/_max;
523         int px2=(_w*_end)/_max;
524         int px3=(_w*_actual)/_max;
525
526         wxMemoryDC temp_dc;
527         temp_dc.SelectObject( *_bitmap );
528
529         // Background
530         wxColour colourParent =  GetParent()->GetBackgroundColour();
531         temp_dc.SetBrush(wxBrush( colourParent ,wxSOLID  ));
532         temp_dc.SetPen(wxPen( colourParent ,1,wxSOLID  ));
533         temp_dc.DrawRectangle(0,0,_w,_h);
534
535         // Yellow line
536         temp_dc.SetBrush(wxBrush( wxColour(255,0,255),wxSOLID  ));
537         temp_dc.SetPen(wxPen( wxColour(255,0,255),1,wxSOLID  ));
538         temp_dc.DrawRectangle( px1 , 0 , px2-px1 , _h );
539
540         // 2 Start End ( triangles )
541         temp_dc.SetBrush(wxBrush( wxColour(0,0,255),wxSOLID  ));
542         temp_dc.SetPen(wxPen( wxColour(0,0,255),1,wxSOLID  ));
543         points[0].x=0;
544         points[0].y=0;
545         points[1].x=-3;
546         points[1].y=_h;
547         points[2].x=3;
548         points[2].y=_h;
549         temp_dc.DrawPolygon(3,points,px1,0);
550         temp_dc.DrawPolygon(3,points,px2,0);
551
552         // Actual ( black triangles )
553         // 2 Start End ( yellow triangles )
554         temp_dc.SetBrush(wxBrush( wxColour(255,255,0),wxSOLID  ));
555         temp_dc.SetPen(wxPen( wxColour(255,255,0),1,wxSOLID  ));
556         points[0].x = -3;
557         points[0].y = 0;
558         points[1].x = 3;
559         points[1].y = 0;
560         points[2].x = 0;
561         points[2].y = _h;
562         temp_dc.DrawPolygon(3,points,px3,0);
563
564 }
565 // ----------------------------------------------------------------------------
566 void BarRange::RefreshForce()
567 {
568         Refresh();
569         Update();
570 }
571 // ----------------------------------------------------------------------------
572 */
573
574 // ----------------------------------------------------------------------------
575 // ----------------------------------------------------------------------------
576 // ----------------------------------------------------------------------------
577
578
579
580 /*
581 PriorityBar::PriorityBar(BarLoading *bar){
582         _bar = bar;
583 }
584 // ----------------------------------------------------------------------------
585 PriorityBar::~PriorityBar(){
586 }
587 // ----------------------------------------------------------------------------
588 void PriorityBar::Action_i(int i){
589         _bar->SetI(i);
590         _bar->Refresh();
591         wxThread::Sleep(100); 
592 }
593 // ----------------------------------------------------------------------------
594 void PriorityBar::RefreshForce(){
595         _bar->RefreshForce();
596 }
597 */
598
599 // ----------------------------------------------------------------------------
600 // ----------------------------------------------------------------------------
601 // ----------------------------------------------------------------------------
602
603 // ----------------------------------------------------------------------------
604 AdminThreadEED::AdminThreadEED()
605 : m_condAllDone(m_mutexAllDone){
606
607          // the mutex associated with a condition must be initially locked, it will
608     // only be unlocked when we call Wait()
609     m_mutexAllDone.Lock();
610     m_waitingUntilAllDone = FALSE;
611
612 }
613 // ----------------------------------------------------------------------------
614 AdminThreadEED::~AdminThreadEED(){
615         // the mutex must be unlocked before being destroyed
616     m_mutexAllDone.Unlock();
617 }
618
619 // ----------------------------------------------------------------------------
620 // ----------------------------------------------------------------------------
621 // ----------------------------------------------------------------------------
622
623 // ----------------------------------------------------------------------------
624 MyThread::MyThread(PriorityLoop *priorityLoop,AdminThreadEED *adminThreadEED){
625    _priorityLoop        =       priorityLoop;
626    _adminThreadEED      =       adminThreadEED;
627
628 }
629
630 // ----------------------------------------------------------------------------
631 void MyThread::OnExit(){
632     _priorityLoop->StopLoop();
633     wxCriticalSectionLocker locker(_adminThreadEED->m_critsect);
634     wxArrayThread& threads = _adminThreadEED->m_threads;
635     threads.Remove(this);
636
637     if ( threads.IsEmpty() )    {
638         if ( _adminThreadEED->m_waitingUntilAllDone ){
639             _adminThreadEED->m_waitingUntilAllDone = FALSE;
640             wxMutexLocker lock(_adminThreadEED->m_mutexAllDone);
641             _adminThreadEED->m_condAllDone.Signal();
642         }
643     }
644 }
645 // ----------------------------------------------------------------------------
646
647 void *MyThread::Entry(){
648         _priorityLoop->InitLoop();
649         while ( _priorityLoop->IsLoop()==true ){
650         if ( TestDestroy() ){
651             break;
652                 }
653                 _priorityLoop->Iteration();
654     }
655     return NULL;
656 }
657
658
659
660 // ----------------------------------------------------------------------------
661 // ----------------------------------------------------------------------------
662 // ----------------------------------------------------------------------------
663
664
665 // ----------------------------------------------------------------------------
666 LoadRangeImage::LoadRangeImage(wxWindow *parent,marFilesBase *marfilesbase ) 
667 : wxPanel(parent, -1)
668 {
669         _marfilesbase           = marfilesbase;
670         
671     wxPanel *panel              = new wxPanel(this, -1);
672         int wx=550;
673         _bar                            = new BarLoading(panel,wx-10,15);
674         //SIL//_barRange                        = new BarRange(panel,wx-10,5);
675         //SIL//
676         _sl_barRange_ROI        = new mBarRange(panel,wx-10 ,70);
677         _sl_barRange_ROI-> SetOrientation(true);
678         _sl_barRange_ROI -> setVisibleLabels ( true );
679         _sl_barRange_ROI -> setActiveStateTo ( true );  
680         _sl_barRange_ROI -> setDeviceBlitStart (2,2);
681         _sl_barRange_ROI -> setDeviceEndMargin(0);
682         _sl_barRange_ROI -> SetTrianglesHalfWidth(5);
683
684         
685         parent->SetBackgroundColour(wxColour(255,255,255 )) ;
686         panel->SetBackgroundColour(parent-> GetBackgroundColour());
687
688 /*
689         parent->SetBackgroundColour(wxColour(255,0,0 )) ;
690         panel->SetBackgroundColour(wxColour(0,255,0 )) ;*/
691         /*
692         panel->SetBackgroundColour(wxColour(0,0,0 ));
693         parent->SetBackgroundColour(wxColour(255,0,0 ));
694         (parent->GetParent())->SetBackgroundColour(wxColour(0,255,0 )) ;
695         ((parent->GetParent())->GetParent())->SetBackgroundColour(wxColour(0,0,255 )) ;*/
696         
697
698         _sl_barRange_ROI-> setBackgroundColor( parent-> GetBackgroundColour()/*wxColour(255,255,255 )*/);
699         reseteableActual =true;
700
701
702 /*_//SIL//
703         _sldActual                      = new wxSlider(panel,-1,0,0,10000,wxDefaultPosition,wxSize(wx-10,20),wxSL_HORIZONTAL);
704         
705         _btnStart                       = new wxButton(panel,-1,_T("Start:"),wxDefaultPosition, wxSize(70,20) );
706         _btnEnd                         = new wxButton(panel,-1,_T("End:"),wxDefaultPosition, wxSize(70,20) );
707         _textActual                     = new wxStaticText(panel,-1,_T("Actual: 0000000000"));*/
708         _textActual                     = new wxStaticText(panel,-1,_T("  "));
709
710 //EEDxx2.4
711 //      _sldActual      ->SetSize(wx-10,20);
712 //      _btnStart       ->SetSize(70,20);
713 //      _btnEnd         ->SetSize(70,20);
714
715         //SIL//_sizer1a = new wxFlexGridSizer(1);
716         //SIL//_sizer1a->Add(_btnStart                                          );
717         //SIL//_sizer1a->Add(_textActual ,wxALIGN_CENTER,0      );
718         //SIL//_sizer1a->Add(_btnEnd                                            );
719
720 //      _btnStart       ->Show(true);
721 //      _btnEnd         ->Show(true);
722
723         //SIL//_sizer1a->Show(_btnStart, false);
724         //SIL//_sizer1a->Show(_btnEnd, false);
725
726
727 //      _sizer1a = new wxFlexGridSizer(1);
728 //      _sizer1a->Add(_textActual ,wxALIGN_CENTER,0     );
729
730         wxFlexGridSizer *sizer1 = new wxFlexGridSizer(1);
731 //      sizer1->AddGrowableRow(1);
732 //      wxBoxSizer *sizer1 =  new wxBoxSizer(wxVERTICAL );
733         //SIL//sizer1->Add(_sldActual);
734         sizer1->Add(_bar);
735         //SIL//sizer1->Add(_barRange);
736         //SIL//
737         sizer1->Add(_sl_barRange_ROI,1,wxEXPAND,0);
738
739         wxFlexGridSizer *sizer = new wxFlexGridSizer(2);
740 //      sizer -> AddGrowableRow(1);//SIL//
741 //      sizer->Add(_sizer1a, 1, wxALL , 5 );
742         sizer->Add(_textActual);
743         sizer->Add(sizer1,1,wxEXPAND,0);
744
745
746
747         /*wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
748         sizer->Add(_bar, 0, wxEXPAND);  
749         sizer->Add(_sl_barRange_ROI, 0, wxEXPAND);*/
750
751
752 /*//SIL//
753         Connect(_btnStart->GetId()  , wxEVT_COMMAND_BUTTON_CLICKED      , (wxObjectEventFunction) (wxCommandEventFunction) &LoadRangeImage::OnBtnStart          );
754         Connect(_btnEnd->GetId()        , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) (wxCommandEventFunction) &LoadRangeImage::OnBtnEnd                    );
755 */
756         //Connect(_sldActual->GetId() , wxEVT_COMMAND_SLIDER_UPDATED    , (wxObjectEventFunction) (wxEventFunction) (wxScrollEventFunction) &LoadRangeImage::OnSliceActual      );
757         //SIL//
758         Connect(_sl_barRange_ROI->GetId(),wxEVT_TSBAR,(wxObjectEventFunction) (wxCommandEventFunction) &LoadRangeImage::OnBarrange );
759         Connect(_sl_barRange_ROI->GetId(),wxEVT_TSBAR_START,(wxObjectEventFunction) (wxCommandEventFunction)  &LoadRangeImage::OnStartChange_BarRange );
760         Connect(_sl_barRange_ROI->GetId(),wxEVT_TSBAR_END,(wxObjectEventFunction) (wxCommandEventFunction)  &LoadRangeImage::OnEndChange_BarRange );
761         Connect(_sl_barRange_ROI->GetId(),wxEVT_TSBAR_ACTUAL,(wxObjectEventFunction) (wxCommandEventFunction)  &LoadRangeImage::OnActualChange_BarRange );
762         Connect(_sl_barRange_ROI->GetId(),wxEVT_TSBAR_MOVED,(wxObjectEventFunction) (wxCommandEventFunction)  &LoadRangeImage::OnBarMoved_BarRange );
763         Connect(_sl_barRange_ROI->GetId(),wxEVT_SELECTION_END,(wxObjectEventFunction) (wxCommandEventFunction)  &LoadRangeImage::OnSelectionEnd_BarRange );
764
765         // EED
766         _timer=NULL;
767 #if defined(WIN32)
768         //...
769 #else
770     _timer = new wxTimer(this);
771         _timer->Start(100);
772     Connect( _timer->GetId(), wxEVT_TIMER , (wxObjectEventFunction) (wxTimerEventFunction)  &LoadRangeImage::OnTimer );
773 #endif
774
775
776         panel->SetSizer(sizer);
777         panel->SetAutoLayout( TRUE );
778
779         this->SetSize(wx,75);
780         panel->SetSize(wx,75);
781
782         _mythread                       = NULL;
783         _priorityLoadImageBar           = new PriorityLoadImageBar(_marfilesbase,_bar);
784         _adminThreadEED                 = new AdminThreadEED();
785 }
786 // ----------------------------------------------------------------------------
787 LoadRangeImage::~LoadRangeImage()
788 {
789         if (_timer!=NULL){
790                 _timer->Stop();
791                 delete _timer;
792         }
793         _Stop();
794 }
795
796 // ----------------------------------------------------------------------------
797 void LoadRangeImage::ResetLoadRegion()
798 {
799         int s,e,a;
800         s=_startSlice;
801         e=_endSlice;
802         //SIL//a=_sldActual->GetValue();
803         //a=_sl_barRange_ROI->GetActual();
804         a = _toShowSlice;
805         if ((a<s) || (a>e))
806         {
807                 s=0;
808                 e=_maxZ;
809         } 
810         _priorityLoadImageBar->SetStartEndActualMax(s,e,a,0,_maxZ);
811 }
812
813 // ----------------------------------------------------------------------------
814 void LoadRangeImage::Start(  )
815 {
816
817     vtkImageData *vol = _marfilesbase->getVolume( )->castVtk();
818         int dim[3];
819         vol->GetDimensions(dim);
820         _maxZ=dim[2]-1;
821
822         _startSlice     =       0;
823         _endSlice       =       _maxZ;
824
825         this->SetStartSlice(_startSlice);
826         this->SetEndSlice(_endSlice);
827
828
829         _bar ->                 SetSizeData(_maxZ);
830 //SIL// Para quitar luego als siguientes dos lineas
831         /*_sldActual -> SetRange(0, _maxZ);
832         _sldActual ->   SetValue( _maxZ/2 );*/
833
834         //SIL//
835         _sl_barRange_ROI -> setRepresentedValues (0,_maxZ);
836         _sl_barRange_ROI -> SetActual(_maxZ/2);
837         _sl_barRange_ROI -> RefreshForce();     
838
839
840         SetStartSlice( 0 );
841         SetEndSlice( _maxZ );
842         _toShowSlice = _sl_barRange_ROI->GetActual();
843         RefreshSlice();
844
845 //EEDx44
846         _marfilesbase->ResetLstFileNotReaded();
847
848         _Start();
849 }
850 // ----------------------------------------------------------------------------
851 void LoadRangeImage::_Start()
852 {
853
854         ResetLoadRegion();
855         _mythread = new MyThread (_priorityLoadImageBar,_adminThreadEED);
856         _mythread->Create();
857         wxCriticalSectionLocker enter(_adminThreadEED->m_critsect);
858         _adminThreadEED->m_threads.Add(_mythread);
859         if ( _mythread->Run() != wxTHREAD_NO_ERROR ){
860                 wxLogError(wxT("Can't start thread!"));
861         }
862 }
863 // ----------------------------------------------------------------------------
864 void LoadRangeImage::_Stop(  )
865 {
866
867     _adminThreadEED->m_critsect.Enter();
868     // stop the last thread
869     if ( _adminThreadEED->m_threads.IsEmpty() ){
870         //wxLogError(wxT("No thread to stop!"));
871        _adminThreadEED->m_critsect.Leave();
872     } else {
873         wxThread *_mythread = _adminThreadEED->m_threads.Last();
874         // it's important to leave critical section before calling Delete()
875         // because delete will (implicitly) call OnExit() which also tries
876         // to enter the same crit section - would dead lock.
877         _adminThreadEED->m_critsect.Leave();
878         _mythread->Delete();
879     }
880
881 }
882
883 // ----------------------------------------------------------------------------
884 /*
885 void LoadRangeImage::OnBtnRestart( wxCommandEvent& event ){
886         _priorityLoadImageBar->WaitUntil_segment_isDone( _sldStart->GetValue(),_sldEnd->GetValue() );
887
888         OnBtnStop(event);
889         _bar->Reset();
890         _bar->Refresh();
891 }
892 */
893 // ----------------------------------------------------------------------------
894 void LoadRangeImage::OnSliceActual(wxScrollEvent& event)
895 {
896         //SIL// metodo para quitar
897         _toShowSlice = _sl_barRange_ROI->GetActual();
898         ResetLoadRegion();
899         RefreshSlice();
900 }
901
902 // ----------------------------------------------------------------------------
903
904 void LoadRangeImage::OnBtnStart( wxCommandEvent &  event )
905 {
906         //SIL// metodo para quitar
907         /*SetStartSlice( _sldActual->GetValue() );
908         ResetLoadRegion();
909         RefreshSlice();*/
910 }
911
912 // ----------------------------------------------------------------------------
913 void LoadRangeImage::OnBtnEnd( wxCommandEvent &  event )
914 {
915         //SIL// metodo para quitar
916         /*SetEndSlice( _sldActual->GetValue() );
917         ResetLoadRegion();
918         RefreshSlice();*/
919 }
920 // ----------------------------------------------------------------------------
921 void LoadRangeImage::SetStartEnd( int start, int end )
922 {
923         SetStartSlice( start );
924         SetEndSlice( end );
925         ResetLoadRegion();
926         RefreshSlice();
927 }
928
929 // ----------------------------------------------------------------------------
930 void LoadRangeImage::Reset( )
931 {
932         int delta= (int) (_maxZ*0.1);
933         SetStartSlice( 0 + delta );
934         SetEndSlice( _maxZ - delta );
935
936 //EEDxx2.4
937 //      _btnStart       ->Show(true);
938 //      _btnEnd         ->Show(true);
939
940         //SIL//_sizer1a->Show(_btnStart, true );
941         //SIL//_sizer1a->Show(_btnEnd   , true );
942 //    _sizer1a->Layout();       
943         RefreshSlice();
944 }
945
946 // ----------------------------------------------------------------------------
947 void LoadRangeImage::SetStartSlice(int value)
948 {
949         if (value<_endSlice){
950                 _startSlice     = value;
951                 /*wxString txt;
952                 txt.Printf(_T("%d"),_startSlice);
953                 _textActual->SetLabel(txt);*/
954                 
955                 //SIL// para quitar las siguientes 4 lineas
956                 /*
957                 _btnStart->SetLabel(_T("Start:  ")+txt);
958                 _barRange->SetMax(_maxZ);
959                 _barRange->SetStart(_startSlice);
960                 _barRange->RefreshForce();*/
961
962                 //SIL//
963                 _sl_barRange_ROI->SetMax(_maxZ);
964                 _sl_barRange_ROI->SetStart(_startSlice);
965                 _sl_barRange_ROI->RefreshForce(); 
966         }
967 }
968 // ----------------------------------------------------------------------------
969 void LoadRangeImage::SetEndSlice(int value)
970 {
971         if (value>_startSlice){
972                 _endSlice       = value;
973                 /*wxString txt;
974                 txt.Printf(_T("%d"),_endSlice);
975                 _textActual->SetLabel(txt);*/
976                 //SIL//_btnEnd->SetLabel(_T("End:  ")+txt);
977                 _sl_barRange_ROI->SetEnd(_endSlice);
978                 _sl_barRange_ROI->RefreshForce(); 
979
980         }
981 }
982
983 // ----------------------------------------------------------------------------
984 void LoadRangeImage::RefreshSlice()
985 {
986 //SIL// _priorityLoadImageBar->WaitUntil_i_isDone( _sldActual->GetValue() );
987         _priorityLoadImageBar->WaitUntil_i_isDone( _toShowSlice );
988
989         wxCommandEvent newevent(wxEVT_COMMAND_MENU_SELECTED,12122);
990         this->GetParent()->ProcessEvent(newevent);
991 /* //SIL//
992         wxString text;  
993         //SIL//text.Printf(_T("Actual: %d "), _sldActual->GetValue() );
994         text.Printf(_T("Start: %d\n Actual: %d\n End: %d"), _sl_barRange_ROI->GetStart(),_sl_barRange_ROI->GetActual(),_sl_barRange_ROI->GetEnd() );
995         _textActual->SetLabel(text);*/
996         
997         //SIL// Para borrar las lineas de barrange
998         /*_barRange->SetMax(_maxZ);
999         _barRange->SetActual( _sldActual->GetValue() );
1000         _barRange->SetStart(_startSlice);
1001         _barRange->SetEnd(_endSlice);
1002         _barRange->RefreshForce();*/
1003
1004         //SIL//
1005         // EED
1006 //      _sl_barRange_ROI->SetMax(_maxZ);
1007         //_sl_barRange_ROI->SetActual(  _sldActual->GetValue() );
1008 //      _sl_barRange_ROI->SetStart(_startSlice);
1009 //      _sl_barRange_ROI->SetEnd(_endSlice);
1010         _sl_barRange_ROI->RefreshForce();
1011
1012 //EED 28 Fev 2008
1013 //      _bar->Refresh();
1014 //      _bar->Update();
1015
1016    Refresh(false);
1017 //      _imageViewerWidget->SetZSlice( _sldActual->GetValue()  );
1018 //      _imageViewerWidget->Render();
1019 }
1020 // ----------------------------------------------------------------------------
1021 void LoadRangeImage::CallBackOnMouseWheel( wxMouseEvent& event )
1022 {
1023         /* //SIL ImageViewer no existe//int actual = _imageViewerWidget->GetZSlice();
1024         if (actual<_sl_barRange_ROI->GetActual()) {
1025                 actual = _sl_barRange_ROI->GetActual();
1026         }
1027         if (actual>_sl_barRange_ROI->GetEnd()) {
1028                 actual = _sl_barRange_ROI->GetEnd();
1029         }
1030         _sl_barRange_ROI->SetActual( actual );
1031         ResetLoadRegion();
1032         RefreshSlice();
1033 */
1034         /*int actual = _imageViewerWidget->GetZSlice();
1035         if (actual<_sldStart->GetValue()) {
1036                 actual = _sldStart->GetValue();
1037         }
1038         if (actual>_sldEnd->GetValue()) {
1039                 actual = _sldEnd->GetValue();
1040         }
1041         _sldActual->SetValue( actual );
1042         ResetLoadRegion();
1043         RefreshSlice();*/
1044
1045 }
1046 // ----------------------------------------------------------------------------
1047 void LoadRangeImage::ForceToFinisReadActiveImages()
1048 {
1049         int s=_startSlice;
1050         int e=_endSlice;
1051
1052         _priorityLoadImageBar->WaitUntil_segment_isDone(s,e,true);
1053
1054
1055 //EEDx44
1056         std::string msgLstFile( _marfilesbase->GetMsgLstFile() );
1057         if ( msgLstFile!="")
1058         {
1059                 wxDialog *dialog = new wxDialog( this , -1, wxString(_T("The following files doesn't exist:")), wxDefaultPosition, wxSize(550,300),  wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER );                        
1060                 wxTextCtrl *textCtrl = new wxTextCtrl(dialog, -1, wxString (msgLstFile.c_str(), wxConvUTF8) ,wxDefaultPosition , wxSize(100,100) , wxTE_MULTILINE | wxTE_READONLY );
1061                 textCtrl->SetSelection(0,0);
1062                 wxBoxSizer *sizer       = new wxBoxSizer(wxVERTICAL  );
1063                 sizer->Add( textCtrl , 1, wxALL|wxEXPAND, 0);
1064                 dialog->SetAutoLayout(true);
1065                 dialog->SetSizer( sizer );      // use the sizer for layout
1066                 dialog->Layout(); 
1067 //EEDxx2.4
1068 //              dialog->FitInside();
1069                 dialog->ShowModal();
1070         }
1071
1072
1073
1074
1075 }
1076 // ----------------------------------------------------------------------------
1077 void LoadRangeImage::GetROI( int extent[6] )
1078 {
1079         extent[4]=_startSlice;
1080         extent[5]=_endSlice;
1081 }
1082 // ----------------------------------------------------------------------------
1083 void LoadRangeImage::SetActualSlice( int actual )
1084 {
1085
1086         if ( actual > _endSlice ){
1087                 actual=_endSlice;
1088         }
1089         if ( actual < _startSlice ){
1090                 actual = _startSlice;
1091         }
1092
1093         //SIL//_sldActual->SetValue( actual  );//SIL// para quitar esta linea
1094         //SIL//
1095         _sl_barRange_ROI->SetActual( actual);//de pronto no se necesita aca
1096         _toShowSlice=actual;
1097
1098         ResetLoadRegion();
1099         RefreshSlice();
1100 }
1101 // ----------------------------------------------------------------------------
1102 int LoadRangeImage::GetActualSlice( )
1103 {
1104         return _toShowSlice;/*_sl_barRange_ROI->GetActual()*/; 
1105 }
1106 // ----------------------------------------------------------------------------
1107
1108 void  LoadRangeImage::ResetActualSlice( )
1109 {
1110         if( reseteableActual )
1111                 _toShowSlice = _sl_barRange_ROI->GetActual();
1112 }
1113
1114 //------------------------------------------------------------------------
1115 // Events captured form the barrange used configuring the ROI
1116 //------------------------------------------------------------------------
1117 void  LoadRangeImage::OnBarrange(wxCommandEvent& event)
1118 {
1119         
1120 }
1121 void  LoadRangeImage::OnActualChange_BarRange(wxCommandEvent& event)
1122 {
1123         //reseteableActual =true;
1124         _toShowSlice = _sl_barRange_ROI->GetActual();
1125         ResetLoadRegion();
1126         RefreshSlice(); 
1127 }
1128 void  LoadRangeImage::OnStartChange_BarRange(wxCommandEvent& event)
1129 {       
1130         reseteableActual =false;
1131         _toShowSlice = _sl_barRange_ROI->GetStart();
1132         SetStartSlice( _sl_barRange_ROI->GetStart() );
1133         ResetLoadRegion();
1134         RefreshSlice();
1135 }
1136
1137 void  LoadRangeImage::OnEndChange_BarRange(wxCommandEvent& event)
1138 {
1139         reseteableActual =false;
1140         _toShowSlice = _sl_barRange_ROI->GetEnd();
1141         SetEndSlice( _sl_barRange_ROI->GetEnd() );
1142         ResetLoadRegion();
1143         RefreshSlice();
1144 }
1145 void  LoadRangeImage:: OnBarMoved_BarRange (wxCommandEvent& event)
1146 {
1147         SetEndSlice( _sl_barRange_ROI->GetEnd() );
1148         SetStartSlice( _sl_barRange_ROI->GetStart() );
1149         ResetLoadRegion();
1150         RefreshSlice();
1151
1152 }
1153 void  LoadRangeImage:: OnSelectionEnd_BarRange (wxCommandEvent& event)
1154 {
1155         reseteableActual = true;
1156 }
1157
1158 // ---  Timer ------
1159 void LoadRangeImage:: OnTimer(wxTimerEvent& event)
1160 {
1161 //  EED  used in linux  (see constructor)
1162         _bar->Refresh(false);
1163         _bar->Update();
1164         RefreshSlice();
1165 }
1166
1167
1168 // ----------------------------------------------------------------------------
1169 // ----------------------------------------------------------------------------
1170 // ----------------------------------------------------------------------------
1171
1172