ソースを参照

Null check for ctkXnatSettings in login dialog, refactorings

Miklos Espak 11 年 前
コミット
326676483b

+ 29 - 19
Applications/ctkXNATBrowser/ctkXNATBrowserMainWindow.cpp

@@ -25,6 +25,7 @@
 #include "ctkXnatLoginDialog.h"
 #include "ctkXnatProjectListModel.h"
 #include "ctkXnatConnection.h"
+#include "ctkXnatConnectionFactory.h"
 #include "ctkXnatServer.h"
 #include "ctkXnatProject.h"
 
@@ -33,51 +34,60 @@
 ctkXNATBrowserMainWindow::ctkXNATBrowserMainWindow(QWidget *parent) :
   QMainWindow(parent),
   ui(new Ui::ctkXNATBrowserMainWindow),
-  xnatConnection(NULL),
-  projectsModel(new ctkXnatProjectListModel()),
-  subjectsModel(new ctkXnatProjectListModel())
+  m_ConnectionFactory(new ctkXnatConnectionFactory()),
+  m_Connection(0),
+  m_ProjectsModel(new ctkXnatProjectListModel()),
+  m_SubjectsModel(new ctkXnatProjectListModel())
 {
   ui->setupUi(this);
 
-  ui->projectsList->setModel(projectsModel);
-  ui->subjectsList->setModel(subjectsModel);
+  ui->projectsList->setModel(m_ProjectsModel);
+  ui->subjectsList->setModel(m_SubjectsModel);
 
-  connect(ui->projectsList, SIGNAL(clicked(QModelIndex)), SLOT(projectSelected(QModelIndex)));
-  connect(ui->loginButton, SIGNAL(clicked()), SLOT(loginButtonPushed()));
+  this->connect(ui->projectsList, SIGNAL(clicked(QModelIndex)), SLOT(projectSelected(QModelIndex)));
+  this->connect(ui->loginButton, SIGNAL(clicked()), SLOT(loginButtonPushed()));
 }
 
 ctkXNATBrowserMainWindow::~ctkXNATBrowserMainWindow()
 {
+  if (m_Connection)
+  {
+    delete m_Connection;
+  }
+  delete m_ConnectionFactory;
   delete ui;
+
+  delete m_SubjectsModel;
+  delete m_ProjectsModel;
 }
 
 void ctkXNATBrowserMainWindow::loginButtonPushed()
 {
-  if (xnatConnection)
+  if (m_Connection)
   {
-    delete xnatConnection;
-    xnatConnection = NULL;
+    delete m_Connection;
+    m_Connection = 0;
     ui->loginButton->setText("Login");
     ui->loginLabel->setText("Disconnected");
 
-    projectsModel->setRootObject(ctkXnatObject::Pointer());
+    m_ProjectsModel->setRootObject(ctkXnatObject::Pointer());
     ui->projectsList->reset();
   }
   else
   {
-    ctkXnatLoginDialog loginDialog(xnatConnectionFactory);
+    ctkXnatLoginDialog loginDialog(m_ConnectionFactory);
     if (loginDialog.exec() == QDialog::Accepted)
     {
-      xnatConnection = loginDialog.getConnection();
-      if (xnatConnection)
+      m_Connection = loginDialog.getConnection();
+      if (m_Connection)
       {
         ui->loginButton->setText("Logout");
-        ui->loginLabel->setText(QString("Connected: %1").arg(xnatConnection->url()));
+        ui->loginLabel->setText(QString("Connected: %1").arg(m_Connection->url()));
 
-        ctkXnatServer::Pointer server = xnatConnection->server();
+        ctkXnatServer::Pointer server = m_Connection->server();
         //xnatConnection->fetch(server);
         server->fetch();
-        projectsModel->setRootObject(server);
+        m_ProjectsModel->setRootObject(server);
         ui->projectsList->reset();
       }
     }
@@ -87,13 +97,13 @@ void ctkXNATBrowserMainWindow::loginButtonPushed()
 void ctkXNATBrowserMainWindow::projectSelected(const QModelIndex& index)
 {
   qDebug() << "Project selected";
-  QVariant variant = projectsModel->data(index, Qt::UserRole);
+  QVariant variant = m_ProjectsModel->data(index, Qt::UserRole);
   if (variant.isValid())
   {
     ctkXnatObject::Pointer project = variant.value<ctkXnatObject::Pointer>();
     qDebug() << "selected project id:" << project->getId();
     project->fetch();
-    subjectsModel->setRootObject(project);
+    m_SubjectsModel->setRootObject(project);
     ui->subjectsList->reset();
   }
 }

+ 5 - 6
Applications/ctkXNATBrowser/ctkXNATBrowserMainWindow.h

@@ -26,9 +26,8 @@
 
 class QModelIndex;
 
-#include "ctkXnatConnectionFactory.h"
-
 class ctkXnatConnection;
+class ctkXnatConnectionFactory;
 class ctkXnatProjectListModel;
 class ctkXnatProject;
 
@@ -53,10 +52,10 @@ private Q_SLOTS:
 private:
   Ui::ctkXNATBrowserMainWindow *ui;
 
-  ctkXnatConnection* xnatConnection;
-  ctkXnatProjectListModel* projectsModel;
-  ctkXnatProjectListModel* subjectsModel;
-  ctkXnatConnectionFactory xnatConnectionFactory;
+  ctkXnatConnectionFactory* m_ConnectionFactory;
+  ctkXnatConnection* m_Connection;
+  ctkXnatProjectListModel* m_ProjectsModel;
+  ctkXnatProjectListModel* m_SubjectsModel;
 };
 
 #endif // CTKXNATBROWSERMAINWINDOW_H

+ 22 - 16
Applications/ctkXnatTreeBrowser/ctkXnatTreeBrowserMainWindow.cpp

@@ -25,52 +25,58 @@
 #include "ctkXnatLoginDialog.h"
 #include "ctkXnatTreeModel.h"
 #include "ctkXnatConnection.h"
+#include "ctkXnatConnectionFactory.h"
 #include "ctkXnatServer.h"
 #include "ctkXnatProject.h"
 
-#include <QDebug>
-
 ctkXnatTreeBrowserMainWindow::ctkXnatTreeBrowserMainWindow(QWidget *parent) :
   QMainWindow(parent),
   ui(new Ui::ctkXnatTreeBrowserMainWindow),
-  xnatConnection(0),
-  treeModel(new ctkXnatTreeModel())
+  m_ConnectionFactory(new ctkXnatConnectionFactory()),
+  m_Connection(0),
+  m_TreeModel(new ctkXnatTreeModel())
 {
   ui->setupUi(this);
 
-  ui->treeView->setModel(treeModel);
+  ui->treeView->setModel(m_TreeModel);
 
-  connect(ui->loginButton, SIGNAL(clicked()), SLOT(loginButtonPushed()));
+  this->connect(ui->loginButton, SIGNAL(clicked()), SLOT(loginButtonPushed()));
 }
 
 ctkXnatTreeBrowserMainWindow::~ctkXnatTreeBrowserMainWindow()
 {
-  delete treeModel;
+  if (m_Connection)
+  {
+    delete m_Connection;
+  }
+  delete m_ConnectionFactory;
   delete ui;
+
+  delete m_TreeModel;
 }
 
 void ctkXnatTreeBrowserMainWindow::loginButtonPushed()
 {
-  if (xnatConnection)
+  if (m_Connection)
   {
-    delete xnatConnection;
-    xnatConnection = NULL;
+    delete m_Connection;
+    m_Connection = 0;
     ui->loginButton->setText("Login");
     ui->loginLabel->setText("Disconnected");
   }
   else
   {
-    ctkXnatLoginDialog loginDialog(xnatConnectionFactory);
+    ctkXnatLoginDialog loginDialog(m_ConnectionFactory);
     if (loginDialog.exec() == QDialog::Accepted)
     {
-      xnatConnection = loginDialog.getConnection();
-      if (xnatConnection)
+      m_Connection = loginDialog.getConnection();
+      if (m_Connection)
       {
         ui->loginButton->setText("Logout");
-        ui->loginLabel->setText(QString("Connected: %1").arg(xnatConnection->url()));
+        ui->loginLabel->setText(QString("Connected: %1").arg(m_Connection->url()));
 
-        ctkXnatServer::Pointer server = xnatConnection->server();
-        treeModel->addServer(server);
+        ctkXnatServer::Pointer server = m_Connection->server();
+        m_TreeModel->addServer(server);
         ui->treeView->reset();
       }
     }

+ 4 - 5
Applications/ctkXnatTreeBrowser/ctkXnatTreeBrowserMainWindow.h

@@ -26,9 +26,8 @@
 
 class QModelIndex;
 
-#include "ctkXnatConnectionFactory.h"
-
 class ctkXnatConnection;
+class ctkXnatConnectionFactory;
 class ctkXnatTreeModel;
 
 namespace Ui {
@@ -50,9 +49,9 @@ private Q_SLOTS:
 private:
   Ui::ctkXnatTreeBrowserMainWindow* ui;
 
-  ctkXnatConnection* xnatConnection;
-  ctkXnatTreeModel* treeModel;
-  ctkXnatConnectionFactory xnatConnectionFactory;
+  ctkXnatConnectionFactory* m_ConnectionFactory;
+  ctkXnatConnection* m_Connection;
+  ctkXnatTreeModel* m_TreeModel;
 };
 
 #endif

+ 32 - 18
Libs/XNAT/Widgets/ctkXnatLoginDialog.cpp

@@ -28,6 +28,7 @@
 #include <QTimer>
 
 #include <ctkXnatConnection.h>
+#include <ctkXnatConnectionFactory.h>
 #include <ctkXnatException.h>
 #include "ctkXnatLoginProfile.h"
 #include "ctkXnatSettings.h"
@@ -35,14 +36,14 @@
 class ctkXnatLoginDialogPrivate
 {
 public:
-  ctkXnatLoginDialogPrivate(ctkXnatConnectionFactory& f)
+  ctkXnatLoginDialogPrivate(ctkXnatConnectionFactory* f)
   : Factory(f)
   {
   }
 
   ctkXnatSettings* Settings;
 
-  ctkXnatConnectionFactory& Factory;
+  ctkXnatConnectionFactory* Factory;
   ctkXnatConnection* Connection;
 
   QMap<QString, ctkXnatLoginProfile*> Profiles;
@@ -53,7 +54,7 @@ public:
   bool Dirty;
 };
 
-ctkXnatLoginDialog::ctkXnatLoginDialog(ctkXnatConnectionFactory& f, QWidget* parent, Qt::WindowFlags flags)
+ctkXnatLoginDialog::ctkXnatLoginDialog(ctkXnatConnectionFactory* f, QWidget* parent, Qt::WindowFlags flags)
 : QDialog(parent, flags)
 , ui(0)
 , d_ptr(new ctkXnatLoginDialogPrivate(f))
@@ -80,7 +81,7 @@ ctkXnatLoginDialog::ctkXnatLoginDialog(ctkXnatConnectionFactory& f, QWidget* par
     ui->btnSave->setEnabled(false);
 
     // Create connections after setting defaults, so you don't trigger stuff when setting defaults.
-    createConnections();
+    this->createConnections();
     }
 }
 
@@ -122,6 +123,10 @@ void ctkXnatLoginDialog::setSettings(ctkXnatSettings* settings)
 {
   Q_D(ctkXnatLoginDialog);
   d->Settings = settings;
+  if (!settings)
+  {
+    return;
+  }
   d->Profiles = d->Settings->getLoginProfiles();
 
   d->ProfileNames = d->Profiles.keys();
@@ -173,9 +178,9 @@ void ctkXnatLoginDialog::accept()
   if (d->Dirty)
     {
     const QString& profileName = ui->edtProfileName->text();
-    if (askToSaveProfile(profileName))
+    if (this->askToSaveProfile(profileName))
       {
-      saveProfile(profileName);
+      this->saveProfile(profileName);
       }
     }
 
@@ -184,7 +189,7 @@ void ctkXnatLoginDialog::accept()
   // create XNAT connection
   try
     {
-    d->Connection = d->Factory.makeConnection(url.toAscii().constData(), userName.toAscii().constData(),
+    d->Connection = d->Factory->makeConnection(url.toAscii().constData(), userName.toAscii().constData(),
                                         password.toAscii().constData());
     d->Connection->setProfileName(ui->edtProfileName->text());
     }
@@ -205,7 +210,7 @@ void ctkXnatLoginDialog::onCurrentProfileChanged(const QModelIndex& currentIndex
 
   if (!currentIndex.isValid())
     {
-    loadProfile();
+    this->loadProfile();
     return;
     }
 
@@ -217,14 +222,14 @@ void ctkXnatLoginDialog::onCurrentProfileChanged(const QModelIndex& currentIndex
   if (d->Dirty)
     {
     QString profileName = ui->edtProfileName->text();
-    if (askToSaveProfile(profileName))
+    if (this->askToSaveProfile(profileName))
       {
-      saveProfile(profileName);
+      this->saveProfile(profileName);
       newProfileSaved = true;
       }
     }
 
-  loadProfile(*profile);
+  this->loadProfile(*profile);
 
   d->Dirty = false;
   ui->btnSave->setEnabled(false);
@@ -280,7 +285,7 @@ void ctkXnatLoginDialog::saveProfile(const QString& profileName)
     d->Model.setData(d->Model.index(idx), profileName);
     }
   
-  storeProfile(*profile);
+  this->storeProfile(*profile);
   
   // If the profile is to be default then remove the default flag from the other profiles.
   // This code assumes that the newly created profiles are not default.
@@ -292,12 +297,18 @@ void ctkXnatLoginDialog::saveProfile(const QString& profileName)
       if (otherProfileName != profileName && otherProfile->isDefault())
         {
         otherProfile->setDefault(false);
-        d->Settings->setLoginProfile(otherProfileName, otherProfile);
+        if (d->Settings)
+          {
+          d->Settings->setLoginProfile(otherProfileName, otherProfile);
+          }
         }
       }
     }
   
-  d->Settings->setLoginProfile(profileName, profile);
+  if (d->Settings)
+    {
+    d->Settings->setLoginProfile(profileName, profile);
+    }
   d->Dirty = false;
   ui->btnSave->setEnabled(false);
 }
@@ -319,7 +330,7 @@ void ctkXnatLoginDialog::on_btnSave_clicked()
       }
     }
 
