Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SelectCropAspectRatioDialog.cpp
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
2 
11 /* This is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This software is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public
22  * License along with this software. If not, see
23  * <http://www.gnu.org/licenses/>.
24  *
25  */
26 
28 #include "panoinc_WX.h"
29 #include "hugin/huginApp.h"
30 #include <wx/textfile.h>
31 
32 static const wxString separator = "@@@";
33 
35 {
36  wxXmlResource::Get()->LoadDialog(this, parent, "choice_aspect_dlg");
37 #ifdef __WXMSW__
38  wxIconBundle myIcons(huginApp::Get()->GetXRCPath() + "data/hugin.ico", wxBITMAP_TYPE_ICO);
39  SetIcons(myIcons);
40 #else
41  wxIcon myIcon(huginApp::Get()->GetXRCPath() + "data/hugin.png", wxBITMAP_TYPE_PNG);
42  SetIcon(myIcon);
43 #endif
44  m_aspectList = XRCCTRL(*this, "aspect_listbox", wxListBox);
45  FillListBox();
46  XRCCTRL(*this, "wxID_OK", wxButton)->Bind(wxEVT_BUTTON, &SelectAspectRatioDialog::OnOk, this);
47  XRCCTRL(*this, "add_aspect_button", wxButton)->Bind(wxEVT_BUTTON, &SelectAspectRatioDialog::OnAddAspectRatio, this);
48  XRCCTRL(*this, "remove_aspect_button", wxButton)->Bind(wxEVT_BUTTON, &SelectAspectRatioDialog::OnRemoveAspectRatio, this);
49  m_aspectList->Bind(wxEVT_LISTBOX, &SelectAspectRatioDialog::OnListBoxSelect, this);
51  m_aspectList->SendSelectionChangedEvent(wxEVT_LISTBOX);
52  RestoreFramePosition(this, "SelectCropAspectRatioDialog");
53 }
54 
56 {
57  // store frame position
58  StoreFramePosition(this, "SelectCropAspectRatioDialog");
59  // store list of aspect ratios
61 }
62 
64 {
65  const int selection = m_aspectList->GetSelection();
66  if (selection != wxNOT_FOUND && selection < m_aspectRatios.size())
67  {
68  return m_aspectRatios[selection];
69  }
70  return 1.0;
71 }
72 
73 void SelectAspectRatioDialog::OnOk(wxCommandEvent& e)
74 {
75  const int selection = m_aspectList->GetSelection();
76  if (selection != wxNOT_FOUND)
77  {
78  EndModal(wxID_OK);
79  };
80 }
81 
83 bool CheckInputs(wxWindow* parent, wxTextCtrl* labelCtrl, wxTextCtrl* aspectRatioCtrl, double& value)
84 {
85  // the label should contain at least one character, trim whitespace
86  const wxString label = labelCtrl->GetValue().Trim(true).Trim(false);
87  if (label.IsEmpty() || label.Find(separator) != wxNOT_FOUND)
88  {
89  // empty label or label contains our separator
90  wxMessageBox(_("You must provide a valid label."), _("Warning"), wxOK | wxICON_ERROR, parent);
91  return false;
92  };
93  // check if it is a simple number
94  wxString valueString = aspectRatioCtrl->GetValue();
95  int pos = valueString.Find(":");
96  if (pos != wxNOT_FOUND)
97  {
98  // we have a : in the string, assume it is format value 1:value 2
99  if (pos > 0)
100  {
101  double val1, val2 = 0;
102  if (valueString.Mid(0, pos).ToDouble(&val1) && valueString.Mid(pos + 1).ToDouble(&val2))
103  {
104  if (val2 > 0.0001)
105  {
106  value = val1 / val2;
107  if (value > 1e-4 && value < 10000)
108  {
109  return true;
110  }
111  else
112  {
113  wxMessageBox(wxString::Format(_("The aspect ratio \"%s\" is not in the valid range."), aspectRatioCtrl->GetValue().c_str()), _("Warning"), wxOK | wxICON_ERROR, parent);
114  return false;
115  };
116  };
117  };
118  };
119  }
120  else
121  {
122  // try to read the string as a simple number
123  if (valueString.ToDouble(&value))
124  {
125  if (value > 1e-4 && value < 10000)
126  {
127  return true;
128  }
129  else
130  {
131  wxMessageBox(wxString::Format(_("The aspect ratio \"%s\" is not in the valid range."), aspectRatioCtrl->GetValue().c_str()), _("Warning"), wxOK | wxICON_ERROR, parent);
132  return false;
133  };
134  };
135  };
136  // not a valid input, issue warning
137  wxMessageBox(wxString::Format(_("The input \"%s\" is not a valid number."), aspectRatioCtrl->GetValue().c_str()), _("Warning"), wxOK | wxICON_ERROR, parent);
138  return false;
139 }
140 
142 {
143  wxDialog dlg;
144  wxXmlResource::Get()->LoadDialog(&dlg, this, "add_aspect_dlg");
145  wxTextCtrl* labelCtrl = XRCCTRL(dlg, "aspect_label_textctrl", wxTextCtrl);
146  wxTextCtrl* aspectRatioCtrl = XRCCTRL(dlg, "aspect_ratio_textctrl", wxTextCtrl);
147  aspectRatioCtrl->SetValue("2:1");
148  // check if input is valid when pressing ok
149  dlg.Bind(wxEVT_COMMAND_BUTTON_CLICKED, [labelCtrl, aspectRatioCtrl, &dlg](wxCommandEvent &e)
150  {
151  double val;
152  if (CheckInputs(&dlg, labelCtrl, aspectRatioCtrl, val))
153  {
154  dlg.EndModal(wxID_OK);
155  };
156  }, XRCID("wxID_OK"));
157  if (dlg.ShowModal() == wxID_OK)
158  {
159  double value;
160  if (CheckInputs(&dlg, labelCtrl, aspectRatioCtrl, value))
161  {
162  m_aspectRatioStrings.push_back(labelCtrl->GetValue().Trim(true).Trim(false));
163  m_aspectList->AppendString(m_aspectRatioStrings.back());
164  m_aspectRatios.push_back(value);
166  };
167  };
168 }
169 
171 {
172  const int selection = m_aspectList->GetSelection();
173  // selection should be valid and list should contain at least one item
174  if (selection != wxNOT_FOUND && selection < m_aspectRatioStrings.size() && m_aspectRatioStrings.size() > 1)
175  {
176  m_aspectRatioStrings.RemoveAt(selection);
177  m_aspectRatios.erase(m_aspectRatios.begin() + selection);
178  m_aspectList->Delete(selection);
179  m_aspectList->SetSelection(-1);
181  }
182  else
183  {
184  wxBell();
185  };
186 }
187 
189 {
191 };
192 
194 {
195  const int selection = m_aspectList->GetSelection();
196  // selection should be valid and list should contain at least one item
197  XRCCTRL(*this, "wxID_OK", wxButton)->Enable(selection != wxNOT_FOUND);
198  XRCCTRL(*this, "remove_aspect_button", wxButton)->Enable(selection != wxNOT_FOUND && m_aspectRatioStrings.size() > 1);
199 }
200 
202 {
204  if (m_aspectRatioStrings.IsEmpty())
205  {
206  // could not read from file, populate with default values
207  const auto addAspect = [this](const double v1, const double v2)
208  {
209  this->m_aspectRatioStrings.Add(wxString::Format("%.0f:%.0f", v1, v2));
210  this->m_aspectRatios.push_back(v1 / v2);
211  };
212  addAspect(2, 1);
213  addAspect(3, 1);
214  addAspect(4, 1);
215  addAspect(3, 2);
216  addAspect(4, 3);
217  addAspect(16, 9);
218  addAspect(1, 1);
219  };
220  // add to list
222 }
223 
225 {
226  return wxFileName(hugin_utils::GetUserAppDataDir(), "crop_aspectratio.txt");
227 }
228 
230 {
231  wxTextFile textFile(GetAspectRatioFilename().GetFullPath());
232  if (textFile.Exists())
233  {
234  // text file exists, open it
235  textFile.Open();
236  // iterate all lines
237  for (wxString line = textFile.GetFirstLine(); !textFile.Eof(); line = textFile.GetNextLine())
238  {
239  // remove whitespaces
240  line = line.Trim(true).Trim(false);
241  const size_t pos = line.Find(separator);
242  if (pos != wxNOT_FOUND && pos > 0 && pos + separator.length() < line.length())
243  {
244  const wxString aspectRatio = line.Mid(pos + separator.length());
245  if (aspectRatio.IsEmpty())
246  {
247  continue;
248  };
249  double ratio;
250  // read aspect ratio, use C locale
251  if (aspectRatio.ToCDouble(&ratio))
252  {
253  m_aspectRatioStrings.push_back(line.Mid(0, pos));
254  m_aspectRatios.push_back(ratio);
255  };
256  };
257  };
258  };
259 }
260 
262 {
263  wxTextFile textFile(GetAspectRatioFilename().GetFullPath());
264  if (textFile.Exists())
265  {
266  // text file exists, open it
267  textFile.Open();
268  }
269  else
270  {
271  // file does not exist, create a new one
272  textFile.Create();
273  }
274  // clear all existing items in file
275  textFile.Clear();
276  // write all items
277  for (size_t i = 0; i < m_aspectRatioStrings.size(); ++i)
278  {
279  // use C locale for writing to be on the safe side for number formatting
280  textFile.AddLine(m_aspectRatioStrings[i] + separator + wxString::FromCDouble(m_aspectRatios[i]));
281  };
282  // write to disc and close
283  textFile.Write();
284  textFile.Close();
285 }
bool CheckInputs(wxWindow *parent, wxTextCtrl *labelCtrl, wxTextCtrl *aspectRatioCtrl, double &value)
check the input of the add aspect ratio dialog
std::vector< double > m_aspectRatios
vector with values of all aspect ratios, ratio=width/height
static const wxString separator
void OnListBoxSelect(wxCommandEvent &e)
event handler for activation/deactivation of buttons if necessary
void OnOk(wxCommandEvent &e)
handler for Ok, check for valid inputs
wxArrayString m_aspectRatioStrings
array with names of all aspect ratios
static char * line
Definition: svm.cpp:2784
void LoadAspectRatios()
load aspect ratios from file
static huginApp * Get()
hack.. kind of a pseudo singleton...
Definition: huginApp.cpp:641
void StoreFramePosition(wxTopLevelWindow *frame, const wxString &basename)
Store window size and position in configfile/registry.
Definition: LensCalApp.cpp:212
Definition of dialog for selecting and editing aspect ratios of crop.
void RestoreFramePosition(wxTopLevelWindow *frame, const wxString &basename)
Restore window size and position from configfile/registry.
Definition: LensCalApp.cpp:158
void SaveAspectRatios()
save the aspect ratios to the file
double GetSelectedAspectRatio() const
returns the selected aspect ratio
void OnRemoveAspectRatio(wxCommandEvent &e)
handler for removing selected aspect ratio
include file for the hugin project
void OnAddAspectRatio(wxCommandEvent &e)
handler for adding new aspect ratio setting
std::string GetUserAppDataDir()
returns the directory for user specific Hugin settings, e.g.
Definition: utils.cpp:497
wxListBox * m_aspectList
pointer to wxListBox of aspect ratios
wxFileName GetAspectRatioFilename() const
returns the file name for the list of aspect ratios
~SelectAspectRatioDialog()
destructor, save position and list of aspect ratios
SelectAspectRatioDialog(wxWindow *parent)
constructor, build dialog and load settings from file
void FillListBox()
read the settings from the file and add them to the list box if the file does not exist...
void ListBoxSelectionChanged()
implementation of activation/deactivation of buttons if necessary