Skip to content

Commit

Permalink
Support showing only the favicon for a tab. "faviconized" urls are re…
Browse files Browse the repository at this point in the history
…membered
  • Loading branch information
vipw committed Oct 3, 2009
1 parent 530ff55 commit db832c0
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 11 deletions.
18 changes: 18 additions & 0 deletions src/tabbar.cpp
Expand Up @@ -63,6 +63,7 @@
#include "tabbar.h"

#include "tabwidget.h"
#include "webview.h"

#include <qaction.h>
#include <qapplication.h>
Expand Down Expand Up @@ -178,6 +179,15 @@ void TabBar::contextMenuRequested(const QPoint &position)
action = menu.addAction(tr("Close &Other Tabs"),
this, SLOT(closeOtherTabs()));
action->setData(index);
menu.addSeparator();

QAction *showIconAction = new QAction(tr("Show Favicon Only"), &menu);
showIconAction->setCheckable(true);
showIconAction->setData(index);
showIconAction->setChecked(tabWidget->webView(index)->showIconOnly());
menu.addAction(showIconAction);
connect(showIconAction, SIGNAL(toggled(bool)),
this, SLOT(showIconOnly(bool)));

menu.addSeparator();

Expand Down Expand Up @@ -307,6 +317,14 @@ void TabBar::dropEvent(QDropEvent *event)
QTabBar::dropEvent(event);
}

void TabBar::showIconOnly(bool show)
{
if (QAction *action = qobject_cast<QAction*>(sender())) {
int index = action->data().toInt();
emit showIconOnly(index, show);
}
}

