Skip to content
mainwindow.cpp 11.8 KiB
Newer Older
raoul's avatar
raoul committed
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "machine.h"
#include <QSerialPortInfo>
#include <QDebug>
raoul's avatar
raoul committed

#include <qtmaterialtooltabs_internal.h>
3dsman's avatar
3dsman committed
#include "qtmaterialiconbutton.h"
#include "qtmaterialtoggle.h"
#include "qtmaterialcheckbox.h"
3dsman's avatar
3dsman committed
#include "OE_ui_toolbox.h"
#include "OE_ui_stitchlist.h"
raoul's avatar
raoul committed
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    machine(new Machine(100, 10))
raoul's avatar
raoul committed
{
	statusBar()->insertWidget(0, &status, 1);
	connect(machine.get(), &Machine::connectionChanged, this, &MainWindow::on_machineConnectionChanged);
	connect(machine.get(), &Machine::stateChanged, this, &MainWindow::on_machineStateChanged);
raoul's avatar
raoul committed
}

MainWindow::~MainWindow()
{
raoul's avatar
raoul committed
}

void MainWindow::keyPressEvent(QKeyEvent* event)
{
	QWidget* widget = ui->tabWidget->currentWidget();
	OE_root* root = static_cast<OE_root*>(widget->property("OEroot").value<void*>());
	root->display->keyPressEvent(event);
raoul's avatar
raoul committed
}

void MainWindow::keyReleaseEvent(QKeyEvent* event)
{
	QWidget* widget = ui->tabWidget->currentWidget();
	OE_root* root = static_cast<OE_root*>(widget->property("OEroot").value<void*>());
	root->display->keyReleaseEvent(event);
raoul's avatar
raoul committed
}

raoul's avatar
raoul committed
{
	QWidget* newtab = new QWidget();
	newtab->setObjectName(QStringLiteral("newtab"));
	QGridLayout * newGridLayout = new QGridLayout(newtab);
	newGridLayout->setSpacing(6);
	newGridLayout->setContentsMargins(11, 11, 11, 11);
	newGridLayout->setObjectName(QStringLiteral("newGridLayout"));
	newGridLayout->setHorizontalSpacing(0);
	newGridLayout->setContentsMargins(0, 0, 0, 0);
	OE_interfaceDisplay* newOpenGLWidget = new OE_interfaceDisplay(newtab);
	newOpenGLWidget->setObjectName(QStringLiteral("newOpenGLWidget"));
	newOpenGLWidget->setMouseTracking(true);
	newOpenGLWidget->setAcceptDrops(true);
	newGridLayout->addWidget(newOpenGLWidget, 0, 0, 1, 1);
3dsman's avatar
3dsman committed
	OE_root* newRoot = new OE_root();
	newRoot->document = new OE_document();
	newRoot->interfaceDisplay = newOpenGLWidget;
	newRoot->interfaceDisplay->setDocument(newRoot->document);
	newRoot->display = newRoot->interfaceDisplay;
	newRoot->controller = new OE_controller(newRoot->display, newRoot->document);
	newRoot->controller->initNewDocument();
	newRoot->display->setController(newRoot->controller);

	//opengl window overlay layout
3dsman's avatar
3dsman committed
	QBoxLayout *openglWidgetLayout = new QHBoxLayout;
	newOpenGLWidget->setLayout(openglWidgetLayout);
3dsman's avatar
3dsman committed
	openglWidgetLayout->setSpacing(0);
    openglWidgetLayout->setMargin(0);
	openglWidgetLayout->setContentsMargins(0, 0, 0, 0);

	//left tool tab
3dsman's avatar
3dsman committed
	m_leftToolTab = new OE_ui_toolBox(this, newRoot);

3dsman's avatar
3dsman committed
	openglWidgetLayout->addWidget(m_leftToolTab);
	openglWidgetLayout->setStretch(0, 0);

	//viewport top bar (viewport options)
	QBoxLayout *topGlWindowLayout = topGlWindowBar();
	openglWidgetLayout->addLayout(topGlWindowLayout);
	openglWidgetLayout->setStretch(1, 0);


	openglWidgetLayout->addStretch(1);

	//viewport top bar (viewport options)
	QBoxLayout *stitchParamLayout = stitchParam();
	openglWidgetLayout->addLayout(stitchParamLayout);
	openglWidgetLayout->setStretch(3, 0);

	//right stitches list
	OE_ui_stitchList *stitchesList = new OE_ui_stitchList();
	stitchesList->setFixedWidth(120);
	/*stitchesList->setMargin(0);
3dsman's avatar
3dsman committed
	stitchesList->setSpacing(0);
	stitchesList->setContentsMargins(0,0,0,0);*/
3dsman's avatar
3dsman committed

	/*QtMaterialFlatButton* button = new QtMaterialFlatButton("here soon the stitches list");
3dsman's avatar
3dsman committed
	button->setFont(QFont("Roboto", 8, QFont::Medium));
	button->setUseThemeColors(true);
	button->setRole(Material::Role::Primary);
	button->setBackgroundMode(Qt::BGMode::OpaqueMode);
3dsman's avatar
3dsman committed
	button->setSizePolicy(QSizePolicy ::Expanding, QSizePolicy ::Expanding );
3dsman's avatar
3dsman committed
	button->setHaloVisible(false);

	button->setMinimumHeight(150);
	stitchesList->addWidget(button);
*/
	openglWidgetLayout->addWidget(stitchesList);
3dsman's avatar
3dsman committed
	openglWidgetLayout->setStretch(4, 0);
	int newIndex = ui->tabWidget->addTab(newtab, QString("newtab"));
	ui->tabWidget->setTabText(ui->tabWidget->indexOf(newtab), QApplication::translate("MainWindow", "Design", nullptr));
raoul's avatar
raoul committed

	QVariant rootPointerProperty(QVariant::fromValue(static_cast<void*>(newRoot)));
	ui->tabWidget->widget(newIndex)->setProperty("OEroot", rootPointerProperty);
raoul's avatar
raoul committed

	ui->tabWidget->setCurrentIndex(newIndex);
	update();
}
raoul's avatar
raoul committed

