Quellcode durchsuchen

Remove 'throws' from ctkDICOMDatabase open failure code

Qt does not like exceptions being thown by event handlers
(see error message below generated by specifying an incorrect
filename to ctkDICOMDatabase).  Instead we'll need to rely
on callers tesing the isOpen property after trying to
open the database.


pieper@boggs:~/ctk/latest/CTK-superbuild$ ./CTK-build/bin/ctkSimplePythonShell
Qt has caught an exception thrown from an event handler. Throwing
exceptions from an event handler is not supported in Qt. You must
reimplement QApplication::notify() and catch all exceptions there.

terminate called after throwing an instance of 'std::runtime_error'
  what():  unable to open database file Error opening database
Aborted
Steve Pieper vor 13 Jahren
Ursprung
Commit
20f51df45b
2 geänderte Dateien mit 23 neuen und 7 gelöschten Zeilen
  1. 14 7
      Libs/DICOM/Core/ctkDICOMDatabase.cpp
  2. 9 0
      Libs/DICOM/Core/ctkDICOMDatabase.h

+ 14 - 7
Libs/DICOM/Core/ctkDICOMDatabase.cpp

@@ -130,20 +130,21 @@ void ctkDICOMDatabase::openDatabase(const QString databaseFile, const QString& c
   if ( ! (d->Database.open()) )
     {
     d->LastError = d->Database.lastError().text();
-    throw std::runtime_error(qPrintable(d->LastError));
+    return;
     }
   if ( d->Database.tables().empty() )
     {
-      if (!initializeDatabase())
-        {
-        throw std::runtime_error("Unable to initialize DICOM database!");
-        }
+    if (!initializeDatabase())
+      {
+      d->LastError = QString("Unable to initialize DICOM database!");
+      return;
+      }
     }
   if (!isInMemory())
-  {
+    {
     QFileSystemWatcher* watcher = new QFileSystemWatcher(QStringList(databaseFile),this);
     connect(watcher, SIGNAL( fileChanged(const QString&)),this, SIGNAL ( databaseChanged() ) );
-  }
+    }
 }
 
 
@@ -601,6 +602,12 @@ void ctkDICOMDatabase::insert ( DcmDataset *dataset, bool storeFile, bool genera
     }
 }
 
+bool ctkDICOMDatabase::isOpen() const
+{
+  Q_D(const ctkDICOMDatabase);
+  return d->Database.isOpen();
+}
+
 bool ctkDICOMDatabase::isInMemory() const
 {
   Q_D(const ctkDICOMDatabase);

+ 9 - 0
Libs/DICOM/Core/ctkDICOMDatabase.h

@@ -49,6 +49,10 @@ class ctkDICOMAbstractThumbnailGenerator;
 class CTK_DICOM_CORE_EXPORT ctkDICOMDatabase : public QObject
 {
   Q_OBJECT
+  Q_PROPERTY(bool isOpen READ isOpen)
+  Q_PROPERTY(QString lastError READ lastError)
+  Q_PROPERTY(QString databaseFilename READ databaseFilename)
+
 public:
   explicit ctkDICOMDatabase(QObject *parent = 0);
   explicit ctkDICOMDatabase(QString databaseFile);
@@ -65,6 +69,11 @@ public:
   const QString databaseDirectory() const;
 
   ///
+  /// Should be checked after trying to open the database
+  /// @Returns true if database is open
+  bool isOpen() const;
+
+  ///
   /// Returns whether the database only resides in memory, i.e. the
   /// SQLITE DB is not written to stored to disk and DICOM objects are not
   /// stored to the file system.