textrender.h 3.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
    Copyright 2011-2012 Heikki Holstila <heikki.holstila@gmail.com>

    This file is part of FingerTerm.

    FingerTerm 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.

    FingerTerm is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with FingerTerm.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef TEXTRENDER_H
#define TEXTRENDER_H

John Brooks's avatar
John Brooks committed
23
#include <QQuickPaintedItem>
24 25 26 27 28 29
#include <QPainter>

#include "terminal.h"

class Util;

John Brooks's avatar
John Brooks committed
30
class TextRender : public QQuickPaintedItem
31
{
John Brooks's avatar
John Brooks committed
32 33
    Q_PROPERTY(int myWidth READ myWidth WRITE setMyWidth NOTIFY myWidthChanged)
    Q_PROPERTY(int myHeight READ myHeight WRITE setMyHeight NOTIFY myHeightChanged)
34 35 36 37 38 39 40
    Q_PROPERTY(int fontWidth READ fontWidth NOTIFY fontSizeChanged)
    Q_PROPERTY(int fontHeight READ fontHeight NOTIFY fontSizeChanged)
    Q_PROPERTY(int fontPointSize READ fontPointSize WRITE setFontPointSize NOTIFY fontSizeChanged)
    Q_PROPERTY(bool showBufferScrollIndicator READ showBufferScrollIndicator WRITE setShowBufferScrollIndicator NOTIFY showBufferScrollIndicatorChanged)

    Q_OBJECT
public:
John Brooks's avatar
John Brooks committed
41
    explicit TextRender(QQuickItem *parent = 0);
42
    virtual ~TextRender();
John Brooks's avatar
John Brooks committed
43
    void paint(QPainter*);
44 45 46 47

    void setTerminal(Terminal* term);
    void setUtil(Util* util) { iUtil = util; }

John Brooks's avatar
John Brooks committed
48 49 50 51
    int myWidth() { return iWidth; }
    int myHeight() { return iHeight; }
    void setMyWidth(int w) { if(iWidth!=w) { iWidth=w; emit myWidthChanged(w); } }
    void setMyHeight(int h) { if(iHeight!=h) { iHeight=h; emit myHeightChanged(h); } }
52 53 54 55 56 57 58 59 60 61 62 63
    int fontWidth() { return iFontWidth; }
    int fontHeight() { return iFontHeight; }
    int fontDescent() { return iFontDescent; }
    int fontPointSize() { return iFont.pointSize(); }
    void setFontPointSize(int psize);
    bool showBufferScrollIndicator() { return iShowBufferScrollIndicator; }
    void setShowBufferScrollIndicator(bool s) { if(iShowBufferScrollIndicator!=s) { iShowBufferScrollIndicator=s; emit showBufferScrollIndicatorChanged(); } }

    Q_INVOKABLE QPoint cursorPixelPos();
    Q_INVOKABLE QSize cursorPixelSize();

signals:
John Brooks's avatar
John Brooks committed
64 65
    void myWidthChanged(int newWidth);
    void myHeightChanged(int newHeight);
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
    void fontSizeChanged();
    void showBufferScrollIndicatorChanged();

public slots:
    void redraw();
    void updateTermSize();

private:
    Q_DISABLE_COPY(TextRender)

    void paintFromBuffer(QPainter* painter, QList<QList<TermChar> >& buffer, int from, int to, int &y);
    void drawBgFragment(QPainter* painter, int x, int y, int width, TermChar style);
    void drawTextFragment(QPainter* painter, int x, int y, QString text, TermChar style);
    QPoint charsToPixels(QPoint pos);

    int iWidth;
    int iHeight;
    QFont iFont;
    int iFontWidth;
    int iFontHeight;
    int iFontDescent;
    bool iShowBufferScrollIndicator;

    Terminal *iTerm;
    Util *iUtil;

    QList<QColor> iColorTable;
};

#endif // TEXTRENDER_H