mirror-chatterino2/src/widgets/splitcontainer.cpp

493 lines
13 KiB
C++
Raw Normal View History

2017-11-12 17:21:50 +01:00
#include "widgets/splitcontainer.hpp"
2018-05-10 23:58:07 +02:00
#include "application.hpp"
#include "common.hpp"
2017-12-31 00:50:07 +01:00
#include "singletons/thememanager.hpp"
#include "util/helpers.hpp"
#include "util/layoutcreator.hpp"
2017-11-12 17:21:50 +01:00
#include "widgets/helper/notebooktab.hpp"
#include "widgets/notebook.hpp"
2017-11-12 17:21:50 +01:00
#include "widgets/split.hpp"
2016-12-29 17:31:07 +01:00
#include <QApplication>
#include <QDebug>
2017-01-18 04:52:47 +01:00
#include <QHBoxLayout>
#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>
#include <boost/foreach.hpp>
2017-01-18 04:52:47 +01:00
#include <algorithm>
2017-04-14 17:52:22 +02:00
namespace chatterino {
namespace widgets {
2017-01-18 21:30:23 +01:00
2017-11-12 17:21:50 +01:00
bool SplitContainer::isDraggingSplit = false;
Split *SplitContainer::draggingSplit = nullptr;
2018-05-10 19:50:31 +02:00
// SplitContainer::Position SplitContainer::dragOriginalPosition;
2017-01-01 02:30:42 +01:00
SplitContainer::SplitContainer(Notebook *parent, NotebookTab *_tab)
: BaseWidget(parent)
, tab(_tab)
, dropPreview(this)
2018-05-10 23:58:07 +02:00
, mouseOverPoint(-10000, -10000)
, overlay(this)
2017-01-01 02:30:42 +01:00
{
this->tab->page = this;
2017-04-12 17:46:44 +02:00
2018-05-10 23:58:07 +02:00
this->refreshTabTitle();
2017-04-12 17:46:44 +02:00
2018-05-10 23:58:07 +02:00
this->managedConnect(Split::modifierStatusChanged, [this](auto) {
// fourtf: maybe optimize
this->layout();
});
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();
});
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-10 19:50:31 +02:00
NotebookTab *SplitContainer::getTab() const
{
2018-05-10 19:50:31 +02:00
return this->tab;
}
2018-05-10 19:50:31 +02:00
void SplitContainer::appendNewSplit(bool openChannelNameDialog)
2017-04-12 17:46:44 +02:00
{
2018-05-10 19:50:31 +02:00
Split *split = new Split(this);
this->appendSplit(split);
2018-05-10 19:50:31 +02:00
if (openChannelNameDialog) {
split->showChangeChannelPopup("Open channel name", true, [=](bool ok) {
if (!ok) {
this->deleteSplit(split);
}
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)
{
2018-05-10 19:50:31 +02:00
this->insertSplit(split, position.direction, position.relativeNode);
}
2018-05-10 19:50:31 +02:00
void SplitContainer::insertSplit(Split *split, Direction direction, Split *relativeTo)
{
2018-05-10 19:50:31 +02:00
Node *node = this->baseNode.findNodeContainingSplit(relativeTo);
assert(node != nullptr);
2018-05-10 19:50:31 +02:00
this->insertSplit(split, direction, node);
}
2018-05-10 19:50:31 +02:00
void SplitContainer::insertSplit(Split *split, Direction direction, Node *relativeTo)
{
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-05-10 19:50:31 +02:00
} else {
assert(this->baseNode.isOrContainsNode(relativeTo));
2018-05-10 19:50:31 +02:00
relativeTo->insertSplitRelative(split, direction);
}
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-10 19:50:31 +02:00
this->splits.push_back(split);
2018-05-10 23:58:07 +02:00
// this->setAcceptDrops(false);
2018-05-10 19:50:31 +02:00
this->layout();
}
2018-05-10 19:50:31 +02:00
SplitContainer::Position SplitContainer::releaseSplit(Split *split)
{
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-10 23:58:07 +02:00
if (splits.size() != 0) {
this->splits.front()->giveFocus(Qt::MouseFocusReason);
}
// if (this->splits.empty()) {
// this->setAcceptDrops(true);
// }
2018-05-10 19:50:31 +02:00
return position;
}
2018-05-10 19:50:31 +02:00
SplitContainer::Position SplitContainer::deleteSplit(Split *split)
{
2018-05-10 19:50:31 +02:00
assert(split != nullptr);
2018-05-10 19:50:31 +02:00
split->deleteLater();
return releaseSplit(split);
}
2018-05-10 19:50:31 +02:00
void SplitContainer::layout()
{
2018-05-10 19:50:31 +02:00
this->baseNode.geometry = this->rect();
2018-05-10 19:50:31 +02:00
std::vector<DropRect> _dropRects;
2018-05-10 23:58:07 +02:00
this->baseNode.layout(
Split::modifierStatus == (Qt::AltModifier | Qt::ControlModifier) || this->isDragging,
this->getScale(), _dropRects);
2018-05-10 19:50:31 +02:00
_dropRects.clear();
2018-05-10 23:58:07 +02:00
this->baseNode.layout(
Split::modifierStatus == (Qt::AltModifier | Qt::ControlModifier) || this->isDragging,
this->getScale(), _dropRects);
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-10 19:50:31 +02:00
this->update();
}
2018-05-10 19:50:31 +02:00
/// EVENTS
void SplitContainer::resizeEvent(QResizeEvent *event)
{
BaseWidget::resizeEvent(event);
2018-05-10 19:50:31 +02:00
this->layout();
}
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-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);
}
}
}
}
2018-05-10 19:50:31 +02:00
void SplitContainer::paintEvent(QPaintEvent *)
{
2018-05-10 19:50:31 +02:00
QPainter painter(this);
2018-05-10 19:50:31 +02:00
if (this->splits.size() == 0) {
painter.fillRect(rect(), this->themeManager->splits.background);
2018-05-10 19:50:31 +02:00
painter.setPen(this->themeManager->splits.header.text);
2018-05-10 19:50:31 +02:00
QString text = "Click to add a <split>";
2018-05-10 19:50:31 +02:00
Notebook *notebook = dynamic_cast<Notebook *>(this->parentWidget());
2018-05-10 19:50:31 +02:00
if (notebook != nullptr) {
if (notebook->tabCount() > 1) {
text += "\n\ntip: you can drag a <split> while holding <Alt>";
text += "\nor add another one by pressing <Ctrl+T>";
}
}
2018-05-10 19:50:31 +02:00
painter.drawText(rect(), text, QTextOption(Qt::AlignCenter));
} else {
painter.fillRect(rect(), this->themeManager->splits.messageSeperator);
}
2018-05-10 19:50:31 +02:00
for (DropRect &dropRect : this->dropRects) {
2018-05-10 23:58:07 +02:00
QColor border = getApp()->themes->splits.dropPreviewBorder;
QColor background = getApp()->themes->splits.dropPreview;
if (!dropRect.rect.contains(this->mouseOverPoint)) {
// border.setAlphaF(0.1);
background.setAlphaF(0.1);
}
painter.setPen(border);
painter.setBrush(background);
2018-05-10 19:50:31 +02:00
painter.drawRect(dropRect.rect.marginsRemoved(QMargins(2, 2, 2, 2)));
}
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);
2018-05-10 19:50:31 +02:00
painter.fillRect(0, 0, width(), 1, accentColor);
}
2017-11-12 17:21:50 +01:00
void SplitContainer::dragEnterEvent(QDragEnterEvent *event)
2017-01-01 02:30:42 +01:00
{
2018-05-10 23:58:07 +02:00
// if (!event->mimeData()->hasFormat("chatterino/split"))
// return;
2017-01-01 02:30:42 +01:00
2018-05-10 23:58:07 +02:00
// if (!SplitContainer::isDraggingSplit) {
// 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
// if (this->splits.empty()) {
// event->acceptProposedAction();
2018-05-10 19:50:31 +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
}
2017-11-12 17:21:50 +01:00
void SplitContainer::dragMoveEvent(QDragMoveEvent *event)
2017-01-01 02:30:42 +01:00
{
2018-05-10 23:58:07 +02:00
// if (this->splits.empty()) {
// event->acceptProposedAction();
// }
2017-01-01 02:30:42 +01:00
2018-05-10 23:58:07 +02:00
// for (auto &dropRect : this->dropRects) {
// if (dropRect.rect.contains(event->pos())) {
// event->acceptProposedAction();
// return;
// }
// }
// event->setAccepted(false);
2017-01-01 02:30:42 +01:00
}
2017-11-12 17:21:50 +01:00
void SplitContainer::dragLeaveEvent(QDragLeaveEvent *event)
2017-01-01 02:30:42 +01:00
{
2018-05-10 23:58:07 +02:00
// this->isDragging = false;
// this->layout();
2017-01-01 02:30:42 +01:00
}
2017-11-12 17:21:50 +01:00
void SplitContainer::dropEvent(QDropEvent *event)
2017-01-01 02:30:42 +01:00
{
2018-05-10 23:58:07 +02:00
// if (this->splits.empty()) {
// this->insertSplit(SplitContainer::draggingSplit, Direction::Above);
2018-05-10 19:50:31 +02:00
// event->acceptProposedAction();
// }
2017-01-01 02:30:42 +01:00
2018-05-10 23:58:07 +02:00
// this->isDragging = false;
// this->layout();
}
2018-05-10 19:50:31 +02:00
// void SplitContainer::requestFocus(int requestedX, int requestedY)
//{
// // XXX: Perhaps if we request an Y coordinate out of bounds, we shuold set all previously set
// // requestedYs to 0 (if -1 is requested) or that x-coordinates vbox count (if requestedY >=
// // currentvbox.count() is requested)
// if (requestedX < 0 || requestedX >= this->ui.hbox.count()) {
// return;
// }
// QLayoutItem *item = this->ui.hbox.itemAt(requestedX);
// QWidget *xW = item->widget();
// if (item->isEmpty()) {
// qDebug() << "Requested hbox item " << requestedX << "is empty";
// if (xW) {
// qDebug() << "but xW is not null";
// // TODO: figure out what to do here
// }
// return;
// }
// QVBoxLayout *vbox = static_cast<QVBoxLayout *>(item->layout());
// if (requestedY < 0) {
// requestedY = 0;
// } else if (requestedY >= vbox->count()) {
// requestedY = vbox->count() - 1;
// }
// this->lastRequestedY[requestedX] = requestedY;
// QLayoutItem *innerItem = vbox->itemAt(requestedY);
// if (innerItem->isEmpty()) {
// qDebug() << "Requested vbox item " << requestedY << "is empty";
// return;
// }
// QWidget *w = innerItem->widget();
// if (w) {
// Split *chatWidget = static_cast<Split *>(w);
// chatWidget->giveFocus(Qt::OtherFocusReason);
// }
//}
2018-05-10 23:58:07 +02:00
// void SplitContainer::enterEvent(QEvent *event)
2018-05-10 19:50:31 +02:00
//{
// if (this->ui.hbox.count() == 0) {
// this->setCursor(QCursor(Qt::PointingHandCursor));
// } else {
// this->setCursor(QCursor(Qt::ArrowCursor));
// }
//}
2018-05-10 23:58:07 +02:00
void SplitContainer::mouseMoveEvent(QMouseEvent *event)
{
this->mouseOverPoint = event->pos();
this->update();
}
void SplitContainer::leaveEvent(QEvent *event)
{
this->mouseOverPoint = QPoint(-1000, -10000);
this->update();
}
2018-05-10 19:50:31 +02:00
// void SplitContainer::setPreviewRect(QPoint mousePos)
//{
// for (DropRegion region : this->dropRegions) {
// if (region.rect.contains(mousePos)) {
// this->dropPreview.setBounds(region.rect);
// if (!this->dropPreview.isVisible()) {
// this->dropPreview.setGeometry(this->rect());
// this->dropPreview.show();
// this->dropPreview.raise();
// }
// dropPosition = region.position;
// return;
// }
// }
// this->dropPreview.hide();
//}
// bool SplitContainer::eventFilter(QObject *object, QEvent *event)
//{
// if (event->type() == QEvent::FocusIn) {
// QFocusEvent *focusEvent = static_cast<QFocusEvent *>(event);
// this->refreshCurrentFocusCoordinates((focusEvent->reason() == Qt::MouseFocusReason));
// }
// return false;
//}
// void SplitContainer::showEvent(QShowEvent *event)
//{
// // Whenever this notebook page is shown, give focus to the last focused chat widget
// // If this is the first time this notebook page is shown, it will give focus to the
// top-left
// // chat widget
// this->requestFocus(this->currentX, this->currentY);
//}
// static std::pair<int, int> getWidgetPositionInLayout(QLayout *layout, const Split
// *chatWidget)
//{
// for (int i = 0; i < layout->count(); ++i) {
// printf("xD\n");
// }
// return std::make_pair(-1, -1);
//}
// std::pair<int, int> SplitContainer::getChatPosition(const Split *chatWidget)
//{
// auto layout = this->ui.hbox.layout();
// if (layout == nullptr) {
// return std::make_pair(-1, -1);
// }
// return getWidgetPositionInLayout(layout, chatWidget);
//}
// Split *SplitContainer::createChatWidget()
//{
// auto split = new Split(this);
// return split;
//}
void SplitContainer::refreshTabTitle()
{
2018-05-10 19:50:31 +02:00
assert(this->tab != nullptr);
if (!this->tab->useDefaultTitle) {
return;
}
2018-05-10 19:50:31 +02:00
this->tab->setTitle("default title");
2018-05-10 19:50:31 +02:00
// QString newTitle = "";
// bool first = true;
2018-05-10 19:50:31 +02:00
// for (const auto &chatWidget : this->splits) {
// auto channelName = chatWidget->getChannel()->name;
// if (channelName.isEmpty()) {
// continue;
// }
2018-05-10 19:50:31 +02:00
// if (!first) {
// newTitle += ", ";
// }
// newTitle += channelName;
2018-05-10 19:50:31 +02:00
// first = false;
// }
// if (newTitle.isEmpty()) {
// newTitle = "empty";
// }
2018-05-10 19:50:31 +02:00
// this->tab->setTitle(newTitle);
}
2017-04-14 17:52:22 +02:00
} // namespace widgets
} // namespace chatterino