Przeglądaj źródła

Rename QPersonName into ctkDICOMPersonName and add test

Rename method names to comply with coding guidelines:
 GetLastName() -> lastName()
Julien Finet 14 lat temu
rodzic
commit
31a91683d6

+ 3 - 0
Libs/DICOM/Core/Testing/Cpp/CMakeLists.txt

@@ -5,6 +5,7 @@ CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cpp
   ctkDICOMImageTest1.cpp
   ctkDICOMIndexerTest1.cpp
   ctkDICOMModelTest1.cpp
+  ctkDICOMPersonNameTest1.cpp
   ctkDICOMTest1.cpp
   )
 
@@ -33,6 +34,8 @@ ADD_TEST( ctkDICOMImageTest1 ${KIT_TESTS}
 SET_PROPERTY(TEST ctkDICOMImageTest1 PROPERTY LABELS ${PROJECT_NAME})
 
 SIMPLE_TEST( ctkDICOMIndexerTest1 )
+SIMPLE_TEST( ctkDICOMPersonNameTest1)
+
 ADD_TEST( ctkDICOMModelTest1 ${KIT_TESTS}
           ctkDICOMModelTest1 ${CMAKE_CURRENT_BINARY_DIR}/dicom.db
                              ${CMAKE_CURRENT_SOURCE_DIR}/../../Resources/dicom-sample.sql)

+ 124 - 0
Libs/DICOM/Core/Testing/Cpp/ctkDICOMPersonNameTest1.cpp

@@ -0,0 +1,124 @@
+
+// Qt includes
+#include <QApplication>
+#include <QDir>
+#include <QTimer>
+
+// ctkDICOMCore includes
+#include "ctkDICOMPersonName.h"
+
+// STD includes
+#include <iostream>
+
+
+int ctkDICOMPersonNameTest1( int argc, char * argv [] )
+{
+  QApplication app(argc, argv);
+
+  ctkDICOMPersonName personName("lastName", "firstName", "middleName", "namePrefix", "nameSuffix");
+  if (personName.lastName() != "lastName")
+    {
+    std::cerr << "ctkDICOMPersonName::lastName() failed:"
+              << qPrintable(personName.lastName()) << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  if (personName.firstName() != "firstName")
+    {
+    std::cerr << "ctkDICOMPersonName::firstName() failed:"
+              << qPrintable(personName.firstName()) << std::endl;
+    return EXIT_FAILURE;
+    }
+  
+  if (personName.middleName() != "middleName")
+    {
+    std::cerr << "ctkDICOMPersonName::middleName() failed:"
+              << qPrintable(personName.middleName()) << std::endl;
+    return EXIT_FAILURE;
+    }
+  
+  if (personName.namePrefix() != "namePrefix")
+    {
+    std::cerr << "ctkDICOMPersonName::namePrefix() failed:"
+              << qPrintable(personName.namePrefix()) << std::endl;
+    return EXIT_FAILURE;
+    }
+    
+  if (personName.nameSuffix() != "nameSuffix")
+    {
+    std::cerr << "ctkDICOMPersonName::nameSuffix() failed:"
+              << qPrintable(personName.nameSuffix()) << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  if (personName.formattedName() != "lastName, firstName middleName, nameSuffix")
+    {
+    std::cerr << "ctkDICOMPersonName::nameSuffix() failed:"
+              << qPrintable(personName.formattedName()) << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  // test operator QString()
+  if (QString(personName) != QString("lastName, firstName middleName, nameSuffix"))
+    {
+    std::cerr << "ctkDICOMPersonName::nameSuffix() failed:"
+              << qPrintable(QString(personName)) << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  // test toStdString()
+  // TODO: make it fail 
+  if (personName.toStdString() != std::string("lastName, firstName middleName, nameSuffix"))
+    {
+    std::cerr << "ctkDICOMPersonName::nameSuffix() failed:"
+              << personName.toStdString() << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  ctkDICOMPersonName samePersonName(personName);
+  
+  if (samePersonName.lastName() != personName.lastName() ||
+      samePersonName.firstName() != personName.firstName() ||
+      samePersonName.middleName() != personName.middleName() ||
+      samePersonName.namePrefix() != personName.namePrefix() ||
+      samePersonName.nameSuffix() != personName.nameSuffix() || 
+      samePersonName.formattedName() != personName.formattedName())
+    {
+    std::cerr << "ctkDICOMPersonName::ctkDICOMPersonName(ctkDICOMPersonName&) failed:"
+              << qPrintable(samePersonName.formattedName()) << std::endl;
+    return EXIT_FAILURE;
+    }
+  ctkDICOMPersonName otherPerson("just a last name");
+  if (otherPerson.lastName() != "just a last name" ||
+      !otherPerson.firstName().isEmpty() ||
+      !otherPerson.middleName().isEmpty() ||
+      !otherPerson.namePrefix().isEmpty() ||
+      !otherPerson.nameSuffix().isEmpty() ||
+      otherPerson.formattedName() != "just a last name")
+    {
+    std::cerr << "ctkDICOMPersonName with empty fields failed:"
+              << qPrintable(otherPerson.formattedName()) << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  otherPerson = samePersonName;
+  
+  if (otherPerson.lastName() != personName.lastName() ||
+      otherPerson.firstName() != personName.firstName() ||
+      otherPerson.middleName() != personName.middleName() ||
+      otherPerson.namePrefix() != personName.namePrefix() ||
+      otherPerson.nameSuffix() != personName.nameSuffix() || 
+      otherPerson.formattedName() != personName.formattedName())
+    {
+    std::cerr << "ctkDICOMPersonName::operator=(const ctkDICOMPersonName&) failed:"
+              << qPrintable(otherPerson.formattedName()) << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  if (argc <= 1 || QString(argv[1]) != "-I")
+    {
+    QTimer::singleShot(200, &app, SLOT(quit()));
+    }
+
+  return app.exec();
+}

+ 13 - 13
Libs/DICOM/Core/ctkDICOMDataset.cpp

@@ -446,7 +446,7 @@ QStringList ctkDICOMDataset::GetElementAsStringList( const DcmTag& tag ) const
   return qsl;
 }
 
-QPersonName ctkDICOMDataset::GetElementAsPersonName( const DcmTag& tag, unsigned long pos ) const
+ctkDICOMPersonName ctkDICOMDataset::GetElementAsPersonName( const DcmTag& tag, unsigned long pos ) const
 {
   this->EnsureDcmDataSetIsInitialized();
   DcmElement* element(NULL);
@@ -454,7 +454,7 @@ QPersonName ctkDICOMDataset::GetElementAsPersonName( const DcmTag& tag, unsigned
 
   DcmPersonName* name = dynamic_cast<DcmPersonName*>(element);
 
-  if (!name) return QPersonName(); // invalid
+  if (!name) return ctkDICOMPersonName(); // invalid
 
   OFString lastName;
   OFString firstName;
@@ -463,7 +463,7 @@ QPersonName ctkDICOMDataset::GetElementAsPersonName( const DcmTag& tag, unsigned
   OFString nameSuffix;
   if (CheckCondition( name->getNameComponents(lastName, firstName, middleName, namePrefix, nameSuffix, pos) ) )
   {
-    return QPersonName(
+    return ctkDICOMPersonName(
       Decode(tag, lastName),
       Decode(tag, firstName),
       Decode(tag, middleName),
@@ -472,14 +472,14 @@ QPersonName ctkDICOMDataset::GetElementAsPersonName( const DcmTag& tag, unsigned
   }
   else
   {
-    return QPersonName();
+    return ctkDICOMPersonName();
   }
 }
 
-QPersonNameList ctkDICOMDataset::GetElementAsPersonNameList( const DcmTag& tag ) const
+ctkDICOMPersonNameList ctkDICOMDataset::GetElementAsPersonNameList( const DcmTag& tag ) const
 {
   this->EnsureDcmDataSetIsInitialized();
-  QPersonNameList qpnl;
+  ctkDICOMPersonNameList qpnl;
 
   DcmElement* element(NULL);
   findAndGetElement(tag, element);
@@ -639,17 +639,17 @@ bool ctkDICOMDataset::SetElementAsStringList( const DcmTag& /*tag*/, QStringList
   return false;
 }
 
-bool ctkDICOMDataset::SetElementAsPersonName( const DcmTag& tag, QPersonName personName )
+bool ctkDICOMDataset::SetElementAsPersonName( const DcmTag& tag, ctkDICOMPersonName personName )
 {
   this->EnsureDcmDataSetIsInitialized();
   DcmPersonName* dcmPersonName = new DcmPersonName( tag ); // TODO leak?
 
   if ( CheckCondition( dcmPersonName->putNameComponents(
-    Encode( tag, personName.GetLastName() ),
-    Encode( tag, personName.GetFirstName() ),
-    Encode( tag, personName.GetMiddleName() ),
-    Encode( tag, personName.GetNamePrefix() ),
-    Encode( tag, personName.GetNameSuffix() ) ) ) )
+    Encode( tag, personName.lastName() ),
+    Encode( tag, personName.firstName() ),
+    Encode( tag, personName.middleName() ),
+    Encode( tag, personName.namePrefix() ),
+    Encode( tag, personName.nameSuffix() ) ) ) )
   {
     return CheckCondition( insert( dcmPersonName ) );
   }
@@ -657,7 +657,7 @@ bool ctkDICOMDataset::SetElementAsPersonName( const DcmTag& tag, QPersonName per
   return false;
 }
 
-bool ctkDICOMDataset::SetElementAsPersonNameList( const DcmTag& tag, QPersonNameList personNameList )
+bool ctkDICOMDataset::SetElementAsPersonNameList( const DcmTag& tag, ctkDICOMPersonNameList personNameList )
 {
   this->EnsureDcmDataSetIsInitialized();
   // TODO: Find out how this can be implemented with DcmDataset methods; there is no method for

+ 4 - 4
Libs/DICOM/Core/ctkDICOMDataset.h

@@ -169,8 +169,8 @@ public:
     QString          GetAllElementValuesAsString( const DcmTag& tag ) const;
     QString                   GetElementAsString( const DcmTag& tag, unsigned long pos = 0 ) const;
     QStringList           GetElementAsStringList( const DcmTag& tag ) const;
-    QPersonName           GetElementAsPersonName( const DcmTag& tag, unsigned long pos = 0 ) const;
-    QPersonNameList   GetElementAsPersonNameList( const DcmTag& tag ) const;
+    ctkDICOMPersonName           GetElementAsPersonName( const DcmTag& tag, unsigned long pos = 0 ) const;
+    ctkDICOMPersonNameList   GetElementAsPersonNameList( const DcmTag& tag ) const;
     QDate                       GetElementAsDate( const DcmTag& tag, unsigned long pos = 0 ) const;
     QTime                       GetElementAsTime( const DcmTag& tag, unsigned long pos = 0 ) const;
     double                    GetElementAsDouble( const DcmTag& tag, unsigned long pos = 0 ) const; // type DS
@@ -189,8 +189,8 @@ public:
     */
     bool SetElementAsString( const DcmTag& tag, QString string );
     bool SetElementAsStringList( const DcmTag& tag, QStringList stringList ); //> Currently not implemented
-    bool SetElementAsPersonName( const DcmTag& tag, QPersonName personName );
-    bool SetElementAsPersonNameList( const DcmTag& tag, QPersonNameList personNameList ); //> Currently not implemented
+    bool SetElementAsPersonName( const DcmTag& tag, ctkDICOMPersonName personName );
+    bool SetElementAsPersonNameList( const DcmTag& tag, ctkDICOMPersonNameList personNameList ); //> Currently not implemented
     bool SetElementAsDate( const DcmTag& tag, QDate date );
     bool SetElementAsTime( const DcmTag& tag, QTime time );
     bool SetElementAsDateTime( const DcmTag& tag, QDateTime dateTime );

+ 43 - 33
Libs/DICOM/Core/ctkDICOMPersonName.cpp

@@ -1,27 +1,27 @@
+// Qt include
 #include <QSharedData>
 
+// CTK DICOM Core
 #include "ctkDICOMPersonName.h"
 
-
-// ------------------------------------------- QPersonName -----------------------------------------------------------
-
-class QPersonNameData : public QSharedData
+//------------------------------------------------------------------------------
+class ctkDICOMPersonNameData : public QSharedData
 {
-  public:
-
-    QString m_LastName;
-    QString m_FirstName;
-    QString m_MiddleName;
-    QString m_NamePrefix;
-    QString m_NameSuffix;
+public:
+  QString m_LastName;
+  QString m_FirstName;
+  QString m_MiddleName;
+  QString m_NamePrefix;
+  QString m_NameSuffix;
 };
 
-QPersonName::QPersonName(const QString& lastName,
-                         const QString& firstName,
-                         const QString& middleName,
-                         const QString& namePrefix,
-                         const QString& nameSuffix)
-:d(new QPersonNameData)
+//------------------------------------------------------------------------------
+ctkDICOMPersonName::ctkDICOMPersonName(const QString& lastName,
+                             const QString& firstName,
+                             const QString& middleName,
+                             const QString& namePrefix,
+                             const QString& nameSuffix)
+  : d(new ctkDICOMPersonNameData)
 {
   d->m_LastName = lastName;
   d->m_FirstName = firstName;
@@ -30,22 +30,26 @@ QPersonName::QPersonName(const QString& lastName,
   d->m_NameSuffix = nameSuffix;
 }
 
-QPersonName::QPersonName(const QPersonName& other) : d(other.d)
+//------------------------------------------------------------------------------
+ctkDICOMPersonName::ctkDICOMPersonName(const ctkDICOMPersonName& other)
+  : d(other.d)
 {
 }
 
-QPersonName& QPersonName::operator=(const QPersonName& other)
+//------------------------------------------------------------------------------
+ctkDICOMPersonName& ctkDICOMPersonName::operator=(const ctkDICOMPersonName& other)
 {
-  d=other.d;
+  d = other.d;
   return *this;
 }
 
-QPersonName::~QPersonName()
+//------------------------------------------------------------------------------
+ctkDICOMPersonName::~ctkDICOMPersonName()
 {
 }
 
-
-QString QPersonName::GetFormattedName() const
+//------------------------------------------------------------------------------
+QString ctkDICOMPersonName::formattedName() const
 {
   QString result("");
 
@@ -65,42 +69,48 @@ QString QPersonName::GetFormattedName() const
   return result;
 }
 
-QString QPersonName::GetLastName()  const
+//------------------------------------------------------------------------------
+QString ctkDICOMPersonName::lastName() const
 {
   return d->m_LastName;
 }
 
-QString QPersonName::GetFirstName()  const
+//------------------------------------------------------------------------------
+QString ctkDICOMPersonName::firstName() const
 {
   return d->m_FirstName;
 }
 
-QString QPersonName::GetMiddleName()  const
+//------------------------------------------------------------------------------
+QString ctkDICOMPersonName::middleName() const
 {
   return d->m_MiddleName;
 }
 
-QString QPersonName::GetNamePrefix()  const
+//------------------------------------------------------------------------------
+QString ctkDICOMPersonName::namePrefix() const
 {
   return d->m_NamePrefix;
 }
 
-QString QPersonName::GetNameSuffix()  const
+//------------------------------------------------------------------------------
+QString ctkDICOMPersonName::nameSuffix() const
 {
   return d->m_NameSuffix;
 }
 
-QPersonName::operator QString() const
+//------------------------------------------------------------------------------
+ctkDICOMPersonName::operator QString() const
 {
-  return this->GetFormattedName();
+  return this->formattedName();
 }
 
-std::string QPersonName::toStdString() const
+//------------------------------------------------------------------------------
+std::string ctkDICOMPersonName::toStdString() const
 {
   // the complicated looking .toLocal8Bit().constData() is on purpose.
   // if we'd use .toStdString(), the string would be converted to ASCII
   // instead of the local 8bit character encoding.
   // changes for correctly looking output of the strings are higher with .toLocal8Bit()
-  return this->GetFormattedName().toLocal8Bit().constData();
+  return this->formattedName().toLocal8Bit().constData();
 }
-

+ 36 - 37
Libs/DICOM/Core/ctkDICOMPersonName.h

@@ -1,5 +1,5 @@
-#ifndef CTKDICOMPERSONNAME_H
-#define CTKDICOMPERSONNAME_H
+#ifndef __ctkDICOMPersonName_h
+#define __ctkDICOMPersonName_h
 
 #include "ctkDICOMCoreExport.h"
 
@@ -9,46 +9,45 @@
 
 #include <string>
 
-class QPersonNameData;
+class ctkDICOMPersonNameData;
 /**
   \brief A person's name as modelled in DICOM.
 */
-class CTK_DICOM_CORE_EXPORT QPersonName
+class CTK_DICOM_CORE_EXPORT ctkDICOMPersonName
 {
-
-  public:
-
-    QPersonName(const QString& lastName = QString::null,
-                const QString& firstName = QString::null,
-                const QString& middleName = QString::null,
-                const QString& namePrefix = QString::null,
-                const QString& nameSuffix = QString::null);
-
-    QPersonName(const QPersonName& other);
-    QPersonName& operator=(const QPersonName& other);
-
-    virtual ~QPersonName();
-    /**
-      \brief "Lastname, FirstName MiddleName, Suffix" (useful for alphabetical sorting)
-    */
-    QString GetFormattedName() const;
-
-    QString GetLastName() const;
-    QString GetFirstName() const;
-    QString GetMiddleName() const;
-    QString GetNamePrefix() const;
-    QString GetNameSuffix() const;
-
-    /// cast operator
-    operator QString() const;
-    std::string toStdString() const;
-
-  private:
-    QSharedDataPointer<QPersonNameData> d;
+public:
+
+  ctkDICOMPersonName(const QString& lastName = QString::null,
+              const QString& firstName = QString::null,
+              const QString& middleName = QString::null,
+              const QString& namePrefix = QString::null,
+              const QString& nameSuffix = QString::null);
+
+  ctkDICOMPersonName(const ctkDICOMPersonName& other);
+  ctkDICOMPersonName& operator=(const ctkDICOMPersonName& other);
+
+  virtual ~ctkDICOMPersonName();
+  /**
+    \brief "Lastname, FirstName MiddleName, Suffix" (useful for alphabetical sorting)
+  */
+  QString formattedName() const;
+
+  QString lastName() const;
+  QString firstName() const;
+  QString middleName() const;
+  QString namePrefix() const;
+  QString nameSuffix() const;
+
+  /// cast operator
+  operator QString() const;
+  std::string toStdString() const;
+
+private:
+  QSharedDataPointer<ctkDICOMPersonNameData> d;
 };
 
-typedef QList<QPersonName> QPersonNameList;
-Q_DECLARE_METATYPE(QPersonName);
+typedef QList<ctkDICOMPersonName> ctkDICOMPersonNameList;
+Q_DECLARE_METATYPE(ctkDICOMPersonName);
 
 
-#endif // CTKDICOMPERSONNAME_H
+#endif // ctkDICOMPersonName_h