]> Creatis software - clitk.git/blob - vv/vv.cxx
Romulo: fixed log file, AppendOn was missing
[clitk.git] / vv / vv.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18 #include <utility>
19 #include <cassert>
20 #include <ctime>
21 #include <string>
22 #include <ostream>
23 #include <sstream>
24 #include <QApplication>
25 #include <QPixmap>
26 #include <QSplashScreen>
27 #include <QTimer>
28 #include <QDesktopWidget>
29 #include <QDir>
30
31 #include "clitkIO.h"
32 #include "vvMainWindow.h"
33 #include "vvConstants.h"
34
35 #include <vtkFileOutputWindow.h>
36 #include <vtkSmartPointer.h>
37 #include <itkFileOutputWindow.h>
38 #include <itkSmartPointer.h>
39 #include <itksys/SystemTools.hxx>
40
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include  <errno.h>
44
45 void load_image_first_error()
46 {
47   std::cerr << "You need to load an image before adding an overlay!" << std::endl;
48   exit(1);
49 }
50
51 std::string create_timed_string()
52 {
53   time_t t;
54   time(&t);
55
56   struct tm* pt = localtime(&t);
57
58   const int size = 64;
59   char st[size];
60   strftime(st, size, "%Y%m%d-%H%M%S", pt);
61
62   return st;
63 }
64
65 //------------------------------------------------------------------------------
66 int main( int argc, char** argv )
67 {
68   CLITK_INIT;
69
70   QApplication app( argc, argv );
71   Q_INIT_RESOURCE(vvIcons);
72   //QPixmap pixmap(":/splashscreen.PNG");
73   QSplashScreen *splash = new QSplashScreen(QPixmap(QString::fromUtf8(":/new/prefix1/splashscreen.PNG")));
74   /*splash->showMessage("VV 1.0 developped by Léon Bérard c`ancer center http://oncora1.lyon.fnclcc.fr and CREATIS-LRMN http://www.creatis.insa-lyon.fr",(Qt::AlignRight | Qt::AlignBottom));*/
75   //  splash->show();
76   QTimer::singleShot(2000, splash, SLOT(close()));
77   while (!splash->isHidden())
78     app.processEvents();
79
80   vvMainWindow window;
81
82   //Try to give the window a sensible default size
83   int width=QApplication::desktop()->width()*0.8;
84   int height=QApplication::desktop()->height()*0.9;
85   if (width> 1.5*height)
86     width=1.5*height;
87   window.resize(width,height);
88
89   window.show();
90
91   std::vector<std::string> sequence_filenames;
92   enum {P_NORMAL,P_SEQUENCE,P_WINDOW,P_LEVEL};
93   int parse_mode=P_NORMAL;
94   int n_image_loaded=0;
95   std::string win(""), lev("");
96
97   if (argc >1) {
98     for (int i = 1; i < argc; i++) {
99       std::string current = argv[i];
100       if (!current.compare(0,2,"--")) { //We are parsing an option
101         if (parse_mode == P_SEQUENCE) {//First finish the current sequence
102           window.LoadImages(sequence_filenames,MERGEDWITHTIME);
103           sequence_filenames.clear();
104           parse_mode=P_NORMAL;
105         }
106         if (current=="--vf") {
107           if (!n_image_loaded) load_image_first_error();
108           window.AddField(argv[i+1],n_image_loaded-1);
109           i++; //skip vf name
110         } else if (current=="--overlay") {
111           if (!n_image_loaded) load_image_first_error();
112           window.AddOverlayImage(n_image_loaded-1,argv[i+1]);
113           i++; //skip overlay name
114         } else if (current=="--roi") {
115           if (!n_image_loaded) load_image_first_error();
116           window.AddROI(n_image_loaded-1,argv[i+1]);
117           i++; //skip roi name
118         } else if (current=="--fusion") {
119           if (!n_image_loaded) load_image_first_error();
120           window.AddFusionImage(n_image_loaded-1,argv[i+1]);
121           i++; //skip fusion name
122         } else if (current == "--sequence") {
123           n_image_loaded++; //count only one for the sequence
124           parse_mode=P_SEQUENCE;
125         } else if (current == "--window") {
126           parse_mode=P_WINDOW;
127         } else if (current == "--level") {
128           parse_mode=P_LEVEL;
129         } else if (current == "--log") {
130           std::string log_dir = QDir::tempPath().toStdString() + std::string("/vv-log");
131
132           if(itksys::SystemTools::FileExists(log_dir.c_str()) &&
133               !itksys::SystemTools::FileIsDirectory(log_dir.c_str())) {
134             itkGenericExceptionMacro(<< "Error creating log directory, file exists and is not a directory.");
135           } else if(!itksys::SystemTools::MakeDirectory(log_dir.c_str())) {
136             itkGenericExceptionMacro(<< "Error creating log directory.");
137           }
138
139           std::string log_file = log_dir + "/" + create_timed_string() + ".log";
140
141           itk::SmartPointer<itk::FileOutputWindow> itk_log = itk::FileOutputWindow::New();
142           itk_log->SetFileName(log_file.c_str());
143           itk_log->FlushOn();
144           itk_log->AppendOn();
145           itk::OutputWindow::SetInstance(itk_log);
146
147           vtkSmartPointer<vtkFileOutputWindow> vtk_log = vtkFileOutputWindow::New();
148           vtk_log->SetFileName(log_file.c_str());
149           vtk_log->FlushOn();
150           vtk_log->AppendOn();
151           vtkOutputWindow::SetInstance(vtk_log);
152         }
153       } else if (parse_mode == P_SEQUENCE) {
154         sequence_filenames.push_back(current);
155       } else if (parse_mode == P_WINDOW) {
156         win=current;
157         parse_mode=P_NORMAL;
158       } else if (parse_mode == P_LEVEL) {
159         lev=current;
160         parse_mode=P_NORMAL;
161       } else {
162         std::vector<std::string> image;
163         image.push_back(current);
164         window.LoadImages(image,IMAGE);
165         n_image_loaded++;
166       }
167     }
168     if (parse_mode == P_SEQUENCE) { //Finish any current sequence
169       window.LoadImages(sequence_filenames,MERGEDWITHTIME);
170       sequence_filenames.clear();
171       parse_mode=P_NORMAL;
172     }
173   }
174
175   if(win!="" && lev!="") {
176     window.WindowLevelChanged(atof(win.c_str()), atof(lev.c_str()), 6, 0);
177     window.ApplyWindowLevelToAllImages();
178   }
179
180   return app.exec();
181 }