• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

rocs/App

  • sources
  • kde-4.14
  • kdeedu
  • rocs
  • App
  • Ui
CodeEditor.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Rocs.
3  Copyright 2009-2010 Tomaz Canabrava <tomaz.canabrava@gmail.com>
4  Copyright 2011-2012 Andreas Cord-Landwehr <cola@uni-paderborn.de>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License as
8  published by the Free Software Foundation; either version 2 of
9  the License, or (at your option) any later version.
10 
11  This program 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 General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "CodeEditor.h"
21 
22 #include <ktexteditor/view.h>
23 #include <ktexteditor/editorchooser.h>
24 #include <KMessageBox>
25 #include <KLocale>
26 #include <QVBoxLayout>
27 #include "MainWindow.h"
28 #include <KFileDialog>
29 #include <QStackedWidget>
30 
31 CodeEditor::CodeEditor(MainWindow *parent) : QWidget(parent)
32 {
33  _layout = new QVBoxLayout();
34  _editor = KTextEditor::EditorChooser::editor();
35  if (!_editor) {
36  KMessageBox::error(this, i18n("A KDE Text Editor could not be found, \n please, check your installation"));
37  exit(1);
38  }
39  _tabDocs = new KTabBar(this);
40  _docArea = new QStackedWidget(this);
41  connect(_tabDocs, SIGNAL(tabCloseRequested(int)), this, SLOT(closeDocument(int)));
42  connect(_tabDocs, SIGNAL(currentChanged(int)), this, SLOT(changeCurrentDocument(int)));
43  connect(_tabDocs, SIGNAL(newTabRequest()), this, SLOT(newScript()));
44 
45  _tabDocs->setTabsClosable(false);
46 
47  _editor->setSimpleMode(false);
48 
49  _layout->addWidget(_tabDocs);
50  _layout->addWidget(_docArea);
51 
52  _layout->setSpacing(0);
53  _layout->setMargin(0);
54 
55  setLayout(_layout);
56 }
57 
58 void CodeEditor::closeAllScripts()
59 {
60  while (_scriptDocs.size() != 0) {
61  closeDocument(0);
62  }
63 
64 }
65 
66 
67 void CodeEditor::closeDocument(int index)
68 {
69  // change tab to next document, if present
70  if (index == 0) {
71  if (_scriptDocs.size() >= 2) {
72  _activeDocument = _scriptDocs.at(1);
73  _activeView = _docViews.at(1);
74  _docArea->setCurrentIndex(1);
75  }
76  } else {
77  _activeDocument = _scriptDocs.at(index - 1);
78  _activeView = _docViews.at(index - 1);
79  _docArea->setCurrentIndex(index - 1);
80  _tabDocs->setCurrentIndex(index - 1);
81  }
82 
83  // remove document
84  _scriptDocs.at(index)->deleteLater();
85  _scriptDocs.removeAt(index);
86 
87  _docArea->removeWidget(_docArea->widget(index));
88  _docViews.removeAt(index);
89  _tabDocs->removeTab(index);
90 }
91 
92 
93 void CodeEditor::changeCurrentDocument(int index)
94 {
95  if (_scriptDocs.size() < index || index < 0) {
96  kDebug() << "Do not change to non-existing code document.";
97  return;
98  }
99  _activeDocument = _scriptDocs.at(index);
100  _activeView = _docViews.at(index);
101  _docArea->setCurrentIndex(index);
102  _tabDocs->setCurrentIndex(index);
103 }
104 
105 
106 KTextEditor::Document* CodeEditor::newScript()
107 {
108  _scriptDocs << _editor->createDocument(0);
109 
110 #ifdef USING_QTSCRIPT
111  _scriptDocs.last()->setMode("JavaScript");
112 #endif
113 
114  _docViews << qobject_cast<KTextEditor::View*>(_scriptDocs.last()->createView(this));
115 
116  _tabDocs->addTab(_scriptDocs.last()->documentName());
117  _docArea->addWidget(_docViews.last());
118  changeCurrentDocument(_docViews.count() - 1);
119  connect(_activeDocument, SIGNAL(documentNameChanged(KTextEditor::Document*)), this, SLOT(updateTabText(KTextEditor::Document*)));
120  connect(_activeDocument, SIGNAL(modifiedChanged(KTextEditor::Document*)), this, SLOT(updateTabText(KTextEditor::Document*)));
121 
122  updateTabText(_scriptDocs.last());
123  kDebug() << "New script created.";
124 
125  return _scriptDocs.last();
126 }
127 
128 
129 KTextEditor::Document* CodeEditor::newScript(const KUrl& file)
130 {
131  KTextEditor::Document* script = newScript();
132  script->saveAs(file);
133  return script;
134 }
135 
136 
137 void CodeEditor::updateTabText(KTextEditor::Document* text)
138 {
139  int index = _scriptDocs.indexOf(text);
140  _tabDocs->setTabText(index, text->documentName());
141 
142  // tell user if current modifications are unsaved
143  if (text->isModified()) {
144  _tabDocs->setTabIcon(index, KIcon("document-save"));
145  return;
146  }
147 
148  if (text->documentName().endsWith(QLatin1String(".js"), Qt::CaseInsensitive)) {
149  _tabDocs->setTabIcon(index, KIcon("application-javascript"));
150  } else if (text->documentName().endsWith(QLatin1String(".py"), Qt::CaseInsensitive)) {
151  _tabDocs->setTabIcon(index, KIcon("text-x-python"));
152  } else {
153  _tabDocs->setTabIcon(index, KIcon("text-x-generic"));
154  }
155 }
156 
157 
158 void CodeEditor::openScript(const KUrl& fileUrl)
159 {
160  if (!fileUrl.isValid()) {
161  return;
162  }
163 
164  KTextEditor::Document *d = _editor->createDocument(0);
165  d->openUrl(fileUrl);
166 
167 #ifdef USING_QTSCRIPT
168  d->setMode("JavaScript");
169 #endif
170  _scriptDocs << d;
171  _docViews << qobject_cast<KTextEditor::View*>(_scriptDocs.last()->createView(this));
172  _tabDocs->addTab(_scriptDocs.last()->documentName());
173  _docArea->addWidget(_docViews.last());
174 
175  connect(d, SIGNAL(documentNameChanged(KTextEditor::Document*)), this, SLOT(updateTabText(KTextEditor::Document*)));
176  connect(d, SIGNAL(modifiedChanged(KTextEditor::Document*)), this, SLOT(updateTabText(KTextEditor::Document*)));
177  updateTabText(d);
178  changeCurrentDocument(_docViews.count() - 1);
179 
180  kDebug() << "Being Called";
181 }
182 
183 
184 void CodeEditor::saveScript(KTextEditor::Document *doc = 0)
185 {
186  qDebug() << "my doc is: " << doc;
187  if ((_docViews.empty()) || (_scriptDocs.empty())) {
188  return;
189  }
190  if (doc == 0) {
191  doc = _activeDocument;
192  }
193  if (doc->isModified() == false) {
194  return;
195  }
196  if (doc->url().isEmpty()) {
197  saveScriptAs(doc);
198  return;
199  }
200  doc->documentSave();
201  updateTabText(doc);
202 }
203 
204 
205 void CodeEditor::saveScriptAs(KTextEditor::Document *doc = 0)
206 {
207  if ((_docViews.empty()) || (_scriptDocs.empty())) {
208  return;
209  }
210  if (doc == 0) {
211  doc = _activeDocument;
212  }
213  doc->documentSaveAs();
214  updateTabText(doc);
215 }
216 
217 
218 void CodeEditor::saveAllScripts()
219 {
220  //FIXME this should only happen for scripts with url
221  if (_scriptDocs.empty()) {
222  return;
223  }
224  foreach(KTextEditor::Document * text, _scriptDocs) {
225  if (text->url().isEmpty()) {
226  if (text->isModified() == false) {
227  continue;
228  }
229  saveScriptAs(text);
230  } else {
231  saveScript(text);
232  }
233  }
234 }
235 
236 
237 void CodeEditor::saveActiveScript()
238 {
239  saveScript();
240 }
241 
242 
243 void CodeEditor::saveActiveScriptAs()
244 {
245  saveScriptAs();
246 }
247 
248 
249 QString CodeEditor::text() const
250 {
251  return _activeDocument->text().toAscii();
252 }
253 
254 
255 bool CodeEditor::isModified() const
256 {
257  bool modified = false;
258  foreach(KTextEditor::Document * text, _scriptDocs) {
259  if (text->isModified()) modified = true;
260  }
261  return modified;
262 }
CodeEditor::saveScript
void saveScript(KTextEditor::Document *doc)
Save the given script or if no text document is given, the currently active script is saved...
Definition: CodeEditor.cpp:184
QWidget
CodeEditor::openScript
void openScript(const KUrl &fileUrl)
Definition: CodeEditor.cpp:158
CodeEditor.h
QList::at
const T & at(int i) const
QList::removeAt
void removeAt(int i)
CodeEditor::isModified
bool isModified() const
This method gives modification state of code-editor texts.
Definition: CodeEditor.cpp:255
MainWindow.h
QList::size
int size() const
QList::indexOf
int indexOf(const T &value, int from) const
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QList::count
int count(const T &value) const
CodeEditor::text
QString text() const
Definition: CodeEditor.cpp:249
QWidget::setLayout
void setLayout(QLayout *layout)
QList::empty
bool empty() const
CodeEditor::saveActiveScript
void saveActiveScript()
Definition: CodeEditor.cpp:237
QStackedWidget::setCurrentIndex
void setCurrentIndex(int index)
QVBoxLayout
QStackedWidget
QString
CodeEditor::newScript
KTextEditor::Document * newScript()
Creates new script.
Definition: CodeEditor.cpp:106
QLayout::setMargin
void setMargin(int margin)
QStackedWidget::removeWidget
void removeWidget(QWidget *widget)
CodeEditor::closeAllScripts
void closeAllScripts()
Definition: CodeEditor.cpp:58
CodeEditor::saveAllScripts
void saveAllScripts()
Definition: CodeEditor.cpp:218
QLatin1String
QList::last
T & last()
CodeEditor::CodeEditor
CodeEditor(MainWindow *parent=0)
Creates new CodeEditor without any scripts.
Definition: CodeEditor.cpp:31
QStackedWidget::widget
QWidget * widget(int index) const
QStackedWidget::addWidget
int addWidget(QWidget *widget)
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
MainWindow
Definition: MainWindow.h:60
QBoxLayout::setSpacing
void setSpacing(int spacing)
CodeEditor::saveActiveScriptAs
void saveActiveScriptAs()
Definition: CodeEditor.cpp:243
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:16:13 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

rocs/App

Skip menu "rocs/App"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal