PTBatcherGUI.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00002 
00027 #include "PTBatcherGUI.h"
00028 #ifdef __WXMSW__
00029 #include <wx/cshelp.h>
00030 #include <wx/stdpaths.h>
00031 #endif
00032 #include "lensdb/LensDB.h"
00033 
00034 // make wxwindows use this class as the main application
00035 IMPLEMENT_APP(PTBatcherGUI)
00036 
00037 BEGIN_EVENT_TABLE(PTBatcherGUI, wxApp)
00038     EVT_LIST_ITEM_ACTIVATED(XRCID("project_listbox"),PTBatcherGUI::OnItemActivated)
00039     EVT_KEY_DOWN(PTBatcherGUI::OnKeyDown)
00040 END_EVENT_TABLE()
00041 
00042 bool PTBatcherGUI::OnInit()
00043 {
00044 #if wxUSE_ON_FATAL_EXCEPTION
00045     wxHandleFatalExceptions();
00046 #endif
00047     // Required to access the preferences of hugin
00048     SetAppName(wxT("hugin"));
00049 
00050 #if defined __WXMSW__
00051 #if defined _MSC_VER && defined Hugin_shared
00052     setlocale(LC_NUMERIC, "");
00053 #endif
00054     int localeID = wxConfigBase::Get()->Read(wxT("language"), (long) wxLANGUAGE_DEFAULT);
00055     m_locale.Init(localeID);
00056     // initialize help provider
00057     wxHelpControllerHelpProvider* provider = new wxHelpControllerHelpProvider;
00058     wxHelpProvider::Set(provider);
00059 
00060 #else
00061     m_locale.Init(wxLANGUAGE_DEFAULT);
00062 #endif
00063 
00064     // setup the environment for the different operating systems
00065 #if defined __WXMSW__
00066     wxFileName exePath(wxStandardPaths::Get().GetExecutablePath());
00067     exePath.RemoveLastDir();
00068     const wxString huginRoot(exePath.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR));
00069     m_xrcPrefix = huginRoot + wxT("share\\hugin\\xrc\\");
00070 
00071     // locale setup
00072     m_locale.AddCatalogLookupPathPrefix(huginRoot + wxT("share\\locale"));
00073 #elif defined __WXMAC__ && defined MAC_SELF_CONTAINED_BUNDLE
00074     {
00075         wxString exec_path = MacGetPathToBundledResourceFile(CFSTR("xrc"));
00076         if(exec_path != wxT(""))
00077         {
00078             m_xrcPrefix = exec_path + wxT("/");
00079         }
00080         else
00081         {
00082             wxMessageBox(_("xrc directory not found in bundle"), _("Fatal Error"));
00083             return false;
00084         }
00085 
00086     }
00087 #else
00088     // add the locale directory specified during configure
00089     m_xrcPrefix = wxT(INSTALL_XRC_DIR);
00090     m_locale.AddCatalogLookupPathPrefix(wxT(INSTALL_LOCALE_DIR));
00091 #endif
00092 
00093     // set the name of locale recource to look for
00094     m_locale.AddCatalog(wxT("hugin"));
00095 
00096     const wxString name = wxString::Format(_T("PTBatcherGUI-%s"), wxGetUserId().c_str());
00097     m_checker = new wxSingleInstanceChecker(name+wxT(".lock"),wxFileName::GetTempDir());
00098     bool IsFirstInstance=(!m_checker->IsAnotherRunning());
00099 
00100     if(IsFirstInstance)
00101     {
00102         if ( ! wxFile::Exists(m_xrcPrefix + wxT("/batch_frame.xrc")) )
00103         {
00104             wxMessageBox(_("xrc directory not found, hugin needs to be properly installed\nTried Path:") + m_xrcPrefix , _("Fatal Error"));
00105             return false;
00106         }
00107         // initialize image handlers
00108         wxInitAllImageHandlers();
00109 
00110         // Initialize all the XRC handlers.
00111         wxXmlResource::Get()->InitAllHandlers();
00112         wxXmlResource::Get()->AddHandler(new ProjectListBoxXmlHandler());
00113         // load XRC files
00114         wxXmlResource::Get()->Load(m_xrcPrefix + wxT("batch_frame.xrc"));
00115         wxXmlResource::Get()->Load(m_xrcPrefix + wxT("batch_toolbar.xrc"));
00116         wxXmlResource::Get()->Load(m_xrcPrefix + wxT("batch_menu.xrc"));
00117         wxXmlResource::Get()->Load(m_xrcPrefix + wxT("lensdb_dialogs.xrc"));
00118         wxXmlResource::Get()->Load(m_xrcPrefix + wxT("dlg_warning.xrc"));
00119     };
00120 
00121     // parse arguments
00122     static const wxCmdLineEntryDesc cmdLineDesc[] =
00123     {
00124         {
00125             wxCMD_LINE_SWITCH, "h", "help", "show this help message",
00126             wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP
00127         },
00128         { wxCMD_LINE_SWITCH, "b", "batch",  "run batch immediately" },
00129         { wxCMD_LINE_SWITCH, "o", "overwrite",  "overwrite previous files without asking" },
00130         { wxCMD_LINE_SWITCH, "s", "shutdown",  "shutdown computer after batch is complete" },
00131         { wxCMD_LINE_SWITCH, "v", "verbose",  "show verbose output when processing projects" },
00132         { wxCMD_LINE_SWITCH, "a", "assistant", "run the assistant on the given projects" },
00133         {
00134             wxCMD_LINE_PARAM,  NULL, NULL, "stitch_project.pto [output prefix]|assistant_project.pto",
00135             wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL + wxCMD_LINE_PARAM_MULTIPLE
00136         },
00137         { wxCMD_LINE_NONE }
00138     };
00139     wxCmdLineParser parser(cmdLineDesc, argc, argv);
00140 
00141     switch ( parser.Parse() )
00142     {
00143         case -1: // -h or --help was given, and help displayed so exit
00144             return false;
00145             break;
00146         case 0:  // all is well
00147             break;
00148         default:
00149             wxLogError(_("Syntax error in parameters detected, aborting."));
00150             return false;
00151             break;
00152     }
00153 
00154     wxClient client;
00155     wxConnectionBase* conn;
00156     wxString servername;
00157 #ifdef _WIN32
00158     servername=name;
00159 #else
00160     servername=wxFileName::GetTempDir()+wxFileName::GetPathSeparator()+name+wxT(".ipc");
00161 #endif
00162     if(IsFirstInstance)
00163     {
00164         m_frame = new BatchFrame(&m_locale,m_xrcPrefix);
00165         m_frame->RestoreSize();
00166 #ifdef __WXMSW__
00167         provider->SetHelpController(&m_frame->GetHelpController());
00168         m_frame->GetHelpController().Initialize(m_xrcPrefix+wxT("data/hugin_help_en_EN.chm"));
00169 #endif
00170         SetTopWindow(m_frame);
00171         if(!(m_frame->IsStartedMinimized()))
00172         {
00173             m_frame->Show(true);
00174         }
00175         else
00176         {
00177             m_frame->SetStatusInformation(_("PTBatcherGUI started"));
00178         };
00179         m_server = new BatchIPCServer();
00180         if (!m_server->Create(servername))
00181         {
00182             delete m_server;
00183             m_server = NULL;
00184         };
00185     }
00186     else
00187     {
00188         conn=client.MakeConnection(wxEmptyString, servername, IPC_START);
00189         if(!conn)
00190         {
00191             return false;
00192         }
00193     };
00194 
00195     size_t count = 0;
00196     if (parser.Found(wxT("a")))
00197     {
00198         //added assistant files
00199         while (parser.GetParamCount() > count)
00200         {
00201             wxString param = parser.GetParam(count);
00202             count++;
00203             wxFileName name(param);
00204             name.MakeAbsolute();
00205             if (name.FileExists())
00206             {
00207                 //only add existing pto files
00208                 if (name.GetExt().CmpNoCase(wxT("pto")) == 0)
00209                 {
00210                     if (IsFirstInstance)
00211                     {
00212                         m_frame->AddToList(name.GetFullPath(), Project::DETECTING);
00213                     }
00214                     else
00215                     {
00216                         conn->Request(wxT("D ") + name.GetFullPath());
00217                     };
00218                 };
00219             };
00220         };
00221     }
00222     else
00223     {
00224         bool projectSpecified = false;
00225         //we collect all parameters - all project files <and their output prefixes>
00226         while (parser.GetParamCount() > count)
00227         {
00228             wxString param = parser.GetParam(count);
00229             count++;
00230             if (!projectSpecified)      //next parameter must be new script file
00231             {
00232                 wxFileName name(param);
00233                 name.MakeAbsolute();
00234                 if (IsFirstInstance)
00235                 {
00236                     m_frame->AddToList(name.GetFullPath());
00237                 }
00238                 else
00239                 {
00240                     conn->Request(wxT("A ") + name.GetFullPath());
00241                 }
00242                 projectSpecified = true;
00243             }
00244             else        //parameter could be previous project's output prefix
00245             {
00246                 wxFileName fn(param);
00247                 fn.MakeAbsolute();
00248                 if (!fn.HasExt())       //if there is no extension we have a prefix
00249                 {
00250                     if (IsFirstInstance)
00251                     {
00252                         m_frame->ChangePrefix(-1, fn.GetFullPath());
00253                     }
00254                     else
00255                     {
00256                         conn->Request(wxT("P ") + fn.GetFullPath());
00257                     }
00258                     projectSpecified = false;
00259                 }
00260                 else
00261                 {
00262                     wxString ext = fn.GetExt();
00263                     //we may still have a prefix, but with added image extension
00264                     if (ext.CmpNoCase(wxT("jpg")) == 0 || ext.CmpNoCase(wxT("jpeg")) == 0 ||
00265                         ext.CmpNoCase(wxT("tif")) == 0 || ext.CmpNoCase(wxT("tiff")) == 0 ||
00266                         ext.CmpNoCase(wxT("png")) == 0 || ext.CmpNoCase(wxT("exr")) == 0 ||
00267                         ext.CmpNoCase(wxT("pnm")) == 0 || ext.CmpNoCase(wxT("hdr")) == 0)
00268                     {
00269                         //extension will be removed before stitch, so there is no need to do it now
00270                         if (IsFirstInstance)
00271                         {
00272                             m_frame->ChangePrefix(-1, fn.GetFullPath());
00273                         }
00274                         else
00275                         {
00276                             conn->Request(wxT("P ") + fn.GetFullPath());
00277                         }
00278                         projectSpecified = false;
00279                     }
00280                     else //if parameter has a different extension we presume it is a new script file
00281                     {
00282                         //we add the new project
00283                         if (IsFirstInstance)
00284                         {
00285                             m_frame->AddToList(fn.GetFullPath());
00286                         }
00287                         else
00288                         {
00289                             conn->Request(wxT("A ") + fn.GetFullPath());
00290                         }
00291                         projectSpecified = true;
00292                     }
00293                 } //else of if(!fn.HasExt())
00294             }
00295         }
00296     };
00297 
00298     if(IsFirstInstance)
00299     {
00300         wxConfigBase* config=wxConfigBase::Get();
00301         if (parser.Found(wxT("s")))
00302         {
00303             config->DeleteEntry(wxT("/BatchFrame/ShutdownCheck"));
00304 #if !defined __WXMAC__ && !defined __WXOSX_COCOA__
00305             // wxMac has not wxShutdown
00306             config->Write(wxT("/BatchFrame/AtEnd"), static_cast<long>(Batch::SHUTDOWN));
00307 #endif
00308         }
00309         if (parser.Found(wxT("o")))
00310         {
00311             config->Write(wxT("/BatchFrame/OverwriteCheck"), 1l);
00312         }
00313         if (parser.Found(wxT("v")))
00314         {
00315             config->Write(wxT("/BatchFrame/VerboseCheck"), 1l);
00316         }
00317         config->Flush();
00318     }
00319     else
00320     {
00321         if (parser.Found(wxT("s")))
00322         {
00323 #if !defined __WXMAC__ && !defined __WXOSX_COCOA__
00324             // wxMac has not wxShutdown
00325             conn->Request(wxT("SetShutdownCheck"));
00326 #endif
00327         }
00328         if (parser.Found(wxT("o")))
00329         {
00330             conn->Request(wxT("SetOverwriteCheck"));
00331         }
00332         if (parser.Found(wxT("v")))
00333         {
00334             conn->Request(wxT("SetVerboseCheck"));
00335         }
00336         conn->Request(wxT("BringWindowToTop"));
00337         if(parser.Found(wxT("b")))
00338         {
00339             conn->Request(wxT("RunBatch"));
00340         }
00341         conn->Disconnect();
00342         delete conn;
00343         delete m_checker;
00344         return false;
00345     };
00346     m_frame->SetCheckboxes();
00347     m_frame->PropagateDefaults();
00348     //deactivate verbose output if started minimized
00349     if(m_frame->IsStartedMinimized())
00350     {
00351         m_frame->SetInternalVerbose(false);
00352     };
00353     if (parser.Found(wxT("b")) )
00354     {
00355         m_frame->RunBatch();
00356     }
00357 #ifdef __WXMAC__
00358     // if there are command line parameters they are handled all by the code above
00359     // but wxMac calls also MacOpenFiles after OnInit with the command line
00360     // parameters again, so the files get added twice. To prevent this we
00361     // reset the internal file list for MacOpenFiles here
00362     // but we need to process the files when PTBatcherGUI is called from finder
00363     // with an open files AppleEvent, in this case the argv is empty (except argc[0])
00364     // and we don't want to skip the MacOpenFile function
00365     if (argc > 1)
00366     {
00367         wxArrayString emptyFiles;
00368         OSXStoreOpenFiles(emptyFiles);
00369     };
00370 #endif
00371     return true;
00372 }
00373 
00374 int PTBatcherGUI::OnExit()
00375 {
00376     HuginBase::LensDB::LensDB::Clean();
00377     delete m_checker;
00378     delete m_server;
00379     return 0;
00380 }
00381 
00382 #if wxUSE_ON_FATAL_EXCEPTION
00383 void PTBatcherGUI::OnFatalException()
00384 {
00385     GenerateReport(wxDebugReport::Context_Exception);
00386 };
00387 #endif
00388 
00389 void PTBatcherGUI::OnItemActivated(wxListEvent& event)
00390 {
00391     wxCommandEvent dummy;
00392     m_frame->OnButtonOpenWithHugin(dummy);
00393 }
00394 
00395 void PTBatcherGUI::OnKeyDown(wxKeyEvent& event)
00396 {
00397     wxCommandEvent dummy;
00398     switch(event.GetKeyCode())
00399     {
00400         case WXK_DELETE:
00401             m_frame->OnButtonRemoveFromList(dummy);
00402             break;
00403         case WXK_INSERT:
00404             m_frame->OnButtonAddToStitchingQueue(dummy);
00405             break;
00406         case WXK_ESCAPE:
00407             m_frame->OnButtonCancel(dummy);
00408             break;
00409         default:
00410             event.Skip();
00411             break;
00412     }
00413 
00414 }
00415 
00416 #ifdef __WXMAC__
00417 // wx calls this method when the app gets "Open files" AppleEvent
00418 void PTBatcherGUI::MacOpenFiles(const wxArrayString &fileNames) 
00419 {
00420     if(m_frame)
00421     {
00422         for (int i = 0; i < fileNames.GetCount(); ++i)
00423         {
00424             wxFileName fn(fileNames[i]);
00425             m_frame->AddToList(fn.GetFullPath());
00426         };
00427     };
00428 }
00429 #endif
00430 
00431 const void* BatchIPCConnection::OnRequest(const wxString& topic, const wxString& item, size_t* size, wxIPCFormat format)
00432 {
00433     *size=wxNO_LEN;
00434     BatchFrame* MyBatchFrame=wxGetApp().GetFrame();
00435     if(item.Left(1)==wxT("A"))
00436     {
00437         MyBatchFrame->AddToList(item.Mid(2));
00438         return wxEmptyString;
00439     };
00440     if(item.Left(1)==wxT("D"))
00441     {
00442         MyBatchFrame->AddToList(item.Mid(2),Project::DETECTING);
00443         return wxEmptyString;
00444     };
00445     if(item.Left(1)==wxT("P"))
00446     {
00447         MyBatchFrame->ChangePrefix(-1,item.Mid(2));
00448         return wxEmptyString;
00449     };
00450     wxCommandEvent event;
00451     event.SetInt(1);
00452 #if !defined __WXMAC__ && !defined __WXOSX_COCOA__
00453     // wxMac has not wxShutdown
00454     if(item==wxT("SetShutdownCheck"))
00455         if (MyBatchFrame->GetEndTask()!=Batch::SHUTDOWN)
00456         {
00457             wxCommandEvent choiceEvent;
00458             choiceEvent.SetInt(Batch::SHUTDOWN);
00459             MyBatchFrame->OnChoiceEnd(choiceEvent);
00460             MyBatchFrame->SetCheckboxes();
00461         };
00462 #endif
00463     if(item==wxT("SetOverwriteCheck"))
00464         if(!MyBatchFrame->GetCheckOverwrite())
00465         {
00466             MyBatchFrame->OnCheckOverwrite(event);
00467             MyBatchFrame->SetCheckboxes();
00468         };
00469     if(item==wxT("SetVerboseCheck"))
00470         if(!MyBatchFrame->GetCheckVerbose())
00471         {
00472             MyBatchFrame->OnCheckVerbose(event);
00473             MyBatchFrame->SetCheckboxes();
00474         };
00475     if(item==wxT("BringWindowToTop"))
00476     {
00477         MyBatchFrame->RequestUserAttention();
00478     }
00479     if(item==wxT("RunBatch"))
00480     {
00481         wxCommandEvent myEvent(wxEVT_COMMAND_TOOL_CLICKED ,XRCID("tool_start"));
00482         MyBatchFrame->GetEventHandler()->AddPendingEvent(myEvent);
00483     };
00484     return wxEmptyString;
00485 };
00486 
00487 wxConnectionBase* BatchIPCServer::OnAcceptConnection (const wxString& topic)
00488 {
00489     if(topic==IPC_START)
00490     {
00491         return new BatchIPCConnection;
00492     }
00493     return NULL;
00494 };

Generated on 26 Aug 2016 for Hugintrunk by  doxygen 1.4.7