Replaced (almost) all occurrences of 'kboard' with 'tagua'.
[tagua/yd.git] / src / console.cpp
blobe02cd0bf572f15b36fbdc81a1d6500f3795d7e49
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #include "console.h"
13 #include <QApplication>
14 #include <QFile>
15 #include <QFont>
16 #include <QLabel>
17 #include <QLayout>
18 #include <QLineEdit>
19 #include <QScrollBar>
20 #include <QTextCursor>
21 #include <QTextEdit>
23 #include <iostream>
24 #include <boost/scoped_ptr.hpp>
25 #include <kstandarddirs.h>
26 #include <kglobal.h>
27 #include "common.h"
28 #include "mastersettings.h"
30 #include "histlineedit.h"
31 #include "highlighter.h"
34 class BackInserterPatternIteratorAdaptor : public PatternListAdaptorOutputIterator {
35 std::vector<Highlighter::Pattern>& m_vector;
36 public:
37 BackInserterPatternIteratorAdaptor(std::vector<Highlighter::Pattern>& vector)
38 : m_vector(vector) { }
40 virtual void add(const Highlighter::Pattern& p) {
41 m_vector.push_back(p);
46 ConsoleOutput::ConsoleOutput(QTextEdit* edit, bool force_scroll)
47 : m_edit(edit) {
48 m_old_val = m_edit->verticalScrollBar()->value();
49 m_scroll_to = force_scroll || (m_edit->verticalScrollBar()->value()
50 >= m_edit->verticalScrollBar()->maximum() );
52 QTextCursor cursor = m_edit->textCursor();
53 cursor.movePosition(QTextCursor::End);
54 m_edit->setTextCursor(cursor);
57 ConsoleOutput::~ConsoleOutput() {
58 if(m_scroll_to)
59 m_edit->verticalScrollBar()->setValue(m_edit->verticalScrollBar()->maximum());
60 else
61 m_edit->verticalScrollBar()->setValue(m_old_val);
62 //m_edit->ensureCursorVisible();
65 void ConsoleOutput::operator()(const QString& text) {
66 m_edit->insertPlainText(text);
69 LuaConsoleHighlighter::LuaConsoleHighlighter()
70 : m_display(0) {
71 KStandardDirs* dirs = KGlobal::dirs();
73 QString luaHighlightingLibrary = dirs->locate("appdata", "scripts/hllib.lua");
74 std::cout << "lua lib: " << luaHighlightingLibrary << std::endl;
75 m_api.runFile(qPrintable(luaHighlightingLibrary));
77 QStringList highlighting = dirs->findAllResources("appdata", "highlighting/*.lua", KStandardDirs::Recursive);
78 foreach (QString f, highlighting) {
79 std::cout << "lua highlighting file: " << f << std::endl;
80 m_api.runFile(qPrintable(f));
84 void LuaConsoleHighlighter::setDisplay(QTextEdit* display) {
85 m_display = display;
88 LuaConsoleHighlighter::Executor::Executor(QTextEdit* display, ConsoleOutput& output)
89 : m_display(display)
90 , m_output(output) { }
92 void LuaConsoleHighlighter::Executor::writeChunk(const QString& text) {
93 m_output(text);
96 void LuaConsoleHighlighter::Executor::setFormat(const QTextCharFormat& format) {
97 m_display->setCurrentCharFormat(format);
100 LuaConsoleHighlighter::Executor::~Executor() {
101 // m_output("\n");
104 void LuaConsoleHighlighter::operator()(const QString& text, ConsoleOutput& output) {
105 QStringList lines = text.split('\n');
107 for (int i = 0; i < lines.size(); i++) {
108 const QString& line = lines[i];
109 boost::scoped_ptr<HLine> hline(m_api.highlight(line));
110 if (hline) {
111 Executor executor(m_display, output);
112 hline->run(executor);
113 if (i < lines.size() - 1) output("\n");
118 #if 0
119 ConsoleHighlighter::ConsoleHighlighter()
120 : m_display(0) {
121 BackInserterPatternIteratorAdaptor out(m_patterns);
122 Highlighter::load(out);
125 void ConsoleHighlighter::setDisplay(QTextEdit* display) {
126 m_display = display;
127 m_basic_format = m_display->currentCharFormat();
130 void ConsoleHighlighter::operator()(const QString& text, ConsoleOutput& output) {
131 Q_ASSERT(m_display);
133 int offset = 0;
134 m_display->setCurrentCharFormat(m_basic_format);
136 while (true) {
137 int best = -1;
138 int best_offset = -1;
139 int best_length = 0;
141 // try each pattern and find the closest matching one
142 for (uint i = 0; i < m_patterns.size(); i++) {
143 int match_offset = text.indexOf(m_patterns[i].pattern, offset);
144 if (match_offset >= 0 &&
145 (best_offset == -1 || match_offset < best_offset)) {
146 int length = m_patterns[i].pattern.matchedLength();
147 if (length > 0) {
148 best = i;
149 best_offset = match_offset;
150 best_length = length;
155 if (best >= 0) {
156 // output the portion of the text before the matching
157 // substring
158 // std::cout << "outputting plain: " << text.mid(offset, best_offset - offset) << std::endl;
159 // std::cout << "weight = " << m_display->currentCharFormat().fontWeight() << std::endl;
160 output(text.mid(offset, best_offset - offset));
162 // change format
163 m_display->setCurrentCharFormat(m_patterns[best].format);
165 // output the matching substring
166 // std::cout << "outputting formatted: " << text.mid(best_offset, best_length) << std::endl;
167 // std::cout << "weight = " << m_display->currentCharFormat().fontWeight() << std::endl;
168 output(text.mid(best_offset, best_length));
170 // restore basic format
171 m_display->setCurrentCharFormat(m_basic_format);
172 // std::cout << "restored" << std::endl;
173 // std::cout << "weight = " << m_display->currentCharFormat().fontWeight() << std::endl;
175 // update offset
176 offset = best_offset + best_length;
178 else break;
181 // output the last portion of the text
182 // std::cout << "outputting plain: " << text.mid(offset) << std::endl;
183 output(text.mid(offset));
184 // std::cout << "weight = " << m_display->currentCharFormat().fontWeight() << std::endl;
186 #endif
188 QRegExp Console::promptRegexp("([a-zA-Z0-9]*% )");
190 Console::Console(QWidget* parent, const QString& caption)
191 : QWidget(parent) {
192 setObjectName("console");
194 resize(600,600);
195 setWindowTitle(caption);
197 QBoxLayout* layout = new QVBoxLayout(this);
198 QHBoxLayout* promptLayout = new QHBoxLayout;
199 layout->setMargin(0);
200 layout->setSpacing(0);
201 promptLayout->setMargin(0);
202 promptLayout->setSpacing(0);
204 edit = new HistLineEdit(this);
205 display = new QTextEdit(this);
206 prompt = new QLabel(this);
208 setFocusProxy(edit);
210 layout->addWidget(display);
211 layout->addLayout(promptLayout);
213 promptLayout->addWidget(prompt);
214 promptLayout->addWidget(edit);
216 display->setFocusProxy(edit);
217 display->setReadOnly(true);
218 ////display->setTextFormat(QTextEdit::PlainText);
219 display->setAcceptRichText(false);
220 display->setWordWrapMode (QTextOption::NoWrap);
221 display->setUndoRedoEnabled (false);
222 Settings s_console = settings.group("console");
223 if (!s_console["font"]) {
224 QFont f = QApplication::font();
225 f.setFamily("Monospace");
226 s_console["font"] = f;
228 display->setFont(s_console["font"].value<QFont>());
230 display->setFontWeight(QFont::Normal);
231 m_highlighter.setDisplay(display);
233 connect(edit, SIGNAL(receivedInput(const QString&)),
234 this, SLOT(input(const QString&)));
237 void Console::echo(const QString& text) {
238 ConsoleOutput output(display, true);
239 display->setFontWeight(QFont::Bold);
240 output(text + "\n");
241 display->setFontWeight(QFont::Normal);
244 void Console::input(const QString& text) {
245 if (edit->echoMode() == QLineEdit::Password) {
246 // edit->setEchoMode(QLineEdit::Normal);
247 echo("(password sent)");
249 else {
250 // echo inputted text
251 echo(text);
254 if (m_notifier)
255 m_notifier->textNotify(text);
256 emit receivedInput(text);
259 void Console::displayText(QString text, int offset) {
260 if (offset == 0 && promptRegexp.indexIn(text) == 0) {
261 prompt->setText(" " + promptRegexp.cap(1));
262 offset += promptRegexp.cap(1).length();
265 text = text.mid(offset);
266 text.replace("\t", " ");
268 QByteArray bytes = text.toAscii();
269 if (bytes.contains("\xff\xfc\x01")) {
270 edit->setEchoMode(QLineEdit::Normal);
272 else if (bytes.contains("\xff\xfb\x01")) {
273 edit->setEchoMode(QLineEdit::Password);
276 if (text[offset].isPrint()) {
277 ConsoleOutput output(display);
278 m_highlighter(text, output);
282 void Console::clear() {
283 display->clear();