ChangeImageVariableDialog.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00002 
00011 /*  This is free software; you can redistribute it and/or
00012  *  modify it under the terms of the GNU General Public
00013  *  License as published by the Free Software Foundation; either
00014  *  version 2 of the License, or (at your option) any later version.
00015  *
00016  *  This software is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  *  Lesser General Public License for more details.
00020  *
00021  *  You should have received a copy of the GNU General Public
00022  *  License along with this software. If not, see
00023  *  <http://www.gnu.org/licenses/>.
00024  *
00025  */
00026 
00027 #include "hugin/ChangeImageVariableDialog.h"
00028 #include "panoinc.h"
00029 #include "hugin/huginApp.h"
00030 #include "base_wx/platform.h"
00031 #include "panodata/ParseExp.h"
00032 #include <iostream>
00033 
00034 BEGIN_EVENT_TABLE(ImageVariablesExpressionDialog, wxDialog)
00035     EVT_BUTTON(wxID_OK, ImageVariablesExpressionDialog::OnOk)
00036     EVT_BUTTON(XRCID("change_variable_load"), ImageVariablesExpressionDialog::OnLoad)
00037     EVT_BUTTON(XRCID("change_variable_save"), ImageVariablesExpressionDialog::OnSave)
00038     EVT_BUTTON(XRCID("change_variable_delete"), ImageVariablesExpressionDialog::OnDelete)
00039     EVT_BUTTON(XRCID("change_variable_test"), ImageVariablesExpressionDialog::OnTest)
00040     EVT_TEXT(XRCID("change_variable_text"), ImageVariablesExpressionDialog::OnTextChange)
00041 END_EVENT_TABLE()
00042 
00043 ImageVariablesExpressionDialog::ImageVariablesExpressionDialog(wxWindow *parent, HuginBase::Panorama* pano)
00044 {
00045     // load our children. some children might need special
00046     // initialization. this will be done later.
00047     wxXmlResource::Get()->LoadDialog(this, parent, wxT("image_variables_change_dialog"));
00048 
00049 #ifdef __WXMSW__
00050     wxIcon myIcon(huginApp::Get()->GetXRCPath() + wxT("data/hugin.ico"),wxBITMAP_TYPE_ICO);
00051 #else
00052     wxIcon myIcon(huginApp::Get()->GetXRCPath() + wxT("data/hugin.png"),wxBITMAP_TYPE_PNG);
00053 #endif
00054     m_textInput = XRCCTRL(*this, "change_variable_text", wxTextCtrl);
00055     m_presetsList = XRCCTRL(*this, "change_variable_choice", wxChoice);
00056     m_textAttrInactive = wxTextAttr(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT));
00057     m_textAttrDefault = wxTextAttr(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
00058     SetIcon(myIcon);
00059     RestoreFramePosition(this, "ChangeImageVariablesDialog");
00060     SetExpression(wxConfig::Get()->Read("/ChangeImageVariablesDialog/LastExpression", wxEmptyString));
00061     m_pano=pano;
00062     const wxFileName filename(hugin_utils::GetUserAppDataDir(), "expressions.ini");
00063     // if no expressions.ini exits in users data dir copy example from global data dir
00064     if (!filename.FileExists())
00065     {
00066         const wxFileName globalFilename(hugin_utils::GetDataDir(), "expressions.ini");
00067         if (globalFilename.FileExists())
00068         {
00069             wxCopyFile(globalFilename.GetFullPath(), filename.GetFullPath());
00070         };
00071     };
00072     m_presets = new wxFileConfig(wxEmptyString, wxEmptyString, filename.GetFullPath());
00073     wxString name;
00074     long index;
00075     if (m_presets->GetFirstGroup(name, index))
00076     {
00077         m_presetsList->AppendString(name);
00078         while (m_presets->GetNextGroup(name, index))
00079         {
00080             m_presetsList->AppendString(name);
00081         };
00082     };
00083 };
00084 
00085 ImageVariablesExpressionDialog::~ImageVariablesExpressionDialog()
00086 {
00087     StoreFramePosition(this, "ChangeImageVariablesDialog");
00088     delete m_presets;
00089 };
00090 
00091 void ImageVariablesExpressionDialog::SetExpression(const wxString& s)
00092 {
00093     if (!s.IsEmpty())
00094     {
00095         m_textInput->SetValue(s);
00096     }
00097     else
00098     {
00099         m_textInput->Clear();
00100     };
00101     m_textInput->MarkDirty();
00102     // update colors
00103     wxCommandEvent dummy;
00104     OnTextChange(dummy);
00105 };
00106 
00107 std::string ImageVariablesExpressionDialog::GetExpression()
00108 {
00109     return std::string(m_textInput->GetValue().mb_str(wxConvLocal));
00110 }
00111 
00112 void ImageVariablesExpressionDialog::OnOk(wxCommandEvent & e)
00113 {
00114     wxConfigBase* config = wxConfig::Get();
00115     config->Write("/ChangeImageVariablesDialog/LastExpression", m_textInput->GetValue());
00116     StoreFramePosition(this, "ChangeImageVariablesDialog");
00117     config->Flush();
00118     EndModal(wxID_OK);
00119 }
00120 
00121 void ImageVariablesExpressionDialog::OnLoad(wxCommandEvent & e)
00122 {
00123     if (m_presetsList->GetSelection() != wxNOT_FOUND)
00124     {
00125         wxString s = m_presets->Read(m_presetsList->GetString(m_presetsList->GetSelection()) + "/Expression", wxEmptyString);
00126         if (!s.IsEmpty())
00127         {
00128             SetExpression(s);
00129         }
00130         else
00131         {
00132             wxBell();
00133         };
00134     }
00135     else
00136     {
00137         wxBell();
00138     };
00139 }
00140 
00141 void ImageVariablesExpressionDialog::OnSave(wxCommandEvent & e)
00142 {
00143     wxTextEntryDialog dlg(this, _("Input name for new preset"), _("Save preset"));
00144     if (dlg.ShowModal() == wxID_OK)
00145     {
00146         wxString s = dlg.GetValue();
00147         // replace slashes with underscore, slashes have a special meaning in wxFileConfig
00148         s.Replace("/", "_", true);
00149         s = s.Trim(true).Trim(false);
00150         if (s.IsEmpty())
00151         {
00152             wxBell();
00153             return;
00154         }
00155         bool newPreset = true;
00156         if (m_presets->HasGroup(s))
00157         {
00158             if (wxMessageBox(wxString::Format(_("Preset with name \"%s\" already exists.\nShould this preset be overwritten?"), s),
00159 #ifdef __WXMSW__
00160                 "Hugin",
00161 #else
00162                 wxEmptyString,
00163 #endif
00164                 wxYES_NO | wxYES_DEFAULT | wxICON_WARNING) == wxNO)
00165             {
00166                 return;
00167             };
00168             newPreset = false;
00169         }
00170         // now save
00171         m_presets->Write(s + "/Expression", m_textInput->GetValue());
00172         m_presets->Flush();
00173         if (newPreset)
00174         {
00175             // add to selection list
00176             m_presetsList->AppendString(s);
00177             m_presetsList->Select(m_presetsList->GetCount() - 1);
00178         };
00179         Layout();
00180     };
00181 }
00182 
00183 void ImageVariablesExpressionDialog::OnDelete(wxCommandEvent & e)
00184 {
00185     if (m_presetsList->GetSelection() != wxNOT_FOUND)
00186     {
00187         const int selection = m_presetsList->GetSelection();
00188         m_presets->DeleteGroup(m_presetsList->GetString(selection));
00189         m_presets->Flush();
00190         m_presetsList->Delete(selection);
00191         Layout();
00192     }
00193     else
00194     {
00195         wxBell();
00196     };
00197 }
00198 
00199 void ImageVariablesExpressionDialog::OnTest(wxCommandEvent & e)
00200 {
00201     HuginBase::Panorama testPano = m_pano->duplicate();
00202     std::ostringstream status, error;
00203     Parser::PanoParseExpression(testPano, GetExpression(), status, error);
00204     wxDialog dlg;
00205     if (wxXmlResource::Get()->LoadDialog(&dlg, this, "log_dialog"))
00206     {
00207         RestoreFramePosition(&dlg, "LogDialog");
00208         if (error.str().empty())
00209         {
00210         // no error, show status
00211             XRCCTRL(dlg, "log_dialog_text", wxTextCtrl)->SetValue(wxString(status.str().c_str(), wxConvLocal));
00212             dlg.SetTitle(_("Result of expression parsing"));
00213         }
00214         else
00215         {
00216             // show errors
00217             XRCCTRL(dlg, "log_dialog_text", wxTextCtrl)->SetValue(wxString(error.str().c_str(), wxConvLocal));
00218             dlg.SetTitle(_("Errors during expression parsing"));
00219         };
00220         dlg.ShowModal();
00221         StoreFramePosition(&dlg, "LogDialog");
00222     };
00223 }
00224 
00225 void ImageVariablesExpressionDialog::OnTextChange(wxCommandEvent & e)
00226 {
00227     if (!m_textInput->IsModified())
00228     {
00229         return;
00230     };
00231     bool newLine = true;
00232     size_t commentStart=-1;
00233     // reset style
00234     m_textInput->SetStyle(0, m_textInput->GetLastPosition(), m_textAttrDefault);
00235     for (size_t i = 0; i < m_textInput->GetLastPosition(); ++i)
00236     {
00237         const wxString s = m_textInput->GetRange(i, i + 1);
00238         if (s == " ")
00239         {
00240             continue;
00241         };
00242         if (newLine && s == "#" && commentStart == -1)
00243         {
00244             commentStart = i;
00245             continue;
00246         };
00247         newLine = false;
00248         if (s == "\n")
00249         {
00250             if (commentStart != -1)
00251             {
00252                 m_textInput->SetStyle(commentStart, i, m_textAttrInactive);
00253             };
00254             newLine = true;
00255             commentStart = -1;
00256             continue;
00257         };
00258     };
00259     if (commentStart != -1)
00260     {
00261         m_textInput->SetStyle(commentStart, m_textInput->GetLastPosition(), m_textAttrInactive);
00262     };
00263 }

Generated on 23 Jan 2018 for Hugintrunk by  doxygen 1.4.7