forked from mercutiodesign/texmaker-3.3.3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linenumberwidget.cpp
220 lines (201 loc) · 8.29 KB
/
linenumberwidget.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/***************************************************************************
* copyright : (C) 2003-2009 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 "linenumberwidget.h"
#include <QTextDocument>
#include <QTextCursor>
#include <QPlainTextEdit>
#include <QTextBlock>
#include <QDebug>
#include "blockdata.h"
LineNumberWidget::LineNumberWidget(LatexEditor* 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()));
connect( m_editor, SIGNAL( setBlockRange(int,int) ), this, SLOT( setRange(int,int) ) );
}
LineNumberWidget::~LineNumberWidget() {
}
void LineNumberWidget::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 QBrush markerbrush(QColor("#FF8000"));
const QPen bookmarkpen(QColor("#FFFFFF"));
QPen rangepen(QColor("#FF8000"));
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());
rangepen.setWidth(1);
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)+18+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();
if ((m_editor->foldedLines.keys().contains(i-1)) && (i!=start+1)) {
painter.setPen(bookmarkpen);
painter.fillRect(width()-20, top+(int) (fm.lineSpacing()/2)-5,10,10, markerbrush);
painter.drawLine(width()-17,top+(int) (fm.lineSpacing()/2),width()-13,top+(int) (fm.lineSpacing()/2));
painter.drawLine(width()-15,top+(int) (fm.lineSpacing()/2)-2,width()-15,top+(int) (fm.lineSpacing()/2)+2);
rangepen.setStyle(Qt::DotLine);
painter.setPen(rangepen);
painter.drawLine(fm.width("0")+8,top+fm.lineSpacing(),width()-12,top+fm.lineSpacing() );
rangepen.setStyle(Qt::SolidLine);
painter.setPen(rangepen);
}
p = p.next();
i++;
}
if (i>=10000) setFixedWidth(max);
//const QBrush rangebrush(QColor("#FF8000"));
painter.setPen(rangepen);
if ((rmin>=0) && (rmax>=0)) {
if (realmin>=0) {
painter.setPen(bookmarkpen);
painter.fillRect(width()-20,rmin+(int) (fm.lineSpacing()/2)-5,10,10, markerbrush);
painter.drawLine(width()-17,rmin+(int) (fm.lineSpacing()/2),width()-14,rmin+(int) (fm.lineSpacing()/2));
painter.setPen(rangepen);
if (m_editor->foldedLines.keys().contains(start)) {
painter.setPen(bookmarkpen);
painter.drawLine(width()-17,rmin+(int) (fm.lineSpacing()/2),width()-13,rmin+(int) (fm.lineSpacing()/2));
painter.drawLine(width()-15,rmin+(int) (fm.lineSpacing()/2)-2,width()-15,rmin+(int) (fm.lineSpacing()/2)+2);
rangepen.setStyle(Qt::DotLine);
painter.setPen(rangepen);
painter.drawLine(fm.width("0")+8,rmin+fm.lineSpacing(),width()-12,rmin+fm.lineSpacing() );
rangepen.setStyle(Qt::SolidLine);
painter.setPen(rangepen);
} else {
//painter.drawLine(width()-7,rmin+2,width()-7,qMin(rmax,height()));
painter.fillRect(width()-7,rmin+2,2,qMin(rmax,height())-rmin-2, markerbrush);
//painter.drawLine(width()-6,rmin+2,width()-6,qMin(rmax,height()));
}
} else {
//painter.drawLine(width()-7,rmin,width()-7,qMin(rmax,height()));
painter.fillRect(width()-7,rmin,2,qMin(rmax,height())-rmin, markerbrush);
//painter.drawLine(width()-6,rmin,width()-6,qMin(rmax,height()));
}
}
painter.end();
}
void LineNumberWidget::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 (abs(p.x())<width()-fm.width("0")-6) {
if (i==start+1) {
/* QMapIterator<int, int> it(m_editor->foldedLines);
while (it.hasNext()) {
it.next();
qDebug() << "folded" << it.key() << ": " << it.value();
}*/
if (m_editor->foldedLines.keys().contains(start)) {
m_editor->unfold(start,end);
} else {
m_editor->fold(start,end);
}
} else if (m_editor->foldedLines.keys().contains(i-1)) {
m_editor->unfold(i-1,m_editor->foldedLines[i-1]);
}
} else {
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 LineNumberWidget::setFont(QFont efont) {
numfont=efont;
}
void LineNumberWidget::setRange(int s, int e) {
start=s;
end=e;
update();
}
bool LineNumberWidget::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);
}