199 lines
7.8 KiB
C++
199 lines
7.8 KiB
C++
#include "moduleunit.h"
|
|
#include "expertboxlayoutwin.h"
|
|
#include <QDirIterator>
|
|
#include <QGraphicsScene>
|
|
#include <QGraphicsSceneMouseEvent>
|
|
#include <QJsonArray>
|
|
#include <QJsonObject>
|
|
#include <QLabel>
|
|
#include <QPainter>
|
|
#include <QSpinBox>
|
|
#include <QMouseEvent>
|
|
|
|
ModuleUnit::ModuleUnit(ExpertBoxLayoutWin *expertWin, const QString &name, int x, int y, int w, int h, QWidget *parent) : QWidget{parent}, expertWin{expertWin}, name{name}, mX{x}, mY{y}, mW{w}, mH{h} {
|
|
setGeometry(x, y, w, h);
|
|
setMouseTracking(true);
|
|
auto font = this->font();
|
|
font.setPixelSize(12);
|
|
setFont(font);
|
|
|
|
mSidePen.setCapStyle(Qt::FlatCap);
|
|
mSidePen.setDashPattern(QVector<qreal>{2,2});
|
|
}
|
|
|
|
void ModuleUnit::paintEvent(QPaintEvent *) {
|
|
QPainter painter(this);
|
|
painter.setPen(mSidePen);
|
|
painter.drawRect(QRectF(0.5, 0.5, width()-1, height()-1));
|
|
painter.drawText(2, 16, name);
|
|
painter.drawText(2, 32, "位置: "+QString::number(mX)+", "+QString::number(mY));
|
|
painter.drawText(2, 48, "大小: "+QString::number(mW)+" x "+QString::number(mH));
|
|
//磁条吸附时两化吸附的边
|
|
static QPen snapPen(QColor(0x00ff00), 2);
|
|
painter.setPen(snapPen);
|
|
if(mLRSnap==1) painter.drawLine(QPointF(1, 0), QPointF(1, height()));
|
|
else if(mLRSnap==2) painter.drawLine(QPointF(width()-1, 0), QPointF(width()-1, height()));
|
|
if(mTBSnap==1) painter.drawLine(QPointF(0, 1), QPointF(width(), 1));
|
|
else if(mTBSnap==2) painter.drawLine(QPointF(0, height()-1), QPointF(width(), height()-1));
|
|
}
|
|
|
|
void ModuleUnit::setFrmSec(const QPoint &pos) {
|
|
if(isMaximized()) return;
|
|
if(pos.y()<8) {
|
|
if(pos.x()<16) setFrmSecIfNeed(Qt::TopLeftSection, Qt::SizeFDiagCursor);
|
|
else if(pos.x()<width()-16) setFrmSecIfNeed(Qt::TopSection, Qt::SizeVerCursor);
|
|
else setFrmSecIfNeed(Qt::TopRightSection, Qt::SizeBDiagCursor);
|
|
} else if(pos.y()>=height()-8) {
|
|
if(pos.x()<16) setFrmSecIfNeed(Qt::BottomLeftSection, Qt::SizeBDiagCursor);
|
|
else if(pos.x()<width()-16) setFrmSecIfNeed(Qt::BottomSection, Qt::SizeVerCursor);
|
|
else setFrmSecIfNeed(Qt::BottomRightSection, Qt::SizeFDiagCursor);
|
|
} else if(pos.x()<8) {
|
|
if(pos.y()<16) setFrmSecIfNeed(Qt::TopLeftSection, Qt::SizeFDiagCursor);
|
|
else if(pos.y()<height()-16) setFrmSecIfNeed(Qt::LeftSection, Qt::SizeHorCursor);
|
|
else setFrmSecIfNeed(Qt::BottomLeftSection, Qt::SizeBDiagCursor);
|
|
} else if(pos.x()>=width()-8) {
|
|
if(pos.y()<16) setFrmSecIfNeed(Qt::TopRightSection, Qt::SizeBDiagCursor);
|
|
else if(pos.y()<height()-16) setFrmSecIfNeed(Qt::RightSection, Qt::SizeHorCursor);
|
|
else setFrmSecIfNeed(Qt::BottomRightSection, Qt::SizeFDiagCursor);
|
|
} else setFrmSecIfNeed(Qt::TitleBarArea, Qt::SizeAllCursor);
|
|
}
|
|
void ModuleUnit::mousePressEvent(QMouseEvent *e) {
|
|
QWidget::mousePressEvent(e);
|
|
if(e->button() != Qt::LeftButton) return;
|
|
setFrmSec(e->pos());
|
|
if(mFrmSec==Qt::TitleBarArea || mFrmSec==Qt::TopSection || mFrmSec==Qt::LeftSection || mFrmSec==Qt::TopLeftSection) mPressRel = mPressRel = pos() - e->globalPos();
|
|
else if(mFrmSec==Qt::BottomRightSection) mPressRel = QPoint(width() - e->globalX(), height() - e->globalY());
|
|
else if(mFrmSec==Qt::RightSection ) mPressRel = QPoint(width() - e->globalX(), height() );
|
|
else if(mFrmSec==Qt::BottomSection ) mPressRel = QPoint(width() , height() - e->globalY());
|
|
else if(mFrmSec==Qt::TopRightSection ) mPressRel = geometry().topRight() - e->globalPos();
|
|
else if(mFrmSec==Qt::BottomLeftSection ) mPressRel = geometry().bottomLeft() - e->globalPos();
|
|
else if(mFrmSec==Qt::NoSection) mPressRel.setX(INT_MIN);
|
|
if(mPressRel.x()!=INT_MIN) {
|
|
mOtherEles.clear();
|
|
auto parent = this->parentWidget();
|
|
if(0 == parent) return;
|
|
auto items = parent->children();
|
|
foreach(auto item, items) {
|
|
if(item==this) continue;
|
|
auto ele = static_cast<ModuleUnit*>(item);
|
|
mOtherEles.append(ele);
|
|
}
|
|
}
|
|
}
|
|
|
|
void ModuleUnit::mouseReleaseEvent(QMouseEvent *event) {
|
|
QWidget::mouseReleaseEvent(event);
|
|
if(Qt::LeftButton == event->button()) {
|
|
mPressRel.setX(INT_MIN);
|
|
clearSnap();
|
|
foreach(auto ele, mOtherEles) ele->clearSnap();
|
|
}
|
|
}
|
|
#define SnapSpace 6
|
|
void ModuleUnit::mouseMoveEvent(QMouseEvent *e){
|
|
if(! (e->buttons() & Qt::LeftButton)) {
|
|
setFrmSec(e->pos());
|
|
return;
|
|
}
|
|
if(mFrmSec==Qt::NoSection || mPressRel.x()==INT_MIN) return;
|
|
auto mousePos = e->globalPos();
|
|
auto dstHor = mPressRel.x() + mousePos.x();
|
|
auto dstVer = mPressRel.y() + mousePos.y();
|
|
mLRSnap = mTBSnap = 0;
|
|
foreach(auto ele, mOtherEles) ele->clearSnap();
|
|
if(mFrmSec==Qt::TitleBarArea) {
|
|
dstHor = qMax(0, dstHor);
|
|
dstVer = qMax(0, dstVer);
|
|
auto needw = qMax(1024, dstHor+width());
|
|
auto needh = qMax(1024, dstVer+height());
|
|
if(needw != expertWin->box->width() || needh != expertWin->box->height()) expertWin->box->resize(needw, needh);
|
|
if(dstHor==0) mLRSnap = 1;
|
|
if(dstVer==0) mTBSnap = 1;
|
|
if(mLRSnap==0) foreach(ModuleUnit *ele, mOtherEles) {//左右
|
|
if(abs(dstHor - ele->x()) < SnapSpace) {
|
|
dstHor = ele->x();
|
|
mLRSnap = 1;
|
|
ele->mLRSnap = 1;
|
|
ele->update();
|
|
break;
|
|
}
|
|
auto eleRight = ele->x() + ele->width();
|
|
if(abs(dstHor - eleRight) < SnapSpace) {
|
|
dstHor = eleRight;
|
|
mLRSnap = 1;
|
|
ele->mLRSnap = 2;
|
|
ele->update();
|
|
break;
|
|
}
|
|
auto right = dstHor + width();
|
|
if(abs(right - ele->x()) < SnapSpace && ele->x() - width() >= 0) {
|
|
dstHor = ele->x() - width();
|
|
mLRSnap = 2;
|
|
ele->mLRSnap = 1;
|
|
ele->update();
|
|
break;
|
|
}
|
|
if(abs(right - eleRight) < SnapSpace && eleRight - width() >= 0) {
|
|
dstHor = eleRight - width();
|
|
mLRSnap = 2;
|
|
ele->mLRSnap = 2;
|
|
ele->update();
|
|
break;
|
|
}
|
|
}
|
|
if(mTBSnap==0) foreach(ModuleUnit *ele, mOtherEles) {//上下
|
|
if(abs(dstVer-ele->y()) < SnapSpace) {
|
|
dstVer = ele->y();
|
|
mTBSnap = 1;
|
|
ele->mTBSnap = 1;
|
|
ele->update();
|
|
break;
|
|
}
|
|
auto eleBtm = ele->y() + ele->height();
|
|
if(abs(dstVer - eleBtm) < SnapSpace) {
|
|
dstVer = eleBtm;
|
|
mTBSnap = 1;
|
|
ele->mTBSnap = 2;
|
|
ele->update();
|
|
break;
|
|
}
|
|
auto btm = dstVer + height();
|
|
if(abs(btm - ele->y()) < SnapSpace && ele->y() - height() >= 0) {
|
|
dstVer = ele->y() - height();
|
|
mTBSnap = 2;
|
|
ele->mTBSnap = 1;
|
|
ele->update();
|
|
break;
|
|
}
|
|
if(abs(btm - eleBtm) < SnapSpace && eleBtm - height() >= 0) {
|
|
dstVer = eleBtm - height();
|
|
mTBSnap = 2;
|
|
ele->mTBSnap = 2;
|
|
ele->update();
|
|
break;
|
|
}
|
|
}
|
|
move(dstHor, dstVer);
|
|
mX = qRound(dstHor / expertWin->rate);
|
|
mY = qRound(dstVer / expertWin->rate);
|
|
update();
|
|
}
|
|
}
|
|
void ModuleUnit::leaveEvent(QEvent *) {
|
|
setFrmSecIfNeed(Qt::NoSection, Qt::ArrowCursor);
|
|
mPressRel.setX(INT_MIN);
|
|
}
|
|
|
|
void ModuleUnit::setFrmSecIfNeed(Qt::WindowFrameSection frmSec, Qt::CursorShape cursor) {
|
|
if(mFrmSec==frmSec) return;
|
|
mFrmSec = frmSec;
|
|
if(cursor==Qt::ArrowCursor) unsetCursor();
|
|
else setCursor(cursor);
|
|
}
|
|
|
|
void ModuleUnit::clearSnap() {
|
|
if(mLRSnap==0 && mTBSnap==0) return;
|
|
mLRSnap = mTBSnap = 0;
|
|
update();
|
|
}
|