Commit f3db38c3 authored by Pekka Vuorela's avatar Pekka Vuorela

Decouple TextRender from Terminal

parent f3257814
......@@ -117,6 +117,9 @@ int main(int argc, char *argv[])
Terminal term;
Util util(settings);
term.setUtil(&util);
TextRender::setUtil(&util);
TextRender::setTerminal(&term);
QString startupErrorMsg;
// copy the default config files to the config dir if they don't already exist
......@@ -153,9 +156,6 @@ int main(int argc, char *argv[])
qFatal("no root object - qml error");
TextRender *tr = root->findChild<TextRender*>("textrender");
tr->setUtil(&util);
tr->setTerminal(&term);
term.setRenderer(tr);
term.setWindow(&view);
util.setWindow(&view);
util.setTerm(&term);
......
......@@ -27,7 +27,7 @@
#include "util.h"
Terminal::Terminal(QObject *parent) :
QObject(parent), iRenderer(0), iPtyIFace(0), iWindow(0), iUtil(0),
QObject(parent), iPtyIFace(0), iWindow(0), iUtil(0),
iTermSize(0,0), iEmitCursorChangeSignal(true),
iShowCursor(true), iUseAltScreenBuffer(false), iAppCursorKeys(false)
{
......@@ -53,20 +53,6 @@ Terminal::Terminal(QObject *parent) :
resetTerminal();
}
void Terminal::setRenderer(TextRender* tr)
{
iRenderer = tr;
if(tr) {
tr->updateTermSize();
connect(this, SIGNAL(displayBufferChanged()), tr, SLOT(redraw()));
connect(this, SIGNAL(cursorPosChanged(QPoint)), tr, SLOT(redraw()));
connect(this, SIGNAL(termSizeChanged(QSize)), tr, SLOT(redraw()));
} else {
qDebug() << "warning: null text renderer";
}
}
void Terminal::setPtyIFace(PtyIFace *pty)
{
iPtyIFace = pty;
......@@ -1343,10 +1329,7 @@ void Terminal::scrollBackBufferFwd(int lines)
if(iBackBufferScrollPos < 0)
iBackBufferScrollPos = 0;
if (iRenderer) {
iRenderer->setShowBufferScrollIndicator(iBackBufferScrollPos != 0);
iRenderer->redraw();
}
emit scrollBackBufferAdjusted(false);
}
void Terminal::scrollBackBufferBack(int lines)
......@@ -1360,10 +1343,7 @@ void Terminal::scrollBackBufferBack(int lines)
if (iBackBufferScrollPos > iBackBuffer.size())
iBackBufferScrollPos = iBackBuffer.size();
if (iRenderer) {
iRenderer->setShowBufferScrollIndicator(iBackBufferScrollPos != 0);
iRenderer->redraw();
}
emit scrollBackBufferAdjusted(false);
}
void Terminal::resetBackBufferScrollPos()
......@@ -1374,10 +1354,7 @@ void Terminal::resetBackBufferScrollPos()
iBackBufferScrollPos = 0;
clearSelection();
if (iRenderer) {
iRenderer->setShowBufferScrollIndicator(false);
iRenderer->redraw();
}
emit scrollBackBufferAdjusted(true);
}
void Terminal::copySelectionToClipboard()
......@@ -1470,8 +1447,7 @@ void Terminal::adjustSelectionPosition(int lines)
iSelection = QRect(QPoint(tx,ty), QPoint(bx,by));
if (iRenderer)
iRenderer->redraw();
emit selectionChanged();
}
void Terminal::setSelection(QPoint start, QPoint end)
......@@ -1492,8 +1468,7 @@ void Terminal::setSelection(QPoint start, QPoint end)
iSelection = QRect(start, end);
if (iRenderer)
iRenderer->redraw();
emit selectionChanged();
}
void Terminal::clearSelection()
......@@ -1505,8 +1480,8 @@ void Terminal::clearSelection()
if (iUtil)
iUtil->selectionFinished();
if (iRenderer)
iRenderer->redraw();
emit selectionChanged();
}
QRect Terminal::selection()
......
......@@ -22,7 +22,6 @@
#include <QtCore>
class TextRender;
class PtyIFace;
class Util;
class QQuickView;
......@@ -60,7 +59,7 @@ public:
explicit Terminal(QObject *parent = 0);
virtual ~Terminal() {}
void setRenderer(TextRender* tr);
void setPtyIFace(PtyIFace* pty);
void setWindow(QQuickView* win) { iWindow=win; }
void setUtil(Util* util) { iUtil = util; }
......@@ -105,6 +104,8 @@ signals:
void cursorPosChanged(QPoint newPos);
void termSizeChanged(QSize newSize);
void displayBufferChanged();
void selectionChanged();
void scrollBackBufferAdjusted(bool reset);
private:
Q_DISABLE_COPY(Terminal)
......@@ -126,7 +127,6 @@ private:
void resetTabs();
void adjustSelectionPosition(int lines);
TextRender* iRenderer;
PtyIFace* iPtyIFace;
QQuickView* iWindow;
Util* iUtil;
......
......@@ -22,10 +22,11 @@
#include "terminal.h"
#include "util.h"
Terminal* TextRender::sTerm = 0;
Util* TextRender::sUtil = 0;
TextRender::TextRender(QQuickItem *parent) :
QQuickPaintedItem(parent),
iTerm(0),
iUtil(0)
QQuickPaintedItem(parent)
{
setFlag(ItemHasContents);
......@@ -71,6 +72,21 @@ TextRender::TextRender(QQuickItem *parent) :
qFatal("invalid color table");
iShowBufferScrollIndicator = false;
iFont = QFont(sUtil->settingsValue("ui/fontFamily").toString(),
sUtil->settingsValue("ui/fontSize").toInt());
iFont.setBold(false);
QFontMetrics fontMetrics(iFont);
iFontHeight = fontMetrics.height();
iFontWidth = fontMetrics.maxWidth();
iFontDescent = fontMetrics.descent();
connect(sTerm, SIGNAL(displayBufferChanged()), this, SLOT(redraw()));
connect(sTerm, SIGNAL(cursorPosChanged(QPoint)), this, SLOT(redraw()));
connect(sTerm, SIGNAL(termSizeChanged(QSize)), this, SLOT(redraw()));
connect(sTerm, SIGNAL(selectionChanged()), this, SLOT(redraw()));
connect(sTerm, SIGNAL(scrollBackBufferAdjusted(bool)), this, SLOT(handleScrollBack(bool)));
updateTermSize();
}
TextRender::~TextRender()
......@@ -79,34 +95,34 @@ TextRender::~TextRender()
void TextRender::paint(QPainter* painter)
{
if (!iTerm)
if (!sTerm)
return;
painter->save();
painter->setFont(iFont);
int y=0;
if (iTerm->backBufferScrollPos() != 0 && iTerm->backBuffer().size()>0) {
int from = iTerm->backBuffer().size() - iTerm->backBufferScrollPos();
if (sTerm->backBufferScrollPos() != 0 && sTerm->backBuffer().size()>0) {
int from = sTerm->backBuffer().size() - sTerm->backBufferScrollPos();
if(from<0)
from=0;
int to = iTerm->backBuffer().size();
if(to-from > iTerm->termSize().height())
to = from + iTerm->termSize().height();
paintFromBuffer(painter, iTerm->backBuffer(), from, to, y);
if(to-from < iTerm->termSize().height() && iTerm->buffer().size()>0) {
int to2 = iTerm->termSize().height() - (to-from);
if(to2 > iTerm->buffer().size())
to2 = iTerm->buffer().size();
paintFromBuffer(painter, iTerm->buffer(), 0, to2, y);
int to = sTerm->backBuffer().size();
if(to-from > sTerm->termSize().height())
to = from + sTerm->termSize().height();
paintFromBuffer(painter, sTerm->backBuffer(), from, to, y);
if(to-from < sTerm->termSize().height() && sTerm->buffer().size()>0) {
int to2 = sTerm->termSize().height() - (to-from);
if(to2 > sTerm->buffer().size())
to2 = sTerm->buffer().size();
paintFromBuffer(painter, sTerm->buffer(), 0, to2, y);
}
} else {
int count = qMin(iTerm->termSize().height(), iTerm->buffer().size());
paintFromBuffer(painter, iTerm->buffer(), 0, count, y);
int count = qMin(sTerm->termSize().height(), sTerm->buffer().size());
paintFromBuffer(painter, sTerm->buffer(), 0, count, y);
}
// cursor
if (iTerm->showCursor()) {
if (sTerm->showCursor()) {
painter->setOpacity(0.5);
QPoint cursor = cursorPixelPos();
QSize csize = cursorPixelSize();
......@@ -116,7 +132,7 @@ void TextRender::paint(QPainter* painter)
}
// selection
QRect selection = iTerm->selection();
QRect selection = sTerm->selection();
if (!selection.isNull()) {
painter->setOpacity(0.5);
painter->setPen(Qt::transparent);
......@@ -130,12 +146,12 @@ void TextRender::paint(QPainter* painter)
end.x()-start.x()+fontWidth(), end.y()-start.y()+fontHeight());
} else {
start = charsToPixels(selection.topLeft());
end = charsToPixels(QPoint(iTerm->termSize().width(), selection.top()));
end = charsToPixels(QPoint(sTerm->termSize().width(), selection.top()));
painter->drawRect(start.x(), start.y(),
end.x()-start.x()+fontWidth(), end.y()-start.y()+fontHeight());
start = charsToPixels(QPoint(1, selection.top()+1));
end = charsToPixels(QPoint(iTerm->termSize().width(), selection.bottom()-1));
end = charsToPixels(QPoint(sTerm->termSize().width(), selection.bottom()-1));
painter->drawRect(start.x(), start.y(),
end.x()-start.x()+fontWidth(), end.y()-start.y()+fontHeight());
......@@ -154,9 +170,9 @@ void TextRender::paintFromBuffer(QPainter* painter, QList<QList<TermChar> >& buf
const int leftmargin = 2;
int cutAfter = property("cutAfter").toInt() + iFontDescent;
TermChar tmp = iTerm->zeroChar;
TermChar nextAttrib = iTerm->zeroChar;
TermChar currAttrib = iTerm->zeroChar;
TermChar tmp = sTerm->zeroChar;
TermChar nextAttrib = sTerm->zeroChar;
TermChar currAttrib = sTerm->zeroChar;
int currentX = leftmargin;
for(int i=from; i<to; i++) {
y += iFontHeight;
......@@ -166,7 +182,7 @@ void TextRender::paintFromBuffer(QPainter* painter, QList<QList<TermChar> >& buf
else
painter->setOpacity(1.0);
int xcount = qMin(buffer.at(i).count(), iTerm->termSize().width());
int xcount = qMin(buffer.at(i).count(), sTerm->termSize().width());
// background for the current line
currentX = leftmargin;
......@@ -267,29 +283,33 @@ void TextRender::redraw()
update();
}
void TextRender::setTerminal(Terminal *term)
void TextRender::setUtil(Util *util)
{
if (!iUtil)
qFatal("textrender: util class not set");
iTerm = term;
sUtil = util;
}
iFont = QFont(iUtil->settingsValue("ui/fontFamily").toString(),
iUtil->settingsValue("ui/fontSize").toInt());
iFont.setBold(false);
QFontMetrics fontMetrics(iFont);
iFontHeight = fontMetrics.height();
iFontWidth = fontMetrics.maxWidth();
iFontDescent = fontMetrics.descent();
void TextRender::setTerminal(Terminal *terminal)
{
sTerm = terminal;
}
void TextRender::updateTermSize()
{
if (!iTerm)
if (!sTerm)
return;
QSize size((width() - 4) / iFontWidth, (height() - 4) / iFontHeight);
iTerm->setTermSize(size);
sTerm->setTermSize(size);
}
void TextRender::handleScrollBack(bool reset)
{
if (reset) {
setShowBufferScrollIndicator(false);
} else {
setShowBufferScrollIndicator(sTerm->backBufferScrollPos() != 0);
}
redraw();
}
void TextRender::setFontPointSize(int psize)
......@@ -302,7 +322,7 @@ void TextRender::setFontPointSize(int psize)
iFontWidth = fontMetrics.maxWidth();
iFontDescent = fontMetrics.descent();
iUtil->setSettingsValue("ui/fontSize", psize);
sUtil->setSettingsValue("ui/fontSize", psize);
emit fontSizeChanged();
}
......@@ -310,7 +330,7 @@ void TextRender::setFontPointSize(int psize)
QPoint TextRender::cursorPixelPos()
{
return charsToPixels(iTerm->cursorPos());
return charsToPixels(sTerm->cursorPos());
}
QPoint TextRender::charsToPixels(QPoint pos)
......
......@@ -40,8 +40,8 @@ public:
virtual ~TextRender();
void paint(QPainter*);
void setTerminal(Terminal* term);
void setUtil(Util* util) { iUtil = util; }
static void setUtil(Util *util);
static void setTerminal(Terminal *terminal);
int fontWidth() { return iFontWidth; }
int fontHeight() { return iFontHeight; }
......@@ -62,6 +62,9 @@ public slots:
void redraw();
void updateTermSize();
private slots:
void handleScrollBack(bool reset);
private:
Q_DISABLE_COPY(TextRender)
......@@ -76,8 +79,8 @@ private:
int iFontDescent;
bool iShowBufferScrollIndicator;
Terminal *iTerm;
Util *iUtil;
static Terminal *sTerm;
static Util *sUtil;
QList<QColor> iColorTable;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment