forked from mercutiodesign/texmaker-3.3.3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightlinenumberwidget.cpp
151 lines (135 loc) · 5.04 KB
/
lightlinenumberwidget.cpp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/***************************************************************************
* copyright : (C) 2003-2011 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 "lightlinenumberwidget.h"
#include <QTextDocument>
#include <QTextCursor>
#include <QPlainTextEdit>
#include <QTextBlock>
#include "blockdata.h"
LightLineNumberWidget::LightLineNumberWidget(LightLatexEditor* editor, QWidget* parent)
: QWidget( parent) ,
m_editor( editor ) {
setAutoFillBackground( true );
QPalette p( palette() );
p.setColor( backgroundRole(), QColor( "#DEE4EB" ) );
setPalette( p );
//setToolTip(tr("Click to add or remove a bookmark"));
start=-1;
end=-1;
connect( m_editor->verticalScrollBar(), SIGNAL( valueChanged( int ) ), this, SLOT( update() ) );
connect( m_editor->verticalScrollBar(), SIGNAL( actionTriggered( int ) ), this, SLOT( update() ) );
connect( m_editor, SIGNAL( textChanged() ), this, SLOT( update() ) );
connect(m_editor, SIGNAL(updatelineWidget()), this, SLOT(update()));
}
LightLineNumberWidget::~LightLineNumberWidget() {
}
void LightLineNumberWidget::paintEvent( QPaintEvent* event ) {
int max=0;
int l=0;
QPainter painter( this );
painter.setFont(numfont);
const QFontMetrics fm(numfont);
const QBrush oldbrush=painter.brush();
QPen oldpen(QColor("#136872"));
oldpen.setStyle(Qt::SolidLine);
const QBrush bookmarkbrush(QColor("#1B8EA6"));
const QPen bookmarkpen(QColor("#FFFFFF"));
QTextDocument *doc = m_editor->document();
int i = 1;
QTextBlock p = doc->begin();
QTextBlock np;
QString numtext;
painter.setPen(oldpen);
painter.drawLine(width()-2,0,width()-2,height());
int rmin=-1;
int rmax=-1;
int realmin=-1;
int top;
int delta;
while ( p.isValid()) {
if (p.isVisible()) top = (int) m_editor->blockGeometry(p).top();
np=p.next();
if ( np.isValid() && np.isVisible()) delta= (int) m_editor->blockGeometry(np).top()-top;
else delta=fm.lineSpacing();
if (top<0) {
if (i==start+1) {
realmin=top;
if (top>=0) rmin=top;
else rmin=0;
}
if (i<=end+1) rmax=top+delta;//top+fm.lineSpacing();
p = p.next();
i++;
continue;
}
if (top>height()) break;
for (int j = 0; j < 3; ++j) {
if (m_editor->UserBookmark[j]==i) {
painter.fillRect(2, top,fm.width("0")+6,fm.lineSpacing(), bookmarkbrush);
painter.setPen(bookmarkpen);
painter.drawText(4,top,width()-4,fm.lineSpacing(),Qt::AlignLeft | Qt::AlignTop,QString::number(j+1));
}
}
painter.setPen(oldpen);
numtext=QString::number(i);
if (p.isVisible()) painter.drawText(10+fm.width("0"), top,fm.width(numtext),fm.lineSpacing(),Qt::AlignRight | Qt::AlignTop,numtext);
l= fm.width(numtext)+8+fm.width("0");
if (l>max) max=l;
if (i==start+1) {
realmin=top;
if (top>=0) rmin=top;
else rmin=0;
}
if (i<=end+1) rmax=top+delta;//top+fm.lineSpacing();
p = p.next();
i++;
}
if (i>=10000) setFixedWidth(max);
painter.end();
}
void LightLineNumberWidget::mousePressEvent(QMouseEvent *e) {
e->accept();
const QFontMetrics fm(numfont);
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 LightLineNumberWidget::setFont(QFont efont) {
numfont=efont;
}
bool LightLineNumberWidget::event(QEvent *event) {
const QFontMetrics fm(numfont);
if (event->type() == QEvent::ToolTip) {
QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event);
int p = helpEvent->pos().x();
if (abs(p)>=fm.width("0")+8) {
QToolTip::hideText();
event->ignore();
} else QToolTip::showText(helpEvent->globalPos(),tr("Click to add or remove a bookmark"));
return true;
}
return QWidget::event(event);
}