1 //=============================================================================
2 //
3 //   File : HelpWidget.cpp
4 //   Creation date : Thu Aug 10 2000 17:42:12 by Szymon Stefanek
5 //
6 //   This file is part of the KVIrc IRC client distribution
7 //   Copyright (C) 2000-2010 Szymon Stefanek (pragma at kvirc dot net)
8 //
9 //   This program is FREE software. You can redistribute it and/or
10 //   modify it under the terms of the GNU General Public License
11 //   as published by the Free Software Foundation; either version 2
12 //   of the License, or (at your option) any later version.
13 //
14 //   This program is distributed in the HOPE that it will be USEFUL,
15 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
16 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 //   See the GNU General Public License for more details.
18 //
19 //   You should have received a copy of the GNU General Public License
20 //   along with this program. If not, write to the Free Software Foundation,
21 //   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 //=============================================================================
24 
25 #include "HelpWidget.h"
26 #include "HelpWindow.h"
27 
28 #include "KviMainWindow.h"
29 #include "KviIconManager.h"
30 #include "KviLocale.h"
31 #include "KviApplication.h"
32 #include "KviMessageBox.h"
33 #include "KviModule.h"
34 #include "KviFileUtils.h"
35 
36 #include <QToolButton>
37 #include <QLineEdit>
38 #include <QToolTip>
39 #include <QTimer>
40 #include <QClipboard>
41 #include <QDir>
42 #include <QAction>
43 
44 extern HelpIndex * g_pDocIndex;
45 extern KviPointerList<HelpWindow> * g_pHelpWindowList;
46 extern KviPointerList<HelpWidget> * g_pHelpWidgetList;
47 
48 #ifdef COMPILE_WEBKIT_SUPPORT
49 #include <QShortcut>
50 #include <QAction>
51 
52 #define HIGHLIGHT_FLAGS QWebPage::HighlightAllOccurrences
53 
HelpWidget(QWidget * par,bool bIsStandalone)54 HelpWidget::HelpWidget(QWidget * par, bool bIsStandalone)
55     : QWidget(par)
56 {
57 	setObjectName("help_widget");
58 	setMinimumWidth(80);
59 	if(bIsStandalone)
60 		g_pHelpWidgetList->append(this);
61 	m_bIsStandalone = bIsStandalone;
62 
63 	new QShortcut(QKeySequence::Copy, this, SLOT(slotCopy()), nullptr, Qt::WidgetWithChildrenShortcut);
64 	new QShortcut(QKeySequence::Find, this, SLOT(slotShowHideFind()), nullptr, bIsStandalone ? Qt::WidgetWithChildrenShortcut : Qt::WindowShortcut);
65 
66 	// layout
67 	m_pLayout = new QVBoxLayout(this);
68 	m_pLayout->setMargin(0);
69 	m_pLayout->setSpacing(0);
70 	setLayout(m_pLayout);
71 
72 	// upper toolbar
73 	m_pToolBar = new QToolBar(this);
74 	m_pLayout->addWidget(m_pToolBar);
75 
76 	// webview
77 	m_pTextBrowser = new QWebView(this);
78 	m_pTextBrowser->setObjectName("text_browser");
79 	m_pTextBrowser->setStyleSheet("QTextBrowser { background-color:white; color:black; }");
80 	m_pLayout->addWidget(m_pTextBrowser);
81 	connect(m_pTextBrowser, SIGNAL(loadFinished(bool)), this, SLOT(slotLoadFinished(bool)));
82 
83 	// lower toolbar
84 	m_pToolBarHighlight = new QToolBar(this);
85 	m_pLayout->addWidget(m_pToolBarHighlight);
86 	m_pToolBarHighlight->hide();
87 
88 	QLabel * pHighlightLabel = new QLabel();
89 	pHighlightLabel->setText(__tr2qs("Find: "));
90 	m_pToolBarHighlight->addWidget(pHighlightLabel);
91 
92 	m_pFindText = new QLineEdit();
93 	m_pToolBarHighlight->addWidget(m_pFindText);
94 	connect(m_pFindText, SIGNAL(textChanged(const QString)), this, SLOT(slotTextChanged(const QString)));
95 
96 	m_pToolBarHighlight->addAction(*g_pIconManager->getBigIcon(KVI_BIGICON_HELPBACK), __tr2qs("Find previous"), this, SLOT(slotFindPrev()));
97 	m_pToolBarHighlight->addAction(*g_pIconManager->getBigIcon(KVI_BIGICON_HELPFORWARD), __tr2qs("Find next"), this, SLOT(slotFindNext()));
98 	m_pToolBarHighlight->addAction(*g_pIconManager->getSmallIcon(KviIconManager::Discard), __tr2qs("Close"), this, SLOT(slotShowHideFind()));
99 
100 	// upper toolbar contents (depends on webview)
101 	m_pToolBar->addAction(*g_pIconManager->getBigIcon(KVI_BIGICON_HELPINDEX), __tr2qs("Show index"), this, SLOT(showIndex()));
102 
103 	QAction * pAction;
104 	pAction = m_pTextBrowser->pageAction(QWebPage::Back);
105 	pAction->setIcon(*g_pIconManager->getBigIcon(KVI_BIGICON_HELPBACK));
106 	m_pToolBar->addAction(pAction);
107 	pAction = m_pTextBrowser->pageAction(QWebPage::Forward);
108 	pAction->setIcon(*g_pIconManager->getBigIcon(KVI_BIGICON_HELPFORWARD));
109 	m_pToolBar->addAction(pAction);
110 
111 	m_pToolBar->addAction(*(g_pIconManager->getSmallIcon(KviIconManager::Minus)), __tr2qs("Zoom out"), this, SLOT(slotZoomOut()));
112 	m_pToolBar->addAction(*(g_pIconManager->getSmallIcon(KviIconManager::Plus)), __tr2qs("Zoom in"), this, SLOT(slotZoomIn()));
113 
114 	if(bIsStandalone)
115 	{
116 		setAttribute(Qt::WA_DeleteOnClose);
117 		m_pToolBar->addAction(*g_pIconManager->getBigIcon(KVI_BIGICON_HELPCLOSE), __tr2qs("Close"), this, SLOT(close()));
118 	}
119 }
120 
slotCopy()121 void HelpWidget::slotCopy()
122 {
123 	m_pTextBrowser->triggerPageAction(QWebPage::Copy);
124 }
125 
slotShowHideFind()126 void HelpWidget::slotShowHideFind()
127 {
128 	if(m_pToolBarHighlight->isVisible())
129 	{
130 		m_pToolBarHighlight->hide();
131 		m_pTextBrowser->findText("", HIGHLIGHT_FLAGS);
132 	}
133 	else
134 	{
135 		m_pToolBarHighlight->show();
136 		m_pFindText->setFocus();
137 	}
138 }
139 
slotLoadFinished(bool)140 void HelpWidget::slotLoadFinished(bool)
141 {
142 	m_pTextBrowser->findText(m_pFindText->text(), HIGHLIGHT_FLAGS);
143 }
144 
slotTextChanged(const QString szFind)145 void HelpWidget::slotTextChanged(const QString szFind)
146 {
147 	m_pTextBrowser->findText("", HIGHLIGHT_FLAGS);
148 	m_pTextBrowser->findText(szFind, HIGHLIGHT_FLAGS);
149 }
150 
slotFindPrev()151 void HelpWidget::slotFindPrev()
152 {
153 	m_pTextBrowser->findText(m_pFindText->text(), QWebPage::FindBackward);
154 }
155 
slotFindNext()156 void HelpWidget::slotFindNext()
157 {
158 	m_pTextBrowser->findText(m_pFindText->text());
159 }
160 
slotZoomIn()161 void HelpWidget::slotZoomIn()
162 {
163 	kvs_real_t dZoom = m_pTextBrowser->zoomFactor();
164 	if(dZoom >= 2)
165 		return;
166 	dZoom += 0.05;
167 	m_pTextBrowser->setZoomFactor(dZoom);
168 }
169 
slotZoomOut()170 void HelpWidget::slotZoomOut()
171 {
172 	kvs_real_t dZoom = m_pTextBrowser->zoomFactor();
173 	if(dZoom <= 0.5)
174 		return;
175 	dZoom -= 0.05;
176 	m_pTextBrowser->setZoomFactor(dZoom);
177 }
178 
179 #else
180 
HelpWidget(QWidget * par,bool bIsStandalone)181 HelpWidget::HelpWidget(QWidget * par, bool bIsStandalone)
182     : QWidget(par)
183 {
184 	setObjectName("help_widget");
185 	setMinimumWidth(80);
186 	if(bIsStandalone)
187 		g_pHelpWidgetList->append(this);
188 	m_bIsStandalone = bIsStandalone;
189 
190 	// layout
191 	m_pLayout = new QVBoxLayout(this);
192 	m_pLayout->setMargin(0);
193 	m_pLayout->setSpacing(0);
194 	setLayout(m_pLayout);
195 
196 	m_pTextBrowser = new QTextBrowser(this);
197 	m_pTextBrowser->setObjectName("text_browser");
198 	m_pTextBrowser->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
199 	m_pTextBrowser->setStyleSheet("QTextBrowser { background-color:white; color:black; }");
200 
201 	m_pToolBar = new QToolBar(this);
202 	m_pToolBar->addAction(*g_pIconManager->getBigIcon(KVI_BIGICON_HELPINDEX), __tr2qs("Show index"), this, SLOT(showIndex()));
203 
204 	m_pBackAction = new QAction(*g_pIconManager->getBigIcon(KVI_BIGICON_HELPBACK), __tr2qs("Back"), this);
205 	m_pBackAction->setEnabled(false);
206 	connect(m_pBackAction, SIGNAL(triggered()), m_pTextBrowser, SLOT(backward()));
207 	m_pToolBar->addAction(m_pBackAction);
208 
209 	m_pForwardAction = new QAction(*g_pIconManager->getBigIcon(KVI_BIGICON_HELPFORWARD), __tr2qs("Forward"), this);
210 	m_pForwardAction->setEnabled(false);
211 	connect(m_pForwardAction, SIGNAL(triggered()), m_pTextBrowser, SLOT(forward()));
212 	m_pToolBar->addAction(m_pForwardAction);
213 
214 	if(bIsStandalone)
215 	{
216 		setAttribute(Qt::WA_DeleteOnClose);
217 
218 		m_pToolBar->addAction(*g_pIconManager->getBigIcon(KVI_BIGICON_HELPCLOSE), __tr2qs("Close"), this, SLOT(close()));
219 	}
220 
221 	m_pLayout->addWidget(m_pToolBar);
222 	m_pLayout->addWidget(m_pTextBrowser);
223 
224 	connect(m_pTextBrowser, SIGNAL(backwardAvailable(bool)), m_pBackAction, SLOT(setEnabled(bool)));
225 	connect(m_pTextBrowser, SIGNAL(forwardAvailable(bool)), m_pForwardAction, SLOT(setEnabled(bool)));
226 }
227 
228 #endif
229 
~HelpWidget()230 HelpWidget::~HelpWidget()
231 {
232 	if(m_bIsStandalone)
233 		g_pHelpWidgetList->removeRef(this);
234 }
235 
showIndex()236 void HelpWidget::showIndex()
237 {
238 	QString szHelpDir;
239 	QDir dirHelp;
240 
241 	g_pApp->getGlobalKvircDirectory(szHelpDir, KviApplication::Help);
242 	dirHelp = QDir(szHelpDir);
243 #ifdef COMPILE_WEBKIT_SUPPORT
244 	m_pTextBrowser->load(QUrl::fromLocalFile(dirHelp.absoluteFilePath("index.html")));
245 #else
246 	m_pTextBrowser->setSource(QUrl::fromLocalFile(dirHelp.absoluteFilePath("index.html")));
247 #endif
248 }
249