182 lines
5.0 KiB
C++
182 lines
5.0 KiB
C++
#include "baseview.h"
|
|
|
|
#include <QApplication>
|
|
#include <QDebug>
|
|
#include <QDrag>
|
|
#include <QHBoxLayout>
|
|
#include <QMimeData>
|
|
#include <QMouseEvent>
|
|
#include <QPushButton>
|
|
|
|
using namespace SplitFrame;
|
|
|
|
DragHeader::DragHeader(BaseView *bind) : QFrame(bind->bind()), icon_store(new QLabel(this)), title_store(new QLabel(this)), view_core(bind) {
|
|
setFrameShadow(QFrame::Shadow::Raised);
|
|
setFrameShape(QFrame::Shape::Box);
|
|
setStyleSheet("background-color: rgb(200, 200, 255);");
|
|
|
|
auto layout = new QHBoxLayout(this);
|
|
layout->setMargin(1);
|
|
layout->setSpacing(2);
|
|
layout->addWidget(icon_store, 0);
|
|
layout->addWidget(title_store, 1);
|
|
|
|
auto hide = new QPushButton("-", this);
|
|
layout->addWidget(hide, 0);
|
|
hide->setMaximumSize(20, 20);
|
|
auto close = new QPushButton("X", this);
|
|
layout->addWidget(close, 0);
|
|
close->setMaximumSize(20, 20);
|
|
|
|
connect(hide, &QPushButton::clicked, [=]() { view_core->pRelease(); });
|
|
connect(close, &QPushButton::clicked, [=]() { view_core->pClose(); });
|
|
}
|
|
|
|
void DragHeader::setIcon(const QIcon &icon) {
|
|
icon_inst = icon;
|
|
auto len = this->height() - 2;
|
|
this->icon_store->setPixmap(icon.pixmap(QSize(len, len)));
|
|
}
|
|
|
|
QIcon DragHeader::icon() const { return this->icon_inst; }
|
|
|
|
void DragHeader::setText(const QString &title) { this->title_store->setText(title); }
|
|
|
|
QString DragHeader::text() const { return title_store->text(); }
|
|
|
|
void DragHeader::mousePressEvent(QMouseEvent *event) {
|
|
if (event->button() == Qt::LeftButton && !std::get<0>(press_flag)) {
|
|
this->press_flag = std::make_tuple(true, event->pos());
|
|
}
|
|
}
|
|
|
|
void DragHeader::mouseReleaseEvent(QMouseEvent *event) {
|
|
if (event->button() == Qt::LeftButton) {
|
|
this->press_flag = std::make_tuple(false, QPointF());
|
|
}
|
|
}
|
|
|
|
void DragHeader::mouseMoveEvent(QMouseEvent *event) {
|
|
auto distance = QLineF(std::get<1>(press_flag), event->pos()).length();
|
|
if (std::get<0>(press_flag) && distance > QApplication::startDragDistance()) {
|
|
auto panel = view_core->viewPanel();
|
|
panel->setAdjustView(view_core);
|
|
|
|
QDrag *drag = new QDrag(this);
|
|
auto pix = view_core->bind()->grab();
|
|
drag->setPixmap(pix);
|
|
drag->setHotSpot(QPoint(20, 20));
|
|
|
|
QMimeData *mimeData = new QMimeData;
|
|
mimeData->setText("移动视图:" + view_core->title());
|
|
drag->setMimeData(mimeData);
|
|
|
|
drag->exec();
|
|
}
|
|
}
|
|
|
|
BaseView::BaseView(const QString &title, QWidget *view, bool empty, bool replaceable, QWidget *parent)
|
|
: QWidget(parent), replace_able(replaceable), title_header(nullptr), parent_store(nullptr) {
|
|
auto layout = new QVBoxLayout(this);
|
|
layout->setMargin(0);
|
|
layout->setSpacing(0);
|
|
|
|
registElement(this);
|
|
setCursor(Qt::CursorShape::ArrowCursor);
|
|
|
|
if (!empty) {
|
|
title_header = new DragHeader(this);
|
|
title_header->setText(title);
|
|
title_header->setMaximumHeight(22);
|
|
|
|
layout->addWidget(title_header, 0);
|
|
registElement(title_header);
|
|
}
|
|
|
|
registElement(view);
|
|
layout->addWidget(view, 1);
|
|
setMouseTracking(true);
|
|
}
|
|
|
|
BaseView::~BaseView() {}
|
|
|
|
void BaseView::setTitle(const QString &title) {
|
|
QWidget::setWindowTitle(title);
|
|
title_store = title;
|
|
if (title_header)
|
|
title_header->setText(title);
|
|
}
|
|
|
|
QString BaseView::title() const { return title_store; }
|
|
|
|
void BaseView::setIcon(const QIcon &icon) {
|
|
this->icon_store = icon;
|
|
if (this->title_header)
|
|
this->title_header->setIcon(icon);
|
|
}
|
|
|
|
QIcon BaseView::viewIcon() const { return this->icon_store; }
|
|
|
|
bool BaseView::canReplace() const { return replace_able; }
|
|
|
|
void BaseView::registElement(QWidget *child) {
|
|
if (this->comps_list.contains(child))
|
|
return;
|
|
|
|
comps_list << child;
|
|
|
|
child->setMouseTracking(true);
|
|
if (child != this) {
|
|
child->setParent(this);
|
|
}
|
|
}
|
|
|
|
ViewPresent *BaseView::viewPanel() const {
|
|
auto parent_inst = parentRes();
|
|
return parent_inst->viewPanel();
|
|
}
|
|
|
|
bool BaseView::contains(QWidget *ptr) const { return comps_list.contains(ptr); }
|
|
|
|
QWidget *BaseView::bind() const { return const_cast<BaseView *>(this); }
|
|
|
|
ViewRes *BaseView::parentRes() const { return parent_store; }
|
|
|
|
void BaseView::pRelease() {
|
|
auto parent = parentRes();
|
|
if (parent) {
|
|
auto host_ptr = viewPanel();
|
|
dynamic_cast<SplitView *>(parent)->removeComp(this);
|
|
host_ptr->markFreedom(this);
|
|
}
|
|
}
|
|
|
|
void BaseView::pClose() {
|
|
auto ptr = viewPanel();
|
|
pRelease();
|
|
ptr->purge(this);
|
|
}
|
|
|
|
void BaseView::setParentRes(ViewRes *pinst) {
|
|
this->parent_store = pinst;
|
|
|
|
this->setParent(pinst != nullptr ? pinst->bind() : nullptr);
|
|
}
|
|
|
|
QRectF BaseView::outline() const { return this->rect(); }
|
|
|
|
void BaseView::relayout(const QRectF &outline) {
|
|
this->setGeometry(outline.toRect());
|
|
update();
|
|
}
|
|
|
|
void BaseView::installPerceptionHandle(ViewPresent *obj) {
|
|
for (auto &it : comps_list) {
|
|
it->setAcceptDrops(true);
|
|
it->removeEventFilter(obj->globalEventFilter());
|
|
it->installEventFilter(obj->globalEventFilter());
|
|
}
|
|
}
|
|
|
|
void BaseView::paintEvent(QPaintEvent *ev) { QWidget::paintEvent(ev); }
|