GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
QGoWizardDB.cxx
Go to the documentation of this file.
1 /*=========================================================================
2  Authors: The GoFigure Dev. Team.
3  at Megason Lab, Systems biology, Harvard Medical school, 2009-11
4 
5  Copyright (c) 2009-11, President and Fellows of Harvard College.
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10 
11  Redistributions of source code must retain the above copyright notice,
12  this list of conditions and the following disclaimer.
13  Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16  Neither the name of the President and Fellows of Harvard College
17  nor the names of its contributors may be used to endorse or promote
18  products derived from this software without specific prior written
19  permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 =========================================================================*/
34 #include "QGoWizardDB.h"
35 
39 #include "CreateDataBaseHelper.h"
40 #include "QueryDataBaseHelper.h"
42 #include "GoDBRecordSet.h"
43 #include "GoDBRecordSetHelper.h"
44 #include "itkMegaCaptureImport.h"
46 
47 #include <iostream>
48 #include <QVariant>
49 #include <QFormLayout>
50 #include <QRect>
51 #include <QFileDialog>
52 #include <QPushButton>
53 #include <string>
54 #include <QLabel>
55 #include <Qt>
56 #include <QMessageBox>
57 #include <QFont>
58 #include <QPushButton>
59 #include <QCheckBox>
60 #include <QCloseEvent>
61 
62 //-------------------------------------------------------------------------
64  QWizard(iParent)
65 {
66  this->m_ImgSessionName = "";
67  this->m_ImgSessionID = 0;
68  this->m_IsAnOpenRecentFile = false;
69  QFont tfont;
70  tfont.setBold(true);
71  this->setFont(tfont);
72 
73  QFont font2;
74  font2.setBold(false);
75 
76  nextButton = new QPushButton( tr("Next") );
77  nextButton->setFont(font2);
78  this->setButton (QWizard::NextButton, nextButton);
79  QPushButton *backButton = new QPushButton( tr("Back") );
80  backButton->setFont(font2);
81  this->setButton (QWizard::BackButton, backButton);
82  this->setOptions(QWizard::NoCancelButton);
83  QPushButton *finishButton = new QPushButton( tr("Finish") );
84  finishButton->setFont(font2);
85  this->setButton (QWizard::FinishButton, finishButton);
91  setWindowTitle( tr("Use DataBase") );
93  this, SLOT( hide() ) );
95  this, SIGNAL( NoGofigureDatabase() ) );
97  this, SIGNAL ( GofigureDatabaseExists() ) );
98 }
99 
100 //-------------------------------------------------------------------------
101 
102 //-------------------------------------------------------------------------
103 std::vector< std::vector< std::string > > QGoWizardDB::GetFilenamesFromDB()
104 {
105  std::vector< std::vector< std::string > > oFilenames;
106  //first, open a connection to the database:
107  std::string Server = field("ServerName").toString().toStdString();
108  std::string User = field("User").toString().toStdString();
109  std::string Password = field("Password").toString().toStdString();
110  std::string DBName = field("DBName").toString().toStdString();
111 
112  std::pair< bool, vtkMySQLDatabase * > ConnectionDatabase = ConnectToDatabase(
113  Server, User, Password, DBName);
114 
115  if ( !ConnectionDatabase.first )
116  {
117  std::cout << "No connection open for QGoOpenOrCreateImgSession" << std::endl;
118  std::cout << "Debug: In " << __FILE__ << ", line " << __LINE__;
119  std::cout << std::endl;
120  }
121 
122  vtkMySQLDatabase *DatabaseConnector = ConnectionDatabase.second;
123 
124  //Get the number of channels with their id as a map
125  //ListChannelsIDNumber[channelID]=ChannelNumber:
126  std::vector< std::string > ChannelAttributes (2);
127  ChannelAttributes[0] = "channelID";
128  ChannelAttributes[1] = "ChannelNumber";
129 
130  boost::unordered_map< std::string, std::string > ListChannelsIDNumber =
131  MapTwoColumnsFromTable( DatabaseConnector, ChannelAttributes, "channel",
132  "ImagingSessionID", field("ImgSessionID").toString().toStdString() );
133 
134  // std::map< std::string, std::string > ListChannelsIDNumber =
135  // MapTwoColumnsFromTable( DatabaseConnector, "channelID", "ChannelNumber",
136  // "channel", "ImagingSessionID",
137  // field("ImgSessionID").toString().toStdString() );
138 
139  boost::unordered_map< std::string, std::string >::iterator it = ListChannelsIDNumber.begin();
140  oFilenames.resize( ListChannelsIDNumber.size() );
141  int i = 0;
142  while ( it != ListChannelsIDNumber.end() )
143  {
144  std::string ChannelID = it->first;
145  //get the filenames of all the images corresponding to ChannelID:
146  oFilenames[i] = ListSpecificValuesForOneColumn(
147  DatabaseConnector, "image", "Filename", "channelID", ChannelID);
148  ++it;
149  ++i;
150  }
151 
152  //close the database connection:
153  DatabaseConnector->Close();
154  DatabaseConnector->Delete();
155  return oFilenames;
156 }
157 
158 //-------------------------------------------------------------------------
159 
160 //-------------------------------------------------------------------------
162 {
163  return field("NameDB").toString();
164 }
165 
166 //-------------------------------------------------------------------------
167 
168 //-------------------------------------------------------------------------
170 {
171  if ( field("ImgSessionID").toInt() != 0 )
172  {
173  return field("ImgSessionID").toInt();
174  }
175  if ( this->m_ImgSessionID != 0 )
176  {
177  return this->m_ImgSessionID;
178  }
179  return 0;
180 }
181 
182 //-------------------------------------------------------------------------
183 
184 //-------------------------------------------------------------------------
186 {
187  if ( this->m_ImgSessionName.empty() )
188  {
189  return field("ImgSessionName").toString();
190  }
191  else
192  {
193  return this->m_ImgSessionName.c_str();
194  }
195 }
196 
197 //-------------------------------------------------------------------------
198 
199 //-------------------------------------------------------------------------
201 {
202  return field("ServerName").toString();
203 }
204 
205 //-------------------------------------------------------------------------
206 
207 //-------------------------------------------------------------------------
209 {
210  return field("User").toString();
211 }
212 
213 //-------------------------------------------------------------------------
214 
215 //-------------------------------------------------------------------------
217 {
218  return field("Password").toString();
219 }
220 
221 //-------------------------------------------------------------------------
222 
223 //-------------------------------------------------------------------------
225 {
226  int CurrentPageID = this->currentId();
227  QWizardPage *CurrentPage = this->currentPage();
228 
229  if ( !field("ImgSessionName").toString().toStdString().empty() )
230  {
231  this->m_ImgSessionName =
232  field("ImgSessionName").toString().toStdString();
233  this->m_ImgSessionID = 0;
234  }
235  if ( !field("DBName").toString().toStdString().empty() )
236  {
237  this->SetFirstFileName();
238  }
239  this->m_ImgSessionName.clear();
240  this->m_IsAnOpenRecentFile = false;
241  //this->restart();
242 
243  switch ( CurrentPageID )
244  {
245  case 0:
246  {
247  //QGoConnectServerPage* ServerPage =
248  // dynamic_cast<QGoConnectServerPage*>(CurrentPage);
249  //ServerPage->m_ConnectionServer.second->Close();
250  //ServerPage->m_ConnectionServer.second->Delete();
251  //delete ServerPage;
252  break;
253  }
254  case 1:
255  {
256  QGoOpenCreateProjectPage *ProjectPage = dynamic_cast< QGoOpenCreateProjectPage * >( CurrentPage );
257  if ( ProjectPage->m_DatabaseConnector )
258  {
259  ProjectPage->m_DatabaseConnector->Close();
260  ProjectPage->m_DatabaseConnector = 0;
261  }
262  break;
263  }
264  case 2:
265  {
266  QGoOpenCreateImgSessionPage *ImgSessionPage = dynamic_cast< QGoOpenCreateImgSessionPage * >( CurrentPage );
267  if ( ImgSessionPage->m_DatabaseConnector )
268  {
269  ImgSessionPage->m_DatabaseConnector->Close();
270  ImgSessionPage->m_DatabaseConnector = 0;
271  }
272  break;
273  }
274  case 3:
275  {
276  QGoCreateImgSessionPage *CreateImgSessionPage = dynamic_cast< QGoCreateImgSessionPage * >( CurrentPage );
277  if ( CreateImgSessionPage->m_DatabaseConnector )
278  {
279  CreateImgSessionPage->m_DatabaseConnector->Close();
280  CreateImgSessionPage->m_DatabaseConnector = 0;
281  }
282  break;
283  }
284  default:
285  {
286  break;
287  }
288  }
289 
290 /* QGoOpenCreateImgSessionPage* F = dynamic_cast<QGoOpenCreateImgSessionPage*>(CurrentPage);
291  if (F !=0)
292  {
293  F->m_DatabaseConnector->Close();
294  F->m_DatabaseConnector->Delete();
295  }
296  delete F;*/
297  iEvent->accept();
298 }
299 
300 //-------------------------------------------------------------------------
301 
302 //-------------------------------------------------------------------------
305 {
306  QGoCreateImgSessionPage *img_page =
307  dynamic_cast< QGoCreateImgSessionPage * >( this->page(CreateImgSessionPageID) );
308 
309  if ( img_page )
310  {
311  return img_page->GetMultiIndexFileContainer();
312  }
313  else
314  {
316  }
317 }
318 
319 //-------------------------------------------------------------------------
320 
321 //-------------------------------------------------------------------------
322 std::string
324 {
325  QGoCreateImgSessionPage *img_page =
326  dynamic_cast< QGoCreateImgSessionPage * >( this->page(CreateImgSessionPageID) );
327 
328  std::string oFilename;
329 
330  if ( img_page )
331  {
332  oFilename = img_page->GetMegaCaptureHeaderFilename();
333  }
334 
335  return oFilename;
336 }
337 
338 //-------------------------------------------------------------------------
339 
340 //-------------------------------------------------------------------------
341 void QGoWizardDB::setImgSessionName(std::string iImgSessionName)
342 {
343  this->m_ImgSessionName = iImgSessionName;
344  this->m_IsAnOpenRecentFile = true;
345  this->m_ConnectServerPage->SetImgSessionName(iImgSessionName);
346 }
347 
348 //-------------------------------------------------------------------------
349 
350 //-------------------------------------------------------------------------
352 {
354  std::string Server = field("ServerName").toString().toStdString();
355  std::string User = field("User").toString().toStdString();
356  std::string Password = field("Password").toString().toStdString();
357  std::string DBName = field("DBName").toString().toStdString();
358 
359  std::pair< bool, vtkMySQLDatabase * > ConnectionDatabase = ConnectToDatabase(
360  Server, User, Password, DBName);
361 
362  if ( !ConnectionDatabase.first )
363  {
364  std::cout << "No connection open for QGoOpenOrCreateImgSession" << std::endl;
365  std::cout << "Debug: In " << __FILE__ << ", line " << __LINE__;
366  std::cout << std::endl;
367  }
368 
369  vtkMySQLDatabase *DatabaseConnector = ConnectionDatabase.second;
370  this->m_ImgSessionID = FindOneID(DatabaseConnector, "imagingsession",
371  "ImagingSessionID", "Name", this->m_ImgSessionName);
372  this->m_FirstFileName = ReturnOnlyOneValue( DatabaseConnector,
373  "image", "Filename", "ImagingSessionID",
374  ConvertToString< int >(this->m_ImgSessionID) );
375 
376  DatabaseConnector->Close();
377  DatabaseConnector->Delete();
378 }
379 
380 //-------------------------------------------------------------------------
381 
382 //-------------------------------------------------------------------------
384 {
385  if ( !this->m_IsAnOpenRecentFile )
386  {
387  this->m_ImgSessionName =
388  field("ImgSessionName").toString().toStdString();
389  }
390  this->SetFirstFileName();
391  this->m_ImgSessionName.clear();
393  return this->m_FirstFileName;
394 }
395 
396 //-------------------------------------------------------------------------
397 
398 //-------------------------------------------------------------------------
400 {
401  return this->m_IsAnOpenRecentFile;
402 }
403 
404 //-------------------------------------------------------------------------
405 
406 //-------------------------------------------------------------------------
407 void QGoWizardDB::SetIsAnOpenRecentFile(bool iIsAnOpenRecentFile)
408 {
409  this->m_IsAnOpenRecentFile = iIsAnOpenRecentFile;
410 }
QVariant field(const QString &name) const
std::string toStdString() const
QString GetImagingSessionName()
QGoConnectServerPage * m_ConnectServerPage
Definition: QGoWizardDB.h:120
QGoWizardDB(QWidget *parent=0)
Definition: QGoWizardDB.cxx:63
void SetIsAnOpenRecentFile(bool iIsAnOpenRecentFile)
void setPage(int id, QWizardPage *page)
QWizardPage * page(int id) const
void setOptions(QFlags< QWizard::WizardOption > options)
int currentId() const
QString GetServer()
QString GetNameDB()
void NoGofigureDatabase()
void SetFirstFileName()
int GetImagingSessionID()
QString tr(const char *sourceText, const char *disambiguation, int n)
boost::unordered_map< std::string, std::string > MapTwoColumnsFromTable(vtkMySQLDatabase *DatabaseConnector, const std::vector< std::string > &iColumnNames, const std::string &iTableName, std::string iField, std::string iValue)
query: "SELECT ColumnName1, ColumnName2 FROM TableName"
void SetIsAnOpenRecentFile(bool iIsAnOpenRecentFile)
void setBold(bool enable)
bool m_IsAnOpenRecentFile
Definition: QGoWizardDB.h:124
void setImgSessionName(std::string iImgSessionName)
int toInt(bool *ok) const
std::string ReturnOnlyOneValue(vtkMySQLDatabase *DatabaseConnector, const std::string &TableName, const std::string &ColumnName, const std::string &field, const std::string &value)
SELECT ColunmName FROM TableName WHERE field=value limit 1.
int m_ImgSessionID
Definition: QGoWizardDB.h:123
GoFigureFileInfoHelperMultiIndexContainer GetMultiIndexFileContainer()
std::string GetFirstFileName()
bool GetIsAnOpenRecentFile()
QWizardPage * currentPage() const
std::string GetMegaCaptureHeaderFilename()
QPushButton * nextButton
Definition: QGoWizardDB.h:93
std::vector< std::string > ListSpecificValuesForOneColumn(vtkMySQLDatabase *iDatabaseConnector, const std::string &TableName, const std::string &ColumnName, const std::string &field, const std::string &value, bool ExcludeZero)
SELECT ColumnName FROM TableName WHERE field = value and ColumnName <> 0 (if excludezero) ...
std::vector< std::vector< std::string > > GetFilenamesFromDB()
return a list for each channel of the filenames for the images in the database as a vector of vector ...
void closeEvent(QCloseEvent *iEvent)
int FindOneID(vtkMySQLDatabase *DatabaseConnector, const std::string &TableName, const std::string &ColumnName, const std::string &field, const std::string &value)
SELECT ColumnName FROM TableName WHERE field = value.
void hide()
std::string m_FirstFileName
Definition: QGoWizardDB.h:122
QString GetLogin()
GoFigureFileInfoHelperMultiIndexContainer GetMultiIndexFileContainer()
void setButton(WizardButton which, QAbstractButton *button)
void accept()
std::pair< bool, vtkMySQLDatabase * > ConnectToDatabase(std::string ServerName, std::string login, std::string Password, std::string DBName)
void setFont(const QFont &)
vtkMySQLDatabase * m_DatabaseConnector
std::string m_ImgSessionName
Definition: QGoWizardDB.h:121
QString GetPassword()
void GofigureDatabaseExists()
void setWindowTitle(const QString &)
boost::multi_index::multi_index_container< GoFigureFileInfoHelper, boost::multi_index::indexed_by< boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_PCoord >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_RCoord >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_CCoord >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_XTileCoord >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_YTileCoord >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_ZTileCoord >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_ZCoord >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_Channel >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_TCoord >, > >> GoFigureFileInfoHelperMultiIndexContainer
void SetImgSessionName(std::string iImgSessionName)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString toString() const
vtkMySQLDatabase * m_DatabaseConnector