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

Generated on 28 May 2016 for Hugintrunk by  doxygen 1.4.7