GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
QGoOpenCreateProjectPage.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 =========================================================================*/
35 #include "QueryDataBaseHelper.h"
37 #include "GoDBRecordSetHelper.h"
38 #include "GoDBProjectRow.h"
39 #include "QGoWizardDB.h"
40 #include "ConvertToStringHelper.h"
42 #include <QMessageBox>
43 #include <QRegExp>
44 #include <QDate>
45 #include <QTextEdit>
46 #include <QGridLayout>
47 #include <QComboBox>
48 #include <QPushButton>
49 
51  QWizardPage(iParent),
52  m_DatabaseConnector(0),
53  ExistingImgSession(false)
54 {
55  QFont tfont;
56 
57  tfont.setBold(false);
58  this->setFont(tfont);
59  m_DatabaseVersion = "Version2";
60 
62  new QRadioButton( tr("Create a new Project ") );
64  new QRadioButton( tr("Open an existing Project") );
65 
66  NewAuthorButton = new QPushButton(tr("Add author"), this);
67 
68  textChoiceProject = new QLabel( tr("Project to open:") );
70  textNewProjectName = new QLabel( tr("Name of the Project: ") );
73  textDescription = new QLabel( tr("Description:") );
74  lineDescription = new QTextEditChild(this, 1000);
75  textChoiceAuthor = new QLabel( tr("Choose the name of the author: ") );
76  ChoiceAuthor = new QComboBox;
77  textAuthor = new QLabel ( tr("Author of the project: ") );
78  lineAuthor = new QLineEdit;
79  lineAuthor->setReadOnly(true);
80 
81  QVBoxLayout *vlayout = new QVBoxLayout;
82  QVBoxLayout *RadioButtonLayout = new QVBoxLayout;
83  RadioButtonLayout->addWidget(CreateProjectRadioButton);
84  RadioButtonLayout->addWidget(OpenProjectRadioButton);
85  vlayout->addLayout(RadioButtonLayout);
86  vlayout->setAlignment(RadioButtonLayout, Qt::AlignHCenter);
87  QGridLayout *gridlayout = new QGridLayout;
88  gridlayout->addWidget(textChoiceProject, 0, 0);
89  gridlayout->addWidget(ChoiceProject, 0, 1);
90  gridlayout->addWidget(textNewProjectName, 3, 0);
91  gridlayout->addWidget(lineNewProjectName, 3, 1);
92 
93  gridlayout->addWidget(textChoiceAuthor, 4, 0);
94  gridlayout->addWidget(ChoiceAuthor, 4, 1);
95  gridlayout->addWidget(textAuthor, 5, 0);
96  gridlayout->addWidget(lineAuthor, 5, 1);
97  gridlayout->addWidget(NewAuthorButton, 4, 3);
98  gridlayout->addWidget(textDescription, 6, 0);
99  gridlayout->addWidget(lineDescription, 6, 1);
100  vlayout->addLayout(gridlayout);
101  setLayout(vlayout);
102 
103  registerField("ProjectName", lineNewProjectName);
104  registerField("Author", ChoiceAuthor);
105 
106  QObject::connect( this->OpenProjectRadioButton, SIGNAL( clicked() ),
107  this, SLOT( ChangeToOpenProjectDisplay() ) );
108 
109  QObject::connect( this->CreateProjectRadioButton, SIGNAL( clicked() ),
110  this, SLOT( ChangeToCreateProjectDisplay() ) );
111 
112  QObject::connect( this->ChoiceProject, SIGNAL( currentIndexChanged(QString) ),
113  this, SLOT( DisplayInfoProject(QString) ) );
114 
115  QObject::connect( NewAuthorButton, SIGNAL( clicked() ), this, SLOT( AddAuthors() ) );
116 }
117 
118 //-------------------------------------------------------------------------
119 
120 //-------------------------------------------------------------------------
122 {
123  if ( !m_ListProject.isEmpty() )
124  {
126  }
127  if ( ChoiceProject->count() != 0 )
128  {
129  ChoiceProject->clear();
130  }
131 
133 
134  setSubTitle(
135  tr("You are currently using The Database %1 ").arg(
136  field("DBName").toString() ) );
139 
140  if ( !GetListProject() )
141  {
146  }
147  else
148  {
153  }
154 
155  field("ProjectName").clear();
156  field("Author").clear();
157 }
158 
159 //-------------------------------------------------------------------------
160 
161 //-------------------------------------------------------------------------
163 {
165  ChoiceProject->clear();
166 
167  std::vector< std::string > vectListProjName =
169 
170  if ( !vectListProjName.empty() )
171  {
172  for ( unsigned int i = 0; i < vectListProjName.size(); ++i )
173  {
174  m_ListProject.append( vectListProjName[i].c_str() );
175  }
177  return true;
178  }
179  else
180  {
181  return false;
182  }
183 }
184 
185 //-------------------------------------------------------------------------
186 
187 //-------------------------------------------------------------------------
189 {
190  QDate DateOfToday = QDate::currentDate();
191 
192  GoDBProjectRow myNewProject;
193 
194  myNewProject.SetField( "Name", field("ProjectName").toString().toStdString() );
195  myNewProject.SetField( "Description", this->lineDescription->toPlainText().toStdString() );
196  myNewProject.SetField( "AuthorID", AuthorIDForNewProject() );
197  myNewProject.SetField( "CreationDate", DateOfToday.toString(Qt::ISODate).toStdString() );
198  myNewProject.SetField("DatabaseVersion", m_DatabaseVersion);
199 
200  if ( m_DatabaseConnector == 0 )
201  {
203  }
204 
205  AddOnlyOneNewObjectInTable< GoDBProjectRow >(m_DatabaseConnector,
206  "project", myNewProject);
207 }
208 
209 //-------------------------------------------------------------------------
210 
211 //-------------------------------------------------------------------------
213 {
214  QString Author = ChoiceAuthor->currentText();
215  int spaces = Author.count( QRegExp(" ") );
216  std::stringstream AuthorName;
217 
218  AuthorName << Author.toStdString();
219  if ( spaces < 2 )
220  {
221  AuthorName << " <none>";
222  }
223  return m_MapAuthorIDName[AuthorName.str()];
224 }
225 
226 //-------------------------------------------------------------------------
227 
228 //-------------------------------------------------------------------------
230 {
231  QStringList ListAuthors;
232 
233  if ( m_DatabaseConnector == 0 )
234  {
236  }
237 
238  std::vector< std::string > ListFirstNames = ListAllValuesForOneColumn(
239  m_DatabaseConnector, "FirstName", "author");
240  std::vector< std::string > ListMiddleNames = ListAllValuesForOneColumn(
241  m_DatabaseConnector, "MiddleName", "author");
242  std::vector< std::string > ListLastNames = ListAllValuesForOneColumn(
243  m_DatabaseConnector, "LastName", "author");
244  std::vector< std::string > ListID = ListAllValuesForOneColumn(
245  m_DatabaseConnector, "AuthorID", "author");
246 
247  if ( !ListFirstNames.empty() )
248  {
249  for ( unsigned int i = 0; i < ListFirstNames.size(); i++ )
250  {
251  std::stringstream AuthorTotalName;
252  AuthorTotalName << ListLastNames[i];
253  AuthorTotalName << " ";
254  AuthorTotalName << ListFirstNames[i];
255  AuthorTotalName << " ";
256  AuthorTotalName << ListMiddleNames[i];
257 
258  std::vector< FieldWithValue > Conditions(3);
259  FieldWithValue FirstName = { "FirstName", ListFirstNames[i], "=" };
260  Conditions[0] = FirstName;
261  FieldWithValue MiddleName = { "MiddleName", ListMiddleNames[i], "=" };
262  Conditions[1] = MiddleName;
263  FieldWithValue LastName = { "LastName", ListLastNames[i], "=" };
264  Conditions[2] = LastName;
265 
266  m_MapAuthorIDName[AuthorTotalName.str()] = FindOneID(m_DatabaseConnector,
267  "author", "AuthorID", Conditions);
268  }
269  for ( std::map< std::string, int >::iterator iter = m_MapAuthorIDName.begin();
270  iter != m_MapAuthorIDName.end(); ++iter )
271  {
272  QString AuthorName = ( *iter ).first.c_str();
273  if ( AuthorName.contains( QRegExp("<none>") ) )
274  {
275  AuthorName.chop(7);
276  }
277  ListAuthors.append(AuthorName);
278  }
279  }
280  else
281  {
282  QMessageBox msgBox;
283  msgBox.setText(
284  tr("Please create the author of your project:") );
285  msgBox.exec();
286  }
287 
288  return ListAuthors;
289 }
290 
291 //-------------------------------------------------------------------------
292 
293 //-------------------------------------------------------------------------
295 {
296  setSubTitle(
297  tr(
298  "When you click on 'Next' the project will be created in the Gofigure Database\n or select 'Open an existing project':") );
299  QStringList ListAuthors;
300 
302  ChoiceProject->setVisible(false);
304  textNewProjectName->setText( tr("Name of the new Project") );
310  ChoiceAuthor->setVisible(true);
312  textAuthor->setVisible(false);
313  lineAuthor->setVisible(false);
314 
315  //if(ListAuthors.isEmpty())
316  //{
317  this->UpdateListAuthors();
318  // }
319  ChoiceAuthor->setVisible(true);
320  OpenOrCreateProject = "Create";
321 }
322 
323 //-------------------------------------------------------------------------
324 
325 //-------------------------------------------------------------------------
327 {
328  setSubTitle(
329  tr("Select the project you want to open or choose 'Create a new project':") );
330 
332  ChoiceProject->setVisible(true);
337  ChoiceAuthor->setVisible(false);
338  NewAuthorButton->setVisible(false);
339  textAuthor->setVisible(true);
340  lineAuthor->setVisible(true);
341  OpenOrCreateProject = "Open";
342 
343  if ( !m_ListProject.isEmpty() )
344  {
346  }
347 }
348 
349 //-------------------------------------------------------------------------
350 
351 //-------------------------------------------------------------------------
353 {
354  if ( ProjectName != "" )
355  {
356  /*Display the description of the existing project "ProjectName" */
357 
358  QString Description;
359 
360  if ( m_DatabaseConnector == 0 )
361  {
363  }
364  std::vector< std::string > ResultQuery;
365  ResultQuery = ListSpecificValuesForOneColumn(
366  m_DatabaseConnector, "project", "Description",
367  "Name", ProjectName.toStdString() );
368  /*only one field in the ResultQuery as the project name is the primary key
369  of the Project Table: */
370  Description = ResultQuery[0].c_str();
371  lineDescription->setText(Description);
372 
373  /*Display the AuthorName of the exisiting project "ProjectName"
374  first, have to find the corresponding AuthorID in the Database :*/
375  ResultQuery.clear();
376  ResultQuery = ListSpecificValuesForOneColumn(
377  m_DatabaseConnector, "project", "AuthorID",
378  "Name", ProjectName.toStdString() );
379  int AuthorID = atoi( ResultQuery[0].c_str() );
380 
381  /*second, have to find the corresponding AuthorName in the map*/
382 
383  if ( m_MapAuthorIDName.empty() )
384  {
385  GetListAuthors();
386  }
387 
388  QString AuthorName;
389  for ( std::map< std::string, int >::iterator iter = m_MapAuthorIDName.begin();
390  iter != m_MapAuthorIDName.end() && AuthorName.isEmpty(); ++iter )
391  {
392  if ( iter->second == AuthorID )
393  {
394  AuthorName = ( *iter ).first.c_str();
395  if ( AuthorName.contains( QRegExp("<none>") ) )
396  {
397  AuthorName.chop(7);
398  }
399  }
400  }
401  lineAuthor->setText(AuthorName);
402  }
403 }
404 
405 //-------------------------------------------------------------------------
406 
407 //-------------------------------------------------------------------------
409 {
410  if ( OpenOrCreateProject == "Open" )
411  {
412  setField( "ProjectName", ChoiceProject->currentText() );
413  }
414  else
415  {
416  if ( lineNewProjectName->displayText() == "" )
417  {
418  QMessageBox msgBox;
419  msgBox.setText( tr("Please enter a name for your new project.") );
420  msgBox.exec();
421  return false;
422  }
423  if ( m_ListProject.contains(field("ProjectName").toString(), Qt::CaseInsensitive) )
424  {
425  QMessageBox msgBox;
426  msgBox.setText( tr("The name you entered for your project already exists.") );
427  msgBox.exec();
428  return false;
429  }
430  if ( ChoiceAuthor->currentText() == "" )
431  {
432  QMessageBox msgBox;
433  msgBox.setText( tr("Please select an Author for your project.") );
434  msgBox.exec();
435  return false;
436  }
437  CreateProject();
438  }
439  if ( m_DatabaseConnector == 0 )
440  {
442  }
444 
446  {
448  }
449  LeavingPage = true;
450 
451  return true;
452 }
453 
454 //-------------------------------------------------------------------------
455 
456 //-------------------------------------------------------------------------
458 {
460  {
462  }
463 }
464 
465 //-------------------------------------------------------------------------
466 
467 //-------------------------------------------------------------------------
469 {
470  if ( m_DatabaseConnector == 0 && !LeavingPage )
471  {
473  }
474 
475  if ( !ExistingImgSession )
476  {
478  }
479 
481 }
482 
483 //-------------------------------------------------------------------------
484 
485 //-------------------------------------------------------------------------
487 {
488  std::string Server = field("ServerName").toString().toStdString();
489  std::string User = field("User").toString().toStdString();
490  std::string Password = field("Password").toString().toStdString();
491  std::string DBName = field("DBName").toString().toStdString();
492 
493  m_DatabaseConnector = OpenDatabaseConnection(Server, User, Password, DBName);
494  LeavingPage = false;
495 }
496 
497 //-------------------------------------------------------------------------
498 
499 //-------------------------------------------------------------------------
501 {
502  if ( m_DatabaseConnector == 0 )
503  {
505  }
506 
507  std::vector< std::string > ListImgSessionID = ListSpecificValuesForOneColumn(
508  m_DatabaseConnector, "imagingsession", "imagingsessionID", "projectName",
509  field("ProjectName").toString().toStdString() );
510 
511  return !ListImgSessionID.empty();
512 }
513 
514 //-------------------------------------------------------------------------
515 
516 //-------------------------------------------------------------------------
518 {
519  this->OpenDBConnection();
520  this->GetListProject();
521 }
522 
523 //-------------------------------------------------------------------------
524 
525 //-------------------------------------------------------------------------
527 {
529 
530  CreateAuthorPage->SetDatabaseVariables(
531  field("User").toString().toStdString(),
532  field("Password").toString().toStdString() );
533  CreateAuthorPage->show();
534  QObject::connect( CreateAuthorPage, SIGNAL( NewAuthorCreated() ),
535  this, SLOT( UpdateListAuthors() ) );
536 }
537 
538 //-------------------------------------------------------------------------
539 
540 //-------------------------------------------------------------------------
542 {
543  QStringList ListAuthors = GetListAuthors();
544 
545  ChoiceAuthor->clear();
546  ChoiceAuthor->addItems(ListAuthors);
547 }
QGoOpenCreateProjectPage(QWidget *parent=0)
std::string toStdString() const
void clear()
in the QTextEdit class, there is no method to have a restriction on the number of characters the user...
QStringList GetListAuthors()
return the QStringList of the authors to be visualized by the user (concatenation of the firstname...
void CreateProject()
insert the new project in the database with the information filled by the user
QString toString(Qt::DateFormat format) const
void addWidget(QWidget *widget, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
void clear()
void setText(const QString &)
void registerField(const QString &name, QWidget *widget, const char *property, const char *changedSignal)
void setSubTitle(const QString &subTitle)
bool CloseDatabaseConnection(vtkMySQLDatabase *DatabaseConnector)
return true if the connection has been closed, false if the connection was already closed ...
void clear()
bool contains(const QString &str, Qt::CaseSensitivity cs) const
void ChangeToOpenProjectDisplay()
hides/shows the related QLabel,QLineEdit...and display the related information of the first existing ...
virtual void setVisible(bool visible)
vtkMySQLDatabase * OpenDatabaseConnection(std::string ServerName, std::string login, std::string Password, std::string DBName)
QString toPlainText() const
void ChangeToCreateProjectDisplay()
hides/shows the related QLabel,QLineEdit...and enables the fields where the user has to enter informa...
void chop(int n)
QString tr(const char *sourceText, const char *disambiguation, int n)
void clear()
void setField(const QString &name, const QVariant &value)
void setReadOnly(bool)
void setBold(bool enable)
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
void append(const T &value)
std::map< std::string, int > m_MapAuthorIDName
void setLayout(QLayout *layout)
std::vector< std::string > ListAllValuesForOneColumn(vtkMySQLDatabase *DatabaseConnector, const std::string &ColumnName, const std::string &TableName, std::string OrderByColumnName)
SELECT ColumnName from TableName ORDER BY OrderbyColumnName.
bool isEmpty() const
bool isEmpty() const
void setText(const QString &text)
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 &lt;&gt; 0 (if excludezero) ...
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 setText(const QString &)
void hide()
void DisplayInfoProject(QString ProjectName)
display the information related to the given existing project
QVariant field(const QString &name) const
void SetDatabaseVariables(std::string iUser, std::string iPassword)
bool contains(QChar ch, Qt::CaseSensitivity cs) const
void clear()
void setFont(const QFont &)
void setChecked(bool)
vtkMySQLDatabase * m_DatabaseConnector
bool setAlignment(QWidget *w, QFlags< Qt::AlignmentFlag > alignment)
void SetField(const std::string &key, const T &value)
convert the value into a string and assign it to the key in the map
Definition: GoDBRow.h:73
int count() const
void OpenDBConnection() const
open a connection to the database.
QDate currentDate()
int AuthorIDForNewProject()
return the AuthorID corresponding to the one in the database based on the name selected by the user i...
bool GetListProject() const
update the m_ListProject and return true if the list is not empty, false if there is no existing proj...
void addItems(const QStringList &texts)
bool DoesProjectHaveExistingImgSession() const
get the list of the existing imaging session for the selected project and return true if the list is ...
void show()
void setReadOnly(bool ro)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setText(const QString &text)
QString toString() const
manages a map with keys matching fields of the gofiguredatabase Project table and values of the map m...
void addLayout(QLayout *layout, int stretch)
void setMaxLength(int)