implemented an ability to check if toolbar is floating

This commit is contained in:
RomanPudashkin 2021-02-11 10:52:33 +02:00 committed by Igor Korsukov
parent c5c63150e6
commit 6b0d53af02
2 changed files with 71 additions and 16 deletions

View file

@ -19,10 +19,10 @@
#include "docktoolbar.h"
#include <QToolBar>
#include "eventswatcher.h"
#include <QToolBar>
using namespace mu::dock;
static const qreal TOOLBAR_GRIP_WIDTH(32);
@ -48,6 +48,10 @@ DockToolBar::DockToolBar(QQuickItem* parent)
emit orientationChanged(orientation);
});
connect(m_tool.bar, &QToolBar::topLevelChanged, [this](bool floating) {
setFloating(floating);
});
m_eventsWatcher = new EventsWatcher(this);
m_tool.bar->installEventFilter(m_eventsWatcher);
connect(m_eventsWatcher, &EventsWatcher::eventReceived, this, &DockToolBar::onWidgetEvent);
@ -58,15 +62,20 @@ DockToolBar::~DockToolBar()
delete m_tool.bar;
}
QToolBar* DockToolBar::toolBar() const
{
return m_tool.bar;
}
void DockToolBar::onComponentCompleted()
{
m_tool.bar->setObjectName("w_" + objectName());
toolBar()->setObjectName("w_" + objectName());
updateStyle();
QWidget* widget = view();
widget->setMinimumWidth(minimumWidth());
widget->setMinimumHeight(minimumHeight());
m_tool.bar->addWidget(widget);
toolBar()->addWidget(widget);
}
void DockToolBar::updateStyle()
@ -74,25 +83,25 @@ void DockToolBar::updateStyle()
QString theme = uiConfiguration()->actualThemeType() == ui::IUiConfiguration::ThemeType::LIGHT_THEME
? "light"
: "dark";
m_tool.bar->setStyleSheet(TOOLBAR_QSS.arg(theme, color().name()));
toolBar()->setStyleSheet(TOOLBAR_QSS.arg(theme, color().name()));
}
void DockToolBar::onWidgetEvent(QEvent* e)
void DockToolBar::onWidgetEvent(QEvent* event)
{
if (QEvent::Resize == e->type()) {
QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(e);
if (QEvent::Resize == event->type()) {
QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(event);
resize(resizeEvent->size());
} else if (QEvent::ShowToParent == e->type()) {
resize(m_tool.bar->size());
} else if (QEvent::ShowToParent == event->type()) {
resize(toolBar()->size());
} else {
DockView::onWidgetEvent(e);
DockView::onWidgetEvent(event);
}
}
void DockToolBar::resize(const QSize& size)
{
QSize newSize = size;
if (m_tool.bar->orientation() == Qt::Horizontal) {
if (toolBar()->orientation() == Qt::Horizontal) {
newSize.setWidth(newSize.width() - TOOLBAR_GRIP_WIDTH);
} else {
newSize.setHeight(newSize.height() - TOOLBAR_GRIP_WIDTH);
@ -100,6 +109,16 @@ void DockToolBar::resize(const QSize& size)
view()->resize(newSize);
}
void DockToolBar::setFloating(bool floating)
{
if (m_floating == floating) {
return;
}
m_floating = floating;
emit floatingChanged(floating);
}
DockToolBar::Widget DockToolBar::widget() const
{
return m_tool;
@ -107,7 +126,7 @@ DockToolBar::Widget DockToolBar::widget() const
int DockToolBar::orientation() const
{
return m_tool.bar->orientation();
return toolBar()->orientation();
}
int DockToolBar::minimumHeight() const
@ -122,7 +141,17 @@ int DockToolBar::minimumWidth() const
Qt::ToolBarAreas DockToolBar::allowedAreas() const
{
return widget().bar->allowedAreas();
return toolBar()->allowedAreas();
}
bool DockToolBar::floating() const
{
return m_floating;
}
bool DockToolBar::floatable() const
{
return toolBar()->isFloatable();
}
void DockToolBar::setMinimumHeight(int minimumHeight)
@ -154,6 +183,20 @@ void DockToolBar::setMinimumWidth(int minimumWidth)
void DockToolBar::setAllowedAreas(Qt::ToolBarAreas allowedAreas)
{
widget().bar->setAllowedAreas(allowedAreas);
if (allowedAreas == this->allowedAreas()) {
return;
}
toolBar()->setAllowedAreas(allowedAreas);
emit allowedAreasChanged(allowedAreas);
}
void DockToolBar::setFloatable(bool floatable)
{
if (floatable == this->floatable()) {
return;
}
toolBar()->setFloatable(floatable);
emit floatableChanged(floatable);
}

View file

@ -33,6 +33,8 @@ class DockToolBar : public DockView
Q_PROPERTY(int minimumHeight READ minimumHeight WRITE setMinimumHeight NOTIFY minimumHeightChanged)
Q_PROPERTY(int minimumWidth READ minimumWidth WRITE setMinimumWidth NOTIFY minimumWidthChanged)
Q_PROPERTY(Qt::ToolBarAreas allowedAreas READ allowedAreas WRITE setAllowedAreas NOTIFY allowedAreasChanged)
Q_PROPERTY(bool floating READ floating NOTIFY floatingChanged)
Q_PROPERTY(bool floatable READ floatable WRITE setFloatable NOTIFY floatableChanged)
public:
explicit DockToolBar(QQuickItem* parent = nullptr);
@ -51,31 +53,41 @@ public:
Qt::ToolBarAreas allowedAreas() const;
bool floating() const;
bool floatable() const;
public slots:
void setMinimumHeight(int minimumHeight);
void setMinimumWidth(int minimumWidth);
void setAllowedAreas(Qt::ToolBarAreas allowedAreas);
void setFloatable(bool floatable);
signals:
void orientationChanged(int orientation);
void minimumHeightChanged(int minimumHeight);
void minimumWidthChanged(int minimumWidth);
void allowedAreasChanged(Qt::ToolBarAreas allowedAreas);
void floatingChanged(bool floating);
void floatableChanged(bool floatable);
protected:
void onComponentCompleted() override;
void updateStyle() override;
private slots:
void onWidgetEvent(QEvent* e) override;
void onWidgetEvent(QEvent* event) override;
private:
QToolBar* toolBar() const;
void resize(const QSize& size);
void setFloating(bool floating);
Widget m_tool;
EventsWatcher* m_eventsWatcher = nullptr;
int m_minimumHeight = 0;
int m_minimumWidth = 0;
bool m_floating = false;
};
}