source: trunk/tools/assistant/compat/tabbedbrowser.cpp@ 280

Last change on this file since 280 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 15.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the Qt Assistant of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "tabbedbrowser.h"
43#include "mainwindow.h"
44#include "helpwindow.h"
45#include "config.h"
46
47#include <QStyleOptionTab>
48#include <QToolTip>
49#include <QFileInfo>
50#include <QToolButton>
51#include <QPixmap>
52#include <QIcon>
53#include <QStyle>
54#include <QTimer>
55#include <QStackedWidget>
56#include <QTimer>
57#include <QTextBlock>
58#include <QKeyEvent>
59
60QT_BEGIN_NAMESPACE
61
62#ifdef Q_WS_MAC
63const QLatin1String ImageLocation(":trolltech/assistant/images/mac/");
64#else
65const QLatin1String ImageLocation(":trolltech/assistant/images/win/");
66#endif
67
68TabbedBrowser::TabbedBrowser(MainWindow *parent)
69 : QWidget(parent)
70{
71 ui.setupUi(this);
72 init();
73
74 QStackedWidget *stack = qFindChild<QStackedWidget*>(ui.tab);
75 Q_ASSERT(stack);
76 stack->setContentsMargins(0, 0, 0, 0);
77 connect(stack, SIGNAL(currentChanged(int)), parent, SLOT(browserTabChanged()));
78
79 QPalette p = palette();
80 p.setColor(QPalette::Inactive, QPalette::Highlight,
81 p.color(QPalette::Active, QPalette::Highlight));
82 p.setColor(QPalette::Inactive, QPalette::HighlightedText,
83 p.color(QPalette::Active, QPalette::HighlightedText));
84 setPalette(p);
85}
86
87TabbedBrowser::~TabbedBrowser()
88{
89}
90
91MainWindow *TabbedBrowser::mainWindow() const
92{
93 return static_cast<MainWindow*>(parentWidget());
94}
95
96void TabbedBrowser::forward()
97{
98 currentBrowser()->forward();
99 emit browserUrlChanged(currentBrowser()->source().toString());
100}
101
102void TabbedBrowser::backward()
103{
104 currentBrowser()->backward();
105 emit browserUrlChanged(currentBrowser()->source().toString());
106}
107
108void TabbedBrowser::setSource( const QString &ref )
109{
110 HelpWindow * win = currentBrowser();
111 win->setSource(ref);
112}
113
114void TabbedBrowser::reload()
115{
116 currentBrowser()->reload();
117}
118
119void TabbedBrowser::home()
120{
121 currentBrowser()->home();
122}
123
124HelpWindow *TabbedBrowser::currentBrowser() const
125{
126 return static_cast<HelpWindow*>(ui.tab->currentWidget());
127}
128
129void TabbedBrowser::nextTab()
130{
131 if(ui.tab->currentIndex()<=ui.tab->count()-1)
132 ui.tab->setCurrentIndex(ui.tab->currentIndex()+1);
133}
134
135void TabbedBrowser::previousTab()
136{
137 int idx = ui.tab->currentIndex()-1;
138 if(idx>=0)
139 ui.tab->setCurrentIndex(idx);
140}
141
142HelpWindow *TabbedBrowser::createHelpWindow()
143{
144 MainWindow *mainWin = mainWindow();
145 HelpWindow *win = new HelpWindow(mainWin, 0);
146 win->setFrameStyle(QFrame::NoFrame);
147 win->setPalette(palette());
148 win->setSearchPaths(Config::configuration()->mimePaths());
149 ui.tab->addTab(win, tr("..."));
150 connect(win, SIGNAL(highlighted(QString)),
151 (const QObject*) (mainWin->statusBar()), SLOT(showMessage(QString)));
152 connect(win, SIGNAL(backwardAvailable(bool)),
153 mainWin, SLOT(backwardAvailable(bool)));
154 connect(win, SIGNAL(forwardAvailable(bool)),
155 mainWin, SLOT(forwardAvailable(bool)));
156 connect(win, SIGNAL(sourceChanged(QUrl)), this, SLOT(sourceChanged()));
157
158 ui.tab->cornerWidget(Qt::TopRightCorner)->setEnabled(ui.tab->count() > 1);
159 win->installEventFilter(this);
160 win->viewport()->installEventFilter(this);
161 ui.editFind->installEventFilter(this);
162 return win;
163}
164
165HelpWindow *TabbedBrowser::newBackgroundTab()
166{
167 HelpWindow *win = createHelpWindow();
168 emit tabCountChanged(ui.tab->count());
169 return win;
170}
171
172void TabbedBrowser::newTab(const QString &lnk)
173{
174 QString link(lnk);
175 if(link.isNull()) {
176 HelpWindow *w = currentBrowser();
177 if(w)
178 link = w->source().toString();
179 }
180 HelpWindow *win = createHelpWindow();
181 ui.tab->setCurrentIndex(ui.tab->indexOf(win));
182 if(!link.isNull()) {
183 win->setSource(link);
184 }
185
186 emit tabCountChanged(ui.tab->count());
187}
188
189void TabbedBrowser::zoomIn()
190{
191 currentBrowser()->zoomIn();
192 Config::configuration()->setFontPointSize(currentBrowser()->font().pointSizeF());
193}
194
195void TabbedBrowser::zoomOut()
196{
197 currentBrowser()->zoomOut();
198 Config::configuration()->setFontPointSize(currentBrowser()->font().pointSizeF());
199}
200
201void TabbedBrowser::init()
202{
203
204 lastCurrentTab = 0;
205 while(ui.tab->count()) {
206 QWidget *page = ui.tab->widget(0);
207 ui.tab->removeTab(0);
208 delete page;
209 }
210
211 connect(ui.tab, SIGNAL(currentChanged(int)),
212 this, SLOT(transferFocus()));
213
214 QTabBar *tabBar = qFindChild<QTabBar*>(ui.tab);
215 QStyleOptionTab opt;
216 if (tabBar) {
217 opt.init(tabBar);
218 opt.shape = tabBar->shape();
219 tabBar->setContextMenuPolicy(Qt::CustomContextMenu);
220 connect(tabBar, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(openTabMenu(const QPoint&)));
221 }
222
223 // workaround for sgi style
224 QPalette pal = palette();
225 pal.setColor(QPalette::Active, QPalette::Button, pal.color(QPalette::Active, QPalette::Window));
226 pal.setColor(QPalette::Disabled, QPalette::Button, pal.color(QPalette::Disabled, QPalette::Window));
227 pal.setColor(QPalette::Inactive, QPalette::Button, pal.color(QPalette::Inactive, QPalette::Window));
228
229 QToolButton *newTabButton = new QToolButton(this);
230 ui.tab->setCornerWidget(newTabButton, Qt::TopLeftCorner);
231 newTabButton->setCursor(Qt::ArrowCursor);
232 newTabButton->setAutoRaise(true);
233 newTabButton->setIcon(QIcon(ImageLocation + QLatin1String("addtab.png")));
234 QObject::connect(newTabButton, SIGNAL(clicked()), this, SLOT(newTab()));
235 newTabButton->setToolTip(tr("Add page"));
236
237 QToolButton *closeTabButton = new QToolButton(this);
238 closeTabButton->setPalette(pal);
239 ui.tab->setCornerWidget(closeTabButton, Qt::TopRightCorner);
240 closeTabButton->setCursor(Qt::ArrowCursor);
241 closeTabButton->setAutoRaise(true);
242 closeTabButton->setIcon(QIcon(ImageLocation + QLatin1String("closetab.png")));
243 QObject::connect(closeTabButton, SIGNAL(clicked()), this, SLOT(closeTab()));
244 closeTabButton->setToolTip(tr("Close page"));
245 closeTabButton->setEnabled(false);
246
247 QObject::connect(ui.toolClose, SIGNAL(clicked()), ui.frameFind, SLOT(hide()));
248 QObject::connect(ui.toolPrevious, SIGNAL(clicked()), this, SLOT(findPrevious()));
249 QObject::connect(ui.toolNext, SIGNAL(clicked()), this, SLOT(findNext()));
250 QObject::connect(ui.editFind, SIGNAL(returnPressed()), this, SLOT(findNext()));
251 QObject::connect(ui.editFind, SIGNAL(textEdited(const QString&)),
252 this, SLOT(find(QString)));
253 ui.frameFind->setVisible(false);
254 ui.labelWrapped->setVisible(false);
255 autoHideTimer = new QTimer(this);
256 autoHideTimer->setInterval(5000);
257 autoHideTimer->setSingleShot(true);
258 QObject::connect(autoHideTimer, SIGNAL(timeout()), ui.frameFind, SLOT(hide()));
259}
260
261void TabbedBrowser::updateTitle(const QString &title)
262{
263 ui.tab->setTabText(ui.tab->indexOf(currentBrowser()), title.trimmed());
264}
265
266void TabbedBrowser::newTab()
267{
268 newTab(QString());
269}
270
271void TabbedBrowser::transferFocus()
272{
273 if(currentBrowser()) {
274 currentBrowser()->setFocus();
275 }
276 mainWindow()->setWindowTitle(Config::configuration()->title()
277 + QLatin1String(" - ")
278 + currentBrowser()->documentTitle());
279}
280
281void TabbedBrowser::initHelpWindow(HelpWindow * /*win*/)
282{
283}
284
285void TabbedBrowser::setup()
286{
287 newTab(QString());
288}
289
290void TabbedBrowser::copy()
291{
292 currentBrowser()->copy();
293}
294
295void TabbedBrowser::closeTab()
296{
297 if(ui.tab->count()==1)
298 return;
299 HelpWindow *win = currentBrowser();
300 mainWindow()->removePendingBrowser(win);
301 ui.tab->removeTab(ui.tab->indexOf(win));
302 QTimer::singleShot(0, win, SLOT(deleteLater()));
303 ui.tab->cornerWidget(Qt::TopRightCorner)->setEnabled(ui.tab->count() > 1);
304 emit tabCountChanged(ui.tab->count());
305}
306
307QStringList TabbedBrowser::sources() const
308{
309 QStringList lst;
310 int cnt = ui.tab->count();
311 for(int i=0; i<cnt; i++) {
312 lst.append(((QTextBrowser*) ui.tab->widget(i))->source().toString());
313 }
314 return lst;
315}
316
317QList<HelpWindow*> TabbedBrowser::browsers() const
318{
319 QList<HelpWindow*> list;
320 for (int i=0; i<ui.tab->count(); ++i) {
321 Q_ASSERT(qobject_cast<HelpWindow*>(ui.tab->widget(i)));
322 list.append(static_cast<HelpWindow*>(ui.tab->widget(i)));
323 }
324 return list;
325}
326
327void TabbedBrowser::sourceChanged()
328{
329 HelpWindow *win = qobject_cast<HelpWindow *>(QObject::sender());
330 Q_ASSERT(win);
331 QString docTitle(win->documentTitle());
332 if (docTitle.isEmpty())
333 docTitle = QLatin1String("...");
334 // Make the classname in the title a bit more visible (otherwise
335 // we just see the "Qt 4.0 : Q..." which isn't really helpful ;-)
336 QString qtTitle = QLatin1String("Qt ") + QString::number( (QT_VERSION >> 16) & 0xff )
337 + QLatin1String(".") + QString::number( (QT_VERSION >> 8) & 0xff )
338 + QLatin1String(": ");
339 if (docTitle.startsWith(qtTitle))
340 docTitle = docTitle.mid(qtTitle.length());
341 setTitle(win, docTitle);
342 ui.frameFind->hide();
343 ui.labelWrapped->hide();
344 win->setTextCursor(win->cursorForPosition(QPoint(0, 0)));
345}
346
347void TabbedBrowser::setTitle(HelpWindow *win, const QString &title)
348{
349 const QString tt = title.trimmed();
350 ui.tab->setTabText(ui.tab->indexOf(win), tt);
351 if (win == currentBrowser())
352 mainWindow()->setWindowTitle(Config::configuration()->title() + QLatin1String(" - ") + tt);
353}
354
355void TabbedBrowser::keyPressEvent(QKeyEvent *e)
356{
357 int key = e->key();
358 QString ttf = ui.editFind->text();
359 QString text = e->text();
360
361 if (ui.frameFind->isVisible()) {
362 switch (key) {
363 case Qt::Key_Escape:
364 ui.frameFind->hide();
365 ui.labelWrapped->hide();
366 return;
367 case Qt::Key_Backspace:
368 ttf.chop(1);
369 break;
370 case Qt::Key_Return:
371 case Qt::Key_Enter:
372 // Return/Enter key events are not accepted by QLineEdit
373 return;
374 default:
375 if (text.isEmpty()) {
376 QWidget::keyPressEvent(e);
377 return;
378 }
379 ttf += text;
380 }
381 } else {
382 if (text.isEmpty() || text[0].isSpace() || !text[0].isPrint()) {
383 QWidget::keyPressEvent(e);
384 return;
385 }
386 if (text.startsWith(QLatin1Char('/'))) {
387 ui.editFind->clear();
388 find();
389 return;
390 }
391 ttf = text;
392 ui.frameFind->show();
393 }
394
395 ui.editFind->setText(ttf);
396 find(ttf, false, false);
397}
398
399void TabbedBrowser::findNext()
400{
401 find(ui.editFind->text(), true, false);
402}
403
404void TabbedBrowser::findPrevious()
405{
406 find(ui.editFind->text(), false, true);
407}
408
409void TabbedBrowser::find()
410{
411 ui.frameFind->show();
412 ui.editFind->setFocus(Qt::ShortcutFocusReason);
413 ui.editFind->selectAll();
414 autoHideTimer->stop();
415}
416
417void TabbedBrowser::find(QString ttf, bool forward, bool backward)
418{
419 HelpWindow *browser = currentBrowser();
420 QTextDocument *doc = browser->document();
421 QString oldText = ui.editFind->text();
422 QTextCursor c = browser->textCursor();
423 QTextDocument::FindFlags options;
424 QPalette p = ui.editFind->palette();
425 p.setColor(QPalette::Active, QPalette::Base, Qt::white);
426
427 if (c.hasSelection())
428 c.setPosition(forward ? c.position() : c.anchor(), QTextCursor::MoveAnchor);
429
430 QTextCursor newCursor = c;
431
432 if (!ttf.isEmpty()) {
433 if (backward)
434 options |= QTextDocument::FindBackward;
435
436 if (ui.checkCase->isChecked())
437 options |= QTextDocument::FindCaseSensitively;
438
439 if (ui.checkWholeWords->isChecked())
440 options |= QTextDocument::FindWholeWords;
441
442 newCursor = doc->find(ttf, c, options);
443 ui.labelWrapped->hide();
444
445 if (newCursor.isNull()) {
446 QTextCursor ac(doc);
447 ac.movePosition(options & QTextDocument::FindBackward
448 ? QTextCursor::End : QTextCursor::Start);
449 newCursor = doc->find(ttf, ac, options);
450 if (newCursor.isNull()) {
451 p.setColor(QPalette::Active, QPalette::Base, QColor(255, 102, 102));
452 newCursor = c;
453 } else
454 ui.labelWrapped->show();
455 }
456 }
457
458 if (!ui.frameFind->isVisible())
459 ui.frameFind->show();
460 browser->setTextCursor(newCursor);
461 ui.editFind->setPalette(p);
462 if (!ui.editFind->hasFocus())
463 autoHideTimer->start();
464}
465
466bool TabbedBrowser::eventFilter(QObject *o, QEvent *e)
467{
468 if (o == ui.editFind) {
469 if (e->type() == QEvent::FocusIn && autoHideTimer->isActive())
470 autoHideTimer->stop();
471 } else if (e->type() == QEvent::KeyPress && ui.frameFind->isVisible()) { // assume textbrowser
472 QKeyEvent *ke = static_cast<QKeyEvent *>(e);
473 if (ke->key() == Qt::Key_Space) {
474 keyPressEvent(ke);
475 return true;
476 }
477 }
478
479 return QWidget::eventFilter(o, e);
480}
481
482void TabbedBrowser::openTabMenu(const QPoint& pos)
483{
484 QTabBar *tabBar = qFindChild<QTabBar*>(ui.tab);
485
486 QMenu m(QLatin1String(""), tabBar);
487 QAction *new_action = m.addAction(tr("New Tab"));
488 QAction *close_action = m.addAction(tr("Close Tab"));
489 QAction *close_others_action = m.addAction(tr("Close Other Tabs"));
490
491 if (tabBar->count() == 1) {
492 close_action->setEnabled(false);
493 close_others_action->setEnabled(false);
494 }
495
496 QAction *action_picked = m.exec(tabBar->mapToGlobal(pos));
497 if (!action_picked)
498 return;
499
500 if (action_picked == new_action) {
501 newTab();
502 return;
503 }
504
505 QList<HelpWindow*> windowList = browsers();
506 for (int i = 0; i < tabBar->count(); ++i) {
507 if (tabBar->tabRect(i).contains(pos)) {
508 HelpWindow *win = static_cast<HelpWindow*>(ui.tab->widget(i));
509 if (action_picked == close_action) {
510 mainWindow()->removePendingBrowser(win);
511 QTimer::singleShot(0, win, SLOT(deleteLater()));
512 }
513 windowList.removeOne(win);
514 break;
515 }
516 }
517
518 if (action_picked == close_others_action) {
519 foreach (HelpWindow* win, windowList) {
520 mainWindow()->removePendingBrowser(win);
521 QTimer::singleShot(0, win, SLOT(deleteLater()));
522 windowList.removeOne(win);
523 }
524 }
525
526 ui.tab->cornerWidget(Qt::TopRightCorner)->setEnabled(windowList.count() > 1);
527 emit tabCountChanged(windowList.count());
528}
529
530QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.