2021-10-06 00:27:49 +01:00
|
|
|
#include "PanelWidget.hpp"
|
|
|
|
|
|
|
|
|
|
#include <QLayout>
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
namespace Bloom::Widgets
|
|
|
|
|
{
|
|
|
|
|
PanelWidget::PanelWidget(QWidget* parent): QFrame(parent) {
|
|
|
|
|
this->setMouseTracking(false);
|
|
|
|
|
this->setAttribute(Qt::WA_Hover, true);
|
|
|
|
|
}
|
2021-10-06 00:27:49 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void PanelWidget::setMinimumResize(int minimumResize) {
|
|
|
|
|
this->minimumResize = minimumResize;
|
2021-10-06 00:27:49 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
const auto currentSize = this->size();
|
2021-10-06 00:27:49 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
if (this->panelType == PanelWidgetType::LEFT && currentSize.width() < this->minimumResize) {
|
|
|
|
|
this->setFixedWidth(this->minimumResize);
|
2021-10-06 00:27:49 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
} else if (this->panelType == PanelWidgetType::BOTTOM && currentSize.height() < this->minimumResize) {
|
|
|
|
|
this->setFixedHeight(this->minimumResize);
|
|
|
|
|
}
|
2021-10-06 00:27:49 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void PanelWidget::setMaximumResize(int maximumResize) {
|
|
|
|
|
this->maximumResize = maximumResize;
|
2021-10-06 00:27:49 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
const auto currentSize = this->size();
|
2021-10-06 00:27:49 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
if (this->panelType == PanelWidgetType::LEFT && currentSize.width() > this->maximumResize) {
|
|
|
|
|
this->setFixedWidth(this->maximumResize);
|
2021-10-06 00:27:49 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
} else if (this->panelType == PanelWidgetType::BOTTOM && currentSize.height() > this->maximumResize) {
|
|
|
|
|
this->setFixedHeight(this->maximumResize);
|
|
|
|
|
}
|
2021-10-06 00:27:49 +01:00
|
|
|
}
|
2021-11-17 21:45:25 +00:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void PanelWidget::setPanelType(PanelWidgetType panelType) {
|
|
|
|
|
this->panelType = panelType;
|
2021-11-17 21:45:25 +00:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
if (this->panelType == PanelWidgetType::LEFT) {
|
|
|
|
|
this->resizeCursor = Qt::SplitHCursor;
|
|
|
|
|
this->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding);
|
2021-10-06 21:12:31 +01:00
|
|
|
|
|
|
|
|
} else {
|
2022-02-05 15:32:08 +00:00
|
|
|
this->resizeCursor = Qt::SplitVCursor;
|
|
|
|
|
this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
|
2021-10-06 21:12:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-06 20:28:46 +00:00
|
|
|
void PanelWidget::setSize(int size) {
|
|
|
|
|
if (this->panelType == PanelWidgetType::LEFT) {
|
|
|
|
|
this->setFixedWidth(std::min(std::max(size, this->minimumResize), this->maximumResize));
|
|
|
|
|
|
|
|
|
|
} else if (this->panelType == PanelWidgetType::BOTTOM) {
|
|
|
|
|
this->setFixedHeight(std::min(std::max(size, this->minimumResize), this->maximumResize));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
bool PanelWidget::event(QEvent* event) {
|
|
|
|
|
if (event->type() == QEvent::Type::HoverMove) {
|
2022-02-06 20:28:46 +00:00
|
|
|
auto* hoverEvent = dynamic_cast<QHoverEvent*>(event);
|
2022-02-05 15:32:08 +00:00
|
|
|
if (this->resizingActive || this->isPositionWithinHandleArea(hoverEvent->position().toPoint())) {
|
|
|
|
|
this->setCursor(this->resizeCursor);
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
} else {
|
|
|
|
|
this->setCursor(Qt::ArrowCursor);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-06 00:27:49 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
return QFrame::event(event);
|
|
|
|
|
}
|
2021-10-06 00:27:49 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void PanelWidget::mousePressEvent(QMouseEvent* event) {
|
|
|
|
|
const auto position = event->pos();
|
|
|
|
|
|
|
|
|
|
if (event->buttons() & Qt::LeftButton && this->isPositionWithinHandleArea(position)) {
|
|
|
|
|
this->resizingActive = true;
|
|
|
|
|
|
|
|
|
|
switch (this->panelType) {
|
|
|
|
|
case PanelWidgetType::LEFT: {
|
|
|
|
|
this->resizingOffset = this->width() - position.x();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case PanelWidgetType::BOTTOM: {
|
|
|
|
|
this->resizingOffset = position.y();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-10-06 00:27:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void PanelWidget::mouseReleaseEvent(QMouseEvent* event) {
|
|
|
|
|
if (this->resizingActive) {
|
|
|
|
|
this->resizingActive = false;
|
|
|
|
|
this->resizingOffset = 0;
|
|
|
|
|
this->setCursor(Qt::ArrowCursor);
|
|
|
|
|
}
|
2021-10-06 00:27:49 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void PanelWidget::mouseMoveEvent(QMouseEvent* event) {
|
|
|
|
|
if (this->resizingActive) {
|
2022-02-06 20:28:46 +00:00
|
|
|
this->setSize(this->panelType == PanelWidgetType::LEFT
|
|
|
|
|
? event->pos().x() + this->resizingOffset
|
|
|
|
|
: this->height() + (-event->pos().y()) + this->resizingOffset
|
|
|
|
|
);
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::pair<QPoint, QPoint> PanelWidget::getHandleArea() const {
|
|
|
|
|
const auto currentSize = this->size();
|
2021-10-14 23:48:32 +01:00
|
|
|
|
2021-10-06 00:27:49 +01:00
|
|
|
if (this->panelType == PanelWidgetType::LEFT) {
|
2022-02-05 15:32:08 +00:00
|
|
|
return std::pair(
|
|
|
|
|
QPoint(currentSize.width() - this->handleSize, 0),
|
|
|
|
|
QPoint(currentSize.width(), currentSize.height())
|
2021-10-06 00:27:49 +01:00
|
|
|
);
|
|
|
|
|
}
|
2022-02-06 20:28:46 +00:00
|
|
|
|
|
|
|
|
return std::pair(
|
|
|
|
|
QPoint(0, 0),
|
|
|
|
|
QPoint(currentSize.width(), this->handleSize)
|
|
|
|
|
);
|
2021-10-06 00:27:49 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
bool PanelWidget::isPositionWithinHandleArea(const QPoint& position) const {
|
|
|
|
|
const auto handleArea = this->getHandleArea();
|
2021-10-06 00:27:49 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
return (
|
|
|
|
|
position.x() >= handleArea.first.x() && position.x() <= handleArea.second.x()
|
|
|
|
|
&& position.y() >= handleArea.first.y() && position.y() <= handleArea.second.y()
|
2021-10-06 00:27:49 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|