~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to tools/assistant/tabbedbrowser.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the assistant application of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "tabbedbrowser.h"
 
30
#include "mainwindow.h"
 
31
#include "helpwindow.h"
 
32
#include "config.h"
 
33
 
 
34
#include <QtGui/QStyleOptionTab>
 
35
#include <qtooltip.h>
 
36
#include <qfileinfo.h>
 
37
#include <qtoolbutton.h>
 
38
#include <qpixmap.h>
 
39
#include <qicon.h>
 
40
#include <qstyle.h>
 
41
#include <qtimer.h>
 
42
#include <qstackedwidget.h>
 
43
 
 
44
TabbedBrowser::TabbedBrowser(MainWindow *parent)
 
45
    : QWidget(parent)
 
46
{
 
47
    ui.setupUi(this);
 
48
    init();
 
49
 
 
50
    QStackedWidget *stack = qFindChild<QStackedWidget*>(ui.tab);
 
51
    Q_ASSERT(stack);
 
52
    stack->setContentsMargins(0, 0, 0, 0);
 
53
    connect(stack, SIGNAL(currentChanged(int)), parent, SLOT(browserTabChanged()));
 
54
 
 
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));
 
60
    setPalette(p);
 
61
}
 
62
 
 
63
TabbedBrowser::~TabbedBrowser()
 
64
{
 
65
}
 
66
 
 
67
MainWindow *TabbedBrowser::mainWindow() const
 
68
{
 
69
    return static_cast<MainWindow*>(parentWidget());
 
70
}
 
71
 
 
72
static QString reduceLabelLength( const QString &s )
 
73
{
 
74
    int maxLength = 16;
 
75
    QString str = s;
 
76
    if ( str.length() < maxLength )
 
77
        return str;
 
78
    str = str.left( maxLength - 3 );
 
79
    str += QLatin1String("...");
 
80
    return str;
 
81
}
 
82
 
 
83
void TabbedBrowser::forward()
 
84
{
 
85
    currentBrowser()->forward();
 
86
}
 
87
 
 
88
void TabbedBrowser::backward()
 
89
{
 
90
    currentBrowser()->backward();
 
91
}
 
92
 
 
93
void TabbedBrowser::setSource( const QString &ref )
 
94
{
 
95
    HelpWindow * win = currentBrowser();
 
96
    win->setSource(ref);
 
97
}
 
98
 
 
99
void TabbedBrowser::reload()
 
100
{
 
101
    currentBrowser()->reload();
 
102
}
 
103
 
 
104
void TabbedBrowser::home()
 
105
{
 
106
    currentBrowser()->home();
 
107
}
 
108
 
 
109
HelpWindow *TabbedBrowser::currentBrowser() const
 
110
{
 
111
    return static_cast<HelpWindow*>(ui.tab->currentWidget());
 
112
}
 
113
 
 
114
void TabbedBrowser::nextTab()
 
115
{
 
116
    if(ui.tab->currentIndex()<=ui.tab->count()-1)
 
117
        ui.tab->setCurrentIndex(ui.tab->currentIndex()+1);
 
118
}
 
119
 
 
120
void TabbedBrowser::previousTab()
 
121
{
 
122
    int idx = ui.tab->currentIndex()-1;
 
123
    if(idx>=0)
 
124
        ui.tab->setCurrentIndex(idx);
 
125
}
 
126
 
 
127
HelpWindow *TabbedBrowser::createHelpWindow(const QString &title)
 
128
{
 
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()));
 
145
 
 
146
    ui.tab->cornerWidget(Qt::TopRightCorner)->setEnabled(ui.tab->count() > 1);
 
147
    return win;
 
148
}
 
149
 
 
150
HelpWindow *TabbedBrowser::newBackgroundTab(const QString &url)
 
151
{
 
152
    HelpWindow *win = createHelpWindow(url);
 
153
    return win;
 
154
}
 
155
 
 
156
void TabbedBrowser::newTab(const QString &lnk)
 
157
{
 
158
    QString link(lnk);
 
159
    if(link.isNull()) {
 
160
        HelpWindow *w = currentBrowser();
 
161
        if(w)
 
162
            link = w->source().toString();
 
163
    }
 
164
    HelpWindow *win = createHelpWindow(link);
 
165
    ui.tab->setCurrentIndex(ui.tab->indexOf(win));
 
166
    if(!link.isNull()) {
 
167
         win->setSource(link);
 
168
    }
 
169
}
 
170
 
 
171
void TabbedBrowser::zoomIn()
 
172
{
 
173
    currentBrowser()->zoomIn();
 
174
}
 
175
 
 
176
void TabbedBrowser::zoomOut()
 
177
{
 
178
    currentBrowser()->zoomOut();
 
179
}
 
180
 
 
181
void TabbedBrowser::init()
 
