Fix reparsing background image items.

This commit is contained in:
Roman Telezhynskyi 2022-01-29 20:13:54 +02:00
parent 882f4ba0bf
commit 317bc432e2
2 changed files with 76 additions and 34 deletions

View File

@ -2398,9 +2398,24 @@ void MainWindow::NewBackgroundImageItem(const VBackgroundPatternImage &image)
m_backgroundImages.insert(image.Id(), item); m_backgroundImages.insert(image.Id(), item);
} }
m_deletedBackgroundImageItems.remove(image.Id()); m_deletedBackgroundImageItems.remove(image.Id());
m_deletedBackgroundImages.remove(image.Id());
} }
else else
{ {
VBackgroundImageItem *item = InitBackgroundImageItem(image);
if (item != nullptr)
{
m_backgroundImages.insert(image.Id(), item);
sceneDraw->addItem(item);
}
}
VMainGraphicsView::NewSceneRect(sceneDraw, ui->view);
}
//---------------------------------------------------------------------------------------------------------------------
auto MainWindow::InitBackgroundImageItem(const VBackgroundPatternImage &image) -> VBackgroundImageItem *
{
VBackgroundImageItem *item = nullptr; VBackgroundImageItem *item = nullptr;
if (image.Type() == PatternImage::Raster) if (image.Type() == PatternImage::Raster)
{ {
@ -2425,12 +2440,9 @@ void MainWindow::NewBackgroundImageItem(const VBackgroundPatternImage &image)
&VWidgetBackgroundImages::ImageSelected); &VWidgetBackgroundImages::ImageSelected);
connect(backgroundImagesWidget, &VWidgetBackgroundImages::SelectImage, m_backgroudcontrols, connect(backgroundImagesWidget, &VWidgetBackgroundImages::SelectImage, m_backgroudcontrols,
&VBackgroundImageControls::ActivateControls); &VBackgroundImageControls::ActivateControls);
sceneDraw->addItem(item);
m_backgroundImages.insert(image.Id(), item);
}
} }
VMainGraphicsView::NewSceneRect(sceneDraw, ui->view); return item;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -3821,6 +3833,7 @@ void MainWindow::FullParseFile()
SetEnabledGUI(true); SetEnabledGUI(true);
doc->Parse(Document::FullParse); doc->Parse(Document::FullParse);
ParseBackgroundImages();
if (VAbstractValApplication::VApp()->getOpeningPattern()) if (VAbstractValApplication::VApp()->getOpeningPattern())
{ {
@ -4358,6 +4371,7 @@ void MainWindow::DeleteBackgroundImageItem(const QUuid &id)
} }
m_backgroundImages.remove(id); m_backgroundImages.remove(id);
m_deletedBackgroundImageItems.insert(id, item); m_deletedBackgroundImageItems.insert(id, item);
m_deletedBackgroundImages.insert(id, item->Image());
if (backgroundImagesWidget != nullptr) if (backgroundImagesWidget != nullptr)
{ {
@ -4425,6 +4439,39 @@ void MainWindow::SaveBackgroundImage(const QUuid &id)
} }
} }
//---------------------------------------------------------------------------------------------------------------------
void MainWindow::ParseBackgroundImages()
{
// No memory leak. Scene should take care of these items
m_backgroudcontrols = nullptr; // force creating new controls
m_backgroundImages.clear(); // clear dangling pointers
QVector<VBackgroundPatternImage> allImages = doc->GetBackgroundImages();
for (const auto &image : allImages)
{
NewBackgroundImageItem(image);
}
backgroundImagesWidget->UpdateImages();
// Undostack rely on m_deletedBackgroundImageItems to prevent crashes
QMap<QUuid, VBackgroundImageItem *> deletedBackgroundImageItems;
QMap<QUuid, VBackgroundImageItem *>::const_iterator i;
for (i = m_deletedBackgroundImageItems.constBegin(); i != m_deletedBackgroundImageItems.constEnd(); ++i)
{
if (m_deletedBackgroundImages.contains(i.key()))
{
VBackgroundPatternImage image = m_deletedBackgroundImages.value(i.key());
VBackgroundImageItem *item = InitBackgroundImageItem(image);
if (item != nullptr)
{
deletedBackgroundImageItems.insert(image.Id(), item);
}
}
}
m_deletedBackgroundImageItems = deletedBackgroundImageItems;
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void MainWindow::InitDimensionControls() void MainWindow::InitDimensionControls()
{ {
@ -5838,16 +5885,7 @@ bool MainWindow::LoadPattern(QString fileName, const QString& customMeasureFile)
/* Collect garbage only after successfully parse. This way wrongly accused items have one more time to restore /* Collect garbage only after successfully parse. This way wrongly accused items have one more time to restore
* a reference. */ * a reference. */
QTimer::singleShot(100, Qt::CoarseTimer, this, [this](){doc->GarbageCollector(true);}); QTimer::singleShot(100, Qt::CoarseTimer, this, [this](){doc->GarbageCollector(true);});
QTimer::singleShot(500, Qt::CoarseTimer, this, &MainWindow::ParseBackgroundImages);
QTimer::singleShot(500, Qt::CoarseTimer, this, [this]()
{
QVector<VBackgroundPatternImage> allImages = doc->GetBackgroundImages();
for (const auto &image : allImages)
{
NewBackgroundImageItem(image);
}
backgroundImagesWidget->UpdateImages();
});
} }
patternReadOnly = doc->IsReadOnly(); patternReadOnly = doc->IsReadOnly();

View File

@ -241,6 +241,8 @@ private slots:
void ShowBackgroundImageInExplorer(const QUuid &id); void ShowBackgroundImageInExplorer(const QUuid &id);
void SaveBackgroundImage(const QUuid &id); void SaveBackgroundImage(const QUuid &id);
void ParseBackgroundImages();
private: private:
Q_DISABLE_COPY(MainWindow) Q_DISABLE_COPY(MainWindow)
/** @brief ui keeps information about user interface */ /** @brief ui keeps information about user interface */
@ -323,7 +325,8 @@ private:
QMap<QUuid, VBackgroundImageItem *> m_backgroundImages{}; QMap<QUuid, VBackgroundImageItem *> m_backgroundImages{};
QMap<QUuid, VBackgroundImageItem *> m_deletedBackgroundImageItems{}; QMap<QUuid, VBackgroundImageItem *> m_deletedBackgroundImageItems{};
VBackgroundImageControls *m_backgroudcontrols{nullptr}; VBackgroundImageControls* m_backgroudcontrols{nullptr};
QMap<QUuid, VBackgroundPatternImage> m_deletedBackgroundImages{};
bool m_groupsActive{false}; bool m_groupsActive{false};
bool m_toolOptionsActive{false}; bool m_toolOptionsActive{false};
@ -446,6 +449,7 @@ private:
void ExportDraw(const QString &fileName); void ExportDraw(const QString &fileName);
void NewBackgroundImageItem(const VBackgroundPatternImage &image); void NewBackgroundImageItem(const VBackgroundPatternImage &image);
auto InitBackgroundImageItem(const VBackgroundPatternImage &image) -> VBackgroundImageItem *;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H