The sidebar must be under the tabbar.

The tabbar must use the whole length and must not be pushed out by the
sidebar.

So we need to split the tabwidget into a tabbar and a stackedWidget.
This commit is contained in:
Matthieu Gautier 2018-10-18 16:43:00 +02:00
parent 3410252837
commit 300540d4b8
10 changed files with 202 additions and 151 deletions

View File

@ -49,7 +49,6 @@ SOURCES += \
src/topwidget.cpp \
src/requestinterceptor.cpp \
src/urlschemehandler.cpp \
src/tabwidget.cpp \
src/webview.cpp \
src/searchbar.cpp \
src/mainmenu.cpp \
@ -58,6 +57,7 @@ SOURCES += \
src/tocsidebar.cpp \
src/contentmanager.cpp \
src/contentmanagerview.cpp \
src/tabbar.cpp \
HEADERS += \
src/mainwindow.h \
@ -68,7 +68,6 @@ HEADERS += \
src/kconstants.h \
src/requestinterceptor.h \
src/urlschemehandler.h \
src/tabwidget.h \
src/webview.h \
src/searchbar.h \
src/mainmenu.h \
@ -77,6 +76,7 @@ HEADERS += \
src/tocsidebar.h \
src/contentmanager.h \
src/contentmanagerview.h \
src/tabbar.h \
FORMS += \
ui/mainwindow.ui \

View File

@ -71,7 +71,7 @@ KiwixApp::KiwixApp(int& argc, char *argv[])
createAction();
mp_mainWindow = new MainWindow;
mp_tabWidget = mp_mainWindow->getTabWidget();
mp_tabWidget = mp_mainWindow->getTabBar();
mp_tabWidget->setContentManagerView(m_manager.getView());
postInit();
@ -143,6 +143,20 @@ void KiwixApp::openUrl(const QUrl &url, bool newTab) {
mp_tabWidget->openUrl(url, newTab);
}
void KiwixApp::setSideBar(KiwixApp::SideBarType type)
{
auto sideDockWidget = mp_mainWindow->getSideDockWidget();
switch(type) {
case SEARCH_BAR:
sideDockWidget->setCurrentIndex(type);
sideDockWidget->show();
break;
case NONE:
sideDockWidget->hide();
break;
}
}
void KiwixApp::openRandomUrl(bool newTab)
{
auto zimId = mp_tabWidget->currentZimId();
@ -229,6 +243,8 @@ void KiwixApp::createAction()
CREATE_ACTION(FindInPageAction, tr("Find in page"));
SET_SHORTCUT(FindInPageAction, QKeySequence::Find);
connect(mpa_actions[FindInPageAction], &QAction::triggered,
this, [=]() { setSideBar(SEARCH_BAR); });
CREATE_ACTION_ICON(ToggleFullscreenAction, "full-screen-enter", tr("Set fullScreen"));
SET_SHORTCUT(ToggleFullscreenAction, QKeySequence::FullScreen);
@ -286,10 +302,5 @@ void KiwixApp::createAction()
}
void KiwixApp::postInit() {
auto realToggleAction = mp_mainWindow->getSideDockWidget()->toggleViewAction();
auto proxyToggleAction = mpa_actions[FindInPageAction];
connect(proxyToggleAction, &QAction::triggered, realToggleAction, &QAction::trigger);
connect(realToggleAction, &QAction::toggled, proxyToggleAction, &QAction::setChecked);
realToggleAction->toggle();
emit(m_library.booksChanged());
}

View File

