1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

TabBar: If not enough space, show close button only on current tab

- removed "Show close button on tabs" option as it is now redundant
This commit is contained in:
nowrep 2012-09-03 22:40:52 +02:00
parent 0c6abb8afc
commit c71cf910cb
7 changed files with 188 additions and 73 deletions

View File

@ -174,7 +174,6 @@ Preferences::Preferences(QupZilla* mainClass, QWidget* parent)
ui->closedInsteadOpened->setChecked(settings.value("closedInsteadOpenedTabs", false).toBool()); ui->closedInsteadOpened->setChecked(settings.value("closedInsteadOpenedTabs", false).toBool());
ui->showTabPreviews->setChecked(settings.value("showTabPreviews", true).toBool()); ui->showTabPreviews->setChecked(settings.value("showTabPreviews", true).toBool());
ui->animatedTabPreviews->setChecked(settings.value("tabPreviewAnimationsEnabled", true).toBool()); ui->animatedTabPreviews->setChecked(settings.value("tabPreviewAnimationsEnabled", true).toBool());
ui->showCloseButtonOnTabs->setChecked(settings.value("showCloseButtonOnTabs", true).toBool());
settings.endGroup(); settings.endGroup();
connect(ui->showTabPreviews, SIGNAL(toggled(bool)), this, SLOT(showTabPreviewsChanged(bool))); connect(ui->showTabPreviews, SIGNAL(toggled(bool)), this, SLOT(showTabPreviewsChanged(bool)));
@ -781,7 +780,6 @@ void Preferences::saveSettings()
settings.setValue("closedInsteadOpenedTabs", ui->closedInsteadOpened->isChecked()); settings.setValue("closedInsteadOpenedTabs", ui->closedInsteadOpened->isChecked());
settings.setValue("showTabPreviews", ui->showTabPreviews->isChecked()); settings.setValue("showTabPreviews", ui->showTabPreviews->isChecked());
settings.setValue("tabPreviewAnimationsEnabled", ui->animatedTabPreviews->isChecked()); settings.setValue("tabPreviewAnimationsEnabled", ui->animatedTabPreviews->isChecked());
settings.setValue("showCloseButtonOnTabs", ui->showCloseButtonOnTabs->isChecked());
settings.endGroup(); settings.endGroup();
//DOWNLOADS //DOWNLOADS

View File

@ -660,13 +660,6 @@
</item> </item>
</layout> </layout>
</item> </item>
<item>
<widget class="QCheckBox" name="showCloseButtonOnTabs">
<property name="text">
<string>Show close button on tabs</string>
</property>
</widget>
</item>
<item> <item>
<widget class="QCheckBox" name="hideTabsOnTab"> <widget class="QCheckBox" name="hideTabsOnTab">
<property name="text"> <property name="text">

View File

