ScriptWidget.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
3 
4  This file is part of the TerraLib - a Framework for building GIS enabled
5  applications.
6 
7  TerraLib is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation, either version 3 of the License,
10  or (at your option) any later version.
11 
12  TerraLib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with TerraLib. See COPYING. If not, write to
19  TerraLib Team at <terralib-team@terralib.org>.
20  */
21 
22 /*!
23  \file terralib/qt/widgets/ceditor/ScriptWidget.cpp
24 
25  \brief A widget that can be used to edit a script.
26 */
27 
28 #include "ScriptWidget.h"
29 #include "Utils.h"
30 
31 // TerraLib
32 #include "../../../Exception.h"
33 #include "../../../vm/core/VirtualMachineManager.h"
34 
35 // boost
36 #include <boost/format.hpp>
37 
38 // Qt
39 #include <QApplication>
40 #include <QFile>
41 #include <QFileInfo>
42 #include <QMessageBox>
43 #include <QTextStream>
44 
46  : QsciScintilla(parent), m_fileName(""), m_dirty(false)
47 {
48  setAutoCompletionSource(QsciScintilla::AcsAll);
49  setMarginLineNumbers(1, true);
50  setFolding(QsciScintilla::PlainFoldStyle, 2);
51  setAutoCompletionThreshold(1);
52  QTimer::singleShot(0, this, SLOT(setFocus()));
53 }
54 
56 
58 {
59  if(m_fileName.isEmpty())
60  return QString(tr("lang-unknown"));
61 
62  QFileInfo info(m_fileName);
63 
64  return info.suffix();
65 }
66 
68 {
69  QFileInfo info(m_fileName);
70  return info.baseName();
71 }
72 
73 void te::qt::widgets::ScriptWidget::open(const QString& fileName)
74 {
75  QFile file(fileName);
76 
77  if(!file.open(QFile::ReadOnly))
78  {
79  QMessageBox::warning(
80  this, tr("TerraLib"),
81  tr("Cannot read file %1:\n%2.").arg(fileName).arg(file.errorString()));
82  return;
83  }
84 
85  QTextStream ifile(&file);
86 
87  QApplication::setOverrideCursor(Qt::WaitCursor);
88 
89  setText(ifile.readAll());
90 
91  QApplication::restoreOverrideCursor();
92 
93  m_fileName = fileName;
94 
95  file.close();
96 
98 }
99 
100 void te::qt::widgets::ScriptWidget::save(const QString& fileName)
101 {
102  QFile file(fileName);
103 
104  if(!file.open(QFile::WriteOnly))
105  {
107  << te::ErrorDescription(tr("Cannot write file %1:\n%2.")
108  .arg(fileName)
109  .arg(file.errorString())
110  .toUtf8()
111  .data());
112  }
113 
114  m_fileName = fileName;
115 
116  QTextStream ofile(&file);
117 
118  QApplication::setOverrideCursor(Qt::WaitCursor);
119 
120  ofile << text();
121 
122  QApplication::restoreOverrideCursor();
123  file.close();
124  m_dirty = false;
125 
127 }
128 
130 {
131  setLexer(te::qt::widgets::LexerFactory(mode));
132 }
133 
135 {
136  m_dirty = dirty;
137 }
TEQTWIDGETSEXPORT QsciLexer * LexerFactory(const QString &lang)
A factory method for language lexers.
A widget that can be used to edit a script.
ScriptWidget(QWidget *parent=0)
ScriptWidget.
boost::error_info< struct tag_error_description, std::string > ErrorDescription
The base type for error report messages.
void setDirty(bool dirty)
Sets the status of the widget. True if the text was modified, false otherwise.
bool m_dirty
Text was changed status.
Definition: ScriptWidget.h:128
QString m_fileName
File name.
Definition: ScriptWidget.h:127
void save(const QString &fileName)
Saves the content of the script into a file.
void setHighlightMode(const QString &mode)
Sets the highlight mode for the script.
void open(const QString &fileName)
Open the script file and presents it on the text edit.
QString getScriptType() const
getScriptType
An exception indicating that a given argument is not valid, for instance if a given item already exis...
Utility functions for dealing with code editor.
file(WRITE ${CMAKE_BINARY_DIR}/config_qhelp.cmake"configure_file (${TERRALIB_ABSOLUTE_ROOT_DIR}/doc/qhelp/help.qhcp.in ${CMAKE_BINARY_DIR}/share/terraview/help/help.qhcp @ONLY)") add_custom_command(OUTPUT del_dir COMMAND $
QString getScriptName() const
getScriptName