ceditor/MainWindow.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 applications.
5 
6  TerraLib is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published by
8  the Free Software Foundation, either version 3 of the License,
9  or (at your option) any later version.
10 
11  TerraLib is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with TerraLib. See COPYING. If not, write to
18  TerraLib Team at <terralib-team@terralib.org>.
19  */
20 
21 // Example
22 #include "MainWindow.h"
23 #include <ui_CodeEditorForm.h>
24 
25 // TerraLib
29 
30 // Qt
31 #include <QFileDialog>
32 
33 // STL
34 #include <iostream>
35 
36 class OutPutStream : public std::basic_streambuf<char>
37 {
38  public:
39 
40  OutPutStream(std::ostream &stream, QTextEdit* text_edit): m_stream(stream)
41  {
42  log_window = text_edit;
43  m_old_buf = stream.rdbuf();
44  stream.rdbuf(this);
45  }
46 
48  {
49 // output anything that is left
50  if(!m_string.empty())
51  log_window->append(m_string.c_str());
52 
53  m_stream.rdbuf(m_old_buf);
54  }
55 
57  {
58  qInstallMessageHandler(myQDebugMessageHandler);
59  }
60 
61  protected:
62 
63  virtual int_type overflow(int_type v)
64  {
65  if(v == '\n')
66  {
67  log_window->append("");
68  }
69  return v;
70  }
71 
72  virtual std::streamsize xsputn(const char *p, std::streamsize n)
73  {
74  QString str(p);
75  if(str.contains("\n"))
76  {
77  QStringList strSplitted = str.split("\n");
78 
79  log_window->moveCursor(QTextCursor::End);
80  log_window->insertPlainText(strSplitted.at(0)); //Index 0 is still on the same old line
81 
82  for(int i = 1; i < strSplitted.size(); i++)
83  {
84  log_window->append(strSplitted.at(i));
85  }
86  }
87  else
88  {
89  log_window->moveCursor(QTextCursor::End);
90  log_window->insertPlainText(str);
91  }
92  return n;
93  }
94 
95 private:
96 
97  static void myQDebugMessageHandler(QtMsgType, const QMessageLogContext &, const QString &msg)
98  {
99  std::cout << msg.toUtf8().data();
100  }
101 
102  std::ostream &m_stream;
103  std::streambuf *m_old_buf;
104  std::string m_string;
105 
106  QTextEdit* log_window;
107 };
108 
110 QMainWindow(parent)
111 {
112  m_ui = new Ui::MainWindow;
113 
114  m_ui->setupUi(this);
115 
116  m_out = new OutPutStream(std::cout, m_ui->m_txt);
117 
119 
120  m_ui->m_txt->setWordWrapMode(QTextOption::NoWrap);
121 
122  m_ui->m_txt->setReadOnly(true);
123 }
124 
126 {
127  delete m_out;
128 }
129 
131 {
132  std::string exPath = te::core::FindInTerraLibPath("share/terralib/examples/lua");
133 
134  m_fileName = QFileDialog::getOpenFileName(this, tr("Open Script File"), QString::fromUtf8(exPath.c_str()), tr("Lua files (*.lua *.LUA);; Pyhton files (*.py *.PY)"), 0);
135 
136  if(!m_fileName.isEmpty())
137  {
138  QFileInfo info(m_fileName);
139 
140  QString scName = info.fileName();
141 
143 
144  m_ui->m_codeTab->insertTab(0, editor, te::qt::widgets::CreateLangIcon(m_fileName), scName);
145 
146  editor->open(m_fileName);
147 
148  connect(editor, SIGNAL(textChanged()), this, SLOT(codeChanged()));
149  }
150 }
151 
153 {
154  m_fileName.clear();
155 }
156 
158 {
159  QString selFilter;
160 
161  std::map<QString, QString> ext;
162 
163  QString luaFilter = tr("Lua files (*.lua *.LUA)"),
164  pyFilter = tr("Pyhton files (*.py *.PY)");
165 
166  ext[luaFilter] = ".lua";
167  ext[pyFilter] = ".py";
168 
169  QString file = QFileDialog::getSaveFileName(this, tr("Save Script File"), "", luaFilter + ";;" + pyFilter, &selFilter);
170 
171  if(!file.isEmpty())
172  {
173  QFileInfo info(file);
174 
175  if(info.suffix().isEmpty())
176  file.append(ext[selFilter]);
177 
178  te::qt::widgets::ScriptWidget* editor = (te::qt::widgets::ScriptWidget*)m_ui->m_codeTab->currentWidget();
179 
180  editor->save(file);
181 
182  m_ui->m_codeTab->setTabText(m_ui->m_codeTab->currentIndex(), info.fileName());
183  }
184 }
185 
187 {
188  te::qt::widgets::ScriptWidget* editor = (te::qt::widgets::ScriptWidget*)m_ui->m_codeTab->currentWidget();
189 
190  m_ui->m_txt->clear();
191 
192  //editor->execute();
193 }
194 
196 {
198  qobject_cast<te::qt::widgets::ScriptWidget*>(sender());
199  int tabIndex = m_ui->m_codeTab->indexOf(editor);
200 
201  QString tabText = m_ui->m_codeTab->tabText(tabIndex);
202  if(!tabText.endsWith(" *"))
203  {
204  tabText += " *";
205  m_ui->m_codeTab->setTabText(tabIndex, tabText);
206  }
207 }
A widget that can be used to edit a script.
Definition: ScriptWidget.h:56
virtual int_type overflow(int_type v)
A widget that can be used to edit a script.
This file is a wrapper around platform specific include files.
std::ostream & m_stream
OutPutStream(std::ostream &stream, QTextEdit *text_edit)
void save(const QString &fileName)
Saves the content of the script into a file.
QString m_fileName
void open(const QString &fileName)
Open the script file and presents it on the text edit.
MainWindow(QWidget *parent=0)
Constructor.
te::gm::Polygon * p
static void registerQDebugMessageHandler()
OutPutStream * m_out
std::streambuf * m_old_buf
TECOREEXPORT std::string FindInTerraLibPath(const std::string &path)
Returns the path relative to a directory or file in the context of TerraLib.
virtual std::streamsize xsputn(const char *p, std::streamsize n)
QTextEdit * log_window
static void myQDebugMessageHandler(QtMsgType, const QMessageLogContext &, const QString &msg)
std::string m_string
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 $
Ui::MainWindow * m_ui
TEQTWIDGETSEXPORT QIcon CreateLangIcon(const QString &fileName)
Returns the icon related to the extension of the file.