1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the assistant application of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "tabbedbrowser.h"
30
#include "mainwindow.h"
31
#include "helpwindow.h"
34
#include <QtGui/QStyleOptionTab>
36
#include <qfileinfo.h>
37
#include <qtoolbutton.h>
42
#include <qstackedwidget.h>
44
TabbedBrowser::TabbedBrowser(MainWindow *parent)
50
QStackedWidget *stack = qFindChild<QStackedWidget*>(ui.tab);
52
stack->setContentsMargins(0, 0, 0, 0);
53
connect(stack, SIGNAL(currentChanged(int)), parent, SLOT(browserTabChanged()));
55
QPalette p = palette();
56
p.setColor(QPalette::Inactive, QPalette::Highlight,
57
p.color(QPalette::Active, QPalette::Highlight));
58
p.setColor(QPalette::Inactive, QPalette::HighlightedText,
59
p.color(QPalette::Active, QPalette::HighlightedText));
63
TabbedBrowser::~TabbedBrowser()
67
MainWindow *TabbedBrowser::mainWindow() const
69
return static_cast<MainWindow*>(parentWidget());
72
static QString reduceLabelLength( const QString &s )
76
if ( str.length() < maxLength )
78
str = str.left( maxLength - 3 );
79
str += QLatin1String("...");
83
void TabbedBrowser::forward()
85
currentBrowser()->forward();
88
void TabbedBrowser::backward()
90
currentBrowser()->backward();
93
void TabbedBrowser::setSource( const QString &ref )
95
HelpWindow * win = currentBrowser();
99
void TabbedBrowser::reload()
101
currentBrowser()->reload();
104
void TabbedBrowser::home()
106
currentBrowser()->home();
109
HelpWindow *TabbedBrowser::currentBrowser() const
111
return static_cast<HelpWindow*>(ui.tab->currentWidget());
114
void TabbedBrowser::nextTab()
116
if(ui.tab->currentIndex()<=ui.tab->count()-1)
117
ui.tab->setCurrentIndex(ui.tab->currentIndex()+1);
120
void TabbedBrowser::previousTab()
122
int idx = ui.tab->currentIndex()-1;
124
ui.tab->setCurrentIndex(idx);
127
HelpWindow *TabbedBrowser::createHelpWindow(const QString &title)
129
MainWindow *mainWin = mainWindow();
130
HelpWindow *win = new HelpWindow(mainWin, 0);
131
win->setFrameStyle(QFrame::NoFrame);
132
win->setFont(browserFont());
133
win->setPalette(palette());
134
win->setSearchPaths(Config::configuration()->mimePaths());
135
ui.tab->addTab(win, reduceLabelLength(title));
136
connect(win, SIGNAL(highlighted(QString)),
137
(const QObject*) (mainWin->statusBar()), SLOT(showMessage(QString)));
138
connect(win, SIGNAL(chooseWebBrowser()), mainWin, SLOT(showWebBrowserSettings()));
139
connect(win, SIGNAL(choosePDFReader()), mainWin, SLOT(showPDFReaderSettings()));
140
connect(win, SIGNAL(backwardAvailable(bool)),
141
mainWin, SLOT(backwardAvailable(bool)));
142
connect(win, SIGNAL(forwardAvailable(bool)),
143
mainWin, SLOT(forwardAvailable(bool)));
144
connect(win, SIGNAL(sourceChanged(QUrl)), this, SLOT(sourceChanged()));
146
ui.tab->cornerWidget(Qt::TopRightCorner)->setEnabled(ui.tab->count() > 1);
150
HelpWindow *TabbedBrowser::newBackgroundTab(const QString &url)
152
HelpWindow *win = createHelpWindow(url);
156
void TabbedBrowser::newTab(const QString &lnk)
160
HelpWindow *w = currentBrowser();
162
link = w->source().toString();
164
HelpWindow *win = createHelpWindow(link);
165
ui.tab->setCurrentIndex(ui.tab->indexOf(win));
167
win->setSource(link);
171
void TabbedBrowser::zoomIn()
173
currentBrowser()->zoomIn();
176
void TabbedBrowser::zoomOut()
178
currentBrowser()->zoomOut();
181
void TabbedBrowser::init()
185
while(ui.tab->count()) {
186
QWidget *page = ui.tab->widget(0);
187
ui.tab->removeTab(0);
191
connect(ui.tab, SIGNAL(currentChanged(int)),
192
this, SLOT(transferFocus()));
194
QTabBar *tabBar = qFindChild<QTabBar*>(ui.tab);
198
opt.shape = tabBar->shape();
201
// workaround for sgi style
202
QPalette pal = palette();
203
pal.setColor(QPalette::Active, QPalette::Button, pal.color(QPalette::Active, QPalette::Background));
204
pal.setColor(QPalette::Disabled, QPalette::Button, pal.color(QPalette::Disabled, QPalette::Background));
205
pal.setColor(QPalette::Inactive, QPalette::Button, pal.color(QPalette::Inactive, QPalette::Background));
207
QToolButton *newTabButton = new QToolButton(this);
208
ui.tab->setCornerWidget(newTabButton, Qt::TopLeftCorner);
209
newTabButton->setCursor(Qt::ArrowCursor);
210
newTabButton->setAutoRaise(true);
211
newTabButton->setIcon(QPixmap(QString::fromUtf8(":/trolltech/assistant/images/addtab.png")));
212
QObject::connect(newTabButton, SIGNAL(clicked()), this, SLOT(newTab()));
213
newTabButton->setToolTip(tr("Add page"));
215
QToolButton *closeTabButton = new QToolButton(this);
216
closeTabButton->setPalette(pal);
217
ui.tab->setCornerWidget(closeTabButton, Qt::TopRightCorner);
218
closeTabButton->setCursor(Qt::ArrowCursor);
219
closeTabButton->setAutoRaise(true);
220
closeTabButton->setIcon(QIcon(QLatin1String(":/trolltech/assistant/images/closetab.png")));
221
QObject::connect(closeTabButton, SIGNAL(clicked()), this, SLOT(closeTab()));
222
closeTabButton->setToolTip(tr("Close page"));
223
closeTabButton->setEnabled(false);
226
void TabbedBrowser::updateTitle(const QString &title)
228
ui.tab->setTabText(ui.tab->indexOf(currentBrowser()), title);
231
void TabbedBrowser::newTab()
236
void TabbedBrowser::transferFocus()
238
if(currentBrowser()) {
239
currentBrowser()->setFocus();
241
mainWindow()->setWindowTitle(Config::configuration()->title()
242
+ QLatin1String(" - ")
243
+ currentBrowser()->documentTitle());
246
void TabbedBrowser::initHelpWindow(HelpWindow * /*win*/)
250
void TabbedBrowser::applySettings()
252
Config *config = Config::configuration();
253
fixedFontFam = config->fontFixedFamily();
254
lnkColor = QColor(config->linkColor());
255
underlineLnk = config->isLinkUnderline();
258
QFontInfo fntInfo(fnt);
259
fnt.setFamily(config->fontFamily());
260
if (config->fontSize() > 0)
261
fnt.setPointSize(config->fontSize());
264
int cnt = ui.tab->count();
265
for(int i=0; i<cnt; i++)
266
((HelpWindow*) ui.tab->widget(i))->updateFormat();
269
void TabbedBrowser::setup()
275
QFont TabbedBrowser::browserFont() const
280
void TabbedBrowser::setBrowserFont(const QFont &fnt)
285
int cnt = ui.tab->count();
286
for(int i=0; i<cnt; i++)
287
((QTextBrowser*) ui.tab->widget(i))->setFont(fnt);
290
void TabbedBrowser::copy()
292
currentBrowser()->copy();
295
void TabbedBrowser::closeTab()
297
if(ui.tab->count()==1)
299
HelpWindow *win = currentBrowser();
300
ui.tab->removeTab(ui.tab->indexOf(win));
301
QTimer::singleShot(0, win, SLOT(deleteLater()));
302
ui.tab->cornerWidget(Qt::TopRightCorner)->setEnabled(ui.tab->count() > 1);
305
QStringList TabbedBrowser::sources() const
308
int cnt = ui.tab->count();
309
for(int i=0; i<cnt; i++) {
310
lst.append(((QTextBrowser*) ui.tab->widget(i))->source().toString());
315
QList<HelpWindow*> TabbedBrowser::browsers() const
317
QList<HelpWindow*> list;
318
for (int i=0; i<ui.tab->count(); ++i) {
319
Q_ASSERT(::qobject_cast<HelpWindow*>(ui.tab->widget(i)));
320
list.append(static_cast<HelpWindow*>(ui.tab->widget(i)));
325
void TabbedBrowser::sourceChanged()
327
HelpWindow *win = ::qobject_cast<HelpWindow *>(QObject::sender());
329
QString docTitle(win->documentTitle());
330
if (docTitle.isEmpty())
331
docTitle = QLatin1String("...");
332
setTitle(win, docTitle);
335
void TabbedBrowser::setTitle(HelpWindow *win, const QString &title)
337
ui.tab->setTabText(ui.tab->indexOf(win), reduceLabelLength(title));
338
if (win == currentBrowser())
339
mainWindow()->setWindowTitle(Config::configuration()->title() + QLatin1String(" - ") + title);