1 // -*- c-basic-offset: 4 -*-
2 
3 /** @file HDRMergeOptionDialog.cpp
4  *
5  *	@brief implementation of dialog for hdrmerge options
6  *
7  *  @author Thomas Modes
8  *
9  *  $Id$
10  *
11  */
12 
13 /*  This is free software; you can redistribute it and/or
14  *  modify it under the terms of the GNU General Public
15  *  License as published by the Free Software Foundation; either
16  *  version 2 of the License, or (at your option) any later version.
17  *
18  *  This software is distributed in the hope that it will be useful,
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  *  Lesser General Public License for more details.
22  *
23  *  You should have received a copy of the GNU General Public
24  *  License along with this software. If not, see
25  *  <http://www.gnu.org/licenses/>.
26  *
27  */
28 
29 #include "hugin/HDRMergeOptionDialog.h"
30 #include "base_wx/wxPlatform.h"
31 #ifdef __APPLE__
32 #include "panoinc_WX.h"
33 #include "panoinc.h"
34 #endif
35 #include <hugin/config_defaults.h>
36 #include "hugin/huginApp.h"
37 
38 // somewhere SetDesc gets defined.. this breaks wx/cmdline.h on OSX
39 #ifdef SetDesc
40 #undef SetDesc
41 #endif
42 
43 #include <wx/cmdline.h>
44 
BEGIN_EVENT_TABLE(HDRMergeOptionsDialog,wxDialog)45 BEGIN_EVENT_TABLE(HDRMergeOptionsDialog,wxDialog)
46 EVT_CHOICE(XRCID("hdrmerge_option_mode"),HDRMergeOptionsDialog::OnModeChanged)
47 EVT_BUTTON(wxID_OK, HDRMergeOptionsDialog::OnOk)
48 END_EVENT_TABLE()
49 
50 HDRMergeOptionsDialog::HDRMergeOptionsDialog(wxWindow *parent)
51 {
52     wxXmlResource::Get()->LoadDialog(this, parent, wxT("hdrmerge_options_dialog"));
53 
54 #ifdef __WXMSW__
55     wxIcon myIcon(huginApp::Get()->GetXRCPath() + wxT("data/hugin.ico"),wxBITMAP_TYPE_ICO);
56 #else
57     wxIcon myIcon(huginApp::Get()->GetXRCPath() + wxT("data/hugin.png"),wxBITMAP_TYPE_PNG);
58 #endif
59     SetIcon(myIcon);
60     m_mode=XRCCTRL(*this,"hdrmerge_option_mode",wxChoice);
61     m_panel_avg=XRCCTRL(*this,"hdrmerge_option_panel_avg",wxPanel);
62     m_panel_avgslow=XRCCTRL(*this,"hdrmerge_option_panel_avgslow",wxPanel);
63     m_panel_khan=XRCCTRL(*this,"hdrmerge_option_panel_khan",wxPanel);
64     m_option_c=XRCCTRL(*this,"hdrmerge_option_c",wxCheckBox);
65     m_khan_iter=XRCCTRL(*this,"hdrmerge_option_khan_iter",wxSpinCtrl);
66     m_khan_iter->SetRange(1,100);
67     m_khan_sigma=XRCCTRL(*this,"hdrmerge_option_khan_sigma",wxTextCtrl);
68     m_option_khan_af=XRCCTRL(*this,"hdrmerge_option_khan_af",wxCheckBox);
69     m_option_khan_ag=XRCCTRL(*this,"hdrmerge_option_khan_ag",wxCheckBox);
70     m_option_khan_am=XRCCTRL(*this,"hdrmerge_option_khan_am",wxCheckBox);
71     this->CenterOnParent();
72 };
73 
SetCommandLineArgument(wxString cmd)74 void HDRMergeOptionsDialog::SetCommandLineArgument(wxString cmd)
75 {
76     m_cmd=cmd;
77     if (m_cmd.IsEmpty())
78         m_cmd=wxT(HUGIN_HDRMERGE_ARGS);
79     m_cmd.LowerCase();
80 	// parse arguments
81     static const wxCmdLineEntryDesc cmdLineDesc[] =
82     {
83         { wxCMD_LINE_OPTION, "m", NULL, NULL, wxCMD_LINE_VAL_STRING },
84         { wxCMD_LINE_SWITCH, "c", NULL, NULL},
85         { wxCMD_LINE_OPTION, "i", NULL, NULL, wxCMD_LINE_VAL_NUMBER },
86         { wxCMD_LINE_OPTION, "s", NULL, NULL, wxCMD_LINE_VAL_STRING },
87         { wxCMD_LINE_OPTION, "a", NULL, NULL, wxCMD_LINE_VAL_STRING },
88         { wxCMD_LINE_NONE }
89     };
90     wxCmdLineParser parser;
91     parser.SetDesc(cmdLineDesc);
92     parser.SetCmdLine(m_cmd);
93     parser.Parse(false);
94     wxString param;
95     if(parser.Found(wxT("m"),&param))
96     {
97         if(param.CmpNoCase(wxT("avg_slow"))==0)
98             m_mode->SetSelection(1);
99         else
100         {
101             if(param.CmpNoCase(wxT("khan"))==0)
102                 m_mode->SetSelection(2);
103             else
104                 m_mode->SetSelection(0);
105         };
106     }
107     else
108         m_mode->SetSelection(0);
109     m_option_c->SetValue(parser.Found(wxT("c")));
110     long i;
111     if(parser.Found(wxT("i"),&i))
112         m_khan_iter->SetValue(i);
113     else
114         m_khan_iter->SetValue(4);
115     if(parser.Found(wxT("s"),&param))
116     {
117         //change locale for correct numeric output
118         char * p = setlocale(LC_NUMERIC,NULL);
119         char * old_locale = strdup(p);
120         setlocale(LC_NUMERIC,"C");
121         double sigma=0.0;
122         param.ToDouble(&sigma);
123         //reset locale
124         setlocale(LC_NUMERIC,old_locale);
125         free(old_locale);
126         //using current locale for value in GUI
127         m_khan_sigma->SetValue(wxString::Format(wxT("%.2f"),sigma));
128     }
129     else
130         m_khan_sigma->SetValue(wxT("30"));
131     if(parser.Found(wxT("a"),&param))
132     {
133         m_option_khan_af->SetValue(param.Contains(wxT("f")));
134         m_option_khan_ag->SetValue(param.Contains(wxT("g")));
135         m_option_khan_am->SetValue(param.Contains(wxT("m")));
136     }
137     wxCommandEvent dummy;
138     OnModeChanged(dummy);
139 };
140 
BuildCommandLineArgument()141 bool HDRMergeOptionsDialog::BuildCommandLineArgument()
142 {
143     int selection=m_mode->GetSelection();
144     m_cmd.Clear();
145     bool correct_input=true;
146     double i = 0;
147     wxString errorstring(_("Invalid input\n"));
148     switch(selection)
149     {
150         case 0:
151             m_cmd.Append(wxT("-m avg"));
152             if(m_option_c->IsChecked())
153                 m_cmd.Append(wxT(" -c"));
154             break;
155         case 1:
156             m_cmd.Append(wxT("-m avg_slow"));
157             break;
158         case 2:
159             m_cmd.Append(wxT("-m khan"));
160             if(m_khan_iter->GetValue())
161             {
162                 m_cmd.Append(wxString::Format(wxT(" -i %d"),m_khan_iter->GetValue()));
163             }
164             else
165             {
166                 correct_input=false;
167                 errorstring.Append(wxString::Format(_("Input \"%s\" for %s is not a valid number\n"),
168                     m_khan_iter->GetValue(),_("Iteration")));
169             };
170             if(m_khan_sigma->GetValue().ToDouble(&i))
171             {
172                 //change locale for correct numeric output
173                 char * p = setlocale(LC_NUMERIC,NULL);
174                 char * old_locale = strdup(p);
175                 setlocale(LC_NUMERIC,"C");
176                 m_cmd.Append(wxString::Format(wxT(" -s %f"),i));
177                 //reset locale
178                 setlocale(LC_NUMERIC,old_locale);
179                 free(old_locale);
180             }
181             else
182             {
183                 correct_input=false;
184                 errorstring.Append(wxString::Format(_("Input \"%s\" for %s is not a valid number\n"),
185                     m_khan_iter->GetValue(),_("Sigma")));
186             };
187             if(m_option_khan_af->IsChecked() || m_option_khan_ag->IsChecked() ||
188                 m_option_khan_am->IsChecked())
189             {
190                 m_cmd.Append(wxT(" -a "));
191                 if(m_option_khan_af->IsChecked())
192                     m_cmd.Append(wxT("f"));
193                 if(m_option_khan_ag->IsChecked())
194                     m_cmd.Append(wxT("g"));
195                 if(m_option_khan_am->IsChecked())
196                     m_cmd.Append(wxT("m"));
197             }
198             break;
199     };
200     if(!correct_input)
201         wxMessageBox(errorstring,_("Wrong input"),wxOK | wxICON_INFORMATION);
202     return correct_input;
203 };
204 
OnModeChanged(wxCommandEvent & e)205 void HDRMergeOptionsDialog::OnModeChanged(wxCommandEvent & e)
206 {
207     int selection=m_mode->GetSelection();
208     m_panel_avg->Show(selection==0);
209     m_panel_avgslow->Show(selection==1);
210     m_panel_khan->Show(selection==2);
211     GetSizer()->Fit(this);
212 };
213 
OnOk(wxCommandEvent & e)214 void HDRMergeOptionsDialog::OnOk(wxCommandEvent & e)
215 {
216     if(BuildCommandLineArgument())
217         this->EndModal(wxOK);
218 };
219 
220