/* * 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" // Sigmodr widget includes #include "ScriptWidget.h" // KDE includes #include #include #include #include #include #include #include #include #include #include #include #include #include // Qt includes #include #include #include #include Sigmodr::Widgets::ScriptWidget::ScriptWidget(QWidget* parent, const Sigcore::Script& value) : QWidget(parent), m_value(value), ui_simpleEdit(NULL), m_editor(KTextEditor::EditorChooser::editor()), m_document(NULL) { QFile file(":/gui/script.ui"); file.open(QFile::ReadOnly); QWidget *formWidget = QUiLoader().load(&file, this); file.close(); ui_interpreter = formWidget->findChild("varInterpreter"); connect(ui_interpreter, SIGNAL(currentIndexChanged(QString)), this, SLOT(interpreterChanged(QString))); m_languages["C#"] = "mono"; m_languages["Falcon"] = "falcon"; m_languages["Java"] = "java"; m_languages["JavaScript"] = "javascript"; m_languages["QtScript"] = "qtscript"; m_languages["Lua"] = "lua"; m_languages["PHP"] = "php"; m_languages["Python"] = "python"; m_languages["Ruby"] = "ruby"; ui_interpreter->addItems(m_languages.keys()); QLabel* labelScript = formWidget->findChild("labelScript"); QGridLayout* gridLayout = formWidget->findChild("gridLayout"); if (m_editor) { m_editor->readConfig(); m_document = m_editor->createDocument(this); ui_kteEdit = m_document->createView(this); KActionCollection* collection = ui_kteEdit->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); ui_kteEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); ui_kteEdit->setMinimumHeight(300); KXmlGuiWindow* topLevel = qobject_cast(KApplication::kApplication()->activeWindow()); if (topLevel) { KMenu *menu = qobject_cast(topLevel->factory()->container("ktexteditor_popup", topLevel)); if (menu) ui_kteEdit->setContextMenu(menu); } labelScript->setBuddy(ui_kteEdit); setTabOrder(ui_interpreter, ui_kteEdit); gridLayout->addWidget(ui_kteEdit, 1, 1); connect(m_document, SIGNAL(textChanged(KTextEditor::Document*)), this, SLOT(scriptChanged())); connect(ui_kteEdit, SIGNAL(focusIn(KTextEditor::View*)), this, SLOT(focused(KTextEditor::View*))); connect(ui_kteEdit, 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"); ui_simpleEdit = new KTextEdit(m_value.script(), this); ui_simpleEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); ui_simpleEdit->setMinimumHeight(300); labelScript->setBuddy(ui_simpleEdit); setTabOrder(ui_interpreter, ui_simpleEdit); gridLayout->addWidget(ui_simpleEdit, 1, 1); connect(ui_simpleEdit, SIGNAL(textChanged()), this, SLOT(scriptChanged())); return; } connect(this, SIGNAL(valueChanged(Sigcore::Script)), SLOT(setGui())); setGui(); } Sigmodr::Widgets::ScriptWidget::~ScriptWidget() { if (m_editor) m_editor->writeConfig(NULL); } Sigcore::Script Sigmodr::Widgets::ScriptWidget::value() const { return m_value; } void Sigmodr::Widgets::ScriptWidget::setGui() { ui_interpreter->setCurrentIndex(ui_interpreter->findData(m_value.interpreter())); if (m_document) { m_document->setHighlightingMode(ui_interpreter->currentText()); KTextEditor::Cursor cursor = ui_kteEdit->cursorPosition(); m_document->setText(m_value.script()); ui_kteEdit->setCursorPosition(cursor); } else { QTextCursor cursor = ui_simpleEdit->textCursor(); ui_simpleEdit->setPlainText(m_value.script()); ui_simpleEdit->setTextCursor(cursor); } } void Sigmodr::Widgets::ScriptWidget::setValue(const Sigcore::Script& value) { if (m_value != value) { m_value = value; emit(valueChanged(m_value)); } } void Sigmodr::Widgets::ScriptWidget::interpreterChanged(const QString& interpreter) { m_value.setInterpreter(m_languages[interpreter]); if (m_document) m_document->setHighlightingMode(interpreter); emit(valueChanged(m_value)); } void Sigmodr::Widgets::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() != ui_simpleEdit->toPlainText()) { m_value.setScript(ui_simpleEdit->toPlainText()); emit(valueChanged(m_value)); } } } void Sigmodr::Widgets::ScriptWidget::focused(KTextEditor::View* view) { KXmlGuiWindow* topLevel = qobject_cast(KApplication::kApplication()->activeWindow()); if (topLevel) topLevel->guiFactory()->addClient(view); } void Sigmodr::Widgets::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); }