@ -29,6 +29,7 @@
#include <QMenu> #include <QMenu>
#include <QMouseEvent> #include <QMouseEvent>
#include <QStyleOption>
#include <QApplication> #include <QApplication>
#include <QTimer> #include <QTimer>
#include <QRect> #include <QRect>
@ -42,7 +43,6 @@ TabBar::TabBar(QupZilla* mainClass, TabWidget* tabWidget)
, m_tabWidget(tabWidget) , m_tabWidget(tabWidget)
, m_tabPreview(new TabPreview(mainClass, tabWidget)) , m_tabPreview(new TabPreview(mainClass, tabWidget))
, m_showTabPreviews(false) , m_showTabPreviews(false)
, m_showCloseButtons(false)
, m_clickedTab(0) , m_clickedTab(0)
, m_pinnedTabsCount(0) , m_pinnedTabsCount(0)
, m_normalTabWidth(0) , m_normalTabWidth(0)
@ -53,6 +53,7 @@ TabBar::TabBar(QupZilla* mainClass, TabWidget* tabWidget)
setElideMode(Qt::ElideRight); setElideMode(Qt::ElideRight);
setDocumentMode(true); setDocumentMode(true);
setFocusPolicy(Qt::NoFocus); setFocusPolicy(Qt::NoFocus);
setTabsClosable(true);
setMouseTracking(true); setMouseTracking(true);
setMovable(true); setMovable(true);
@ -76,10 +77,8 @@ void TabBar::loadSettings()
m_tabPreview->setAnimationsEnabled(settings.value("tabPreviewAnimationsEnabled", true).toBool()); m_tabPreview->setAnimationsEnabled(settings.value("tabPreviewAnimationsEnabled", true).toBool());
m_showTabPreviews = settings.value("showTabPreviews", true).toBool(); m_showTabPreviews = settings.value("showTabPreviews", true).toBool();
m_showCloseButtons = settings.value("showCloseButtonOnTabs", true).toBool();
bool activateLastTab = settings.value("ActivateLastTabWhenClosingActual", false).toBool(); bool activateLastTab = settings.value("ActivateLastTabWhenClosingActual", false).toBool();
setTabsClosable(m_showCloseButtons);
setSelectionBehaviorOnRemove(activateLastTab ? QTabBar::SelectPreviousTab : QTabBar::SelectRightTab); setSelectionBehaviorOnRemove(activateLastTab ? QTabBar::SelectPreviousTab : QTabBar::SelectRightTab);
settings.endGroup(); settings.endGroup();
@ -221,8 +220,12 @@ QSize TabBar::tabSizeHint(int index) const
size.setWidth(m_lastTabWidth); size.setWidth(m_lastTabWidth);
} }
if (tabsClosable()) {
// Hiding close buttons to save some space // Hiding close buttons to save some space
tabBar->setTabsClosable(false); tabBar->setTabsClosable(false);
tabBar->showCloseButton(currentIndex());
}
} }
} }
else { else {
@ -241,12 +244,12 @@ QSize TabBar::tabSizeHint(int index) const
} }
// Restore close buttons according to preferences // Restore close buttons according to preferences
if (tabsClosable() != m_showCloseButtons) { if (!tabsClosable()) {
tabBar->setTabsClosable(m_showCloseButtons); tabBar->setTabsClosable(true);
// Hide close buttons on pinned tabs // Hide close buttons on pinned tabs
for (int i = 0; i < count(); ++i) { for (int i = 0; i < count(); ++i) {
updateCloseButton(i); tabBar->updatePinnedTabCloseButton(i);
} }
} }
} }
@ -268,84 +271,98 @@ QSize TabBar::tabSizeHint(int index) const
return size; return size;
} }
//void TabBar::emitMoveAddTabButton(int pox)
//{
// emit moveAddTabButton(pox);
//}
#if 0
void TabBar::tabInserted(int index)
{
// CloseButton* closeButton = new CloseButton(this);
// closeButton->setAutoRaise(true);
// closeButton->setMaximumSize(17,17);
// closeButton->setIcon(style()->standardIcon(QStyle::SP_TitleBarMaxButton));
// connect(closeButton, SIGNAL(clicked()), this, SLOT(closeCurrentTab()));
// setTabButton(index, QTabBar::RightSide, closeButton);
QAbstractButton* button = (QAbstractButton*)tabButton(index, QTabBar::RightSide);
if (!button) {
return;
}
button->resize(17, 17);
}
void TabBar::showCloseButton(int index) void TabBar::showCloseButton(int index)
{ {
if (!validIndex(index)) {
return;
}
WebTab* webTab = qobject_cast<WebTab*>(m_tabWidget->widget(index)); WebTab* webTab = qobject_cast<WebTab*>(m_tabWidget->widget(index));
if (webTab && webTab->isPinned()) { QAbstractButton* button = qobject_cast<QAbstractButton*>(tabButton(index, QTabBar::RightSide));
if (button || (webTab && webTab->isPinned())) {
return; return;
} }
CloseButton* button = (CloseButton*)tabButton(index, QTabBar::RightSide); QAbstractButton* closeButton = new CloseButton(this);
if (!button) { connect(closeButton, SIGNAL(clicked()), this, SLOT(closeTabFromButton()));
return; setTabButton(index, QTabBar::RightSide, closeButton);
}
button->show();
} }
void TabBar::hideCloseButton(int index) void TabBar::hideCloseButton(int index)
{ {
CloseButton* button = (CloseButton*)tabButton(index, QTabBar::RightSide); if (!validIndex(index) || tabsClosable()) {
return;
}
CloseButton* button = qobject_cast<CloseButton*>(tabButton(index, QTabBar::RightSide));
if (!button) { if (!button) {
return; return;
} }
button->hide();
}
#endif
void TabBar::updateCloseButton(int index) const setTabButton(index, QTabBar::RightSide, 0);
button->deleteLater();
}
void TabBar::updatePinnedTabCloseButton(int index)
{ {
QAbstractButton* button = qobject_cast<QAbstractButton*>(tabButton(index, QTabBar::RightSide)); if (!validIndex(index)) {
if (!button) {
return; return;
} }
WebTab* webTab = qobject_cast<WebTab*>(m_tabWidget->widget(index)); WebTab* webTab = qobject_cast<WebTab*>(m_tabWidget->widget(index));
if (webTab && webTab->isPinned()) { QAbstractButton* button = qobject_cast<QAbstractButton*>(tabButton(index, QTabBar::RightSide));
bool pinned = webTab && webTab->isPinned();
if (pinned) {
if (button) {
button->hide(); button->hide();
} }
}
else { else {
if (button) {
button->show(); button->show();
} }
else {
showCloseButton(index);
}
}
} }
void TabBar::closeCurrentTab() void TabBar::closeCurrentTab()
{ {
int id = currentIndex(); m_tabWidget->closeTab(currentIndex());
if (id < 0) {
return;
} }
m_tabWidget->closeTab(id); void TabBar::closeTabFromButton()
{
QWidget* button = qobject_cast<QWidget*>(sender());
int tabToClose = -1;
for (int i = 0; i < count(); ++i) {
if (tabButton(i, QTabBar::RightSide) == button) {
tabToClose = i;
break;
}
}
if (tabToClose != -1) {
m_tabWidget->closeTab(tabToClose);
}
} }
void TabBar::currentTabChanged(int index) void TabBar::currentTabChanged(int index)
{ {
Q_UNUSED(index) if (!validIndex(index)) {
return;
}
hideTabPreview(false); hideTabPreview(false);
showCloseButton(index);
hideCloseButton(m_tabWidget->lastTabIndex());
} }
void TabBar::bookmarkTab() void TabBar::bookmarkTab()
@ -586,3 +603,76 @@ void TabBar::disconnectObjects()
{ {
disconnect(this); disconnect(this);
} }
CloseButton::CloseButton(QWidget* parent)
: QAbstractButton(parent)
{
setFocusPolicy(Qt::NoFocus);
setCursor(Qt::ArrowCursor);
setToolTip(QupZilla::tr("Close Tab"));
resize(sizeHint());
}
QSize CloseButton::sizeHint() const
{
ensurePolished();
int width = style()->pixelMetric(QStyle::PM_TabCloseIndicatorWidth, 0, this);
int height = style()->pixelMetric(QStyle::PM_TabCloseIndicatorHeight, 0, this);
return QSize(width, height);
}
void CloseButton::enterEvent(QEvent* event)
{
if (isEnabled()) {
update();
}
QAbstractButton::enterEvent(event);
}
void CloseButton::leaveEvent(QEvent* event)
{
if (isEnabled()) {
update();
}
QAbstractButton::leaveEvent(event);
}
void CloseButton::hideEvent(QHideEvent* event)
{
QAbstractButton::hideEvent(event);
if (!isVisible()) {
deleteLater();
}
}
void CloseButton::paintEvent(QPaintEvent*)
{
QPainter p(this);
QStyleOption opt;
opt.init(this);
opt.state |= QStyle::State_AutoRaise;
if (isEnabled() && underMouse() && !isChecked() && !isDown()) {
opt.state |= QStyle::State_Raised;
}
if (isChecked()) {
opt.state |= QStyle::State_On;
}
if (isDown()) {
opt.state |= QStyle::State_Sunken;
}
if (const QTabBar* tb = qobject_cast<const QTabBar*>(parent())) {
int index = tb->currentIndex();
QTabBar::ButtonPosition position = (QTabBar::ButtonPosition)style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, tb);
if (tb->tabButton(index, position) == this) {
opt.state |= QStyle::State_Selected;
}
}
style()->drawPrimitive(QStyle::PE_IndicatorTabClose, &opt, &p, this);
}

