fw4spl
PatientEditor.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "uiMedDataQt/widget/PatientEditor.hpp"
8 
9 #include "uiMedDataQt/constants.hpp"
10 
11 #include <fwData/String.hpp>
12 
13 #include <fwMedData/Patient.hpp>
14 #include <fwMedData/Series.hpp>
15 
16 #include <fwMedDataTools/functions.hpp>
17 
18 #include <boost/algorithm/string.hpp>
19 
20 #include <QComboBox>
21 #include <QFormLayout>
22 #include <QGroupBox>
23 #include <QLineEdit>
24 #include <QPalette>
25 
26 namespace uiMedDataQt
27 {
28 namespace widget
29 {
30 
31 //-----------------------------------------------------------------------------
32 
33 PatientEditor::PatientEditor(QWidget* parent) :
34  QWidget(parent)
35 {
36  m_txtName = new QLineEdit();
37 
38  m_txtBirthdate = new QLineEdit();
39 
40  m_cbSex = new QComboBox();
41  m_cbSex->addItem(QString::fromStdString("Male"));
42  m_cbSex->addItem(QString::fromStdString("Female"));
43  m_cbSex->addItem(QString::fromStdString("Unknown"));
44 
45  QFormLayout* layout = new QFormLayout();
46  layout->addRow(tr("&Name"), m_txtName);
47  layout->addRow(tr("&Birthdate"), m_txtBirthdate);
48  layout->addRow(tr("&Sex"), m_cbSex);
49 
50  QObject::connect(m_txtName, SIGNAL(textChanged(const QString &)), this, SLOT(onNameChanged(const QString &)));
51  QObject::connect(m_txtBirthdate, SIGNAL(textChanged(const QString &)), this, SLOT(onBirthChanged(const QString &)));
52 
53  QGroupBox* group = new QGroupBox(tr("Patient"));
54  group->setLayout(layout);
55  QVBoxLayout* topLayout = new QVBoxLayout();
56  topLayout->addWidget(group);
57  this->setLayout(topLayout);
58 }
59 
60 //-----------------------------------------------------------------------------
61 
63 {
64  QObject::disconnect(m_txtName, SIGNAL(textChanged(QString)), this, SLOT(onNameChanged));
65  QObject::disconnect(m_txtBirthdate, SIGNAL(textChanged(QString)), this, SLOT(onBirthdateChanged));
66 }
67 
68 //-----------------------------------------------------------------------------
69 
71 {
72  return !m_txtName->text().trimmed().isEmpty()
73  && m_txtName->text().trimmed().toStdString() != s_NEW_PATIENT_TEXT
74  && !m_txtBirthdate->text().trimmed().isEmpty();
75 }
76 
77 //-----------------------------------------------------------------------------
78 
79 void PatientEditor::onNameChanged(const QString& text)
80 {
81  if(m_txtName->text().trimmed().isEmpty() || m_txtName->text().trimmed().toStdString() == s_NEW_PATIENT_TEXT)
82  {
83  m_paletteName.setColor(QPalette::Base, QColor(Qt::red));
84  }
85  else
86  {
87  m_paletteName.setColor(QPalette::Base, QColor(Qt::white));
88  }
89 
90  m_txtName->setPalette(m_paletteName);
91 }
92 
93 //-----------------------------------------------------------------------------
94 
95 void PatientEditor::onBirthChanged(const QString& text)
96 {
97  if(m_txtBirthdate->text().trimmed().isEmpty())
98  {
99  m_paletteBirthdate.setColor(QPalette::Base, QColor(Qt::red));
100  }
101  else
102  {
103  m_paletteBirthdate.setColor(QPalette::Base, QColor(Qt::white));
104  }
105 
106  m_txtBirthdate->setPalette(m_paletteBirthdate);
107 }
108 
109 //-----------------------------------------------------------------------------
110 
112 {
113  ::fwMedData::Patient::sptr srcPatient = m_series->getPatient();
114  ::fwMedData::Patient::sptr patient = ::fwMedData::Patient::New();
115  patient->setName(m_txtName->text().trimmed().toStdString());
116  patient->setBirthdate(m_txtBirthdate->text().trimmed().toStdString());
117  patient->setPatientId(srcPatient->getPatientId());
118 
119  const std::string sex = m_cbSex->currentText().toStdString();
120  if(sex == "Male")
121  {
122  patient->setSex("M");
123  }
124  else if(sex == "Female")
125  {
126  patient->setSex("F");
127  }
128  else
129  {
130  patient->setSex("O");
131  }
132 
133  // Tell to not compare patient sex if value is not managed
134  std::string srcPatientSex = srcPatient->getSex();
135  ::boost::algorithm::trim(srcPatientSex);
136  const bool compareSex = (srcPatientSex == "M" || srcPatientSex == "F" || srcPatientSex == "O");
137 
138  std::string name = patient->getName();
139  ::boost::algorithm::trim(name);
140  std::string birth = patient->getBirthdate();
141  ::boost::algorithm::trim(birth);
142  std::string sexStr = patient->getSex();
143  ::boost::algorithm::trim(sexStr);
144 
145  const bool same = srcPatient->getName() == name
146  && srcPatient->getBirthdate() == birth
147  && (compareSex ? (srcPatientSex == sexStr) : true)
148  && srcPatient->getPatientId() == patient->getPatientId();
149 
150  ::fwData::String::sptr fieldPatient = srcPatient->getField< ::fwData::String >(s_NEW_PATIENT_FIELD_NAME);
151  if(fieldPatient || !same)
152  {
153  patient->setPatientId(::fwMedDataTools::generatePatientId());
154  }
155 
156  return patient;
157 }
158 
159 //-----------------------------------------------------------------------------
160 
162 {
163  SLM_ASSERT("Given series is null", series);
164  m_series = series;
165 
166  ::fwMedData::Patient::sptr patient = m_series->getPatient();
167  SLM_ASSERT("Given series patient is null", patient);
168 
169  m_txtName->setText(QString::fromStdString(patient->getName()).trimmed());
170  m_txtBirthdate->setText(QString::fromStdString(patient->getBirthdate()).trimmed());
171 
172  // force signal (manage empty text case)
173  this->onNameChanged(m_txtName->text());
174  this->onBirthChanged(m_txtBirthdate->text());
175 
176  std::string sex = patient->getSex();
177  ::boost::algorithm::trim(sex);
178 
179  if(sex == "M")
180  {
181  m_cbSex->setCurrentIndex(0);
182  }
183  else if(sex == "F")
184  {
185  m_cbSex->setCurrentIndex(1);
186  }
187  else
188  {
189  m_cbSex->setCurrentIndex(2);
190  SLM_WARN_IF("Unknown patient sex for value '" + patient->getSex() + "'", patient->getSex() != "O");
191  }
192 }
193 
194 //-----------------------------------------------------------------------------
195 
196 } // namespace widget
197 } // namespace uiMedDataQt
198 
The namespace uiMedDataQt contains editors for medical data.
#define SPTR(_cls_)
Widget to edit fwMedData::Patient information.
UIMEDDATAQT_API bool isValid() const
Returns true if the patient information given through the editor is valid.
UIMEDDATAQT_API std::shared_ptr< ::fwMedData::Patient > getPatient() const
Returns the patient object created by this editor.
UIMEDDATAQT_API ~PatientEditor()
Destructor.
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
FWMEDDATATOOLS_API std::string generatePatientId()
Generates a random Dicom Patient ID using GDCM. It must be at most 64 char long and non null...
UIMEDDATAQT_API PatientEditor(QWidget *parent=0)
Constructor.
This class contains an std::string value.
UIMEDDATAQT_API void setSeries(std::shared_ptr< ::fwMedData::Series > series)
Set referring series.
#define SLM_WARN_IF(message, cond)
Definition: spyLog.hpp:265