summaryrefslogtreecommitdiffstats
path: root/sigmodr/widgets/ScriptWidget.cpp
diff options
context:
space:
mode:
authorBen Boeckel <MathStuf@gmail.com>2009-02-23 11:20:47 -0500
committerBen Boeckel <MathStuf@gmail.com>2009-02-23 11:20:47 -0500
commit7aff48012c3040a675543a0ff3d23af6cb8a8638 (patch)
tree6dd17b90d1f1c6ba9b0b7c5ddc40c2a849c25286 /sigmodr/widgets/ScriptWidget.cpp
parent25ec942048336dde5e1a17e6c75e15e4f8d8290d (diff)
downloadsigen-7aff48012c3040a675543a0ff3d23af6cb8a8638.tar.gz
sigen-7aff48012c3040a675543a0ff3d23af6cb8a8638.tar.xz
sigen-7aff48012c3040a675543a0ff3d23af6cb8a8638.zip
Started restructuring how sigmodr is built and moving things into libraries
Diffstat (limited to 'sigmodr/widgets/ScriptWidget.cpp')
-rw-r--r--sigmodr/widgets/ScriptWidget.cpp185
1 files changed, 185 insertions, 0 deletions
diff --git a/sigmodr/widgets/ScriptWidget.cpp b/sigmodr/widgets/ScriptWidget.cpp
new file mode 100644
index 00000000..d31a5178
--- /dev/null
+++ b/sigmodr/widgets/ScriptWidget.cpp
@@ -0,0 +1,185 @@
+/*
+ * Copyright 2008-2009 Ben Boeckel <MathStuf@gmail.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+// Header include
+#include "ScriptWidget.h"
+
+// KDE includes
+#include <KActionCollection>
+#include <KApplication>
+#include <KMenu>
+#include <KMessageBox>
+#include <KStandardAction>
+#include <KTextEdit>
+#include <KXMLGUIFactory>
+#include <KXmlGuiWindow>
+#include <KTextEditor/Document>
+#include <KTextEditor/Editor>
+#include <KTextEditor/EditorChooser>
+#include <KTextEditor/View>
+
+Sigmodr::ScriptWidget::ScriptWidget(QWidget* parent, const Sigcore::Script& value) :
+ QWidget(parent),
+ m_value(value),
+ m_simpleEdit(NULL),
+ m_editor(KTextEditor::EditorChooser::editor()),
+ m_document(NULL)
+{
+ setupUi(this);
+ QMap<QString, QString> languages;
+ languages["C#"] = "mono";
+ languages["Falcon"] = "falcon";
+ languages["Java"] = "java";
+ languages["JavaScript"] = "javascript";
+ languages["QtScript"] = "qtscript";
+ languages["Lua"] = "lua";
+ languages["PHP"] = "php";
+ languages["Python"] = "python";
+ languages["Ruby"] = "ruby";
+ QStringList langs = languages.keys();
+ foreach (const QString& language, langs)
+ varInterpreter->addItem(language, languages[language]);
+ if (m_editor)
+ {
+ m_editor->readConfig();
+ m_document = m_editor->createDocument(this);
+ m_view = m_document->createView(this);
+ KActionCollection* collection = m_view->actionCollection();
+ collection->action("file_save")->setVisible(false);
+ collection->action("file_save")->setEnabled(false);
+ collection->action("file_save_as")->setVisible(false);
+ collection->action("file_save_as")->setEnabled(false);
+ collection->action("edit_undo")->setVisible(false);
+ collection->action("edit_undo")->setEnabled(false);
+ collection->action("edit_redo")->setVisible(false);
+ collection->action("edit_redo")->setEnabled(false);
+ collection->action("edit_cut")->setVisible(false);
+ collection->action("edit_copy")->setVisible(false);
+ collection->action("edit_paste")->setVisible(false);
+ m_view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
+ m_view->setMinimumHeight(300);
+ KXmlGuiWindow* topLevel = qobject_cast<KXmlGuiWindow*>(KApplication::kApplication()->activeWindow());
+ if (topLevel)
+ {
+ KMenu *menu = qobject_cast<KMenu*>(topLevel->factory()->container("ktexteditor_popup", topLevel));
+ if (menu)
+ m_view->setContextMenu(menu);
+ }
+ labelScript->setBuddy(m_view);
+ setTabOrder(varInterpreter, m_view);
+ gridLayout->addWidget(m_view, 1, 1);
+ connect(m_document, SIGNAL(textChanged(KTextEditor::Document*)), this, SLOT(scriptChanged()));
+ connect(m_view, SIGNAL(focusIn(KTextEditor::View*)), this, SLOT(focused(KTextEditor::View*)));
+ connect(m_view, SIGNAL(focusOut(KTextEditor::View*)), this, SLOT(unfocused(KTextEditor::View*)));
+ }
+ else
+ {
+ KMessageBox::information(this, "A KDE text-editor component could not be found.\nPlease check your KDE installation.\n\nEnhanced editing will not be used.", "KDE editor component not found", "kte-component");
+ m_simpleEdit = new KTextEdit(m_value.script(), this);
+ m_simpleEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
+ m_simpleEdit->setMinimumHeight(300);
+ labelScript->setBuddy(m_simpleEdit);
+ setTabOrder(varInterpreter, m_simpleEdit);
+ gridLayout->addWidget(m_simpleEdit, 1, 1);
+ connect(m_simpleEdit, SIGNAL(textChanged()), this, SLOT(scriptChanged()));
+ return;
+ }
+ connect(this, SIGNAL(valueChanged(Sigcore::Script)), SLOT(setGui()));
+ setGui();
+}
+
+Sigmodr::ScriptWidget::~ScriptWidget()
+{
+ if (m_editor)
+ m_editor->writeConfig(NULL);
+}
+
+Sigcore::Script Sigmodr::ScriptWidget::value() const
+{
+ return m_value;
+}
+
+void Sigmodr::ScriptWidget::setGui()
+{
+ varInterpreter->setCurrentIndex(varInterpreter->findData(m_value.interpreter()));
+ if (m_document)
+ {
+ m_document->setHighlightingMode(varInterpreter->currentText());
+ KTextEditor::Cursor cursor = m_view->cursorPosition();
+ m_document->setText(m_value.script());
+ m_view->setCursorPosition(cursor);
+ }
+ else
+ {
+ QTextCursor cursor = m_simpleEdit->textCursor();
+ m_simpleEdit->setPlainText(m_value.script());
+ m_simpleEdit->setTextCursor(cursor);
+ }
+}
+
+void Sigmodr::ScriptWidget::setValue(const Sigcore::Script& value)
+{
+ if (m_value != value)
+ {
+ m_value = value;
+ emit(valueChanged(m_value));
+ }
+}
+
+void Sigmodr::ScriptWidget::on_varInterpreter_activated()
+{
+ m_value.setInterpreter(varInterpreter->itemData(varInterpreter->currentIndex()).toString());
+ if (m_document)
+ m_document->setHighlightingMode(varInterpreter->currentText());
+ emit(valueChanged(m_value));
+}
+
+void Sigmodr::ScriptWidget::scriptChanged()
+{
+ if (m_document)
+ {
+ if (m_value.script() != m_document->text())
+ {
+ m_value.setScript(m_document->text());
+ emit(valueChanged(m_value));
+ }
+ }
+ else
+ {
+ if (m_value.script() != m_simpleEdit->toPlainText())
+ {
+ m_value.setScript(m_simpleEdit->toPlainText());
+ emit(valueChanged(m_value));
+ }
+ }
+}
+
+void Sigmodr::ScriptWidget::focused(KTextEditor::View* view)
+{
+ KXmlGuiWindow* topLevel = qobject_cast<KXmlGuiWindow*>(KApplication::kApplication()->activeWindow());
+ if (topLevel)
+ topLevel->guiFactory()->addClient(view);
+}
+
+void Sigmodr::ScriptWidget::unfocused(KTextEditor::View* view)
+{
+ KXmlGuiWindow* topLevel = qobject_cast<KXmlGuiWindow*>(KApplication::kApplication()->activeWindow());
+ QWidget* focused = KApplication::focusWidget();
+ // FIXME: Does this fail with non-Kate KTextEditor implementations?
+ if (topLevel && focused && ((focused->metaObject()->className() != QString("KateViewInternal")) || ((focused != view) && (QString(focused->metaObject()->className()) == view->metaObject()->className()))))
+ topLevel->guiFactory()->removeClient(view);
+}