View File

@ -19,6 +19,7 @@
#define TABBAR_H #define TABBAR_H
#include <QTabBar> #include <QTabBar>
#include <QAbstractButton>
#include "qz_namespace.h" #include "qz_namespace.h"
@ -31,13 +32,8 @@ class QT_QUPZILLA_EXPORT TabBar : public QTabBar
Q_OBJECT Q_OBJECT
public: public:
explicit TabBar(QupZilla* mainClass, TabWidget* tabWidget); explicit TabBar(QupZilla* mainClass, TabWidget* tabWidget);
// void hideCloseButton(int index);
// void showCloseButton(int index);
void updateCloseButton(int index) const;
QSize getTabSizeHint(int index) { return QTabBar::tabSizeHint(index); }
void loadSettings(); void loadSettings();
QSize getTabSizeHint(int index) const { return QTabBar::tabSizeHint(index); }
void setVisible(bool visible); void setVisible(bool visible);
void updateVisibilityWithFullscreen(bool visible); void updateVisibilityWithFullscreen(bool visible);
@ -45,6 +41,8 @@ public:
int pinnedTabsCount(); int pinnedTabsCount();
int normalTabsCount(); int normalTabsCount();
void updatePinnedTabCloseButton(int index);
void disconnectObjects(); void disconnectObjects();
void wheelEvent(QWheelEvent* event); void wheelEvent(QWheelEvent* event);
@ -74,12 +72,19 @@ private slots:
void duplicateTab() { emit duplicateTab(m_clickedTab); } void duplicateTab() { emit duplicateTab(m_clickedTab); }
void bookmarkTab(); void bookmarkTab();
void pinTab(); void pinTab();
void closeCurrentTab(); void closeCurrentTab();
void closeTabFromButton();
void showTabPreview(); void showTabPreview();
void hideTabPreview(bool delayed = true); void hideTabPreview(bool delayed = true);
private: private:
inline bool validIndex(int index) const { return index >= 0 && index < count(); }
void hideCloseButton(int index);
void showCloseButton(int index);
void mouseDoubleClickEvent(QMouseEvent* event); void mouseDoubleClickEvent(QMouseEvent* event);
void mousePressEvent(QMouseEvent* event); void mousePressEvent(QMouseEvent* event);
void mouseMoveEvent(QMouseEvent* event); void mouseMoveEvent(QMouseEvent* event);
@ -90,9 +95,6 @@ private:
void dropEvent(QDropEvent* event); void dropEvent(QDropEvent* event);
QSize tabSizeHint(int index) const; QSize tabSizeHint(int index) const;
// void tabInserted(int index);
// void emitMoveAddTabButton(int pox);
QupZilla* p_QupZilla; QupZilla* p_QupZilla;
TabWidget* m_tabWidget; TabWidget* m_tabWidget;
@ -100,7 +102,6 @@ private:
QTimer* m_tabPreviewTimer; QTimer* m_tabPreviewTimer;
bool m_showTabPreviews; bool m_showTabPreviews;
bool m_showCloseButtons;
int m_clickedTab; int m_clickedTab;
int m_pinnedTabsCount; int m_pinnedTabsCount;
@ -111,4 +112,22 @@ private:
QPoint m_dragStartPosition; QPoint m_dragStartPosition;
}; };
// Class for close button on tabs
// * taken from qtabbar.cpp
class CloseButton : public QAbstractButton
{
Q_OBJECT
public:
CloseButton(QWidget* parent = 0);
QSize sizeHint() const;
inline QSize minimumSizeHint() const { return sizeHint(); }
void enterEvent(QEvent* event);
void leaveEvent(QEvent* event);
void hideEvent(QHideEvent* event);
void paintEvent(QPaintEvent* event);
};
#endif // TABBAR_H #endif // TABBAR_H