@ -5,7 +5,7 @@
#include "contentmanager.h"
#include "mainwindow.h"
#include "kiwix/downloader.h"
#include "tabwidget.h"
#include "tabbar.h"
#include "tocsidebar.h"
#include "urlschemehandler.h"
#include "requestinterceptor.h"
@ -49,6 +49,10 @@ public:
ExitAction,
MAX_ACTION
};
enum SideBarType {
SEARCH_BAR,
NONE
};
KiwixApp(int& argc, char *argv[]);
virtual ~KiwixApp();
@ -63,13 +67,14 @@ public:
Library* getLibrary() { return &m_library; }
MainWindow* getMainWindow() { return mp_mainWindow; }
kiwix::Downloader* getDownloader() { return &m_downloader; }
TabWidget* getTabWidget() { return mp_tabWidget; }
TabBar* getTabWidget() { return mp_tabWidget; }
QAction* getAction(Actions action);
public slots:
void openZimFile(const QString& zimfile="");
void openUrl(const QString& url, bool newTab=true);
void openUrl(const QUrl& url, bool newTab=true);
void setSideBar(SideBarType type);
void printPage();
protected:
@ -82,7 +87,7 @@ private:
kiwix::Downloader m_downloader;
ContentManager m_manager;
MainWindow* mp_mainWindow;
TabWidget* mp_tabWidget;
TabBar* mp_tabWidget;
QErrorMessage* mp_errorDialog;
UrlSchemeHandler m_schemeHandler;

View File

