Use a custom tool bar.

We don't want a titlebar and we want a custom toolbar instead.
This commit is contained in:
Matthieu Gautier 2018-07-17 11:52:18 +02:00
parent b1dbddf127
commit e16a625f4c
5 changed files with 100 additions and 19 deletions

View File

@ -37,7 +37,8 @@ SOURCES += \
blobbuffer.cpp \
kiwixrequestinterceptor.cpp \
kiwixwebview.cpp \
library.cpp
library.cpp \
topwidget.cpp
HEADERS += \
mainwindow.h \
@ -46,7 +47,8 @@ HEADERS += \
blobbuffer.h \
kiwixrequestinterceptor.h \
kiwixwebview.h \
library.h
library.h \
topwidget.h
FORMS += \
mainwindow.ui

View File

@ -10,6 +10,7 @@ MainWindow::MainWindow(QWidget *parent) :
{
ui->setupUi(this);
ui->tabWidget->tabBar()->setExpanding(false);
setWindowFlags(Qt::Window | Qt::CustomizeWindowHint);// | Qt::WindowCloseButtonHint);// | Qt::WindowTitleHint);
}
MainWindow::~MainWindow()

View File

@ -13,6 +13,9 @@
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="unifiedTitleAndToolBarOnMac">
<bool>true</bool>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
@ -30,23 +33,7 @@
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>873</width>
<height>42</height>
</rect>
</property>
<widget class="QMenu" name="menuFichier">
<property name="title">
<string>Fichier</string>
</property>
</widget>
<addaction name="menuFichier"/>
</widget>
<widget class="QToolBar" name="mainToolBar">
<widget class="TopWidget" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
@ -57,6 +44,13 @@
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>TopWidget</class>
<extends>QToolBar</extends>
<header>topwidget.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

55
topwidget.cpp Normal file
View File

@ -0,0 +1,55 @@
#include "topwidget.h"
#include "kiwixapp.h"
#include <QMouseEvent>
TopWidget::TopWidget(QWidget *parent) :
QToolBar(parent),
fullScreen(false)
{
addAction(QIcon(":/icons/back.svg"), "back");
addAction(QIcon(":/icons/forward.svg"), "forward");
addSeparator();
addWidget(&searchEntry);
addSeparator();
addAction(QIcon(":/icons/minimize.svg"), "minimize", parent, SLOT(showMinimized()));
fullScreenAction = addAction(QIcon(":/icons/full-screen-enter.svg"), "fullscreen", this, SLOT(toggleFullScreen()));
normalScreenAction = addAction(QIcon(":/icons/full-screen-exit.svg"), "unfullscreen", this, SLOT(toggleFullScreen()));
normalScreenAction->setVisible(false);
addAction(QIcon(":/icons/close.svg"), "close", parent, SLOT(close()));
setMovable(false);
}
void TopWidget::toggleFullScreen() {
if (fullScreen)
parentWidget()->showNormal();
else
parentWidget()->showFullScreen();
fullScreen = !fullScreen;
fullScreenAction->setVisible(!fullScreen);
normalScreenAction->setVisible(fullScreen);
}
void TopWidget::mousePressEvent(QMouseEvent *event) {
if(event->button() != Qt::LeftButton)
return;
m_pCursor = event->globalPos() + frameGeometry().topLeft() - parentWidget()->frameGeometry().topLeft();
timestamp = event->timestamp();
event->accept();
}
void TopWidget::mouseMoveEvent(QMouseEvent *event) {
if(event->timestamp() <= timestamp)
return;
timestamp = event->timestamp();
auto delta = event->globalPos() - m_pCursor;
parentWidget()->move(delta);
event->accept();
}

29
topwidget.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef TOPWIDGET_H
#define TOPWIDGET_H
#include <QToolBar>
#include <QLineEdit>
class TopWidget : public QToolBar
{
Q_OBJECT
public:
explicit TopWidget(QWidget *parent = nullptr);
protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
private:
QLineEdit searchEntry;
QAction* fullScreenAction;
QAction* normalScreenAction;
bool fullScreen;
QPoint m_pCursor;
ulong timestamp;
protected slots:
void toggleFullScreen();
};
#endif // TOPWIDGET_H