File indexing completed on 2024-04-14 04:00:11

0001 //
0002 // C++ Implementation: cscripteditor
0003 //
0004 // Description: Script editor - used in dialogs
0005 /*
0006 Copyright 2010-2011 Tomas Mecir <kmuddy@kmuddy.com>
0007 
0008 This program is free software; you can redistribute it and/or
0009 modify it under the terms of the GNU General Public License as
0010 published by the Free Software Foundation; either version 2 of 
0011 the License, or (at your option) any later version.
0012 
0013 This program is distributed in the hope that it will be useful,
0014 but WITHOUT ANY WARRANTY; without even the implied warranty of
0015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0016 GNU General Public License for more details.
0017 
0018 You should have received a copy of the GNU General Public License
0019 along with this program.  If not, see <http://www.gnu.org/licenses/>.
0020 */
0021 
0022 #include "cscripteditor.h"
0023 
0024 #include "cscripteval.h"
0025 
0026 #include <KTextEdit>
0027 #include <QLabel>
0028 #include <QTimer>
0029 #include <QVBoxLayout>
0030 
0031 struct cScriptEditor::Private {
0032   KTextEdit *script;
0033   QLabel *scriptError;
0034   QTimer *scriptChecker;
0035 };
0036 
0037 
0038 cScriptEditor::cScriptEditor (QWidget *parent) : QWidget (parent)
0039 {
0040   d = new Private;
0041 
0042   QVBoxLayout *scriptlayout = new QVBoxLayout (this);
0043 
0044   d->script = new KTextEdit (this);
0045   d->scriptError = new QLabel (this);
0046   d->scriptChecker = new QTimer (this);
0047   d->scriptChecker->setSingleShot (true);
0048   connect (d->script, &KTextEdit::textChanged, this, &cScriptEditor::timedCheckScript);
0049   connect (d->scriptChecker, &QTimer::timeout, this, &cScriptEditor::checkScript);
0050 
0051   scriptlayout->setSpacing (10);
0052   scriptlayout->addWidget (d->script);
0053   scriptlayout->addWidget (d->scriptError);
0054   scriptlayout->setStretchFactor (d->script, 5);
0055 }
0056 
0057 cScriptEditor::~cScriptEditor ()
0058 {
0059   delete d;
0060 }
0061 
0062 void cScriptEditor::setText (const QString &text)
0063 {
0064   d->script->setText (text);
0065 }
0066 
0067 QString cScriptEditor::text() const
0068 {
0069   return d->script->toPlainText();
0070 }
0071 
0072 void cScriptEditor::timedCheckScript ()
0073 {
0074   d->scriptChecker->stop();
0075   d->scriptChecker->start (500);  // check the script if they don't type anything for 500 ms
0076 }
0077 
0078 void cScriptEditor::checkScript ()
0079 {
0080   QString script = d->script->toPlainText();
0081   QString err = cScriptEval::validate (script);
0082   if (err.isEmpty()) err = "Syntax OK";
0083   d->scriptError->setText (err);
0084 }
0085 
0086 #include "moc_cscripteditor.cpp"