diff options
Diffstat (limited to 'src/tabwindow')
-rw-r--r-- | src/tabwindow/tabbar.cpp | 126 | ||||
-rw-r--r-- | src/tabwindow/tabbar.h | 15 | ||||
-rw-r--r-- | src/tabwindow/tabpreviewpopup.cpp | 124 | ||||
-rw-r--r-- | src/tabwindow/tabpreviewpopup.h | 58 |
4 files changed, 321 insertions, 2 deletions
diff --git a/src/tabwindow/tabbar.cpp b/src/tabwindow/tabbar.cpp index 0bed6e69..133ece5c 100644 --- a/src/tabwindow/tabbar.cpp +++ b/src/tabwindow/tabbar.cpp @@ -23,6 +23,8 @@ #include "tabwindow.h" #include "tabhighlighteffect.h" +#include "tabpreviewpopup.h" +#include "webwindow.h" #include <KDebug> #include <KAcceleratorManager> @@ -34,6 +36,9 @@ #include <QPropertyAnimation> #include <QSignalMapper> #include <QStyleOptionFrameV3> +#include <QMouseEvent> +#include <QTimer> +#include <QUrl> static inline QByteArray highlightPropertyName(int index) @@ -85,7 +90,7 @@ QSize TabBar::tabSizeHint(int index) const int minTabWidth = p->sizeHint().width() / genericTabNumber; int w = baseTabWidth; - if (count() >= genericTabNumber) + if (count() > genericTabNumber) { w = minTabWidth; } @@ -270,7 +275,6 @@ void TabBar::setTabHighlighted(int index, bool b) { removeAnimation(index); setTabTextColor(index, KColorScheme(QPalette::Active, KColorScheme::Window).foreground(KColorScheme::NormalText).color()); - return; } @@ -319,3 +323,121 @@ void TabBar::removeAnimation(int index) if (m_highlightAnimation.isEmpty()) m_tabHighlightEffect->setEnabled(false); } + + +void TabBar::tabRemoved(int index) +{ + hideTabPreview(); + removeAnimation(index); +} + + +void TabBar::mouseMoveEvent(QMouseEvent *event) +{ + KTabBar::mouseMoveEvent(event); + + if (count() == 1) + { + return; + } + + // Find the tab under the mouse + const int tabIndex = tabAt(event->pos()); + + // if found and not the current tab then show tab preview + if (tabIndex != -1 + && tabIndex != currentIndex() + && m_currentTabPreviewIndex != tabIndex + && event->buttons() == Qt::NoButton + ) + { + m_currentTabPreviewIndex = tabIndex; + + // if first time over tab, apply a small delay. If not, show it now! + m_isFirstTimeOnTab + ? QTimer::singleShot(200, this, SLOT(showTabPreview())) + : showTabPreview(); + } + + // if current tab or not found then hide previous tab preview + if (tabIndex == currentIndex() || tabIndex == -1) + { + hideTabPreview(); + } +} + + +void TabBar::leaveEvent(QEvent *event) +{ + hideTabPreview(); + m_isFirstTimeOnTab = true; + + KTabBar::leaveEvent(event); +} + + +void TabBar::mousePressEvent(QMouseEvent *event) +{ + hideTabPreview(); + + // just close tab on middle mouse click + if (event->button() == Qt::MidButton) + return; + + KTabBar::mousePressEvent(event); +} + + +void TabBar::showTabPreview() +{ + if (m_isFirstTimeOnTab) + m_isFirstTimeOnTab = false; + + //delete previous tab preview + delete m_previewPopup.data(); + m_previewPopup.clear(); + + TabWindow *tabW = qobject_cast<TabWindow *>(parent()); + + WebWindow *indexedTab = tabW->webWindow(m_currentTabPreviewIndex); + WebWindow *currentTab = tabW->webWindow(currentIndex()); + + // check if view && currentView exist before using them :) + if (!currentTab || !indexedTab) + return; + + // no previews during load + if (indexedTab->isLoading()) + return; + + int w = tabSizeHint(0).width(); + int h = w * tabW->size().height() / tabW->size().width(); + + m_previewPopup = new TabPreviewPopup(indexedTab->tabPreview(w,h), indexedTab->url().toString() , this); + + int tabBarWidth = tabW->size().width(); + int leftIndex = tabRect(m_currentTabPreviewIndex).x() + (tabRect(m_currentTabPreviewIndex).width() - w) / 2; + + if (leftIndex < 0) + { + leftIndex = 0; + } + else if (leftIndex + w > tabBarWidth) + { + leftIndex = tabBarWidth - w; + } + + QPoint pos(leftIndex, tabRect(m_currentTabPreviewIndex).y() + tabRect(m_currentTabPreviewIndex).height()); + m_previewPopup.data()->show(mapToGlobal(pos)); +} + + +void TabBar::hideTabPreview() +{ + if (!m_previewPopup.isNull()) + { + m_previewPopup.data()->hide(); + } + m_currentTabPreviewIndex = -1; + +} diff --git a/src/tabwindow/tabbar.h b/src/tabwindow/tabbar.h index 4cc437ac..af97c998 100644 --- a/src/tabwindow/tabbar.h +++ b/src/tabwindow/tabbar.h @@ -28,6 +28,7 @@ #include <QPropertyAnimation> // Forward Declarations +class TabPreviewPopup; class TabHighlightEffect; class QSignalMapper; @@ -48,6 +49,12 @@ public: protected: virtual QSize tabSizeHint(int index) const; + virtual void mouseMoveEvent(QMouseEvent *event); + virtual void leaveEvent(QEvent *event); + virtual void mousePressEvent(QMouseEvent *event); + + virtual void tabRemoved(int index); + Q_SIGNALS: void cloneTab(int); void closeTab(int); @@ -69,11 +76,19 @@ private Q_SLOTS: void removeAnimation(int index); + void showTabPreview(); + void hideTabPreview(); + private: // highlightAnimation TabHighlightEffect *m_tabHighlightEffect; QHash<QByteArray, QPropertyAnimation*> m_highlightAnimation; QSignalMapper *m_animationMapper; + + // tab preview + QWeakPointer<TabPreviewPopup> m_previewPopup; + int m_currentTabPreviewIndex; + bool m_isFirstTimeOnTab; }; #endif // TAB_BAR diff --git a/src/tabwindow/tabpreviewpopup.cpp b/src/tabwindow/tabpreviewpopup.cpp new file mode 100644 index 00000000..2a05c414 --- /dev/null +++ b/src/tabwindow/tabpreviewpopup.cpp @@ -0,0 +1,124 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2011 by Vyacheslav Blinov <blinov dot vyacheslav at gmail dot com> +* Copyright (C) 2011-2012 by Andrea Diamantini <adjam7 at gmail dot com> +* +* +* 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 the Free Software Foundation; either version 2 of +* the License or (at your option) version 3 or any later version +* accepted by the membership of KDE e.V. (or its successor approved +* by the membership of KDE e.V.), which shall act as a proxy +* defined in Section 14 of version 3 of the license. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +* +* ============================================================ */ + + +// Self Includes +#include "tabpreviewpopup.h" +#include "tabpreviewpopup.moc" + +// Rekonq Includes +#include "tabbar.h" + +// Qt Includes +#include <QLabel> +#include <QVBoxLayout> +#include <QPalette> +#include <QBitmap> +#include <QPoint> +#include <QPaintEvent> +#include <QStylePainter> +#include <QStyleOptionFrame> + + +TabPreviewPopup::TabPreviewPopup(const QPixmap &pixmap, const QString &urlText, QWidget *parent) + : KPassivePopup(parent), + m_thumbLabel(new QLabel(this)), + m_urlLabel(new QLabel(this)) +{ + m_thumbLabel->setAlignment(Qt::AlignHCenter); + m_urlLabel->setAlignment(Qt::AlignHCenter); + + QVBoxLayout *vb = new QVBoxLayout(this); + vb->addWidget(m_thumbLabel); + vb->addWidget(m_urlLabel); + this->setLayout(vb); + + layout()->setAlignment(Qt::AlignTop); + layout()->setMargin(0); + + setPopupStyle(KPassivePopup::CustomStyle + 1); + + // use ToolTip appearance + QPalette p; + + // adjust background color to use tooltip colors + p.setColor(backgroundRole(), p.color(QPalette::ToolTipBase)); + p.setColor(QPalette::Base, p.color(QPalette::ToolTipBase)); + + // adjust foreground color to use tooltip colors + p.setColor(foregroundRole(), p.color(QPalette::ToolTipText)); + p.setColor(QPalette::Text, p.color(QPalette::ToolTipText)); + + setPalette(p); + + // window flags and attributes + setWindowFlags(Qt::ToolTip); + setAttribute(Qt::WA_TranslucentBackground); + setWindowOpacity(style()->styleHint(QStyle::SH_ToolTipLabel_Opacity, 0, this) / 255.0); + + // margins + const int margin = 1 + style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth, 0, this); + setContentsMargins(margin, margin, margin, margin); + + m_thumbLabel->setPixmap(pixmap); + m_urlLabel->setText(urlText); + + setFixedSize(pixmap.width(), pixmap.height() + m_urlLabel->heightForWidth(pixmap.width())); +} + + +TabPreviewPopup::~TabPreviewPopup() +{ + delete m_thumbLabel; + delete m_urlLabel; +} + + +void TabPreviewPopup::setFixedSize(int w, int h) +{ + KPassivePopup::setFixedSize(w, h); + const int margin = 1 + style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth, 0, this); + m_urlLabel->setText(m_urlLabel->fontMetrics().elidedText(m_urlLabel->text(), Qt::ElideMiddle, this->width() - margin * 2)); + + //calculate mask + QStyleOptionFrame opt; + opt.init(this); + + QStyleHintReturnMask mask; + style()->styleHint(QStyle::SH_ToolTip_Mask, &opt, this, &mask); + setMask(mask.region); +} + + +void TabPreviewPopup::paintEvent(QPaintEvent* event) +{ + QStyleOptionFrame opt; + opt.init(this); + + QStylePainter painter(this); + painter.setClipRegion(event->region()); + painter.drawPrimitive(QStyle::PE_PanelTipLabel, opt); +} diff --git a/src/tabwindow/tabpreviewpopup.h b/src/tabwindow/tabpreviewpopup.h new file mode 100644 index 00000000..b496f60f --- /dev/null +++ b/src/tabwindow/tabpreviewpopup.h @@ -0,0 +1,58 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2011 by Vyacheslav Blinov <blinov dot vyacheslav at gmail dot com> +* Copyright (C) 2011-2012 by Andrea Diamantini <adjam7 at gmail dot com> +* +* +* 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 the Free Software Foundation; either version 2 of +* the License or (at your option) version 3 or any later version +* accepted by the membership of KDE e.V. (or its successor approved +* by the membership of KDE e.V.), which shall act as a proxy +* defined in Section 14 of version 3 of the license. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +* +* ============================================================ */ + + +#ifndef TABPREVIEWPOPUP_H +#define TABPREVIEWPOPUP_H + +// KDE Includes +#include <KPassivePopup> + +// forward declatrations +class QLabel; +class QPixmap; +class QString; + +class TabPreviewPopup : public KPassivePopup +{ + Q_OBJECT + +public: + TabPreviewPopup(const QPixmap &pixmap, const QString &urlText, QWidget *parent = 0); + virtual ~TabPreviewPopup(); + +private: + void setFixedSize(int w, int h); + +protected: + void paintEvent(QPaintEvent *event); + +private: + QLabel *m_thumbLabel; + QLabel *m_urlLabel; +}; + +#endif // TABPREVIEWPOPUP_H |