1 | /**********************************************************************
|
---|
2 | ** Copyright (C) 2000-2007 Trolltech ASA. All rights reserved.
|
---|
3 | **
|
---|
4 | ** This file is part of the Qt Assistant.
|
---|
5 | **
|
---|
6 | ** This file may be distributed and/or modified under the terms of the
|
---|
7 | ** GNU General Public License version 2 as published by the Free Software
|
---|
8 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
9 | ** packaging of this file.
|
---|
10 | **
|
---|
11 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
---|
12 | ** licenses may use this file in accordance with the Qt Commercial License
|
---|
13 | ** Agreement provided with the Software.
|
---|
14 | **
|
---|
15 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
16 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
17 | **
|
---|
18 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
19 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
20 | ** information about Qt Commercial License Agreements.
|
---|
21 | **
|
---|
22 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
23 | ** not clear to you.
|
---|
24 | **
|
---|
25 | **********************************************************************/
|
---|
26 |
|
---|
27 | #include "helpwindow.h"
|
---|
28 | #include "mainwindow.h"
|
---|
29 | #include "tabbedbrowser.h"
|
---|
30 | #include "helpdialogimpl.h"
|
---|
31 | #include "config.h"
|
---|
32 |
|
---|
33 | #include <qapplication.h>
|
---|
34 | #include <qclipboard.h>
|
---|
35 | #include <qurl.h>
|
---|
36 | #include <qmessagebox.h>
|
---|
37 | #include <qdir.h>
|
---|
38 | #include <qfile.h>
|
---|
39 | #include <qprocess.h>
|
---|
40 | #include <qpopupmenu.h>
|
---|
41 | #include <qaction.h>
|
---|
42 | #include <qfileinfo.h>
|
---|
43 | #include <qevent.h>
|
---|
44 | #include <qtextstream.h>
|
---|
45 | #include <qtextcodec.h>
|
---|
46 |
|
---|
47 | #if defined(Q_OS_WIN32)
|
---|
48 | #include <windows.h>
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | HelpWindow::HelpWindow( MainWindow *w, QWidget *parent, const char *name )
|
---|
52 | : QTextBrowser( parent, name ), mw( w ), blockScroll( FALSE ),
|
---|
53 | shiftPressed( FALSE ), newWindow( FALSE )
|
---|
54 | {
|
---|
55 | connect(this, SIGNAL(forwardAvailable(bool)), this, SLOT(updateForward(bool)));
|
---|
56 | connect(this, SIGNAL(backwardAvailable(bool)), this, SLOT(updateBackward(bool)));
|
---|
57 | }
|
---|
58 |
|
---|
59 | void HelpWindow::setSource( const QString &name )
|
---|
60 | {
|
---|
61 | if ( name.isEmpty() )
|
---|
62 | return;
|
---|
63 |
|
---|
64 | if (newWindow || shiftPressed) {
|
---|
65 | removeSelection();
|
---|
66 | mw->saveSettings();
|
---|
67 | mw->saveToolbarSettings();
|
---|
68 | MainWindow *nmw = new MainWindow;
|
---|
69 |
|
---|
70 | QFileInfo currentInfo( source() );
|
---|
71 | QFileInfo linkInfo( name );
|
---|
72 | QString target = name;
|
---|
73 | if( linkInfo.isRelative() )
|
---|
74 | target = currentInfo.dirPath( TRUE ) + "/" + name;
|
---|
75 |
|
---|
76 | nmw->setup();
|
---|
77 | nmw->move( mw->geometry().topLeft() );
|
---|
78 | if ( mw->isMaximized() )
|
---|
79 | nmw->showMaximized();
|
---|
80 | else
|
---|
81 | nmw->show();
|
---|
82 | nmw->showLink( target );
|
---|
83 | return;
|
---|
84 | }
|
---|
85 |
|
---|
86 | if ( name.left( 7 ) == "http://" || name.left( 6 ) == "ftp://" ) {
|
---|
87 | QString webbrowser = Config::configuration()->webBrowser();
|
---|
88 | if ( webbrowser.isEmpty() ) {
|
---|
89 | #if defined(Q_OS_WIN32)
|
---|
90 | QT_WA( {
|
---|
91 | ShellExecute( winId(), 0, (TCHAR*)name.ucs2(), 0, 0, SW_SHOWNORMAL );
|
---|
92 | } , {
|
---|
93 | ShellExecuteA( winId(), 0, name.local8Bit(), 0, 0, SW_SHOWNORMAL );
|
---|
94 | } );
|
---|
95 | #elif defined(Q_OS_MACX)
|
---|
96 | webbrowser = "/usr/bin/open";
|
---|
97 | #else
|
---|
98 | int result = QMessageBox::information( mw, tr( "Help" ),
|
---|
99 | tr( "Currently no Web browser is selected.\nPlease use the settings dialog to specify one!\n" ),
|
---|
100 | "Open", "Cancel" );
|
---|
101 | if ( result == 0 ) {
|
---|
102 | emit chooseWebBrowser();
|
---|
103 | webbrowser = Config::configuration()->webBrowser();
|
---|
104 | }
|
---|
105 | #endif
|
---|
106 | if ( webbrowser.isEmpty() )
|
---|
107 | return;
|
---|
108 | }
|
---|
109 | QProcess *proc = new QProcess(this);
|
---|
110 |
|
---|
111 | proc->addArgument( webbrowser );
|
---|
112 | QObject::connect(proc, SIGNAL(processExited()), proc, SLOT(deleteLater()));
|
---|
113 | proc->addArgument( name );
|
---|
114 | proc->start();
|
---|
115 | return;
|
---|
116 | }
|
---|
117 |
|
---|
118 | if ( name.right( 3 ) == "pdf" ) {
|
---|
119 | QString pdfbrowser = Config::configuration()->pdfReader();
|
---|
120 | if ( pdfbrowser.isEmpty() ) {
|
---|
121 | #if defined(Q_OS_MACX)
|
---|
122 | pdfbrowser = "/usr/bin/open";
|
---|
123 | #else
|
---|
124 | QMessageBox::information( mw,
|
---|
125 | tr( "Help" ),
|
---|
126 | tr( "No PDF Viewer has been specified\n"
|
---|
127 | "Please use the settings dialog to specify one!\n" ) );
|
---|
128 | return;
|
---|
129 | #endif
|
---|
130 | }
|
---|
131 | QFileInfo info( pdfbrowser );
|
---|
132 | if( !info.exists() ) {
|
---|
133 | QMessageBox::information( mw,
|
---|
134 | tr( "Help" ),
|
---|
135 | tr( "Qt Assistant is unable to start the PDF Viewer\n\n"
|
---|
136 | "%1\n\n"
|
---|
137 | "Please make sure that the executable exists and is located at\n"
|
---|
138 | "the specified location." ).arg( pdfbrowser ) );
|
---|
139 | return;
|
---|
140 | }
|
---|
141 | QProcess *proc = new QProcess(this);
|
---|
142 | QObject::connect(proc, SIGNAL(processExited()), proc, SLOT(deleteLater()));
|
---|
143 | proc->addArgument( pdfbrowser );
|
---|
144 | proc->addArgument( name );
|
---|
145 | proc->start();
|
---|
146 |
|
---|
147 | return;
|
---|
148 | }
|
---|
149 |
|
---|
150 | QUrl u( context(), name );
|
---|
151 | if ( !u.isLocalFile() ) {
|
---|
152 | QMessageBox::information( mw, tr( "Help" ), tr( "Can't load and display non-local file\n"
|
---|
153 | "%1" ).arg( name ) );
|
---|
154 | return;
|
---|
155 | }
|
---|
156 |
|
---|
157 | setText("<body bgcolor=\"" + paletteBackgroundColor().name() + "\">");
|
---|
158 | QTextBrowser::setSource( name );
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | void HelpWindow::openLinkInNewWindow()
|
---|
163 | {
|
---|
164 | if ( lastAnchor.isEmpty() )
|
---|
165 | return;
|
---|
166 | newWindow = TRUE;
|
---|
167 | setSource(lastAnchor);
|
---|
168 | newWindow = FALSE;
|
---|
169 | }
|
---|
170 |
|
---|
171 | void HelpWindow::openLinkInNewWindow( const QString &link )
|
---|
172 | {
|
---|
173 | lastAnchor = link;
|
---|
174 | openLinkInNewWindow();
|
---|
175 | }
|
---|
176 |
|
---|
177 | void HelpWindow::openLinkInNewPage()
|
---|
178 | {
|
---|
179 | if( lastAnchor.isEmpty() )
|
---|
180 | return;
|
---|
181 | mw->browsers()->newTab( lastAnchor );
|
---|
182 | lastAnchor = QString::null;
|
---|
183 | }
|
---|
184 |
|
---|
185 | void HelpWindow::openLinkInNewPage( const QString &link )
|
---|
186 | {
|
---|
187 | lastAnchor = link;
|
---|
188 | openLinkInNewPage();
|
---|
189 | }
|
---|
190 |
|
---|
191 | QPopupMenu *HelpWindow::createPopupMenu( const QPoint& pos )
|
---|
192 | {
|
---|
193 | QPopupMenu *m = new QPopupMenu(0);
|
---|
194 | lastAnchor = anchorAt( pos );
|
---|
195 | if ( !lastAnchor.isEmpty() ) {
|
---|
196 | if ( lastAnchor.at( 0 ) == '#' ) {
|
---|
197 | QString src = source();
|
---|
198 | int hsh = src.find( '#' );
|
---|
199 | lastAnchor = ( hsh>=0 ? src.left( hsh ) : src ) + lastAnchor;
|
---|
200 | }
|
---|
201 | m->insertItem( tr("Open Link in New Window\tShift+LMB"),
|
---|
202 | this, SLOT( openLinkInNewWindow() ) );
|
---|
203 | m->insertItem( tr("Open Link in New Tab"),
|
---|
204 | this, SLOT( openLinkInNewPage() ) );
|
---|
205 | }
|
---|
206 | mw->actionNewWindow->addTo( m );
|
---|
207 | mw->actionOpenPage->addTo( m );
|
---|
208 | mw->actionClosePage->addTo( m );
|
---|
209 | m->insertSeparator();
|
---|
210 | mw->actionGoPrevious->addTo( m );
|
---|
211 | mw->actionGoNext->addTo( m );
|
---|
212 | mw->actionGoHome->addTo( m );
|
---|
213 | m->insertSeparator();
|
---|
214 | mw->actionZoomIn->addTo( m );
|
---|
215 | mw->actionZoomOut->addTo( m );
|
---|
216 | m->insertSeparator();
|
---|
217 | mw->actionEditCopy->addTo( m );
|
---|
218 | mw->actionEditFind->addTo( m );
|
---|
219 | return m;
|
---|
220 | }
|
---|
221 |
|
---|
222 | void HelpWindow::blockScrolling( bool b )
|
---|
223 | {
|
---|
224 | blockScroll = b;
|
---|
225 | }
|
---|
226 |
|
---|
227 | void HelpWindow::ensureCursorVisible()
|
---|
228 | {
|
---|
229 | if ( !blockScroll )
|
---|
230 | QTextBrowser::ensureCursorVisible();
|
---|
231 | }
|
---|
232 |
|
---|
233 | void HelpWindow::contentsMousePressEvent(QMouseEvent *e)
|
---|
234 | {
|
---|
235 | shiftPressed = ( e->state() & ShiftButton );
|
---|
236 | QTextBrowser::contentsMousePressEvent(e);
|
---|
237 | }
|
---|
238 |
|
---|
239 | void HelpWindow::keyPressEvent(QKeyEvent *e)
|
---|
240 | {
|
---|
241 | shiftPressed = ( e->state() & ShiftButton );
|
---|
242 | QTextBrowser::keyPressEvent(e);
|
---|
243 | }
|
---|
244 |
|
---|
245 | void HelpWindow::copy()
|
---|
246 | {
|
---|
247 | if (textFormat() == PlainText) {
|
---|
248 | QTextEdit::copy();
|
---|
249 | } else {
|
---|
250 | TextFormat oldTf = textFormat();
|
---|
251 | setTextFormat(PlainText);
|
---|
252 | QString selectText = selectedText();
|
---|
253 | selectText.replace("<br>", "\n");
|
---|
254 | selectText.replace("\xa0", " ");
|
---|
255 | selectText.replace(">", ">");
|
---|
256 | selectText.replace("<", "<");
|
---|
257 | selectText.replace("&", "&");
|
---|
258 |
|
---|
259 | QClipboard *cb = QApplication::clipboard();
|
---|
260 | if (cb->supportsSelection())
|
---|
261 | cb->setText(selectText, QClipboard::Selection);
|
---|
262 | cb->setText(selectText, QClipboard::Clipboard);
|
---|
263 | setTextFormat(oldTf);
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | void HelpWindow::updateForward(bool fwd)
|
---|
268 | {
|
---|
269 | fwdAvail = fwd;
|
---|
270 | }
|
---|
271 |
|
---|
272 | void HelpWindow::updateBackward(bool back)
|
---|
273 | {
|
---|
274 | backAvail = back;
|
---|
275 | }
|
---|