Skip to content

Commit

Permalink
Initial version of LRU tab switcher, fixes #52
Browse files Browse the repository at this point in the history
  • Loading branch information
Emdek committed Feb 28, 2015
1 parent 56f07bc commit 20b0812
Show file tree
Hide file tree
Showing 10 changed files with 335 additions and 60 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -147,6 +147,7 @@ set(otter_src
src/ui/StartupDialog.cpp
src/ui/TabBarToolBarWidget.cpp
src/ui/TabBarWidget.cpp
src/ui/TabSwitcherWidget.cpp
src/ui/TextLabelWidget.cpp
src/ui/TrayIcon.cpp
src/ui/UserAgentsManagerDialog.cpp
Expand Down
2 changes: 2 additions & 0 deletions otter.pro
Expand Up @@ -99,6 +99,7 @@ SOURCES += src/main.cpp \
src/ui/StartupDialog.cpp \
src/ui/TabBarToolBarWidget.cpp \
src/ui/TabBarWidget.cpp \
src/ui/TabSwitcherWidget.cpp \
src/ui/TextLabelWidget.cpp \
src/ui/TrayIcon.cpp \
src/ui/UserAgentsManagerDialog.cpp \
Expand Down Expand Up @@ -209,6 +210,7 @@ HEADERS += src/core/Action.h \
src/ui/StartupDialog.h \
src/ui/TabBarToolBarWidget.h \
src/ui/TabBarWidget.h \
src/ui/TabSwitcherWidget.h \
src/ui/TextLabelWidget.h \
src/ui/TrayIcon.h \
src/ui/UserAgentsManagerDialog.h \
Expand Down
65 changes: 45 additions & 20 deletions src/core/WindowsManager.cpp
Expand Up @@ -236,7 +236,7 @@ void WindowsManager::closeAll()
{
for (int i = (m_mainWindow->getTabBar()->count() - 1); i >= 0; --i)
{
Window *window = getWindow(i);
Window *window = getWindowByIndex(i);

if (window)
{
Expand Down Expand Up @@ -288,14 +288,14 @@ void WindowsManager::restore(const SessionMainWindow &session)
m_isRestored = true;

connect(SessionsManager::getInstance(), SIGNAL(requestedRemoveStoredUrl(QString)), this, SLOT(removeStoredUrl(QString)));
connect(m_mainWindow->getTabBar(), SIGNAL(currentChanged(int)), this, SLOT(setActiveWindow(int)));
connect(m_mainWindow->getTabBar(), SIGNAL(currentChanged(int)), this, SLOT(setActiveWindowByIndex(int)));
connect(m_mainWindow->getTabBar(), SIGNAL(requestedClone(int)), this, SLOT(cloneWindow(int)));
connect(m_mainWindow->getTabBar(), SIGNAL(requestedDetach(int)), this, SLOT(detachWindow(int)));
connect(m_mainWindow->getTabBar(), SIGNAL(requestedPin(int,bool)), this, SLOT(pinWindow(int,bool)));
connect(m_mainWindow->getTabBar(), SIGNAL(requestedClose(int)), this, SLOT(closeWindow(int)));
connect(m_mainWindow->getTabBar(), SIGNAL(requestedCloseOther(int)), this, SLOT(closeOther(int)));

setActiveWindow(session.index);
setActiveWindowByIndex(session.index);
}

void WindowsManager::restore(int index)
Expand Down Expand Up @@ -356,7 +356,7 @@ void WindowsManager::triggerAction(int identifier, bool checked)

for (int i = (m_mainWindow->getTabBar()->count() - 1); i > 0; --i)
{
if (getWindow(i)->isPrivate())
if (getWindowByIndex(i)->isPrivate())
{
closeWindow(i);
}
Expand Down Expand Up @@ -436,7 +436,7 @@ void WindowsManager::addWindow(Window *window, OpenHints hints)

if (m_isRestored)
{
setActiveWindow(index);
setActiveWindowByIndex(index);
}
}

Expand Down Expand Up @@ -477,7 +477,7 @@ void WindowsManager::openWindow(ContentsWidget *widget, OpenHints hints)

void WindowsManager::cloneWindow(int index)
{
Window *window = getWindow(index);
Window *window = getWindowByIndex(index);

if (window && window->canClone())
{
Expand All @@ -487,7 +487,7 @@ void WindowsManager::cloneWindow(int index)

void WindowsManager::detachWindow(int index)
{
Window *window = getWindow(index);
Window *window = getWindowByIndex(index);

if (!window)
{
Expand Down Expand Up @@ -541,7 +541,7 @@ void WindowsManager::closeWindow(int index)
return;
}

Window *window = getWindow(index);
Window *window = getWindowByIndex(index);

if (window)
{
Expand Down Expand Up @@ -590,7 +590,7 @@ void WindowsManager::closeWindow(Window *window)

if (lastTabClosingAction == QLatin1String("openTab"))
{
window = getWindow(0);
window = getWindowByIndex(0);

if (window)
{
Expand Down Expand Up @@ -662,7 +662,7 @@ void WindowsManager::setZoom(int zoom)
}
}

void WindowsManager::setActiveWindow(int index)
void WindowsManager::setActiveWindowByIndex(int index)
{
if (index < 0 || index >= m_mainWindow->getTabBar()->count())
{
Expand All @@ -687,7 +687,7 @@ void WindowsManager::setActiveWindow(int index)

setStatusMessage(QString());

window = getWindow(index);
window = getWindowByIndex(index);

m_mainWindow->getActionsManager()->setCurrentWindow(window);

Expand All @@ -714,6 +714,21 @@ void WindowsManager::setActiveWindow(int index)
emit currentWindowChanged(index);
}

void WindowsManager::setActiveWindowByIdentifier(qint64 identifier)
{
for (int i = 0; i < m_mainWindow->getTabBar()->count(); ++i)
{
Window *window = getWindowByIndex(i);

if (window && window->getIdentifier() == identifier)
{
setActiveWindowByIndex(i);

break;
}
}
}

void WindowsManager::setTitle(const QString &title)
{
const QString text = (title.isEmpty() ? tr("Empty") : title);
Expand Down Expand Up @@ -744,14 +759,24 @@ Action* WindowsManager::getAction(int identifier)
return (window ? window->getContentsWidget()->getAction(identifier) : NULL);
}

Window* WindowsManager::getWindow(int index) const
Window* WindowsManager::getWindowByIndex(int index) const
{
if (index < 0)
return ((index >= m_mainWindow->getTabBar()->count()) ? NULL : qvariant_cast<Window*>(m_mainWindow->getTabBar()->tabData(index)));
}

Window* WindowsManager::getWindowByIdentifier(qint64 identifier) const
{
for (int i = 0; i < m_mainWindow->getTabBar()->count(); ++i)
{
return m_mainWindow->getMdi()->getActiveWindow();
Window *window = getWindowByIndex(i);

if (window && window->getIdentifier() == identifier)
{
return window;
}
}

return ((index >= m_mainWindow->getTabBar()->count()) ? NULL : qvariant_cast<Window*>(m_mainWindow->getTabBar()->tabData(index)));
return NULL;
}

QVariant WindowsManager::getOption(const QString &key) const
Expand Down Expand Up @@ -782,7 +807,7 @@ SessionMainWindow WindowsManager::getSession() const

for (int i = 0; i < m_mainWindow->getTabBar()->count(); ++i)
{
Window *window = getWindow(i);
Window *window = getWindowByIndex(i);

if (window && !window->isPrivate())
{
Expand Down Expand Up @@ -863,7 +888,7 @@ int WindowsManager::getWindowCount(bool onlyPrivate) const

for (int i = 0; i < m_mainWindow->getTabBar()->count(); ++i)
{
if (getWindow(i)->isPrivate())
if (getWindowByIndex(i)->isPrivate())
{
++amount;
}
Expand All @@ -885,7 +910,7 @@ bool WindowsManager::event(QEvent *event)
{
for (int i = 0; i < m_mainWindow->getTabBar()->count(); ++i)
{
Window *window = getWindow(i);
Window *window = getWindowByIndex(i);

if (window)
{
Expand Down Expand Up @@ -923,13 +948,13 @@ bool WindowsManager::hasUrl(const QUrl &url, bool activate)
{
for (int i = 0; i < m_mainWindow->getTabBar()->count(); ++i)
{
Window *window = getWindow(i);
Window *window = getWindowByIndex(i);

if (window && window->getUrl() == url)
{
if (activate)
{
setActiveWindow(i);
setActiveWindowByIndex(i);
}

return true;
Expand Down
6 changes: 4 additions & 2 deletions src/core/WindowsManager.h
Expand Up @@ -65,7 +65,8 @@ class WindowsManager : public QObject
explicit WindowsManager(bool isPrivate, MainWindow *parent);

Action* getAction(int identifier);
Window* getWindow(int index = -1) const;
Window* getWindowByIndex(int index) const;
Window* getWindowByIdentifier(qint64 identifier) const;
QVariant getOption(const QString &key) const;
QString getTitle() const;
QUrl getUrl() const;
Expand All @@ -88,7 +89,8 @@ public slots:
void restore(const SessionMainWindow &session);
void triggerAction(int identifier, bool checked = false);
void clearClosedWindows();
void setActiveWindow(int index);
void setActiveWindowByIndex(int index);
void setActiveWindowByIdentifier(qint64 identifier);
void setOption(const QString &key, const QVariant &value);
void setZoom(int zoom);

Expand Down
81 changes: 68 additions & 13 deletions src/ui/MainWindow.cpp
Expand Up @@ -31,6 +31,7 @@
#include "SaveSessionDialog.h"
#include "SessionsManagerDialog.h"
#include "TabBarToolBarWidget.h"
#include "TabSwitcherWidget.h"
#include "Window.h"
#include "preferences/ContentBlockingDialog.h"
#include "toolbars/ZoomWidget.h"
Expand Down Expand Up @@ -65,6 +66,7 @@ namespace Otter
MainWindow::MainWindow(bool isPrivate, const SessionMainWindow &session, QWidget *parent) : QMainWindow(parent),
m_actionsManager(NULL),
m_windowsManager(NULL),
m_tabSwitcher(NULL),
m_mdiWidget(new MdiWidget(this)),
m_tabBarToolBarWidget(NULL),
m_menuBar(NULL),
Expand Down Expand Up @@ -149,18 +151,6 @@ MainWindow::~MainWindow()
delete m_ui;
}

void MainWindow::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu(this);
menu.addAction(m_actionsManager->getAction(Action::ShowMenuBarAction));
menu.addAction(m_actionsManager->getAction(Action::ShowTabBarAction));
menu.addAction(m_actionsManager->getAction(Action::ShowSidebarAction));
menu.addAction(m_actionsManager->getAction(Action::ShowErrorConsoleAction));
menu.addSeparator();
menu.addAction(m_actionsManager->getAction(Action::LockToolBarsAction));
menu.exec(event->globalPos());
}

void MainWindow::closeEvent(QCloseEvent *event)
{
if (SessionsManager::isLastWindow() && !Application::getInstance()->canClose())
Expand Down Expand Up @@ -191,6 +181,66 @@ void MainWindow::closeEvent(QCloseEvent *event)
event->accept();
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Tab || event->key() == Qt::Key_Backtab)
{
if (m_windowsManager->getWindowCount() > 1)
{
if (!m_tabSwitcher)
{
m_tabSwitcher = new TabSwitcherWidget(m_windowsManager, this);
}

m_tabSwitcher->raise();
m_tabSwitcher->resize(size());
m_tabSwitcher->show();
m_tabSwitcher->selectTab(event->key() == Qt::Key_Tab);
}
else
{
m_windowsManager->triggerAction(((event->key() == Qt::Key_Tab) ? Action::ActivateTabOnRightAction : Action::ActivateTabOnLeftAction), parentWidget());
}

event->accept();
}
else
{
QMainWindow::keyPressEvent(event);
}
}

void MainWindow::keyReleaseEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Control && m_tabSwitcher && m_tabSwitcher->isVisible())
{
m_tabSwitcher->accept();

event->accept();
}
else
{
QMainWindow::keyReleaseEvent(event);
}
}

void MainWindow::contextMenuEvent(QContextMenuEvent *event)
{
if (m_tabSwitcher && m_tabSwitcher->isVisible())
{
return;
}

QMenu menu(this);
menu.addAction(m_actionsManager->getAction(Action::ShowMenuBarAction));
menu.addAction(m_actionsManager->getAction(Action::ShowTabBarAction));
menu.addAction(m_actionsManager->getAction(Action::ShowSidebarAction));
menu.addAction(m_actionsManager->getAction(Action::ShowErrorConsoleAction));
menu.addSeparator();
menu.addAction(m_actionsManager->getAction(Action::LockToolBarsAction));
menu.exec(event->globalPos());
}

void MainWindow::optionChanged(const QString &option, const QVariant &value)
{
if (option == QLatin1String("Network/WorkOffline"))
Expand Down Expand Up @@ -289,7 +339,7 @@ void MainWindow::openUrl(const QString &text)
connect(interpreter, SIGNAL(requestedOpenUrl(QUrl,OpenHints)), m_windowsManager, SLOT(open(QUrl,OpenHints)));
connect(interpreter, SIGNAL(requestedSearch(QString,QString,OpenHints)), m_windowsManager, SLOT(search(QString,QString,OpenHints)));

interpreter->interpret(text, ((m_windowsManager->getWindowCount() == 0 || m_windowsManager->getWindow()->isUrlEmpty()) ? CurrentTabOpen : NewTabOpen));
interpreter->interpret(text, ((!m_mdiWidget->getActiveWindow() || m_mdiWidget->getActiveWindow()->isUrlEmpty()) ? CurrentTabOpen : NewTabOpen));
}
}

Expand Down Expand Up @@ -832,6 +882,11 @@ bool MainWindow::event(QEvent *event)
case QEvent::Resize:
updateSidebars();

if (m_tabSwitcher && m_tabSwitcher->isVisible())
{
m_tabSwitcher->resize(size());
}

SessionsManager::markSessionModified();

break;
Expand Down
6 changes: 5 additions & 1 deletion src/ui/MainWindow.h
Expand Up @@ -43,6 +43,7 @@ class MdiWidget;
class Menu;
class TabBarToolBarWidget;
class TabBarWidget;
class TabSwitcherWidget;
class WindowsManager;

class MainWindow : public QMainWindow
Expand All @@ -67,8 +68,10 @@ public slots:
void triggerAction(int identifier, bool checked = false);

protected:
void contextMenuEvent(QContextMenuEvent *event);
void closeEvent(QCloseEvent *event);
void keyPressEvent(QKeyEvent *event);
void keyReleaseEvent(QKeyEvent *event);
void contextMenuEvent(QContextMenuEvent *event);
void createMenuBar();
void createToggleEdge();
void placeSidebars();
Expand All @@ -85,6 +88,7 @@ protected slots:
private:
ActionsManager *m_actionsManager;
WindowsManager *m_windowsManager;
TabSwitcherWidget *m_tabSwitcher;
MdiWidget *m_mdiWidget;
TabBarToolBarWidget *m_tabBarToolBarWidget;
QMenuBar *m_menuBar;
Expand Down

1 comment on commit 20b0812

@beastie1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic! Good job!!!

Please sign in to comment.