RunStitchPanel.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00002 
00025 #include <hugin_config.h>
00026 #include "panoinc_WX.h"
00027 #include "panoinc.h"
00028 
00029 #include <wx/wfstream.h>
00030 #include <wx/stdpaths.h>
00031 
00032 #include <fstream>
00033 #include <sstream>
00034 #include <vigra/error.hxx>
00035 #include "base_wx/huginConfig.h"
00036 #include "base_wx/MyExternalCmdExecDialog.h"
00037 #include "base_wx/Executor.h"
00038 #include "base_wx/AssistantExecutor.h"
00039 #include "base_wx/StitchingExecutor.h"
00040 
00041 #include "base_wx/platform.h"
00042 #include "base_wx/wxPlatform.h"
00043 
00044 #include "RunStitchPanel.h"
00045 
00046 #include "hugin/config_defaults.h"
00047 
00048 using namespace vigra;
00049 using namespace std;
00050 using namespace hugin_utils;
00051 
00052 
00053 // ==========================================
00054 // Implementation of stitch window
00055 
00056 // event ID's for RunStitchPanel
00057 enum
00058 {
00059     ID_Quit = 1,
00060     ID_About   
00061 };
00062 
00063 
00064 BEGIN_EVENT_TABLE(RunStitchPanel, wxPanel)
00065     EVT_END_PROCESS(-1, RunStitchPanel::OnProcessTerminate)
00066 END_EVENT_TABLE()
00067 
00068 RunStitchPanel::RunStitchPanel(wxWindow * parent)
00069     : wxPanel(parent)
00070 {
00071         m_paused=false;
00072         m_overwrite=false;
00073     /*
00074     wxMenu *menuFile = new wxMenu;
00075 
00076     menuFile->AppendSeparator();
00077     menuFile->Append( ID_Quit, _("E&xit") );
00078 
00079     wxMenu *menuHelp = new wxMenu;
00080     menuHelp->Append( ID_About, _("&About...") );
00081 
00082     wxMenuBar *menuBar = new wxMenuBar;
00083     menuBar->Append( menuFile, _("&File") );
00084     menuBar->Append( menuHelp, _("&Help") );
00085     SetMenuBar( menuBar );
00086     */
00087 
00088     wxBoxSizer * topsizer = new wxBoxSizer( wxVERTICAL );
00089     m_execPanel = new MyExecPanel(this);
00090 
00091 #ifdef __WXMSW__
00092     // wxFrame does have a strange background color on Windows, copy color from a child widget
00093     this->SetBackgroundColour(m_execPanel->GetBackgroundColour());
00094 #endif
00095     topsizer->Add(m_execPanel, 1, wxEXPAND, 0);
00096     SetSizer( topsizer );
00097 //    topsizer->SetSizeHints( this );   // set size hints to honour minimum size
00098 }
00099 
00100 bool RunStitchPanel::StitchProject(const wxString& scriptFile, const wxString& outname, const wxString& userDefinedOutput)
00101 {
00102     DEBUG_TRACE("");
00103     wxFileName fname(scriptFile);
00104     if ( !fname.FileExists() ) {
00105         wxLogError( _("Could not open project file:") + scriptFile);
00106         return false;
00107     }
00108 
00109     wxString pathToPTO;
00110     wxFileName::SplitPath(scriptFile, &pathToPTO, NULL, NULL);
00111     pathToPTO.Append(wxFileName::GetPathSeparator());
00112 
00113     ifstream prjfile((const char *)scriptFile.mb_str(HUGIN_CONV_FILENAME));
00114     if (prjfile.bad()) {
00115         wxLogError( wxString::Format(_("could not open script: %s"), scriptFile.c_str()) );
00116         return false;
00117     }
00118     HuginBase::Panorama pano;
00119     HuginBase::PanoramaMemento newPano;
00120     int ptoVersion = 0;
00121     if (newPano.loadPTScript(prjfile, ptoVersion, (const char *)pathToPTO.mb_str(HUGIN_CONV_FILENAME))) {
00122         pano.setMemento(newPano);
00123         if (pano.getActiveImages().empty())
00124         {
00125             wxLogError(wxString::Format(_("Project %s does not contain any active images."), scriptFile.c_str()));
00126             return false;
00127         }
00128         HuginBase::PanoramaOptions opts = pano.getOptions();
00129         if (ptoVersion < 2) {
00130             // no options stored in file, use default arguments in config
00131                         
00132                         wxConfig* config = new wxConfig(wxT("hugin"));  //needed for PTBatcher console application
00133                         wxConfigBase::Set(config);                      //
00134                         opts.enblendOptions = wxConfigBase::Get()->Read(wxT("/Enblend/Args"), wxT(HUGIN_ENBLEND_ARGS)).mb_str(wxConvLocal);
00135                         opts.enfuseOptions = wxConfigBase::Get()->Read(wxT("/Enfuse/Args"), wxT(HUGIN_ENFUSE_ARGS)).mb_str(wxConvLocal);
00136 
00137         }
00138         opts.remapUsingGPU = wxConfigBase::Get()->Read(wxT("/Nona/UseGPU"), HUGIN_NONA_USEGPU) == 1;
00139         pano.setOptions(opts);
00140     } else {
00141         wxLogError( wxString::Format(_("error while parsing panotools script: %s"), scriptFile.c_str()) );
00142         return false;
00143     }
00144     // get options and correct for correct makefile
00145     HuginBase::PanoramaOptions opts = pano.getOptions();
00146     if (!userDefinedOutput.IsEmpty())
00147     {
00148         wxFileInputStream input(userDefinedOutput);
00149         if (!input.IsOk())
00150         {
00151             wxLogError(wxString::Format(_("Can't open user defined output sequence \"%s\"."), userDefinedOutput.c_str()));
00152             return false;
00153         }
00154         wxFileConfig settings(input);
00155         // disable cropped output if user defined setting is requesting
00156         long supportsCroppedOutput;
00157         settings.Read(wxT("/General/SupportsCroppedTIFF"), &supportsCroppedOutput, 1l);
00158         if (supportsCroppedOutput != 1)
00159         {
00160             opts.tiff_saveROI = false;
00161         };
00162     };
00163     opts.outputFormat = HuginBase::PanoramaOptions::TIFF_m;
00164     if (opts.enblendOptions.length() == 0) {
00165         // no options stored in file, use default arguments in config file
00166         opts.enblendOptions = wxConfigBase::Get()->Read(wxT("/Enblend/Args"), wxT(HUGIN_ENBLEND_ARGS)).mb_str(wxConvLocal);
00167     }
00168     pano.setOptions(opts);
00169     DEBUG_DEBUG("output file specified is " << (const char *)outname.mb_str(wxConvLocal));
00170 
00171     wxString basename;
00172     wxString outpath;
00173     wxFileName outputPrefix(outname);
00174     outputPrefix.MakeAbsolute();
00175     outpath = outputPrefix.GetPath();
00176     basename = outputPrefix.GetFullName();
00177     //get temp dir from preferences
00178     wxString tempDir= wxConfigBase::Get()->Read(wxT("tempDir"),wxT(""));
00179     if(!tempDir.IsEmpty())
00180         if(tempDir.Last()!=wxFileName::GetPathSeparator())
00181             tempDir.Append(wxFileName::GetPathSeparator());
00182 
00183     try {
00184         // copy pto file to temporary file
00185         m_currentPTOfn = wxFileName::CreateTempFileName(tempDir+wxT("huginpto_"));
00186         if(m_currentPTOfn.size() == 0) {
00187             wxLogError(_("Could not create temporary file"));
00188         }
00189         DEBUG_DEBUG("tmpPTOfn file: " << (const char *)m_currentPTOfn.mb_str(wxConvLocal));
00190         // copy is not enough, need to adjust image path names...
00191         ofstream script(m_currentPTOfn.mb_str(HUGIN_CONV_FILENAME));
00192         HuginBase::UIntSet all;
00193         fill_set(all, 0, pano.getNrOfImages()-1);
00194         pano.printPanoramaScript(script, pano.getOptimizeVector(), pano.getOptions(), all, false, "");
00195         script.close();
00196 
00197         // cd to output directory, if one is given.
00198         m_oldCwd = wxFileName::GetCwd();
00199         wxFileName::SetCwd(outpath);
00200         const wxFileName exePath(wxStandardPaths::Get().GetExecutablePath());
00201         wxArrayString outputFiles;
00202         wxString statusText;
00203         m_tempFiles.clear();
00204         HuginQueue::CommandQueue* commands;
00205         if (userDefinedOutput.IsEmpty())
00206         {
00207             commands = HuginQueue::GetStitchingCommandQueue(pano, exePath.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR), m_currentPTOfn, basename, statusText, outputFiles, m_tempFiles);
00208         }
00209         else
00210         {
00211             commands= HuginQueue::GetStitchingCommandQueueUserOutput(pano, exePath.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR), m_currentPTOfn, basename, userDefinedOutput, statusText, outputFiles, m_tempFiles);
00212         };
00213         if (commands->empty())
00214         {
00215             wxMessageBox(_("Queue is empty. This should never happen.") , _("Error during stitching"), wxICON_ERROR | wxOK);
00216             return false;
00217         };
00218         // check output directories.
00219         wxString overwrittenFiles;
00220         if (!m_overwrite)
00221         {
00222             for (size_t i = 0; i < outputFiles.size(); i++)
00223             {
00224                 wxString fn(outputFiles[i]);
00225                 if (wxFile::Exists(fn))
00226                 {
00227                     overwrittenFiles.Append(fn + wxT(" "));
00228                 };
00229             };
00230 
00231             if (!overwrittenFiles.IsEmpty())
00232             {
00233                 int overwriteret = wxMessageBox(_("Overwrite existing images?\n\n") + overwrittenFiles, _("Overwrite existing images"), wxYES_NO | wxICON_QUESTION);
00234                 // TODO: change button label ok to overwrite
00235                 if (overwriteret != wxYES) {
00236                     DEBUG_DEBUG("Abort, do not overwrite images!");
00237                     return false;
00238                 }
00239                 DEBUG_DEBUG("Overwrite existing images!");
00240             };
00241         };
00242         if (!statusText.empty())
00243         {
00244             m_execPanel->AddString(statusText);
00245         };
00246         if (m_execPanel->ExecQueue(commands) == -1)
00247         {
00248             wxMessageBox(wxString::Format(_("Error while stitching project\n%s"), scriptFile.c_str()),
00249                              _("Error during stitching"),  wxICON_ERROR | wxOK );
00250         };
00251     } catch (std::exception & e)
00252     {
00253         cerr << "caught exception: " << e.what() << std::endl;
00254         wxMessageBox(wxString(e.what(), wxConvLocal),
00255                      _("Error during stitching"), wxICON_ERROR | wxOK );
00256     }
00257     return true;
00258 }
00259 
00260 bool RunStitchPanel::DetectProject(const wxString& scriptFile)
00261 {
00262     m_currentPTOfn=wxEmptyString;
00263     wxFileName fname(scriptFile);
00264     if ( !fname.FileExists() ) {
00265         wxLogError( _("Could not open project file:") + scriptFile);
00266         return false;
00267     }
00268     // get path to project file
00269     wxString pathToPTO;
00270     wxFileName::SplitPath(scriptFile, &pathToPTO, NULL, NULL);
00271     pathToPTO.Append(wxFileName::GetPathSeparator());
00272 
00273     //read project file
00274     ifstream prjfile((const char *)scriptFile.mb_str(HUGIN_CONV_FILENAME));
00275     if (prjfile.bad())
00276     {
00277         wxLogError( wxString::Format(_("could not open script: %s"), scriptFile.c_str()));
00278         return false;
00279     }
00280     HuginBase::Panorama pano;
00281     HuginBase::PanoramaMemento newPano;
00282     int ptoVersion = 0;
00283     if (!newPano.loadPTScript(prjfile, ptoVersion, (const char *)pathToPTO.mb_str(HUGIN_CONV_FILENAME)))
00284     {
00285         wxLogError(wxString::Format(_("error while parsing panotools script: %s"), scriptFile.c_str()));
00286         return false;
00287     }
00288     pano.setMemento(newPano);
00289 
00290     //read settings
00291     wxString tempDir= wxConfigBase::Get()->Read(wxT("tempDir"),wxT(""));
00292     if (!tempDir.IsEmpty())
00293     {
00294         if (tempDir.Last() != wxFileName::GetPathSeparator())
00295         {
00296             tempDir.Append(wxFileName::GetPathSeparator());
00297         };
00298     };
00299 
00300     try {
00301         // prepare running assistant
00302         fname.Normalize();
00303         const wxFileName exePath(wxStandardPaths::Get().GetExecutablePath());
00304         HuginQueue::CommandQueue* commands = HuginQueue::GetAssistantCommandQueue(pano, exePath.GetPath(wxPATH_GET_VOLUME|wxPATH_GET_SEPARATOR), fname.GetFullPath());
00305         if (commands->empty())
00306         {
00307             wxMessageBox(_("Queue is empty. This should never happen."), _("Error during running assistant"), wxICON_ERROR | wxOK);
00308             return false;
00309         };
00310         if (m_execPanel->ExecQueue(commands) == -1)
00311         {
00312             wxMessageBox(wxString::Format(_("Error while running assistant\n%s"), scriptFile.c_str()),
00313                          _("Error during running assistant"),  wxICON_ERROR | wxOK );
00314         }
00315     } 
00316     catch (std::exception & e)
00317     {
00318         cerr << "caught exception: " << e.what() << std::endl;
00319         wxMessageBox(wxString(e.what(), wxConvLocal),
00320                      _("Error during running assistant"), wxICON_ERROR | wxOK );
00321     }
00322     return true;
00323 }
00324 
00325 void RunStitchPanel::OnProcessTerminate(wxProcessEvent & event)
00326 {
00327     DEBUG_TRACE("");
00328 #ifndef DEBUG
00329     if(!m_currentPTOfn.IsEmpty())
00330     {
00331         wxRemoveFile(m_currentPTOfn);
00332     };
00333 #endif
00334     // delete all temp files
00335     if (!m_tempFiles.empty())
00336     {
00337         for (size_t i = 0; i < m_tempFiles.size(); ++i)
00338         {
00339             if (wxFileExists(m_tempFiles[i]))
00340             {
00341                 wxRemoveFile(m_tempFiles[i]);
00342             };
00343         };
00344     };
00345     // restore old working directory, if changed
00346     if (!m_oldCwd.IsEmpty())
00347     {
00348         wxFileName::SetCwd(m_oldCwd);
00349     };
00350     // notify parent of exit
00351     if (this->GetParent())
00352     {
00353         event.SetEventObject( this );
00354         DEBUG_TRACE("Sending wxProcess event");   
00355         this->GetParent()->GetEventHandler()->ProcessEvent( event );
00356     }
00357 }
00358 
00359 void RunStitchPanel::CancelStitch()
00360 {
00361     DEBUG_TRACE("");
00362     m_execPanel->KillProcess();
00363 }
00364 
00365 bool RunStitchPanel::IsPaused()
00366 {
00367         return m_paused;
00368 }
00369 
00370 void RunStitchPanel::SetOverwrite(bool over)
00371 {
00372         m_overwrite = over;
00373 }
00374 
00375 void RunStitchPanel::PauseStitch()
00376 {
00377         m_paused=true;
00378         m_execPanel->PauseProcess();
00379 }
00380 
00381 void RunStitchPanel::ContinueStitch()
00382 {
00383         m_execPanel->ContinueProcess();
00384         m_paused=false;
00385 }
00386 
00387 long RunStitchPanel::GetPid()
00388 {
00389         return m_execPanel->GetPid();
00390 }
00391 
00392 bool RunStitchPanel::SaveLog(const wxString &filename)
00393 {
00394     return m_execPanel->SaveLog(filename);
00395 };

Generated on 2 Aug 2015 for Hugintrunk by  doxygen 1.4.7