@ -14,7 +14,8 @@ MainWindow::MainWindow(QWidget *parent) :
mp_about(new About(this))
{
mp_ui->setupUi(this);
mp_ui->tabWidget->tabBar()->setExpanding(false);
mp_ui->tabBar->setExpanding(false);
mp_ui->tabBar->setStackedWidget(mp_ui->mainView);
auto app = KiwixApp::instance();
connect(app->getAction(KiwixApp::ExitAction), &QAction::triggered,
this, &QMainWindow::close);
@ -39,12 +40,12 @@ void MainWindow::toggleFullScreen() {
showFullScreen();
}
TabWidget* MainWindow::getTabWidget()
TabBar* MainWindow::getTabBar()
{
return mp_ui->tabWidget;
return mp_ui->tabBar;
}
QDockWidget* MainWindow::getSideDockWidget()
QStackedWidget *MainWindow::getSideDockWidget()
{
return mp_ui->sideDockWidget;
return mp_ui->sideBar;
}

View File

@ -4,7 +4,7 @@
#include <QMainWindow>
#include <QDockWidget>
#include "webview.h"
#include "tabwidget.h"
#include "tabbar.h"
#include "tocsidebar.h"
#include "about.h"
@ -20,8 +20,8 @@ public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
TabWidget* getTabWidget();
QDockWidget* getSideDockWidget();
TabBar* getTabBar();
QStackedWidget* getSideDockWidget();
protected slots:
void toggleFullScreen();

View File

@ -1,4 +1,4 @@
#include "tabwidget.h"
#include "tabbar.h"
#include "kiwixapp.h"
#include <QAction>
@ -7,15 +7,15 @@
#define QUITIFNOTCURRENT(VIEW) if((VIEW)!=currentWidget()) {return;}
#define CURRENTIFNULL(VIEW) if(nullptr==VIEW) { VIEW = currentWidget();}
TabWidget::TabWidget(QWidget *parent) :
QTabWidget(parent)
TabBar::TabBar(QWidget *parent) :
QTabBar(parent)
{
setTabsClosable(true);
setElideMode(Qt::ElideNone);
setDocumentMode(true);
setFocusPolicy(Qt::NoFocus);
connect(this, &QTabWidget::tabCloseRequested, this, &TabWidget::closeTab);
connect(this, &QTabWidget::currentChanged, this, &TabWidget::onCurrentChanged);
connect(this, &QTabBar::tabCloseRequested, this, &TabBar::closeTab);
connect(this, &QTabBar::currentChanged, this, &TabBar::onCurrentChanged);
auto app = KiwixApp::instance();
connect(app->getAction(KiwixApp::NewTabAction), &QAction::triggered,
this, [=]() {
@ -31,6 +31,10 @@ TabWidget::TabWidget(QWidget *parent) :
return;
}
this->closeTab(index);
auto widget = mp_stackedWidget->widget(index);
mp_stackedWidget->removeWidget(widget);
widget->setParent(nullptr);
delete widget;
});
connect(app->getAction(KiwixApp::ZoomInAction), &QAction::triggered,
this, [=]() {
@ -58,13 +62,22 @@ TabWidget::TabWidget(QWidget *parent) :
});
}
void TabWidget::setContentManagerView(ContentManagerView* view)
{
mp_contentManagerView = view;
addTab(mp_contentManagerView, "");
void TabBar::setStackedWidget(QStackedWidget *widget) {
mp_stackedWidget = widget;
connect(this, &QTabBar::currentChanged,
widget, &QStackedWidget::setCurrentIndex);
}
WebView* TabWidget::createNewTab(bool setCurrent)
void TabBar::setContentManagerView(ContentManagerView* view)
{
qInfo() << "add widget";
mp_contentManagerView = view;
mp_stackedWidget->addWidget(mp_contentManagerView);
mp_stackedWidget->show();
addTab("contentManager");
}
WebView* TabBar::createNewTab(bool setCurrent)
{
WebView* webView = new WebView();
connect(webView, &WebView::titleChanged, this,
@ -78,14 +91,15 @@ WebView* TabWidget::createNewTab(bool setCurrent)
}
);
// Ownership of webview is passed to the tabWidget
addTab(webView, "");
mp_stackedWidget->addWidget(webView);
auto index = addTab("");
if (setCurrent) {
setCurrentWidget(webView);
setCurrentIndex(index);
}
return webView;
}
void TabWidget::openUrl(const QUrl& url, bool newTab)
void TabBar::openUrl(const QUrl& url, bool newTab)
{
WebView* webView = currentWidget();
if (newTab || !webView) {
@ -95,31 +109,31 @@ void TabWidget::openUrl(const QUrl& url, bool newTab)
webView->setUrl(url);
}
void TabWidget::setTitleOf(const QString& title, WebView* webView)
void TabBar::setTitleOf(const QString& title, WebView* webView)
{
CURRENTIFNULL(webView);
if (title.startsWith("zim://")) {
auto url = QUrl(title);
setTabText(indexOf(webView), url.path());
setTabText(mp_stackedWidget->indexOf(webView), url.path());
} else {
setTabText(indexOf(webView), title);
setTabText(mp_stackedWidget->indexOf(webView), title);
}
}
void TabWidget::setIconOf(const QIcon &icon, WebView *webView)
void TabBar::setIconOf(const QIcon &icon, WebView *webView)
{
CURRENTIFNULL(webView);
setTabIcon(indexOf(webView), icon);
setTabIcon(mp_stackedWidget->indexOf(webView), icon);
}
QString TabWidget::currentZimId()
QString TabBar::currentZimId()
{
if (!currentWidget())
return "";
return currentWidget()->zimId();
}
void TabWidget::triggerWebPageAction(QWebEnginePage::WebAction action, WebView *webView)
void TabBar::triggerWebPageAction(QWebEnginePage::WebAction action, WebView *webView)
{
CURRENTIFNULL(webView);
QUITIFNULL(webView);
@ -127,7 +141,7 @@ void TabWidget::triggerWebPageAction(QWebEnginePage::WebAction action, WebView *
webView->setFocus();
}
void TabWidget::closeTab(int index)
void TabBar::closeTab(int index)
{
if (index == 0)
return;
@ -137,7 +151,7 @@ void TabWidget::closeTab(int index)
delete webview;
}
void TabWidget::onCurrentChanged(int index)
void TabBar::onCurrentChanged(int index)
{
if (index == -1)
return;

View File

@ -1,22 +1,24 @@
#ifndef TABWIDGET_H
#define TABWIDGET_H
#include <QTableWidget>
#include <QTabBar>
#include <QStackedWidget>
#include <memory>
#include "webview.h"
#include "contentmanagerview.h"
class TabWidget : public QTabWidget
class TabBar : public QTabBar
{
Q_OBJECT
Q_PROPERTY(QString currentZimId READ currentZimId NOTIFY currentZimIdChanged)
public:
TabWidget(QWidget* parent=nullptr);
TabBar(QWidget* parent=nullptr);
void setStackedWidget(QStackedWidget* widget);
void setContentManagerView(ContentManagerView* view);
WebView* createNewTab(bool setCurrent);
WebView* widget(int index) { return (index != 0) ? static_cast<WebView*>(QTabWidget::widget(index)) : nullptr; }
WebView* currentWidget() { auto current = QTabWidget::currentWidget();
WebView* widget(int index) { return (index != 0) ? static_cast<WebView*>(mp_stackedWidget->widget(index)) : nullptr; }
WebView* currentWidget() { auto current = mp_stackedWidget->currentWidget();
if (current == mp_contentManagerView) return nullptr;
return static_cast<WebView*>(current);
}
@ -38,6 +40,7 @@ public slots:
private:
ContentManagerView* mp_contentManagerView;
QStackedWidget* mp_stackedWidget;
};

View File

@ -8,6 +8,8 @@ TocSideBar::TocSideBar(QWidget *parent) :
{
mp_ui->setupUi(this);
mp_findLineEdit = mp_ui->findEdit;
connect(mp_ui->hideButton, &QPushButton::released,
this, [=]() { KiwixApp::instance()->setSideBar(KiwixApp::NONE);});
connect(mp_ui->fNextButton, &QPushButton::released,
this, &TocSideBar::findNext);
connect(mp_ui->fPreviousButton, &QPushButton::released,

View File

@ -20,75 +20,97 @@
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="sizeConstraint">
<enum>QLayout::SetMaximumSize</enum>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout" stretch="1,0,0">
<property name="spacing">
<number>0</number>
</property>
<property name="sizeConstraint">
<enum>QLayout::SetMaximumSize</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLineEdit" name="findEdit"/>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="hideButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Hide</string>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QPushButton" name="fNextButton">
<property name="text">
<string/>
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,0,0">
<property name="spacing">
<number>0</number>
</property>
<property name="icon">
<iconset resource="../resources/kiwix.qrc">
<normaloff>:/icons/search_forward.svg</normaloff>:/icons/search_forward.svg</iconset>
<property name="sizeConstraint">
<enum>QLayout::SetMaximumSize</enum>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
<item>
<widget class="QLineEdit" name="findEdit"/>
</item>
<item>
<widget class="QPushButton" name="fNextButton">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../resources/kiwix.qrc">
<normaloff>:/icons/search_forward.svg</normaloff>:/icons/search_forward.svg</iconset>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="fPreviousButton">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../resources/kiwix.qrc">
<normaloff>:/icons/search_backward.svg</normaloff>:/icons/search_backward.svg</iconset>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QPushButton" name="fPreviousButton">
<property name="text">
<string/>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="icon">
<iconset resource="../resources/kiwix.qrc">
<normaloff>:/icons/search_backward.svg</normaloff>:/icons/search_backward.svg</iconset>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</spacer>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<resources>

View File

@ -24,6 +24,9 @@
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
@ -37,17 +40,40 @@
<number>0</number>
</property>
<item>
<widget class="TabWidget" name="tabWidget">
<property name="tabShape">
<enum>QTabWidget::Rounded</enum>
<widget class="TabBar" name="tabBar" native="true"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="currentIndex">
<number>-1</number>
</property>
<property name="documentMode">
<bool>false</bool>
</property>
</widget>
<item>
<widget class="QStackedWidget" name="sideBar">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="lineWidth">
<number>0</number>
</property>
<widget class="TocSideBar" name="tocsidebar"/>
</widget>
</item>
<item>
<widget class="QStackedWidget" name="mainView"/>
</item>
</layout>
</item>
</layout>
</widget>
@ -60,39 +86,6 @@
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<widget class="QDockWidget" name="sideDockWidget">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="floating">
<bool>false</bool>
</property>
<property name="features">
<set>QDockWidget::DockWidgetClosable</set>
</property>
<property name="allowedAreas">
<set>Qt::LeftDockWidgetArea</set>
</property>
<property name="windowTitle">
<string>Find in page</string>
</property>
<attribute name="dockWidgetArea">
<number>1</number>
</attribute>
<widget class="QWidget" name="dockWidgetContents">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="TocSideBar" name="tocPage" native="true"/>
</item>
</layout>
</widget>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
@ -101,18 +94,18 @@
<extends>QToolBar</extends>
<header>src/topwidget.h</header>
</customwidget>
<customwidget>
<class>TabWidget</class>
<extends>QTabWidget</extends>
<header>src/tabwidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>TocSideBar</class>
<extends>QWidget</extends>
<header>src/tocsidebar.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>TabBar</class>
<extends>QWidget</extends>
<header>src/tabbar.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>