From 7aff48012c3040a675543a0ff3d23af6cb8a8638 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Mon, 23 Feb 2009 11:20:47 -0500 Subject: Started restructuring how sigmodr is built and moving things into libraries --- sigmodr/widgets/ScriptWidget.cpp | 185 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 sigmodr/widgets/ScriptWidget.cpp (limited to 'sigmodr/widgets/ScriptWidget.cpp') 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 + * + * 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 . + */ + +// Header include +#include "ScriptWidget.h" + +// KDE includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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 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(KApplication::kApplication()->activeWindow()); + if (topLevel) + { + KMenu *menu = qobject_cast(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(KApplication::kApplication()->activeWindow()); + if (topLevel) + topLevel->guiFactory()->addClient(view); +} + +void Sigmodr::ScriptWidget::unfocused(KTextEditor::View* view) +{ + KXmlGuiWindow* topLevel = qobject_cast(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); +} -- cgit