From 9461c52f07a2bf8b9bc25f037b17805cda51b2b0 Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Sun, 10 Mar 2013 19:02:12 +0100 Subject: Supporting panel (again) :) - Move to a pure QWidget base window (instead of TabWidget one) (this to properly store panels position) - Restoring && rewamping panels code - Restoring actions to activate/deactivate them BUG: 312354 --- src/tabwindow/rekonqwindow.cpp | 461 +++++---------------- src/tabwindow/rekonqwindow.h | 130 ++---- src/tabwindow/rwindow.cpp | 397 ++++++++++++++++++ src/tabwindow/rwindow.h | 131 ++++++ src/tabwindow/tabbar.cpp | 19 +- src/tabwindow/tabwidget.cpp | 900 +++++++++++++++++++++++++++++++++++++++++ src/tabwindow/tabwidget.h | 146 +++++++ src/tabwindow/tabwindow.cpp | 831 ------------------------------------- src/tabwindow/tabwindow.h | 140 ------- 9 files changed, 1723 insertions(+), 1432 deletions(-) create mode 100644 src/tabwindow/rwindow.cpp create mode 100644 src/tabwindow/rwindow.h create mode 100644 src/tabwindow/tabwidget.cpp create mode 100644 src/tabwindow/tabwidget.h delete mode 100644 src/tabwindow/tabwindow.cpp delete mode 100644 src/tabwindow/tabwindow.h (limited to 'src/tabwindow') diff --git a/src/tabwindow/rekonqwindow.cpp b/src/tabwindow/rekonqwindow.cpp index ee37eba9..a14dcf15 100644 --- a/src/tabwindow/rekonqwindow.cpp +++ b/src/tabwindow/rekonqwindow.cpp @@ -2,7 +2,7 @@ * * This file is a part of the rekonq project * -* Copyright (C) 2012 by Andrea Diamantini +* Copyright (C) 2013 by Andrea Diamantini * * * This program is free software; you can redistribute it and/or @@ -27,422 +27,163 @@ // Self Includes #include "rekonqwindow.h" #include "rekonqwindow.moc" -#include - -// KDE Includes -#include -#include -#include - -// Qt Includes -#include -#include - - -static bool s_no_query_exit = false; - - -class KRWSessionManager : public KSessionManager -{ - -public: - KRWSessionManager() - { - } - - ~KRWSessionManager() - { - } - - bool dummyInit() - { - return true; - } - - bool saveState(QSessionManager&) - { - KConfig* config = KApplication::kApplication()->sessionConfig(); - int n = 0; - Q_FOREACH(RekonqWindow * rw, RekonqWindow::windowList()) - { - n++; - rw->savePropertiesInternal(config, n); - } - - KConfigGroup group(config, "Number"); - group.writeEntry("NumberOfWindows", n); - return true; - } - - bool commitData(QSessionManager& sm) - { - // not really a fast method but the only compatible one - if (sm.allowsInteraction()) - { - bool canceled = false; - ::s_no_query_exit = true; - - Q_FOREACH(RekonqWindow * window, RekonqWindow::windowList()) - { - if (!window->testAttribute(Qt::WA_WState_Hidden)) - { - QCloseEvent e; - QApplication::sendEvent(window, &e); - canceled = !e.isAccepted(); - if (canceled) - break; - } - } - ::s_no_query_exit = false; - if (canceled) - return false; - - return true; - } - - // the user wants it, the user gets it - return true; - } -}; +// Auto Includes +#include "rekonq.h" -K_GLOBAL_STATIC(KRWSessionManager, ktwsm) -K_GLOBAL_STATIC(QList, sWindowList) +// Local Includes +#include "application.h" +#include "tabwidget.h" +#include "tabbar.h" -// ---------------------------------------------------------------------------------------------------- +#include "webpage.h" +#include "webwindow.h" +// KDE Includes +#include +#include -RekonqWindow::RekonqWindow(QWidget* parent) - : KTabWidget(parent) -{ - // This has to be a window... - setWindowFlags(Qt::Window); - - // Setting attributes (just to be sure...) - setAttribute(Qt::WA_DeleteOnClose, true); - setAttribute(Qt::WA_QuitOnClose, true); - - ktwsm->dummyInit(); - sWindowList->append(this); - - QString geometry; - KCmdLineArgs *args = KCmdLineArgs::parsedArgs("kde"); - if (args && args->isSet("geometry")) - geometry = args->getOption("geometry"); - - if (geometry.isNull()) // if there is no geometry, it doesn't matter - { - KSharedConfig::Ptr cf = KGlobal::config(); - KConfigGroup cg(cf, QL1S("TabWindow")); - restoreWindowSize(cg); - } - else - { - parseGeometry(); - } - - setWindowTitle(KGlobal::caption()); -} - - -RekonqWindow::~RekonqWindow() -{ - sWindowList->removeAll(this); - - KSharedConfig::Ptr cf = KGlobal::config(); - KConfigGroup cg(cf, QL1S("TabWindow")); - saveWindowSize(cg); -} - +// Qt Includes +#include +#include -QSize RekonqWindow::sizeHint() const -{ - QRect desktopRect = QApplication::desktop()->screenGeometry(); - QSize size = desktopRect.size() * 0.8; - return size; -} -QList RekonqWindow::windowList() +RekonqWindow::RekonqWindow(bool withTab, bool privateBrowsingMode, QWidget *parent) + : RWindow(parent) + , _tabWidget(new TabWidget(withTab, privateBrowsingMode, this)) + , _splitter(new QSplitter(this)) { - return *sWindowList; + init(); } -void RekonqWindow::savePropertiesInternal(KConfig *config, int number) +RekonqWindow::RekonqWindow(WebPage *pg, QWidget *parent) + : RWindow(parent) + , _tabWidget(new TabWidget(pg, this)) + , _splitter(new QSplitter(this)) { - QString s; - s.setNum(number); - s.prepend(QL1S("WindowProperties")); - KConfigGroup cg(config, s); - - // store objectName, className, Width and Height for later restoring - // (Only useful for session management) - cg.writeEntry(QL1S("ObjectName"), objectName()); - cg.writeEntry(QL1S("ClassName"), metaObject()->className()); - - saveWindowSize(cg); - - s.setNum(number); - cg = KConfigGroup(config, s); - saveProperties(cg); + init(); } -bool RekonqWindow::readPropertiesInternal(KConfig *config, int number) +RekonqWindow::~RekonqWindow() { - // in order they are in toolbar list - QString s; - s.setNum(number); - s.prepend(QL1S("WindowProperties")); - - KConfigGroup cg(config, s); - - // restore the object name (window role) - if (cg.hasKey(QL1S("ObjectName"))) - setObjectName(cg.readEntry("ObjectName").toLatin1()); // latin1 is right here - - restoreWindowSize(cg); - - s.setNum(number); - KConfigGroup grp(config, s); - readProperties(grp); - - return true; } -void RekonqWindow::restoreWindowSize(const KConfigGroup & _cg) +void RekonqWindow::init() { - int scnum = QApplication::desktop()->screenNumber(window()); - QRect desktopRect = QApplication::desktop()->screenGeometry(scnum); + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - KConfigGroup cg(_cg); + QVBoxLayout *l = new QVBoxLayout(this); + l->setMargin(0); + l->setSpacing(0); - QString geometryKey = QString::fromLatin1("geometry-%1-%2").arg(desktopRect.width()).arg(desktopRect.height()); - QByteArray geometry = cg.readEntry(geometryKey, QByteArray()); + if (ReKonfig::showBookmarksPanel()) + showBookmarksPanel(true); + + if (ReKonfig::showHistoryPanel()) + showHistoryPanel(true); - // if first time run, center window: resize && move.. - if (!restoreGeometry(QByteArray::fromBase64(geometry))) - { - QSize defaultSize = desktopRect.size() * 0.8; - resize(defaultSize); + _splitter->addWidget(_tabWidget); + _tabWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - move((desktopRect.width() - width()) / 2, (desktopRect.height() - height()) / 2); - } + l->addWidget(_splitter); - checkPosition(); + + // fix focus handling + setFocusProxy(_tabWidget); } - -void RekonqWindow::saveWindowSize(const KConfigGroup & _cg) const -{ - int scnum = QApplication::desktop()->screenNumber(window()); - QRect desktopRect = QApplication::desktop()->screenGeometry(scnum); - - int w, h; - if (isMaximized()) - { - w = desktopRect.width() + 1; - h = desktopRect.height() + 1; - } - else - { - w = width(); - h = height(); - } - - KConfigGroup cg(_cg); - - QString widthString = QString::fromLatin1("Width %1").arg(desktopRect.width()); - cg.writeEntry(widthString, w); - - QString heightString = QString::fromLatin1("Height %1").arg(desktopRect.height()); - cg.writeEntry(heightString, h); - - // geometry is saved separately for each resolution - QString geometryKey = QString::fromLatin1("geometry-%1-%2").arg(desktopRect.width()).arg(desktopRect.height()); - QByteArray geometry = saveGeometry(); - cg.writeEntry(geometryKey, geometry.toBase64()); -} +// -------------------------------------------------------------------------------------------------- -void RekonqWindow::parseGeometry() +TabWidget *RekonqWindow::tabWidget() { - QString cmdlineGeometry; - KCmdLineArgs *args = KCmdLineArgs::parsedArgs("kde"); - if (args->isSet("geometry")) - cmdlineGeometry = args->getOption("geometry"); - - Q_ASSERT(!cmdlineGeometry.isNull()); - -// #if defined Q_WS_X11 -// int x, y; -// int w, h; -// int m = XParseGeometry( cmdlineGeometry.toLatin1(), &x, &y, (unsigned int*)&w, (unsigned int*)&h); -// if (parsewidth) { -// const QSize minSize = minimumSize(); -// const QSize maxSize = maximumSize(); -// if ( !(m & WidthValue) ) -// w = width(); -// if ( !(m & HeightValue) ) -// h = height(); -// w = qMin(w,maxSize.width()); -// h = qMin(h,maxSize.height()); -// w = qMax(w,minSize.width()); -// h = qMax(h,minSize.height()); -// resize(w, h); -// } else { -// if ( (m & XNegative) ) -// x = KApplication::desktop()->width() + x - w; -// else if ( (m & XValue) ) -// x = geometry().x(); -// if ( (m & YNegative) ) -// y = KApplication::desktop()->height() + y - h; -// else if ( (m & YValue) ) -// y = geometry().y(); -// -// move(x, y); -// } -// #endif + return _tabWidget; } -void RekonqWindow::resizeEvent(QResizeEvent *event) +TabBar *RekonqWindow::tabBar() { - if (!isFullScreen()) - saveAutoSaveSettings(); - KTabWidget::resizeEvent(event); + return _tabWidget->tabBar(); } -void RekonqWindow::saveAutoSaveSettings() +WebWindow *RekonqWindow::currentWebWindow() const { - kDebug() << "AUTO SAVING SETTINGS..."; - - KSharedConfig::Ptr cf = KGlobal::config(); - KConfigGroup cg(cf, QL1S("TabWindow")); - saveWindowSize(cg); + return _tabWidget->currentWebWindow(); } -bool RekonqWindow::canBeRestored(int number) -{ - if (!qApp->isSessionRestored()) - return false; - KConfig *config = kapp->sessionConfig(); - if (!config) - return false; - - KConfigGroup group(config, "Number"); - const int n = group.readEntry("NumberOfWindows", 1); - return number >= 1 && number <= n; -} +// -------------------------------------------------------------------------------------------------- -bool RekonqWindow::restore(int number, bool show) +void RekonqWindow::loadUrl(const KUrl &url, Rekonq::OpenType type, TabHistory *history) { - if (!canBeRestored(number)) - return false; - KConfig *config = kapp->sessionConfig(); - if (readPropertiesInternal(config, number)) + switch (type) { - if (show) - RekonqWindow::show(); - return true; - } - return false; -} - - -// NOTE: For internal purpose only ------------------------------------------------------ - - -int RekonqWindow::addTab(QWidget *page, const QString &label) -{ - setUpdatesEnabled(false); - int i = KTabWidget::addTab(page, label); - setUpdatesEnabled(true); - - return i; -} - - -int RekonqWindow::addTab(QWidget *page, const QIcon &icon, const QString &label) -{ - setUpdatesEnabled(false); - int i = KTabWidget::addTab(page, icon, label); - setUpdatesEnabled(true); - - return i; -} - - -int RekonqWindow::insertTab(int index, QWidget *page, const QString &label) -{ - if (! ReKonfig::openNewTabsNextToCurrent()) - index = -1; - setUpdatesEnabled(false); - int i = KTabWidget::insertTab(index, page, label); - setUpdatesEnabled(true); + case Rekonq::NewWindow: + case Rekonq::NewPrivateWindow: + rApp->loadUrl(url, type); + return; - return i; + case Rekonq::NewTab: + case Rekonq::NewBackGroundTab: + case Rekonq::NewFocusedTab: + case Rekonq::CurrentTab: + default: + _tabWidget->loadUrl(url, type, history); + break; + }; } -int RekonqWindow::insertTab(int index, QWidget *page, const QIcon &icon, const QString &label) +void RekonqWindow::showBookmarksPanel(bool on) { - if (! ReKonfig::openNewTabsNextToCurrent()) - index = -1; - setUpdatesEnabled(false); - int i = KTabWidget::insertTab(index, page, icon, label); - setUpdatesEnabled(true); + if (on) + { + if (_bookmarksPanel.isNull()) + { + _bookmarksPanel = new BookmarksPanel(i18n("Bookmarks Panel"), this); + connect(_bookmarksPanel.data(), SIGNAL(openUrl(KUrl, Rekonq::OpenType)), this, SLOT(loadUrl(KUrl, Rekonq::OpenType))); - return i; + QAction *a = _tabWidget->actionByName(QL1S("show_bookmarks_panel")); + connect(_bookmarksPanel.data(), SIGNAL(visibilityChanged(bool)), a, SLOT(setChecked(bool))); + } + _splitter->insertWidget(0, _bookmarksPanel.data()); + _bookmarksPanel.data()->show(); + } + else + { + _bookmarksPanel.data()->hide(); + delete _bookmarksPanel.data(); + _bookmarksPanel.clear(); + } } -// -------------------------------------------------------------------------------------- - - -void RekonqWindow::checkPosition() +void RekonqWindow::showHistoryPanel(bool on) { - // no need to check trivial positions... - if (isMaximized()) - return; - - QList wList = RekonqWindow::windowList(); - int wNumber = wList.count(); - - // no need to check first window... - if (wNumber <= 1) - return; - - int div = wNumber % 4; - - int scnum = QApplication::desktop()->screenNumber(window()); - QRect desktopRect = QApplication::desktop()->screenGeometry(scnum); + if (on) + { + if (_historyPanel.isNull()) + { + _historyPanel = new HistoryPanel(i18n("History Panel"), this); + connect(_historyPanel.data(), SIGNAL(openUrl(KUrl, Rekonq::OpenType)), this, SLOT(loadUrl(KUrl, Rekonq::OpenType))); + + QAction *a = _tabWidget->actionByName(QL1S("show_history_panel")); + connect(_historyPanel.data(), SIGNAL(visibilityChanged(bool)), a, SLOT(setChecked(bool))); - switch (div) + } + _splitter->insertWidget(0, _historyPanel.data()); + _historyPanel.data()->show(); + } + else { - case 2: - // left down - move(desktopRect.width() - width(), desktopRect.height() - height()); - break; - case 3: - // right down - move(0, desktopRect.height() - height()); - break; - case 0: - // left top - move(desktopRect.width() - width(), 0); - break; - case 1: - // right top - move(0, 0); - break; - default: - kDebug() << "OOPS...THIS SHOULD NEVER HAPPEN!!"; - break; + _historyPanel.data()->hide(); + delete _historyPanel.data(); + _historyPanel.clear(); } } diff --git a/src/tabwindow/rekonqwindow.h b/src/tabwindow/rekonqwindow.h index ed018e4a..1d46faeb 100644 --- a/src/tabwindow/rekonqwindow.h +++ b/src/tabwindow/rekonqwindow.h @@ -2,7 +2,7 @@ * * This file is a part of the rekonq project * -* Copyright (C) 2012 by Andrea Diamantini +* Copyright (C) 2013 by Andrea Diamantini * * * This program is free software; you can redistribute it and/or @@ -32,109 +32,55 @@ // Rekonq Includes #include "rekonq_defines.h" -// KDE Includes -#include -#include -#include +// Local Includes +#include "rwindow.h" +#include "tabwidget.h" + +#include "bookmarkspanel.h" +#include "historypanel.h" // Qt Includes -#include - -/** - * This is rekonq (re)implementation of KMainWindow, - * given that we'd like to NOT use a "real" xMainWindow - * but a widget with the nice mainwindow properties - * (eg: session management, auto save dimension, etc) but - * NOT its peculiar containers (eg: toolbars, menubar, statusbar, - * central widget...) - * - */ -class RekonqWindow : public KTabWidget -{ - friend class KRWSessionManager; +#include +#include + +// Forward Declarations +class TabBar; + +class WebPage; +class WebWindow; + +class RekonqWindow : public RWindow +{ Q_OBJECT public: - explicit RekonqWindow(QWidget* parent = 0); + explicit RekonqWindow(bool withTab = true, bool PrivateBrowsingMode = false, QWidget *parent = 0); + explicit RekonqWindow(WebPage *pg, QWidget *parent = 0); virtual ~RekonqWindow(); - QSize sizeHint() const; - - /** - * List of members of RekonqWindow class. - */ - static QList windowList(); - - /** - * If the session did contain so high a @p number, @p true is returned, - * else @p false. - * @see restore() - **/ - static bool canBeRestored(int number); - - /** - * Try to restore the toplevel widget as defined by @p number (1..X). - * - * You should call canBeRestored() first. - * - **/ - bool restore(int number, bool show = true); - - // NOTE: For internal purpose only ------------------------------------------------------ - int addTab(QWidget *page, const QString &label); - int addTab(QWidget *page, const QIcon &icon, const QString &label); - - int insertTab(int index, QWidget *page, const QString &label); - int insertTab(int index, QWidget *page, const QIcon &icon, const QString &label); - // -------------------------------------------------------------------------------------- - -protected: - /** - * Save your instance-specific properties. The function is - * invoked when the session manager requests your application - * to save its state. - * - * Please reimplement these function in childclasses. - * - * Note: No user interaction is allowed - * in this function! - * - */ - virtual void saveProperties(KConfigGroup &) {} - - /** - * Read your instance-specific properties. - * - * Is called indirectly by restore(). - */ - virtual void readProperties(const KConfigGroup &) {} - - void savePropertiesInternal(KConfig*, int); - bool readPropertiesInternal(KConfig*, int); - - /** - * For inherited classes - */ - void saveWindowSize(const KConfigGroup &config) const; - /** - * For inherited classes - * Note that a -geometry on the command line has priority. - */ - void restoreWindowSize(const KConfigGroup & config); - - /// parse the geometry from the geometry command line argument - void parseGeometry(); - - virtual void resizeEvent(QResizeEvent *); + TabWidget *tabWidget(); + TabBar *tabBar(); + WebWindow *currentWebWindow() const; -private Q_SLOTS: - void saveAutoSaveSettings(); +private: + void init(); + +public Q_SLOTS: + void loadUrl(const KUrl &, Rekonq::OpenType type = Rekonq::CurrentTab, TabHistory *history = 0); +private Q_SLOTS: + void showBookmarksPanel(bool); + void showHistoryPanel(bool); + private: - /// This has been added to just fix window position && not let them be overlying - void checkPosition(); + TabWidget *_tabWidget; + + QSplitter *_splitter; + + QWeakPointer _historyPanel; + QWeakPointer _bookmarksPanel; }; #endif // REKONQ_WINDOW_H diff --git a/src/tabwindow/rwindow.cpp b/src/tabwindow/rwindow.cpp new file mode 100644 index 00000000..f7304da5 --- /dev/null +++ b/src/tabwindow/rwindow.cpp @@ -0,0 +1,397 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2012-2013 by Andrea Diamantini +* +* +* 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 . +* +* ============================================================ */ + + +// Self Includes +#include "rwindow.h" +#include "rwindow.moc" + +// KDE Includes +#include +#include +#include + +// Qt Includes +#include +#include + + +static bool s_no_query_exit = false; + + +class KRWSessionManager : public KSessionManager +{ + +public: + KRWSessionManager() + { + } + + ~KRWSessionManager() + { + } + + bool dummyInit() + { + return true; + } + + bool saveState(QSessionManager&) + { + KConfig* config = KApplication::kApplication()->sessionConfig(); + int n = 0; + Q_FOREACH(RWindow * rw, RWindow::windowList()) + { + n++; + rw->savePropertiesInternal(config, n); + } + + KConfigGroup group(config, "Number"); + group.writeEntry("NumberOfWindows", n); + return true; + } + + bool commitData(QSessionManager& sm) + { + // not really a fast method but the only compatible one + if (sm.allowsInteraction()) + { + bool canceled = false; + ::s_no_query_exit = true; + + Q_FOREACH(RWindow * window, RWindow::windowList()) + { + if (!window->testAttribute(Qt::WA_WState_Hidden)) + { + QCloseEvent e; + QApplication::sendEvent(window, &e); + canceled = !e.isAccepted(); + if (canceled) + break; + } + } + ::s_no_query_exit = false; + if (canceled) + return false; + + return true; + } + + // the user wants it, the user gets it + return true; + } +}; + + +K_GLOBAL_STATIC(KRWSessionManager, ktwsm) +K_GLOBAL_STATIC(QList, sWindowList) + + +// ---------------------------------------------------------------------------------------------------- + + +RWindow::RWindow(QWidget* parent) + : QWidget(parent) +{ + // This has to be a window... + setWindowFlags(Qt::Window); + + // Setting attributes (just to be sure...) + setAttribute(Qt::WA_DeleteOnClose, true); + setAttribute(Qt::WA_QuitOnClose, true); + + ktwsm->dummyInit(); + sWindowList->append(this); + + QString geometry; + KCmdLineArgs *args = KCmdLineArgs::parsedArgs("kde"); + if (args && args->isSet("geometry")) + geometry = args->getOption("geometry"); + + if (geometry.isNull()) // if there is no geometry, it doesn't matter + { + KSharedConfig::Ptr cf = KGlobal::config(); + KConfigGroup cg(cf, QL1S("RekonqWindow")); + restoreWindowSize(cg); + } + else + { + parseGeometry(); + } + + setWindowTitle(KGlobal::caption()); +} + + +RWindow::~RWindow() +{ + sWindowList->removeAll(this); + + KSharedConfig::Ptr cf = KGlobal::config(); + KConfigGroup cg(cf, QL1S("RekonqWindow")); + saveWindowSize(cg); +} + + +QSize RWindow::sizeHint() const +{ + QRect desktopRect = QApplication::desktop()->screenGeometry(); + QSize size = desktopRect.size() * 0.8; + return size; +} + +QList RWindow::windowList() +{ + return *sWindowList; +} + + +void RWindow::savePropertiesInternal(KConfig *config, int number) +{ + QString s; + s.setNum(number); + s.prepend(QL1S("WindowProperties")); + KConfigGroup cg(config, s); + + // store objectName, className, Width and Height for later restoring + // (Only useful for session management) + cg.writeEntry(QL1S("ObjectName"), objectName()); + cg.writeEntry(QL1S("ClassName"), metaObject()->className()); + + saveWindowSize(cg); + + s.setNum(number); + cg = KConfigGroup(config, s); + saveProperties(cg); +} + + +bool RWindow::readPropertiesInternal(KConfig *config, int number) +{ + // in order they are in toolbar list + QString s; + s.setNum(number); + s.prepend(QL1S("WindowProperties")); + + KConfigGroup cg(config, s); + + // restore the object name (window role) + if (cg.hasKey(QL1S("ObjectName"))) + setObjectName(cg.readEntry("ObjectName").toLatin1()); // latin1 is right here + + restoreWindowSize(cg); + + s.setNum(number); + KConfigGroup grp(config, s); + readProperties(grp); + + return true; +} + + +void RWindow::restoreWindowSize(const KConfigGroup & _cg) +{ + int scnum = QApplication::desktop()->screenNumber(window()); + QRect desktopRect = QApplication::desktop()->screenGeometry(scnum); + + KConfigGroup cg(_cg); + + QString geometryKey = QString::fromLatin1("geometry-%1-%2").arg(desktopRect.width()).arg(desktopRect.height()); + QByteArray geometry = cg.readEntry(geometryKey, QByteArray()); + + // if first time run, center window: resize && move.. + if (!restoreGeometry(QByteArray::fromBase64(geometry))) + { + QSize defaultSize = desktopRect.size() * 0.8; + resize(defaultSize); + + move((desktopRect.width() - width()) / 2, (desktopRect.height() - height()) / 2); + } + + checkPosition(); +} + + +void RWindow::saveWindowSize(const KConfigGroup & _cg) const +{ + int scnum = QApplication::desktop()->screenNumber(window()); + QRect desktopRect = QApplication::desktop()->screenGeometry(scnum); + + int w, h; + if (isMaximized()) + { + w = desktopRect.width() + 1; + h = desktopRect.height() + 1; + } + else + { + w = width(); + h = height(); + } + + KConfigGroup cg(_cg); + + QString widthString = QString::fromLatin1("Width %1").arg(desktopRect.width()); + cg.writeEntry(widthString, w); + + QString heightString = QString::fromLatin1("Height %1").arg(desktopRect.height()); + cg.writeEntry(heightString, h); + + // geometry is saved separately for each resolution + QString geometryKey = QString::fromLatin1("geometry-%1-%2").arg(desktopRect.width()).arg(desktopRect.height()); + QByteArray geometry = saveGeometry(); + cg.writeEntry(geometryKey, geometry.toBase64()); +} + + +void RWindow::parseGeometry() +{ + QString cmdlineGeometry; + KCmdLineArgs *args = KCmdLineArgs::parsedArgs("kde"); + if (args->isSet("geometry")) + cmdlineGeometry = args->getOption("geometry"); + + Q_ASSERT(!cmdlineGeometry.isNull()); + +// #if defined Q_WS_X11 +// int x, y; +// int w, h; +// int m = XParseGeometry( cmdlineGeometry.toLatin1(), &x, &y, (unsigned int*)&w, (unsigned int*)&h); +// if (parsewidth) { +// const QSize minSize = minimumSize(); +// const QSize maxSize = maximumSize(); +// if ( !(m & WidthValue) ) +// w = width(); +// if ( !(m & HeightValue) ) +// h = height(); +// w = qMin(w,maxSize.width()); +// h = qMin(h,maxSize.height()); +// w = qMax(w,minSize.width()); +// h = qMax(h,minSize.height()); +// resize(w, h); +// } else { +// if ( (m & XNegative) ) +// x = KApplication::desktop()->width() + x - w; +// else if ( (m & XValue) ) +// x = geometry().x(); +// if ( (m & YNegative) ) +// y = KApplication::desktop()->height() + y - h; +// else if ( (m & YValue) ) +// y = geometry().y(); +// +// move(x, y); +// } +// #endif +} + + +void RWindow::resizeEvent(QResizeEvent *event) +{ + if (!isFullScreen()) + saveAutoSaveSettings(); + QWidget::resizeEvent(event); +} + + +void RWindow::saveAutoSaveSettings() +{ + kDebug() << "AUTO SAVING SETTINGS..."; + + KSharedConfig::Ptr cf = KGlobal::config(); + KConfigGroup cg(cf, QL1S("RekonqWindow")); + saveWindowSize(cg); +} + + +bool RWindow::canBeRestored(int number) +{ + if (!qApp->isSessionRestored()) + return false; + KConfig *config = kapp->sessionConfig(); + if (!config) + return false; + + KConfigGroup group(config, "Number"); + const int n = group.readEntry("NumberOfWindows", 1); + return number >= 1 && number <= n; +} + + +bool RWindow::restore(int number, bool show) +{ + if (!canBeRestored(number)) + return false; + KConfig *config = kapp->sessionConfig(); + if (readPropertiesInternal(config, number)) + { + if (show) + RWindow::show(); + return true; + } + return false; +} + + +void RWindow::checkPosition() +{ + // no need to check trivial positions... + if (isMaximized()) + return; + + QList wList = RWindow::windowList(); + int wNumber = wList.count(); + + // no need to check first window... + if (wNumber <= 1) + return; + + int div = wNumber % 4; + + int scnum = QApplication::desktop()->screenNumber(window()); + QRect desktopRect = QApplication::desktop()->screenGeometry(scnum); + + switch (div) + { + case 2: + // left down + move(desktopRect.width() - width(), desktopRect.height() - height()); + break; + case 3: + // right down + move(0, desktopRect.height() - height()); + break; + case 0: + // left top + move(desktopRect.width() - width(), 0); + break; + case 1: + // right top + move(0, 0); + break; + default: + kDebug() << "OOPS...THIS SHOULD NEVER HAPPEN!!"; + break; + } +} diff --git a/src/tabwindow/rwindow.h b/src/tabwindow/rwindow.h new file mode 100644 index 00000000..871d3788 --- /dev/null +++ b/src/tabwindow/rwindow.h @@ -0,0 +1,131 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2012-2013 by Andrea Diamantini +* +* +* 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 . +* +* ============================================================ */ + + + +#ifndef R_WINDOW_H +#define R_WINDOW_H + + +// Rekonq Includes +#include "rekonq_defines.h" + +// KDE Includes +#include +#include + +// Qt Includes +#include + +/** + * This is rekonq (re)implementation of KMainWindow, + * given that we'd like to NOT use a "real" xMainWindow + * but a widget with the nice mainwindow properties + * (eg: session management, auto save dimension, etc) but + * NOT its peculiar containers (eg: toolbars, menubar, statusbar, + * central widget...) + * + */ +class RWindow : public QWidget +{ + friend class KRWSessionManager; + + Q_OBJECT + +public: + explicit RWindow(QWidget* parent = 0); + + virtual ~RWindow(); + + QSize sizeHint() const; + + /** + * List of members of RekonqWindow class. + */ + static QList windowList(); + + /** + * If the session did contain so high a @p number, @p true is returned, + * else @p false. + * @see restore() + **/ + static bool canBeRestored(int number); + + /** + * Try to restore the toplevel widget as defined by @p number (1..X). + * + * You should call canBeRestored() first. + * + **/ + bool restore(int number, bool show = true); + +protected: + /** + * Save your instance-specific properties. The function is + * invoked when the session manager requests your application + * to save its state. + * + * Please reimplement these function in childclasses. + * + * Note: No user interaction is allowed + * in this function! + * + */ + virtual void saveProperties(KConfigGroup &) {} + + /** + * Read your instance-specific properties. + * + * Is called indirectly by restore(). + */ + virtual void readProperties(const KConfigGroup &) {} + + void savePropertiesInternal(KConfig*, int); + bool readPropertiesInternal(KConfig*, int); + + /** + * For inherited classes + */ + void saveWindowSize(const KConfigGroup &config) const; + /** + * For inherited classes + * Note that a -geometry on the command line has priority. + */ + void restoreWindowSize(const KConfigGroup & config); + + /// parse the geometry from the geometry command line argument + void parseGeometry(); + + virtual void resizeEvent(QResizeEvent *); + +private Q_SLOTS: + void saveAutoSaveSettings(); + +private: + /// This has been added to just fix window position && not let them be overlying + void checkPosition(); +}; + +#endif // R_WINDOW_H diff --git a/src/tabwindow/tabbar.cpp b/src/tabwindow/tabbar.cpp index bdd07573..7363d965 100644 --- a/src/tabwindow/tabbar.cpp +++ b/src/tabwindow/tabbar.cpp @@ -33,7 +33,8 @@ #include "rekonq.h" // Local Includes -#include "tabwindow.h" +#include "tabwidget.h" + #include "tabhighlighteffect.h" #include "tabpreviewpopup.h" #include "webwindow.h" @@ -186,7 +187,7 @@ void TabBar::detachTab() void TabBar::contextMenu(int tabIndex, const QPoint &pos) { - TabWindow *w = qobject_cast(parent()); + TabWidget *w = qobject_cast(parent()); QAction *a; @@ -262,7 +263,7 @@ void TabBar::contextMenu(int tabIndex, const QPoint &pos) void TabBar::emptyAreaContextMenu(const QPoint &pos) { - TabWindow *w = qobject_cast(parent()); + TabWidget *w = qobject_cast(parent()); QAction *a; @@ -355,7 +356,7 @@ void TabBar::tabInserted(int index) if (index < availableIndex) { - TabWindow *w = qobject_cast(parent()); + TabWidget *w = qobject_cast(parent()); w->moveTab(index, availableIndex); } @@ -446,7 +447,7 @@ void TabBar::mouseReleaseEvent(QMouseEvent *event) { if (!tabData(i).toBool()) { - TabWindow *w = qobject_cast(parent()); + TabWidget *w = qobject_cast(parent()); w->moveTab(i, pinnedTabs); w->setCurrentIndex(pinnedTabs); } @@ -457,7 +458,7 @@ void TabBar::mouseReleaseEvent(QMouseEvent *event) { if (tabData(i).toBool()) { - TabWindow *w = qobject_cast(parent()); + TabWidget *w = qobject_cast(parent()); w->moveTab(i, pinnedTabs - 1); w->setCurrentIndex(pinnedTabs - 1); } @@ -482,7 +483,7 @@ void TabBar::showTabPreview() delete m_previewPopup.data(); m_previewPopup.clear(); - TabWindow *tabW = qobject_cast(parent()); + TabWidget *tabW = qobject_cast(parent()); WebWindow *indexedTab = tabW->webWindow(m_currentTabPreviewIndex); WebWindow *currentTab = tabW->webWindow(currentIndex()); @@ -546,7 +547,7 @@ void TabBar::pinTab() } } - TabWindow *w = qobject_cast(parent()); + TabWidget *w = qobject_cast(parent()); w->moveTab(index, availableIndex); index = availableIndex; @@ -593,7 +594,7 @@ void TabBar::unpinTab() } } - TabWindow *w = qobject_cast(parent()); + TabWidget *w = qobject_cast(parent()); w->moveTab(index, availableIndex); index = availableIndex; diff --git a/src/tabwindow/tabwidget.cpp b/src/tabwindow/tabwidget.cpp new file mode 100644 index 00000000..bb62ca27 --- /dev/null +++ b/src/tabwindow/tabwidget.cpp @@ -0,0 +1,900 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2012 by Andrea Diamantini +* +* +* 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 . +* +* ============================================================ */ + + +// Self Includes +#include "tabwidget.h" +#include "tabwidget.moc" + +// Auto Includes +#include "rekonq.h" + +// Local Includes +#include "application.h" +#include "rekonqwindow.h" + +#include "webpage.h" +#include "webwindow.h" +#include "tabbar.h" + +#include "tabhistory.h" + +#include "bookmarkmanager.h" +#include "iconmanager.h" +#include "sessionmanager.h" + +// KDE Includes +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +// Qt Includes +#include +#include +#include +#include +#include +#include +#include +#include + + +TabWidget::TabWidget(bool withTab, bool PrivateBrowsingMode, QWidget *parent) + : KTabWidget(parent) + , _addTabButton(new QToolButton(this)) + , _openedTabsCounter(0) + , _isPrivateBrowsing(PrivateBrowsingMode) + , _ac(new KActionCollection(this)) +{ + init(); + + // NOTE: we usually create TabWidget with AT LEAST one tab, but + // in one important case... + if (withTab) + { + WebWindow *tab = prepareNewTab(); + addTab(tab, i18n("new tab")); + setCurrentWidget(tab); + } +} + + +TabWidget::TabWidget(WebPage *pg, QWidget *parent) + : KTabWidget(parent) + , _addTabButton(new QToolButton(this)) + , _openedTabsCounter(0) + , _isPrivateBrowsing(false) + , _ac(new KActionCollection(this)) +{ + init(); + + WebWindow *tab = prepareNewTab(pg); + addTab(tab, i18n("new tab")); + setCurrentWidget(tab); +} + + +void TabWidget::init() +{ + setContentsMargins(0, 0, 0, 0); + + setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); + + // set mouse tracking for tab previews + setMouseTracking(true); + + // setting tabbar + TabBar *tabBar = new TabBar(this); + setTabBar(tabBar); + + // sets document mode; this removes the frame around the tabs + setDocumentMode(true); + + // connecting tabbar signals + connect(tabBar, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int))); + connect(tabBar, SIGNAL(mouseMiddleClick(int)), this, SLOT(closeTab(int))); + + connect(tabBar, SIGNAL(newTabRequest()), this, SLOT(newTab())); + + connect(tabBar, SIGNAL(cloneTab(int)), this, SLOT(cloneTab(int))); + connect(tabBar, SIGNAL(closeTab(int)), this, SLOT(closeTab(int))); + connect(tabBar, SIGNAL(closeOtherTabs(int)), this, SLOT(closeOtherTabs(int))); + connect(tabBar, SIGNAL(reloadTab(int)), this, SLOT(reloadTab(int))); + connect(tabBar, SIGNAL(detachTab(int)), this, SLOT(detachTab(int))); + + connect(tabBar, SIGNAL(tabLayoutChanged()), this, SLOT(updateNewTabButtonPosition())); + + // ============================== Tab Window Actions ==================================== + _ac->addAssociatedWidget(this); + + KAction* a; + + a = new KAction(KIcon("tab-new"), i18n("New &Tab"), this); + a->setShortcut(KShortcut(Qt::CTRL + Qt::Key_T)); + actionCollection()->addAction(QL1S("new_tab"), a); + connect(a, SIGNAL(triggered(bool)), this, SLOT(newTab())); + + a = new KAction(KIcon("tab-new"), i18n("Open Last Closed Tab"), this); + a->setShortcut(KShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_T)); + actionCollection()->addAction(QL1S("open_last_closed_tab"), a); + connect(a, SIGNAL(triggered(bool)), this, SLOT(restoreLastClosedTab())); + + a = new KAction(KIcon("tab-close"), i18n("&Close Tab"), this); + a->setShortcuts(KStandardShortcut::close()); + actionCollection()->addAction(QL1S("close_tab"), a); + connect(a, SIGNAL(triggered(bool)), this, SLOT(closeTab())); + + a = new KAction(i18n("Show Next Tab"), this); + a->setShortcuts(QApplication::isRightToLeft() ? KStandardShortcut::tabPrev() : KStandardShortcut::tabNext()); + actionCollection()->addAction(QL1S("show_next_tab"), a); + connect(a, SIGNAL(triggered(bool)), this, SLOT(nextTab())); + + a = new KAction(i18n("Show Previous Tab"), this); + a->setShortcuts(QApplication::isRightToLeft() ? KStandardShortcut::tabNext() : KStandardShortcut::tabPrev()); + actionCollection()->addAction(QL1S("show_prev_tab"), a); + connect(a, SIGNAL(triggered(bool)), this, SLOT(previousTab())); + + a = KStandardAction::fullScreen(this, SLOT(setFullScreen(bool)), this, actionCollection()); + KShortcut fullScreenShortcut = KStandardShortcut::fullScreen(); + fullScreenShortcut.setAlternate(Qt::Key_F11); + a->setShortcut(fullScreenShortcut); + + a = new KAction(KIcon("bookmarks"), i18n("Bookmark all tabs"), this); + actionCollection()->addAction(QL1S("bookmark_all_tabs"), a); + connect(a, SIGNAL(triggered(bool)), this, SLOT(bookmarkAllTabs())); + + // ---------------------------------------------------------------------------------------------- + // Add Tab Button + _addTabButton->setDefaultAction(actionByName(QL1S("new_tab"))); + _addTabButton->setAutoRaise(true); + _addTabButton->raise(); + _addTabButton->setToolButtonStyle(Qt::ToolButtonIconOnly); + + connect(this, SIGNAL(currentChanged(int)), this, SLOT(currentChanged(int))); + + // ---------------------------------------------------------------------------------------------- + RekonqWindow *rw = qobject_cast(parent()); + // setup bookmarks panel action + a = new KAction(KIcon("bookmarks-organize"), i18n("Bookmarks Panel"), this); + a->setShortcut(KShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_B)); + actionCollection()->addAction(QL1S("show_bookmarks_panel"), a); + a->setCheckable(true); + connect(a, SIGNAL(triggered(bool)), rw, SLOT(showBookmarksPanel(bool))); + + // setup history panel action + a = new KAction(KIcon("view-history"), i18n("History Panel"), this); + a->setShortcut(KShortcut(Qt::CTRL + Qt::Key_H)); + actionCollection()->addAction(QL1S("show_history_panel"), a); + a->setCheckable(true); + connect(a, SIGNAL(triggered(bool)), rw, SLOT(showHistoryPanel(bool))); + + // ---------------------------------------------------------------------------------------------- + // shortcuts for quickly switching to a tab + QSignalMapper *tabSignalMapper = new QSignalMapper(this); + for (int i = 0; i < 9; i++) + { + a = new KAction(i18n("Switch to Tab %1", i+1), this); + a->setShortcut(KShortcut(QString("Alt+%1").arg(i+1))); + actionCollection()->addAction(QL1S(QString("switch_tab_" + QString::number(i+1)).toAscii()), a); + connect(a, SIGNAL(triggered(bool)), tabSignalMapper, SLOT(map())); + tabSignalMapper->setMapping(a, i); + } + connect(tabSignalMapper, SIGNAL(mapped(int)), this, SLOT(setCurrentIndex(int))); + + // shortcuts for loading favorite pages + QSignalMapper *favoritesSignalMapper = new QSignalMapper(this); + for (int i = 1; i <= 9; ++i) + { + a = new KAction(i18n("Switch to Favorite Page %1", i), this); + a->setShortcut(KShortcut(QString("Ctrl+%1").arg(i))); + actionCollection()->addAction(QL1S(QString("switch_favorite_" + QString::number(i)).toAscii()), a); + connect(a, SIGNAL(triggered(bool)), favoritesSignalMapper, SLOT(map())); + favoritesSignalMapper->setMapping(a, i); + } + connect(favoritesSignalMapper, SIGNAL(mapped(int)), this, SLOT(loadFavorite(int))); + + _ac->readSettings(); + + // ---------------------------------------------------------------------------------------------- + int n = rApp->rekonqWindowList().count() + 1; + QList list = SessionManager::self()->closedSitesForWindow( QL1S("win") + QString::number(n) ); + Q_FOREACH(const TabHistory & tab, list) + { + if (tab.url.startsWith(QL1S("about"))) + continue; + m_recentlyClosedTabs.removeAll(tab); + m_recentlyClosedTabs.prepend(tab); + } +} + + +// ---------------------------------------------------------------------------------------------------- + + +KActionCollection *TabWidget::actionCollection() const +{ + return _ac; +} + + +QAction *TabWidget::actionByName(const QString &name) +{ + return actionCollection()->action(name); +} + + +TabBar *TabWidget::tabBar() const +{ + TabBar *tabBar = qobject_cast(QTabWidget::tabBar()); + return tabBar; +} + + +WebWindow *TabWidget::currentWebWindow() const +{ + return webWindow(currentIndex()); +} + + +WebWindow *TabWidget::webWindow(int index) const +{ + WebWindow *tab = qobject_cast(this->widget(index)); + if (tab) + { + return tab; + } + + kDebug() << "WebWindow with index " << index << "not found. Returning NULL." ; + return 0; +} + + +QList TabWidget::recentlyClosedTabs() +{ + return m_recentlyClosedTabs; +} + + +void TabWidget::newTab(WebPage *page) +{ + WebWindow *tab = prepareNewTab(page); + addTab(tab, i18n("new tab")); + setCurrentWidget(tab); + + // no need to load an url if we already have a page... + if (page) + return; + + switch (ReKonfig::newTabsBehaviour()) + { + case 0: // new tab page + tab->load(KUrl("about:home")); + break; + case 2: // homepage + tab->load(KUrl(ReKonfig::homePage())); + break; + case 1: // blank page + default: + tab->load(KUrl("about:blank")); + break; + } +} + + +WebWindow *TabWidget::prepareNewTab(WebPage *page) +{ + WebWindow *tab = new WebWindow(this, _isPrivateBrowsing, page); + + connect(tab, SIGNAL(titleChanged(QString)), this, SLOT(tabTitleChanged(QString))); + connect(tab, SIGNAL(urlChanged(QUrl)), this, SLOT(tabUrlChanged(QUrl))); + connect(tab, SIGNAL(iconChanged()), this, SLOT(tabIconChanged())); + + connect(tab, SIGNAL(loadStarted()), this, SLOT(tabLoadStarted())); + connect(tab, SIGNAL(loadFinished(bool)), this, SLOT(tabLoadFinished(bool))); + + connect(tab, SIGNAL(pageCreated(WebPage*)), this, SLOT(pageCreated(WebPage*))); + + connect(tab, SIGNAL(setFullScreen(bool)), this, SLOT(setFullScreen(bool))); + + return tab; +} + + +void TabWidget::loadUrl(const KUrl &url, Rekonq::OpenType type, TabHistory *history) +{ + WebWindow *tab = 0; + switch (type) + { + case Rekonq::NewTab: + tab = prepareNewTab(); + _openedTabsCounter++; + insertTab(currentIndex() + _openedTabsCounter, tab, i18n("new tab")); + if (ReKonfig::openNewTabsInForeground()) + { + setCurrentWidget(tab); + } + break; + case Rekonq::NewBackGroundTab: + tab = prepareNewTab(); + _openedTabsCounter++; + insertTab(currentIndex() + _openedTabsCounter, tab, i18n("new tab")); + break; + + case Rekonq::NewFocusedTab: + tab = prepareNewTab(); + _openedTabsCounter++; + insertTab(currentIndex() + _openedTabsCounter, tab, i18n("new tab")); + setCurrentWidget(tab); + break; + + case Rekonq::NewWindow: + case Rekonq::NewPrivateWindow: + rApp->loadUrl(url, type); + return; + + case Rekonq::CurrentTab: + default: + tab = currentWebWindow(); + break; + }; + + tab->load(url); + + if (history) + { + history->applyHistory(tab->page()->history()); + } +} + + +void TabWidget::pageCreated(WebPage *page) +{ + WebWindow *tab = prepareNewTab(page); + + // Now, the dirty jobs... + _openedTabsCounter++; + insertTab(currentIndex() + _openedTabsCounter, tab, i18n("new tab")); + + setCurrentWidget(tab); +} + + +void TabWidget::currentChanged(int newIndex) +{ + _openedTabsCounter = 0; + + tabBar()->setTabHighlighted(newIndex, false); + + // update window title & icon + WebWindow* tab = webWindow(newIndex); + if (!tab) + return; + + QString t = tab->title(); + + (t.isEmpty() || t == QL1S("rekonq")) + ? setWindowTitle(QL1S("rekonq")) + : setWindowTitle(t + QL1S(" - rekonq")); + + tab->checkFocus(); +} + + +void TabWidget::updateNewTabButtonPosition() +{ + if (isFullScreen()) + return; + + setUpdatesEnabled(false); + + int tabWidgetWidth = frameSize().width(); + int tabBarWidth = tabBar()->sizeHint().width(); + + if (tabBarWidth + _addTabButton->width() > tabWidgetWidth) + { + setCornerWidget(_addTabButton); + } + else + { + setCornerWidget(0); + _addTabButton->move(tabBarWidth, 0); + } + + _addTabButton->show(); + setUpdatesEnabled(true); +} + + +void TabWidget::tabTitleChanged(const QString &title) +{ + WebWindow *tab = qobject_cast(sender()); + if (!tab) + return; + + QString tabTitle = title.isEmpty() ? tab->title() : title; + tabTitle.replace('&', "&&"); + + int index = indexOf(tab); + + if (-1 != index && !tabBar()->tabData(index).toBool()) + { + setTabText(index, tabTitle); + } + + if (currentIndex() != index) + { + tabBar()->setTabHighlighted(index, true); + } + else + { + setWindowTitle(tabTitle + QL1S(" - rekonq")); + } + + if (ReKonfig::hoveringTabOption() == 1) + tabBar()->setTabToolTip(index, tabTitle.remove('&')); +} + + +void TabWidget::tabUrlChanged(const QUrl &url) +{ + WebWindow *tab = qobject_cast(sender()); + if (!tab) + return; + + int index = indexOf(tab); + if (ReKonfig::hoveringTabOption() == 2) + tabBar()->setTabToolTip(index, url.toString()); +} + + +void TabWidget::tabIconChanged() +{ + WebWindow *tab = qobject_cast(sender()); + if (!tab) + return; + + if (tab->isLoading()) + return; + + int index = indexOf(tab); + + if (-1 == index) + return; + + QLabel *label = qobject_cast(tabBar()->tabButton(index, QTabBar::LeftSide)); + if (!label) + { + label = new QLabel(this); + tabBar()->setTabButton(index, QTabBar::LeftSide, 0); + tabBar()->setTabButton(index, QTabBar::LeftSide, label); + } + + KIcon ic = IconManager::self()->iconForUrl(tab->url()); + label->setPixmap(ic.pixmap(16, 16)); +} + + +void TabWidget::tabLoadStarted() +{ + WebWindow *tab = qobject_cast(sender()); + if (!tab) + return; + + int index = indexOf(tab); + if (index != -1) + { + QLabel *label = qobject_cast(tabBar()->tabButton(index, QTabBar::LeftSide)); + if (!label) + { + label = new QLabel(this); + } + + if (!label->movie()) + { + static QString loadingGitPath = KStandardDirs::locate("appdata" , "pics/loading.mng"); + + QMovie *movie = new QMovie(loadingGitPath, QByteArray(), label); + movie->setSpeed(50); + label->setMovie(movie); + movie->start(); + } + + tabBar()->setTabButton(index, QTabBar::LeftSide, 0); + tabBar()->setTabButton(index, QTabBar::LeftSide, label); + + if (!tabBar()->tabData(index).toBool()) + tabBar()->setTabText(index, i18n("Loading...")); + else + { + tabBar()->tabButton(index, QTabBar::RightSide)->hide(); // NOTE: not really good this, but..."Repetita iuvant"!!! + } + } +} + + +void TabWidget::tabLoadFinished(bool ok) +{ + Q_UNUSED(ok); + + WebWindow *tab = qobject_cast(sender()); + if (!tab) + return; + + int index = indexOf(tab); + + if (-1 == index) + return; + + QLabel *label = qobject_cast(tabBar()->tabButton(index, QTabBar::LeftSide)); + if (!label) + { + label = new QLabel(this); + tabBar()->setTabButton(index, QTabBar::LeftSide, 0); + tabBar()->setTabButton(index, QTabBar::LeftSide, label); + } + + QMovie *movie = label->movie(); + if (movie) + { + movie->stop(); + delete movie; + } + + label->setMovie(0); + + KIcon ic = IconManager::self()->iconForUrl(tab->url()); + label->setPixmap(ic.pixmap(16, 16)); + + if (!tabBar()->tabData(index).toBool()) + { + setTabText(index, tab->title()); + } + else + { + setTabText(index, QString()); + } + + if (index == currentIndex()) + tab->checkFocus(); +} + + +void TabWidget::cloneTab(int index) +{ + if (index < 0) + index = currentIndex(); + if (index < 0 || index >= count()) + return; + + QUrl u = webWindow(index)->url(); + QWebHistory* history = webWindow(index)->page()->history(); + TabHistory clonedHistory(history); + + loadUrl(u, Rekonq::NewTab, &clonedHistory); +} + + +void TabWidget::closeTab(int index, bool del) +{ + if (index < 0) + index = currentIndex(); + if (index < 0 || index >= count()) + return; + + WebWindow *tabToClose = webWindow(index); + if (!tabToClose) + return; + + // what to do if there is just one tab... + if (count() == 1) + { + kDebug() << "CANNOT CLOSE WINDOW FROM HERE..."; + currentWebWindow()->load(KUrl("about:home")); + return; + } + + if (!tabToClose->url().isEmpty() + && tabToClose->url().scheme() != QL1S("about") + && !tabToClose->page()->settings()->testAttribute(QWebSettings::PrivateBrowsingEnabled) + ) + { + const int recentlyClosedTabsLimit = 8; + TabHistory history(tabToClose->page()->history()); + history.title = tabToClose->title(); + history.url = tabToClose->url().url(); + history.position = index; + + m_recentlyClosedTabs.removeAll(history); + if (m_recentlyClosedTabs.count() == recentlyClosedTabsLimit) + m_recentlyClosedTabs.removeLast(); + m_recentlyClosedTabs.prepend(history); + } + + removeTab(index); + + if (del) + { + tabToClose->deleteLater(); + } +} + + +void TabWidget::closeOtherTabs(int index) +{ + if (index < 0) + index = currentIndex(); + if (index < 0 || index >= count()) + return; + + for (int i = count() - 1; i > index; --i) + { + closeTab(i); + } + + for (int i = index - 1; i >= 0; --i) + { + closeTab(i); + } +} + + +void TabWidget::detachTab(int index, RekonqWindow *toWindow) +{ + if (index < 0) + index = currentIndex(); + if (index < 0 || index >= count()) + return; + + WebWindow *tab = webWindow(index); + KUrl u = tab->url(); + if (u.scheme() == QL1S("about")) + { + closeTab(index); + loadUrl(u, Rekonq::NewWindow); + return; + } + // else + + closeTab(index, false); + + RekonqWindow *w = 0; + w = (toWindow == 0) + ? new RekonqWindow(false) + : toWindow; + + TabWidget *hostTabWidget = w->tabWidget(); + + hostTabWidget->addTab(tab, tab->title()); + hostTabWidget->setCurrentWidget(tab); + + // disconnect signals from old tabwindow + // WARNING: Code copied from prepareNewTab method. + // Any new changes there should be applied here... + disconnect(tab, SIGNAL(titleChanged(QString)), this, SLOT(tabTitleChanged(QString))); + disconnect(tab, SIGNAL(iconChanged()), this, SLOT(tabIconChanged())); + disconnect(tab, SIGNAL(loadStarted()), this, SLOT(tabLoadStarted())); + disconnect(tab, SIGNAL(loadFinished(bool)), this, SLOT(tabLoadFinished(bool))); + disconnect(tab, SIGNAL(pageCreated(WebPage*)), this, SLOT(pageCreated(WebPage*))); + + // reconnect signals to new tabwindow + // WARNING: Code copied from prepareNewTab method. + // Any new changes there should be applied here... + connect(tab, SIGNAL(titleChanged(QString)), hostTabWidget, SLOT(tabTitleChanged(QString))); + connect(tab, SIGNAL(iconChanged()), hostTabWidget, SLOT(tabIconChanged())); + connect(tab, SIGNAL(loadStarted()), hostTabWidget, SLOT(tabLoadStarted())); + connect(tab, SIGNAL(loadFinished(bool)), hostTabWidget, SLOT(tabLoadFinished(bool))); + connect(tab, SIGNAL(pageCreated(WebPage*)), hostTabWidget, SLOT(pageCreated(WebPage*))); + + w->show(); +} + + +void TabWidget::reloadTab(int index) +{ + // When index is -1 index chooses the current tab + if (index < 0) + index = currentIndex(); + + if (index < 0 || index >= count()) + return; + + WebWindow *reloadingTab = webWindow(index); + QAction *action = reloadingTab->page()->action(QWebPage::Reload); + action->trigger(); +} + + +void TabWidget::reloadAllTabs() +{ + for (int i = 0; i < count(); ++i) + { + reloadTab(i); + } +} + + +void TabWidget::bookmarkAllTabs() +{ + KBookmarkGroup rGroup = BookmarkManager::self()->rootGroup(); + KBookmarkGroup folderGroup = rGroup.createNewFolder(i18n("Bookmarked tabs: ") + QDate::currentDate().toString()); + for (int i = 0; i < count(); ++i) + { + WebWindow *tab = webWindow(i); + KBookmark bk = folderGroup.addBookmark(tab->title(), tab->url()); + } +} + + +void TabWidget::restoreLastClosedTab() +{ + restoreClosedTab(0); +} + + +void TabWidget::restoreClosedTab(int index, bool inNewTab) +{ + if (m_recentlyClosedTabs.isEmpty()) + return; + + if (index >= m_recentlyClosedTabs.count()) + return; + + TabHistory history = m_recentlyClosedTabs.takeAt(index); + + QUrl u = QUrl(history.url); + + int restorePosition = history.position; + + WebWindow *tab; + + if (inNewTab) + { + tab = prepareNewTab(); + if (restorePosition < count()) + insertTab(restorePosition, tab, i18n("restored tab")); + else + addTab(tab, i18n("restored tab")); + + setCurrentWidget(tab); + } + else + { + tab = currentWebWindow(); + } + + tab->load(u); + + // just to get sure... + m_recentlyClosedTabs.removeAll(history); +} + + +void TabWidget::nextTab() +{ + int next = currentIndex() + 1; + if (next == count()) + next = 0; + setCurrentIndex(next); +} + + +void TabWidget::previousTab() +{ + int next = currentIndex() - 1; + if (next < 0) + next = count() - 1; + setCurrentIndex(next); +} + + +void TabWidget::setFullScreen(bool makeFullScreen) +{ + tabBar()->setVisible(!makeFullScreen); + _addTabButton->setVisible(!makeFullScreen); + + KToggleFullScreenAction::setFullScreen(this, makeFullScreen); + + for (int i = 0; i < count(); i++) + webWindow(i)->setWidgetsHidden(makeFullScreen); +} + + +bool TabWidget::isPrivateBrowsingWindowMode() +{ + return _isPrivateBrowsing; +} + + +void TabWidget::loadFavorite(const int index) +{ + QStringList urls = ReKonfig::previewUrls(); + if (index < 0 || index > urls.length()) + return; + + KUrl url = KUrl(urls.at(index - 1)); + loadUrl(url); + currentWebWindow()->setFocus(); +} + + +// NOTE: For internal purpose only ------------------------------------------------------ + + +int TabWidget::addTab(QWidget *page, const QString &label) +{ + setUpdatesEnabled(false); + int i = KTabWidget::addTab(page, label); + setUpdatesEnabled(true); + + return i; +} + + +int TabWidget::addTab(QWidget *page, const QIcon &icon, const QString &label) +{ + setUpdatesEnabled(false); + int i = KTabWidget::addTab(page, icon, label); + setUpdatesEnabled(true); + + return i; +} + + +int TabWidget::insertTab(int index, QWidget *page, const QString &label) +{ + if (! ReKonfig::openNewTabsNextToCurrent()) + index = -1; + setUpdatesEnabled(false); + int i = KTabWidget::insertTab(index, page, label); + setUpdatesEnabled(true); + + return i; +} + + +int TabWidget::insertTab(int index, QWidget *page, const QIcon &icon, const QString &label) +{ + if (! ReKonfig::openNewTabsNextToCurrent()) + index = -1; + setUpdatesEnabled(false); + int i = KTabWidget::insertTab(index, page, icon, label); + setUpdatesEnabled(true); + + return i; +} + + +// -------------------------------------------------------------------------------------- diff --git a/src/tabwindow/tabwidget.h b/src/tabwindow/tabwidget.h new file mode 100644 index 00000000..9c79df11 --- /dev/null +++ b/src/tabwindow/tabwidget.h @@ -0,0 +1,146 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2012 by Andrea Diamantini +* +* +* 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 . +* +* ============================================================ */ + + +#ifndef TAB_WIDGET +#define TAB_WIDGET + + +// Rekonq Includes +#include "rekonq_defines.h" + +// KDE Includes +#include +#include + +// Forward Declarations +class KUrl; + +class QLabel; +class QToolButton; +class QWebHistory; + +class TabHistory; + +class TabBar; +class WebPage; +class WebWindow; + +class RekonqWindow; + +// -------------------------------------------------------------------------------------- + + +class TabWidget : public KTabWidget +{ + Q_OBJECT + +public: + explicit TabWidget(bool withTab = true, bool PrivateBrowsingMode = false, QWidget *parent = 0); + explicit TabWidget(WebPage *pg, QWidget *parent = 0); + + WebWindow* currentWebWindow() const; + WebWindow* webWindow(int index) const; + + TabBar* tabBar() const; + + bool isPrivateBrowsingWindowMode(); + + virtual KActionCollection *actionCollection() const; + QAction *actionByName(const QString &name); + + QList recentlyClosedTabs(); + void restoreClosedTab(int index, bool inNewTab = true); + + // NOTE: For internal purpose only ------------------------------------------------------ + int addTab(QWidget *page, const QString &label); + int addTab(QWidget *page, const QIcon &icon, const QString &label); + + int insertTab(int index, QWidget *page, const QString &label); + int insertTab(int index, QWidget *page, const QIcon &icon, const QString &label); + // -------------------------------------------------------------------------------------- + +public Q_SLOTS: + void loadUrl(const KUrl &, Rekonq::OpenType type = Rekonq::CurrentTab, TabHistory *history = 0); + void newTab(WebPage *page = 0); + +private: + /** + * Prepares the new WebWindow to be open + */ + WebWindow *prepareNewTab(WebPage *page = 0); + + void init(); + +private Q_SLOTS: + /** + * Updates new tab button position + */ + void updateNewTabButtonPosition(); + + void tabTitleChanged(const QString &); + void tabUrlChanged(const QUrl &); + void tabIconChanged(); + + void tabLoadStarted(); + void tabLoadFinished(bool); + + void pageCreated(WebPage *); + + void currentChanged(int); + + // Indexed slots + void cloneTab(int index = -1); + void closeTab(int index = -1, bool del = true); + void closeOtherTabs(int index = -1); + void detachTab(int index = -1, RekonqWindow *toWindow = 0); + void reloadTab(int index = -1); + + void reloadAllTabs(); + void bookmarkAllTabs(); + + void nextTab(); + void previousTab(); + + void restoreLastClosedTab(); + + void setFullScreen(bool); + + void loadFavorite(const int); + +private: + // the new tab button + QToolButton *_addTabButton; + + int _openedTabsCounter; + + QList m_recentlyClosedTabs; + + bool _isPrivateBrowsing; + + KActionCollection *_ac; +}; + +#endif // TAB_WIDGET diff --git a/src/tabwindow/tabwindow.cpp b/src/tabwindow/tabwindow.cpp deleted file mode 100644 index 2ade757b..00000000 --- a/src/tabwindow/tabwindow.cpp +++ /dev/null @@ -1,831 +0,0 @@ -/* ============================================================ -* -* This file is a part of the rekonq project -* -* Copyright (C) 2012 by Andrea Diamantini -* -* -* 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 . -* -* ============================================================ */ - - -// Self Includes -#include "tabwindow.h" -#include "tabwindow.moc" - -// Auto Includes -#include "rekonq.h" - -// Local Includes -#include "application.h" -#include "webpage.h" -#include "webwindow.h" -#include "tabbar.h" - -#include "tabhistory.h" - -#include "bookmarkmanager.h" -#include "iconmanager.h" -#include "sessionmanager.h" - -// KDE Includes -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include -#include - -// Qt Includes -#include -#include -#include -#include -#include -#include -#include -#include - - -TabWindow::TabWindow(bool withTab, bool PrivateBrowsingMode, QWidget *parent) - : RekonqWindow(parent) - , _addTabButton(new QToolButton(this)) - , _openedTabsCounter(0) - , _isPrivateBrowsing(PrivateBrowsingMode) - , _ac(new KActionCollection(this)) -{ - init(); - - // NOTE: we usually create TabWindow with AT LEAST one tab, but - // in one important case... - if (withTab) - { - WebWindow *tab = prepareNewTab(); - addTab(tab, i18n("new tab")); - setCurrentWidget(tab); - } -} - - -TabWindow::TabWindow(WebPage *pg, QWidget *parent) - : RekonqWindow(parent) - , _addTabButton(new QToolButton(this)) - , _openedTabsCounter(0) - , _isPrivateBrowsing(false) - , _ac(new KActionCollection(this)) -{ - init(); - - WebWindow *tab = prepareNewTab(pg); - addTab(tab, i18n("new tab")); - setCurrentWidget(tab); -} - - -void TabWindow::init() -{ - setContentsMargins(0, 0, 0, 0); - - setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); - - // set mouse tracking for tab previews - setMouseTracking(true); - - // setting tabbar - TabBar *tabBar = new TabBar(this); - setTabBar(tabBar); - - // sets document mode; this removes the frame around the tabs - setDocumentMode(true); - - // connecting tabbar signals - connect(tabBar, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int))); - connect(tabBar, SIGNAL(mouseMiddleClick(int)), this, SLOT(closeTab(int))); - - connect(tabBar, SIGNAL(newTabRequest()), this, SLOT(newTab())); - - connect(tabBar, SIGNAL(cloneTab(int)), this, SLOT(cloneTab(int))); - connect(tabBar, SIGNAL(closeTab(int)), this, SLOT(closeTab(int))); - connect(tabBar, SIGNAL(closeOtherTabs(int)), this, SLOT(closeOtherTabs(int))); - connect(tabBar, SIGNAL(reloadTab(int)), this, SLOT(reloadTab(int))); - connect(tabBar, SIGNAL(detachTab(int)), this, SLOT(detachTab(int))); - - connect(tabBar, SIGNAL(tabLayoutChanged()), this, SLOT(updateNewTabButtonPosition())); - - // ============================== Tab Window Actions ==================================== - _ac->addAssociatedWidget(this); - - KAction* a; - - a = new KAction(KIcon("tab-new"), i18n("New &Tab"), this); - a->setShortcut(KShortcut(Qt::CTRL + Qt::Key_T)); - actionCollection()->addAction(QL1S("new_tab"), a); - connect(a, SIGNAL(triggered(bool)), this, SLOT(newTab())); - - a = new KAction(KIcon("tab-new"), i18n("Open Last Closed Tab"), this); - a->setShortcut(KShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_T)); - actionCollection()->addAction(QL1S("open_last_closed_tab"), a); - connect(a, SIGNAL(triggered(bool)), this, SLOT(restoreLastClosedTab())); - - a = new KAction(KIcon("tab-close"), i18n("&Close Tab"), this); - a->setShortcuts(KStandardShortcut::close()); - actionCollection()->addAction(QL1S("close_tab"), a); - connect(a, SIGNAL(triggered(bool)), this, SLOT(closeTab())); - - a = new KAction(i18n("Show Next Tab"), this); - a->setShortcuts(QApplication::isRightToLeft() ? KStandardShortcut::tabPrev() : KStandardShortcut::tabNext()); - actionCollection()->addAction(QL1S("show_next_tab"), a); - connect(a, SIGNAL(triggered(bool)), this, SLOT(nextTab())); - - a = new KAction(i18n("Show Previous Tab"), this); - a->setShortcuts(QApplication::isRightToLeft() ? KStandardShortcut::tabNext() : KStandardShortcut::tabPrev()); - actionCollection()->addAction(QL1S("show_prev_tab"), a); - connect(a, SIGNAL(triggered(bool)), this, SLOT(previousTab())); - - a = KStandardAction::fullScreen(this, SLOT(setFullScreen(bool)), this, actionCollection()); - KShortcut fullScreenShortcut = KStandardShortcut::fullScreen(); - fullScreenShortcut.setAlternate(Qt::Key_F11); - a->setShortcut(fullScreenShortcut); - - a = new KAction(KIcon("bookmarks"), i18n("Bookmark all tabs"), this); - actionCollection()->addAction(QL1S("bookmark_all_tabs"), a); - connect(a, SIGNAL(triggered(bool)), this, SLOT(bookmarkAllTabs())); - - // ---------------------------------------------------------------------------------------------- - // Add Tab Button - _addTabButton->setDefaultAction(actionByName(QL1S("new_tab"))); - _addTabButton->setAutoRaise(true); - _addTabButton->raise(); - _addTabButton->setToolButtonStyle(Qt::ToolButtonIconOnly); - - connect(this, SIGNAL(currentChanged(int)), this, SLOT(currentChanged(int))); - - // ---------------------------------------------------------------------------------------------- - // shortcuts for quickly switching to a tab - QSignalMapper *tabSignalMapper = new QSignalMapper(this); - for (int i = 0; i < 9; i++) - { - a = new KAction(i18n("Switch to Tab %1", i+1), this); - a->setShortcut(KShortcut(QString("Alt+%1").arg(i+1))); - actionCollection()->addAction(QL1S(QString("switch_tab_" + QString::number(i+1)).toAscii()), a); - connect(a, SIGNAL(triggered(bool)), tabSignalMapper, SLOT(map())); - tabSignalMapper->setMapping(a, i); - } - connect(tabSignalMapper, SIGNAL(mapped(int)), this, SLOT(setCurrentIndex(int))); - - // shortcuts for loading favorite pages - QSignalMapper *favoritesSignalMapper = new QSignalMapper(this); - for (int i = 1; i <= 9; ++i) - { - a = new KAction(i18n("Switch to Favorite Page %1", i), this); - a->setShortcut(KShortcut(QString("Ctrl+%1").arg(i))); - actionCollection()->addAction(QL1S(QString("switch_favorite_" + QString::number(i)).toAscii()), a); - connect(a, SIGNAL(triggered(bool)), favoritesSignalMapper, SLOT(map())); - favoritesSignalMapper->setMapping(a, i); - } - connect(favoritesSignalMapper, SIGNAL(mapped(int)), this, SLOT(loadFavorite(int))); - - _ac->readSettings(); - - // ---------------------------------------------------------------------------------------------- - int n = rApp->tabWindowList().count() + 1; - QList list = SessionManager::self()->closedSitesForWindow( QL1S("win") + QString::number(n) ); - Q_FOREACH(const TabHistory & tab, list) - { - if (tab.url.startsWith(QL1S("about"))) - continue; - m_recentlyClosedTabs.removeAll(tab); - m_recentlyClosedTabs.prepend(tab); - } -} - - -// ---------------------------------------------------------------------------------------------------- - - -KActionCollection *TabWindow::actionCollection() const -{ - return _ac; -} - - -QAction *TabWindow::actionByName(const QString &name) -{ - return actionCollection()->action(name); -} - - -TabBar *TabWindow::tabBar() const -{ - TabBar *tabBar = qobject_cast(QTabWidget::tabBar()); - return tabBar; -} - - -WebWindow *TabWindow::currentWebWindow() const -{ - return webWindow(currentIndex()); -} - - -WebWindow *TabWindow::webWindow(int index) const -{ - WebWindow *tab = qobject_cast(this->widget(index)); - if (tab) - { - return tab; - } - - kDebug() << "WebWindow with index " << index << "not found. Returning NULL." ; - return 0; -} - - -QList TabWindow::recentlyClosedTabs() -{ - return m_recentlyClosedTabs; -} - - -WebWindow *TabWindow::prepareNewTab(WebPage *page) -{ - WebWindow *tab = new WebWindow(this, _isPrivateBrowsing, page); - - connect(tab, SIGNAL(titleChanged(QString)), this, SLOT(tabTitleChanged(QString))); - connect(tab, SIGNAL(urlChanged(QUrl)), this, SLOT(tabUrlChanged(QUrl))); - connect(tab, SIGNAL(iconChanged()), this, SLOT(tabIconChanged())); - - connect(tab, SIGNAL(loadStarted()), this, SLOT(tabLoadStarted())); - connect(tab, SIGNAL(loadFinished(bool)), this, SLOT(tabLoadFinished(bool))); - - connect(tab, SIGNAL(pageCreated(WebPage*)), this, SLOT(pageCreated(WebPage*))); - - connect(tab, SIGNAL(setFullScreen(bool)), this, SLOT(setFullScreen(bool))); - - return tab; -} - - -void TabWindow::loadUrl(const KUrl &url, Rekonq::OpenType type, TabHistory *history) -{ - WebWindow *tab = 0; - switch (type) - { - case Rekonq::NewTab: - tab = prepareNewTab(); - _openedTabsCounter++; - insertTab(currentIndex() + _openedTabsCounter, tab, i18n("new tab")); - if (ReKonfig::openNewTabsInForeground()) - { - setCurrentWidget(tab); - } - break; - case Rekonq::NewBackGroundTab: - tab = prepareNewTab(); - _openedTabsCounter++; - insertTab(currentIndex() + _openedTabsCounter, tab, i18n("new tab")); - break; - - case Rekonq::NewFocusedTab: - tab = prepareNewTab(); - _openedTabsCounter++; - insertTab(currentIndex() + _openedTabsCounter, tab, i18n("new tab")); - setCurrentWidget(tab); - break; - - case Rekonq::NewWindow: - case Rekonq::NewPrivateWindow: - rApp->loadUrl(url, type); - return; - - case Rekonq::CurrentTab: - default: - tab = currentWebWindow(); - break; - }; - - tab->load(url); - - if (history) - { - history->applyHistory(tab->page()->history()); - } -} - - -void TabWindow::newTab(WebPage *page) -{ - WebWindow *tab = prepareNewTab(page); - addTab(tab, i18n("new tab")); - setCurrentWidget(tab); - - // no need to load an url if we already have a page... - if (page) - return; - - switch (ReKonfig::newTabsBehaviour()) - { - case 0: // new tab page - tab->load(KUrl("about:home")); - break; - case 2: // homepage - tab->load(KUrl(ReKonfig::homePage())); - break; - case 1: // blank page - default: - tab->load(KUrl("about:blank")); - break; - } - -} - - -void TabWindow::pageCreated(WebPage *page) -{ - WebWindow *tab = prepareNewTab(page); - - // Now, the dirty jobs... - _openedTabsCounter++; - insertTab(currentIndex() + _openedTabsCounter, tab, i18n("new tab")); - - setCurrentWidget(tab); -} - - -void TabWindow::currentChanged(int newIndex) -{ - _openedTabsCounter = 0; - - tabBar()->setTabHighlighted(newIndex, false); - - // update window title & icon - WebWindow* tab = webWindow(newIndex); - if (!tab) - return; - - QString t = tab->title(); - - (t.isEmpty() || t == QL1S("rekonq")) - ? setWindowTitle(QL1S("rekonq")) - : setWindowTitle(t + QL1S(" - rekonq")); - - tab->checkFocus(); -} - - -void TabWindow::updateNewTabButtonPosition() -{ - if (isFullScreen()) - return; - - setUpdatesEnabled(false); - - int tabWidgetWidth = frameSize().width(); - int tabBarWidth = tabBar()->sizeHint().width(); - - if (tabBarWidth + _addTabButton->width() > tabWidgetWidth) - { - setCornerWidget(_addTabButton); - } - else - { - setCornerWidget(0); - _addTabButton->move(tabBarWidth, 0); - } - - _addTabButton->show(); - setUpdatesEnabled(true); -} - - -void TabWindow::tabTitleChanged(const QString &title) -{ - WebWindow *tab = qobject_cast(sender()); - if (!tab) - return; - - QString tabTitle = title.isEmpty() ? tab->title() : title; - tabTitle.replace('&', "&&"); - - int index = indexOf(tab); - - if (-1 != index && !tabBar()->tabData(index).toBool()) - { - setTabText(index, tabTitle); - } - - if (currentIndex() != index) - { - tabBar()->setTabHighlighted(index, true); - } - else - { - setWindowTitle(tabTitle + QL1S(" - rekonq")); - } - - if (ReKonfig::hoveringTabOption() == 1) - tabBar()->setTabToolTip(index, tabTitle.remove('&')); -} - - -void TabWindow::tabUrlChanged(const QUrl &url) -{ - WebWindow *tab = qobject_cast(sender()); - if (!tab) - return; - - int index = indexOf(tab); - if (ReKonfig::hoveringTabOption() == 2) - tabBar()->setTabToolTip(index, url.toString()); -} - - -void TabWindow::tabIconChanged() -{ - WebWindow *tab = qobject_cast(sender()); - if (!tab) - return; - - if (tab->isLoading()) - return; - - int index = indexOf(tab); - - if (-1 == index) - return; - - QLabel *label = qobject_cast(tabBar()->tabButton(index, QTabBar::LeftSide)); - if (!label) - { - label = new QLabel(this); - tabBar()->setTabButton(index, QTabBar::LeftSide, 0); - tabBar()->setTabButton(index, QTabBar::LeftSide, label); - } - - KIcon ic = IconManager::self()->iconForUrl(tab->url()); - label->setPixmap(ic.pixmap(16, 16)); -} - - -void TabWindow::tabLoadStarted() -{ - WebWindow *tab = qobject_cast(sender()); - if (!tab) - return; - - int index = indexOf(tab); - if (index != -1) - { - QLabel *label = qobject_cast(tabBar()->tabButton(index, QTabBar::LeftSide)); - if (!label) - { - label = new QLabel(this); - } - - if (!label->movie()) - { - static QString loadingGitPath = KStandardDirs::locate("appdata" , "pics/loading.mng"); - - QMovie *movie = new QMovie(loadingGitPath, QByteArray(), label); - movie->setSpeed(50); - label->setMovie(movie); - movie->start(); - } - - tabBar()->setTabButton(index, QTabBar::LeftSide, 0); - tabBar()->setTabButton(index, QTabBar::LeftSide, label); - - if (!tabBar()->tabData(index).toBool()) - tabBar()->setTabText(index, i18n("Loading...")); - else - { - tabBar()->tabButton(index, QTabBar::RightSide)->hide(); // NOTE: not really good this, but..."Repetita iuvant"!!! - } - } -} - - -void TabWindow::tabLoadFinished(bool ok) -{ - Q_UNUSED(ok); - - WebWindow *tab = qobject_cast(sender()); - if (!tab) - return; - - int index = indexOf(tab); - - if (-1 == index) - return; - - QLabel *label = qobject_cast(tabBar()->tabButton(index, QTabBar::LeftSide)); - if (!label) - { - label = new QLabel(this); - tabBar()->setTabButton(index, QTabBar::LeftSide, 0); - tabBar()->setTabButton(index, QTabBar::LeftSide, label); - } - - QMovie *movie = label->movie(); - if (movie) - { - movie->stop(); - delete movie; - } - - label->setMovie(0); - - KIcon ic = IconManager::self()->iconForUrl(tab->url()); - label->setPixmap(ic.pixmap(16, 16)); - - if (!tabBar()->tabData(index).toBool()) - { - setTabText(index, tab->title()); - } - else - { - setTabText(index, QString()); - } - - if (index == currentIndex()) - tab->checkFocus(); -} - - -void TabWindow::cloneTab(int index) -{ - if (index < 0) - index = currentIndex(); - if (index < 0 || index >= count()) - return; - - QUrl u = webWindow(index)->url(); - QWebHistory* history = webWindow(index)->page()->history(); - TabHistory clonedHistory(history); - - loadUrl(u, Rekonq::NewTab, &clonedHistory); -} - - -void TabWindow::closeTab(int index, bool del) -{ - if (index < 0) - index = currentIndex(); - if (index < 0 || index >= count()) - return; - - WebWindow *tabToClose = webWindow(index); - if (!tabToClose) - return; - - // what to do if there is just one tab... - if (count() == 1) - { - kDebug() << "CANNOT CLOSE WINDOW FROM HERE..."; - currentWebWindow()->load(KUrl("about:home")); - return; - } - - if (!tabToClose->url().isEmpty() - && tabToClose->url().scheme() != QL1S("about") - && !tabToClose->page()->settings()->testAttribute(QWebSettings::PrivateBrowsingEnabled) - ) - { - const int recentlyClosedTabsLimit = 8; - TabHistory history(tabToClose->page()->history()); - history.title = tabToClose->title(); - history.url = tabToClose->url().url(); - history.position = index; - - m_recentlyClosedTabs.removeAll(history); - if (m_recentlyClosedTabs.count() == recentlyClosedTabsLimit) - m_recentlyClosedTabs.removeLast(); - m_recentlyClosedTabs.prepend(history); - } - - removeTab(index); - - if (del) - { - tabToClose->deleteLater(); - } -} - - -void TabWindow::closeOtherTabs(int index) -{ - if (index < 0) - index = currentIndex(); - if (index < 0 || index >= count()) - return; - - for (int i = count() - 1; i > index; --i) - { - closeTab(i); - } - - for (int i = index - 1; i >= 0; --i) - { - closeTab(i); - } -} - - -void TabWindow::detachTab(int index, TabWindow *toWindow) -{ - if (index < 0) - index = currentIndex(); - if (index < 0 || index >= count()) - return; - - WebWindow *tab = webWindow(index); - KUrl u = tab->url(); - if (u.scheme() == QL1S("about")) - { - closeTab(index); - loadUrl(u, Rekonq::NewWindow); - return; - } - // else - - closeTab(index, false); - - TabWindow *w = 0; - w = (toWindow == 0) - ? new TabWindow(false) - : toWindow; - - w->addTab(tab, tab->title()); - w->setCurrentWidget(tab); - - // disconnect signals from old tabwindow - // WARNING: Code copied from prepareNewTab method. - // Any new changes there should be applied here... - disconnect(tab, SIGNAL(titleChanged(QString)), this, SLOT(tabTitleChanged(QString))); - disconnect(tab, SIGNAL(iconChanged()), this, SLOT(tabIconChanged())); - disconnect(tab, SIGNAL(loadStarted()), this, SLOT(tabLoadStarted())); - disconnect(tab, SIGNAL(loadFinished(bool)), this, SLOT(tabLoadFinished(bool))); - disconnect(tab, SIGNAL(pageCreated(WebPage*)), this, SLOT(pageCreated(WebPage*))); - - // reconnect signals to new tabwindow - // WARNING: Code copied from prepareNewTab method. - // Any new changes there should be applied here... - connect(tab, SIGNAL(titleChanged(QString)), w, SLOT(tabTitleChanged(QString))); - connect(tab, SIGNAL(iconChanged()), w, SLOT(tabIconChanged())); - connect(tab, SIGNAL(loadStarted()), w, SLOT(tabLoadStarted())); - connect(tab, SIGNAL(loadFinished(bool)), w, SLOT(tabLoadFinished(bool))); - connect(tab, SIGNAL(pageCreated(WebPage*)), w, SLOT(pageCreated(WebPage*))); - - w->show(); -} - - -void TabWindow::reloadTab(int index) -{ - // When index is -1 index chooses the current tab - if (index < 0) - index = currentIndex(); - - if (index < 0 || index >= count()) - return; - - WebWindow *reloadingTab = webWindow(index); - QAction *action = reloadingTab->page()->action(QWebPage::Reload); - action->trigger(); -} - - -void TabWindow::reloadAllTabs() -{ - for (int i = 0; i < count(); ++i) - { - reloadTab(i); - } -} - - -void TabWindow::bookmarkAllTabs() -{ - KBookmarkGroup rGroup = BookmarkManager::self()->rootGroup(); - KBookmarkGroup folderGroup = rGroup.createNewFolder(i18n("Bookmarked tabs: ") + QDate::currentDate().toString()); - for (int i = 0; i < count(); ++i) - { - WebWindow *tab = webWindow(i); - KBookmark bk = folderGroup.addBookmark(tab->title(), tab->url()); - } -} - - -void TabWindow::restoreLastClosedTab() -{ - restoreClosedTab(0); -} - - -void TabWindow::restoreClosedTab(int index, bool inNewTab) -{ - if (m_recentlyClosedTabs.isEmpty()) - return; - - if (index >= m_recentlyClosedTabs.count()) - return; - - TabHistory history = m_recentlyClosedTabs.takeAt(index); - - QUrl u = QUrl(history.url); - - int restorePosition = history.position; - - WebWindow *tab; - - if (inNewTab) - { - tab = prepareNewTab(); - if (restorePosition < count()) - insertTab(restorePosition, tab, i18n("restored tab")); - else - addTab(tab, i18n("restored tab")); - - setCurrentWidget(tab); - } - else - { - tab = currentWebWindow(); - } - - tab->load(u); - - // just to get sure... - m_recentlyClosedTabs.removeAll(history); -} - - -void TabWindow::nextTab() -{ - int next = currentIndex() + 1; - if (next == count()) - next = 0; - setCurrentIndex(next); -} - - -void TabWindow::previousTab() -{ - int next = currentIndex() - 1; - if (next < 0) - next = count() - 1; - setCurrentIndex(next); -} - - -void TabWindow::setFullScreen(bool makeFullScreen) -{ - tabBar()->setVisible(!makeFullScreen); - _addTabButton->setVisible(!makeFullScreen); - - KToggleFullScreenAction::setFullScreen(this, makeFullScreen); - - for (int i = 0; i < count(); i++) - webWindow(i)->setWidgetsHidden(makeFullScreen); -} - - -bool TabWindow::isPrivateBrowsingWindowMode() -{ - return _isPrivateBrowsing; -} - - -void TabWindow::loadFavorite(const int index) -{ - QStringList urls = ReKonfig::previewUrls(); - if (index < 0 || index > urls.length()) - return; - - KUrl url = KUrl(urls.at(index - 1)); - loadUrl(url); - currentWebWindow()->setFocus(); -} diff --git a/src/tabwindow/tabwindow.h b/src/tabwindow/tabwindow.h deleted file mode 100644 index c72c252b..00000000 --- a/src/tabwindow/tabwindow.h +++ /dev/null @@ -1,140 +0,0 @@ -/* ============================================================ -* -* This file is a part of the rekonq project -* -* Copyright (C) 2012 by Andrea Diamantini -* -* -* 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 . -* -* ============================================================ */ - - -#ifndef TAB_WINDOW -#define TAB_WINDOW - - -// Rekonq Includes -#include "rekonq_defines.h" - -// Local Includes -#include "rekonqwindow.h" - -// KDE Includes -#include -#include - -// Forward Declarations -class KUrl; - -class QLabel; -class QToolButton; -class QWebHistory; - -class TabHistory; - -class TabBar; -class WebPage; -class WebWindow; - - -// -------------------------------------------------------------------------------------- - - -class TabWindow : public RekonqWindow -{ - Q_OBJECT - -public: - explicit TabWindow(bool withTab = true, bool PrivateBrowsingMode = false, QWidget *parent = 0); - explicit TabWindow(WebPage *pg, QWidget *parent = 0); - - WebWindow* currentWebWindow() const; - WebWindow* webWindow(int index) const; - - TabBar* tabBar() const; - - bool isPrivateBrowsingWindowMode(); - - virtual KActionCollection *actionCollection() const; - QAction *actionByName(const QString &name); - - QList recentlyClosedTabs(); - void restoreClosedTab(int index, bool inNewTab = true); - -public Q_SLOTS: - void loadUrl(const KUrl &, Rekonq::OpenType type = Rekonq::CurrentTab, TabHistory *history = 0); - void newTab(WebPage *page = 0); - -private: - /** - * Prepares the new WebWindow to be open - */ - WebWindow *prepareNewTab(WebPage *page = 0); - - void init(); - -private Q_SLOTS: - /** - * Updates new tab button position - */ - void updateNewTabButtonPosition(); - - void tabTitleChanged(const QString &); - void tabUrlChanged(const QUrl &); - void tabIconChanged(); - - void tabLoadStarted(); - void tabLoadFinished(bool); - - void pageCreated(WebPage *); - - void currentChanged(int); - - // Indexed slots - void cloneTab(int index = -1); - void closeTab(int index = -1, bool del = true); - void closeOtherTabs(int index = -1); - void detachTab(int index = -1, TabWindow *toWindow = 0); - void reloadTab(int index = -1); - - void reloadAllTabs(); - void bookmarkAllTabs(); - - void nextTab(); - void previousTab(); - - void restoreLastClosedTab(); - - void setFullScreen(bool); - - void loadFavorite(const int); - -private: - // the new tab button - QToolButton *_addTabButton; - - int _openedTabsCounter; - - QList m_recentlyClosedTabs; - - bool _isPrivateBrowsing; - - KActionCollection *_ac; -}; - -#endif // TAB_WINDOW -- cgit v1.2.1