2018-06-26 14:39:22 +02:00
|
|
|
#include "widgets/splits/SplitContainer.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "Application.hpp"
|
2018-06-26 15:33:51 +02:00
|
|
|
#include "common/Common.hpp"
|
2018-06-26 17:20:03 +02:00
|
|
|
#include "debug/AssertInGuiThread.hpp"
|
2018-06-28 20:03:04 +02:00
|
|
|
#include "singletons/Theme.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "singletons/WindowManager.hpp"
|
|
|
|
#include "util/Helpers.hpp"
|
|
|
|
#include "util/LayoutCreator.hpp"
|
|
|
|
#include "widgets/Notebook.hpp"
|
2018-06-26 17:20:03 +02:00
|
|
|
#include "widgets/helper/NotebookTab.hpp"
|
2018-06-26 14:39:22 +02:00
|
|
|
#include "widgets/splits/Split.hpp"
|
2016-12-29 17:31:07 +01:00
|
|
|
|
2017-08-17 16:43:19 +02:00
|
|
|
#include <QApplication>
|
2017-01-28 22:35:23 +01:00
|
|
|
#include <QDebug>
|
2017-01-18 04:52:47 +01:00
|
|
|
#include <QHBoxLayout>
|
2018-05-16 14:55:45 +02:00
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonObject>
|
2017-01-18 04:52:47 +01:00
|
|
|
#include <QMimeData>
|
2017-01-29 12:26:22 +01:00
|
|
|
#include <QObject>
|
2017-01-18 04:52:47 +01:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
#include <QWidget>
|
2017-01-28 22:35:23 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2017-01-18 04:52:47 +01:00
|
|
|
|
2017-07-02 13:10:06 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
namespace chatterino {
|
2017-01-18 21:30:23 +01:00
|
|
|
|
2017-11-12 17:21:50 +01:00
|
|
|
bool SplitContainer::isDraggingSplit = false;
|
|
|
|
Split *SplitContainer::draggingSplit = nullptr;
|
2017-01-01 02:30:42 +01:00
|
|
|
|
2018-05-23 11:59:37 +02:00
|
|
|
SplitContainer::SplitContainer(Notebook *parent)
|
2018-04-27 22:11:19 +02:00
|
|
|
: BaseWidget(parent)
|
2017-07-02 13:10:06 +02:00
|
|
|
, dropPreview(this)
|
2018-05-10 23:58:07 +02:00
|
|
|
, overlay(this)
|
2018-05-24 17:22:51 +02:00
|
|
|
, mouseOverPoint(-10000, -10000)
|
|
|
|
, tab(nullptr)
|
2017-01-01 02:30:42 +01:00
|
|
|
{
|
2018-05-10 23:58:07 +02:00
|
|
|
this->refreshTabTitle();
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
this->managedConnect(Split::modifierStatusChanged, [this](auto modifiers) {
|
2018-05-10 23:58:07 +02:00
|
|
|
this->layout();
|
2018-05-16 14:55:45 +02:00
|
|
|
|
2018-06-01 14:20:46 +02:00
|
|
|
if (modifiers == showResizeHandlesModifiers) {
|
2018-05-16 14:55:45 +02:00
|
|
|
for (std::unique_ptr<ResizeHandle> &handle : this->resizeHandles) {
|
|
|
|
handle->show();
|
|
|
|
handle->raise();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (std::unique_ptr<ResizeHandle> &handle : this->resizeHandles) {
|
|
|
|
handle->hide();
|
|
|
|
}
|
|
|
|
}
|
2018-06-01 14:20:46 +02:00
|
|
|
|
|
|
|
if (modifiers == showSplitOverlayModifiers) {
|
|
|
|
this->setCursor(Qt::PointingHandCursor);
|
|
|
|
} else {
|
|
|
|
this->unsetCursor();
|
|
|
|
}
|
2018-05-10 23:58:07 +02:00
|
|
|
});
|
2017-05-29 21:26:55 +02:00
|
|
|
|
2018-05-10 23:58:07 +02:00
|
|
|
this->setCursor(Qt::PointingHandCursor);
|
|
|
|
this->setAcceptDrops(true);
|
2017-05-29 21:26:55 +02:00
|
|
|
|
2018-05-10 23:58:07 +02:00
|
|
|
this->managedConnect(this->overlay.dragEnded, [this]() {
|
|
|
|
this->isDragging = false;
|
|
|
|
this->layout();
|
|
|
|
});
|
2017-08-17 22:25:41 +02:00
|
|
|
|
2018-05-10 23:58:07 +02:00
|
|
|
this->overlay.hide();
|
2017-11-12 17:21:50 +01:00
|
|
|
|
2018-05-10 23:58:07 +02:00
|
|
|
this->setMouseTracking(true);
|
2018-05-10 19:50:31 +02:00
|
|
|
this->setAcceptDrops(true);
|
2017-11-12 17:21:50 +01:00
|
|
|
}
|
|
|
|
|
2018-05-23 11:59:37 +02:00
|
|
|
NotebookTab *SplitContainer::getTab() const
|
2018-01-16 18:55:30 +01:00
|
|
|
{
|
2018-05-10 19:50:31 +02:00
|
|
|
return this->tab;
|
2018-01-16 18:55:30 +01:00
|
|
|
}
|
|
|
|
|
2018-05-23 11:59:37 +02:00
|
|
|
void SplitContainer::setTab(NotebookTab *_tab)
|
2018-05-23 04:22:17 +02:00
|
|
|
{
|
|
|
|
this->tab = _tab;
|
|
|
|
|
|
|
|
this->tab->page = this;
|
|
|
|
|
|
|
|
this->refreshTabTitle();
|
|
|
|
}
|
|
|
|
|
2018-05-25 16:11:03 +02:00
|
|
|
void SplitContainer::hideResizeHandles()
|
|
|
|
{
|
|
|
|
this->overlay.hide();
|
|
|
|
|
|
|
|
for (std::unique_ptr<ResizeHandle> &handle : this->resizeHandles) {
|
|
|
|
handle->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-01 14:46:41 +02:00
|
|
|
void SplitContainer::resetMouseStatus()
|
|
|
|
{
|
|
|
|
this->mouseOverPoint = QPoint(-10000, -10000);
|
|
|
|
this->update();
|
|
|
|
}
|
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
void SplitContainer::appendNewSplit(bool openChannelNameDialog)
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2018-06-26 17:06:17 +02:00
|
|
|
assertInGuiThread();
|
2018-05-25 14:57:17 +02:00
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
Split *split = new Split(this);
|
|
|
|
this->appendSplit(split);
|
2018-04-07 12:43:28 +02:00
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
if (openChannelNameDialog) {
|
|
|
|
split->showChangeChannelPopup("Open channel name", true, [=](bool ok) {
|
|
|
|
if (!ok) {
|
|
|
|
this->deleteSplit(split);
|
2018-04-07 12:43:28 +02:00
|
|
|
}
|
2018-05-10 19:50:31 +02:00
|
|
|
});
|
2017-01-01 02:30:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
void SplitContainer::appendSplit(Split *split)
|
2017-01-01 02:30:42 +01:00
|
|
|
{
|
2018-05-10 19:50:31 +02:00
|
|
|
this->insertSplit(split, Direction::Right);
|
2017-01-01 02:30:42 +01:00
|
|
|
}
|
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
void SplitContainer::insertSplit(Split *split, const Position &position)
|
2017-07-02 13:10:06 +02:00
|
|
|
{
|
2018-05-24 17:22:51 +02:00
|
|
|
this->insertSplit(split, position.direction, reinterpret_cast<Node *>(position.relativeNode));
|
2017-07-02 13:10:06 +02:00
|
|
|
}
|
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
void SplitContainer::insertSplit(Split *split, Direction direction, Split *relativeTo)
|
2018-04-06 23:31:34 +02:00
|
|
|
{
|
2018-05-10 19:50:31 +02:00
|
|
|
Node *node = this->baseNode.findNodeContainingSplit(relativeTo);
|
|
|
|
assert(node != nullptr);
|
2018-04-06 23:31:34 +02:00
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
this->insertSplit(split, direction, node);
|
|
|
|
}
|
2018-04-06 23:31:34 +02:00
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
void SplitContainer::insertSplit(Split *split, Direction direction, Node *relativeTo)
|
|
|
|
{
|
2018-06-26 17:06:17 +02:00
|
|
|
assertInGuiThread();
|
2018-05-25 14:57:17 +02:00
|
|
|
|
2018-05-10 23:58:07 +02:00
|
|
|
split->setContainer(this);
|
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
if (relativeTo == nullptr) {
|
|
|
|
if (this->baseNode.type == Node::EmptyRoot) {
|
|
|
|
this->baseNode.setSplit(split);
|
|
|
|
} else if (this->baseNode.type == Node::_Split) {
|
|
|
|
this->baseNode.nestSplitIntoCollection(split, direction);
|
|
|
|
} else {
|
|
|
|
this->baseNode.insertSplitRelative(split, direction);
|
2018-04-06 23:31:34 +02:00
|
|
|
}
|
2018-05-10 19:50:31 +02:00
|
|
|
} else {
|
|
|
|
assert(this->baseNode.isOrContainsNode(relativeTo));
|
2018-04-06 23:31:34 +02:00
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
relativeTo->insertSplitRelative(split, direction);
|
2018-04-06 23:31:34 +02:00
|
|
|
}
|
|
|
|
|
2018-05-24 17:13:46 +02:00
|
|
|
this->addSplit(split);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SplitContainer::addSplit(Split *split)
|
|
|
|
{
|
2018-06-26 17:06:17 +02:00
|
|
|
assertInGuiThread();
|
2018-05-25 14:57:17 +02:00
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
split->setParent(this);
|
|
|
|
split->show();
|
2018-05-10 23:58:07 +02:00
|
|
|
split->giveFocus(Qt::MouseFocusReason);
|
2018-05-25 16:20:39 +02:00
|
|
|
this->unsetCursor();
|
2018-05-10 19:50:31 +02:00
|
|
|
this->splits.push_back(split);
|
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
this->refreshTabTitle();
|
2018-05-10 23:58:07 +02:00
|
|
|
|
2018-05-23 04:22:17 +02:00
|
|
|
split->getChannelView().tabHighlightRequested.connect([this](HighlightState state) {
|
|
|
|
if (this->tab != nullptr) {
|
|
|
|
this->tab->setHighlightState(state);
|
|
|
|
}
|
|
|
|
});
|
2018-05-25 16:11:03 +02:00
|
|
|
|
2018-05-25 14:57:17 +02:00
|
|
|
split->focused.connect([this, split] { this->setSelected(split); });
|
2018-05-23 04:22:17 +02:00
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
this->layout();
|
2018-04-06 23:31:34 +02:00
|
|
|
}
|
|
|
|
|
2018-05-25 14:57:17 +02:00
|
|
|
void SplitContainer::setSelected(Split *split)
|
|
|
|
{
|
|
|
|
this->selected = split;
|
|
|
|
|
|
|
|
if (Node *node = this->baseNode.findNodeContainingSplit(split)) {
|
|
|
|
this->setPreferedTargetRecursive(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SplitContainer::setPreferedTargetRecursive(Node *node)
|
|
|
|
{
|
|
|
|
if (node->parent != nullptr) {
|
|
|
|
node->parent->preferedFocusTarget = node;
|
|
|
|
|
|
|
|
this->setPreferedTargetRecursive(node->parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
SplitContainer::Position SplitContainer::releaseSplit(Split *split)
|
2017-07-02 13:10:06 +02:00
|
|
|
{
|
2018-06-26 17:06:17 +02:00
|
|
|
assertInGuiThread();
|
2018-05-25 14:57:17 +02:00
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
Node *node = this->baseNode.findNodeContainingSplit(split);
|
|
|
|
assert(node != nullptr);
|
|
|
|
|
|
|
|
this->splits.erase(std::find(this->splits.begin(), this->splits.end(), split));
|
|
|
|
split->setParent(nullptr);
|
|
|
|
Position position = node->releaseSplit();
|
|
|
|
this->layout();
|
2018-05-25 14:57:17 +02:00
|
|
|
if (splits.size() == 0) {
|
|
|
|
this->setSelected(nullptr);
|
2018-05-25 16:20:39 +02:00
|
|
|
this->setCursor(Qt::PointingHandCursor);
|
2018-05-25 14:57:17 +02:00
|
|
|
} else {
|
2018-05-10 23:58:07 +02:00
|
|
|
this->splits.front()->giveFocus(Qt::MouseFocusReason);
|
|
|
|
}
|
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
this->refreshTabTitle();
|
2018-05-10 23:58:07 +02:00
|
|
|
|
2018-05-25 16:11:03 +02:00
|
|
|
// fourtf: really bad
|
|
|
|
split->getChannelView().tabHighlightRequested.disconnectAll();
|
|
|
|
|
2018-05-23 04:22:17 +02:00
|
|
|
split->getChannelView().tabHighlightRequested.disconnectAll();
|
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
return position;
|
2017-07-02 13:10:06 +02:00
|
|
|
}
|
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
SplitContainer::Position SplitContainer::deleteSplit(Split *split)
|
2017-07-02 13:10:06 +02:00
|
|
|
{
|
2018-06-26 17:06:17 +02:00
|
|
|
assertInGuiThread();
|
2018-05-10 19:50:31 +02:00
|
|
|
assert(split != nullptr);
|
2017-07-02 13:10:06 +02:00
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
split->deleteLater();
|
|
|
|
return releaseSplit(split);
|
2017-07-02 13:10:06 +02:00
|
|
|
}
|
|
|
|
|
2018-05-25 14:57:17 +02:00
|
|
|
void SplitContainer::selectNextSplit(Direction direction)
|
|
|
|
{
|
2018-06-26 17:06:17 +02:00
|
|
|
assertInGuiThread();
|
2018-05-25 14:57:17 +02:00
|
|
|
|
|
|
|
if (Node *node = this->baseNode.findNodeContainingSplit(this->selected)) {
|
|
|
|
this->selectSplitRecursive(node, direction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SplitContainer::selectSplitRecursive(Node *node, Direction direction)
|
|
|
|
{
|
|
|
|
if (node->parent != nullptr) {
|
|
|
|
if (node->parent->type == Node::toContainerType(direction)) {
|
|
|
|
auto &siblings = node->parent->children;
|
|
|
|
|
|
|
|
auto it = std::find_if(siblings.begin(), siblings.end(),
|
|
|
|
[node](const auto &other) { return other.get() == node; });
|
|
|
|
assert(it != siblings.end());
|
|
|
|
|
|
|
|
if (direction == Direction::Left || direction == Direction::Above) {
|
|
|
|
if (it == siblings.begin()) {
|
|
|
|
this->selectSplitRecursive(node->parent, direction);
|
|
|
|
} else {
|
|
|
|
this->focusSplitRecursive(siblings[it - siblings.begin() - 1].get(), direction);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (it->get() == siblings.back().get()) {
|
|
|
|
this->selectSplitRecursive(node->parent, direction);
|
|
|
|
} else {
|
|
|
|
this->focusSplitRecursive(siblings[it - siblings.begin() + 1].get(), direction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this->selectSplitRecursive(node->parent, direction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SplitContainer::focusSplitRecursive(Node *node, Direction direction)
|
|
|
|
{
|
|
|
|
switch (node->type) {
|
|
|
|
case Node::_Split: {
|
|
|
|
node->split->giveFocus(Qt::OtherFocusReason);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Node::HorizontalContainer:
|
|
|
|
case Node::VerticalContainer: {
|
|
|
|
auto &children = node->children;
|
|
|
|
|
|
|
|
auto it = std::find_if(children.begin(), children.end(), [node](const auto &other) {
|
|
|
|
return node->preferedFocusTarget == other.get();
|
|
|
|
});
|
|
|
|
|
|
|
|
if (it != children.end()) {
|
|
|
|
this->focusSplitRecursive(it->get(), direction);
|
|
|
|
} else {
|
|
|
|
this->focusSplitRecursive(node->children.front().get(), direction);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default:;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
void SplitContainer::layout()
|
2017-08-12 15:41:14 +02:00
|
|
|
{
|
2018-05-10 19:50:31 +02:00
|
|
|
this->baseNode.geometry = this->rect();
|
2017-08-12 15:41:14 +02:00
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
std::vector<DropRect> _dropRects;
|
2018-05-16 14:55:45 +02:00
|
|
|
std::vector<ResizeRect> _resizeRects;
|
2018-06-01 14:20:46 +02:00
|
|
|
this->baseNode.layout(Split::modifierStatus == showAddSplitRegions || this->isDragging,
|
|
|
|
this->getScale(), _dropRects, _resizeRects);
|
2017-08-12 15:41:14 +02:00
|
|
|
|
2018-05-10 23:58:07 +02:00
|
|
|
this->dropRects = _dropRects;
|
|
|
|
|
|
|
|
for (Split *split : this->splits) {
|
|
|
|
const QRect &g = split->geometry();
|
|
|
|
|
|
|
|
Node *node = this->baseNode.findNodeContainingSplit(split);
|
|
|
|
|
|
|
|
_dropRects.push_back(DropRect(QRect(g.left(), g.top(), g.width() / 4, g.height()),
|
|
|
|
Position(node, Direction::Left)));
|
|
|
|
_dropRects.push_back(
|
|
|
|
DropRect(QRect(g.left() + g.width() / 4 * 3, g.top(), g.width() / 4, g.height()),
|
|
|
|
Position(node, Direction::Right)));
|
|
|
|
|
|
|
|
_dropRects.push_back(DropRect(QRect(g.left(), g.top(), g.width(), g.height() / 2),
|
|
|
|
Position(node, Direction::Above)));
|
|
|
|
_dropRects.push_back(
|
|
|
|
DropRect(QRect(g.left(), g.top() + g.height() / 2, g.width(), g.height() / 2),
|
|
|
|
Position(node, Direction::Below)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->splits.empty()) {
|
|
|
|
QRect g = this->rect();
|
|
|
|
_dropRects.push_back(DropRect(QRect(g.left(), g.top(), g.width(), g.height()),
|
|
|
|
Position(nullptr, Direction::Below)));
|
|
|
|
}
|
|
|
|
|
|
|
|
this->overlay.setRects(std::move(_dropRects));
|
2018-05-16 14:55:45 +02:00
|
|
|
|
|
|
|
// handle resizeHandles
|
|
|
|
if (this->resizeHandles.size() < _resizeRects.size()) {
|
|
|
|
while (this->resizeHandles.size() < _resizeRects.size()) {
|
|
|
|
this->resizeHandles.push_back(std::make_unique<ResizeHandle>(this));
|
|
|
|
}
|
|
|
|
} else if (this->resizeHandles.size() > _resizeRects.size()) {
|
|
|
|
this->resizeHandles.resize(_resizeRects.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-05-24 17:13:46 +02:00
|
|
|
size_t i = 0;
|
2018-05-16 14:55:45 +02:00
|
|
|
for (ResizeRect &resizeRect : _resizeRects) {
|
|
|
|
ResizeHandle *handle = this->resizeHandles[i].get();
|
|
|
|
handle->setGeometry(resizeRect.rect);
|
|
|
|
handle->setVertical(resizeRect.vertical);
|
|
|
|
handle->node = resizeRect.node;
|
|
|
|
|
2018-06-01 14:20:46 +02:00
|
|
|
if (Split::modifierStatus == showResizeHandlesModifiers) {
|
2018-05-16 14:55:45 +02:00
|
|
|
handle->show();
|
|
|
|
handle->raise();
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// redraw
|
2018-05-10 19:50:31 +02:00
|
|
|
this->update();
|
|
|
|
}
|
2017-08-12 15:41:14 +02:00
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
void SplitContainer::resizeEvent(QResizeEvent *event)
|
|
|
|
{
|
|
|
|
BaseWidget::resizeEvent(event);
|
2017-08-12 15:41:14 +02:00
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
this->layout();
|
|
|
|
}
|
2017-08-12 15:41:14 +02:00
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
void SplitContainer::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (event->button() == Qt::LeftButton) {
|
|
|
|
if (this->splits.size() == 0) {
|
|
|
|
// "Add Chat" was clicked
|
|
|
|
this->appendNewSplit(true);
|
2018-06-01 14:20:46 +02:00
|
|
|
this->mouseOverPoint = QPoint(-10000, -10000);
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-10 23:58:07 +02:00
|
|
|
// this->setCursor(QCursor(Qt::ArrowCursor));
|
2018-05-10 19:50:31 +02:00
|
|
|
} else {
|
|
|
|
auto it =
|
|
|
|
std::find_if(this->dropRects.begin(), this->dropRects.end(),
|
|
|
|
[event](DropRect &rect) { return rect.rect.contains(event->pos()); });
|
|
|
|
if (it != this->dropRects.end()) {
|
|
|
|
this->insertSplit(new Split(this), it->position);
|
2017-08-12 15:41:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
void SplitContainer::paintEvent(QPaintEvent *)
|
2017-08-12 15:41:14 +02:00
|
|
|
{
|
2018-05-10 19:50:31 +02:00
|
|
|
QPainter painter(this);
|
2017-08-12 15:41:14 +02:00
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
if (this->splits.size() == 0) {
|
|
|
|
painter.fillRect(rect(), this->themeManager->splits.background);
|
2017-08-12 15:41:14 +02:00
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
painter.setPen(this->themeManager->splits.header.text);
|
2017-08-12 15:41:14 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
QString text = "Click to add a split";
|
2017-08-12 15:41:14 +02:00
|
|
|
|
2018-05-23 11:59:37 +02:00
|
|
|
Notebook *notebook = dynamic_cast<Notebook *>(this->parentWidget());
|
2017-08-12 15:41:14 +02:00
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
if (notebook != nullptr) {
|
2018-05-23 04:22:17 +02:00
|
|
|
if (notebook->getPageCount() > 1) {
|
2018-05-16 14:55:45 +02:00
|
|
|
text += "\n\nTip: After adding a split you can hold <Alt> to move it or split it "
|
|
|
|
"further.";
|
2018-05-10 19:50:31 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-12 15:41:14 +02:00
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
painter.drawText(rect(), text, QTextOption(Qt::AlignCenter));
|
|
|
|
} else {
|
2018-06-07 17:43:21 +02:00
|
|
|
if (getApp()->themes->isLightTheme()) {
|
|
|
|
painter.fillRect(rect(), QColor("#999"));
|
|
|
|
} else {
|
|
|
|
painter.fillRect(rect(), QColor("#555"));
|
|
|
|
}
|
2017-08-12 15:41:14 +02:00
|
|
|
}
|
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
for (DropRect &dropRect : this->dropRects) {
|
2018-06-01 14:20:46 +02:00
|
|
|
QColor border = getApp()->themes->splits.dropTargetRectBorder;
|
|
|
|
QColor background = getApp()->themes->splits.dropTargetRect;
|
2018-05-10 23:58:07 +02:00
|
|
|
|
|
|
|
if (!dropRect.rect.contains(this->mouseOverPoint)) {
|
|
|
|
// border.setAlphaF(0.1);
|
2018-06-01 14:20:46 +02:00
|
|
|
// background.setAlphaF(0.1);
|
|
|
|
} else {
|
|
|
|
// background.setAlphaF(0.1);
|
|
|
|
border.setAlpha(255);
|
2018-05-10 23:58:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
painter.setPen(border);
|
|
|
|
painter.setBrush(background);
|
2017-08-12 15:41:14 +02:00
|
|
|
|
2018-06-01 14:20:46 +02:00
|
|
|
auto rect = dropRect.rect.marginsRemoved(QMargins(2, 2, 2, 2));
|
|
|
|
|
|
|
|
painter.drawRect(rect);
|
|
|
|
|
|
|
|
int s = std::min<int>(dropRect.rect.width(), dropRect.rect.height()) - 12;
|
|
|
|
|
2018-06-07 17:43:21 +02:00
|
|
|
if (this->themeManager->isLightTheme()) {
|
|
|
|
painter.setPen(QColor(0, 0, 0));
|
|
|
|
} else {
|
|
|
|
painter.setPen(QColor(255, 255, 255));
|
|
|
|
}
|
2018-06-01 14:20:46 +02:00
|
|
|
painter.drawLine(rect.left() + rect.width() / 2 - (s / 2), rect.top() + rect.height() / 2,
|
|
|
|
rect.left() + rect.width() / 2 + (s / 2), rect.top() + rect.height() / 2);
|
|
|
|
painter.drawLine(rect.left() + rect.width() / 2, rect.top() + rect.height() / 2 - (s / 2),
|
|
|
|
rect.left() + rect.width() / 2, rect.top() + rect.height() / 2 + (s / 2));
|
2017-01-01 13:07:36 +01:00
|
|
|
}
|
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
QBrush accentColor = (QApplication::activeWindow() == this->window()
|
|
|
|
? this->themeManager->tabs.selected.backgrounds.regular
|
|
|
|
: this->themeManager->tabs.selected.backgrounds.unfocused);
|
2017-01-16 03:15:07 +01:00
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
painter.fillRect(0, 0, width(), 1, accentColor);
|
2017-01-01 13:07:36 +01:00
|
|
|
}
|
|
|
|
|
2017-11-12 17:21:50 +01:00
|
|
|
void SplitContainer::dragEnterEvent(QDragEnterEvent *event)
|
2017-01-01 02:30:42 +01:00
|
|
|
{
|
2018-05-16 14:55:45 +02:00
|
|
|
if (!event->mimeData()->hasFormat("chatterino/split"))
|
|
|
|
return;
|
2017-01-01 02:30:42 +01:00
|
|
|
|
2018-05-23 12:35:10 +02:00
|
|
|
if (!SplitContainer::isDraggingSplit)
|
2018-05-16 14:55:45 +02:00
|
|
|
return;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2018-05-10 19:50:31 +02:00
|
|
|
this->isDragging = true;
|
|
|
|
this->layout();
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2018-05-10 23:58:07 +02:00
|
|
|
this->overlay.setGeometry(this->rect());
|
|
|
|
this->overlay.show();
|
|
|
|
this->overlay.raise();
|
2017-01-01 02:30:42 +01:00
|
|
|
}
|
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
void SplitContainer::mouseMoveEvent(QMouseEvent *event)
|
2017-01-01 02:30:42 +01:00
|
|
|
{
|
2018-06-01 14:46:41 +02:00
|
|
|
if (Split::modifierStatus == showSplitOverlayModifiers) {
|
|
|
|
this->setCursor(Qt::PointingHandCursor);
|
|
|
|
}
|
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
this->mouseOverPoint = event->pos();
|
|
|
|
this->update();
|
|
|
|
}
|
2017-01-01 02:30:42 +01:00
|
|
|
|
2018-05-24 17:13:46 +02:00
|
|
|
void SplitContainer::leaveEvent(QEvent *)
|
2018-05-16 14:55:45 +02:00
|
|
|
{
|
2018-05-23 04:22:17 +02:00
|
|
|
this->mouseOverPoint = QPoint(-10000, -10000);
|
2018-05-16 14:55:45 +02:00
|
|
|
this->update();
|
2017-01-01 02:30:42 +01:00
|
|
|
}
|
|
|
|
|
2018-05-31 16:02:20 +02:00
|
|
|
void SplitContainer::focusInEvent(QFocusEvent *)
|
|
|
|
{
|
|
|
|
if (this->baseNode.findNodeContainingSplit(this->selected) != nullptr) {
|
|
|
|
this->selected->setFocus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->splits.size() != 0) {
|
|
|
|
this->splits.front()->setFocus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
void SplitContainer::refreshTabTitle()
|
2017-01-01 02:30:42 +01:00
|
|
|
{
|
2018-05-23 04:22:17 +02:00
|
|
|
if (this->tab == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-16 14:55:45 +02:00
|
|
|
|
|
|
|
QString newTitle = "";
|
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
for (const auto &chatWidget : this->splits) {
|
|
|
|
auto channelName = chatWidget->getChannel()->name;
|
|
|
|
if (channelName.isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!first) {
|
|
|
|
newTitle += ", ";
|
|
|
|
}
|
|
|
|
newTitle += channelName;
|
|
|
|
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newTitle.isEmpty()) {
|
|
|
|
newTitle = "empty";
|
|
|
|
}
|
|
|
|
|
2018-06-01 16:01:49 +02:00
|
|
|
this->tab->setDefaultTitle(newTitle);
|
2017-01-01 02:30:42 +01:00
|
|
|
}
|
|
|
|
|
2018-05-25 14:57:17 +02:00
|
|
|
int SplitContainer::getSplitCount()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<Split *> SplitContainer::getSplits() const
|
|
|
|
{
|
|
|
|
return this->splits;
|
|
|
|
}
|
|
|
|
|
|
|
|
SplitContainer::Node *SplitContainer::getBaseNode()
|
|
|
|
{
|
|
|
|
return &this->baseNode;
|
|
|
|
}
|
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
void SplitContainer::decodeFromJson(QJsonObject &obj)
|
2017-01-01 02:30:42 +01:00
|
|
|
{
|
2018-05-16 14:55:45 +02:00
|
|
|
assert(this->baseNode.type == Node::EmptyRoot);
|
2017-01-01 02:30:42 +01:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
this->decodeNodeRecusively(obj, &this->baseNode);
|
2017-08-12 15:41:14 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
void SplitContainer::decodeNodeRecusively(QJsonObject &obj, Node *node)
|
|
|
|
{
|
|
|
|
QString type = obj.value("type").toString();
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
if (type == "split") {
|
|
|
|
auto *split = new Split(this);
|
2018-06-28 19:38:57 +02:00
|
|
|
split->setChannel(WindowManager::decodeChannel(obj.value("data").toObject()));
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
this->appendSplit(split);
|
|
|
|
} else if (type == "horizontal" || type == "vertical") {
|
|
|
|
bool vertical = type == "vertical";
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
Direction direction = vertical ? Direction::Below : Direction::Right;
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
node->type = vertical ? Node::VerticalContainer : Node::HorizontalContainer;
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
for (QJsonValue _val : obj.value("items").toArray()) {
|
|
|
|
auto _obj = _val.toObject();
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
auto _type = _obj.value("type");
|
|
|
|
if (_type == "split") {
|
|
|
|
auto *split = new Split(this);
|
2018-07-03 17:19:49 +02:00
|
|
|
split->setChannel(WindowManager::decodeChannel(_obj.value("data").toObject()));
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-24 17:13:46 +02:00
|
|
|
Node *_node = new Node();
|
|
|
|
_node->parent = node;
|
|
|
|
_node->split = split;
|
|
|
|
_node->type = Node::_Split;
|
|
|
|
|
|
|
|
_node->flexH = _obj.value("flexh").toDouble(1.0);
|
|
|
|
_node->flexV = _obj.value("flexv").toDouble(1.0);
|
|
|
|
node->children.emplace_back(_node);
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-24 17:13:46 +02:00
|
|
|
this->addSplit(split);
|
2018-05-16 14:55:45 +02:00
|
|
|
} else {
|
|
|
|
Node *_node = new Node();
|
|
|
|
_node->parent = node;
|
|
|
|
node->children.emplace_back(_node);
|
|
|
|
this->decodeNodeRecusively(_obj, _node);
|
|
|
|
}
|
|
|
|
}
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
if (node->getChildren().size() < 2) {
|
|
|
|
auto *split = new Split(this);
|
2018-07-03 17:19:49 +02:00
|
|
|
split->setChannel(WindowManager::decodeChannel(obj.value("data").toObject()));
|
2018-05-16 14:55:45 +02:00
|
|
|
|
|
|
|
this->insertSplit(split, direction, node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Node
|
|
|
|
//
|
|
|
|
|
|
|
|
SplitContainer::Node::Type SplitContainer::Node::getType()
|
2018-05-10 23:58:07 +02:00
|
|
|
{
|
2018-05-16 14:55:45 +02:00
|
|
|
return this->type;
|
|
|
|
}
|
|
|
|
Split *SplitContainer::Node::getSplit()
|
|
|
|
{
|
|
|
|
return this->split;
|
2018-05-10 23:58:07 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
SplitContainer::Node *SplitContainer::Node::getParent()
|
2018-05-10 23:58:07 +02:00
|
|
|
{
|
2018-05-16 14:55:45 +02:00
|
|
|
return this->parent;
|
2018-05-10 23:58:07 +02:00
|
|
|
}
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
qreal SplitContainer::Node::getHorizontalFlex()
|
|
|
|
{
|
|
|
|
return this->flexH;
|
|
|
|
}
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
qreal SplitContainer::Node::getVerticalFlex()
|
|
|
|
{
|
|
|
|
return this->flexV;
|
|
|
|
}
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
const std::vector<std::unique_ptr<SplitContainer::Node>> &SplitContainer::Node::getChildren()
|
|
|
|
{
|
|
|
|
return this->children;
|
|
|
|
}
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
SplitContainer::Node::Node()
|
|
|
|
: type(SplitContainer::Node::Type::EmptyRoot)
|
|
|
|
, split(nullptr)
|
|
|
|
, parent(nullptr)
|
|
|
|
{
|
|
|
|
}
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
SplitContainer::Node::Node(Split *_split, Node *_parent)
|
|
|
|
: type(Type::_Split)
|
|
|
|
, split(_split)
|
|
|
|
, parent(_parent)
|
|
|
|
{
|
|
|
|
}
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
bool SplitContainer::Node::isOrContainsNode(SplitContainer::Node *_node)
|
|
|
|
{
|
|
|
|
if (this == _node) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
return std::any_of(this->children.begin(), this->children.end(),
|
|
|
|
[_node](std::unique_ptr<Node> &n) { return n->isOrContainsNode(_node); });
|
|
|
|
}
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
SplitContainer::Node *SplitContainer::Node::findNodeContainingSplit(Split *_split)
|
|
|
|
{
|
|
|
|
if (this->type == Type::_Split && this->split == _split) {
|
|
|
|
return this;
|
|
|
|
}
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
for (std::unique_ptr<Node> &node : this->children) {
|
|
|
|
Node *a = node->findNodeContainingSplit(_split);
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
if (a != nullptr) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SplitContainer::Node::insertSplitRelative(Split *_split, Direction _direction)
|
|
|
|
{
|
|
|
|
if (this->parent == nullptr) {
|
|
|
|
switch (this->type) {
|
|
|
|
case Node::EmptyRoot: {
|
|
|
|
this->setSplit(_split);
|
|
|
|
} break;
|
|
|
|
case Node::_Split: {
|
|
|
|
this->nestSplitIntoCollection(_split, _direction);
|
|
|
|
} break;
|
|
|
|
case Node::HorizontalContainer: {
|
|
|
|
this->nestSplitIntoCollection(_split, _direction);
|
|
|
|
} break;
|
|
|
|
case Node::VerticalContainer: {
|
|
|
|
this->nestSplitIntoCollection(_split, _direction);
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parent != nullptr
|
|
|
|
if (parent->type == toContainerType(_direction)) {
|
|
|
|
// hell yeah we'll just insert it next to outselves
|
|
|
|
this->_insertNextToThis(_split, _direction);
|
|
|
|
} else {
|
|
|
|
this->nestSplitIntoCollection(_split, _direction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SplitContainer::Node::nestSplitIntoCollection(Split *_split, Direction _direction)
|
|
|
|
{
|
|
|
|
if (toContainerType(_direction) == this->type) {
|
|
|
|
this->children.emplace_back(new Node(_split, this));
|
|
|
|
} else {
|
|
|
|
// we'll need to nest outselves
|
|
|
|
// move all our data into a new node
|
|
|
|
Node *clone = new Node();
|
|
|
|
clone->type = this->type;
|
|
|
|
clone->children = std::move(this->children);
|
|
|
|
for (std::unique_ptr<Node> &node : clone->children) {
|
|
|
|
node->parent = clone;
|
|
|
|
}
|
|
|
|
clone->split = this->split;
|
|
|
|
clone->parent = this;
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
// add the node to our children and change our type
|
|
|
|
this->children.push_back(std::unique_ptr<Node>(clone));
|
|
|
|
this->type = toContainerType(_direction);
|
|
|
|
this->split = nullptr;
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
clone->_insertNextToThis(_split, _direction);
|
|
|
|
}
|
|
|
|
}
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
void SplitContainer::Node::_insertNextToThis(Split *_split, Direction _direction)
|
|
|
|
{
|
|
|
|
auto &siblings = this->parent->children;
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
qreal width = this->parent->geometry.width() / siblings.size();
|
|
|
|
qreal height = this->parent->geometry.height() / siblings.size();
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
if (siblings.size() == 1) {
|
2018-05-24 17:13:46 +02:00
|
|
|
this->geometry = QRect(0, 0, int(width), int(height));
|
2018-05-16 14:55:45 +02:00
|
|
|
}
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
auto it = std::find_if(siblings.begin(), siblings.end(),
|
|
|
|
[this](auto &node) { return this == node.get(); });
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
assert(it != siblings.end());
|
|
|
|
if (_direction == Direction::Right || _direction == Direction::Below) {
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *node = new Node(_split, this->parent);
|
|
|
|
node->geometry = QRectF(0, 0, width, height);
|
|
|
|
siblings.insert(it, std::unique_ptr<Node>(node));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SplitContainer::Node::setSplit(Split *_split)
|
2017-08-13 16:52:16 +02:00
|
|
|
{
|
2018-05-16 14:55:45 +02:00
|
|
|
assert(this->split == nullptr);
|
|
|
|
assert(this->children.size() == 0);
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
this->split = _split;
|
|
|
|
this->type = Type::_Split;
|
|
|
|
}
|
|
|
|
|
|
|
|
SplitContainer::Position SplitContainer::Node::releaseSplit()
|
|
|
|
{
|
|
|
|
assert(this->type == Type::_Split);
|
|
|
|
|
|
|
|
if (parent == nullptr) {
|
|
|
|
this->type = Type::EmptyRoot;
|
|
|
|
this->split = nullptr;
|
|
|
|
|
|
|
|
Position pos;
|
|
|
|
pos.relativeNode = nullptr;
|
|
|
|
pos.direction = Direction::Right;
|
|
|
|
return pos;
|
|
|
|
} else {
|
|
|
|
auto &siblings = this->parent->children;
|
|
|
|
|
|
|
|
auto it = std::find_if(begin(siblings), end(siblings),
|
|
|
|
[this](auto &node) { return this == node.get(); });
|
|
|
|
assert(it != siblings.end());
|
|
|
|
|
|
|
|
Position position;
|
|
|
|
if (siblings.size() == 2) {
|
|
|
|
// delete this and move split to parent
|
|
|
|
position.relativeNode = this->parent;
|
|
|
|
if (this->parent->type == Type::VerticalContainer) {
|
|
|
|
position.direction = siblings.begin() == it ? Direction::Above : Direction::Below;
|
|
|
|
} else {
|
|
|
|
position.direction = siblings.begin() == it ? Direction::Left : Direction::Right;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *_parent = this->parent;
|
|
|
|
siblings.erase(it);
|
|
|
|
std::unique_ptr<Node> &sibling = siblings.front();
|
|
|
|
_parent->type = sibling->type;
|
|
|
|
_parent->split = sibling->split;
|
|
|
|
std::vector<std::unique_ptr<Node>> nodes = std::move(sibling->children);
|
|
|
|
for (auto &node : nodes) {
|
|
|
|
node->parent = _parent;
|
|
|
|
}
|
|
|
|
_parent->children = std::move(nodes);
|
|
|
|
} else {
|
|
|
|
if (this == siblings.back().get()) {
|
|
|
|
position.direction = this->parent->type == Type::VerticalContainer
|
|
|
|
? Direction::Below
|
|
|
|
: Direction::Right;
|
|
|
|
siblings.erase(it);
|
|
|
|
position.relativeNode = siblings.back().get();
|
|
|
|
} else {
|
|
|
|
position.relativeNode = (it + 1)->get();
|
|
|
|
position.direction = this->parent->type == Type::VerticalContainer
|
|
|
|
? Direction::Above
|
|
|
|
: Direction::Left;
|
|
|
|
siblings.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal SplitContainer::Node::getFlex(bool isVertical)
|
|
|
|
{
|
|
|
|
return isVertical ? this->flexV : this->flexH;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal SplitContainer::Node::getSize(bool isVertical)
|
|
|
|
{
|
|
|
|
return isVertical ? this->geometry.height() : this->geometry.width();
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal SplitContainer::Node::getChildrensTotalFlex(bool isVertical)
|
|
|
|
{
|
|
|
|
return std::accumulate(
|
2018-05-24 17:22:51 +02:00
|
|
|
this->children.begin(), this->children.end(), qreal(0),
|
2018-05-16 14:55:45 +02:00
|
|
|
[=](qreal val, std::unique_ptr<Node> &node) { return val + node->getFlex(isVertical); });
|
|
|
|
}
|
|
|
|
|
|
|
|
void SplitContainer::Node::layout(bool addSpacing, float _scale, std::vector<DropRect> &dropRects,
|
|
|
|
std::vector<ResizeRect> &resizeRects)
|
|
|
|
{
|
|
|
|
for (std::unique_ptr<Node> &node : this->children) {
|
|
|
|
if (node->flexH <= 0)
|
|
|
|
node->flexH = 0;
|
|
|
|
if (node->flexV <= 0)
|
|
|
|
node->flexV = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (this->type) {
|
|
|
|
case Node::_Split: {
|
|
|
|
QRect rect = this->geometry.toRect();
|
|
|
|
this->split->setGeometry(rect.marginsRemoved(QMargins(1, 1, 1, 1)));
|
|
|
|
} break;
|
|
|
|
case Node::VerticalContainer:
|
|
|
|
case Node::HorizontalContainer: {
|
|
|
|
bool isVertical = this->type == Node::VerticalContainer;
|
|
|
|
|
|
|
|
// vars
|
2018-05-24 17:22:51 +02:00
|
|
|
qreal minSize = qreal(48 * _scale);
|
2018-05-16 14:55:45 +02:00
|
|
|
|
|
|
|
qreal totalFlex = this->getChildrensTotalFlex(isVertical);
|
|
|
|
qreal totalSize = std::accumulate(
|
2018-05-24 17:22:51 +02:00
|
|
|
this->children.begin(), this->children.end(), qreal(0),
|
2018-05-16 14:55:45 +02:00
|
|
|
[=](int val, std::unique_ptr<Node> &node) {
|
|
|
|
return val + std::max<qreal>(this->getSize(isVertical) / totalFlex *
|
|
|
|
node->getFlex(isVertical),
|
|
|
|
minSize);
|
|
|
|
});
|
|
|
|
|
|
|
|
qreal sizeMultiplier = this->getSize(isVertical) / totalSize;
|
|
|
|
QRectF childRect = this->geometry;
|
|
|
|
|
|
|
|
// add spacing if reqested
|
|
|
|
if (addSpacing) {
|
2018-05-24 17:22:51 +02:00
|
|
|
qreal offset =
|
|
|
|
std::min<qreal>(this->getSize(!isVertical) * 0.1, qreal(_scale * 24));
|
2018-05-16 14:55:45 +02:00
|
|
|
|
|
|
|
// droprect left / above
|
|
|
|
dropRects.emplace_back(
|
2018-05-24 17:22:51 +02:00
|
|
|
QRectF(this->geometry.left(), this->geometry.top(),
|
|
|
|
isVertical ? offset : this->geometry.width(),
|
|
|
|
isVertical ? this->geometry.height() : offset)
|
|
|
|
.toRect(),
|
2018-05-16 14:55:45 +02:00
|
|
|
Position(this, isVertical ? Direction::Left : Direction::Above));
|
|
|
|
|
|
|
|
// droprect right / below
|
|
|
|
if (isVertical) {
|
|
|
|
dropRects.emplace_back(
|
2018-05-24 17:22:51 +02:00
|
|
|
QRectF(this->geometry.right() - offset, this->geometry.top(), offset,
|
|
|
|
this->geometry.height())
|
|
|
|
.toRect(),
|
2018-05-16 14:55:45 +02:00
|
|
|
Position(this, Direction::Right));
|
|
|
|
} else {
|
|
|
|
dropRects.emplace_back(
|
2018-05-24 17:22:51 +02:00
|
|
|
QRectF(this->geometry.left(), this->geometry.bottom() - offset,
|
|
|
|
this->geometry.width(), offset)
|
|
|
|
.toRect(),
|
2018-05-16 14:55:45 +02:00
|
|
|
Position(this, Direction::Below));
|
|
|
|
}
|
|
|
|
|
|
|
|
// shrink childRect
|
|
|
|
if (isVertical) {
|
|
|
|
childRect.setLeft(childRect.left() + offset);
|
|
|
|
childRect.setRight(childRect.right() - offset);
|
|
|
|
} else {
|
|
|
|
childRect.setTop(childRect.top() + offset);
|
|
|
|
childRect.setBottom(childRect.bottom() - offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate children
|
|
|
|
qreal pos = isVertical ? childRect.top() : childRect.left();
|
|
|
|
for (std::unique_ptr<Node> &child : this->children) {
|
|
|
|
// set rect
|
|
|
|
QRectF rect = childRect;
|
|
|
|
if (isVertical) {
|
|
|
|
rect.setTop(pos);
|
|
|
|
rect.setHeight(
|
|
|
|
std::max<qreal>(this->geometry.height() / totalFlex * child->flexV,
|
|
|
|
minSize) *
|
|
|
|
sizeMultiplier);
|
|
|
|
} else {
|
|
|
|
rect.setLeft(pos);
|
|
|
|
rect.setWidth(std::max<qreal>(this->geometry.width() / totalFlex * child->flexH,
|
|
|
|
minSize) *
|
|
|
|
sizeMultiplier);
|
|
|
|
}
|
|
|
|
|
|
|
|
child->geometry = rect;
|
|
|
|
child->layout(addSpacing, _scale, dropRects, resizeRects);
|
|
|
|
|
|
|
|
pos += child->getSize(isVertical);
|
|
|
|
|
|
|
|
// add resize rect
|
|
|
|
if (child != this->children.front()) {
|
2018-05-24 17:22:51 +02:00
|
|
|
QRectF r = isVertical ? QRectF(this->geometry.left(), child->geometry.top() - 4,
|
|
|
|
this->geometry.width(), 8)
|
|
|
|
: QRectF(child->geometry.left() - 4, this->geometry.top(),
|
|
|
|
8, this->geometry.height());
|
|
|
|
resizeRects.push_back(ResizeRect(r.toRect(), child.get(), isVertical));
|
2018-05-16 14:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// normalize flex
|
|
|
|
if (isVertical) {
|
|
|
|
child->flexV = child->flexV / totalFlex * this->children.size();
|
|
|
|
child->flexH = 1;
|
|
|
|
} else {
|
|
|
|
child->flexH = child->flexH / totalFlex * this->children.size();
|
|
|
|
child->flexV = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
SplitContainer::Node::Type SplitContainer::Node::toContainerType(Direction _dir)
|
|
|
|
{
|
|
|
|
return _dir == Direction::Left || _dir == Direction::Right ? Type::HorizontalContainer
|
|
|
|
: Type::VerticalContainer;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// DropOverlay
|
|
|
|
//
|
|
|
|
|
|
|
|
SplitContainer::DropOverlay::DropOverlay(SplitContainer *_parent)
|
|
|
|
: QWidget(_parent)
|
|
|
|
, mouseOverPoint(-10000, -10000)
|
2018-05-24 17:13:46 +02:00
|
|
|
, parent(_parent)
|
2018-05-16 14:55:45 +02:00
|
|
|
{
|
|
|
|
this->setMouseTracking(true);
|
|
|
|
this->setAcceptDrops(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SplitContainer::DropOverlay::setRects(std::vector<SplitContainer::DropRect> _rects)
|
|
|
|
{
|
|
|
|
this->rects = std::move(_rects);
|
|
|
|
}
|
|
|
|
|
|
|
|
// pajlada::Signals::NoArgSignal dragEnded;
|
|
|
|
|
2018-05-24 17:13:46 +02:00
|
|
|
void SplitContainer::DropOverlay::paintEvent(QPaintEvent *)
|
2018-05-16 14:55:45 +02:00
|
|
|
{
|
|
|
|
QPainter painter(this);
|
|
|
|
|
|
|
|
// painter.fillRect(this->rect(), QColor("#334"));
|
|
|
|
|
|
|
|
bool foundMover = false;
|
|
|
|
|
|
|
|
for (DropRect &rect : this->rects) {
|
|
|
|
if (!foundMover && rect.rect.contains(this->mouseOverPoint)) {
|
|
|
|
painter.setBrush(getApp()->themes->splits.dropPreview);
|
|
|
|
painter.setPen(getApp()->themes->splits.dropPreviewBorder);
|
|
|
|
foundMover = true;
|
|
|
|
} else {
|
|
|
|
painter.setBrush(QColor(0, 0, 0, 0));
|
|
|
|
painter.setPen(QColor(0, 0, 0, 0));
|
|
|
|
// painter.setPen(getApp()->themes->splits.dropPreviewBorder);
|
|
|
|
}
|
|
|
|
|
|
|
|
painter.drawRect(rect.rect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SplitContainer::DropOverlay::dragEnterEvent(QDragEnterEvent *event)
|
|
|
|
{
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SplitContainer::DropOverlay::dragMoveEvent(QDragMoveEvent *event)
|
|
|
|
{
|
|
|
|
event->acceptProposedAction();
|
|
|
|
|
|
|
|
this->mouseOverPoint = event->pos();
|
|
|
|
this->update();
|
|
|
|
}
|
|
|
|
|
2018-05-24 17:22:51 +02:00
|
|
|
void SplitContainer::DropOverlay::dragLeaveEvent(QDragLeaveEvent *)
|
2018-05-16 14:55:45 +02:00
|
|
|
{
|
|
|
|
this->mouseOverPoint = QPoint(-10000, -10000);
|
|
|
|
this->close();
|
|
|
|
this->dragEnded.invoke();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SplitContainer::DropOverlay::dropEvent(QDropEvent *event)
|
|
|
|
{
|
|
|
|
Position *position = nullptr;
|
|
|
|
for (DropRect &rect : this->rects) {
|
|
|
|
if (rect.rect.contains(this->mouseOverPoint)) {
|
|
|
|
position = &rect.position;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (position != nullptr) {
|
|
|
|
this->parent->insertSplit(SplitContainer::draggingSplit, *position);
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
this->mouseOverPoint = QPoint(-10000, -10000);
|
|
|
|
this->close();
|
|
|
|
this->dragEnded.invoke();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// ResizeHandle
|
|
|
|
//
|
|
|
|
|
|
|
|
void SplitContainer::ResizeHandle::setVertical(bool isVertical)
|
|
|
|
{
|
|
|
|
this->setCursor(isVertical ? Qt::SplitVCursor : Qt::SplitHCursor);
|
|
|
|
this->vertical = isVertical;
|
|
|
|
}
|
|
|
|
|
|
|
|
SplitContainer::ResizeHandle::ResizeHandle(SplitContainer *_parent)
|
|
|
|
: QWidget(_parent)
|
|
|
|
, parent(_parent)
|
|
|
|
{
|
|
|
|
this->setMouseTracking(true);
|
2018-05-25 15:04:41 +02:00
|
|
|
this->hide();
|
2018-05-16 14:55:45 +02:00
|
|
|
}
|
|
|
|
|
2018-05-24 17:22:51 +02:00
|
|
|
void SplitContainer::ResizeHandle::paintEvent(QPaintEvent *)
|
2018-05-16 14:55:45 +02:00
|
|
|
{
|
|
|
|
QPainter painter(this);
|
2018-06-01 14:20:46 +02:00
|
|
|
painter.setPen(QPen(getApp()->themes->splits.resizeHandle, 2));
|
2018-05-16 14:55:45 +02:00
|
|
|
|
2018-06-01 14:20:46 +02:00
|
|
|
painter.fillRect(this->rect(), getApp()->themes->splits.resizeHandleBackground);
|
2018-05-25 15:03:58 +02:00
|
|
|
|
|
|
|
if (this->vertical) {
|
|
|
|
painter.drawLine(0, this->height() / 2, this->width(), this->height() / 2);
|
|
|
|
} else {
|
|
|
|
painter.drawLine(this->width() / 2, 0, this->width() / 2, this->height());
|
|
|
|
}
|
2018-05-16 14:55:45 +02:00
|
|
|
}
|
|
|
|
|
2018-05-24 17:22:51 +02:00
|
|
|
void SplitContainer::ResizeHandle::mousePressEvent(QMouseEvent *)
|
2018-05-16 14:55:45 +02:00
|
|
|
{
|
|
|
|
this->isMouseDown = true;
|
|
|
|
}
|
|
|
|
|
2018-05-24 17:22:51 +02:00
|
|
|
void SplitContainer::ResizeHandle::mouseReleaseEvent(QMouseEvent *)
|
2018-05-16 14:55:45 +02:00
|
|
|
{
|
|
|
|
this->isMouseDown = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SplitContainer::ResizeHandle::mouseMoveEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (!this->isMouseDown) {
|
2017-08-13 16:52:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
assert(node != nullptr);
|
|
|
|
assert(node->parent != nullptr);
|
2017-08-13 16:52:16 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
auto &siblings = node->parent->getChildren();
|
|
|
|
auto it =
|
|
|
|
std::find_if(siblings.begin(), siblings.end(),
|
|
|
|
[this](const std::unique_ptr<Node> &n) { return n.get() == this->node; });
|
2017-08-17 22:25:41 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
assert(it != siblings.end());
|
|
|
|
Node *before = siblings[it - siblings.begin() - 1].get();
|
2017-08-13 16:52:16 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
QPoint topLeft = this->parent->mapToGlobal(before->geometry.topLeft().toPoint());
|
|
|
|
QPoint bottomRight = this->parent->mapToGlobal(this->node->geometry.bottomRight().toPoint());
|
2017-08-13 16:52:16 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
int globalX = topLeft.x() > event->globalX()
|
|
|
|
? topLeft.x()
|
|
|
|
: (bottomRight.x() < event->globalX() ? bottomRight.x() : event->globalX());
|
|
|
|
int globalY = topLeft.y() > event->globalY()
|
|
|
|
? topLeft.y()
|
|
|
|
: (bottomRight.y() < event->globalY() ? bottomRight.y() : event->globalY());
|
2018-05-10 19:50:31 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
QPoint mousePoint(globalX, globalY);
|
2017-08-17 22:25:41 +02:00
|
|
|
|
2018-05-16 14:55:45 +02:00
|
|
|
if (this->vertical) {
|
|
|
|
qreal totalFlexV = this->node->flexV + before->flexV;
|
|
|
|
before->flexV =
|
|
|
|
totalFlexV * (mousePoint.y() - topLeft.y()) / (bottomRight.y() - topLeft.y());
|
|
|
|
this->node->flexV = totalFlexV - before->flexV;
|
|
|
|
|
|
|
|
this->parent->layout();
|
|
|
|
|
|
|
|
// move handle
|
2018-05-24 17:22:51 +02:00
|
|
|
this->move(this->x(), int(before->geometry.bottom() - 4));
|
2018-05-16 14:55:45 +02:00
|
|
|
} else {
|
|
|
|
qreal totalFlexH = this->node->flexH + before->flexH;
|
|
|
|
before->flexH =
|
|
|
|
totalFlexH * (mousePoint.x() - topLeft.x()) / (bottomRight.x() - topLeft.x());
|
|
|
|
this->node->flexH = totalFlexH - before->flexH;
|
|
|
|
|
|
|
|
this->parent->layout();
|
|
|
|
|
|
|
|
// move handle
|
2018-05-24 17:22:51 +02:00
|
|
|
this->move(int(before->geometry.right() - 4), this->y());
|
2018-05-16 14:55:45 +02:00
|
|
|
}
|
2017-06-26 16:41:20 +02:00
|
|
|
}
|
|
|
|
|
2018-07-04 19:52:11 +02:00
|
|
|
void SplitContainer::ResizeHandle::mouseDoubleClickEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
event->accept();
|
|
|
|
|
|
|
|
for (auto &sibling : this->node->getParent()->getChildren()) {
|
|
|
|
sibling->flexH = 1;
|
|
|
|
sibling->flexV = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->parent->layout();
|
|
|
|
}
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace chatterino
|