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