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