1 /**************************************************************************
2 ** This file is part of LiteIDE
3 **
4 ** Copyright (c) 2011-2019 LiteIDE. All rights reserved.
5 **
6 ** This library is free software; you can redistribute it and/or
7 ** modify it under the terms of the GNU Lesser General Public
8 ** License as published by the Free Software Foundation; either
9 ** version 2.1 of the License, or (at your option) any later version.
10 **
11 ** This library is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ** Lesser General Public License for more details.
15 **
16 ** In addition, as a special exception,  that plugins developed for LiteIDE,
17 ** are allowed to remain closed sourced and can be distributed under any license .
18 ** These rights are included in the file LGPL_EXCEPTION.txt in this package.
19 **
20 **************************************************************************/
21 // Module: textbrowserhtmlwidget.cpp
22 // Creator: visualfc <visualfc@gmail.com>
23 
24 #include "textbrowserhtmlwidget.h"
25 #include <QTextBrowser>
26 #include <QScrollBar>
27 #include <QFileInfo>
28 #include <QFile>
29 #include <QTextCursor>
30 #include <QTextBlock>
31 //lite_memory_check_begin
32 #if defined(WIN32) && defined(_MSC_VER) &&  defined(_DEBUG)
33      #define _CRTDBG_MAP_ALLOC
34      #include <stdlib.h>
35      #include <crtdbg.h>
36      #define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )
37      #define new DEBUG_NEW
38 #endif
39 //lite_memory_check_end
40 
TextBrowserHtmlWidget(QObject * parent)41 TextBrowserHtmlWidget::TextBrowserHtmlWidget(QObject *parent) :
42     IHtmlWidget(parent)
43 {
44     m_widget = new QTextBrowser;
45     m_widget->setOpenLinks(false);
46     m_widget->setOpenExternalLinks(false);
47     connect(m_widget,SIGNAL(anchorClicked(QUrl)),this,SIGNAL(linkClicked(QUrl)));
48     connect(m_widget,SIGNAL(highlighted(QUrl)),this,SIGNAL(linkHovered(QUrl)));
49 }
50 
widget() const51 QWidget *TextBrowserHtmlWidget::widget() const
52 {
53     return m_widget;
54 }
55 
className() const56 QString TextBrowserHtmlWidget::className() const
57 {
58     return QLatin1String("QTextBrowser");
59 }
60 
setSearchPaths(const QStringList & paths)61 void TextBrowserHtmlWidget::setSearchPaths(const QStringList &paths)
62 {
63     m_widget->setSearchPaths(paths);
64 }
65 
setHtml(const QString & html,const QUrl & url)66 void TextBrowserHtmlWidget::setHtml(const QString &html, const QUrl &url)
67 {
68     m_widget->setHtml(html);
69     m_url = url;
70     if (!url.isEmpty()) {
71         QString file = url.toLocalFile();
72         if (!file.isEmpty()) {
73             QFileInfo info(file);
74             QStringList paths = m_widget->searchPaths();
75             paths.append(info.path());
76             paths.removeDuplicates();
77             m_widget->setSearchPaths(paths);
78         }
79     }
80     emit contentsSizeChanged();
81     emit loadFinished(true);
82 }
83 
url() const84 QUrl TextBrowserHtmlWidget::url() const
85 {
86     return m_url;
87 }
88 
clear()89 void TextBrowserHtmlWidget::clear()
90 {
91     m_widget->clear();
92 }
93 
scrollToAnchor(const QString & anchor)94 void TextBrowserHtmlWidget::scrollToAnchor(const QString &anchor)
95 {
96     m_widget->scrollToAnchor(anchor);
97     emit anchorChanged(anchor);
98 }
99 
setScrollBarValue(Qt::Orientation orientation,int value)100 void TextBrowserHtmlWidget::setScrollBarValue(Qt::Orientation orientation, int value)
101 {
102     if (orientation == Qt::Horizontal) {
103         m_widget->horizontalScrollBar()->setValue(value);
104     } else {
105         m_widget->verticalScrollBar()->setValue(value);
106     }
107 }
108 
scrollBarValue(Qt::Orientation orientation) const109 int TextBrowserHtmlWidget::scrollBarValue(Qt::Orientation orientation) const
110 {
111     if (orientation == Qt::Horizontal) {
112         return m_widget->horizontalScrollBar()->value();
113     }
114     return m_widget->verticalScrollBar()->value();
115 }
116 
scrollBarMinimum(Qt::Orientation orientation) const117 int TextBrowserHtmlWidget::scrollBarMinimum(Qt::Orientation orientation) const
118 {
119     if (orientation == Qt::Horizontal) {
120         return m_widget->horizontalScrollBar()->minimum();
121     }
122     return m_widget->verticalScrollBar()->minimum();
123 
124 }
125 
scrollBarMaximum(Qt::Orientation orientation) const126 int TextBrowserHtmlWidget::scrollBarMaximum(Qt::Orientation orientation) const
127 {
128     if (orientation == Qt::Horizontal) {
129         return m_widget->horizontalScrollBar()->maximum();
130     }
131     return m_widget->verticalScrollBar()->maximum();
132 }
133 
selectedText() const134 QString TextBrowserHtmlWidget::selectedText() const
135 {
136     return m_widget->textCursor().selectedText();
137 }
138 
findText(const QString & exp,QTextDocument::FindFlags options)139 bool TextBrowserHtmlWidget::findText(const QString &exp, QTextDocument::FindFlags options)
140 {
141     QTextDocument *doc = m_widget->document();
142     if (!doc) {
143         return false;
144     }
145     QTextCursor cursor = m_widget->cursorForPosition(QPoint(0,0));
146     int from = cursor.position();
147     if (cursor.hasSelection()) {
148         if (options & QTextDocument::FindBackward) {
149             from = cursor.selectionStart();
150         } else {
151             from = cursor.selectionEnd();
152         }
153     }
154     QTextCursor find;
155     find = doc->find(exp,from,options);
156     if (!find.isNull()) {
157         m_widget->setTextCursor(find);
158         m_widget->ensureCursorVisible();
159         return true;
160     }
161     return false;
162 }
163 
164 #ifndef QT_NO_PRINTER
print(QPrinter * printer)165 void TextBrowserHtmlWidget::print(QPrinter *printer)
166 {
167     m_widget->print(printer);
168 }
169 #endif
170 
TextBrowserHtmlWidgetFactory(QObject * parent)171 TextBrowserHtmlWidgetFactory::TextBrowserHtmlWidgetFactory(QObject *parent)
172     : IHtmlWidgetFactory(parent)
173 {
174 }
175 
className() const176 QString TextBrowserHtmlWidgetFactory::className() const
177 {
178     return "QTextBrowser";
179 }
180 
create(QObject * parent)181 IHtmlWidget *TextBrowserHtmlWidgetFactory::create(QObject *parent)
182 {
183     return new TextBrowserHtmlWidget(parent);
184 }
185 
createDocument(QObject * parent)186 IHtmlDocument *TextBrowserHtmlWidgetFactory::createDocument(QObject *parent)
187 {
188     return new TextBrowserHtmlDocument(parent);
189 }
190 
191 
TextBrowserHtmlDocument(QObject * parent)192 TextBrowserHtmlDocument::TextBrowserHtmlDocument(QObject *parent) :
193     IHtmlDocument(parent)
194 {
195     m_doc = new QTextBrowser;
196     m_doc->setVisible(false);
197 }
198 
~TextBrowserHtmlDocument()199 TextBrowserHtmlDocument::~TextBrowserHtmlDocument()
200 {
201     delete m_doc;
202 }
203 
setHtml(const QString & html,const QUrl & url)204 void TextBrowserHtmlDocument::setHtml(const QString &html, const QUrl &url)
205 {
206     m_doc->setHtml(html);
207     if (!url.isEmpty()) {
208         QString file = url.toLocalFile();
209         if (!file.isEmpty()) {
210             QFileInfo info(file);
211             QStringList paths = m_doc->searchPaths();
212             paths.append(info.path());
213             paths.removeDuplicates();
214             m_doc->setSearchPaths(paths);
215         }
216     }
217     emit loadFinished(true);
218 }
219 
220 #ifndef QT_NO_PRINTER
print(QPrinter * printer)221 void TextBrowserHtmlDocument::print(QPrinter *printer)
222 {
223     m_doc->print(printer);
224 }
225 #endif
226 
toHtml() const227 QString TextBrowserHtmlDocument::toHtml() const
228 {
229     return m_doc->toHtml();
230 }
231 
toPlainText() const232 QString TextBrowserHtmlDocument::toPlainText() const
233 {
234     return m_doc->toPlainText();
235 }
236