View File

@ -405,6 +405,7 @@ void TabWidget::currentTabChanged(int index)
m_isClosingToLastTabIndex = m_lastBackgroundTabIndex == index; m_isClosingToLastTabIndex = m_lastBackgroundTabIndex == index;
m_lastBackgroundTabIndex = -1; m_lastBackgroundTabIndex = -1;
m_lastTabIndex = index;
WebTab* webTab = weTab(index); WebTab* webTab = weTab(index);
LocationBar* locBar = webTab->locationBar(); LocationBar* locBar = webTab->locationBar();
@ -415,7 +416,6 @@ void TabWidget::currentTabChanged(int index)
webTab->setCurrentTab(); webTab->setCurrentTab();
p_QupZilla->currentTabChanged(); p_QupZilla->currentTabChanged();
m_tabBar->updateCloseButton(index);
} }
void TabWidget::tabMoved(int before, int after) void TabWidget::tabMoved(int before, int after)
@ -425,6 +425,7 @@ void TabWidget::tabMoved(int before, int after)
m_isClosingToLastTabIndex = false; m_isClosingToLastTabIndex = false;
m_lastBackgroundTabIndex = -1; m_lastBackgroundTabIndex = -1;
m_lastTabIndex = before;
} }
void TabWidget::tabInserted(int index) void TabWidget::tabInserted(int index)
@ -478,6 +479,13 @@ void TabWidget::stopTabAnimation(int index)
} }
} }
void TabWidget::setCurrentIndex(int index)
{
m_lastTabIndex = currentIndex();
QTabWidget::setCurrentIndex(index);
}
void TabWidget::setTabIcon(int index, const QIcon &icon) void TabWidget::setTabIcon(int index, const QIcon &icon)
{ {
if (!validIndex(index)) { if (!validIndex(index)) {
@ -554,6 +562,11 @@ void TabWidget::showTabBar()
} }
} }
int TabWidget::lastTabIndex() const
{
return m_lastTabIndex;
}
void TabWidget::reloadAllTabs() void TabWidget::reloadAllTabs()
{ {
for (int i = 0; i < count(); i++) { for (int i = 0; i < count(); i++) {
@ -775,7 +788,7 @@ void TabWidget::restorePinnedTabs()
emit pinnedTabAdded(); emit pinnedTabAdded();
} }
m_tabBar->updateCloseButton(addedIndex); m_tabBar->updatePinnedTabCloseButton(addedIndex);
// m_tabBar->moveTab(addedIndex, i); // m_tabBar->moveTab(addedIndex, i);
} }

