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

Generated on 31 Jul 2015 for Hugintrunk by  doxygen 1.4.7