3dsman's avatar
3dsman committed

QtMaterialIconButton* MainWindow::setIconButton(QString iconFile, const char * method = NULL)
{
	QIcon icon;
	icon.addFile(iconFile, QSize(), QIcon::Normal, QIcon::Off);

	QtMaterialIconButton* button = new QtMaterialIconButton(icon);
	button->setUseThemeColors(false);
	button->setIconSize(QSize(25,25));
	if (method)
		connect(button, SIGNAL(clicked(bool)), this, method);
	return button;
}

QBoxLayout* MainWindow::topGlWindowBar()
{
	QHBoxLayout *topGlWindowLayout = new QHBoxLayout;
	topGlWindowLayout->setAlignment(Qt::AlignTop);
	topGlWindowLayout->setSpacing(4);
	topGlWindowLayout->setMargin(3);

	topGlWindowLayout->addWidget(setIconButton(":/resources/zoom_selection.svg"));
	topGlWindowLayout->addWidget(setIconButton(":/resources/zoom_document.svg"));
	topGlWindowLayout->addSpacing(8);
	topGlWindowLayout->addWidget(setIconButton(":/resources/grid.svg"));
	topGlWindowLayout->addWidget(setIconButton(":/resources/show_curve.svg"));
	topGlWindowLayout->addWidget(setIconButton(":/resources/show_stitch.svg"));
	topGlWindowLayout->addSpacing(8);
	topGlWindowLayout->addWidget(setIconButton(":/resources/select_curve.svg"));
	topGlWindowLayout->addWidget(setIconButton(":/resources/select_stitch.svg"));

	return topGlWindowLayout;
}

