QtDcmPreferences.cpp
Go to the documentation of this file.
1 /*
2  QtDcm is a C++ Qt based library for communication and conversion of Dicom images.
3  Copyright (C) 2011 Alexandre Abadie <Alexandre.Abadie@univ-rennes1.fr>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 
21 #include <QtDcmPreferences.h>
22 #include <QtDcmManager.h>
23 
25 {
26 public:
27  QFile iniFile;
28  QString aetitle;
29  QString port;
30  QString hostname;
32  bool useDcm2nii;
33  QString dcm2niiPath;
35  QList<QtDcmServer> servers;
36 };
37 
38 QtDcmPreferences * QtDcmPreferences::_instance = 0;
39 
40 QtDcmPreferences::QtDcmPreferences(QObject * parent)
41  : QObject(parent),
42  d ( new QtDcmPreferencesPrivate )
43 {
44 }
45 
46 QtDcmPreferences::~QtDcmPreferences()
47 {
48  delete d;
49 }
50 
52 {
53  if ( _instance == 0 ) {
54  _instance = new QtDcmPreferences();
55  }
56 
57  return _instance;
58 }
59 
61 {
62  if (_instance != 0) {
63  delete _instance;
64  _instance = 0;
65  }
66 }
67 
69 {
70  d->servers.append ( server );
71 }
72 
74 {
75  d->servers.removeAt ( index );
76 }
77 
79 {
80  if ( !d->iniFile.exists() ) {
81  this->setDefaultIniFile();
82  }
83 
84  //Instantiate a QSettings object from the ini file.
85  QSettings prefs ( d->iniFile.fileName(), QSettings::IniFormat );
86  //Load local settings
87  prefs.beginGroup ( "LocalSettings" );
88  d->aetitle = prefs.value ( "AETitle" ).toString();
89  d->port = prefs.value ( "Port" ).toString();
90  d->hostname = prefs.value ( "Hostname" ).toString();
91  prefs.endGroup();
92 
93  prefs.beginGroup ( "Converter" );
94  d->useDcm2nii = prefs.value ( "UseDcm2nii" ).toBool();
95  d->dcm2niiPath = prefs.value ( "Dcm2nii" ).toString();
96  prefs.endGroup();
97 
98  //For each server load corresponding settings
99  prefs.beginGroup ( "Servers" );
100  for ( int i = 0; i < prefs.childGroups().size(); i++ )
101  {
102  QtDcmServer server;
103  server.setAetitle ( prefs.value ( "Server" + QString::number ( i + 1 ) + "/AETitle" ).toString() );
104  server.setAddress ( prefs.value ( "Server" + QString::number ( i + 1 ) + "/Hostname" ).toString() );
105  server.setPort ( prefs.value ( "Server" + QString::number ( i + 1 ) + "/Port" ).toString() );
106  server.setName ( prefs.value ( "Server" + QString::number ( i + 1 ) + "/Name" ).toString() );
107  d->servers.append ( server );
108  }
109  prefs.endGroup();
110 
111  emit preferencesUpdated();
112 }
113 
115 {
116  //Instantiate a QSettings object with the ini file.
117  QSettings prefs ( d->iniFile.fileName(), QSettings::IniFormat );
118  prefs.clear();
119 
120  //Write local settings from the private attributes
121  prefs.beginGroup ( "LocalSettings" );
122  prefs.setValue ( "AETitle", d->aetitle );
123  prefs.setValue ( "Port", d->port );
124  prefs.setValue ( "Hostname", d->hostname );
125  prefs.endGroup();
126 
127  prefs.beginGroup ( "Converter" );
128  prefs.setValue ( "Dcm2nii", d->dcm2niiPath );
129  prefs.setValue ( "UseDcm2nii", d->useDcm2nii );
130  prefs.endGroup();
131 
132  //Do the job for each server
133  prefs.beginGroup ( "Servers" );
134  for ( int i = 0; i < d->servers.size(); i++ ) {
135  QtDcmServer server = d->servers[i];
136  prefs.beginGroup ( "Server" + QString::number ( i + 1 ) );
137  prefs.setValue ( "AETitle", server.aetitle() );
138  prefs.setValue ( "Hostname", server.address() );
139  prefs.setValue ( "Port", server.port() );
140  prefs.setValue ( "Name", server.name() );
141  prefs.endGroup();
142  }
143 
144  prefs.endGroup();
145 
146  emit preferencesUpdated();
147 }
148 
149 void QtDcmPreferences::setDefaultIniFile()
150 {
151  //Create an ini file with default parameters
152  d->aetitle = "QTDCM";
153  d->port = "2010";
154  d->hostname = "localhost";
155 
156  d->dcm2niiPath = "";
157  d->useDcm2nii = 0;
158 
159  QtDcmServer server;
160  server.setAetitle ( "SERVER" );
161  server.setAddress ( "hostname" );
162  server.setName ( "Server" + QString::number ( d->servers.size() + 1 ) );
163  server.setPort ( "11112" );
164  d->servers.append ( server );
165  this->writeSettings();
166 }
167 
169 {
170  return d->iniFile.fileName();
171 }
172 
173 void QtDcmPreferences::setIniFile ( const QString ini )
174 {
175  QDir iniDir = QDir ( QDir::homePath() + QDir::separator() + ".qtdcm" );
176 
177  if ( !iniDir.exists() )
178  QDir::home().mkdir ( ".qtdcm" );
179 
180  d->iniFile.setFileName ( iniDir.absolutePath() + QDir::separator() + ini );
181 
182  if ( !d->iniFile.exists() ) {
183  qWarning() << "Configuration not found, using default settings";
184  this->setDefaultIniFile(); //If it doesn't exist create it with default parameters
185  }
186  else {
187  qDebug() << "Reading configuration file" << d->iniFile.fileName();
188  this->readSettings();
189  }
190 
191  emit preferencesUpdated();
192 }
193 
195 {
196  return d->aetitle;
197 }
198 
199 QString QtDcmPreferences::port() const
200 {
201  return d->port;
202 }
203 
204 QList<QtDcmServer> QtDcmPreferences::servers() const
205 {
206  return d->servers;
207 }
208 
209 void QtDcmPreferences::setHostname ( const QString & hostname )
210 {
211  d->hostname = hostname;
212 }
213 
215 {
216  return d->hostname;
217 }
218 
219 void QtDcmPreferences::setAetitle ( const QString & aetitle )
220 {
221  d->aetitle = aetitle;
222 }
223 
224 void QtDcmPreferences::setPort ( const QString & port )
225 {
226  d->port = port;
227 }
228 
229 void QtDcmPreferences::setServers ( const QList<QtDcmServer> & servers )
230 {
231  d->servers = servers;
232 }
233 
235 {
236  return d->dcm2niiPath;
237 }
238 
239 void QtDcmPreferences::setDcm2niiPath ( const QString & path )
240 {
241  d->dcm2niiPath = path;
242 }
243 
245 {
246  return d->useDcm2nii;
247 }
248 
250 {
251  d->useDcm2nii = use;
252 }
253 
QList< QtDcmServer > servers
The dcm2nii binary path.
QString port() const
PACS server port getter (QtDcm only ports between 1000 and 100000)
Definition: QtDcmServer.h:69
QString hostname
Local port of qtdcm.
void setAetitle(QString _aetitle)
PACS AETitle setter.
Definition: QtDcmServer.h:89
static QtDcmPreferences * instance()
void setAddress(QString _server)
PACS server hostname setter.
Definition: QtDcmServer.h:119
void writeSettings()
Store current settings in the ini file.
bool useDcm2nii() const
void setIniFile(const QString ini)
QString aetitle() const
PACS AETitle getter.
Definition: QtDcmServer.h:49
QString name() const
Description name getter.
Definition: QtDcmServer.h:59
QList< QtDcmServer > servers() const
QtDcm server list getter.
void setPort(const QString &port)
QtDcm local application TCP port setter.
void setUseDcm2nii(bool use)
void setPort(QString port)
PACS server port setter (QtDcm only ports between 1000 and 100000)
Definition: QtDcmServer.h:109
QString dcm2niiPath
Use dcm2nii as a conversion tool.
bool useDcm2nii
Local hostname of qtdcm.
QString hostname() const
void setDcm2niiPath(const QString &path)
static void destroy()
void setServers(const QList< QtDcmServer > &servers)
QtDcm server list setter.
QString aetitle() const
QtDcm local AETitle getter.
QString dcm2niiPath() const
void setAetitle(const QString &aetitle)
QtDcm local AETitle setter.
void readSettings()
Read the settings from the ini file.
void setName(QString _name)
PACS Name setter.
Definition: QtDcmServer.h:99
void setHostname(const QString &hostname)
QtDcm local AETitle setter.
QString aetitle
File stored on the file system that contains the preferences.
QString address() const
PACS server hostname getter.
Definition: QtDcmServer.h:79
QString port
Local aetitle of QtDcm.
QString iniFile(void)
This class is a representation of a Dicom PACS server.
Definition: QtDcmServer.h:30
void addServer(const QtDcmServer &server)
Add server to the QList.
QString port() const
QtDcm local application TCP port getter.
Class that manages the settings of QtDcm.
void removeServer(int index)
Remove server from the QList at position i.