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 #include "base_wx/wxutils.h"
32 
33 static const wxString separator = "@@@";
34 
36 {
37  wxXmlResource::Get()->LoadDialog(this, parent, "choice_aspect_dlg");
38  m_aspectList = XRCCTRL(*this, "aspect_listbox", wxListBox);
39  FillListBox();
40  XRCCTRL(*this, "wxID_OK", wxButton)->Bind(wxEVT_BUTTON, &SelectAspectRatioDialog::OnOk, this);
41  XRCCTRL(*this, "add_aspect_button", wxButton)->Bind(wxEVT_BUTTON, &SelectAspectRatioDialog::OnAddAspectRatio, this);
42  XRCCTRL(*this, "remove_aspect_button", wxButton)->Bind(wxEVT_BUTTON, &SelectAspectRatioDialog::OnRemoveAspectRatio, this);
43  m_aspectList->Bind(wxEVT_LISTBOX, &SelectAspectRatioDialog::OnListBoxSelect, this);
45  m_aspectList->SendSelectionChangedEvent(wxEVT_LISTBOX);
46  hugin_utils::RestoreFramePosition(this, "SelectCropAspectRatioDialog");
47 }
48 
50 {
51  // store frame position
52  hugin_utils::StoreFramePosition(this, "SelectCropAspectRatioDialog");
53  // store list of aspect ratios
55 }
56 
58 {
59  const int selection = m_aspectList->GetSelection();
60  if (selection != wxNOT_FOUND && selection < m_aspectRatios.size())
61  {
62  return m_aspectRatios[selection];
63  }
64  return 1.0;
65 }
66 
67 void SelectAspectRatioDialog::OnOk(wxCommandEvent& e)
68 {
69  const int selection = m_aspectList->GetSelection();
70  if (selection != wxNOT_FOUND)
71  {
72  EndModal(wxID_OK);
73  };
74 }
75 
77 bool CheckInputs(wxWindow* parent, wxTextCtrl* labelCtrl, wxTextCtrl* aspectRatioCtrl, double& value)
78 {
79  // the label should contain at least one character, trim whitespace
80  const wxString label = labelCtrl->GetValue().Trim(true).Trim(false);
81  if (label.IsEmpty() || label.Find(separator) != wxNOT_FOUND)
82  {
83  // empty label or label contains our separator
84  hugin_utils::HuginMessageBox(_("You must provide a valid label."), _("Hugin"), wxOK | wxICON_ERROR, parent);
85  return false;
86  };
87  // check if it is a simple number
88  wxString valueString = aspectRatioCtrl->GetValue();
89  int pos = valueString.Find(":");
90  if (pos != wxNOT_FOUND)
91  {
92  // we have a : in the string, assume it is format value 1:value 2
93  if (pos > 0)
94  {
95  double val1, val2 = 0;
96  if (valueString.Mid(0, pos).ToDouble(&val1) && valueString.Mid(pos + 1).ToDouble(&val2))
97  {
98  if (val2 > 0.0001)
99  {
100  value = val1 / val2;
101  if (value > 1e-4 && value < 10000)
102  {
103  return true;
104  }
105  else
106  {
107  hugin_utils::HuginMessageBox(wxString::Format(_("The aspect ratio \"%s\" is not in the valid range."), aspectRatioCtrl->GetValue()),
108  _("Hugin"), wxOK | wxICON_ERROR, parent);
109  return false;
110  };
111  };
112  };
113  };
114  }
115  else
116  {
117  // try to read the string as a simple number
118  if (valueString.ToDouble(&value))
119  {
120  if (value > 1e-4 && value < 10000)
121  {
122  return true;
123  }
124  else
125  {
126  hugin_utils::HuginMessageBox(wxString::Format(_("The aspect ratio \"%s\" is not in the valid range."), aspectRatioCtrl->GetValue()), _("Hugin"), wxOK | wxICON_ERROR, parent);
127  return false;
128  };
129  };
130  };
131  // not a valid input, issue warning
132  hugin_utils::HuginMessageBox(wxString::Format(_("The input \"%s\" is not a valid number."), aspectRatioCtrl->GetValue()), _("Hugin"), wxOK | wxICON_ERROR, parent);
133  return false;
134 }
135 
137 {
138  wxDialog dlg;
139  wxXmlResource::Get()->LoadDialog(&dlg, this, "add_aspect_dlg");
140  wxTextCtrl* labelCtrl = XRCCTRL(dlg, "aspect_label_textctrl", wxTextCtrl);
141  wxTextCtrl* aspectRatioCtrl = XRCCTRL(dlg, "aspect_ratio_textctrl", wxTextCtrl);
142  aspectRatioCtrl->SetValue("2:1");
143  // check if input is valid when pressing ok
144  dlg.Bind(wxEVT_COMMAND_BUTTON_CLICKED, [labelCtrl, aspectRatioCtrl, &dlg](wxCommandEvent &e)
145  {
146  double val;
147  if (CheckInputs(&dlg, labelCtrl, aspectRatioCtrl, val))
148  {
149  dlg.EndModal(wxID_OK);
150  };
151  }, XRCID("wxID_OK"));
152  if (dlg.ShowModal() == wxID_OK)
153  {
154  double value;
155  if (CheckInputs(&dlg, labelCtrl, aspectRatioCtrl, value))
156  {
157  m_aspectRatioStrings.push_back(labelCtrl->GetValue().Trim(true).Trim(false));
158  m_aspectList->AppendString(m_aspectRatioStrings.back());
159  m_aspectRatios.push_back(value);
161  };
162  };
163 }
164 
166 {
167  const int selection = m_aspectList->GetSelection();
168  // selection should be valid and list should contain at least one item
169  if (selection != wxNOT_FOUND && selection < m_aspectRatioStrings.size() && m_aspectRatioStrings.size() > 1)
170  {
171  m_aspectRatioStrings.RemoveAt(selection);
172  m_aspectRatios.erase(m_aspectRatios.begin() + selection);
173  m_aspectList->Delete(selection);
174  m_aspectList->SetSelection(-1);
176  }
177  else
178  {
179  wxBell();
180  };
181 }
182 
184 {
186 };
187 
189 {
190  const int selection = m_aspectList->GetSelection();
191  // selection should be valid and list should contain at least one item
192  XRCCTRL(*this, "wxID_OK", wxButton)->Enable(selection != wxNOT_FOUND);
193  XRCCTRL(*this, "remove_aspect_button", wxButton)->Enable(selection != wxNOT_FOUND && m_aspectRatioStrings.size() > 1);
194 }
195 
197 {
199  if (m_aspectRatioStrings.IsEmpty())
200  {
201  // could not read from file, populate with default values
202  const auto addAspect = [this](const double v1, const double v2)
203  {
204  this->m_aspectRatioStrings.Add(wxString::Format("%.0f:%.0f", v1, v2));
205  this->m_aspectRatios.push_back(v1 / v2);
206  };
207  addAspect(2, 1);
208  addAspect(3, 1);
209  addAspect(4, 1);
210  addAspect(3, 2);
211  addAspect(4, 3);
212  addAspect(16, 9);
213  addAspect(1, 1);
214  };
215  // add to list
217 }
218 
220 {
221  return wxFileName(hugin_utils::GetUserAppDataDir(), "crop_aspectratio.txt");
222 }
223 
225 {
226  wxTextFile textFile(GetAspectRatioFilename().GetFullPath());
227  if (textFile.Exists())
228  {
229  // text file exists, open it
230  textFile.Open();
231  // iterate all lines
232  for (wxString line = textFile.GetFirstLine(); !textFile.Eof(); line = textFile.GetNextLine())
233  {
234  // remove whitespaces
235  line = line.Trim(true).Trim(false);
236  const size_t pos = line.Find(separator);
237  if (pos != wxNOT_FOUND && pos > 0 && pos + separator.length() < line.length())
238  {
239  const wxString aspectRatio = line.Mid(pos + separator.length());
240  if (aspectRatio.IsEmpty())
241  {
242  continue;
243  };
244  double ratio;
245  // read aspect ratio, use C locale
246  if (aspectRatio.ToCDouble(&ratio))
247  {
248  m_aspectRatioStrings.push_back(line.Mid(0, pos));
249  m_aspectRatios.push_back(ratio);
250  };
251  };
252  };
253  };
254 }
255 
257 {
258  wxTextFile textFile(GetAspectRatioFilename().GetFullPath());
259  if (textFile.Exists())
260  {
261  // text file exists, open it
262  textFile.Open();
263  }
264  else
265  {
266  // file does not exist, create a new one
267  textFile.Create();
268  }
269  // clear all existing items in file
270  textFile.Clear();
271  // write all items
272  for (size_t i = 0; i < m_aspectRatioStrings.size(); ++i)
273  {
274  // use C locale for writing to be on the safe side for number formatting
275  textFile.AddLine(m_aspectRatioStrings[i] + separator + wxString::FromCDouble(m_aspectRatios[i]));
276  };
277  // write to disc and close
278  textFile.Write();
279  textFile.Close();
280 }
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
void RestoreFramePosition(wxTopLevelWindow *frame, const wxString &basename, const bool ignoreMaximize)
Definition: wxutils.cpp:67
static const wxString separator
void StoreFramePosition(wxTopLevelWindow *frame, const wxString &basename, const bool ignoreMaximize)
Definition: wxutils.cpp:106
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
Definition of dialog for selecting and editing aspect ratios of crop.
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...
int HuginMessageBox(const wxString &message, const wxString &caption, int style, wxWindow *parent)
Definition: wxutils.cpp:176
void ListBoxSelectionChanged()
implementation of activation/deactivation of buttons if necessary