File: vtkXMLVVSelectionFrameLayoutManagerReader.cxx

package info (click to toggle)
volview 3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 25,204 kB
  • sloc: cpp: 132,585; ansic: 11,612; tcl: 236; sh: 64; makefile: 25; xml: 8
file content (175 lines) | stat: -rw-r--r-- 6,639 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*=========================================================================

  Copyright (c) Kitware, Inc.
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/VolViewCopyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "XML/vtkXMLVVSelectionFrameLayoutManagerReader.h"

#include "vtkObjectFactory.h"
#include "vtkVVSelectionFrameLayoutManager.h"
#include "vtkXMLDataElement.h"
#include "vtkVVSelectionFrame.h"
#include "vtkKWApplication.h"
#include "vtkKW2DRenderWidget.h"

#include "XML/vtkXMLVVSelectionFrameLayoutManagerWriter.h"
#include "XML/vtkXMLVVSelectionFrameReader.h"

vtkStandardNewMacro(vtkXMLVVSelectionFrameLayoutManagerReader);
vtkCxxRevisionMacro(vtkXMLVVSelectionFrameLayoutManagerReader, "$Revision: 1.10 $");

//----------------------------------------------------------------------------
const char* vtkXMLVVSelectionFrameLayoutManagerReader::GetRootElementName()
{
  return "VVSelectionFrameLayoutManager";
}

//----------------------------------------------------------------------------
int vtkXMLVVSelectionFrameLayoutManagerReader::Parse(vtkXMLDataElement *elem)
{
  if (!this->Superclass::Parse(elem))
    {
    return 0;
    }

  vtkVVSelectionFrameLayoutManager *obj = 
    vtkVVSelectionFrameLayoutManager::SafeDownCast(this->Object);
  if (!obj)
    {
    vtkWarningMacro(<< "The VVSelectionFrameLayoutManager is not set!");
    return 0;
    }

  // Get Attributes

  int old_reorg = obj->GetReorganizeWidgetPositionsAutomatically();
  obj->SetReorganizeWidgetPositionsAutomatically(0);

  // Set the resolution to an empty screen, so that we are sure repacking
  // will occur. Since the position of each frame is set explicitly in the
  // loop below, loading a session, closing the data (i.e. unpack), then
  // reloading it would not repack anything.

  int res[2] = {0, 0};
  int origin[2] = {0, 0};
  obj->SetResolutionAndOrigin(res, origin);

  // Get nested elements

  vtkXMLDataElement *nested_elem;

  // Selection Frames

  int pos_changed = 0;
  nested_elem = elem->FindNestedElementWithName(
    vtkXMLVVSelectionFrameLayoutManagerWriter::GetSelectionFramesElementName());
  if (nested_elem)
    {
    int idx, nb_container_elems = nested_elem->GetNumberOfNestedElements();
    for (idx = 0; idx < nb_container_elems; idx++)
      {
      vtkXMLDataElement *container_elem = nested_elem->GetNestedElement(idx);
      if (!strcmp(container_elem->GetName(), 
                  vtkXMLVVSelectionFrameLayoutManagerWriter::GetSelectionFrameContainerElementName()))
        {
        const char *tag = container_elem->GetAttribute("Tag");
        const char *group = container_elem->GetAttribute("Group");
        int pos[2];
        if (tag && group && 
            container_elem->GetVectorAttribute("Position", 2, pos) == 2)
          {
          vtkVVSelectionFrame *sel = vtkVVSelectionFrame::SafeDownCast(
            obj->GetWidgetWithTagAndGroup(tag, group));
          // Backward compatibility: the tag here was created by
          // vtkVVDataItemVolume::CreateRenderWidget, according to the "scope"
          // of the data (medical or scientific). Sadly, the scope is
          // detected from the file type: if that test fails, the scope
          // ends up different, all selection frames are created with 
          // different tags (i.e. "Axial" instead of "X-Y" for example), 
          // and no match is found.
          // The scope is now stored in the session, but let's try harder.
          if (!sel)
            {
            int orient = vtkKW2DRenderWidget::
              GetSliceOrientationFromMedicalOrientationString(tag);
            if (orient >= vtkKW2DRenderWidget::SLICE_ORIENTATION_YZ)
              {
              // Found tag as a default orientation string, try medical tag
              tag = vtkKW2DRenderWidget::
                GetSliceOrientationAsDefaultOrientationString(orient);
              }
            else
              {
              orient = vtkKW2DRenderWidget::
                GetSliceOrientationFromDefaultOrientationString(tag);
              if (orient >= vtkKW2DRenderWidget::SLICE_ORIENTATION_YZ)
                {
                // Found tag as a medical orientation string, try default tag
                tag = vtkKW2DRenderWidget::
                  GetSliceOrientationAsMedicalOrientationString(orient);
                }
              }
            sel = vtkVVSelectionFrame::SafeDownCast(
              obj->GetWidgetWithTagAndGroup(tag, group));
            }
          if (sel)
            {
            vtkXMLObjectReader *xmlr = sel->GetNewXMLReader();
            xmlr->ParseInElement(container_elem);
            xmlr->Delete();
            pos_changed += obj->SetImmediateWidgetPosition(sel, pos);
            }
          }
        }
      }
    }
  
  // What's going on here is that new selection frames have been added, and 
  // this usually triggers a reorganization in the manager *asynchronously*,
  // i.e. when the app becomes idle. So even if we set the resolution below
  // the reorganization code will be called later on (at idle time) and
  // the resolution will be re-adjusted automatically to fit *all* widgets.
  // To bypass that, let's just call the callback manually, this will perform
  // the reorganisation and cancel the timer that was going to schedule the
  // reorg. But first, make sure we start from a (0,0) layout, as the loop
  // above may have change the resolution already to a value identical
  // to the one we are aiming at but *before* each widget had a valid position
  // assigned (the callback would then be useless).

  obj->SetResolutionAndOrigin(res, origin);
  obj->NumberOfWidgetsHasChangedCallback();

  // Get Attributes

  int ival;

  obj->GetResolution(res);
  elem->GetVectorAttribute("Resolution", 2, res);

  obj->GetOrigin(origin);
  elem->GetVectorAttribute("Origin", 2, origin);

  obj->SetResolutionAndOrigin(res, origin);

  obj->SetReorganizeWidgetPositionsAutomatically(old_reorg);

  if (elem->GetScalarAttribute("ReorganizeWidgetPositionsAutomatically", ival))
    {
    obj->SetReorganizeWidgetPositionsAutomatically(ival);
    }


  return 1;
}

//----------------------------------------------------------------------------
void vtkXMLVVSelectionFrameLayoutManagerReader::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
}