ctkDICOM.cpp

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Library:   CTK
00004  
00005   Copyright (c) 2010  Kitware Inc.
00006 
00007   Licensed under the Apache License, Version 2.0 (the "License");
00008   you may not use this file except in compliance with the License.
00009   You may obtain a copy of the License at
00010 
00011       http://www.commontk.org/LICENSE
00012 
00013   Unless required by applicable law or agreed to in writing, software
00014   distributed under the License is distributed on an "AS IS" BASIS,
00015   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016   See the License for the specific language governing permissions and
00017   limitations under the License.
00018  
00019 =========================================================================*/
00020 
00021 // Qt includes
00022 #include <QDebug>
00023 #include <QSqlDatabase>
00024 #include <QSqlError>
00025 #include <QSqlQuery>
00026 #include <QFile>
00027 #include <QStringList>
00028 
00029 // ctkDICOM includes
00030 #include "ctkDICOM.h"
00031 
00032 // STD includes
00033 #include <iostream>
00034 
00035 //----------------------------------------------------------------------------
00036 class ctkDICOMPrivate: public ctkPrivate<ctkDICOM>
00037 {
00038 public:
00039   ctkDICOMPrivate(); 
00040   bool executeScript(const QString& script);
00041 
00042   QSqlDatabase  Database;
00043   QString       DatabaseFileName;
00044   QString       LastError;
00045 };
00046 
00047 //----------------------------------------------------------------------------
00048 // ctkDICOMPrivate methods
00049 
00050 //------------------------------------------------------------------------------
00051 ctkDICOMPrivate::ctkDICOMPrivate()
00052 {
00053 }
00054 
00055 //----------------------------------------------------------------------------
00056 // ctkDICOMWidget methods
00057 
00058 //------------------------------------------------------------------------------
00059 ctkDICOM::ctkDICOM(QObject* _parent): Superclass(_parent)
00060 {
00061   CTK_INIT_PRIVATE(ctkDICOM);
00062 }
00063 
00064 //----------------------------------------------------------------------------
00065 ctkDICOM::~ctkDICOM()
00066 {
00067 }
00068 
00069 //----------------------------------------------------------------------------
00070 bool ctkDICOM::openDatabase(const QString& databaseFileName)
00071 {
00072   CTK_D(ctkDICOM);
00073   d->Database = QSqlDatabase::addDatabase("QSQLITE","DICOM-DB");
00074   d->Database.setDatabaseName(databaseFileName);
00075   if ( ! (d->Database.open()) )
00076     {
00077     d->LastError = d->Database.lastError().text();
00078     return false;
00079     }
00080   if ( d->Database.tables().empty() ) 
00081     {
00082     initializeDatabase();
00083     }
00084   return true;
00085 }
00086 
00087 //------------------------------------------------------------------------------
00088 const QString& ctkDICOM::GetLastError() const {
00089   CTK_D(const ctkDICOM);
00090   return d->LastError; 
00091 }
00092 
00093 //------------------------------------------------------------------------------
00094 const QSqlDatabase& ctkDICOM::database() const {
00095   CTK_D(const ctkDICOM);
00096   return d->Database;
00097 }
00098 
00099 //------------------------------------------------------------------------------
00100 bool ctkDICOMPrivate::executeScript(const QString& script) {
00101   QFile scriptFile(script);
00102   scriptFile.open(QIODevice::ReadOnly);
00103   if  ( !scriptFile.isOpen() )
00104     {
00105     qDebug() << "Script file " << script << " could not be opened!\n";
00106     return false;
00107     }
00108 
00109   QString sqlCommands( QTextStream(&scriptFile).readAll() );
00110   sqlCommands.replace( '\n', ' ' );
00111   sqlCommands.replace("; ", ";\n");
00112 
00113   QStringList sqlCommandsLines = sqlCommands.split('\n');
00114 
00115   QSqlQuery query(Database);
00116 
00117   for (QStringList::iterator it = sqlCommandsLines.begin(); it != sqlCommandsLines.end()-1; ++it)
00118   {
00119     if (! (*it).startsWith("--") )
00120       {
00121       query.exec(*it);
00122       if (query.lastError().type())
00123         {
00124         qDebug() << "There was an error during execution of the statement: " << (*it);
00125         qDebug() << "Error message: " << query.lastError().text();
00126         return false;
00127         }
00128       }
00129   }
00130   return true;
00131 }
00132 
00133 //------------------------------------------------------------------------------
00134 bool ctkDICOM::initializeDatabase(const char* sqlFileName)
00135 {
00136   CTK_D(ctkDICOM);
00137   return d->executeScript(sqlFileName);
00138 }
00139 
00140 //------------------------------------------------------------------------------
00141 void ctkDICOM::closeDatabase()
00142 {
00143   CTK_D(ctkDICOM);
00144   d->Database.close();
00145 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines