2020-09-18 12:11:40 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2020 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "utils_global.h"
|
|
|
|
|
|
2022-02-15 10:41:13 +01:00
|
|
|
#include "optional.h"
|
|
|
|
|
|
2020-09-18 12:11:40 +02:00
|
|
|
#include <QList>
|
|
|
|
|
#include <QString>
|
2021-03-02 12:48:46 +01:00
|
|
|
#include <QVariant>
|
2020-09-18 12:11:40 +02:00
|
|
|
|
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
class QLayout;
|
|
|
|
|
class QWidget;
|
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
|
|
|
|
|
namespace Utils {
|
|
|
|
|
|
|
|
|
|
class BaseAspect;
|
2021-03-18 18:23:21 +01:00
|
|
|
class BoolAspect;
|
2020-09-18 12:11:40 +02:00
|
|
|
|
2022-07-26 11:35:19 +02:00
|
|
|
namespace Layouting {
|
|
|
|
|
|
|
|
|
|
enum AttachType {
|
|
|
|
|
WithMargins,
|
|
|
|
|
WithoutMargins,
|
2022-07-28 11:01:05 +02:00
|
|
|
WithFormAlignment, // Handle Grid similar to QFormLayout, i.e. use special alignment for the first column on Mac
|
2022-07-26 11:35:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // Layouting
|
|
|
|
|
|
2020-09-18 12:11:40 +02:00
|
|
|
class QTCREATOR_UTILS_EXPORT LayoutBuilder
|
|
|
|
|
{
|
|
|
|
|
public:
|
2021-02-18 14:18:34 +01:00
|
|
|
enum LayoutType {
|
2021-03-11 19:02:42 +01:00
|
|
|
HBoxLayout,
|
|
|
|
|
VBoxLayout,
|
|
|
|
|
FormLayout,
|
|
|
|
|
GridLayout,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum class AlignmentType {
|
|
|
|
|
DefaultAlignment,
|
|
|
|
|
AlignAsFormLabel,
|
2021-02-18 14:18:34 +01:00
|
|
|
};
|
2020-10-08 07:24:19 +02:00
|
|
|
|
2021-03-02 12:48:46 +01:00
|
|
|
enum class SpecialType {
|
|
|
|
|
NotSpecial,
|
|
|
|
|
Space,
|
|
|
|
|
Stretch,
|
|
|
|
|
Break,
|
|
|
|
|
};
|
|
|
|
|
|
2022-08-10 17:13:21 +02:00
|
|
|
using Setter = std::function<void(QObject *target)>;
|
2022-07-22 18:54:04 +02:00
|
|
|
|
2020-09-25 14:58:04 +02:00
|
|
|
class QTCREATOR_UTILS_EXPORT LayoutItem
|
2020-09-18 12:11:40 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2020-09-21 09:39:54 +02:00
|
|
|
LayoutItem();
|
2021-03-11 19:02:42 +01:00
|
|
|
LayoutItem(QLayout *layout);
|
|
|
|
|
LayoutItem(QWidget *widget);
|
|
|
|
|
LayoutItem(BaseAspect *aspect); // Remove
|
|
|
|
|
LayoutItem(BaseAspect &aspect);
|
|
|
|
|
LayoutItem(const QString &text);
|
|
|
|
|
LayoutItem(const LayoutBuilder &builder);
|
2022-08-10 17:13:21 +02:00
|
|
|
LayoutItem(const Setter &setter) { this->setter = setter; }
|
2020-09-18 12:11:40 +02:00
|
|
|
|
|
|
|
|
QLayout *layout = nullptr;
|
|
|
|
|
QWidget *widget = nullptr;
|
|
|
|
|
BaseAspect *aspect = nullptr;
|
2021-03-11 19:02:42 +01:00
|
|
|
|
2021-03-02 12:48:46 +01:00
|
|
|
QString text; // FIXME: Use specialValue for that
|
2020-09-18 12:11:40 +02:00
|
|
|
int span = 1;
|
2022-08-10 17:13:21 +02:00
|
|
|
AlignmentType align = AlignmentType::DefaultAlignment;
|
|
|
|
|
Setter setter;
|
2021-03-02 12:48:46 +01:00
|
|
|
SpecialType specialType = SpecialType::NotSpecial;
|
|
|
|
|
QVariant specialValue;
|
2021-02-18 14:18:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using LayoutItems = QList<LayoutItem>;
|
|
|
|
|
|
|
|
|
|
explicit LayoutBuilder(LayoutType layoutType, const LayoutItems &items = {});
|
|
|
|
|
|
|
|
|
|
LayoutBuilder(const LayoutBuilder &) = delete;
|
|
|
|
|
LayoutBuilder(LayoutBuilder &&) = default;
|
|
|
|
|
LayoutBuilder &operator=(const LayoutBuilder &) = delete;
|
|
|
|
|
LayoutBuilder &operator=(LayoutBuilder &&) = default;
|
|
|
|
|
|
|
|
|
|
~LayoutBuilder();
|
|
|
|
|
|
2022-02-15 10:41:13 +01:00
|
|
|
LayoutBuilder &setSpacing(int spacing);
|
|
|
|
|
|
2020-09-18 04:54:41 +02:00
|
|
|
LayoutBuilder &addItem(const LayoutItem &item);
|
2021-02-18 14:18:34 +01:00
|
|
|
LayoutBuilder &addItems(const LayoutItems &items);
|
2020-09-18 12:11:40 +02:00
|
|
|
|
2020-09-18 04:54:41 +02:00
|
|
|
LayoutBuilder &finishRow();
|
2020-09-18 12:11:40 +02:00
|
|
|
LayoutBuilder &addRow(const LayoutItem &item);
|
2021-02-18 14:18:34 +01:00
|
|
|
LayoutBuilder &addRow(const LayoutItems &items);
|
2020-09-18 12:11:40 +02:00
|
|
|
|
2021-03-11 19:02:42 +01:00
|
|
|
LayoutType layoutType() const { return m_layoutType; }
|
2021-02-18 14:18:34 +01:00
|
|
|
|
2022-07-26 11:35:19 +02:00
|
|
|
void attachTo(QWidget *w, Layouting::AttachType attachType = Layouting::WithMargins) const;
|
|
|
|
|
QWidget *emerge(Layouting::AttachType attachType = Layouting::WithMargins);
|
2020-09-18 12:11:40 +02:00
|
|
|
|
2021-03-02 12:48:46 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT Space : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
2022-07-25 17:56:47 +02:00
|
|
|
explicit Space(int space);
|
2021-03-02 12:48:46 +01:00
|
|
|
};
|
|
|
|
|
|
2021-03-11 19:02:42 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT Span : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Span(int span, const LayoutItem &item);
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-02 12:48:46 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT Stretch : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit Stretch(int stretch = 1);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT Break : public LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Break();
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-11 19:02:42 +01:00
|
|
|
protected:
|
|
|
|
|
explicit LayoutBuilder(); // Adds to existing layout.
|
2020-09-18 12:11:40 +02:00
|
|
|
|
2022-02-15 10:41:13 +01:00
|
|
|
QLayout *createLayout() const;
|
2022-07-26 11:35:19 +02:00
|
|
|
void doLayout(QWidget *parent, Layouting::AttachType attachType) const;
|
2021-03-11 19:02:42 +01:00
|
|
|
|
|
|
|
|
LayoutItems m_items;
|
|
|
|
|
LayoutType m_layoutType;
|
2022-02-15 10:41:13 +01:00
|
|
|
Utils::optional<int> m_spacing;
|
2021-03-11 19:02:42 +01:00
|
|
|
};
|
2021-02-18 14:18:34 +01:00
|
|
|
|
2021-03-11 19:02:42 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT LayoutExtender : public LayoutBuilder
|
2021-02-18 14:18:34 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2022-07-28 11:01:05 +02:00
|
|
|
explicit LayoutExtender(QLayout *layout, Layouting::AttachType attachType);
|
2021-03-11 19:02:42 +01:00
|
|
|
~LayoutExtender();
|
2021-02-18 14:18:34 +01:00
|
|
|
|
2021-03-11 19:02:42 +01:00
|
|
|
private:
|
|
|
|
|
QLayout *m_layout = nullptr;
|
2022-07-28 11:01:05 +02:00
|
|
|
Layouting::AttachType m_attachType = {};
|
2021-02-18 14:18:34 +01:00
|
|
|
};
|
|
|
|
|
|
2021-03-11 19:02:42 +01:00
|
|
|
namespace Layouting {
|
|
|
|
|
|
2022-07-21 12:10:56 +02:00
|
|
|
QTCREATOR_UTILS_EXPORT LayoutBuilder::Setter title(const QString &title,
|
|
|
|
|
BoolAspect *checker = nullptr);
|
|
|
|
|
|
2022-08-01 18:18:02 +02:00
|
|
|
QTCREATOR_UTILS_EXPORT LayoutBuilder::Setter text(const QString &text);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT LayoutBuilder::Setter tooltip(const QString &toolTip);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT LayoutBuilder::Setter onClicked(const std::function<void()> &func,
|
|
|
|
|
QObject *guard = nullptr);
|
|
|
|
|
|
2021-03-11 19:02:42 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT Group : public LayoutBuilder::LayoutItem
|
2021-02-18 14:18:34 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2022-08-10 17:13:21 +02:00
|
|
|
Group(std::initializer_list<LayoutItem> items);
|
2021-02-18 14:18:34 +01:00
|
|
|
};
|
|
|
|
|
|
2022-08-01 18:18:02 +02:00
|
|
|
class QTCREATOR_UTILS_EXPORT PushButton : public LayoutBuilder::LayoutItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
2022-08-10 17:13:21 +02:00
|
|
|
PushButton(std::initializer_list<LayoutItem> items);
|
2022-08-01 18:18:02 +02:00
|
|
|
};
|
|
|
|
|
|
2021-03-11 19:02:42 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT Column : public LayoutBuilder
|
2021-02-18 14:18:34 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2021-03-17 08:46:15 +01:00
|
|
|
Column() : LayoutBuilder(VBoxLayout) {}
|
2021-03-11 19:02:42 +01:00
|
|
|
Column(std::initializer_list<LayoutItem> items) : LayoutBuilder(VBoxLayout, items) {}
|
2021-02-18 14:18:34 +01:00
|
|
|
};
|
|
|
|
|
|
2021-03-11 19:02:42 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT Row : public LayoutBuilder
|
2021-02-18 14:18:34 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2021-03-17 08:46:15 +01:00
|
|
|
Row() : LayoutBuilder(HBoxLayout) {}
|
2021-03-11 19:02:42 +01:00
|
|
|
Row(std::initializer_list<LayoutItem> items) : LayoutBuilder(HBoxLayout, items) {}
|
2021-02-18 14:18:34 +01:00
|
|
|
};
|
|
|
|
|
|
2021-03-11 19:02:42 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT Grid : public LayoutBuilder
|
2021-02-18 14:18:34 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2021-03-17 08:46:15 +01:00
|
|
|
Grid() : LayoutBuilder(GridLayout) {}
|
2021-03-11 19:02:42 +01:00
|
|
|
Grid(std::initializer_list<LayoutItem> items) : LayoutBuilder(GridLayout, items) {}
|
2021-02-18 14:18:34 +01:00
|
|
|
};
|
|
|
|
|
|
2021-03-11 19:02:42 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT Form : public LayoutBuilder
|
2021-03-02 12:48:46 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2021-03-17 08:46:15 +01:00
|
|
|
Form() : LayoutBuilder(FormLayout) {}
|
2021-03-11 19:02:42 +01:00
|
|
|
Form(std::initializer_list<LayoutItem> items) : LayoutBuilder(FormLayout, items) {}
|
2021-03-02 12:48:46 +01:00
|
|
|
};
|
|
|
|
|
|
2021-02-18 14:18:34 +01:00
|
|
|
using Space = LayoutBuilder::Space;
|
2021-03-11 19:02:42 +01:00
|
|
|
using Span = LayoutBuilder::Span;
|
2021-02-18 14:18:34 +01:00
|
|
|
|
2022-07-22 18:54:04 +02:00
|
|
|
QTCREATOR_UTILS_EXPORT extern LayoutBuilder::Break br;
|
|
|
|
|
QTCREATOR_UTILS_EXPORT extern LayoutBuilder::Stretch st;
|
2022-07-25 17:56:47 +02:00
|
|
|
QTCREATOR_UTILS_EXPORT extern LayoutBuilder::Space empty;
|
2022-07-22 18:54:04 +02:00
|
|
|
|
|
|
|
|
} // Layouting
|
|
|
|
|
} // Utils
|