mirror-chatterino2/src/widgets/splits/SplitContainer.hpp

247 lines
6.5 KiB
C++
Raw Normal View History

#pragma once
2016-12-29 17:31:07 +01:00
2018-06-26 14:09:39 +02:00
#include "widgets/BaseWidget.hpp"
#include "widgets/helper/DropPreview.hpp"
#include "widgets/helper/NotebookTab.hpp"
#include "widgets/splits/Split.hpp"
2016-12-30 12:20:26 +01:00
2017-01-18 04:52:47 +01:00
#include <QDragEnterEvent>
#include <QHBoxLayout>
#include <QRect>
#include <QVBoxLayout>
#include <QVector>
#include <QWidget>
2018-05-10 19:50:31 +02:00
#include <algorithm>
#include <functional>
#include <vector>
2018-05-10 23:58:07 +02:00
#include <pajlada/signals/signal.hpp>
2018-05-10 19:50:31 +02:00
#include <pajlada/signals/signalholder.hpp>
2018-05-16 14:55:45 +02:00
class QJsonObject;
2018-05-10 23:58:07 +02:00
2017-01-18 21:30:23 +01:00
namespace chatterino {
2018-05-16 14:55:45 +02:00
//
// Note: This class is a spaghetti container. There is a lot of spaghetti code inside but it doesn't
// expose any of it publicly.
//
2018-05-10 19:50:31 +02:00
class SplitContainer : public BaseWidget, pajlada::Signals::SignalHolder
2016-12-29 17:31:07 +01:00
{
2017-01-11 18:52:09 +01:00
Q_OBJECT
2016-12-29 17:31:07 +01:00
2018-05-16 17:47:58 +02:00
public:
2018-05-16 14:55:45 +02:00
struct Node;
2018-05-10 19:50:31 +02:00
// fourtf: !!! preserve the order of left, up, right and down
enum Direction { Left, Above, Right, Below };
struct Position final {
2018-05-10 19:50:31 +02:00
private:
Position() = default;
Position(Node *_relativeNode, Direction _direcion)
: relativeNode(_relativeNode)
, direction(_direcion)
{
}
Node *relativeNode;
Direction direction;
friend struct Node;
friend class SplitContainer;
};
2018-05-16 14:55:45 +02:00
private:
struct DropRect final {
2018-05-10 19:50:31 +02:00
QRect rect;
Position position;
DropRect(const QRect &_rect, const Position &_position)
: rect(_rect)
, position(_position)
{
}
};
struct ResizeRect final {
2018-05-16 14:55:45 +02:00
QRect rect;
Node *node;
bool vertical;
2018-05-10 19:50:31 +02:00
2018-05-16 14:55:45 +02:00
ResizeRect(const QRect &_rect, Node *_node, bool _vertical)
: rect(_rect)
, node(_node)
, vertical(_vertical)
2018-05-10 19:50:31 +02:00
{
}
2018-05-16 14:55:45 +02:00
};
public:
struct Node final {
enum Type { EmptyRoot, _Split, VerticalContainer, HorizontalContainer };
Type getType();
Split *getSplit();
Node *getParent();
qreal getHorizontalFlex();
qreal getVerticalFlex();
const std::vector<std::unique_ptr<Node>> &getChildren();
2018-05-10 19:50:31 +02:00
private:
Type type;
Split *split;
2018-05-25 14:57:17 +02:00
Node *preferedFocusTarget;
2018-05-10 19:50:31 +02:00
Node *parent;
QRectF geometry;
2018-05-16 14:55:45 +02:00
qreal flexH = 1;
qreal flexV = 1;
2018-05-10 19:50:31 +02:00
std::vector<std::unique_ptr<Node>> children;
2018-05-16 14:55:45 +02:00
Node();
Node(Split *_split, Node *_parent);
2018-05-10 19:50:31 +02:00
2018-05-16 14:55:45 +02:00
bool isOrContainsNode(Node *_node);
Node *findNodeContainingSplit(Split *_split);
void insertSplitRelative(Split *_split, Direction _direction);
void nestSplitIntoCollection(Split *_split, Direction _direction);
void _insertNextToThis(Split *_split, Direction _direction);
void setSplit(Split *_split);
Position releaseSplit();
qreal getFlex(bool isVertical);
qreal getSize(bool isVertical);
qreal getChildrensTotalFlex(bool isVertical);
void layout(bool addSpacing, float _scale, std::vector<DropRect> &dropRects,
std::vector<ResizeRect> &resizeRects);
2018-05-10 19:50:31 +02:00
2018-05-16 14:55:45 +02:00
static Type toContainerType(Direction _dir);
2018-05-10 19:50:31 +02:00
friend class SplitContainer;
};
2018-05-16 14:55:45 +02:00
private:
class DropOverlay final : public QWidget
2018-05-10 23:58:07 +02:00
{
public:
2018-05-16 14:55:45 +02:00
DropOverlay(SplitContainer *_parent = nullptr);
2018-05-10 23:58:07 +02:00
2018-05-16 14:55:45 +02:00
void setRects(std::vector<SplitContainer::DropRect> _rects);
2018-05-10 23:58:07 +02:00
pajlada::Signals::NoArgSignal dragEnded;
protected:
2018-05-16 14:55:45 +02:00
void paintEvent(QPaintEvent *event) override;
void dragEnterEvent(QDragEnterEvent *event) override;
void dragMoveEvent(QDragMoveEvent *event) override;
void dragLeaveEvent(QDragLeaveEvent *event) override;
void dropEvent(QDropEvent *event) override;
2018-05-10 23:58:07 +02:00
2018-05-16 14:55:45 +02:00
private:
std::vector<DropRect> rects;
QPoint mouseOverPoint;
SplitContainer *parent;
};
2018-05-10 23:58:07 +02:00
class ResizeHandle final : public QWidget
2018-05-16 14:55:45 +02:00
{
public:
SplitContainer *parent;
Node *node;
2018-05-10 23:58:07 +02:00
2018-05-16 14:55:45 +02:00
void setVertical(bool isVertical);
ResizeHandle(SplitContainer *_parent = nullptr);
void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
2018-07-04 19:52:11 +02:00
void mouseDoubleClickEvent(QMouseEvent *event) override;
2018-05-10 23:58:07 +02:00
2018-05-16 14:55:45 +02:00
friend class SplitContainer;
2018-05-10 23:58:07 +02:00
private:
2018-05-16 14:55:45 +02:00
bool vertical;
bool isMouseDown = false;
2018-05-10 23:58:07 +02:00
};
2018-05-16 14:55:45 +02:00
public:
2018-05-23 11:59:37 +02:00
SplitContainer(Notebook *parent);
2018-05-10 19:50:31 +02:00
void appendNewSplit(bool openChannelNameDialog);
void appendSplit(Split *split);
void insertSplit(Split *split, const Position &position);
void insertSplit(Split *split, Direction direction, Split *relativeTo);
void insertSplit(Split *split, Direction direction, Node *relativeTo = nullptr);
Position releaseSplit(Split *split);
Position deleteSplit(Split *split);
2018-05-25 14:57:17 +02:00
void selectNextSplit(Direction direction);
2018-05-10 19:50:31 +02:00
2018-05-25 14:57:17 +02:00
void decodeFromJson(QJsonObject &obj);
2018-05-10 19:50:31 +02:00
2018-05-25 14:57:17 +02:00
int getSplitCount();
const std::vector<Split *> getSplits() const;
2018-05-10 19:50:31 +02:00
void refreshTabTitle();
2018-05-23 11:59:37 +02:00
NotebookTab *getTab() const;
2018-05-25 14:57:17 +02:00
Node *getBaseNode();
2018-05-23 11:59:37 +02:00
void setTab(NotebookTab *tab);
void hideResizeHandles();
void resetMouseStatus();
2018-05-23 04:22:17 +02:00
2018-05-10 19:50:31 +02:00
static bool isDraggingSplit;
static Split *draggingSplit;
2017-11-12 17:21:50 +01:00
2016-12-30 18:00:25 +01:00
protected:
2018-04-07 12:53:10 +02:00
void paintEvent(QPaintEvent *event) override;
2017-01-01 02:30:42 +01:00
2018-05-31 16:02:20 +02:00
void focusInEvent(QFocusEvent *event) override;
2018-05-10 23:58:07 +02:00
void leaveEvent(QEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
2018-04-07 12:53:10 +02:00
void mouseReleaseEvent(QMouseEvent *event) override;
2018-04-07 12:53:10 +02:00
void dragEnterEvent(QDragEnterEvent *event) override;
2017-01-01 02:30:42 +01:00
2018-05-10 19:50:31 +02:00
void resizeEvent(QResizeEvent *event) override;
2017-04-12 17:46:44 +02:00
private:
2017-01-11 18:52:09 +01:00
struct DropRegion {
2017-01-01 02:30:42 +01:00
QRect rect;
std::pair<int, int> position;
DropRegion(QRect rect, std::pair<int, int> position)
{
this->rect = rect;
this->position = position;
}
};
void addSplit(Split *split);
2018-05-10 19:50:31 +02:00
std::vector<DropRect> dropRects;
std::vector<DropRegion> dropRegions;
NotebookPageDropPreview dropPreview;
2018-05-10 23:58:07 +02:00
DropOverlay overlay;
2018-05-16 14:55:45 +02:00
std::vector<std::unique_ptr<ResizeHandle>> resizeHandles;
2018-05-10 23:58:07 +02:00
QPoint mouseOverPoint;
2018-05-10 19:50:31 +02:00
void layout();
2017-01-01 02:30:42 +01:00
2018-05-10 19:50:31 +02:00
Node baseNode;
2018-05-25 14:57:17 +02:00
Split *selected;
void setSelected(Split *selected);
void selectSplitRecursive(Node *node, Direction direction);
void focusSplitRecursive(Node *node, Direction direction);
void setPreferedTargetRecursive(Node *node);
2017-04-12 17:46:44 +02:00
2018-05-23 11:59:37 +02:00
NotebookTab *tab;
std::vector<Split *> splits;
2018-05-10 19:50:31 +02:00
bool isDragging = false;
2017-01-01 02:30:42 +01:00
2018-05-16 14:55:45 +02:00
void decodeNodeRecusively(QJsonObject &obj, Node *node);
2016-12-29 17:31:07 +01:00
};
2017-04-14 17:52:22 +02:00
} // namespace chatterino