182
{
 
183
 
 
184
    lastCurrentTab = 0;
 
185
    while(ui.tab->count()) {
 
186
        QWidget *page = ui.tab->widget(0);
 
187
        ui.tab->removeTab(0);
 
188
        delete page;
 
189
    }
 
190
 
 
191
    connect(ui.tab, SIGNAL(currentChanged(int)),
 
192
             this, SLOT(transferFocus()));
 
193
 
 
194
    QTabBar *tabBar = qFindChild<QTabBar*>(ui.tab);
 
195
    QStyleOptionTab opt;
 
196
    if (tabBar) {
 
197
        opt.init(tabBar);
 
198
        opt.shape = tabBar->shape();
 
199
    }
 
200
 
 
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));
 
206
 
 
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"));
 
214
 
 
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);
 
224
}
 
225
 
 
226
void TabbedBrowser::updateTitle(const QString &title)
 
227
{
 
228
    ui.tab->setTabText(ui.tab->indexOf(currentBrowser()), title);
 
229
}
 
230
 
 
231
void TabbedBrowser::newTab()
 
232
{
 
233
    newTab(QString());
 
234
}
 
235
 
 
236
void TabbedBrowser::transferFocus()
 
237
{
 
238
    if(currentBrowser()) {
 
239
        currentBrowser()->setFocus();
 
240
    }
 
241
    mainWindow()->setWindowTitle(Config::configuration()->title()
 
242
                             + QLatin1String(" - ")
 
243
                             + currentBrowser()->documentTitle());
 
244
}
 
245
 
 
246
void TabbedBrowser::initHelpWindow(HelpWindow * /*win*/)
 
247
{
 
248
}
 
249
 
 
250
void TabbedBrowser::applySettings()
 
251
{
 
252
    Config *config = Config::configuration();
 
253
    fixedFontFam = config->fontFixedFamily();
 
254
    lnkColor = QColor(config->linkColor());
 
255
    underlineLnk = config->isLinkUnderline();
 
256
 
 
257
    QFont fnt(font());
 
258
    QFontInfo fntInfo(fnt);
 
259
    fnt.setFamily(config->fontFamily());
 
260
    if (config->fontSize() > 0)
 
261
        fnt.setPointSize(config->fontSize());
 
262
    setBrowserFont(fnt);
 
263
 
 
264
    int cnt = ui.tab->count();
 
265
    for(int i=0; i<cnt; i++)
 
266
        ((HelpWindow*) ui.tab->widget(i))->updateFormat();
 
267
}
 
268
 
 
269
void TabbedBrowser::setup()
 
270
{
 
271
    applySettings();
 
272
    newTab(QString());
 
273
}
 
274
 
 
275
QFont TabbedBrowser::browserFont() const
 
276
{
 
277
    return tabFont;
 
278
}
 
279
 
 
280
void TabbedBrowser::setBrowserFont(const QFont &fnt)
 
281
{
 
282
    if(tabFont == fnt)
 
283
        return;
 
284
    tabFont = fnt;
 
285
    int cnt = ui.tab->count();
 
286
    for(int i=0; i<cnt; i++)
 
287
        ((QTextBrowser*) ui.tab->widget(i))->setFont(fnt);
 
288
}
 
289
 
 
290
void TabbedBrowser::copy()
 
291
{
 
292
    currentBrowser()->copy();
 
293
}
 
294
 
 
295
void TabbedBrowser::closeTab()
 
296
{
 
297
    if(ui.tab->count()==1)
 
298
        return;
 
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);
 
303
}
 
304
 
 
305
QStringList TabbedBrowser::sources() const
 
306
{
 
307
    QStringList lst;
 
308
    int cnt = ui.tab->count();
 
309
    for(int i=0; i<cnt; i++) {
 
310
        lst.append(((QTextBrowser*) ui.tab->widget(i))->source().toString());
 
311
    }
 
312
    return lst;
 
313
}
 
314
 
 
315
QList<HelpWindow*> TabbedBrowser::browsers() const
 
316
{
 
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)));
 
321
    }
 
322
    return list;
 
323
}
 
324
 
 
325
void TabbedBrowser::sourceChanged()
 
326
{
 
327
    HelpWindow *win = ::qobject_cast<HelpWindow *>(QObject::sender());
 
328
    Q_ASSERT(win);
 
329
    QString docTitle(win->documentTitle());
 
330
    if (docTitle.isEmpty())
 
331
        docTitle = QLatin1String("...");
 
332
    setTitle(win, docTitle);
 
333
}
 
334
 
 
335
void TabbedBrowser::setTitle(HelpWindow *win, const QString &title)
 
336
{
 
337
    ui.tab->setTabText(ui.tab->indexOf(win), reduceLabelLength(title));
 
338
    if (win == currentBrowser())
 
339
        mainWindow()->setWindowTitle(Config::configuration()->title() + QLatin1String(" - ") + title);
 
340
}