PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
QPlusConfigFileSaverDialog.cxx
Go to the documentation of this file.
1 /*=Plus=header=begin======================================================
2  Program: Plus
3  Copyright (c) Laboratory for Percutaneous Surgery. All rights reserved.
4  See License.txt for details.
5 =========================================================Plus=header=end*/
6 
7 // Local includes
8 #include "igsioCommon.h"
10 
11 // VTK includes
12 #include <vtkXMLUtilities.h>
13 #include <vtkXMLDataElement.h>
14 
15 // Qt includes
16 #include <QFileDialog>
17 #include <QSettings>
18 #include <QString>
19 
20 //-----------------------------------------------------------------------------
22  : QDialog(aParent)
23 {
24  ui.setupUi(this);
25 
26  connect(ui.pushButton_OpenDestinationDirectory, SIGNAL(clicked()), this, SLOT(OpenDestinationDirectoryClicked()));
27  connect(ui.pushButton_Save, SIGNAL(clicked()), this, SLOT(SaveClicked()));
28 
29  SetDestinationDirectory(vtkPlusConfig::GetInstance()->GetDeviceSetConfigurationDirectory());
30 
32 }
33 
34 //-----------------------------------------------------------------------------
36 {
37 }
38 
39 //-----------------------------------------------------------------------------
41 {
42  LOG_TRACE("QPlusConfigFileSaverDialog::OpenDestinationDirectoryClicked");
43 
44  // Directory open dialog for selecting configuration directory
45  QString dirName = QFileDialog::getExistingDirectory(NULL, QString(tr("Select destination directory")), m_DestinationDirectory);
46  if (dirName.isNull())
47  {
48  return;
49  }
50 
51  this->SetDestinationDirectory(dirName.toStdString());
52 
53  ui.lineEdit_DestinationDirectory->setText(dirName);
54  ui.lineEdit_DestinationDirectory->setToolTip(dirName);
55 }
56 
57 //-----------------------------------------------------------------------------
58 void QPlusConfigFileSaverDialog::SetDestinationDirectory(const std::string& aDirectory)
59 {
60  LOG_TRACE("QPlusConfigFileSaverDialog::SetDestinationDirectory(" << aDirectory << ")");
61 
62  m_DestinationDirectory = QString::fromStdString(aDirectory);
63 
64  ui.lineEdit_DestinationDirectory->setText(m_DestinationDirectory);
65  ui.lineEdit_DestinationDirectory->setToolTip(m_DestinationDirectory);
66 }
67 
68 //-----------------------------------------------------------------------------
70 {
71  LOG_TRACE("QPlusConfigFileSaverDialog::ReadConfiguration");
72 
73  // Find Device set element
74  vtkXMLDataElement* dataCollection = vtkPlusConfig::GetInstance()->GetDeviceSetConfigurationData()->FindNestedElementWithName("DataCollection");
75  if (dataCollection == NULL)
76  {
77  LOG_ERROR("No DataCollection element is found in the XML tree!");
78  return PLUS_FAIL;
79  }
80 
81  vtkXMLDataElement* deviceSet = dataCollection->FindNestedElementWithName("DeviceSet");
82  if (deviceSet == NULL)
83  {
84  LOG_ERROR("No DeviceSet element is found in the XML tree!");
85  return PLUS_FAIL;
86  }
87 
88  // Get name and description
89  bool isEqual(true);
90  if (igsioCommon::XML::SafeCheckAttributeValueInsensitive(*deviceSet, "Name", "", isEqual) == PLUS_FAIL || isEqual)
91  {
92  LOG_WARNING("Name attribute cannot be found in DeviceSet element!");
93  return PLUS_FAIL;
94  }
95 
96  if (igsioCommon::XML::SafeCheckAttributeValueInsensitive(*deviceSet, "Description", "", isEqual) == PLUS_FAIL || isEqual)
97  {
98  LOG_WARNING("Description attribute cannot be found in DeviceSet element!");
99  return PLUS_FAIL;
100  }
101 
102  // Set text field values
103  ui.lineEdit_DeviceSetName->setText(deviceSet->GetAttribute("Name"));
104  ui.textEdit_Description->setText(deviceSet->GetAttribute("Description"));
105 
106  return PLUS_SUCCESS;
107 }
108 
109 //-----------------------------------------------------------------------------
111 {
112  LOG_TRACE("QPlusConfigFileSaverDialog::SaveClicked");
113 
114  // Get root element
115  vtkXMLDataElement* configRootElement = vtkPlusConfig::GetInstance()->GetDeviceSetConfigurationData();
116  if (configRootElement == NULL)
117  {
118  LOG_ERROR("No configuration XML found!");
119  return;
120  }
121 
122  // Find Device set element
123  vtkXMLDataElement* dataCollection = configRootElement->FindNestedElementWithName("DataCollection");
124  if (dataCollection == NULL)
125  {
126  LOG_ERROR("No DataCollection element is found in the XML tree!");
127  return;
128  }
129 
130  vtkXMLDataElement* deviceSet = dataCollection->FindNestedElementWithName("DeviceSet");
131  if (deviceSet == NULL)
132  {
133  LOG_ERROR("No DeviceSet element is found in the XML tree!");
134  return;
135  }
136  // Set name and description to XML
137  deviceSet->SetAttribute("Name", ui.lineEdit_DeviceSetName->text().toStdString().c_str());
138  deviceSet->SetAttribute("Description", ui.textEdit_Description->toPlainText().toStdString().c_str());
139 
140  // Display file save dialog and save XML
141  QString filter = QString(tr("XML files ( *.xml );;"));
142  QString destinationFile = QString("%1/%2").arg(m_DestinationDirectory).arg(vtkPlusConfig::GetInstance()->GetNewDeviceSetConfigurationFileName().c_str());
143  QString fileName = QFileDialog::getSaveFileName(NULL, tr("Save result configuration XML"), destinationFile, filter);
144 
145  if (!fileName.isNull())
146  {
147  igsioCommon::XML::PrintXML(fileName.toStdString().c_str(), vtkPlusConfig::GetInstance()->GetDeviceSetConfigurationData());
148  LOG_INFO("Device set configuration saved as '" << fileName.toStdString() << "'");
149  }
150 
151  accept();
152 }
igsioStatus PlusStatus
Definition: PlusCommon.h:40
#define PLUS_FAIL
Definition: PlusCommon.h:43
static vtkPlusConfig * GetInstance()
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
void SetDestinationDirectory(const std::string &aDirectory)
virtual vtkXMLDataElement * GetDeviceSetConfigurationData()