File: jslinenumberwidget.cpp

package info (click to toggle)
algobox 1.0.3+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,380 kB
  • sloc: cpp: 13,758; makefile: 13; xml: 8; sh: 5
file content (116 lines) | stat: -rw-r--r-- 3,370 bytes parent folder | download
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
110
111
112
113
114
115
116
/***************************************************************************
 *   copyright       : (C) 2009-2019 by Pascal Brachet                     *
 *                                                                         *
 *   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 "jslinenumberwidget.h"
#include <QTextDocument>
#include <QTextCursor>
#include <QTextEdit>
#include <QTextBlock>
#include <QScrollBar>

JSLineNumberWidget::JSLineNumberWidget(JSEditor* editor, QWidget* parent)
	: QWidget( parent) ,
	  m_editor( editor )
{
setAutoFillBackground( true );
QPalette p( palette() );
p.setColor( backgroundRole(), QColor( "#DEE4EB" ) );
setPalette( p );
setToolTip(QString::fromUtf8("Cliquer pour ajouter un signet"));
connect( m_editor->verticalScrollBar(), SIGNAL( valueChanged( int ) ), this, SLOT( update() ) );
connect( m_editor, SIGNAL( textChanged() ), this, SLOT( update() ) );
}

JSLineNumberWidget::~JSLineNumberWidget()
{
}

void JSLineNumberWidget::paintEvent( QPaintEvent* /*e*/ )
{
int max=0;
int l=0;
QPainter painter( this );
painter.setFont(numfont);
const QFontMetrics fm(numfont);
int yOffset = m_editor->verticalScrollBar()->value();
QTextDocument *doc = m_editor->document();
int i = 1;
QTextBlock p = doc->begin();
QString numtext;
const QBrush oldbrush=painter.brush();
QPen oldpen(QColor("#136872"));
oldpen.setStyle(Qt::SolidLine);
painter.setPen(oldpen);
painter.drawLine(width()-2,0,width()-2,height());
while ( p.isValid() ) 
	{
	QPointF point = p.layout()->position();
	if ( point.y() + 20 - yOffset < 0 ) 
		{
		i++;
		p = p.next();
		continue;
		}		
	if ( (int)(point.y()) - yOffset > height() ) break;
	for (int j = 0; j < 3; ++j)
		{
		if (m_editor->UserBookmark[j]==i) 
 			{
			const QBrush brush(QColor("#1B8EA6"));
			painter.fillRect(2, (int)(point.y()) - yOffset,fm.width("0")+6,fm.lineSpacing(), brush);
			const QPen pen(QColor("#FFFFFF"));
			painter.setPen(pen);
			painter.drawText(4, (int)(point.y()) - yOffset,width()-4,fm.lineSpacing(),Qt::AlignLeft | Qt::AlignTop,QString::number(j+1));
 			}
		}
	painter.setPen(oldpen);
	numtext=QString::number(i);
	painter.drawText(0, (int)(point.y()) - yOffset,width()-4,fm.lineSpacing(),Qt::AlignRight | Qt::AlignTop,numtext);
	l= fm.width(numtext)+18+fm.width("0");
	if (l>max) max=l;
	i++;
	p = p.next();
	}
	if (i>=10000) setFixedWidth(max);	
painter.end();
}

void JSLineNumberWidget::mousePressEvent(QMouseEvent *e)
{
e->accept();
QPoint p = m_editor->viewport()->mapFromGlobal(e->globalPos());
QTextCursor cur( m_editor->cursorForPosition(p) );
int i = m_editor->linefromblock(cur.block());	
if ( i==-1 ) return;
for (int j = 0; j < 3; ++j)
	{
	if (m_editor->UserBookmark[j]==i) 
		{
		m_editor->UserBookmark[j]=0;
		update();
		return;
		}
	}
for (int j = 0; j < 3; ++j)
	{
	if (m_editor->UserBookmark[j]==0) 
		{
		m_editor->UserBookmark[j]=i;
		update();
		return;
		}
	}
}

void JSLineNumberWidget::setFont(QFont efont)
{
numfont=efont;
}