Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ChangeImageVariableDialog.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.h"
29 #include "hugin/huginApp.h"
30 #include "base_wx/platform.h"
31 #include "panodata/ParseExp.h"
32 #include <iostream>
33 
35 {
36  // load our children. some children might need special
37  // initialization. this will be done later.
38  wxXmlResource::Get()->LoadDialog(this, parent, wxT("image_variables_change_dialog"));
39 
40  m_textInput = XRCCTRL(*this, "change_variable_text", wxTextCtrl);
41  m_presetsList = XRCCTRL(*this, "change_variable_choice", wxChoice);
42  m_textAttrInactive = wxTextAttr(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT));
43  m_textAttrDefault = wxTextAttr(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
44  RestoreFramePosition(this, "ChangeImageVariablesDialog");
45  SetExpression(wxConfig::Get()->Read("/ChangeImageVariablesDialog/LastExpression", wxEmptyString));
46  m_pano=pano;
47  const wxFileName filename(hugin_utils::GetUserAppDataDir(), "expressions.ini");
48  // if no expressions.ini exits in users data dir copy example from global data dir
49  if (!filename.FileExists())
50  {
51  const wxFileName globalFilename(hugin_utils::GetDataDir(), "expressions.ini");
52  if (globalFilename.FileExists())
53  {
54  wxCopyFile(globalFilename.GetFullPath(), filename.GetFullPath());
55  };
56  };
57  m_presets = new wxFileConfig(wxEmptyString, wxEmptyString, filename.GetFullPath());
58  wxString name;
59  long index;
60  if (m_presets->GetFirstGroup(name, index))
61  {
62  m_presetsList->AppendString(name);
63  while (m_presets->GetNextGroup(name, index))
64  {
65  m_presetsList->AppendString(name);
66  };
67  };
68  Bind(wxEVT_BUTTON, &ImageVariablesExpressionDialog::OnLoad, this, XRCID("change_variable_load"));
69  Bind(wxEVT_BUTTON, &ImageVariablesExpressionDialog::OnSave, this, XRCID("change_variable_save"));
70  Bind(wxEVT_BUTTON, &ImageVariablesExpressionDialog::OnDelete, this, XRCID("change_variable_delete"));
71  Bind(wxEVT_BUTTON, &ImageVariablesExpressionDialog::OnTest, this, XRCID("change_variable_test"));
72  Bind(wxEVT_TEXT, &ImageVariablesExpressionDialog::OnTextChange, this, XRCID("change_variable_text"));
73  Bind(wxEVT_BUTTON, &ImageVariablesExpressionDialog::OnOk, this, wxID_OK);
74 };
75 
77 {
78  StoreFramePosition(this, "ChangeImageVariablesDialog");
79  delete m_presets;
80 };
81 
83 {
84  if (!s.IsEmpty())
85  {
86  m_textInput->SetValue(s);
87  }
88  else
89  {
90  m_textInput->Clear();
91  };
92  m_textInput->MarkDirty();
93  // update colors
94  wxCommandEvent dummy;
95  OnTextChange(dummy);
96 };
97 
99 {
100  return std::string(m_textInput->GetValue().mb_str(wxConvLocal));
101 }
102 
103 void ImageVariablesExpressionDialog::OnOk(wxCommandEvent & e)
104 {
105  wxConfigBase* config = wxConfig::Get();
106  config->Write("/ChangeImageVariablesDialog/LastExpression", m_textInput->GetValue());
107  StoreFramePosition(this, "ChangeImageVariablesDialog");
108  config->Flush();
109  EndModal(wxID_OK);
110 }
111 
113 {
114  if (m_presetsList->GetSelection() != wxNOT_FOUND)
115  {
116  wxString s = m_presets->Read(m_presetsList->GetString(m_presetsList->GetSelection()) + "/Expression", wxEmptyString);
117  if (!s.IsEmpty())
118  {
119  SetExpression(s);
120  }
121  else
122  {
123  wxBell();
124  };
125  }
126  else
127  {
128  wxBell();
129  };
130 }
131 
133 {
134  wxTextEntryDialog dlg(this, _("Input name for new preset"), _("Save preset"));
135  if (dlg.ShowModal() == wxID_OK)
136  {
137  wxString s = dlg.GetValue();
138  // replace slashes with underscore, slashes have a special meaning in wxFileConfig
139  s.Replace("/", "_", true);
140  s = s.Trim(true).Trim(false);
141  if (s.IsEmpty())
142  {
143  wxBell();
144  return;
145  }
146  bool newPreset = true;
147  if (m_presets->HasGroup(s))
148  {
149  if (wxMessageBox(wxString::Format(_("Preset with name \"%s\" already exists.\nShould this preset be overwritten?"), s),
150 #ifdef __WXMSW__
151  "Hugin",
152 #else
153  wxEmptyString,
154 #endif
155  wxYES_NO | wxYES_DEFAULT | wxICON_WARNING) == wxNO)
156  {
157  return;
158  };
159  newPreset = false;
160  }
161  // now save
162  m_presets->Write(s + "/Expression", m_textInput->GetValue());
163  m_presets->Flush();
164  if (newPreset)
165  {
166  // add to selection list
167  m_presetsList->AppendString(s);
168  m_presetsList->Select(m_presetsList->GetCount() - 1);
169  };
170  Layout();
171  };
172 }
173 
175 {
176  if (m_presetsList->GetSelection() != wxNOT_FOUND)
177  {
178  const int selection = m_presetsList->GetSelection();
179  m_presets->DeleteGroup(m_presetsList->GetString(selection));
180  m_presets->Flush();
181  m_presetsList->Delete(selection);
182  Layout();
183  Update();
184  }
185  else
186  {
187  wxBell();
188  };
189 }
190 
192 {
193  HuginBase::Panorama testPano = m_pano->duplicate();
194  std::ostringstream status, error;
195  Parser::PanoParseExpression(testPano, GetExpression(), status, error);
196  wxDialog dlg;
197  if (wxXmlResource::Get()->LoadDialog(&dlg, this, "log_dialog"))
198  {
199  RestoreFramePosition(&dlg, "LogDialog");
200  if (error.str().empty())
201  {
202  // no error, show status
203  XRCCTRL(dlg, "log_dialog_text", wxTextCtrl)->SetValue(wxString(status.str().c_str(), wxConvLocal));
204  dlg.SetTitle(_("Result of expression parsing"));
205  }
206  else
207  {
208  // show errors
209  XRCCTRL(dlg, "log_dialog_text", wxTextCtrl)->SetValue(wxString(error.str().c_str(), wxConvLocal));
210  dlg.SetTitle(_("Errors during expression parsing"));
211  };
212  dlg.ShowModal();
213  StoreFramePosition(&dlg, "LogDialog");
214  };
215 }
216 
218 {
219  if (!m_textInput->IsModified())
220  {
221  return;
222  };
223  bool newLine = true;
224  size_t commentStart=-1;
225  // reset style
226  m_textInput->SetStyle(0, m_textInput->GetLastPosition(), m_textAttrDefault);
227  for (size_t i = 0; i < m_textInput->GetLastPosition(); ++i)
228  {
229  const wxString s = m_textInput->GetRange(i, i + 1);
230  if (s == " ")
231  {
232  continue;
233  };
234  if (newLine && s == "#" && commentStart == -1)
235  {
236  commentStart = i;
237  continue;
238  };
239  newLine = false;
240  if (s == "\n")
241  {
242  if (commentStart != -1)
243  {
244  m_textInput->SetStyle(commentStart, i, m_textAttrInactive);
245  };
246  newLine = true;
247  commentStart = -1;
248  continue;
249  };
250  };
251  if (commentStart != -1)
252  {
253  m_textInput->SetStyle(commentStart, m_textInput->GetLastPosition(), m_textAttrInactive);
254  };
255 }
implementation of huginApp Class
void OnOk(wxCommandEvent &e)
Saves current expression when closing dialog with Ok.
void OnLoad(wxCommandEvent &e)
loads the selected preset into control
include file for the hugin project
Panorama duplicate() const
duplicate the panorama
Definition: Panorama.cpp:1653
IMPEX void PanoParseExpression(HuginBase::Panorama &pano, const std::string &expression, std::ostream &statusStream=std::cout, std::ostream &errorStream=std::cerr)
parses the given expression and apply the changes to the Panorama
Model for a panorama.
Definition: Panorama.h:152
std::string GetDataDir()
returns the full path to the data directory
Definition: utils.cpp:441
ImageVariablesExpressionDialog(wxWindow *parent, HuginBase::Panorama *pano)
Constructor, read from xrc ressource; restore last uses settings and position.
void OnTextChange(wxCommandEvent &e)
text change event, used to format text
~ImageVariablesExpressionDialog()
destructor, saves position
void StoreFramePosition(wxTopLevelWindow *frame, const wxString &basename)
Store window size and position in configfile/registry.
Definition: LensCalApp.cpp:210
Definition of dialog to edit image variables by parsing an expression.
void RestoreFramePosition(wxTopLevelWindow *frame, const wxString &basename)
Restore window size and position from configfile/registry.
Definition: LensCalApp.cpp:156
void OnDelete(wxCommandEvent &e)
deletes the seletected preset
void OnSave(wxCommandEvent &e)
saves the current expression as preset
std::string GetUserAppDataDir()
returns the directory for user specific Hugin settings, e.g.
Definition: utils.cpp:497
void OnTest(wxCommandEvent &e)
tests the current expression
function to parse expressions from strings