1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
/***************************************************************************
* copyright : (C) 2003-2017 by Pascal Brachet *
* http://www.xm1math.net/texmaker/ *
* *
* 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "algoconsole.h"
#include <QScrollBar>
#include <QAction>
#include <QMenu>
#include <QDebug>
AlgoConsole::AlgoConsole(QWidget *parent) :
QTextEdit(parent)
{
setFocusPolicy(Qt::WheelFocus);
setTextInteractionFlags(Qt::TextSelectableByMouse|Qt::TextSelectableByKeyboard);
isLocked = true;
answer="";
prompt="";
}
void AlgoConsole::contextMenuEvent(QContextMenuEvent *e)
{
QMenu *menu=new QMenu(this);
//menu = createStandardContextMenu();
QAction *a;
a = menu->addAction(tr("Copy"), this, SLOT(copy()));
a->setShortcut(Qt::CTRL+Qt::Key_C);
a->setEnabled(textCursor().hasSelection());
a = menu->addAction(tr("Select All"), this, SLOT(selectAll()));
a->setShortcut(Qt::CTRL+Qt::Key_A);
a->setEnabled(!document()->isEmpty());
menu->exec(e->globalPos());
delete menu;
}
void AlgoConsole::keyPressEvent(QKeyEvent *event)
{
if(isLocked) return;
QString t=event->text();
QChar l;
if (t.count()>0)
{
l=t.at(0);
if (l.isLetterOrNumber() || l.isPrint())
{
QTextEdit::keyPressEvent(event);
return;
}
}
if(event->key() >= 0x20 && event->key() <= 0x7e
&& (event->modifiers() == Qt::NoModifier || event->modifiers() == Qt::ShiftModifier))
QTextEdit::keyPressEvent(event);
if(event->key() == Qt::Key_Backspace
&& event->modifiers() == Qt::NoModifier
&& textCursor().positionInBlock() > prompt.length())
QTextEdit::keyPressEvent(event);
if(event->key() == Qt::Key_Return && event->modifiers() == Qt::NoModifier)
onEnter();
if(event->key() == Qt::Key_Enter)
onEnter();
}
void AlgoConsole::onEnter()
{
if(textCursor().positionInBlock() == prompt.length())
{
return;
}
answer = textCursor().block().text().mid(prompt.length());
isLocked = true;
textCursor().insertBlock();
scrollDown();
emit done();
}
void AlgoConsole::output(QString s)
{
prompt=s+" ";
if (!textCursor().block().text().isEmpty()) textCursor().insertBlock();
textCursor().insertText(prompt);
scrollDown();
setFocus();
isLocked = false;
answer="";
}
void AlgoConsole::scrollDown()
{
QScrollBar *vbar = verticalScrollBar();
vbar->setValue(vbar->maximum());
}
void AlgoConsole::mouseMoveEvent(QMouseEvent *e)
{
if (isLocked) QTextEdit::mouseMoveEvent(e);
}
|