2016-02-02 22:01:48 +08:00
|
|
|
#include "ads/DropOverlay.h"
|
2015-12-09 19:21:38 +08:00
|
|
|
|
|
|
|
#include <QPointer>
|
|
|
|
#include <QPaintEvent>
|
|
|
|
#include <QResizeEvent>
|
|
|
|
#include <QMoveEvent>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QGridLayout>
|
|
|
|
#include <QCursor>
|
2016-02-12 15:00:31 +08:00
|
|
|
#include <QIcon>
|
|
|
|
#include <QLabel>
|
2015-12-09 19:21:38 +08:00
|
|
|
|
|
|
|
ADS_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
// Helper /////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static QWidget* createDropWidget(const QString& img)
|
|
|
|
{
|
2016-02-12 14:15:10 +08:00
|
|
|
QLabel* label = new QLabel();
|
2015-12-09 19:21:38 +08:00
|
|
|
label->setObjectName("DropAreaLabel");
|
|
|
|
label->setPixmap(QPixmap(img));
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-04-15 14:21:23 +08:00
|
|
|
DropOverlay::DropOverlay(QWidget* parent) :
|
2015-12-09 19:21:38 +08:00
|
|
|
QFrame(parent),
|
2016-04-15 18:14:50 +08:00
|
|
|
_allowedAreas(InvalidDropArea),
|
2016-04-18 13:18:50 +08:00
|
|
|
_cross(new DropOverlayCross(this)),
|
2016-04-15 14:21:23 +08:00
|
|
|
_fullAreaDrop(false),
|
|
|
|
_lastLocation(InvalidDropArea)
|
2015-12-09 19:21:38 +08:00
|
|
|
{
|
2016-02-15 20:00:09 +08:00
|
|
|
setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
|
2015-12-09 19:21:38 +08:00
|
|
|
setWindowOpacity(0.2);
|
2016-02-15 20:00:09 +08:00
|
|
|
setWindowTitle("DropOverlay");
|
2015-12-09 19:21:38 +08:00
|
|
|
|
2016-02-02 20:49:10 +08:00
|
|
|
QBoxLayout* l = new QBoxLayout(QBoxLayout::TopToBottom);
|
2015-12-09 19:21:38 +08:00
|
|
|
l->setContentsMargins(0, 0, 0, 0);
|
|
|
|
l->setSpacing(0);
|
|
|
|
setLayout(l);
|
2016-04-15 18:14:50 +08:00
|
|
|
|
2016-04-18 14:27:28 +08:00
|
|
|
// Cross widget.
|
|
|
|
QHash<DropArea, QWidget*> areaWidgets;
|
|
|
|
areaWidgets.insert(ADS_NS::TopDropArea, createDropWidget(":/img/split-top.png"));
|
|
|
|
areaWidgets.insert(ADS_NS::RightDropArea, createDropWidget(":/img/split-right.png"));
|
|
|
|
areaWidgets.insert(ADS_NS::BottomDropArea, createDropWidget(":/img/split-bottom.png"));
|
|
|
|
areaWidgets.insert(ADS_NS::LeftDropArea, createDropWidget(":/img/split-left.png"));
|
|
|
|
areaWidgets.insert(ADS_NS::CenterDropArea, createDropWidget(":/img/dock-center.png"));
|
|
|
|
_cross->setAreaWidgets(areaWidgets);
|
|
|
|
|
2016-04-18 13:18:50 +08:00
|
|
|
_cross->setVisible(false);
|
2016-04-15 18:14:50 +08:00
|
|
|
setVisible(false);
|
2015-12-09 19:21:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DropOverlay::~DropOverlay()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-04-15 18:14:50 +08:00
|
|
|
void DropOverlay::setAllowedAreas(DropAreas areas)
|
2016-04-15 14:21:23 +08:00
|
|
|
{
|
2016-04-15 18:14:50 +08:00
|
|
|
if (areas == _allowedAreas)
|
|
|
|
return;
|
|
|
|
_allowedAreas = areas;
|
|
|
|
|
2016-04-18 13:18:50 +08:00
|
|
|
_cross->reset();
|
2016-04-18 14:27:28 +08:00
|
|
|
// _cross->move(pos());
|
|
|
|
// _cross->resize(size());
|
2016-04-15 18:14:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DropAreas DropOverlay::allowedAreas() const
|
|
|
|
{
|
|
|
|
return _allowedAreas;
|
2016-04-15 14:21:23 +08:00
|
|
|
}
|
|
|
|
|
2015-12-09 19:21:38 +08:00
|
|
|
DropArea DropOverlay::cursorLocation() const
|
|
|
|
{
|
2016-04-18 14:27:28 +08:00
|
|
|
return _cross->cursorLocation();
|
2015-12-09 19:21:38 +08:00
|
|
|
}
|
|
|
|
|
2016-04-15 18:14:50 +08:00
|
|
|
DropArea DropOverlay::showDropOverlay(QWidget* target)
|
2016-04-15 14:21:23 +08:00
|
|
|
{
|
|
|
|
if (_target == target)
|
|
|
|
{
|
|
|
|
// Hint: We could update geometry of overlay here.
|
|
|
|
DropArea da = cursorLocation();
|
|
|
|
if (da != _lastLocation)
|
|
|
|
{
|
|
|
|
repaint();
|
|
|
|
_lastLocation = da;
|
|
|
|
}
|
|
|
|
return da;
|
|
|
|
}
|
2016-04-15 18:14:50 +08:00
|
|
|
|
2016-04-15 14:21:23 +08:00
|
|
|
hideDropOverlay();
|
2016-04-15 18:14:50 +08:00
|
|
|
_fullAreaDrop = false;
|
|
|
|
_target = target;
|
|
|
|
_targetRect = QRect();
|
|
|
|
_lastLocation = InvalidDropArea;
|
2016-04-15 14:21:23 +08:00
|
|
|
|
2016-04-15 18:14:50 +08:00
|
|
|
// Move it over the target.
|
2016-04-15 14:21:23 +08:00
|
|
|
resize(target->size());
|
|
|
|
move(target->mapToGlobal(target->rect().topLeft()));
|
2016-04-15 18:14:50 +08:00
|
|
|
|
2016-04-15 14:21:23 +08:00
|
|
|
show();
|
2016-04-15 18:14:50 +08:00
|
|
|
|
2016-04-15 14:21:23 +08:00
|
|
|
return cursorLocation();
|
|
|
|
}
|
|
|
|
|
2016-04-15 18:14:50 +08:00
|
|
|
void DropOverlay::showDropOverlay(QWidget* target, const QRect& targetAreaRect)
|
2016-04-15 14:21:23 +08:00
|
|
|
{
|
|
|
|
if (_target == target && _targetRect == targetAreaRect)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-04-15 18:14:50 +08:00
|
|
|
|
2016-04-15 14:21:23 +08:00
|
|
|
hideDropOverlay();
|
2016-04-15 18:14:50 +08:00
|
|
|
_fullAreaDrop = true;
|
|
|
|
_target = target;
|
|
|
|
_targetRect = targetAreaRect;
|
|
|
|
_lastLocation = InvalidDropArea;
|
2016-04-15 14:21:23 +08:00
|
|
|
|
2016-04-15 18:14:50 +08:00
|
|
|
// Move it over the target's area.
|
2016-04-15 14:21:23 +08:00
|
|
|
resize(targetAreaRect.size());
|
|
|
|
move(target->mapToGlobal(QPoint(targetAreaRect.x(), targetAreaRect.y())));
|
2016-04-15 18:14:50 +08:00
|
|
|
|
2016-04-15 14:21:23 +08:00
|
|
|
show();
|
2016-04-15 18:14:50 +08:00
|
|
|
|
2016-04-15 14:21:23 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DropOverlay::hideDropOverlay()
|
|
|
|
{
|
|
|
|
hide();
|
2016-04-15 18:14:50 +08:00
|
|
|
_fullAreaDrop = false;
|
2016-04-15 14:21:23 +08:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
|
|
|
|
_target.clear();
|
|
|
|
#else
|
|
|
|
_target = 0;
|
|
|
|
#endif
|
|
|
|
_targetRect = QRect();
|
|
|
|
_lastLocation = InvalidDropArea;
|
|
|
|
}
|
|
|
|
|
2015-12-09 19:21:38 +08:00
|
|
|
void DropOverlay::paintEvent(QPaintEvent*)
|
|
|
|
{
|
|
|
|
QPainter p(this);
|
2016-04-18 13:06:46 +08:00
|
|
|
const QColor areaColor = palette().color(QPalette::Active, QPalette::Highlight);//QColor(0, 100, 255)
|
2015-12-09 19:21:38 +08:00
|
|
|
|
2016-02-03 17:50:34 +08:00
|
|
|
// Always draw drop-rect over the entire rect()
|
|
|
|
if (_fullAreaDrop)
|
|
|
|
{
|
|
|
|
QRect r = rect();
|
2016-04-18 13:06:46 +08:00
|
|
|
p.fillRect(r, QBrush(areaColor, Qt::Dense4Pattern));
|
|
|
|
p.setBrush(QBrush(areaColor));
|
2016-02-03 17:50:34 +08:00
|
|
|
p.drawRect(r);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-02 20:49:10 +08:00
|
|
|
// Draw rect based on location
|
|
|
|
QRect r = rect();
|
|
|
|
const DropArea da = cursorLocation();
|
|
|
|
switch (da)
|
|
|
|
{
|
2016-02-15 20:56:42 +08:00
|
|
|
case ADS_NS::TopDropArea:
|
2016-02-02 20:49:10 +08:00
|
|
|
r.setHeight(r.height() / 2);
|
|
|
|
break;
|
2016-02-15 20:56:42 +08:00
|
|
|
case ADS_NS::RightDropArea:
|
2016-02-02 20:49:10 +08:00
|
|
|
r.setX(r.width() / 2);
|
|
|
|
break;
|
2016-02-15 20:56:42 +08:00
|
|
|
case ADS_NS::BottomDropArea:
|
2016-02-02 20:49:10 +08:00
|
|
|
r.setY(r.height() / 2);
|
|
|
|
break;
|
2016-02-15 20:56:42 +08:00
|
|
|
case ADS_NS::LeftDropArea:
|
2016-02-02 20:49:10 +08:00
|
|
|
r.setWidth(r.width() / 2);
|
|
|
|
break;
|
2016-02-15 20:56:42 +08:00
|
|
|
case ADS_NS::CenterDropArea:
|
2016-02-02 20:49:10 +08:00
|
|
|
r = rect();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
r = QRect();
|
|
|
|
}
|
|
|
|
if (!r.isNull())
|
|
|
|
{
|
2016-04-18 13:06:46 +08:00
|
|
|
p.fillRect(r, QBrush(areaColor, Qt::Dense4Pattern));
|
|
|
|
p.setBrush(QBrush(areaColor));
|
2016-02-02 20:49:10 +08:00
|
|
|
p.drawRect(r);
|
|
|
|
}
|
|
|
|
|
2015-12-09 19:21:38 +08:00
|
|
|
// Draw rect over the entire size + border.
|
2016-02-02 20:49:10 +08:00
|
|
|
// auto r = rect();
|
|
|
|
// r.setWidth(r.width() - 1);
|
|
|
|
// r.setHeight(r.height() - 1);
|
2015-12-09 19:21:38 +08:00
|
|
|
|
2016-02-02 20:49:10 +08:00
|
|
|
// p.fillRect(r, QBrush(QColor(0, 100, 255), Qt::Dense4Pattern));
|
|
|
|
// p.setBrush(QBrush(QColor(0, 100, 255)));
|
2016-04-15 18:14:50 +08:00
|
|
|
// p.drawRect(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DropOverlay::showEvent(QShowEvent*)
|
|
|
|
{
|
2016-04-18 13:18:50 +08:00
|
|
|
_cross->show();
|
2016-04-15 18:14:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DropOverlay::hideEvent(QHideEvent*)
|
|
|
|
{
|
2016-04-18 13:18:50 +08:00
|
|
|
_cross->hide();
|
2015-12-09 19:21:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DropOverlay::resizeEvent(QResizeEvent* e)
|
|
|
|
{
|
2016-04-18 13:18:50 +08:00
|
|
|
_cross->resize(e->size());
|
2015-12-09 19:21:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DropOverlay::moveEvent(QMoveEvent* e)
|
|
|
|
{
|
2016-04-18 13:18:50 +08:00
|
|
|
_cross->move(e->pos());
|
2015-12-09 19:21:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-04-18 14:27:28 +08:00
|
|
|
static QPair<QPoint, int> gridPosForArea(const DropArea area)
|
|
|
|
{
|
|
|
|
switch (area)
|
|
|
|
{
|
|
|
|
case ADS_NS::TopDropArea:
|
|
|
|
return qMakePair(QPoint(0, 1), (int) Qt::AlignHCenter | Qt::AlignBottom);
|
|
|
|
case ADS_NS::RightDropArea:
|
|
|
|
return qMakePair(QPoint(1, 2), (int) Qt::AlignLeft | Qt::AlignVCenter);
|
|
|
|
case ADS_NS::BottomDropArea:
|
|
|
|
return qMakePair(QPoint(2, 1), (int) Qt::AlignHCenter | Qt::AlignTop);
|
|
|
|
case ADS_NS::LeftDropArea:
|
|
|
|
return qMakePair(QPoint(1, 0), (int) Qt::AlignRight | Qt::AlignVCenter);
|
|
|
|
case ADS_NS::CenterDropArea:
|
|
|
|
return qMakePair(QPoint(1, 1), (int) Qt::AlignCenter);
|
|
|
|
}
|
|
|
|
return QPair<QPoint, int>();
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:06:46 +08:00
|
|
|
DropOverlayCross::DropOverlayCross(DropOverlay* overlay) :
|
2016-04-15 18:14:50 +08:00
|
|
|
QWidget(overlay->parentWidget()),
|
|
|
|
_overlay(overlay),
|
|
|
|
_widgets()
|
2015-12-09 19:21:38 +08:00
|
|
|
{
|
2016-02-15 20:00:09 +08:00
|
|
|
setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
|
|
|
|
setWindowTitle("DropSplitAreas");
|
2015-12-09 19:21:38 +08:00
|
|
|
setAttribute(Qt::WA_TranslucentBackground);
|
|
|
|
|
2016-04-15 18:14:50 +08:00
|
|
|
_grid = new QGridLayout();
|
|
|
|
_grid->setContentsMargins(0, 0, 0, 0);
|
|
|
|
_grid->setSpacing(6);
|
2015-12-09 19:21:38 +08:00
|
|
|
|
|
|
|
QBoxLayout* bl1 = new QBoxLayout(QBoxLayout::TopToBottom);
|
|
|
|
bl1->setContentsMargins(0, 0, 0, 0);
|
|
|
|
bl1->setSpacing(0);
|
|
|
|
setLayout(bl1);
|
|
|
|
|
|
|
|
QBoxLayout* bl2 = new QBoxLayout(QBoxLayout::LeftToRight);
|
|
|
|
bl2->setContentsMargins(0, 0, 0, 0);
|
|
|
|
bl2->setSpacing(0);
|
|
|
|
|
|
|
|
bl1->addStretch(1);
|
|
|
|
bl1->addLayout(bl2);
|
|
|
|
bl2->addStretch(1);
|
2016-04-15 18:14:50 +08:00
|
|
|
bl2->addLayout(_grid, 0);
|
2015-12-09 19:21:38 +08:00
|
|
|
bl2->addStretch(1);
|
|
|
|
bl1->addStretch(1);
|
|
|
|
}
|
|
|
|
|
2016-04-18 14:27:28 +08:00
|
|
|
DropOverlayCross::~DropOverlayCross()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void DropOverlayCross::setAreaWidgets(const QHash<DropArea, QWidget*>& widgets)
|
2016-04-15 18:14:50 +08:00
|
|
|
{
|
2016-04-18 14:27:28 +08:00
|
|
|
// Delete old widgets.
|
|
|
|
QMutableHashIterator<DropArea, QWidget*> i(_widgets);
|
|
|
|
while (i.hasNext())
|
|
|
|
{
|
|
|
|
i.next();
|
|
|
|
QWidget* widget = i.value();
|
|
|
|
_grid->removeWidget(widget);
|
|
|
|
delete widget;
|
|
|
|
i.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert new widgets into grid.
|
|
|
|
_widgets = widgets;
|
|
|
|
QHashIterator<DropArea, QWidget*> i2(_widgets);
|
|
|
|
while (i2.hasNext())
|
|
|
|
{
|
|
|
|
i2.next();
|
|
|
|
const DropArea area = i2.key();
|
|
|
|
QWidget* widget = i2.value();
|
|
|
|
const QPair<QPoint, int> opts = gridPosForArea(area);
|
|
|
|
_grid->addWidget(widget, opts.first.x(), opts.first.y(), (Qt::Alignment) opts.second);
|
|
|
|
}
|
|
|
|
reset();
|
2016-04-15 18:14:50 +08:00
|
|
|
}
|
|
|
|
|
2016-04-18 13:06:46 +08:00
|
|
|
DropArea DropOverlayCross::cursorLocation() const
|
2015-12-09 19:21:38 +08:00
|
|
|
{
|
2016-04-15 18:14:50 +08:00
|
|
|
const QPoint pos = mapFromGlobal(QCursor::pos());
|
|
|
|
QHashIterator<DropArea, QWidget*> i(_widgets);
|
|
|
|
while (i.hasNext())
|
2015-12-09 19:21:38 +08:00
|
|
|
{
|
2016-04-15 18:14:50 +08:00
|
|
|
i.next();
|
|
|
|
if (_overlay->allowedAreas().testFlag(i.key())
|
|
|
|
&& i.value()
|
|
|
|
&& i.value()->isVisible()
|
|
|
|
&& i.value()->geometry().contains(pos))
|
|
|
|
{
|
|
|
|
return i.key();
|
|
|
|
}
|
2015-12-09 19:21:38 +08:00
|
|
|
}
|
2016-04-15 18:14:50 +08:00
|
|
|
return InvalidDropArea;
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:06:46 +08:00
|
|
|
void DropOverlayCross::showEvent(QShowEvent*)
|
2016-04-15 18:14:50 +08:00
|
|
|
{
|
|
|
|
resize(_overlay->size());
|
|
|
|
move(_overlay->pos());
|
2015-12-09 19:21:38 +08:00
|
|
|
}
|
|
|
|
|
2016-04-18 14:27:28 +08:00
|
|
|
void DropOverlayCross::reset()
|
|
|
|
{
|
|
|
|
QList<DropArea> allAreas;
|
|
|
|
allAreas << ADS_NS::TopDropArea << ADS_NS::RightDropArea << ADS_NS::BottomDropArea << ADS_NS::LeftDropArea << ADS_NS::CenterDropArea;
|
|
|
|
const DropAreas allowedAreas = _overlay->allowedAreas();
|
|
|
|
|
|
|
|
// Update visibility of area widgets based on allowedAreas.
|
|
|
|
for (int i = 0; i < allAreas.count(); ++i)
|
|
|
|
{
|
|
|
|
const QPair<QPoint, int> opts = gridPosForArea(allAreas.at(i));
|
|
|
|
|
|
|
|
QLayoutItem* item = _grid->itemAtPosition(opts.first.x(), opts.first.y());
|
|
|
|
QWidget* w = NULL;
|
|
|
|
if (item && (w = item->widget()) != NULL)
|
|
|
|
{
|
|
|
|
w->setVisible(allowedAreas.testFlag(allAreas.at(i)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-03 17:50:34 +08:00
|
|
|
ADS_NAMESPACE_END
|