dust3d/src/mainwindow.cpp

199 lines
6.6 KiB
C++
Raw Normal View History

2018-03-11 03:18:24 +00:00
#include <QVBoxLayout>
#include <QPushButton>
#include <QButtonGroup>
#include <QGridLayout>
#include <QToolBar>
2018-03-11 16:02:15 +00:00
#include <QThread>
2018-03-13 06:39:36 +00:00
#include <QFileDialog>
2018-03-11 16:02:15 +00:00
#include <assert.h>
#include "mainwindow.h"
#include "skeletoneditwidget.h"
#include "meshlite.h"
#include "skeletontomesh.h"
2018-03-13 06:39:36 +00:00
#include "turnaroundloader.h"
MainWindow::MainWindow() :
m_skeletonToMesh(NULL),
2018-03-13 06:39:36 +00:00
m_skeletonDirty(false),
m_turnaroundLoader(NULL),
m_turnaroundDirty(false)
{
2018-03-13 12:49:24 +00:00
//QPushButton *skeletonButton = new QPushButton("Skeleton");
//QPushButton *motionButton = new QPushButton("Motion");
//QPushButton *modelButton = new QPushButton("Model");
2018-03-11 03:18:24 +00:00
2018-03-13 12:49:24 +00:00
//QButtonGroup *pageButtonGroup = new QButtonGroup;
//pageButtonGroup->addButton(skeletonButton);
//pageButtonGroup->addButton(motionButton);
//pageButtonGroup->addButton(modelButton);
2018-03-11 03:18:24 +00:00
2018-03-13 12:49:24 +00:00
//skeletonButton->setCheckable(true);
//motionButton->setCheckable(true);
//modelButton->setCheckable(true);
2018-03-11 03:18:24 +00:00
2018-03-13 12:49:24 +00:00
//pageButtonGroup->setExclusive(true);
2018-03-11 03:18:24 +00:00
2018-03-13 12:49:24 +00:00
//skeletonButton->setChecked(true);
2018-03-11 03:18:24 +00:00
QHBoxLayout *topLayout = new QHBoxLayout;
topLayout->addStretch();
2018-03-13 12:49:24 +00:00
//topLayout->addWidget(skeletonButton);
//topLayout->addWidget(motionButton);
//topLayout->addWidget(modelButton);
2018-03-11 03:18:24 +00:00
topLayout->addStretch();
2018-03-13 12:49:24 +00:00
//skeletonButton->adjustSize();
//motionButton->adjustSize();
//modelButton->adjustSize();
2018-03-11 03:18:24 +00:00
2018-03-11 16:02:15 +00:00
m_skeletonEditWidget = new SkeletonEditWidget;
2018-03-13 12:49:24 +00:00
m_modelingWidget = new ModelingWidget(this);
m_modelingWidget->setMinimumSize(128, 128);
m_modelingWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
m_modelingWidget->setWindowFlags(Qt::Tool | Qt::Window);
m_modelingWidget->setWindowTitle("3D Model");
2018-03-11 03:18:24 +00:00
2018-03-13 12:49:24 +00:00
QPushButton *changeTurnaroundButton = new QPushButton("");
changeTurnaroundButton->setIcon(QIcon(":/resources/picture.png"));
2018-03-11 03:18:24 +00:00
QVBoxLayout *rightLayout = new QVBoxLayout;
rightLayout->addSpacing(10);
rightLayout->addWidget(changeTurnaroundButton);
rightLayout->addStretch();
QToolBar *toolbar = new QToolBar;
2018-03-13 12:49:24 +00:00
toolbar->setIconSize(QSize(16, 16));
2018-03-11 03:18:24 +00:00
toolbar->setOrientation(Qt::Vertical);
QAction *addAction = new QAction(tr("Add"), this);
2018-03-13 12:49:24 +00:00
addAction->setIcon(QIcon(":/resources/add.png"));
2018-03-11 03:18:24 +00:00
QAction *selectAction = new QAction(tr("Select"), this);
2018-03-13 12:49:24 +00:00
selectAction->setIcon(QIcon(":/resources/select.png"));
2018-03-11 03:18:24 +00:00
toolbar->addAction(addAction);
toolbar->addAction(selectAction);
QVBoxLayout *leftLayout = new QVBoxLayout;
leftLayout->addWidget(toolbar);
leftLayout->addStretch();
QHBoxLayout *middleLayout = new QHBoxLayout;
middleLayout->addLayout(leftLayout);
2018-03-11 16:02:15 +00:00
middleLayout->addWidget(m_skeletonEditWidget);
2018-03-11 03:18:24 +00:00
middleLayout->addLayout(rightLayout);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(topLayout);
mainLayout->addSpacing(20);
mainLayout->addLayout(middleLayout);
QWidget *centralWidget = new QWidget;
centralWidget->setLayout(mainLayout);
setCentralWidget(centralWidget);
setWindowTitle(tr("Dust 3D"));
2018-03-11 16:02:15 +00:00
bool connectResult;
connectResult = connect(addAction, SIGNAL(triggered(bool)), m_skeletonEditWidget->graphicsView(), SLOT(turnOnAddNodeMode()));
assert(connectResult);
connectResult = connectResult = connect(selectAction, SIGNAL(triggered(bool)), m_skeletonEditWidget->graphicsView(), SLOT(turnOffAddNodeMode()));
assert(connectResult);
connectResult = connect(m_skeletonEditWidget->graphicsView(), SIGNAL(nodesChanged()), this, SLOT(skeletonChanged()));
assert(connectResult);
2018-03-13 06:39:36 +00:00
connectResult = connect(m_skeletonEditWidget, SIGNAL(sizeChanged()), this, SLOT(turnaroundChanged()));
assert(connectResult);
connectResult = connect(changeTurnaroundButton, SIGNAL(released()), this, SLOT(changeTurnaround()));
assert(connectResult);
}
2018-03-11 16:02:15 +00:00
void MainWindow::meshReady()
{
2018-03-13 12:49:24 +00:00
Mesh *resultMesh = m_skeletonToMesh->takeResultMesh();
if (resultMesh) {
if (!m_modelingWidget->isVisible()) {
QRect referenceRect = m_skeletonEditWidget->geometry();
QPoint pos = QPoint(referenceRect.right() - m_modelingWidget->width(),
referenceRect.bottom() - m_modelingWidget->height());
m_modelingWidget->move(pos.x(), pos.y());
m_modelingWidget->show();
}
}
m_modelingWidget->updateMesh(resultMesh);
delete m_skeletonToMesh;
m_skeletonToMesh = NULL;
if (m_skeletonDirty) {
skeletonChanged();
2018-03-11 16:02:15 +00:00
}
}
void MainWindow::skeletonChanged()
{
if (m_skeletonToMesh) {
m_skeletonDirty = true;
return;
}
m_skeletonDirty = false;
2018-03-11 16:02:15 +00:00
QThread *thread = new QThread;
m_skeletonToMesh = new SkeletonToMesh(m_skeletonEditWidget->graphicsView());
m_skeletonToMesh->moveToThread(thread);
connect(thread, SIGNAL(started()), m_skeletonToMesh, SLOT(process()));
connect(m_skeletonToMesh, SIGNAL(finished()), this, SLOT(meshReady()));
connect(m_skeletonToMesh, SIGNAL(finished()), thread, SLOT(quit()));
2018-03-11 16:02:15 +00:00
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
2018-03-13 06:39:36 +00:00
void MainWindow::turnaroundChanged()
{
if (m_turnaroundFilename.isEmpty())
return;
if (m_turnaroundLoader) {
m_turnaroundDirty = true;
return;
}
m_turnaroundDirty = false;
QThread *thread = new QThread;
m_turnaroundLoader = new TurnaroundLoader(m_turnaroundFilename,
m_skeletonEditWidget->rect().size());
m_turnaroundLoader->moveToThread(thread);
connect(thread, SIGNAL(started()), m_turnaroundLoader, SLOT(process()));
connect(m_turnaroundLoader, SIGNAL(finished()), this, SLOT(turnaroundImageReady()));
connect(m_turnaroundLoader, SIGNAL(finished()), thread, SLOT(quit()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
void MainWindow::turnaroundImageReady()
{
QImage *backgroundImage = m_turnaroundLoader->takeResultImage();
2018-03-13 12:49:24 +00:00
if (backgroundImage && backgroundImage->width() > 0 && backgroundImage->height() > 0) {
2018-03-13 06:39:36 +00:00
m_skeletonEditWidget->graphicsView()->updateBackgroundImage(*backgroundImage);
2018-03-13 12:49:24 +00:00
}
2018-03-13 06:39:36 +00:00
delete backgroundImage;
delete m_turnaroundLoader;
m_turnaroundLoader = NULL;
if (m_turnaroundDirty) {
turnaroundChanged();
}
}
void MainWindow::changeTurnaround()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Turnaround Reference Image"),
QString(),
tr("Image Files (*.png *.jpg *.bmp)")).trimmed();
if (fileName.isEmpty())
return;
m_turnaroundFilename = fileName;
turnaroundChanged();
}