#include "liveeditor.h" #include "gutil/qgui.h" #include "main.h" #include "layer.h" #include #include #include LiveEditor::LiveEditor(QWidget *parent) : QWidget(parent) { gScene = new QGraphicsScene(this); auto hBox = new HBox(this); hBox->setContentsMargins(0, 0, 0, 0); hBox->setSpacing(0); gEditView = new GraphicsView; gEditView->setBackgroundBrush(Qt::black); gEditView->setAlignment(Qt::AlignTop|Qt::AlignLeft); gEditView->setTransformationAnchor(GraphicsView::NoAnchor); gEditView->setInteractive(false); gEditView->setSceneRect({-gOrigin / gScale, QSizeF(1, 1)}); gEditView->setScene(gScene); gEditView->scale(gScale, gScale); auto origin = ((GraphicsView*)gEditView)->originWgt = new OriginWgt(gEditView); origin->setGeometry(gOrigin.x()-10, gOrigin.y()-10, 20, 20); hBox->addWidget(gEditView); auto toolBar = new QToolBar; toolBar->setOrientation(Qt::Vertical); toolBar->setStyleSheet("QToolBar{spacing: 2px;}"); toolBar->setIconSize({18, 18}); auto actScaleUp = new QAction(QIcon(":/res/program/ScaleUp.png"), tr("Zoom In")); toolBar->addAction(actScaleUp); connect(actScaleUp, &QAction::triggered, this, [this] { if(gScale >= 8) return; gScale *= 1.1; scaleChanged(); }); edScale = new QLabel("0.1"); edScale->setMaximumWidth(24); edScale->setAlignment(Qt::AlignCenter); toolBar->addWidget(edScale); auto actScaleDown = new QAction(QIcon(":/res/program/ScaleDown.png"), tr("Zoom Out")); toolBar->addAction(actScaleDown); connect(actScaleDown, &QAction::triggered, this, [this] { if(gScale <= 0.01) return; gScale *= 0.909090909090; scaleChanged(); }); auto actScaleOrg = new QAction(QIcon(":/res/program/ScaleOrg.png"), tr("Original Size")); connect(actScaleOrg, &QAction::triggered, this, [this] { gScale = 0.1; scaleChanged(); }); toolBar->addAction(actScaleOrg); auto actFull = new QAction(QIcon(":/res/program/TileFull.png"), tr("Full Screen")); connect(actFull, &QAction::triggered, this, &LiveEditor::onTileFull); toolBar->addAction(actFull); hBox->addWidget(toolBar); } void LiveEditor::scaleChanged() { gEditView->setSceneRect({-gOrigin / gScale, QSizeF(1, 1)}); QTransform tran; tran.scale(gScale, gScale); gEditView->setTransform(tran); for(auto child : gEditView->children()) { auto layer = dynamic_cast(child); if(layer) layer->setGeo(); } edScale->setText(QString::number(gScale)); } void LiveEditor::onTileFull() { // EBase *element = getElementSelected(); // if(nullptr == element) return; // element->setPos(0,0); } void GraphicsView::mousePressEvent(QMouseEvent *e) { QWidget::mousePressEvent(e); if(e->button() != Qt::LeftButton) return; pressRel = gOrigin - e->globalPosition().toPoint(); } void GraphicsView::mouseReleaseEvent(QMouseEvent *event) { QWidget::mouseReleaseEvent(event); if(Qt::LeftButton == event->button()) { pressRel.setX(INT_MAX); } } void GraphicsView::mouseMoveEvent(QMouseEvent *e) { if(! (e->buttons() & Qt::LeftButton)) return; if(pressRel.x()==INT_MAX) return; auto mousePos = e->globalPosition().toPoint(); gOrigin = pressRel + mousePos; originWgt->move(gOrigin - QPoint{10,10}); gEditView->setSceneRect({-gOrigin / gScale, QSizeF(1, 1)}); for(auto child : children()) { auto layer = dynamic_cast(child); if(layer) layer->move(layer->sPos*gScale+gOrigin); } }