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 #if defined USE_GDKBACKEND_X11
00036 // wxWidgets does not support wxTaskBarIcon::IsAvailable on Wayland
00037 // so until it is fixed upstream enforce using x11 backendj
00038 // see ticket http://trac.wxwidgets.org/ticket/17779
00039 #warning Using Hugin with hard coded GDK_BACKEND=x11
00040 wxIMPLEMENT_WX_THEME_SUPPORT
00041 wxIMPLEMENT_APP_NO_MAIN(PTBatcherGUI);
00042 #include <stdlib.h>
00043 int main(int argc, char **argv)
00044 {   
00045     wxDISABLE_DEBUG_SUPPORT();
00046     char backend[]="GDK_BACKEND=x11";
00047     putenv(backend);
00048     return wxEntry(argc, argv);
00049 };
00050 #else
00051 wxIMPLEMENT_APP(PTBatcherGUI);
00052 #endif
00053 
00054 BEGIN_EVENT_TABLE(PTBatcherGUI, wxApp)
00055     EVT_LIST_ITEM_ACTIVATED(XRCID("project_listbox"),PTBatcherGUI::OnItemActivated)
00056     EVT_KEY_DOWN(PTBatcherGUI::OnKeyDown)
00057 END_EVENT_TABLE()
00058 
00059 bool PTBatcherGUI::OnInit()
00060 {
00061 #if wxUSE_ON_FATAL_EXCEPTION
00062     wxHandleFatalExceptions();
00063 #endif
00064     // Required to access the preferences of hugin
00065     SetAppName(wxT("hugin"));
00066 
00067 #if defined __WXMSW__
00068 #if defined _MSC_VER && defined Hugin_shared
00069     setlocale(LC_NUMERIC, "");
00070 #endif
00071     int localeID = wxConfigBase::Get()->Read(wxT("language"), (long) wxLANGUAGE_DEFAULT);
00072     m_locale.Init(localeID);
00073 #else
00074     m_locale.Init(wxLANGUAGE_DEFAULT);
00075 #endif
00076     // initialize help provider
00077     wxHelpControllerHelpProvider* provider = new wxHelpControllerHelpProvider;
00078     wxHelpProvider::Set(provider);
00079 
00080     // setup the environment for the different operating systems
00081 #if defined __WXMSW__
00082     wxFileName exePath(wxStandardPaths::Get().GetExecutablePath());
00083     exePath.RemoveLastDir();
00084     const wxString huginRoot(exePath.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR));
00085     m_xrcPrefix = huginRoot + wxT("share\\hugin\\xrc\\");
00086 
00087     // locale setup
00088     m_locale.AddCatalogLookupPathPrefix(huginRoot + wxT("share\\locale"));
00089 #elif defined __WXMAC__ && defined MAC_SELF_CONTAINED_BUNDLE
00090     {
00091         wxString exec_path = MacGetPathToBundledResourceFile(CFSTR("xrc"));
00092         if(exec_path != wxT(""))
00093         {
00094             m_xrcPrefix = exec_path + wxT("/");
00095         }
00096         else
00097         {
00098             wxMessageBox(_("xrc directory not found in bundle"), _("Fatal Error"));
00099             return false;
00100         }
00101 
00102     }
00103 #else
00104     // add the locale directory specified during configure
00105     m_xrcPrefix = wxT(INSTALL_XRC_DIR);
00106     m_locale.AddCatalogLookupPathPrefix(wxT(INSTALL_LOCALE_DIR));
00107 #endif
00108 
00109     // set the name of locale recource to look for
00110     m_locale.AddCatalog(wxT("hugin"));
00111 
00112     const wxString name = wxString::Format(_T("PTBatcherGUI-%s"), wxGetUserId().c_str());
00113     m_checker = new wxSingleInstanceChecker(name+wxT(".lock"),wxFileName::GetTempDir());
00114     bool IsFirstInstance=(!m_checker->IsAnotherRunning());
00115 
00116     if(IsFirstInstance)
00117     {
00118         if ( ! wxFile::Exists(m_xrcPrefix + wxT("/batch_frame.xrc")) )
00119         {
00120             wxMessageBox(_("xrc directory not found, hugin needs to be properly installed\nTried Path:") + m_xrcPrefix , _("Fatal Error"));
00121             return false;
00122         }
00123         // initialize image handlers
00124         wxInitAllImageHandlers();
00125 
00126         // Initialize all the XRC handlers.
00127         wxXmlResource::Get()->InitAllHandlers();
00128         wxXmlResource::Get()->AddHandler(new ProjectListBoxXmlHandler());
00129         // load XRC files
00130         wxXmlResource::Get()->Load(m_xrcPrefix + wxT("batch_frame.xrc"));
00131         wxXmlResource::Get()->Load(m_xrcPrefix + wxT("batch_toolbar.xrc"));
00132         wxXmlResource::Get()->Load(m_xrcPrefix + wxT("batch_menu.xrc"));
00133         wxXmlResource::Get()->Load(m_xrcPrefix + wxT("lensdb_dialogs.xrc"));
00134         wxXmlResource::Get()->Load(m_xrcPrefix + wxT("dlg_warning.xrc"));
00135     };
00136 
00137     // parse arguments
00138     static const wxCmdLineEntryDesc cmdLineDesc[] =
00139     {
00140         {
00141             wxCMD_LINE_SWITCH, "h", "help", "show this help message",
00142             wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP
00143         },
00144         { wxCMD_LINE_SWITCH, "b", "batch",  "run batch immediately" },
00145         { wxCMD_LINE_SWITCH, "o", "overwrite",  "overwrite previous files without asking" },
00146         { wxCMD_LINE_SWITCH, "s", "shutdown",  "shutdown computer after batch is complete" },
00147         { wxCMD_LINE_SWITCH, "v", "verbose",  "show verbose output when processing projects" },
00148         { wxCMD_LINE_SWITCH, "a", "assistant", "run the assistant on the given projects" },
00149         {
00150             wxCMD_LINE_PARAM,  NULL, NULL, "stitch_project.pto [output prefix]|assistant_project.pto",
00151             wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL + wxCMD_LINE_PARAM_MULTIPLE
00152         },
00153         { wxCMD_LINE_NONE }
00154     };
00155     wxCmdLineParser parser(cmdLineDesc, argc, argv);
00156 
00157     switch ( parser.Parse() )
00158     {
00159         case -1: // -h or --help was given, and help displayed so exit
00160             return false;
00161             break;
00162         case 0:  // all is well
00163             break;
00164         default:
00165             wxLogError(_("Syntax error in parameters detected, aborting."));
00166             return false;
00167             break;
00168     }
00169 
00170     wxClient client;
00171     wxConnectionBase* conn;
00172     wxString servername;
00173 #ifdef _WIN32
00174     servername=name;
00175 #else
00176     servername=wxFileName::GetTempDir()+wxFileName::GetPathSeparator()+name+wxT(".ipc");
00177 #endif
00178     if(IsFirstInstance)
00179     {
00180         m_frame = new BatchFrame(&m_locale,m_xrcPrefix);
00181         m_frame->RestoreSize();
00182         // init help system
00183         provider->SetHelpController(&m_frame->GetHelpController());
00184 #ifdef __WXMSW__
00185         m_frame->GetHelpController().Initialize(m_xrcPrefix + wxT("data/hugin_help_en_EN.chm"));
00186 #else
00187 #if wxUSE_WXHTML_HELP
00188         // using wxHtmlHelpController
00189 #if defined __WXMAC__ && defined MAC_SELF_CONTAINED_BUNDLE
00190         // On Mac, xrc/data/help_LOCALE should be in the bundle as LOCALE.lproj/help
00191         // which we can rely on the operating sytem to pick the right locale's.
00192         wxString strFile = MacGetPathToBundledResourceFile(CFSTR("help"));
00193         if (!strFile.IsEmpty())
00194         {
00195             m_frame->GetHelpController().AddBook(wxFileName(strFile + wxT("/hugin_help_en_EN.hhp")));
00196         }
00197         else
00198         {
00199             wxLogError(wxString::Format(wxT("Could not find help directory in the bundle"), strFile.c_str()));
00200             return false;
00201         }
00202 #else
00203         m_frame->GetHelpController().AddBook(wxFileName(m_xrcPrefix + wxT("data/help_en_EN/hugin_help_en_EN.hhp")));
00204 #endif
00205 #else
00206         // using wxExtHelpController
00207         m_frame->GetHelpController().Initialize(Initialize(m_xrcPrefix + wxT("data/help_en_EN")));
00208 #endif
00209 #endif
00210 
00211         SetTopWindow(m_frame);
00212         if(!(m_frame->IsStartedMinimized()))
00213         {
00214             m_frame->Show(true);
00215         }
00216         else
00217         {
00218             m_frame->SetStatusInformation(_("PTBatcherGUI started"));
00219         };
00220         m_server = new BatchIPCServer();
00221         if (!m_server->Create(servername))
00222         {
00223             delete m_server;
00224             m_server = NULL;
00225         };
00226     }
00227     else
00228     {
00229         conn=client.MakeConnection(wxEmptyString, servername, IPC_START);
00230         if(!conn)
00231         {
00232             return false;
00233         }
00234     };
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     if(IsFirstInstance)
00340     {
00341         wxConfigBase* config=wxConfigBase::Get();
00342         if (parser.Found(wxT("s")))
00343         {
00344             config->DeleteEntry(wxT("/BatchFrame/ShutdownCheck"));
00345 #if !defined __WXMAC__ && !defined __WXOSX_COCOA__
00346             // wxMac has not wxShutdown
00347             config->Write(wxT("/BatchFrame/AtEnd"), static_cast<long>(Batch::SHUTDOWN));
00348 #endif
00349         }
00350         if (parser.Found(wxT("o")))
00351         {
00352             config->Write(wxT("/BatchFrame/OverwriteCheck"), 1l);
00353         }
00354         if (parser.Found(wxT("v")))
00355         {
00356             config->Write(wxT("/BatchFrame/VerboseCheck"), 1l);
00357         }
00358         config->Flush();
00359     }
00360     else
00361     {
00362         if (parser.Found(wxT("s")))
00363         {
00364 #if !defined __WXMAC__ && !defined __WXOSX_COCOA__
00365             // wxMac has not wxShutdown
00366             conn->Request(wxT("SetShutdownCheck"));
00367 #endif
00368         }
00369         if (parser.Found(wxT("o")))
00370         {
00371             conn->Request(wxT("SetOverwriteCheck"));
00372         }
00373         if (parser.Found(wxT("v")))
00374         {
00375             conn->Request(wxT("SetVerboseCheck"));
00376         }
00377         conn->Request(wxT("BringWindowToTop"));
00378         if(parser.Found(wxT("b")))
00379         {
00380             conn->Request(wxT("RunBatch"));
00381         }
00382         conn->Disconnect();
00383         delete conn;
00384         delete m_checker;
00385         return false;
00386     };
00387     m_frame->SetCheckboxes();
00388     m_frame->PropagateDefaults();
00389     //deactivate verbose output if started minimized
00390     if(m_frame->IsStartedMinimized())
00391     {
00392         m_frame->SetInternalVerbose(false);
00393     };
00394     if (parser.Found(wxT("b")) )
00395     {
00396         m_frame->RunBatch();
00397     }
00398 #ifdef __WXMAC__
00399     // if there are command line parameters they are handled all by the code above
00400     // but wxMac calls also MacOpenFiles after OnInit with the command line
00401     // parameters again, so the files get added twice. To prevent this we
00402     // reset the internal file list for MacOpenFiles here
00403     // but we need to process the files when PTBatcherGUI is called from finder
00404     // with an open files AppleEvent, in this case the argv is empty (except argc[0])
00405     // and we don't want to skip the MacOpenFile function
00406     if (argc > 1)
00407     {
00408         wxArrayString emptyFiles;
00409         OSXStoreOpenFiles(emptyFiles);
00410     };
00411 #endif
00412     return true;
00413 }
00414 
00415 int PTBatcherGUI::OnExit()
00416 {
00417     HuginBase::LensDB::LensDB::Clean();
00418     delete m_checker;
00419     delete m_server;
00420     delete wxHelpProvider::Set(NULL);
00421     return 0;
00422 }
00423 
00424 #if wxUSE_ON_FATAL_EXCEPTION
00425 void PTBatcherGUI::OnFatalException()
00426 {
00427     GenerateReport(wxDebugReport::Context_Exception);
00428 };
00429 #endif
00430 
00431 void PTBatcherGUI::OnItemActivated(wxListEvent& event)
00432 {
00433     wxCommandEvent dummy;
00434     m_frame->OnButtonOpenWithHugin(dummy);
00435 }
00436 
00437 void PTBatcherGUI::OnKeyDown(wxKeyEvent& event)
00438 {
00439     wxCommandEvent dummy;
00440     switch(event.GetKeyCode())
00441     {
00442         case WXK_DELETE:
00443             m_frame->OnButtonRemoveFromList(dummy);
00444             break;
00445         case WXK_INSERT:
00446             m_frame->OnButtonAddToStitchingQueue(dummy);
00447             break;
00448         case WXK_ESCAPE:
00449             m_frame->OnButtonCancel(dummy);
00450             break;
00451         default:
00452             event.Skip();
00453             break;
00454     }
00455 
00456 }
00457 
00458 #ifdef __WXMAC__
00459 // wx calls this method when the app gets "Open files" AppleEvent
00460 void PTBatcherGUI::MacOpenFiles(const wxArrayString &fileNames) 
00461 {
00462     if(m_frame)
00463     {
00464         for (int i = 0; i < fileNames.GetCount(); ++i)
00465         {
00466             wxFileName fn(fileNames[i]);
00467             m_frame->AddToList(fn.GetFullPath());
00468         };
00469     };
00470 }
00471 #endif
00472 
00473 const void* BatchIPCConnection::OnRequest(const wxString& topic, const wxString& item, size_t* size, wxIPCFormat format)
00474 {
00475     *size=wxNO_LEN;
00476     BatchFrame* MyBatchFrame=wxGetApp().GetFrame();
00477     if(item.Left(1)==wxT("A"))
00478     {
00479         MyBatchFrame->AddToList(item.Mid(2));
00480         return wxEmptyString;
00481     };
00482     if(item.Left(1)==wxT("D"))
00483     {
00484         MyBatchFrame->AddToList(item.Mid(2),Project::DETECTING);
00485         return wxEmptyString;
00486     };
00487     if(item.Left(1)==wxT("P"))
00488     {
00489         MyBatchFrame->ChangePrefix(-1,item.Mid(2));
00490         return wxEmptyString;
00491     };
00492     wxCommandEvent event;
00493     event.SetInt(1);
00494 #if !defined __WXMAC__ && !defined __WXOSX_COCOA__
00495     // wxMac has not wxShutdown
00496     if(item==wxT("SetShutdownCheck"))
00497         if (MyBatchFrame->GetEndTask()!=Batch::SHUTDOWN)
00498         {
00499             wxCommandEvent choiceEvent;
00500             choiceEvent.SetInt(Batch::SHUTDOWN);
00501             MyBatchFrame->OnChoiceEnd(choiceEvent);
00502             MyBatchFrame->SetCheckboxes();
00503         };
00504 #endif
00505     if(item==wxT("SetOverwriteCheck"))
00506         if(!MyBatchFrame->GetCheckOverwrite())
00507         {
00508             MyBatchFrame->OnCheckOverwrite(event);
00509             MyBatchFrame->SetCheckboxes();
00510         };
00511     if(item==wxT("SetVerboseCheck"))
00512         if(!MyBatchFrame->GetCheckVerbose())
00513         {
00514             MyBatchFrame->OnCheckVerbose(event);
00515             MyBatchFrame->SetCheckboxes();
00516         };
00517     if(item==wxT("BringWindowToTop"))
00518     {
00519         MyBatchFrame->RequestUserAttention();
00520     }
00521     if(item==wxT("RunBatch"))
00522     {
00523         wxCommandEvent myEvent(wxEVT_COMMAND_TOOL_CLICKED ,XRCID("tool_start"));
00524         MyBatchFrame->GetEventHandler()->AddPendingEvent(myEvent);
00525     };
00526     return wxEmptyString;
00527 };
00528 
00529 wxConnectionBase* BatchIPCServer::OnAcceptConnection (const wxString& topic)
00530 {
00531     if(topic==IPC_START)
00532     {
00533         return new BatchIPCConnection;
00534     }
00535     return NULL;
00536 };

Generated on 16 Aug 2017 for Hugintrunk by  doxygen 1.4.7