Add Layouting::Config

To host many of the scatered static member variables which
held some settings that rarely change
This commit is contained in:
Sergio Martins
2020-05-23 18:40:20 +01:00
parent 2527b39368
commit 9ad6eefa7a
8 changed files with 196 additions and 37 deletions

View File

@@ -26,9 +26,9 @@
*/
#include "Config.h"
#include "multisplitter/Config.h"
#include "DockRegistry_p.h"
#include "FrameworkWidgetFactory.h"
#include "multisplitter/Separator_p.h"
#include <QApplication>
#include <QDebug>
@@ -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)

View File

@@ -1,4 +1,6 @@
set(MULTISPLITTER_SRCS
Config.cpp
Config.h
Item.cpp
Item_p.h
Logging.cpp

View File

@@ -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 <sergio.martins@kdab.com>
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 <http://www.gnu.org/licenses/>.
*/
#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;
}

View File

@@ -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 <sergio.martins@kdab.com>
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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <qglobal.h>
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);
};
}

View File

@@ -20,6 +20,7 @@
#include "Item_p.h"
#include "Separator_p.h"
#include "Config.h"
#include <QEvent>
#include <QDebug>
@@ -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);
}

View File

@@ -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 <QMouseEvent>
#include <QRubberBand>
@@ -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) {

View File

@@ -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;

View File

@@ -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;
}