PTBatcherGUI.cpp

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

Generated on 7 Dec 2016 for Hugintrunk by  doxygen 1.4.7