summaryrefslogtreecommitdiffstats
path: root/sigmodr/ScriptWidget.cpp
diff options
context:
space:
mode:
authorBen Boeckel <MathStuf@gmail.com>2008-09-05 20:41:05 +0000
committerBen Boeckel <MathStuf@gmail.com>2008-09-05 20:41:05 +0000
commitb81f5bffa2772eb9bd3c67fb35485ab1ee2d96e7 (patch)
tree6609f31b1635d948cf7a216c7fea72cfb3c905a0 /sigmodr/ScriptWidget.cpp
parentb99ffef4aa68dd5f0af64de9aec0f610e267d8cc (diff)
downloadsigen-b81f5bffa2772eb9bd3c67fb35485ab1ee2d96e7.tar.gz
sigen-b81f5bffa2772eb9bd3c67fb35485ab1ee2d96e7.tar.xz
sigen-b81f5bffa2772eb9bd3c67fb35485ab1ee2d96e7.zip
[FIX] Moving stuff for the move to the new name, Sigma Game Engine (sigen for short)
git-svn-id: https://pokegen.svn.sourceforge.net/svnroot/pokegen/trunk@249 6ecfd1a5-f3ed-3746-8530-beee90d26b22
Diffstat (limited to 'sigmodr/ScriptWidget.cpp')
-rw-r--r--sigmodr/ScriptWidget.cpp148
1 files changed, 148 insertions, 0 deletions
diff --git a/sigmodr/ScriptWidget.cpp b/sigmodr/ScriptWidget.cpp
new file mode 100644
index 00000000..380699b0
--- /dev/null
+++ b/sigmodr/ScriptWidget.cpp
@@ -0,0 +1,148 @@
+/*
+ * Copyright 2008 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 <KXMLGUIFactory>
+#include <KXmlGuiWindow>
+#include <KTextEditor/Document>
+#include <KTextEditor/Editor>
+#include <KTextEditor/EditorChooser>
+#include <KTextEditor/View>
+
+Pokemodr::ScriptWidget::ScriptWidget(QWidget* parent, const Pokemod::Script& value) :
+ QWidget(parent),
+ m_value(value),
+ 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"] = "kjs";
+ languages["Lua"] = "lua";
+ languages["PHP"] = "php";
+ languages["Python"] = "python";
+ languages["Ruby"] = "ruby";
+ QList<QString> langs = languages.keys();
+ foreach (QString language, langs)
+ varInterpreter->addItem(language, languages[language]);
+ if (!m_editor)
+ {
+ KMessageBox::error(this, "A KDE text-editor component could not be found;\nplease check your KDE installation.");
+ KApplication::kApplication()->exit(1);
+ }
+ m_editor->readConfig(NULL);
+ m_document = m_editor->createDocument(NULL);
+ m_view = m_document->createView(NULL);
+ 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::Preferred, QSizePolicy::Expanding);
+ m_view->setMinimumHeight(300);
+ KXmlGuiWindow* topLevel = qobject_cast<KXmlGuiWindow*>(KApplication::kApplication()->activeWindow());
+ if (topLevel)
+ {
+ QMenu *menu = qobject_cast<KMenu*>(topLevel->factory()->container("ktexteditor_popup", topLevel));
+ if (menu)
+ m_view->setContextMenu(menu);
+ }
+ layoutScript->addWidget(m_view);
+ connect(m_document, SIGNAL(textChanged(KTextEditor::Document*)), this, SLOT(scriptChanged()));
+ connect(this, SIGNAL(valueChanged(const Pokemod::Script&)), SLOT(setGui()));
+ connect(m_view, SIGNAL(focusIn(KTextEditor::View*)), this, SLOT(focused(KTextEditor::View*)));
+ connect(m_view, SIGNAL(focusOut(KTextEditor::View*)), this, SLOT(unfocused(KTextEditor::View*)));
+ setGui();
+}
+
+Pokemodr::ScriptWidget::~ScriptWidget()
+{
+ m_editor->writeConfig(NULL);
+// delete m_document;
+}
+
+Pokemod::Script Pokemodr::ScriptWidget::value() const
+{
+ return m_value;
+}
+
+void Pokemodr::ScriptWidget::setGui()
+{
+ varInterpreter->setCurrentIndex(varInterpreter->findData(m_value.interpreter()));
+ m_document->setHighlightingMode(varInterpreter->currentText());
+ KTextEditor::Cursor cursor = m_view->cursorPosition();
+ m_document->setText(m_value.script());
+ m_view->setCursorPosition(cursor);
+}
+
+void Pokemodr::ScriptWidget::setValue(const Pokemod::Script& value)
+{
+ if (m_value == value)
+ return;
+ m_value = value;
+ emit(valueChanged(m_value));
+}
+
+void Pokemodr::ScriptWidget::on_varInterpreter_activated()
+{
+ m_value.setInterpreter(varInterpreter->itemData(varInterpreter->currentIndex()).toString());
+ m_document->setHighlightingMode(varInterpreter->currentText());
+ emit(valueChanged(m_value));
+}
+
+void Pokemodr::ScriptWidget::scriptChanged()
+{
+ if (m_value.script() != m_document->text())
+ {
+ m_value.setScript(m_document->text());
+ emit(valueChanged(m_value));
+ }
+}
+
+void Pokemodr::ScriptWidget::focused(KTextEditor::View* view)
+{
+ KXmlGuiWindow* topLevel = qobject_cast<KXmlGuiWindow*>(KApplication::kApplication()->activeWindow());
+ if (topLevel)
+ topLevel->guiFactory()->addClient(view);
+}
+
+void Pokemodr::ScriptWidget::unfocused(KTextEditor::View* view)
+{
+ KXmlGuiWindow* topLevel = qobject_cast<KXmlGuiWindow*>(KApplication::kApplication()->activeWindow());
+ QWidget* focused = KApplication::focusWidget();
+ // TODO: Does this fail with non-Kate KTextEditor implementations?
+ if (topLevel && focused && ((focused->metaObject()->className() != QString("KateViewInternal")) || ((QString(focused->metaObject()->className()) == view->metaObject()->className()) && (focused != view))))
+ topLevel->guiFactory()->removeClient(view);
+}