QBoxLayout* MainWindow::stitchParam()
{
	QVBoxLayout *topGlWindowLayout = new QVBoxLayout;
	topGlWindowLayout->setAlignment(Qt::AlignTop);

	topGlWindowLayout->addWidget(new QtMaterialToggle());
	QtMaterialCheckBox* m_checkBox = new QtMaterialCheckBox();
	m_checkBox->setText("param2");
	m_checkBox->setIconSize(QSize(5,5));
	topGlWindowLayout->addWidget(m_checkBox);
	topGlWindowLayout->setAlignment(m_checkBox, Qt::AlignCenter);
	topGlWindowLayout->addWidget(new QtMaterialFlatButton("param3"));

	return topGlWindowLayout;
}

QtMaterialFlatButton* MainWindow::setButton(QString name, const char * method = NULL)
{
	QtMaterialFlatButton* button = new QtMaterialFlatButton(name);
	button->setFont(QFont("Roboto", 8, QFont::Medium));
	button->setUseThemeColors(true);

	button->setHaloVisible(false);

	button->setMinimumHeight(0);
	if (method)
		connect(button, SIGNAL(clicked(bool)), this, method);
	return button;
}

OE_interfaceDisplay* MainWindow::getItfDisplay()
{
	QWidget* widget = ui->tabWidget->currentWidget();
	OE_root* root = static_cast<OE_root*>(widget->property("OEroot").value<void*>());
	return root->interfaceDisplay;
}
raoul's avatar
raoul committed

void MainWindow::sendKeyPulse(Qt::Key key, Qt::KeyboardModifiers modifiers)
{
	QKeyEvent fakeKeyPress(QKeyEvent::Type::KeyPress, key, modifiers);
	getItfDisplay()->key(&fakeKeyPress);
	QKeyEvent fakeKeyRelease(QKeyEvent::Type::KeyRelease, key, modifiers);
	getItfDisplay()->key(&fakeKeyRelease);
}

void MainWindow::on_tabWidget_tabCloseRequested(int index)
{
	// TODO some brain and warn if loss of data
	if (ui->tabWidget->count() == 1)
		addTab();
	QWidget* widget = ui->tabWidget->widget(index);
	OE_root* root = static_cast<OE_root*>(widget->property("OEroot").value<void*>());
	delete root->document;
	delete root->controller;
	delete root;
	ui->tabWidget->removeTab(index);
raoul's avatar
raoul committed
}

void MainWindow::on_actionNew_triggered()
{
	addTab();
}

void MainWindow::on_actionClose_triggered()
{
	on_tabWidget_tabCloseRequested(ui->tabWidget->currentIndex());
}

void MainWindow::on_actionOpen_triggered()
{ // TODO real call to a load function, not a fake key event
	sendKeyPulse(Qt::Key_O, Qt::KeyboardModifier::ControlModifier);
}

void MainWindow::on_actionSave_triggered()
{ // TODO real call to a save function, not a fake key event
	sendKeyPulse(Qt::Key_S, Qt::KeyboardModifier::ControlModifier);
}