QSize TabBar::tabSizeHint(int index) const
{
QSize sizeHint = QTabBar::tabSizeHint(index);
Expand Down
2 changes: 2 additions & 0 deletions src/tabbar.h
Expand Up @@ -82,6 +82,7 @@ class TabBar : public QTabBar
void reloadTab(int index);
void reloadAllTabs();
void loadUrl(const QUrl &url, TabWidget::OpenUrlIn tab);
void showIconOnly(int index, bool show);

public:
TabBar(QWidget *parent = 0);
Expand Down Expand Up @@ -111,6 +112,7 @@ private slots:
void contextMenuRequested(const QPoint &position);
void updateViewToolBarAction();
void viewTabBar();
void showIconOnly(bool show);

private:
void updateVisibility();
Expand Down
84 changes: 74 additions & 10 deletions src/tabwidget.cpp
@@ -1,5 +1,5 @@
/*
* Copyright 2008 Benjamin C. Meyer <ben@meyerhome.net>
* Copyright 2008-2009 Benjamin C. Meyer <ben@meyerhome.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -125,6 +125,8 @@ TabWidget::TabWidget(QWidget *parent)
connect(m_tabBar, SIGNAL(closeOtherTabs(int)), this, SLOT(closeOtherTabs(int)));
connect(m_tabBar, SIGNAL(reloadTab(int)), this, SLOT(reloadTab(int)));
connect(m_tabBar, SIGNAL(reloadAllTabs()), this, SLOT(reloadAllTabs()));
connect(m_tabBar, SIGNAL(showIconOnly(int, bool)),
this, SLOT(showIconOnly(int, bool)));
setTabBar(m_tabBar);
setDocumentMode(true);
connect(m_tabBar, SIGNAL(tabMoved(int, int)),
Expand Down Expand Up @@ -208,6 +210,16 @@ TabWidget::TabWidget(QWidget *parent)
connect(BrowserApplication::historyManager(), SIGNAL(historyCleared()),
this, SLOT(historyCleared()));

settings.endGroup();

// Read the urls that should only have an icon displayed in the tab.
int size = settings.beginReadArray(QLatin1String("iconOnlyUrls"));
for (int i = 0; i < size; ++i) {
settings.setArrayIndex(i);
m_iconOnlyUrls.append(settings.value(QLatin1String("url")).toString());
}
settings.endArray();

// Initialize Actions' labels
retranslate();
}
Expand Down Expand Up @@ -556,6 +568,35 @@ void TabWidget::bookmarkTabs()
}
}

void TabWidget::showIconOnly(int index, bool show)
{
WebView *tab = webView(index);
tab->setShowIconOnly(show);

if (show) {
setTitle(tab, QString());
m_iconOnlyUrls.append(tab->url());
} else {
setTitle(tab, tab->title());
m_iconOnlyUrls.removeAll(tab->url());
}

saveIconOnlyUrls();
}

void TabWidget::saveIconOnlyUrls()
{
// Save the "faviconized" urls.
QSettings settings;
settings.beginWriteArray(QLatin1String("iconOnlyUrls"));
for (int i = 0; i < m_iconOnlyUrls.size(); ++i) {
settings.setArrayIndex(i);
settings.setValue(QLatin1String("url"),
m_iconOnlyUrls.at(i).toString());
}
settings.endArray();
}

void TabWidget::lineEditReturnPressed()
{
if (QLineEdit *lineEdit = qobject_cast<QLineEdit*>(sender())) {
Expand Down Expand Up @@ -752,21 +793,36 @@ void TabWidget::webViewIconChanged()
}
}

void TabWidget::webViewTitleChanged(const QString &title)
void TabWidget::setTitle(WebView *webView, const QString& title)
{
WebView *webView = qobject_cast<WebView*>(sender());
QString tabTitle;

if (! webView->showIconOnly()) {
tabTitle = title;
if (title.isEmpty())
tabTitle = QString::fromUtf8(webView->url().toEncoded());
tabTitle.replace(QLatin1Char('&'), QLatin1String("&&"));
}

int index = webViewIndex(webView);
if (-1 == index)
return;
QString tabTitle = title;
if (title.isEmpty())
tabTitle = QString::fromUtf8(webView->url().toEncoded());
tabTitle.replace(QLatin1Char('&'), QLatin1String("&&"));

setTabText(index, tabTitle);
setTabToolTip(index, tabTitle);

if (currentIndex() == index)
emit setCurrentTitle(title);
BrowserApplication::historyManager()->updateHistoryEntry(webView->url(), title);
BrowserApplication::historyManager()->updateHistoryEntry(webView->url(),
title);
}

void TabWidget::webViewTitleChanged(const QString &title)
{
WebView *webView = qobject_cast<WebView*>(sender());
int index = webViewIndex(webView);
if (-1 == index)
return;

setTitle(webView, title);
}

void TabWidget::webViewUrlChanged(const QUrl &url)
Expand All @@ -775,6 +831,14 @@ void TabWidget::webViewUrlChanged(const QUrl &url)
int index = webViewIndex(webView);
if (-1 == index)
return;

QUrl element;
foreach (element, m_iconOnlyUrls) {
if (element == url)
webView->setShowIconOnly(true);
}


m_tabBar->setTabData(index, url);
emit tabsChanged();
}
Expand Down
6 changes: 5 additions & 1 deletion src/tabwidget.h
@@ -1,5 +1,5 @@
/*
* Copyright 2008 Benjamin C. Meyer <ben@meyerhome.net>
* Copyright 2008-2009 Benjamin C. Meyer <ben@meyerhome.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -164,6 +164,7 @@ public slots:
void nextTab();
void previousTab();
void bookmarkTabs();
void showIconOnly(int index, bool show);

private slots:
void currentChanged(int index);
Expand All @@ -189,6 +190,8 @@ private slots:
static QUrl guessUrlFromString(const QString &url);
QLabel *animationLabel(int index, bool addMovie);
void retranslate();
void setTitle(WebView *webView, const QString& title);
void saveIconOnlyUrls();

QAction *m_recentlyClosedTabsAction;
QAction *m_newTabAction;
Expand All @@ -200,6 +203,7 @@ private slots:
QMenu *m_recentlyClosedTabsMenu;
static const int m_recentlyClosedTabsSize = 10;
QList<QUrl> m_recentlyClosedTabs;
QList<QUrl> m_iconOnlyUrls;
QList<QByteArray> m_recentlyClosedTabsHistory;
QList<WebActionMapper*> m_actions;
bool m_swappedDelayedWidget;
Expand Down
1 change: 1 addition & 0 deletions src/webview.cpp
Expand Up @@ -103,6 +103,7 @@ WebView::WebView(QWidget *parent)
, m_progress(0)
, m_currentZoom(100)
, m_page(new WebPage(this))
, m_showIconOnly(false)
#if QT_VERSION >= 0x040600 || defined(WEBKIT_TRUNK)
, m_enableAccessKeys(true)
, m_accessKeysPressed(false)
Expand Down
3 changes: 3 additions & 0 deletions src/webview.h
Expand Up @@ -100,6 +100,8 @@ class WebView : public QWebView
QString lastStatusBarText() const;
inline int progress() const { return m_progress; }
TabWidget *tabWidget() const;
inline void setShowIconOnly(bool show) { m_showIconOnly = show; }
inline bool showIconOnly() { return m_showIconOnly; }

signals:
void search(const QUrl &searchUrl, TabWidget::OpenUrlIn openIn);
Expand Down Expand Up @@ -153,6 +155,7 @@ private slots:
int m_currentZoom;
QList<int> m_zoomLevels;
WebPage *m_page;
bool m_showIconOnly;

#if QT_VERSION >= 0x040600 || defined(WEBKIT_TRUNK)
bool m_enableAccessKeys;
Expand Down

0 comments on commit db832c0

Please sign in to comment.