dust3d/src/mainwindow.cpp

195 lines
6.4 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-14 02:55:59 +00:00
#include <QApplication>
#include <QDesktopWidget>
2018-03-14 15:22:00 +00:00
#include <QStackedWidget>
#include <QXmlStreamReader>
#include <QBuffer>
2018-03-11 16:02:15 +00:00
#include <assert.h>
#include "mainwindow.h"
2018-03-14 06:43:16 +00:00
#include "skeletonwidget.h"
#include "theme.h"
#include "ds3file.h"
2018-03-14 06:43:16 +00:00
MainWindow::MainWindow()
{
m_modelPageButton = new QPushButton("Model");
m_sharePageButton = new QPushButton("Share");
2018-03-11 03:18:24 +00:00
2018-03-14 06:43:16 +00:00
QWidget *hrWidget = new QWidget;
hrWidget->setFixedHeight(2);
hrWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
hrWidget->setStyleSheet(QString("background-color: #fc6621;"));
hrWidget->setContentsMargins(0, 0, 0, 0);
2018-03-11 03:18:24 +00:00
2018-03-14 06:43:16 +00:00
QButtonGroup *pageButtonGroup = new QButtonGroup;
pageButtonGroup->addButton(m_modelPageButton);
pageButtonGroup->addButton(m_sharePageButton);
2018-03-11 03:18:24 +00:00
m_modelPageButton->setCheckable(true);
m_sharePageButton->setCheckable(true);
2018-03-11 03:18:24 +00:00
2018-03-14 06:43:16 +00:00
pageButtonGroup->setExclusive(true);
2018-03-11 03:18:24 +00:00
m_modelPageButton->setChecked(true);
2018-03-11 03:18:24 +00:00
2018-03-14 06:43:16 +00:00
QHBoxLayout *topButtonsLayout = new QHBoxLayout;
topButtonsLayout->setContentsMargins(0, 0, 0, 0);
topButtonsLayout->setSpacing(0);
topButtonsLayout->addStretch();
topButtonsLayout->addWidget(m_modelPageButton);
topButtonsLayout->addWidget(m_sharePageButton);
2018-03-14 06:43:16 +00:00
topButtonsLayout->addStretch();
2018-03-11 03:18:24 +00:00
2018-03-14 06:43:16 +00:00
QVBoxLayout *topLayout = new QVBoxLayout;
topLayout->setContentsMargins(0, 0, 0, 0);
topLayout->setSpacing(0);
topLayout->addLayout(topButtonsLayout);
topLayout->addWidget(hrWidget);
2018-03-11 16:02:15 +00:00
QVBoxLayout *modelRightLayout = new QVBoxLayout;
2018-03-14 15:22:00 +00:00
modelRightLayout->addSpacing(20);
2018-03-14 15:22:00 +00:00
QPushButton *changeTurnaroundButton = new QPushButton(" Change Turnaround ");
modelRightLayout->addWidget(changeTurnaroundButton);
2018-03-14 15:22:00 +00:00
QPushButton *exportPartsModelButton = new QPushButton(" Export ");
modelRightLayout->addWidget(exportPartsModelButton);
2018-03-14 15:22:00 +00:00
QPushButton *newModelButton = new QPushButton(" New ");
modelRightLayout->addWidget(newModelButton);
2018-03-14 15:22:00 +00:00
QPushButton *loadModelButton = new QPushButton(" Load ");
modelRightLayout->addWidget(loadModelButton);
2018-03-14 15:22:00 +00:00
QPushButton *saveModelButton = new QPushButton(" Save ");
modelRightLayout->addWidget(saveModelButton);
2018-03-14 15:22:00 +00:00
QPushButton *saveModelAsButton = new QPushButton(" Save as ");
modelRightLayout->addWidget(saveModelAsButton);
saveModelAsButton->hide();
2018-03-14 15:22:00 +00:00
modelRightLayout->addStretch();
2018-03-14 15:22:00 +00:00
2018-03-14 06:43:16 +00:00
SkeletonWidget *skeletonWidget = new SkeletonWidget(this);
2018-03-14 15:22:00 +00:00
m_skeletonWidget = skeletonWidget;
QHBoxLayout *modelPageLayout = new QHBoxLayout;
modelPageLayout->addWidget(skeletonWidget);
modelPageLayout->addLayout(modelRightLayout);
2018-03-14 15:22:00 +00:00
QWidget *modelPageWidget = new QWidget;
modelPageWidget->setLayout(modelPageLayout);
2018-03-14 15:22:00 +00:00
QWidget *sharePageWidget = new QWidget;
2018-03-14 15:22:00 +00:00
QStackedWidget *stackedWidget = new QStackedWidget;
stackedWidget->addWidget(modelPageWidget);
stackedWidget->addWidget(sharePageWidget);
2018-03-14 15:22:00 +00:00
m_stackedWidget = stackedWidget;
2018-03-11 03:18:24 +00:00
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(topLayout);
2018-03-14 15:22:00 +00:00
mainLayout->addWidget(stackedWidget);
2018-03-11 03:18:24 +00:00
QWidget *centralWidget = new QWidget;
centralWidget->setLayout(mainLayout);
setCentralWidget(centralWidget);
setWindowTitle(tr("Dust 3D"));
2018-03-11 16:02:15 +00:00
2018-03-14 06:43:16 +00:00
bool connectResult = false;
2018-03-11 16:02:15 +00:00
2018-03-14 15:22:00 +00:00
connectResult = connect(changeTurnaroundButton, SIGNAL(clicked()), skeletonWidget, SLOT(changeTurnaround()));
assert(connectResult);
connectResult = connect(m_modelPageButton, SIGNAL(clicked()), this, SLOT(updatePageButtons()));
2018-03-11 16:02:15 +00:00
assert(connectResult);
2018-03-14 15:22:00 +00:00
connectResult = connect(m_sharePageButton, SIGNAL(clicked()), this, SLOT(updatePageButtons()));
2018-03-14 15:22:00 +00:00
assert(connectResult);
connectResult = connect(saveModelButton, SIGNAL(clicked()), this, SLOT(saveModel()));
2018-03-14 15:22:00 +00:00
assert(connectResult);
connectResult = connect(loadModelButton, SIGNAL(clicked()), this, SLOT(loadModel()));
2018-03-14 15:22:00 +00:00
assert(connectResult);
updatePageButtons();
}
void MainWindow::loadModel()
2018-03-14 15:22:00 +00:00
{
QString filename = QFileDialog::getOpenFileName(this,
tr("Load Model"), ".",
tr("Dust 3D Project (*.ds3)"));
2018-03-14 15:22:00 +00:00
if (filename.isEmpty())
return;
Ds3FileReader ds3Reader(filename);
for (int i = 0; i < ds3Reader.items().size(); ++i) {
Ds3ReaderItem item = ds3Reader.items().at(i);
if (item.type == "model") {
QByteArray data;
ds3Reader.loadItem(item.name, &data);
QXmlStreamReader xmlReader(data);
m_skeletonWidget->graphicsView()->loadFromXmlStream(xmlReader);
} else if (item.type == "asset") {
if (item.name == "canvas.png") {
QByteArray data;
ds3Reader.loadItem(item.name, &data);
QImage image = QImage::fromData(data, "PNG");
m_skeletonWidget->graphicsView()->updateBackgroundImage(image);
}
}
}
2018-03-14 15:22:00 +00:00
}
void MainWindow::saveModel()
2018-03-14 15:22:00 +00:00
{
if (m_saveModelAs.isEmpty()) {
m_saveModelAs = QFileDialog::getSaveFileName(this,
tr("Save Model"), ".",
tr("Dust 3D Project (*.ds3)"));
if (m_saveModelAs.isEmpty()) {
2018-03-14 15:22:00 +00:00
return;
}
}
Ds3FileWriter ds3Writer;
QByteArray modelXml;
QXmlStreamWriter stream(&modelXml);
m_skeletonWidget->graphicsView()->saveToXmlStream(&stream);
if (modelXml.size() > 0)
ds3Writer.add("model1.xml", "model", &modelXml);
QByteArray imageByteArray;
QBuffer pngBuffer(&imageByteArray);
pngBuffer.open(QIODevice::WriteOnly);
m_skeletonWidget->graphicsView()->backgroundImage().save(&pngBuffer, "PNG");
if (imageByteArray.size() > 0)
ds3Writer.add("canvas.png", "asset", &imageByteArray);
ds3Writer.save(m_saveModelAs);
}
2018-03-14 06:43:16 +00:00
void MainWindow::updatePageButtons()
2018-03-11 16:02:15 +00:00
{
if (m_modelPageButton->isChecked()) {
2018-03-14 15:22:00 +00:00
m_stackedWidget->setCurrentIndex(0);
}
if (m_sharePageButton->isChecked()) {
2018-03-14 15:22:00 +00:00
m_stackedWidget->setCurrentIndex(1);
}
m_modelPageButton->setStyleSheet(m_modelPageButton->isChecked() ? Theme::tabButtonSelectedStylesheet : Theme::tabButtonStylesheet);
m_sharePageButton->setStyleSheet(m_sharePageButton->isChecked() ? Theme::tabButtonSelectedStylesheet : Theme::tabButtonStylesheet);
2018-03-11 16:02:15 +00:00
}