View File

@ -69,6 +69,7 @@ public:
void startTabAnimation(int index); void startTabAnimation(int index);
void stopTabAnimation(int index); void stopTabAnimation(int index);
void setCurrentIndex(int index);
void setTabIcon(int index, const QIcon &icon); void setTabIcon(int index, const QIcon &icon);
void setTabText(int index, const QString &text); void setTabText(int index, const QString &text);
@ -79,6 +80,7 @@ public:
int pinnedTabsCount() const; int pinnedTabsCount() const;
void showTabBar(); void showTabBar();
int lastTabIndex() const;
TabBar* getTabBar() { return m_tabBar; } TabBar* getTabBar() { return m_tabBar; }
ClosedTabsManager* closedTabsManager() { return m_closedTabsManager; } ClosedTabsManager* closedTabsManager() { return m_closedTabsManager; }

View File

@ -301,7 +301,7 @@ void WebTab::pinTab(int index)
if (m_pinned) { //Unpin tab if (m_pinned) { //Unpin tab
m_pinned = false; m_pinned = false;
tabWidget->setTabText(index, m_view->title()); tabWidget->setTabText(index, m_view->title());
tabWidget->getTabBar()->updateCloseButton(index); tabWidget->getTabBar()->updatePinnedTabCloseButton(index);
} }
else { // Pin tab else { // Pin tab
m_pinned = true; m_pinned = true;
@ -309,7 +309,7 @@ void WebTab::pinTab(int index)
tabWidget->getTabBar()->moveTab(index, 0); // | weird behavior with bad tabWidget->getTabBar()->moveTab(index, 0); // | weird behavior with bad
tabWidget->setTabText(0, QString()); // | tabwidget update if we tabWidget->setTabText(0, QString()); // | tabwidget update if we
tabWidget->setCurrentIndex(0); // <<-- are moving current tab tabWidget->setCurrentIndex(0); // <<-- are moving current tab
tabWidget->getTabBar()->updateCloseButton(0); tabWidget->getTabBar()->updatePinnedTabCloseButton(0);
} }
} }