All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ScriptWidget.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011-2011 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file ScriptWidget.cpp
22 
23  \brief A widget that can be used to show and control he execution of a script.
24 */
25 
26 // TerraLib
27 #include "../../../common/StringUtils.h"
28 #include "LexerFactory.h"
29 #include "ScriptWidget.h"
30 
31 //// TerraLib
32 //#include <terralib/binding/vm/VirtualMachine.h>
33 //#include <terralib/binding/vm/VirtualMachineFactory.h>
34 //#include <terralib/binding/vm/VirtualMachineManager.h>
35 
36 // Boost
37 #include <boost/filesystem.hpp>
38 #include <boost/format.hpp>
39 
40 // Qt
41 #include <QtCore/QFile>
42 #include <QtCore/QTextStream>
43 #include <QtGui/QApplication>
44 #include <QtGui/QFileDialog>
45 #include <QtGui/QMessageBox>
46 #include <QtGui/QVBoxLayout>
47 
48 // QScintilla
49 #include <Qsci/qsciscintilla.h>
50 
52  : QWidget(parent),
53  m_txtEditor(0),
54  m_lexer(0),
55  m_fileName(0),
56  m_hasChanged(false)
57 {
58  m_txtEditor = new QsciScintilla(this);
59 
60  m_txtEditor->setAutoCompletionSource(QsciScintilla::AcsAll);
61 
62  m_txtEditor->setMarginLineNumbers(1, true);
63 
64  m_txtEditor->setFolding(QsciScintilla::PlainFoldStyle, 2);
65 
66  QVBoxLayout* mainLayout = new QVBoxLayout;
67 
68  mainLayout->addWidget(m_txtEditor);
69 
70  setLayout(mainLayout);
71 
72  connect(m_txtEditor, SIGNAL(textChanged()), this, SLOT(setTextChanged()));
73 }
74 
76 {
77 // dont't save if close is called, let's wait for application decision!
78  //close();
79 
80  delete m_fileName;
81 }
82 
84 {
85  if(m_fileName == 0)
86  return QString(tr("lang-unknown"));
87 
88  boost::filesystem::path fpath(m_fileName->toStdString());
89 
90  //testar: extension() ou inves de stem()
91  std::string fext = te::common::Convert2UCase(fpath.stem().string());
92 
93  std::string::size_type pos = fext.find('.');
94 
95  if(pos != std::string::npos)
96  return QString(fext.substr(pos + 1).c_str());
97  else
98  return QString(fext.c_str());
99 }
100 
102 {
103  if(m_fileName == 0)
104  return QString(tr("New-Script"));
105 
106  boost::filesystem::path fpath(m_fileName->toStdString());
107 
108  return QString(fpath.filename().string().c_str());
109 }
110 
111 void te::qt::widgets::ScriptWidget::open(const QString& fileName)
112 {
113 // close any previous opened script
114  close();
115 
116  QFile file(fileName);
117 
118  if(!file.open(QFile::ReadOnly))
119  {
120  QMessageBox::warning(this,
121  tr("TerraLib"),
122  tr("Cannot read file %1:\n%2.").arg(fileName).arg(file.errorString()));
123  return;
124  }
125 
126  QTextStream ifile(&file);
127 
128  QApplication::setOverrideCursor(Qt::WaitCursor);
129 
130  m_txtEditor->setText(ifile.readAll());
131 
132  QApplication::restoreOverrideCursor();
133 
134  m_fileName = new QString(fileName);
135 
136  setLexer();
137 
138  m_hasChanged = false;
139 }
140 
142 {
143  if(m_fileName == 0)
144  saveAs();
145  else
146  saveFile(*m_fileName);
147 
148  m_hasChanged = false;
149 }
150 
152 {
153  QString fileName = QFileDialog::getSaveFileName(this);
154 
155  if(fileName.isEmpty())
156  return;
157 
158  delete m_fileName;
159 
160  m_fileName = new QString(fileName);
161 
162  saveFile(fileName);
163 
164  setLexer();
165 
166  m_hasChanged = false;
167 }
168 
170 {
171 // save changes
172  if(m_hasChanged)
173  save();
174 
175 // clear text editor
176  m_txtEditor->clear();
177 
178 // release file name
179  delete m_fileName;
180 
181  m_fileName = 0;
182 
183 // set to no-changes state
184  m_hasChanged = false;
185 }
186 
188 {
189 // save changes before executing
190  if(m_hasChanged)
191  save();
192  else if(!m_hasChanged && (m_fileName == 0)) // don't execute if text editor is empty!
193  return;
194 
195  //try
196  //{
197  // te::vm::VirtualMachine* vm = te::vm::VirtualMachineFactory::make("LUA_VM");
198 
199  // vm->build(m_fileName->toStdString());
200  //
201  // vm->execute();
202  //}
203  //catch(const std::exception& e)
204  //{
205  // QMessageBox m(QMessageBox::Critical,
206  // tr("TerraLib Code Editor"),
207  // tr("Could not execute the script due to the following problem: %1.").arg(e.what()),
208  // QMessageBox::Ok,
209  // this);
210  // m.exec();
211  //}
212  //catch(...)
213  //{
214  // QMessageBox m(QMessageBox::Critical,
215  // tr("TerraLib Code Editor"),
216  // tr("Could not execute the script due to an unknown error!"),
217  // QMessageBox::Ok,
218  // this);
219  // m.exec();
220  //}
221 }
222 
223 
225 {
226 }
227 
228 
230 {
231 }
232 
234 {
235  m_txtEditor->zoomIn();
236  m_txtEditor->setMarginWidth(1, QString("0000"));
237 }
238 
240 {
241  m_txtEditor->zoomOut();
242  m_txtEditor->setMarginWidth(1, QString("0000"));
243 }
244 
246 {
247  m_hasChanged = true;
248 }
249 
250 void te::qt::widgets::ScriptWidget::saveFile(const QString& fileName)
251 {
252  QFile file(fileName);
253 
254  if(!file.open(QFile::WriteOnly))
255  {
256  QMessageBox::warning(this,
257  tr("TerraLib Code Editor"),
258  tr("Cannot write file %1:\n%2.").arg(fileName).arg(file.errorString()));
259  return;
260  }
261 
262  QTextStream ofile(&file);
263 
264  QApplication::setOverrideCursor(Qt::WaitCursor);
265 
266  ofile << m_txtEditor->text();
267 
268  QApplication::restoreOverrideCursor();
269 
270  //statusBar()->showMessage(tr("File saved"), 2000);
271 }
272 
274 {
275  //if(m_lexer || (m_fileName == 0))
276  //{
277  //m_txtEditor->setLexer(0);
278  //delete m_lexer;
279  //m_lexer = 0;
280  //}
281 
282  if(m_fileName == 0)
283  return;
284 
285  QString lang = getScriptType();
286 
287  m_lexer = LexerFactory::make(lang, m_txtEditor);
288 
289  m_txtEditor->setLexer(m_lexer);
290 }
291 
void saveFile(const QString &fileName)
A widget that can be used to show and control he execution of a script.
QString getScriptType() const
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
Definition: StringUtils.h:163
static QsciLexer * make(const QString &lang, QObject *parent=0)
void open(const QString &fileName)
A factory method for language lexers.