我已经做了SWI-的Prolog,pqConsole一个QTextEdit基于终端,具有一定的功能,如ANSI着色序列(子集)解码,命令历史管理,多个插入点,完井,暗示...
它在提供模式REPL(读/评估/打印/循环)时运行非阻塞用户界面,这是解释型语言最常用的界面,如Prolog。
该代码由线程问题(用户请求,可能有多个控制台,或多个线程在主要交互),但它的核心是相当简单的复杂。我只是跟踪插入点,并允许光标四处移动,禁止在输出区域进行编辑。
pqConsole它是一个共享对象(我喜欢这种类型的代码重用),但对于部署,独立程序swipl-win更方便。
这里一些选定的片段,用于控制输出状态变量是promptPosition和fixedPosition。
/** display different cursor where editing available
*/
void ConsoleEdit::onCursorPositionChanged() {
QTextCursor c = textCursor();
set_cursor_tip(c);
if (fixedPosition > c.position()) {
viewport()->setCursor(Qt::OpenHandCursor);
set_editable(false);
clickable_message_line(c, true);
} else {
set_editable(true);
viewport()->setCursor(Qt::IBeamCursor);
}
if (pmatched.size()) {
pmatched.format_both(c);
pmatched = ParenMatching::range();
}
ParenMatching pm(c);
if (pm)
(pmatched = pm.positions).format_both(c, pmatched.bold());
}
/** strict control on keyboard events required
*/
void ConsoleEdit::keyPressEvent(QKeyEvent *event) {
using namespace Qt;
...
bool accept = true, ret = false, down = true, editable = (cp >= fixedPosition);
QString cmd;
switch (k) {
case Key_Space:
if (!on_completion && ctrl && editable) {
compinit2(c);
return;
}
accept = editable;
break;
case Key_Tab:
if (ctrl) {
event->ignore(); // otherwise tab control get lost !
return;
}
if (!on_completion && !ctrl && editable) {
compinit(c);
return;
}
break;
case Key_Backtab:
// otherwise tab control get lost !
event->ignore();
return;
case Key_Home:
if (!ctrl && cp > fixedPosition) {
c.setPosition(fixedPosition, (event->modifiers() & SHIFT) ? c.KeepAnchor : c.MoveAnchor);
setTextCursor(c);
return;
}
case Key_End:
case Key_Left:
case Key_Right:
case Key_PageUp:
case Key_PageDown:
break;
}
你可以看到,大多数复杂进去键盘管理...
/** \brief send text to output
*
* Decode ANSI terminal sequences, to output coloured text.
* Colours encoding are (approx) derived from swipl console.
*/
void ConsoleEdit::user_output(QString text) {
#if defined(Q_OS_WIN)
text.replace("\r\n", "\n");
#endif
QTextCursor c = textCursor();
if (status == wait_input)
c.setPosition(promptPosition);
else {
promptPosition = c.position(); // save for later
c.movePosition(QTextCursor::End);
}
auto instext = [&](QString text) {
c.insertText(text, output_text_fmt);
// Jan requested extension: put messages *above* the prompt location
if (status == wait_input) {
int ltext = text.length();
promptPosition += ltext;
fixedPosition += ltext;
ensureCursorVisible();
}
};
// filter and apply (some) ANSI sequence
int pos = text.indexOf('\x1B');
if (pos >= 0) {
int left = 0;
...
instext(text.mid(pos));
}
else
instext(text);
linkto_message_source();
}
我
认为你不应该使用一个静态变量(如在你的代码中出现),而是依靠QTextCursor接口和一些状态变量,就像我一样。
很好的回复!在摆脱回车时有效使用replace()。这是一个常见问题吗? 在进一步深入研究之前,fixedPosition是应该打印东西的位置(不能由用户直接控制),并且promptPosition用户可以更改的位置? – user3050215
@ user3050215:1)我在Linux中开发时,在移植到Windows时,(微型)问题已通过简单的#ifdef修复。 2)fixedPosition保留最后一个用户可编辑的位置 - 即输出后放置插入符号的位置,稍后引入promptPosition,以允许从异步过程输出而不干扰模式对话框。净效应很难用语言来解释,在发布任何从后台引擎生成输出的命令时很明显 - 例如[consult](http://www.swi-prolog.org/pldoc/doc_for?object=consult/1) ... – CapelliC