-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhtmlviewer.cpp
More file actions
121 lines (101 loc) · 4.24 KB
/
htmlviewer.cpp
File metadata and controls
121 lines (101 loc) · 4.24 KB
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
#include "htmlviewer.h"
#include <QTextBlock>
#include <QDebug>
#include <QScrollBar>
#include <QAbstractTextDocumentLayout>
HTMLViewer::HTMLViewer()
{
setReadOnly(true);
this->setFrameStyle(QFrame::NoFrame);
this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->setOpenExternalLinks(true);
this->setOpenLinks(true);
this->document()->setIndentWidth(LIST_INDENT_WIDTH);
this->setStyleSheet("background-color:white;");
this->document()->setDocumentMargin(DOCUMENT_MARGIN);
}
void HTMLViewer::paintEvent(QPaintEvent *event){
// qDebug()<<num<<verticalScrollBar()->value()<<verticalScrollBar()->maximum();
// qDebug()<<contentsRect()<<viewport()->rect()<<event->rect();
// if(num > 2) return;
QTextBrowser::paintEvent(event);
QPainter p;
p.begin(this->viewport());
p.setRenderHint(QPainter::Antialiasing);
// painter.setCompositionMode(QPainter::CompositionMode_SourceAtop);
// painter.setCompositionMode(QPainter::CompositionMode_DestinationAtop);
// qDebug()<<"blockCount"<<document()->blockCount();
int maxWidth = 0,maxHeight = 0,count = 0;
QTextBlock textBlock = document()->begin();
while(textBlock.isValid()){
p.setPen(QPen(QColor(0 , 0 , 255), 1, Qt::SolidLine, Qt::FlatCap,Qt::MiterJoin));
// qDebug()<<textBlock.text()<<textBlock.fragmentIndex();;
// QTextBlock::iterator it;
// for (it = textBlock.begin(); !(it.atEnd()); ++it) {
// QTextFragment currentFragment = it.fragment();
// if (currentFragment.isValid()){
// qDebug()<<currentFragment.text();
// }
// }
QRect temp(textBlock.layout()->boundingRect().toRect().x()+4,
textBlock.layout()->position().y(),
textBlock.layout()->boundingRect().toRect().width(),
textBlock.layout()->boundingRect().toRect().height());
// qDebug()<<textBlock.text()
// <<textBlock.layout()->position() //(x,y) margin = 4
// <<textBlock.layout()->boundingRect().toRect() // (indent_x,y,w,h)
// <<temp;
count = temp.bottomRight().x() > maxWidth ? 0 : count + ( maxWidth == temp.bottomRight().x() );
maxWidth = qMax(maxWidth,temp.bottomRight().x());
maxHeight = qMax(maxHeight,temp.bottomRight().y());
// qDebug()<<textBlock.text();
temp.translate(-horizontalScrollBar()->value(), -verticalScrollBar()->value());
QPoint tl = textBlock.layout()->boundingRect().toRect().topLeft();
QPoint br = textBlock.layout()->boundingRect().toRect().bottomRight();
if(tl.x() == CODE_X){
// qDebug("code block");
QRect rect(DOCUMENT_MARGIN,temp.y(),CODE_X - CODE_MARGIN,temp.height());
p.fillRect(rect,QBrush(QColor(CODE_PRE_BG)));
QRect rect2(CODE_X - CODE_MARGIN + DOCUMENT_MARGIN,temp.y(),CODE_MARGIN,temp.height());
p.fillRect(rect2,QBrush(QColor(CODE_BG)));
// QPainterPath path;
// path.addRoundedRect(temp, 6, 6);
// p.setPen(QPen(QColor(CODE_PRE_BG), 1));
// p.drawPath(path);
}else if(tl.x()>0){
// qDebug()<<"INDENT_WIDTH";
if(br.x() < maxWidth - LIST_INDENT_WIDTH){
// qDebug()<<"quote";
for(int i = 0;i < tl.x()/LIST_INDENT_WIDTH;i++){
QRect rect(DOCUMENT_MARGIN + LIST_INDENT_WIDTH*i,temp.y() - LINE_MARGIN/2, QUOTE_PRE_WIDTH ,temp.height() + LINE_MARGIN);
p.fillRect(rect,QBrush(QColor(QUOTE_PRE_BG)));
}
}else{
// qDebug()<<"list";
}
}else{
// p.drawRect(temp);
}
textBlock = textBlock.next();
}
p.end();
if(count < document()->blockCount()*0.5 ){
qDebug()<<"+=+=+=+=";
maxWidth += 40;
setMinimumWidth(maxWidth);
}
// qDebug()<<count<<maxWidth<<maxHeight;
setMinimumHeight(maxHeight + 60);
num++;
return;
}
void HTMLViewer::sourceChanged(const QUrl &src){
qDebug()<<src;
return sourceChanged(src);
}
void HTMLViewer::setSource(const QUrl &name){
qDebug()<<name;
return;
// return setSource(name);
}