1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 02:36: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->showTabPreviews->setChecked(settings.value("showTabPreviews", true).toBool());
ui->animatedTabPreviews->setChecked(settings.value("tabPreviewAnimationsEnabled", true).toBool());
ui->showCloseButtonOnTabs->setChecked(settings.value("showCloseButtonOnTabs", true).toBool());
settings.endGroup();
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("showTabPreviews", ui->showTabPreviews->isChecked());
settings.setValue("tabPreviewAnimationsEnabled", ui->animatedTabPreviews->isChecked());
settings.setValue("showCloseButtonOnTabs", ui->showCloseButtonOnTabs->isChecked());
settings.endGroup();
//DOWNLOADS

View File

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

View File

@ -29,6 +29,7 @@
#include <QMenu>
#include <QMouseEvent>
#include <QStyleOption>
#include <QApplication>
#include <QTimer>
#include <QRect>
@ -42,7 +43,6 @@ TabBar::TabBar(QupZilla* mainClass, TabWidget* tabWidget)
, m_tabWidget(tabWidget)
, m_tabPreview(new TabPreview(mainClass, tabWidget))
, m_showTabPreviews(false)
, m_showCloseButtons(false)
, m_clickedTab(0)
, m_pinnedTabsCount(0)
, m_normalTabWidth(0)
@ -53,6 +53,7 @@ TabBar::TabBar(QupZilla* mainClass, TabWidget* tabWidget)
setElideMode(Qt::ElideRight);
setDocumentMode(true);
setFocusPolicy(Qt::NoFocus);
setTabsClosable(true);
setMouseTracking(true);
setMovable(true);
@ -76,10 +77,8 @@ void TabBar::loadSettings()
m_tabPreview->setAnimationsEnabled(settings.value("tabPreviewAnimationsEnabled", true).toBool());
m_showTabPreviews = settings.value("showTabPreviews", true).toBool();
m_showCloseButtons = settings.value("showCloseButtonOnTabs", true).toBool();
bool activateLastTab = settings.value("ActivateLastTabWhenClosingActual", false).toBool();
setTabsClosable(m_showCloseButtons);
setSelectionBehaviorOnRemove(activateLastTab ? QTabBar::SelectPreviousTab : QTabBar::SelectRightTab);
settings.endGroup();
@ -221,8 +220,12 @@ QSize TabBar::tabSizeHint(int index) const
size.setWidth(m_lastTabWidth);
}
// Hiding close buttons to save some space
tabBar->setTabsClosable(false);
if (tabsClosable()) {
// Hiding close buttons to save some space
tabBar->setTabsClosable(false);
tabBar->showCloseButton(currentIndex());
}
}
}
else {
@ -241,12 +244,12 @@ QSize TabBar::tabSizeHint(int index) const
}
// Restore close buttons according to preferences
if (tabsClosable() != m_showCloseButtons) {
tabBar->setTabsClosable(m_showCloseButtons);
if (!tabsClosable()) {
tabBar->setTabsClosable(true);
// Hide close buttons on pinned tabs
for (int i = 0; i < count(); ++i) {
updateCloseButton(i);
tabBar->updatePinnedTabCloseButton(i);
}
}
}
@ -268,84 +271,98 @@ QSize TabBar::tabSizeHint(int index) const
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)
{
if (!validIndex(index)) {
return;
}
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;
}
CloseButton* button = (CloseButton*)tabButton(index, QTabBar::RightSide);
if (!button) {
return;
}
button->show();
QAbstractButton* closeButton = new CloseButton(this);
connect(closeButton, SIGNAL(clicked()), this, SLOT(closeTabFromButton()));
setTabButton(index, QTabBar::RightSide, closeButton);
}
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) {
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 (!button) {
if (!validIndex(index)) {
return;
}
WebTab* webTab = qobject_cast<WebTab*>(m_tabWidget->widget(index));
if (webTab && webTab->isPinned()) {
button->hide();
QAbstractButton* button = qobject_cast<QAbstractButton*>(tabButton(index, QTabBar::RightSide));
bool pinned = webTab && webTab->isPinned();
if (pinned) {
if (button) {
button->hide();
}
}
else {
button->show();
if (button) {
button->show();
}
else {
showCloseButton(index);
}
}
}
void TabBar::closeCurrentTab()
{
int id = currentIndex();
if (id < 0) {
return;
m_tabWidget->closeTab(currentIndex());
}
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;
}
}
m_tabWidget->closeTab(id);
if (tabToClose != -1) {
m_tabWidget->closeTab(tabToClose);
}
}
void TabBar::currentTabChanged(int index)
{
Q_UNUSED(index)
if (!validIndex(index)) {
return;
}
hideTabPreview(false);
showCloseButton(index);
hideCloseButton(m_tabWidget->lastTabIndex());
}
void TabBar::bookmarkTab()
@ -586,3 +603,76 @@ void TabBar::disconnectObjects()
{
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
#include <QTabBar>
#include <QAbstractButton>
#include "qz_namespace.h"
@ -31,13 +32,8 @@ class QT_QUPZILLA_EXPORT TabBar : public QTabBar
Q_OBJECT
public:
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();
QSize getTabSizeHint(int index) const { return QTabBar::tabSizeHint(index); }
void setVisible(bool visible);
void updateVisibilityWithFullscreen(bool visible);
@ -45,6 +41,8 @@ public:
int pinnedTabsCount();
int normalTabsCount();
void updatePinnedTabCloseButton(int index);
void disconnectObjects();
void wheelEvent(QWheelEvent* event);
@ -74,12 +72,19 @@ private slots:
void duplicateTab() { emit duplicateTab(m_clickedTab); }
void bookmarkTab();
void pinTab();
void closeCurrentTab();
void closeTabFromButton();
void showTabPreview();
void hideTabPreview(bool delayed = true);
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 mousePressEvent(QMouseEvent* event);
void mouseMoveEvent(QMouseEvent* event);
@ -90,9 +95,6 @@ private:
void dropEvent(QDropEvent* event);
QSize tabSizeHint(int index) const;
// void tabInserted(int index);
// void emitMoveAddTabButton(int pox);
QupZilla* p_QupZilla;
TabWidget* m_tabWidget;
@ -100,7 +102,6 @@ private:
QTimer* m_tabPreviewTimer;
bool m_showTabPreviews;
bool m_showCloseButtons;
int m_clickedTab;
int m_pinnedTabsCount;
@ -111,4 +112,22 @@ private:
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

View File

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

View File

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

View File

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