2018-05-08 15:12:04 +02:00
|
|
|
#include "splitoverlay.hpp"
|
|
|
|
|
|
|
|
#include <QEvent>
|
|
|
|
#include <QGraphicsOpacityEffect>
|
|
|
|
#include <QGridLayout>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QPushButton>
|
|
|
|
|
|
|
|
#include "application.hpp"
|
|
|
|
#include "singletons/resourcemanager.hpp"
|
|
|
|
#include "widgets/split.hpp"
|
2018-05-10 19:50:31 +02:00
|
|
|
#include "widgets/splitcontainer.hpp"
|
2018-05-08 15:12:04 +02:00
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace widgets {
|
|
|
|
|
|
|
|
SplitOverlay::SplitOverlay(Split *parent)
|
|
|
|
: BaseWidget(parent)
|
|
|
|
, split(parent)
|
|
|
|
{
|
|
|
|
QGridLayout *layout = new QGridLayout(this);
|
2018-05-10 23:58:07 +02:00
|
|
|
this->_layout = layout;
|
2018-05-08 15:12:04 +02:00
|
|
|
layout->setMargin(1);
|
|
|
|
layout->setSpacing(1);
|
|
|
|
|
|
|
|
layout->setRowStretch(1, 1);
|
|
|
|
layout->setRowStretch(3, 1);
|
|
|
|
layout->setColumnStretch(1, 1);
|
|
|
|
layout->setColumnStretch(3, 1);
|
|
|
|
|
|
|
|
QPushButton *move = new QPushButton(getApp()->resources->split.move, QString());
|
2018-05-10 23:58:07 +02:00
|
|
|
QPushButton *left = this->_left = new QPushButton(getApp()->resources->split.left, QString());
|
|
|
|
QPushButton *right = this->_right =
|
|
|
|
new QPushButton(getApp()->resources->split.right, QString());
|
|
|
|
QPushButton *up = this->_up = new QPushButton(getApp()->resources->split.up, QString());
|
|
|
|
QPushButton *down = this->_down = new QPushButton(getApp()->resources->split.down, QString());
|
2018-05-08 15:12:04 +02:00
|
|
|
|
|
|
|
move->setGraphicsEffect(new QGraphicsOpacityEffect(this));
|
|
|
|
left->setGraphicsEffect(new QGraphicsOpacityEffect(this));
|
|
|
|
right->setGraphicsEffect(new QGraphicsOpacityEffect(this));
|
|
|
|
up->setGraphicsEffect(new QGraphicsOpacityEffect(this));
|
|
|
|
down->setGraphicsEffect(new QGraphicsOpacityEffect(this));
|
|
|
|
|
|
|
|
move->setFlat(true);
|
|
|
|
left->setFlat(true);
|
|
|
|
right->setFlat(true);
|
|
|
|
up->setFlat(true);
|
|
|
|
down->setFlat(true);
|
|
|
|
|
|
|
|
layout->addWidget(move, 2, 2);
|
|
|
|
layout->addWidget(left, 2, 0);
|
|
|
|
layout->addWidget(right, 2, 4);
|
|
|
|
layout->addWidget(up, 0, 2);
|
|
|
|
layout->addWidget(down, 4, 2);
|
|
|
|
|
|
|
|
move->installEventFilter(new ButtonEventFilter(this, SplitMove));
|
|
|
|
left->installEventFilter(new ButtonEventFilter(this, SplitLeft));
|
|
|
|
right->installEventFilter(new ButtonEventFilter(this, SplitRight));
|
|
|
|
up->installEventFilter(new ButtonEventFilter(this, SplitUp));
|
|
|
|
down->installEventFilter(new ButtonEventFilter(this, SplitDown));
|
|
|
|
|
|
|
|
move->setFocusPolicy(Qt::NoFocus);
|
|
|
|
left->setFocusPolicy(Qt::NoFocus);
|
|
|
|
right->setFocusPolicy(Qt::NoFocus);
|
|
|
|
up->setFocusPolicy(Qt::NoFocus);
|
|
|
|
down->setFocusPolicy(Qt::NoFocus);
|
|
|
|
|
2018-05-25 16:11:03 +02:00
|
|
|
move->setCursor(Qt::SizeAllCursor);
|
2018-05-08 20:35:31 +02:00
|
|
|
left->setCursor(Qt::PointingHandCursor);
|
|
|
|
right->setCursor(Qt::PointingHandCursor);
|
|
|
|
up->setCursor(Qt::PointingHandCursor);
|
|
|
|
down->setCursor(Qt::PointingHandCursor);
|
|
|
|
|
|
|
|
this->managedConnect(this->scaleChanged, [=](float _scale) {
|
2018-05-25 16:11:03 +02:00
|
|
|
int a = int(_scale * 30);
|
2018-05-08 15:12:04 +02:00
|
|
|
QSize size(a, a);
|
|
|
|
|
|
|
|
move->setIconSize(size);
|
|
|
|
left->setIconSize(size);
|
|
|
|
right->setIconSize(size);
|
|
|
|
up->setIconSize(size);
|
|
|
|
down->setIconSize(size);
|
|
|
|
});
|
2018-05-17 14:47:23 +02:00
|
|
|
|
|
|
|
this->setMouseTracking(true);
|
2018-05-25 16:11:03 +02:00
|
|
|
|
|
|
|
this->setCursor(Qt::ArrowCursor);
|
2018-05-08 15:12:04 +02:00
|
|
|
}
|
|
|
|
|
2018-05-25 16:11:03 +02:00
|
|
|
void SplitOverlay::paintEvent(QPaintEvent *)
|
2018-05-08 15:12:04 +02:00
|
|
|
{
|
|
|
|
QPainter painter(this);
|
2018-05-17 12:16:13 +02:00
|
|
|
painter.fillRect(this->rect(), QColor(0, 0, 0, 150));
|
2018-05-08 15:12:04 +02:00
|
|
|
|
|
|
|
QRect rect;
|
|
|
|
switch (this->hoveredElement) {
|
|
|
|
case SplitLeft: {
|
|
|
|
rect = QRect(0, 0, this->width() / 2, this->height());
|
|
|
|
} break;
|
2018-05-25 16:11:03 +02:00
|
|
|
|
2018-05-08 15:12:04 +02:00
|
|
|
case SplitRight: {
|
|
|
|
rect = QRect(this->width() / 2, 0, this->width() / 2, this->height());
|
|
|
|
} break;
|
2018-05-25 16:11:03 +02:00
|
|
|
|
2018-05-08 15:12:04 +02:00
|
|
|
case SplitUp: {
|
|
|
|
rect = QRect(0, 0, this->width(), this->height() / 2);
|
|
|
|
} break;
|
2018-05-25 16:11:03 +02:00
|
|
|
|
2018-05-08 15:12:04 +02:00
|
|
|
case SplitDown: {
|
|
|
|
rect = QRect(0, this->height() / 2, this->width(), this->height() / 2);
|
|
|
|
} break;
|
2018-05-25 16:11:03 +02:00
|
|
|
|
|
|
|
default:;
|
2018-05-08 15:12:04 +02:00
|
|
|
}
|
2018-05-25 16:11:03 +02:00
|
|
|
|
2018-05-08 15:12:04 +02:00
|
|
|
if (!rect.isNull()) {
|
2018-05-08 20:35:31 +02:00
|
|
|
painter.setPen(getApp()->themes->splits.dropPreviewBorder);
|
|
|
|
painter.setBrush(getApp()->themes->splits.dropPreview);
|
2018-05-08 15:12:04 +02:00
|
|
|
painter.drawRect(rect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 23:58:07 +02:00
|
|
|
void SplitOverlay::resizeEvent(QResizeEvent *event)
|
|
|
|
{
|
2018-05-11 13:55:10 +02:00
|
|
|
float _scale = this->getScale();
|
|
|
|
bool wideEnough = event->size().width() > 150 * _scale;
|
|
|
|
bool highEnough = event->size().height() > 150 * _scale;
|
2018-05-10 23:58:07 +02:00
|
|
|
|
|
|
|
this->_left->setVisible(wideEnough);
|
|
|
|
this->_right->setVisible(wideEnough);
|
|
|
|
this->_up->setVisible(highEnough);
|
|
|
|
this->_down->setVisible(highEnough);
|
|
|
|
}
|
|
|
|
|
2018-05-17 14:47:23 +02:00
|
|
|
void SplitOverlay::mouseMoveEvent(QMouseEvent *event)
|
|
|
|
{
|
2018-05-17 16:39:38 +02:00
|
|
|
BaseWidget::mouseMoveEvent(event);
|
|
|
|
|
|
|
|
// qDebug() << QGuiApplication::queryKeyboardModifiers();
|
|
|
|
|
|
|
|
// if ((QGuiApplication::queryKeyboardModifiers() & Qt::AltModifier) == Qt::AltModifier) {
|
|
|
|
// this->hide();
|
|
|
|
// }
|
2018-05-17 14:47:23 +02:00
|
|
|
}
|
|
|
|
|
2018-05-08 15:12:04 +02:00
|
|
|
SplitOverlay::ButtonEventFilter::ButtonEventFilter(SplitOverlay *_parent, HoveredElement _element)
|
|
|
|
: QObject(_parent)
|
|
|
|
, parent(_parent)
|
|
|
|
, hoveredElement(_element)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SplitOverlay::ButtonEventFilter::eventFilter(QObject *watched, QEvent *event)
|
|
|
|
{
|
|
|
|
switch (event->type()) {
|
|
|
|
case QEvent::Enter: {
|
|
|
|
QGraphicsOpacityEffect *effect =
|
|
|
|
dynamic_cast<QGraphicsOpacityEffect *>(((QWidget *)watched)->graphicsEffect());
|
|
|
|
|
|
|
|
if (effect != nullptr) {
|
2018-05-16 15:42:45 +02:00
|
|
|
effect->setOpacity(0.99);
|
2018-05-08 15:12:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this->parent->hoveredElement = this->hoveredElement;
|
|
|
|
this->parent->update();
|
|
|
|
} break;
|
|
|
|
case QEvent::Leave: {
|
|
|
|
QGraphicsOpacityEffect *effect =
|
|
|
|
dynamic_cast<QGraphicsOpacityEffect *>(((QWidget *)watched)->graphicsEffect());
|
|
|
|
|
|
|
|
if (effect != nullptr) {
|
|
|
|
effect->setOpacity(0.7);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->parent->hoveredElement = HoveredElement::None;
|
|
|
|
this->parent->update();
|
|
|
|
} break;
|
|
|
|
case QEvent::MouseButtonPress: {
|
|
|
|
if (this->hoveredElement == HoveredElement::SplitMove) {
|
|
|
|
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
|
|
|
|
if (mouseEvent->button() == Qt::LeftButton) {
|
|
|
|
this->parent->split->drag();
|
|
|
|
}
|
|
|
|
return true;
|
2018-05-25 16:11:03 +02:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case QEvent::MouseButtonRelease: {
|
|
|
|
if (this->hoveredElement != HoveredElement::SplitMove) {
|
2018-05-10 19:50:31 +02:00
|
|
|
SplitContainer *container = this->parent->split->getContainer();
|
|
|
|
|
|
|
|
if (container != nullptr) {
|
|
|
|
auto *_split = new Split(container);
|
2018-05-25 16:11:03 +02:00
|
|
|
auto dir = SplitContainer::Direction(this->hoveredElement +
|
|
|
|
SplitContainer::Left - SplitLeft);
|
|
|
|
container->insertSplit(_split, dir, this->parent->split);
|
|
|
|
this->parent->hide();
|
2018-05-10 19:50:31 +02:00
|
|
|
}
|
2018-05-08 15:12:04 +02:00
|
|
|
}
|
|
|
|
} break;
|
2018-05-25 16:11:03 +02:00
|
|
|
default:;
|
2018-05-08 15:12:04 +02:00
|
|
|
}
|
|
|
|
return QObject::eventFilter(watched, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace widgets
|
|
|
|
} // namespace chatterino
|