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