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