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

Generated on 27 Nov 2014 for Hugintrunk by  doxygen 1.4.7