#include "widget.h"
#include <QApplication>
#include <QListWidget>
#include <QPropertyAnimation>
#include <QVBoxLayout>
#include <QPushButton>
#include <QBitmap>
#include <QPainter>
#include <QResizeEvent>
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *lay = new QVBoxLayout(this);
listWidget = new QListWidget;
listWidget->setViewMode(QListWidget::IconMode);
listWidget->addItem(new QListWidgetItem(QIcon(":/img/test.png"),QStringLiteral("test")));
listWidget->addItem(new QListWidgetItem(QIcon(":/img/trash.png"),QStringLiteral("trash")));
listWidget->installEventFilter(this);
lay->addWidget(listWidget);
reRect = new QLabel(this);
reRect->setAutoFillBackground(true);
QPalette pal = reRect->palette();
pal.setBrush(QPalette::Background,Qt::lightGray);
reRect->setPalette(pal);
QPixmap pix(":/img/trash.png");
reRect->resize(width(),pix.height() + 20);
reRect->setMinimumHeight(pix.height() + 20);
reLabel = new QLabel;
reLabel->resize(pix.size());
reLabel->setPixmap(pix);
reLabel->setMask(pix.mask());
reLabel->setAcceptDrops(true);
reLabel->installEventFilter(this);
QHBoxLayout *hlay = new QHBoxLayout(reRect);
hlay->addWidget(reLabel,0,Qt::AlignCenter);
ani = new QPropertyAnimation(reRect, "geometry");
ani->setEasingCurve(QEasingCurve::InCurve);
ani->setDuration(150);
connect(ani,&QPropertyAnimation::finished,[&]{ani->setDirection(QAbstractAnimation::Direction(!ani->direction()));});
}
void Widget::resizeEvent(QResizeEvent *event)
{
reRect->resize(event->size().width(),reRect->height());
if(initFlag){
initFlag = false;
reRect->move(reRect->x(),-reRect->height());
}
ani->setStartValue(QRect(reRect->x(),-reRect->height(),reRect->width(),reRect->height()));
ani->setEndValue(reRect->rect());
return QWidget::resizeEvent(event);
}
Widget::~Widget()
{
}
bool Widget::eventFilter(QObject *watched, QEvent *event)
{
if(reLabel = qobject_cast<QLabel*>(watched)){
if(event->type() == QEvent::DragEnter){
QDragEnterEvent *drageEnterE{nullptr};
try{
drageEnterE = dynamic_cast<QDragEnterEvent*>(event);
}catch(std::bad_cast &e){
qDebug() << e.what();
}
if(drageEnterE){
trashPainted = false;
drageEnterE->acceptProposedAction();
}
return true;
}else if(event->type() == QEvent::Drop){
QDropEvent *dropE{nullptr};
try{
dropE = dynamic_cast<QDropEvent*>(event);
}catch(std::bad_cast &e){
qDebug() << e.what();
}
if(dropE){
listWidget->takeItem(listWidget->row(listWidget->currentItem()));
QPixmap *pix = const_cast<QPixmap*>(reLabel->pixmap());
*pix = QPixmap(":/img/trash.png");
reLabel->repaint();
}
return true;
}else if(event->type() == QEvent::DragMove){
QDragMoveEvent *mvEvent{nullptr};
try{
mvEvent = dynamic_cast<QDragMoveEvent*>(event);
}catch(std::bad_cast &e){
qDebug() << e.what();
}
if(mvEvent && !trashPainted){
QPixmap *pix = const_cast<QPixmap*>(reLabel->pixmap());
QPainter p(pix);
p.initFrom(pix);
p.fillRect(pix->rect(),QColor(200,0,0,50));
reLabel->repaint();
trashPainted = !trashPainted;
}
return true;
}else if(event->type() == QEvent::DragLeave){
QDragLeaveEvent *leEvent{nullptr};
try{
leEvent = dynamic_cast<QDragLeaveEvent*>(event);
}catch(std::bad_cast &e){
qDebug() << e.what();
}
if(leEvent){
QPixmap *pix = const_cast<QPixmap*>(reLabel->pixmap());
*pix = QPixmap(":/img/trash.png");
reLabel->repaint();
}
return true;
}
}else if(listWidget == qobject_cast<QListWidget*>(watched)){
QChildEvent *childEvent{nullptr};
switch (event->type()) {
case QEvent::ChildAdded:
try{
childEvent = dynamic_cast<QChildEvent*>(event);
}catch(std::bad_cast &e){
qDebug() << e.what();
}
if(childEvent){
ani->start();
}
break;
case QEvent::ChildRemoved:
try{
childEvent = dynamic_cast<QChildEvent*>(event);
}catch(std::bad_cast &e){
qDebug() << e.what();
}
if(childEvent){
ani->start();
}
break;
}
}
return QWidget::eventFilter(watched,event);
}
wangzai6378
- 粉丝: 255
- 资源: 18