-  saveProfile(editedProfileName);
+  this->saveProfile(editedProfileName);
 
   if (selectSavedProfile)
     {
@@ -340,7 +351,7 @@ void ctkXnatLoginDialog::blockSignalsOfFields(bool value)
 
 void ctkXnatLoginDialog::loadProfile(const ctkXnatLoginProfile& profile)
 {
-  blockSignalsOfFields(true);
+  this->blockSignalsOfFields(true);
 
   ui->edtProfileName->setText(profile.name());
   ui->edtServerUri->setText(profile.serverUri());
@@ -348,7 +359,7 @@ void ctkXnatLoginDialog::loadProfile(const ctkXnatLoginProfile& profile)
   ui->edtPassword->setText(profile.password());
   ui->cbxDefaultProfile->setChecked(profile.isDefault());
 
-  blockSignalsOfFields(false);
+  this->blockSignalsOfFields(false);
 }
 
 void ctkXnatLoginDialog::storeProfile(ctkXnatLoginProfile& profile)
@@ -377,7 +388,10 @@ void ctkXnatLoginDialog::on_btnDelete_clicked()
     ui->edtProfileName->setFocus();
     }
   
-  d->Settings->removeLoginProfile(profileName);
+  if (d->Settings)
+    {
+    d->Settings->removeLoginProfile(profileName);
+    }
 }
 
 void ctkXnatLoginDialog::on_edtProfileName_textChanged(const QString& /*text*/)

+ 2 - 2
Libs/XNAT/Widgets/ctkXnatLoginDialog.h

@@ -28,10 +28,10 @@
 
 #include "ui_ctkXnatLoginDialog.h"
 
-#include <ctkXnatConnectionFactory.h>
 #include "ctkXnatLoginProfile.h"
 
 class ctkXnatConnection;
+class ctkXnatConnectionFactory;
 class ctkXnatLoginDialogPrivate;
 class ctkXnatSettings;
 
@@ -40,7 +40,7 @@ class CTK_XNAT_WIDGETS_EXPORT ctkXnatLoginDialog : public QDialog
   Q_OBJECT
 
 public:
-  explicit ctkXnatLoginDialog(ctkXnatConnectionFactory& f, QWidget* parent = 0, Qt::WindowFlags flags = 0);
+  explicit ctkXnatLoginDialog(ctkXnatConnectionFactory* f, QWidget* parent = 0, Qt::WindowFlags flags = 0);
   virtual ~ctkXnatLoginDialog();
 
   ctkXnatSettings* settings() const;