diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 73ed42ee..2118b1fb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -35,6 +35,7 @@ set(DOCKSLIBS_SRCS TitleBar.cpp ObjectViewer.cpp ObjectViewer_p.h + QWidgetAdapter.cpp DebugWindow.cpp DebugWindow_p.h WindowBeingDragged.cpp diff --git a/src/QWidgetAdapter.cpp b/src/QWidgetAdapter.cpp new file mode 100644 index 00000000..f7a4d1bf --- /dev/null +++ b/src/QWidgetAdapter.cpp @@ -0,0 +1,63 @@ +/* This file is part of KDDockWidgets. + + Copyright (C) 2019 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + Author: Sérgio Martins + + 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) any later version. + + 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 . +*/ + +/** + * @file + * @brief A class that is QWidget when building for QtWidgets, and QObject when building for QtQuick. + * + * Allows to have the same code base supporting both stacks. + * + * @author Sérgio Martins \ + */ + +#include "QWidgetAdapter.h" + +#include + +using namespace KDDockWidgets; + +QWidgetAdapter::QWidgetAdapter(QWidget *parent) + : QWidget(parent) +{ +} + +QWidgetAdapter::~QWidgetAdapter() +{ +} + +bool QWidgetAdapter::event(QEvent *e) +{ + if (e->type() == QEvent::LayoutRequest) + onLayoutRequest(); + + return QWidget::event(e); +} + +void QWidgetAdapter::resizeEvent(QResizeEvent *ev) +{ + if (!onResize(ev->oldSize(), ev->size())) + QWidget::resizeEvent(ev); +} + +bool QWidgetAdapter::onResize(QSize, QSize) { return false; } +void QWidgetAdapter::onLayoutRequest() {} diff --git a/src/QWidgetAdapter.h b/src/QWidgetAdapter.h new file mode 100644 index 00000000..754d5d5d --- /dev/null +++ b/src/QWidgetAdapter.h @@ -0,0 +1,56 @@ +/* + This file is part of KDDockWidgets. + + Copyright (C) 2019 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + Author: Sérgio Martins + + 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) any later version. + + 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 . +*/ + +/** + * @file + * @brief A class that is QWidget when building for QtWidgets, and QObject when building for QtQuick. + * + * Allows to have the same code base supporting both stacks. + * + * @author Sérgio Martins \ + */ + +#ifndef KDDOCKWIDGETS_QWIDGETADAPTER_H +#define KDDOCKWIDGETS_QWIDGETADAPTER_H + +#include "docks_export.h" + +#include + +namespace KDDockWidgets { + +class DOCKS_EXPORT QWidgetAdapter : public QWidget // TODO ifdef the base class once we support QtQuick +{ + Q_OBJECT +public: + explicit QWidgetAdapter(QWidget *parent = nullptr); + ~QWidgetAdapter() override; + +protected: + bool event(QEvent *e) override; + void resizeEvent(QResizeEvent *) override; + + virtual bool onResize(QSize oldSize, QSize newSize); + virtual void onLayoutRequest(); +}; + +} + +#endif diff --git a/src/multisplitter/MultiSplitter.cpp b/src/multisplitter/MultiSplitter.cpp index 072be674..82991689 100644 --- a/src/multisplitter/MultiSplitter.cpp +++ b/src/multisplitter/MultiSplitter.cpp @@ -34,12 +34,12 @@ #include "FloatingWindow_p.h" #include "LayoutSaver.h" -#include +#include using namespace KDDockWidgets; MultiSplitter::MultiSplitter(QWidget *parent) - : QWidget(parent) + : QWidgetAdapter(parent) , m_layout(new MultiSplitterLayout(this)) { connect(m_layout, &MultiSplitterLayout::minimumSizeChanged, this, [this] (QSize sz) { @@ -63,29 +63,26 @@ int MultiSplitter::count() const return m_layout->count(); } -void MultiSplitter::resizeEvent(QResizeEvent *ev) +void MultiSplitter::onLayoutRequest() { - qCDebug(sizing) << Q_FUNC_INFO << "; new=" << ev->size() << "; old=" << ev->oldSize() + m_layout->updateSizeConstraints(); +} + +bool MultiSplitter::onResize(QSize oldSize, QSize newSize) +{ + qCDebug(sizing) << Q_FUNC_INFO << "; new=" << newSize << "; old=" << oldSize << "; window=" << window(); - m_inResizeEvent = true; // to avoid re-entrancy + QScopedValueRollback(m_inResizeEvent, true); // to avoid re-entrancy if (!LayoutSaver::restoreInProgress()) { // don't resize anything while we're restoring the layout - m_layout->setContentsSize(ev->size()); + m_layout->setContentsSize(newSize); } - QWidget::resizeEvent(ev); - m_inResizeEvent = false; + return false; // So QWidget::resizeEvent is called } -bool MultiSplitter::event(QEvent *e) -{ - if (e->type() == QEvent::LayoutRequest) - m_layout->updateSizeConstraints(); - - return QWidget::event(e); -} bool MultiSplitter::isInMainWindow() const { diff --git a/src/multisplitter/MultiSplitter_p.h b/src/multisplitter/MultiSplitter_p.h index 1f7719e5..27f3093a 100644 --- a/src/multisplitter/MultiSplitter_p.h +++ b/src/multisplitter/MultiSplitter_p.h @@ -30,7 +30,7 @@ #define KDDOCKWIDGETS_MULTISPLITTER_P_H #include "docks_export.h" -#include +#include "QWidgetAdapter.h" namespace KDDockWidgets { @@ -44,7 +44,7 @@ class FloatingWindow; * * The actual layouting is done by @ref MultiSplitterLayout. */ -class DOCKS_EXPORT_FOR_UNIT_TESTS MultiSplitter : public QWidget +class DOCKS_EXPORT_FOR_UNIT_TESTS MultiSplitter : public QWidgetAdapter { Q_OBJECT public: @@ -56,8 +56,8 @@ public: MainWindow* mainWindow() const; FloatingWindow* floatingWindow() const; protected: - bool event(QEvent *e) override; - void resizeEvent(QResizeEvent *) override; + void onLayoutRequest() override; + bool onResize(QSize oldSize, QSize newSize) override; MultiSplitterLayout *const m_layout; private: bool m_inResizeEvent = false;