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

Generated on 28 Sep 2016 for Hugintrunk by  doxygen 1.4.7