diff --git a/src/Config.cpp b/src/Config.cpp index 1f3c5aeb..d0c6e7fc 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -26,9 +26,9 @@ */ #include "Config.h" +#include "multisplitter/Config.h" #include "DockRegistry_p.h" #include "FrameworkWidgetFactory.h" -#include "multisplitter/Separator_p.h" #include #include @@ -68,7 +68,7 @@ Config::Config() return Config::self().frameworkWidgetFactory()->createSeparator(parent); }; - Layouting::Separator::setSeparatorFactoryFunc(separatorCreator); + Layouting::Config::self().setSeparatorFactoryFunc(separatorCreator); } Config& Config::self() @@ -94,10 +94,12 @@ void Config::setFlags(Flags f) return; } - Layouting::Separator::usesLazyResize = f & Flag_LazyResize; // TODO: We'll soon have Layouting::Config and rely less on static members - d->m_flags = f; d->fixFlags(); + + auto multisplitterFlags = Layouting::Config::self().flags(); + multisplitterFlags.setFlag(Layouting::Config::Flag::LazyResize, d->m_flags & Flag_LazyResize); + Layouting::Config::self().setFlags(multisplitterFlags); } void Config::setDockWidgetFactoryFunc(DockWidgetFactoryFunc func) @@ -134,22 +136,17 @@ FrameworkWidgetFactory *Config::frameworkWidgetFactory() const int Config::separatorThickness() const { - return Layouting::Item::separatorThickness; + return Layouting::Config::self().separatorThickness(); } void Config::setSeparatorThickness(int value) { - if (value < 0 || value >= 100) { - qWarning() << Q_FUNC_INFO << "Invalid value" << value; - return; - } - if (!DockRegistry::self()->isEmpty()) { qWarning() << Q_FUNC_INFO << "Only use this function at startup before creating any DockWidget or MainWindow"; return; } - Layouting::Item::separatorThickness = value; + Layouting::Config::self().setSeparatorThickness(value); } void Config::setQmlEngine(QQmlEngine *qmlEngine) diff --git a/src/private/multisplitter/CMakeLists.txt b/src/private/multisplitter/CMakeLists.txt index 27c12d8e..cf5fa318 100644 --- a/src/private/multisplitter/CMakeLists.txt +++ b/src/private/multisplitter/CMakeLists.txt @@ -1,4 +1,6 @@ set(MULTISPLITTER_SRCS + Config.cpp + Config.h Item.cpp Item_p.h Logging.cpp diff --git a/src/private/multisplitter/Config.cpp b/src/private/multisplitter/Config.cpp new file mode 100644 index 00000000..97fd8e66 --- /dev/null +++ b/src/private/multisplitter/Config.cpp @@ -0,0 +1,90 @@ +/* + This file is part of KDDockWidgets. + + Copyright (C) 2020 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 . +*/ + +#include "Config.h" +#include "Item_p.h" +#include "Separator_p.h" + +using namespace Layouting; + +Config::Config() +{ +} + +Separator *Config::createSeparator(QWidget *parent) const +{ + if (m_separatorFactoryFunc) + return m_separatorFactoryFunc(parent); + + // won't be visible, but still create it, so we don't have to look for nullptrs in the code + // won't be a use case anyway, you'll want to provide a factory func + return new Separator(parent); +} + +Config &Config::self() +{ + static Config config; + return config; +} + +Config::~Config() +{ +} + +int Config::separatorThickness() const +{ + // TODO: Make Item call Config::separatorThickness instead ? + return Item::separatorThickness; +} + +void Config::setSeparatorThickness(int value) +{ + if (value < 0 || value >= 100) { + qWarning() << Q_FUNC_INFO << "Invalid value" << value; + return; + } + + Layouting::Item::separatorThickness = value; +} + +void Config::setSeparatorFactoryFunc(SeparatorFactoryFunc func) +{ + m_separatorFactoryFunc = func; +} + +SeparatorFactoryFunc Config::separatorFactoryFunc() const +{ + return m_separatorFactoryFunc; +} + +Config::Flags Config::flags() const +{ + return m_flags; +} + +void Config::setFlags(Flags flags) +{ + if (m_flags == flags) + return; + + // validations here, if any + + m_flags = flags; +} diff --git a/src/private/multisplitter/Config.h b/src/private/multisplitter/Config.h new file mode 100644 index 00000000..a77bc8cb --- /dev/null +++ b/src/private/multisplitter/Config.h @@ -0,0 +1,84 @@ +/* + This file is part of KDDockWidgets. + + Copyright (C) 2020 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 . +*/ + +#pragma once + +#include + +class QWidget; // TODO + +namespace Layouting { + +class Separator; + +typedef Separator* (*SeparatorFactoryFunc)(QWidget *parent); + +class Config { +public: + + enum class Flag { + None = 0, + LazyResize = 1 + }; + Q_DECLARE_FLAGS(Flags, Flag); + + ///@brief returns the singleton Config instance + static Config &self(); + + ///@brief destructor, called at shutdown + ~Config(); + + /** + * @brief Returns the thickness of the separator. + * + * Default is 5px. + */ + int separatorThickness() const; + + ///@brief setter for @ref separatorThickness + ///Note: Only use this function at startup before creating any Item + void setSeparatorThickness(int value); + + ///@brief sets the function used internally to create the separators + void setSeparatorFactoryFunc(SeparatorFactoryFunc); + + ///@brief Returns the function used to create separators, null by default + SeparatorFactoryFunc separatorFactoryFunc() const; + + ///@brief returns the flags; + Config::Flags flags() const; + + ///@brief sets the flags. Set only before creating any Item + void setFlags(Flags); + +private: + friend class Item; + friend class ItemContainer; + + Config(); + Separator* createSeparator(QWidget*) const; + + SeparatorFactoryFunc m_separatorFactoryFunc = nullptr; + Flags m_flags = Flag::None; + + Q_DISABLE_COPY(Config); +}; + +} diff --git a/src/private/multisplitter/Item.cpp b/src/private/multisplitter/Item.cpp index 0386b511..9c97ead5 100644 --- a/src/private/multisplitter/Item.cpp +++ b/src/private/multisplitter/Item.cpp @@ -20,6 +20,7 @@ #include "Item_p.h" #include "Separator_p.h" +#include "Config.h" #include #include @@ -2766,7 +2767,7 @@ void ItemContainer::Private::updateSeparators() newSeparators.push_back(separator); m_separators.removeOne(separator); } else { - separator = Separator::createSeparator(q->hostWidget()); + separator = Config::self().createSeparator(q->hostWidget()); separator->init(q, m_orientation); newSeparators.push_back(separator); } diff --git a/src/private/multisplitter/Separator.cpp b/src/private/multisplitter/Separator.cpp index 1789d4ce..50a8eb01 100644 --- a/src/private/multisplitter/Separator.cpp +++ b/src/private/multisplitter/Separator.cpp @@ -19,8 +19,9 @@ */ #include "Separator_p.h" -#include "Logging_p.h" // TODO: Have our own +#include "Logging_p.h" #include "Item_p.h" +#include "Config.h" #include #include @@ -34,9 +35,6 @@ using namespace Layouting; Separator* Separator::s_separatorBeingDragged = nullptr; -static SeparatorFactoryFunc s_separatorFactoryFunc = nullptr; -bool Separator::usesLazyResize = false; - struct Separator::Private { // Only set when anchor is moved through mouse. Side1 if going towards left or top, Side2 otherwise. @@ -47,6 +45,7 @@ struct Separator::Private { QRubberBand *lazyResizeRubberBand = nullptr; ItemContainer *parentContainer = nullptr; Layouting::Side lastMoveDirection = Side1; + const bool usesLazyResize = Config::self().flags() & Config::Flag::LazyResize; }; Separator::Separator(QWidget *hostWidget) @@ -182,8 +181,8 @@ void Separator::init(ItemContainer *parentContainer, Qt::Orientation orientation d->parentContainer = parentContainer; d->orientation = orientation; - d->lazyResizeRubberBand = usesLazyResize ? new QRubberBand(QRubberBand::Line, hostWidget()) - : nullptr; + d->lazyResizeRubberBand = d->usesLazyResize ? new QRubberBand(QRubberBand::Line, hostWidget()) + : nullptr; setVisible(true); } @@ -213,19 +212,6 @@ bool Separator::isResizing() return s_separatorBeingDragged != nullptr; } -void Separator::setSeparatorFactoryFunc(SeparatorFactoryFunc func) -{ - s_separatorFactoryFunc = func; -} - -Separator* Separator::createSeparator(QWidget *host) -{ - if (s_separatorFactoryFunc) - return s_separatorFactoryFunc(host); - - return new Separator(host); -} - void Separator::setLazyPosition(int pos) { if (d->lazyPosition != pos) { diff --git a/src/private/multisplitter/Separator_p.h b/src/private/multisplitter/Separator_p.h index 7ef25e2f..5ffdaf1e 100644 --- a/src/private/multisplitter/Separator_p.h +++ b/src/private/multisplitter/Separator_p.h @@ -31,11 +31,10 @@ QT_END_NAMESPACE namespace Layouting { +class Config; class ItemContainer; class Separator; -typedef Separator* (*SeparatorFactoryFunc)(QWidget *parent); - class Separator : public QWidget { Q_OBJECT @@ -59,9 +58,6 @@ public: ///@brief Returns whether we're dragging a separator. Can be useful for the app to stop other work while we're not in the final size static bool isResizing(); - static void setSeparatorFactoryFunc(SeparatorFactoryFunc); - static Separator* createSeparator(QWidget *host); - static bool usesLazyResize; protected: explicit Separator(QWidget *hostWidget); @@ -70,9 +66,12 @@ protected: void mouseReleaseEvent(QMouseEvent *) override; void mouseDoubleClickEvent(QMouseEvent *) override; private: + friend class Config; + void onMouseReleased(); void setLazyPosition(int); bool isBeingDragged() const; + bool usesLazyResize() const; static bool s_isResizing; static Separator* s_separatorBeingDragged; struct Private; diff --git a/tests/tst_docks.cpp b/tests/tst_docks.cpp index 25baa8be..2ba957c3 100644 --- a/tests/tst_docks.cpp +++ b/tests/tst_docks.cpp @@ -410,7 +410,7 @@ Frame* createFrameWithWidget(const QString &name, MultiSplitter *parent, int min QWidget *w = createWidget(minLength, name); auto dw = new DockWidget(name); dw->setWidget(w); - auto frame = Config::self().frameworkWidgetFactory()->createFrame(parent); + auto frame =KDDockWidgets::Config::self().frameworkWidgetFactory()->createFrame(parent); frame->addWidget(dw); return frame; }