void MainWindow::on_actionLine_triggered()
{ // TODO real call to a save function, not a fake key event
	sendKeyPulse(Qt::Key_L, Qt::KeyboardModifier::NoModifier);
	sendKeyPulse(Qt::Key_Z, Qt::KeyboardModifier::ControlModifier);
	sendKeyPulse(Qt::Key_Z, Qt::KeyboardModifier::ControlModifier|Qt::KeyboardModifier::ShiftModifier);
	sendKeyPulse(Qt::Key_B, Qt::KeyboardModifier::NoModifier);
void MainWindow::on_actionPattern1_triggered() { sendKeyPulse(Qt::Key_1, Qt::KeyboardModifier::KeypadModifier); }
void MainWindow::on_actionPattern2_triggered() { sendKeyPulse(Qt::Key_2, Qt::KeyboardModifier::KeypadModifier); }
void MainWindow::on_actionPattern3_triggered() { sendKeyPulse(Qt::Key_3, Qt::KeyboardModifier::KeypadModifier); }
void MainWindow::on_actionPattern4_triggered() { sendKeyPulse(Qt::Key_4, Qt::KeyboardModifier::KeypadModifier); }
void MainWindow::on_actionPattern5_triggered() { sendKeyPulse(Qt::Key_5, Qt::KeyboardModifier::KeypadModifier); }
void MainWindow::on_actionPattern6_triggered() { sendKeyPulse(Qt::Key_6, Qt::KeyboardModifier::KeypadModifier); }
void MainWindow::on_actionPattern7_triggered() { sendKeyPulse(Qt::Key_7, Qt::KeyboardModifier::KeypadModifier); }
void MainWindow::on_actionPattern8_triggered() { sendKeyPulse(Qt::Key_8, Qt::KeyboardModifier::KeypadModifier); }
void MainWindow::on_actionPattern9_triggered() { sendKeyPulse(Qt::Key_9, Qt::KeyboardModifier::KeypadModifier); }

void MainWindow::on_tabWidget_currentChanged(int index)
{
	if (index >= 0)
	{ // Discard previous modifiers on that widget
		QKeyEvent fakeKey(QKeyEvent::Type::KeyRelease, Qt::Key_Escape, Qt::KeyboardModifier::NoModifier);
		getItfDisplay()->key(&fakeKey);
	sendKeyPulse(Qt::Key_C, Qt::KeyboardModifier::NoModifier);
}

void MainWindow::on_actionReverse_curve_R_triggered()
{
	sendKeyPulse(Qt::Key_R, Qt::KeyboardModifier::NoModifier);
raoul's avatar
raoul committed
{
	sendKeyPulse(Qt::Key_F, Qt::KeyboardModifier::NoModifier);
raoul's avatar
raoul committed
}

void MainWindow::on_actionConnectToMachine_triggered(bool checked)
{
	QString msg;
	//msg += checked ? QString::fromUtf8("[ Connected  ] ") : QString::fromUtf8("[Disconnected] ");
	//msg += QString("Seq %1. Status %2. Space %3/%4. Pos (%5, %6). Point %7. tSensor %8")
	//         .arg(42,5).arg(7).arg(800,5).arg(1016,-5).arg(24,5).arg(32,-5).arg(301,4).arg(123);
	//status.setText(msg);
	if (checked)
	{
		QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts();
		if (ports.size())
		{
			machine->connectPort(ports.front().portName());
			for (const QSerialPortInfo& port : ports)
			{
				qDebug() << port.portName() << ". " << port.description() << ". " << port.manufacturer() << ". "
				         << port.vendorIdentifier() << ". " << port.productIdentifier();
			}
		}
	}
	else
	{
		machine->disconnectPort();
	}
}
void MainWindow::on_machineConnectionChanged(bool connected)
{
	if (!connected)
	{
		QString msg = status.text().replace("[ Connected  ]", "[Disconnected]");
		status.setText(msg);
	}
	ui->actionMachineRun->setEnabled(connected);
	ui->actionMachineLoad->setEnabled(connected);
3dsman's avatar
3dsman committed
	machineConnectionChanged(connected);
}
void MainWindow::on_machineStateChanged(Machine::SlaveInfo info)
{
	QString msg;
	msg = QString("[ Connected  ] Seq %1. Status %2. Space %3/%4. Pos (%5, %6). Point %7")
	        .arg(info.sequence,5)
	        .arg(info.status)
	        .arg(info.spaceSize-info.freeSpace,5).arg(info.spaceSize,-5)
	        .arg(info.posX,5).arg(info.posY,-5)
	        .arg(info.nPoint,4);
	status.setText(msg);
	printf("%s\n",msg.toStdString().c_str());
}

void MainWindow::on_actionMachineRun_triggered()
3dsman's avatar
3dsman committed
{
	machineRun();
}

void MainWindow::on_actionMachineLoad_triggered()
{
	machineLoad();
}
void MainWindow::machineRun()
{
	if (machine)
		machine->startMove();
}

3dsman's avatar
3dsman committed
void MainWindow::machineLoad()
{
	if (machine)
	{
		QWidget* widget = ui->tabWidget->currentWidget();
		OE_root* root = static_cast<OE_root*>(widget->property("OEroot").value<void*>());
		root->controller->sendInstPoint(machine.get());
	}
}