valentina/src/app/puzzle/puzzlemainwindow.cpp

1024 lines
36 KiB
C++
Raw Normal View History

2020-02-16 17:17:50 +01:00
/************************************************************************
**
** @file puzzlemainwindow.cpp
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 16 2, 2020
**
** @brief
** @copyright
** This source code is part of the Valentina project, a pattern making
** program, whose allow create and modeling patterns of clothing.
** Copyright (C) 2020 Valentina project
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
**
** Valentina is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Valentina is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#include "puzzlemainwindow.h"
2020-04-18 16:32:54 +02:00
#include <QFileDialog>
2020-05-23 13:49:38 +02:00
#include <QCloseEvent>
2020-04-18 16:32:54 +02:00
2020-02-16 17:17:50 +01:00
#include "ui_puzzlemainwindow.h"
2020-05-23 13:49:38 +02:00
#include "dialogs/vpdialogabout.h"
2020-04-18 16:32:54 +02:00
#include "xml/vpuzzlelayoutfilewriter.h"
2020-04-19 16:01:46 +02:00
#include "xml/vpuzzlelayoutfilereader.h"
#include "puzzleapplication.h"
#include "../vlayout/vrawlayout.h"
#include "../vmisc/vsysexits.h"
#include "../vmisc/projectversion.h"
#include "../ifc/xml/vlayoutconverter.h"
#include "../ifc/exception/vexception.h"
2020-04-19 16:01:46 +02:00
#include <QLoggingCategory>
2020-04-19 16:01:46 +02:00
QT_WARNING_PUSH
QT_WARNING_DISABLE_CLANG("-Wmissing-prototypes")
QT_WARNING_DISABLE_INTEL(1418)
Q_LOGGING_CATEGORY(pWindow, "p.window")
QT_WARNING_POP
2020-02-16 17:17:50 +01:00
2020-02-16 18:18:39 +01:00
//---------------------------------------------------------------------------------------------------------------------
PuzzleMainWindow::PuzzleMainWindow(const VPuzzleCommandLinePtr &cmd, QWidget *parent) :
2020-02-16 17:17:50 +01:00
QMainWindow(parent),
2020-04-13 12:24:26 +02:00
ui(new Ui::PuzzleMainWindow),
m_cmd(cmd)
2020-02-16 17:17:50 +01:00
{
2020-04-18 16:32:54 +02:00
2020-04-19 11:58:43 +02:00
m_layout = new VPuzzleLayout();
2020-04-26 12:09:28 +02:00
// ----- for test purposes, to be removed------------------
2020-05-05 07:44:20 +02:00
m_layout->SetLayoutMarginsConverted(2, 2, 2, 2);
m_layout->SetLayoutSizeConverted(30.0, 45);
m_layout->SetPiecesGapConverted(1);
2020-04-19 11:58:43 +02:00
m_layout->SetUnit(Unit::Cm);
2020-04-19 12:47:38 +02:00
m_layout->SetWarningSuperpositionOfPieces(true);
// --------------------------------------------------------
ui->setupUi(this);
InitMenuBar();
InitProperties();
InitPieceCarrousel();
2020-05-05 07:44:20 +02:00
InitMainGraphics();
2020-04-19 11:58:43 +02:00
SetPropertiesData();
ReadSettings();
2020-02-16 17:17:50 +01:00
}
2020-02-16 18:18:39 +01:00
//---------------------------------------------------------------------------------------------------------------------
2020-02-16 17:17:50 +01:00
PuzzleMainWindow::~PuzzleMainWindow()
{
delete ui;
2020-04-26 12:09:28 +02:00
delete m_pieceCarrousel;
2020-02-16 17:17:50 +01:00
}
2020-02-16 18:18:39 +01:00
//---------------------------------------------------------------------------------------------------------------------
bool PuzzleMainWindow::LoadFile(QString path)
2020-02-16 18:18:39 +01:00
{
2020-05-02 09:30:53 +02:00
try
{
VLayoutConverter converter(path);
path = converter.Convert();
}
catch (VException &e)
{
qCCritical(pWindow, "%s\n\n%s\n\n%s", qUtf8Printable(tr("File error.")),
qUtf8Printable(e.ErrorMessage()), qUtf8Printable(e.DetailedInformation()));
return false;
}
2020-04-19 16:01:46 +02:00
QFile file(path);
file.open(QIODevice::ReadOnly);
QScopedPointer<VPuzzleLayoutFileReader> fileReader(new VPuzzleLayoutFileReader());
2020-04-19 16:01:46 +02:00
if(m_layout == nullptr)
{
m_layout = new VPuzzleLayout();
}
fileReader->ReadFile(m_layout, &file);
// TODO / FIXME : better return value and error handling
return true;
}
//---------------------------------------------------------------------------------------------------------------------
bool PuzzleMainWindow::SaveFile(const QString &path)
{
QFile file(path);
file.open(QIODevice::WriteOnly);
VPuzzleLayoutFileWriter *fileWriter = new VPuzzleLayoutFileWriter();
fileWriter->WriteFile(m_layout, &file);
// TODO / FIXME : better return value and error handling
2020-02-16 20:51:41 +01:00
return true;
2020-02-16 18:18:39 +01:00
}
2020-04-10 21:08:29 +02:00
//---------------------------------------------------------------------------------------------------------------------
2020-04-26 12:09:28 +02:00
void PuzzleMainWindow::ImportRawLayouts(const QStringList &rawLayouts)
{
2020-04-26 12:09:28 +02:00
VRawLayout rawLayoutReader;
2020-04-26 12:09:28 +02:00
for(auto &path : rawLayouts)
{
VRawLayoutData data;
2020-04-26 12:09:28 +02:00
if (rawLayoutReader.ReadFile(path, data))
{
2020-04-26 12:09:28 +02:00
for (int i = 0; i < data.pieces.size(); ++i)
{
VLayoutPiece rawPiece = data.pieces.at(i);
2020-04-26 15:36:20 +02:00
2020-05-05 07:44:20 +02:00
// We translate the piece, so that the origin of the bounding rect of the piece is at (0,0)
// It makes positioning later on easier.
QRectF boundingRect = rawPiece.DetailBoundingRect();
QPointF topLeft = boundingRect.topLeft();
rawPiece.Translate(-topLeft.x(), -topLeft.y());
2020-04-26 15:36:20 +02:00
2020-05-02 12:17:06 +02:00
// TODO / FIXME: make a few tests, on the data to check for validity. If not
//
// If seam allowance enabled, but the path is empty — invalid.
// If seam line path not hidden, but the path is empty — invalid.
// If seam allowance is built-in, but the seam line path is empty — invalid.
2020-05-05 07:44:20 +02:00
// TODO for feature "Update piece" : CreateOrUpdate() function indstead of CreatePiece()
2020-04-26 12:09:28 +02:00
VPuzzlePiece *piece = CreatePiece(rawPiece);
m_layout->GetUnplacedPiecesLayer()->AddPiece(piece);
}
m_pieceCarrousel->Refresh();
}
else
{
qCCritical(pWindow, "%s\n", qPrintable(tr("Could not extract data from file '%1'. %2")
2020-04-26 12:09:28 +02:00
.arg(path, rawLayoutReader.ErrorString())));
if (m_cmd != nullptr && not m_cmd->IsGuiEnabled())
{
m_cmd->ShowHelp(V_EX_DATAERR);
}
}
}
}
2020-04-26 12:09:28 +02:00
//---------------------------------------------------------------------------------------------------------------------
VPuzzlePiece* PuzzleMainWindow::CreatePiece(const VLayoutPiece &rawPiece)
{
VPuzzlePiece *piece = new VPuzzlePiece();
piece->SetName(rawPiece.GetName());
piece->SetUuid(rawPiece.GetUUID());
2020-04-26 15:36:20 +02:00
piece->SetCuttingLine(rawPiece.GetMappedSeamAllowancePoints());
2020-05-02 12:17:06 +02:00
piece->SetSeamLine(rawPiece.GetMappedContourPoints());
2020-04-26 12:09:28 +02:00
piece->SetIsGrainlineEnabled(rawPiece.IsGrainlineEnabled());
if(rawPiece.IsGrainlineEnabled())
{
piece->SetGrainlineAngle(rawPiece.GrainlineAngle());
piece->SetGrainline(rawPiece.GetGrainline());
}
2020-04-26 12:09:28 +02:00
// TODO : set all the information we need for the piece!
2020-05-05 17:40:36 +02:00
//
connect(piece, &VPuzzlePiece::SelectionChanged, this, &PuzzleMainWindow::on_PieceSelectionChanged);
connect(piece, &VPuzzlePiece::PositionChanged, this, &PuzzleMainWindow::on_PiecePositionChanged);
2020-05-09 14:45:36 +02:00
connect(piece, &VPuzzlePiece::RotationChanged, this, &PuzzleMainWindow::on_PieceRotationChanged);
2020-05-05 17:40:36 +02:00
2020-04-26 12:09:28 +02:00
return piece;
}
2020-04-10 21:08:29 +02:00
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::InitMenuBar()
{
// most of the actions are connected through name convention (auto-connection)
2020-04-13 12:24:26 +02:00
// -------------------- connects the actions for the file menu
2020-04-10 21:08:29 +02:00
connect(ui->actionExit, &QAction::triggered, this, &PuzzleMainWindow::close);
2020-04-13 12:24:26 +02:00
// -------------------- connects the actions for the edit menu
2020-04-10 21:08:29 +02:00
// TODO : initialise the undo / redo
2020-04-13 12:24:26 +02:00
// -------------------- connects the actions for the windows menu
2020-04-10 21:08:29 +02:00
// TODO : initialise the entries for the different windows
2020-04-13 12:24:26 +02:00
// Add dock properties action
QAction* actionDockWidgetToolOptions = ui->dockWidgetProperties->toggleViewAction();
ui->menuWindows->addAction(actionDockWidgetToolOptions);
2020-04-10 21:08:29 +02:00
}
2020-04-11 11:17:53 +02:00
//---------------------------------------------------------------------------------------------------------------------
2020-04-13 12:24:26 +02:00
void PuzzleMainWindow::InitProperties()
2020-04-11 11:17:53 +02:00
{
2020-04-11 16:25:23 +02:00
InitPropertyTabCurrentPiece();
InitPropertyTabLayout();
InitPropertyTabLayers();
InitPropertyTabTiles();
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::InitPropertyTabCurrentPiece()
{
// ------------------------------ placement -----------------------------------
connect(ui->doubleSpinBoxCurrentPieceBoxPositionX, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
2020-05-05 17:40:36 +02:00
&PuzzleMainWindow::on_CurrentPiecePositionEdited);
connect(ui->doubleSpinBoxCurrentPieceBoxPositionY, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
2020-05-05 17:40:36 +02:00
&PuzzleMainWindow::on_CurrentPiecePositionEdited);
2020-04-11 11:17:53 +02:00
}
2020-04-11 16:25:23 +02:00
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::InitPropertyTabLayout()
{
// -------------------- init the unit combobox ---------------------
ui->comboBoxLayoutUnit->addItem(tr("Centimeters"), QVariant(UnitsToStr(Unit::Cm)));
ui->comboBoxLayoutUnit->addItem(tr("Millimiters"), QVariant(UnitsToStr(Unit::Mm)));
ui->comboBoxLayoutUnit->addItem(tr("Inches"), QVariant(UnitsToStr(Unit::Inch)));
// set default unit - TODO when we have the setting for the unit
2020-04-13 14:05:22 +02:00
// const qint32 indexUnit = -1;//ui->comboBoxLayoutUnit->findData(qApp->ValentinaSettings()->GetUnit());
// if (indexUnit != -1)
// {
// ui->comboBoxLayoutUnit->setCurrentIndex(indexUnit);
// }
2020-04-11 16:25:23 +02:00
// some of the UI Elements are connected to the slots via auto-connect
// see https://doc.qt.io/qt-5/designer-using-a-ui-file.html#widgets-and-dialogs-with-auto-connect
2020-04-11 16:25:23 +02:00
// -------------------- layout width, length, orientation ------------------------
connect(ui->doubleSpinBoxLayoutWidth, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
&PuzzleMainWindow::on_LayoutSizeChanged);
connect(ui->doubleSpinBoxLayoutLength, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
&PuzzleMainWindow::on_LayoutSizeChanged);
connect(ui->radioButtonLayoutPortrait, QOverload<bool>::of(&QRadioButton::clicked), this,
&PuzzleMainWindow::on_LayoutOrientationChanged);
connect(ui->radioButtonLayoutLandscape, QOverload<bool>::of(&QRadioButton::clicked), this,
&PuzzleMainWindow::on_LayoutOrientationChanged);
2020-04-11 16:25:23 +02:00
// -------------------- margins ------------------------
connect(ui->doubleSpinBoxLayoutMarginTop, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
&PuzzleMainWindow::on_LayoutMarginChanged);
connect(ui->doubleSpinBoxLayoutMarginRight, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
&PuzzleMainWindow::on_LayoutMarginChanged);
connect(ui->doubleSpinBoxLayoutMarginBottom, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
&PuzzleMainWindow::on_LayoutMarginChanged);
connect(ui->doubleSpinBoxLayoutMarginLeft, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
&PuzzleMainWindow::on_LayoutMarginChanged);
2020-04-11 16:25:23 +02:00
// ------------------- follow grainline -----------------------
connect(ui->radioButtonLayoutFollowGrainlineNo, QOverload<bool>::of(&QRadioButton::clicked), this,
&PuzzleMainWindow::on_LayoutFollowGrainlineChanged);
connect(ui->radioButtonLayoutFollowGrainlineVertical, QOverload<bool>::of(&QRadioButton::clicked), this,
&PuzzleMainWindow::on_LayoutFollowGrainlineChanged);
connect(ui->radioButtonLayoutFollowGrainlineHorizontal, QOverload<bool>::of(&QRadioButton::clicked), this,
&PuzzleMainWindow::on_LayoutFollowGrainlineChanged);
2020-04-11 16:25:23 +02:00
// -------------------- export ---------------------------
2020-04-11 16:32:49 +02:00
// TODO init the file format export combobox
2020-04-11 16:25:23 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::InitPropertyTabTiles()
{
// for the MVP we don't want the tiles tab.
// we remove it. As soon as we need it, update this code
ui->tabWidgetProperties->removeTab(2); // remove tiles
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::InitPropertyTabLayers()
{
// for the MVP we don't want the layers tab.
// we remove it. As soon as we need it, update this code
ui->tabWidgetProperties->removeTab(3); // remove layers
}
2020-04-13 12:24:26 +02:00
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::InitPieceCarrousel()
{
2020-04-26 14:03:43 +02:00
m_pieceCarrousel = new VPieceCarrousel(m_layout, ui->dockWidgetPieceCarrousel);
2020-04-26 12:09:28 +02:00
ui->dockWidgetPieceCarrousel->setWidget(m_pieceCarrousel);
2020-04-13 12:24:26 +02:00
connect(ui->dockWidgetPieceCarrousel, QOverload<Qt::DockWidgetArea>::of(&QDockWidget::dockLocationChanged), this,
&PuzzleMainWindow::on_PieceCarrouselLocationChanged);
2020-04-13 12:24:26 +02:00
}
2020-04-19 11:58:43 +02:00
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::SetPropertiesData()
{
if(m_layout == nullptr)
{
// TODO : hide the tabs when there is no layout
}
else
{
SetPropertyTabCurrentPieceData();
SetPropertyTabLayoutData();
SetPropertyTabTilesData();
SetPropertyTabLayersData();
}
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::SetPropertyTabCurrentPieceData()
{
2020-05-05 17:40:36 +02:00
if(m_selectedPieces.count() == 0)
2020-04-19 11:58:43 +02:00
{
// show the content "no piece selected"
2020-05-05 17:40:36 +02:00
ui->containerCurrentPieceNoData->setVisible(true);
ui->containerCurrentPieceData->setVisible(false);
ui->containerCurrentPieceMultipleData->setVisible(false);
2020-04-19 11:58:43 +02:00
}
2020-05-05 17:40:36 +02:00
else if(m_selectedPieces.count() == 1)
2020-04-19 11:58:43 +02:00
{
// show the content "selected piece data"
2020-05-01 19:08:48 +02:00
ui->containerCurrentPieceNoData->setVisible(false);
ui->containerCurrentPieceData->setVisible(true);
ui->containerCurrentPieceMultipleData->setVisible(false);
VPuzzlePiece *selectedPiece = m_selectedPieces.first();
2020-05-01 19:08:48 +02:00
// set the value to the current piece
2020-05-05 17:40:36 +02:00
ui->lineEditCurrentPieceName->setText(selectedPiece->GetName());
2020-05-05 17:40:36 +02:00
ui->checkBoxCurrentPieceShowSeamline->setChecked(selectedPiece->GetShowSeamLine());
ui->checkBoxCurrentPieceMirrorPiece->setChecked(selectedPiece->GetPieceMirrored());
2020-05-01 19:08:48 +02:00
2020-05-05 17:40:36 +02:00
QPointF pos = selectedPiece->GetPosition();
2020-05-11 16:44:30 +02:00
SetDoubleSpinBoxValue(ui->doubleSpinBoxCurrentPieceBoxPositionX,
UnitConvertor(pos.x(), Unit::Px, m_layout->GetUnit()));
SetDoubleSpinBoxValue(ui->doubleSpinBoxCurrentPieceBoxPositionY,
UnitConvertor(pos.y(), Unit::Px, m_layout->GetUnit()));
2020-05-05 17:40:36 +02:00
2020-05-09 11:13:29 +02:00
qreal angle = selectedPiece->GetRotation();
SetDoubleSpinBoxValue(ui->doubleSpinBoxCurrentPieceAngle, angle);
2020-05-05 17:40:36 +02:00
}
else
{
// show the content "multiple pieces selected"
ui->containerCurrentPieceNoData->setVisible(false);
ui->containerCurrentPieceData->setVisible(false);
ui->containerCurrentPieceMultipleData->setVisible(true);
// if needed in the future, we can show some properties that coul be edited for all the pieces
2020-04-19 11:58:43 +02:00
}
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::SetPropertyTabLayoutData()
{
// set Unit
2020-04-23 14:26:24 +02:00
int index = ui->comboBoxLayoutUnit->findData(QVariant(UnitsToStr(m_layout->GetUnit())));
2020-04-19 11:58:43 +02:00
if(index != -1)
{
ui->comboBoxLayoutUnit->blockSignals(true); // FIXME: is there a better way to block the signals?
ui->comboBoxLayoutUnit->setCurrentIndex(index);
ui->comboBoxLayoutUnit->blockSignals(false);
}
// set Width / Length
QSizeF size = m_layout->GetLayoutSizeConverted();
SetDoubleSpinBoxValue(ui->doubleSpinBoxLayoutWidth, size.width());
SetDoubleSpinBoxValue(ui->doubleSpinBoxLayoutLength, size.height());
2020-04-19 12:47:38 +02:00
// Set Orientation
if(size.width() <= size.height())
{
ui->radioButtonLayoutPortrait->setChecked(true);
}
else
{
ui->radioButtonLayoutLandscape->setChecked(true);
}
2020-04-19 11:58:43 +02:00
// set margins
QMarginsF margins = m_layout->GetLayoutMarginsConverted();
SetDoubleSpinBoxValue(ui->doubleSpinBoxLayoutMarginLeft, margins.left());
SetDoubleSpinBoxValue(ui->doubleSpinBoxLayoutMarginTop, margins.top());
SetDoubleSpinBoxValue(ui->doubleSpinBoxLayoutMarginRight, margins.right());
SetDoubleSpinBoxValue(ui->doubleSpinBoxLayoutMarginBottom, margins.bottom());
// set pieces gap
SetDoubleSpinBoxValue(ui->doubleSpinBoxLayoutPiecesGap, m_layout->GetPiecesGapConverted());
2020-04-19 12:47:38 +02:00
// set the checkboxes
SetCheckBoxValue(ui->checkBoxLayoutWarningPiecesOutOfBound, m_layout->GetWarningPiecesOutOfBound());
SetCheckBoxValue(ui->checkBoxLayoutWarningPiecesSuperposition, m_layout->GetWarningSuperpositionOfPieces());
SetCheckBoxValue(ui->checkBoxLayoutStickyEdges, m_layout->GetStickyEdges());
2020-04-19 11:58:43 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::SetPropertyTabTilesData()
{
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::SetPropertyTabLayersData()
{
}
2020-05-05 07:44:20 +02:00
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::InitMainGraphics()
{
m_graphicsView = new VPuzzleMainGraphicsView(m_layout, this);
ui->centralWidget->layout()->addWidget(m_graphicsView);
m_graphicsView->RefreshLayout();
}
2020-04-19 11:58:43 +02:00
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::SetDoubleSpinBoxValue(QDoubleSpinBox *spinBox, qreal value)
{
spinBox->blockSignals(true);
spinBox->setValue(value);
spinBox->blockSignals(false);
}
2020-04-13 12:24:26 +02:00
2020-04-19 12:47:38 +02:00
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::SetCheckBoxValue(QCheckBox *checkbox, bool value)
{
checkbox->blockSignals(true);
checkbox->setChecked(value);
checkbox->blockSignals(false);
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::ReadSettings()
{
qCDebug(pWindow, "Reading settings.");
const VPuzzleSettings *settings = qApp->PuzzleSettings();
if (settings->status() == QSettings::NoError)
{
restoreGeometry(settings->GetGeometry());
restoreState(settings->GetWindowState());
restoreState(settings->GetToolbarsState(), APP_VERSION);
ui->dockWidgetProperties->setVisible(settings->IsDockWidgetPropertiesActive());
ui->dockWidgetPropertiesContents->setVisible(settings->IsDockWidgetPropertiesContentsActive());
// Scene antialiasing
m_graphicsView->SetAntialiasing(settings->GetGraphicalOutput());
// Stack limit
// qApp->getUndoStack()->setUndoLimit(settings->GetUndoCount());
}
else
{
qWarning() << tr("Cannot read settings from a malformed .INI file.");
}
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::WriteSettings()
{
VPuzzleSettings *settings = qApp->PuzzleSettings();
settings->SetGeometry(saveGeometry());
settings->SetWindowState(saveState());
settings->SetToolbarsState(saveState(APP_VERSION));
settings->SetDockWidgetPropertiesActive(ui->dockWidgetProperties->isEnabled());
settings->SetDockWidgetPropertiesContentsActive(ui->dockWidgetPropertiesContents->isEnabled());
2020-04-19 12:47:38 +02:00
settings->sync();
if (settings->status() == QSettings::AccessError)
{
qWarning() << tr("Cannot save settings. Access denied.");
}
}
//---------------------------------------------------------------------------------------------------------------------
bool PuzzleMainWindow::MaybeSave()
{
// TODO: Implement maybe save check
// if (this->isWindowModified())
// {
// if (curFile.isEmpty() && ui->tableWidget->rowCount() == 0)
// {
// return true;// Don't ask if file was created without modifications.
// }
// QScopedPointer<QMessageBox> messageBox(new QMessageBox(tr("Unsaved changes"),
// tr("Measurements have been modified.\n"
// "Do you want to save your changes?"),
// QMessageBox::Warning, QMessageBox::Yes, QMessageBox::No,
// QMessageBox::Cancel, this, Qt::Sheet));
// messageBox->setDefaultButton(QMessageBox::Yes);
// messageBox->setEscapeButton(QMessageBox::Cancel);
// messageBox->setButtonText(QMessageBox::Yes, curFile.isEmpty() || mIsReadOnly ? tr("Save…") : tr("Save"));
// messageBox->setButtonText(QMessageBox::No, tr("Don't Save"));
// messageBox->setWindowModality(Qt::ApplicationModal);
// const auto ret = static_cast<QMessageBox::StandardButton>(messageBox->exec());
// switch (ret)
// {
// case QMessageBox::Yes:
// if (mIsReadOnly)
// {
// return FileSaveAs();
// }
// else
// {
// return FileSave();
// }
// case QMessageBox::No:
// return true;
// case QMessageBox::Cancel:
// return false;
// default:
// break;
// }
// }
return true;
}
2020-04-13 12:24:26 +02:00
2020-04-10 21:08:29 +02:00
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_actionNew_triggered()
2020-04-10 21:08:29 +02:00
{
// just for test purpuses, to be removed:
QMessageBox msgBox;
msgBox.setText("TODO PuzzleMainWindow::New");
int ret = msgBox.exec();
2020-04-11 16:25:23 +02:00
Q_UNUSED(ret);
2020-04-10 21:08:29 +02:00
// TODO
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::closeEvent(QCloseEvent *event)
{
#if defined(Q_OS_MAC) && QT_VERSION < QT_VERSION_CHECK(5, 11, 1)
// Workaround for Qt bug https://bugreports.qt.io/browse/QTBUG-43344
static int numCalled = 0;
if (numCalled++ >= 1)
{
return;
}
#endif
if (MaybeSave())
{
WriteSettings();
event->accept();
deleteLater();
}
else
{
event->ignore();
}
}
2020-04-10 21:08:29 +02:00
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_actionOpen_triggered()
2020-04-10 21:08:29 +02:00
{
qCDebug(pWindow, "Openning puzzle layout file.");
2020-04-10 21:08:29 +02:00
2020-04-19 16:01:46 +02:00
const QString filter(tr("Layout files") + QLatin1String(" (*.vlt)"));
2020-04-11 16:25:23 +02:00
2020-04-19 16:01:46 +02:00
//Get list last open files
QStringList recentFiles = qApp->PuzzleSettings()->GetRecentFileList();
QString dir;
if (recentFiles.isEmpty())
{
dir = QDir::homePath();
}
else
{
//Absolute path to last open file
dir = QFileInfo(recentFiles.first()).absolutePath();
}
qCDebug(pWindow, "Run QFileDialog::getOpenFileName: dir = %s.", qUtf8Printable(dir));
2020-04-23 20:17:49 +02:00
const QString filePath = QFileDialog::getOpenFileName(this, tr("Open file"), dir, filter, nullptr);
2020-04-19 16:01:46 +02:00
if (filePath.isEmpty())
{
return;
}
// TODO : if m_layout == nullptr, open in current window
// otherwise open in new window
// TODO : if layout file has a lock, warning message
if(!LoadFile(filePath))
{
return;
}
// Updates the list of recent files
recentFiles.removeAll(filePath);
recentFiles.prepend(filePath);
while (recentFiles.size() > MaxRecentFiles)
{
recentFiles.removeLast();
}
qApp->PuzzleSettings()->SetRecentFileList(recentFiles);
// updates the properties with the loaded data
SetPropertiesData();
// TODO : update the Carrousel and the QGraphicView
2020-04-10 21:08:29 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_actionSave_triggered()
2020-04-10 21:08:29 +02:00
{
// just for test purpuses, to be removed:
QMessageBox msgBox;
msgBox.setText("TODO PuzzleMainWindow::Save");
int ret = msgBox.exec();
2020-04-11 16:25:23 +02:00
Q_UNUSED(ret);
2020-04-10 21:08:29 +02:00
// TODO
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_actionSaveAs_triggered()
2020-04-10 21:08:29 +02:00
{
2020-04-18 16:32:54 +02:00
// TODO / FIXME : See valentina how the save is done over there. we need to add the extension .vlt, check for empty file names etc.
2020-04-10 21:08:29 +02:00
2020-04-19 16:01:46 +02:00
//Get list last open files
QStringList recentFiles = qApp->PuzzleSettings()->GetRecentFileList();
QString dir;
if (recentFiles.isEmpty())
{
dir = QDir::homePath();
}
else
{
//Absolute path to last open file
dir = QFileInfo(recentFiles.first()).absolutePath();
}
2020-04-11 16:25:23 +02:00
2020-04-19 16:01:46 +02:00
QString filters(tr("Layout files") + QLatin1String("(*.vlt)"));
2020-04-18 16:32:54 +02:00
QString fileName = QFileDialog::getSaveFileName(this, tr("Save as"),
2020-04-19 16:01:46 +02:00
dir + QLatin1String("/") + tr("Layout") + QLatin1String(".vlt"),
2020-04-18 16:32:54 +02:00
filters, nullptr
#ifdef Q_OS_LINUX
, QFileDialog::DontUseNativeDialog
#endif
);
2020-04-19 16:01:46 +02:00
SaveFile(fileName);
2020-04-10 21:08:29 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_actionImportRawLayout_triggered()
2020-04-10 21:08:29 +02:00
{
2020-04-26 12:09:28 +02:00
// TODO: here the code is probably just bad, to be edited
2020-04-10 21:08:29 +02:00
2020-04-26 12:09:28 +02:00
QString dir;
if (true)
{
dir = QDir::homePath();
}
else
{
// TODO / FIXME get the default path for raw layouts
}
const QString filter(tr("Raw Layout files") + QLatin1String(" (*.rld)"));
qCDebug(pWindow, "Run QFileDialog::getOpenFileName: dir = %s.", qUtf8Printable(dir));
const QString filePath = QFileDialog::getOpenFileName(this, tr("Open file"), dir, filter, nullptr);
QStringList rawLayouts = QStringList();
rawLayouts.append(filePath);
ImportRawLayouts(rawLayouts);
// TODO / FIXME : better error handling
2020-04-11 16:25:23 +02:00
2020-04-10 21:08:29 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_actionCloseLayout_triggered()
2020-04-10 21:08:29 +02:00
{
// just for test purpuses, to be removed:
QMessageBox msgBox;
msgBox.setText("TODO PuzzleMainWindow::CloseLayout");
int ret = msgBox.exec();
2020-04-11 16:25:23 +02:00
Q_UNUSED(ret);
2020-04-10 21:08:29 +02:00
// TODO
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_actionAboutQt_triggered()
2020-04-10 21:08:29 +02:00
{
QMessageBox::aboutQt(this, tr("About Qt"));
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_actionAboutPuzzle_triggered()
2020-04-10 21:08:29 +02:00
{
2020-05-23 13:49:38 +02:00
auto *aboutDialog = new VPDialogAbout(this);
2020-04-11 11:40:02 +02:00
aboutDialog->setAttribute(Qt::WA_DeleteOnClose, true);
aboutDialog->show();
2020-04-10 21:08:29 +02:00
}
2020-04-11 16:25:23 +02:00
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_comboBoxLayoutUnit_currentIndexChanged(int index)
2020-04-11 16:25:23 +02:00
{
Q_UNUSED(index);
2020-04-19 12:47:38 +02:00
QVariant comboBoxValue = ui->comboBoxLayoutUnit->currentData();
2020-04-11 16:25:23 +02:00
2020-04-19 12:47:38 +02:00
if(comboBoxValue == QVariant(UnitsToStr(Unit::Cm)))
{
m_layout->SetUnit(Unit::Cm);
}
else if(comboBoxValue == QVariant(UnitsToStr(Unit::Mm)))
{
m_layout->SetUnit(Unit::Mm);
}
else if(comboBoxValue == QVariant(UnitsToStr(Unit::Inch)))
{
m_layout->SetUnit(Unit::Inch);
}
2020-04-11 16:25:23 +02:00
2020-04-19 12:47:38 +02:00
SetPropertyTabLayoutData();
SetPropertyTabCurrentPieceData();
2020-04-11 16:25:23 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_comboBoxLayoutTemplate_currentIndexChanged(int index)
2020-04-11 16:25:23 +02:00
{
// just for test purpuses, to be removed:
QMessageBox msgBox;
msgBox.setText("TODO PuzzleMainWindow::LayoutTemplateChanged");
int ret = msgBox.exec();
Q_UNUSED(index);
Q_UNUSED(ret);
// TODO
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_LayoutSizeChanged()
2020-04-11 16:25:23 +02:00
{
2020-04-19 12:47:38 +02:00
m_layout->SetLayoutSizeConverted(ui->doubleSpinBoxLayoutWidth->value(), ui->doubleSpinBoxLayoutLength->value());
2020-04-11 16:25:23 +02:00
2020-04-19 12:47:38 +02:00
// updates orientation - no need to block signals because the signal reacts on "clicked"
if(ui->doubleSpinBoxLayoutWidth->value() <= ui->doubleSpinBoxLayoutLength->value())
{
//portrait
ui->radioButtonLayoutPortrait->setChecked(true);
}
else
{
//landscape
ui->radioButtonLayoutLandscape->setChecked(true);
}
2020-04-11 16:32:49 +02:00
2020-04-19 12:47:38 +02:00
// TODO Undo / Redo
2020-05-05 07:44:20 +02:00
m_graphicsView->RefreshLayout();
2020-04-11 16:25:23 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_LayoutOrientationChanged()
2020-04-11 16:25:23 +02:00
{
2020-04-19 12:47:38 +02:00
// swap the width and length
qreal width_before = ui->doubleSpinBoxLayoutWidth->value();
qreal length_before = ui->doubleSpinBoxLayoutLength->value();
2020-04-11 16:25:23 +02:00
2020-04-19 12:47:38 +02:00
SetDoubleSpinBoxValue(ui->doubleSpinBoxLayoutWidth, length_before);
SetDoubleSpinBoxValue(ui->doubleSpinBoxLayoutLength, width_before);
2020-04-11 16:32:49 +02:00
2020-05-05 07:44:20 +02:00
m_layout->SetLayoutSizeConverted(ui->doubleSpinBoxLayoutWidth->value(), ui->doubleSpinBoxLayoutLength->value());
2020-04-19 12:47:38 +02:00
// TODO Undo / Redo
2020-05-05 07:44:20 +02:00
m_graphicsView->RefreshLayout();
2020-04-11 16:25:23 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_pushButtonLayoutRemoveUnusedLength_clicked()
2020-04-11 16:25:23 +02:00
{
// just for test purpuses, to be removed:
QMessageBox msgBox;
msgBox.setText("TODO PuzzleMainWindow::LayoutRemoveUnusedLength");
int ret = msgBox.exec();
Q_UNUSED(ret);
2020-04-11 16:32:49 +02:00
// TODO
2020-04-11 16:25:23 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_LayoutMarginChanged()
2020-04-11 16:25:23 +02:00
{
2020-04-19 12:47:38 +02:00
m_layout->SetLayoutMarginsConverted(
ui->doubleSpinBoxLayoutMarginLeft->value(),
ui->doubleSpinBoxLayoutMarginTop->value(),
ui->doubleSpinBoxLayoutMarginRight->value(),
ui->doubleSpinBoxLayoutMarginBottom->value()
);
2020-04-11 16:25:23 +02:00
2020-04-19 12:47:38 +02:00
// TODO Undo / Redo
2020-05-05 07:44:20 +02:00
m_graphicsView->RefreshLayout();
2020-04-11 16:25:23 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_LayoutFollowGrainlineChanged()
2020-04-11 16:25:23 +02:00
{
// just for test purpuses, to be removed:
QMessageBox msgBox;
msgBox.setText("TODO PuzzleMainWindow::LayoutFollowGrainlineChanged");
int ret = msgBox.exec();
Q_UNUSED(ret);
2020-04-11 16:32:49 +02:00
// TODO
2020-04-11 16:25:23 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_doubleSpinBoxLayoutPiecesGap_valueChanged(double value)
2020-04-11 16:25:23 +02:00
{
2020-04-19 12:47:38 +02:00
m_layout->SetPiecesGapConverted(value);
2020-04-11 16:32:49 +02:00
2020-04-19 12:47:38 +02:00
// TODO Undo / Redo
// TODO update the QGraphicView
2020-04-11 16:25:23 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_checkBoxLayoutWarningPiecesSuperposition_toggled(bool checked)
2020-04-11 16:25:23 +02:00
{
2020-04-19 12:47:38 +02:00
m_layout->SetWarningSuperpositionOfPieces(checked);
2020-04-11 16:25:23 +02:00
2020-04-19 12:47:38 +02:00
// TODO Undo / Redo
// TODO update the QGraphicView
2020-04-11 16:25:23 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_checkBoxLayoutWarningPiecesOutOfBound_toggled(bool checked)
2020-04-11 16:25:23 +02:00
{
2020-04-19 12:47:38 +02:00
m_layout->SetWarningPiecesOutOfBound(checked);
2020-04-11 16:32:49 +02:00
2020-04-19 12:47:38 +02:00
// TODO Undo / Redo
// TODO update the QGraphicView
2020-04-11 16:25:23 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_checkBoxLayoutStickyEdges_toggled(bool checked)
2020-04-11 16:25:23 +02:00
{
2020-04-19 12:47:38 +02:00
m_layout->SetStickyEdges(checked);
2020-04-11 16:32:49 +02:00
2020-04-19 12:47:38 +02:00
// TODO Undo / Redo
// TODO update the QGraphicView
2020-04-11 16:25:23 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_pushButtonLayoutExport_clicked()
2020-04-11 16:25:23 +02:00
{
// just for test purpuses, to be removed:
QMessageBox msgBox;
msgBox.setText("TODO PuzzleMainWindow::LayoutExport");
int ret = msgBox.exec();
Q_UNUSED(ret);
2020-04-11 16:32:49 +02:00
// TODO
2020-04-11 16:25:23 +02:00
}
2020-04-10 21:08:29 +02:00
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_checkBoxCurrentPieceShowSeamline_toggled(bool checked)
{
2020-05-05 17:40:36 +02:00
if(m_selectedPieces.count() == 1)
2020-05-01 19:08:48 +02:00
{
2020-05-05 17:40:36 +02:00
m_selectedPieces.first()->SetShowSeamLine(checked);
2020-05-01 19:08:48 +02:00
}
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_checkBoxCurrentPieceMirrorPiece_toggled(bool checked)
{
2020-05-05 17:40:36 +02:00
if(m_selectedPieces.count() == 1)
2020-05-01 19:08:48 +02:00
{
2020-05-05 17:40:36 +02:00
m_selectedPieces.first()->SetPieceMirrored(checked);
2020-05-01 19:08:48 +02:00
}
}
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_doubleSpinBoxCurrentPieceAngle_valueChanged(double value)
{
2020-05-09 11:13:29 +02:00
if(m_selectedPieces.count() == 1)
{
VPuzzlePiece *piece = m_selectedPieces.first();
piece->SetRotation(value);
}
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-05 17:40:36 +02:00
void PuzzleMainWindow::on_CurrentPiecePositionEdited()
{
2020-05-05 17:40:36 +02:00
if(m_selectedPieces.count() == 1)
{
VPuzzlePiece *piece = m_selectedPieces.first();
QPointF pos(UnitConvertor(ui->doubleSpinBoxCurrentPieceBoxPositionX->value(), m_layout->GetUnit(), Unit::Px),
UnitConvertor(ui->doubleSpinBoxCurrentPieceBoxPositionY->value(), m_layout->GetUnit(), Unit::Px));
piece->SetPosition(pos);
}
}
2020-04-13 12:24:26 +02:00
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_PieceCarrouselLocationChanged(Qt::DockWidgetArea area)
2020-04-13 12:24:26 +02:00
{
if(area == Qt::BottomDockWidgetArea || area == Qt::TopDockWidgetArea)
{
2020-04-26 12:09:28 +02:00
m_pieceCarrousel->SetOrientation(Qt::Horizontal);
2020-04-13 12:24:26 +02:00
}
else if (area == Qt::LeftDockWidgetArea || area == Qt::RightDockWidgetArea)
{
2020-04-26 12:09:28 +02:00
m_pieceCarrousel->SetOrientation(Qt::Vertical);
2020-04-13 12:24:26 +02:00
}
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-05 17:40:36 +02:00
void PuzzleMainWindow::on_PieceSelectionChanged()
{
2020-05-05 17:40:36 +02:00
m_selectedPieces = m_layout->GetSelectedPieces();
// update the property of the piece currently selected
SetPropertyTabCurrentPieceData();
}
2020-05-05 17:40:36 +02:00
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_PiecePositionChanged()
{
if(m_selectedPieces.count() == 1)
{
VPuzzlePiece *piece = m_selectedPieces.first();
QPointF pos = piece->GetPosition();
2020-05-10 17:35:15 +02:00
SetDoubleSpinBoxValue(ui->doubleSpinBoxCurrentPieceBoxPositionX,
UnitConvertor(pos.x(), Unit::Px, m_layout->GetUnit()));
SetDoubleSpinBoxValue(ui->doubleSpinBoxCurrentPieceBoxPositionY,
UnitConvertor(pos.y(), Unit::Px, m_layout->GetUnit()));
2020-05-05 17:40:36 +02:00
}
}
2020-05-09 14:45:36 +02:00
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::on_PieceRotationChanged()
{
if(m_selectedPieces.count() == 1)
{
VPuzzlePiece *piece = m_selectedPieces.first();
qreal angle = piece->GetRotation();
SetDoubleSpinBoxValue(ui->doubleSpinBoxCurrentPieceAngle, angle);
}
}