Refactored duplicate code.

This commit is contained in:
Leonardo Zide 2020-12-18 12:15:35 -08:00
parent dcf8ddfd95
commit baa0a70de2
9 changed files with 60 additions and 97 deletions

View file

@ -778,12 +778,36 @@ View* lcMainWindow::CreateView(lcModel* Model)
return NewView;
}
void lcMainWindow::PreviewPiece(const QString &PartType, int ColorCode)
void lcMainWindow::PreviewPiece(const QString& PartId, int ColorCode)
{
if (mPreviewWidget) {
if (!mPreviewWidget->SetCurrentPiece(PartType, ColorCode))
QMessageBox::critical(gMainWindow, tr("Error"), tr("Part preview for % failed.").arg(PartType));
lcPreferences& Preferences = lcGetPreferences();
if (!Preferences.mPreviewEnabled)
return;
if (Preferences.mPreviewPosition != lcPreviewPosition::Floating)
{
if (mPreviewWidget && mPreviewWidget->SetCurrentPiece(PartId, ColorCode))
return;
}
else
{
lcPreviewWidget* Preview = new lcPreviewWidget();
lcViewWidget* ViewWidget = new lcViewWidget(nullptr, Preview);
if (Preview && ViewWidget)
{
ViewWidget->setAttribute(Qt::WA_DeleteOnClose, true);
if (Preview->SetCurrentPiece(PartId, ColorCode))
{
ViewWidget->SetPreviewPosition(rect());
return;
}
}
}
QMessageBox::information(this, tr("Error"), tr("Part preview for '%1' failed.").arg(PartId));
}
void lcMainWindow::CreatePreviewWidget()
@ -802,14 +826,14 @@ void lcMainWindow::CreatePreviewWidget()
connect(mPreviewToolBar, SIGNAL(topLevelChanged(bool)), this, SLOT(EnableWindowFlags(bool)));
}
void lcMainWindow::TogglePreviewWidget(bool visible)
void lcMainWindow::TogglePreviewWidget(bool Visible)
{
if (mPreviewToolBar) {
if (visible)
if (Visible)
mPreviewToolBar->show();
else
mPreviewToolBar->hide();
} else if (visible) {
} else if (Visible) {
CreatePreviewWidget();
}
}

View file

@ -354,8 +354,8 @@ public:
public slots:
void ProjectFileChanged(const QString& Path);
void PreviewPiece(const QString &, int);
void TogglePreviewWidget(bool);
void PreviewPiece(const QString& PartId, int ColorCode);
void TogglePreviewWidget(bool Visible);
protected slots:
void ViewFocusReceived();

View file

@ -682,48 +682,24 @@ void lcPartSelectionListView::startDrag(Qt::DropActions SupportedActions)
Drag->exec(Qt::CopyAction);
}
void lcPartSelectionListView::mouseDoubleClickEvent(QMouseEvent *event)
void lcPartSelectionListView::mouseDoubleClickEvent(QMouseEvent* MouseEvent)
{
QAbstractItemView::mouseDoubleClickEvent(event);
if ( event->button() == Qt::LeftButton )
{
if (MouseEvent->button() == Qt::LeftButton )
PreviewSelection(currentIndex().row());
}
QListView::mouseDoubleClickEvent(MouseEvent);
}
void lcPartSelectionListView::PreviewSelection(int InfoIndex)
{
lcPreferences& Preferences = lcGetPreferences();
if (!Preferences.mPreviewEnabled)
return;
PieceInfo* Info = mListModel->GetPieceInfo(InfoIndex);
if (!Info)
return;
quint32 ColorCode = lcGetColorCode(mListModel->GetColorIndex());
if (Preferences.mPreviewPosition != lcPreviewPosition::Floating)
{
gMainWindow->PreviewPiece(Info->mFileName, ColorCode);
return;
}
lcPreviewWidget* Preview = new lcPreviewWidget();
lcViewWidget* ViewWidget = new lcViewWidget(nullptr, Preview);
if (Preview && ViewWidget)
{
ViewWidget->setAttribute(Qt::WA_DeleteOnClose, true);
if (!Preview->SetCurrentPiece(Info->mFileName, ColorCode))
QMessageBox::critical(gMainWindow, tr("Error"), tr("Preview %1 failed.").arg(Info->mFileName));
ViewWidget->SetPreviewPosition(rect());
}
else
{
QMessageBox::critical(gMainWindow, tr("Error"), tr("Preview %1 failed.").arg(Info->mFileName));
}
gMainWindow->PreviewPiece(Info->mFileName, ColorCode);
}
lcPartSelectionWidget::lcPartSelectionWidget(QWidget* Parent)

View file

@ -188,7 +188,7 @@ public slots:
protected:
void SetIconSize(int Size);
void PreviewSelection(int InfoIndex);
void mouseDoubleClickEvent(QMouseEvent *event) override;
void mouseDoubleClickEvent(QMouseEvent* MouseEvent) override;
lcPartSelectionListModel* mListModel;
lcPartSelectionWidget* mPartSelectionWidget;

View file

@ -509,44 +509,33 @@ void lcTimelineWidget::mousePressEvent(QMouseEvent* Event)
QTreeWidget::mousePressEvent(Event);
}
void lcTimelineWidget::mouseDoubleClickEvent(QMouseEvent *event)
void lcTimelineWidget::mouseDoubleClickEvent(QMouseEvent* MouseEvent)
{
QTreeWidget::mouseDoubleClickEvent(event);
if ( event->button() == Qt::LeftButton )
if (MouseEvent->button() == Qt::LeftButton)
{
lcPreferences& Preferences = lcGetPreferences();
if (Preferences.mPreviewEnabled && Preferences.mPreviewPosition == lcPreviewPosition::Floating)
{
QTreeWidgetItem* CurrentItem = currentItem();
PreviewSelection(CurrentItem);
}
QTreeWidgetItem* CurrentItem = currentItem();
PreviewSelection(CurrentItem);
}
QTreeWidget::mouseDoubleClickEvent(MouseEvent);
}
void lcTimelineWidget::PreviewSelection(QTreeWidgetItem* Current)
void lcTimelineWidget::PreviewSelection(QTreeWidgetItem* CurrentItem)
{
lcPiece* Piece = (lcPiece*)Current->data(0, Qt::UserRole).value<uintptr_t>();
if (!CurrentItem)
return;
lcPiece* Piece = (lcPiece*)CurrentItem->data(0, Qt::UserRole).value<uintptr_t>();
if (!Piece)
return;
PieceInfo* Info = Piece->mPieceInfo;
if (!Info)
return;
lcPreviewWidget* Preview = new lcPreviewWidget();
lcViewWidget* ViewWidget = new lcViewWidget(nullptr, Preview);
if (Preview && ViewWidget)
{
if (!Preview->SetCurrentPiece(Info->mFileName, Piece->mColorCode))
QMessageBox::critical(gMainWindow, tr("Error"), tr("Part preview for %1 failed.").arg(Info->mFileName));
ViewWidget->SetPreviewPosition(rect());
}
else
{
QMessageBox::critical(gMainWindow, tr("Error"), tr("Preview %1 failed.").arg(Info->mFileName));
}
gMainWindow->PreviewPiece(Info->mFileName, Piece->mColorCode);
}
void lcTimelineWidget::UpdateModel()

View file

@ -26,7 +26,7 @@ public slots:
protected:
void dropEvent(QDropEvent* DropEvent) override;
void mousePressEvent(QMouseEvent* MouseEvent) override;
void mouseDoubleClickEvent(QMouseEvent *event) override;
void mouseDoubleClickEvent(QMouseEvent* MouseEvent) override;
void UpdateModel();
void UpdateCurrentStepItem();

View file

@ -135,13 +135,14 @@ void lcViewWidget::SetPreviewPosition(const QRect& ParentRect)
pos = mapToGlobal(ParentRect.bottomLeft());
break;
}
if (pos.x() < desktop.left())
pos.setX(desktop.left());
if (pos.y() < desktop.top())
pos.setY(desktop.top());
if ((pos.x() + width()) > desktop.width())
pos.setX(desktop.width() - width());
if ((pos.x() + width()) > desktop.right())
pos.setX(desktop.right() - width());
if ((pos.y() + height()) > desktop.bottom())
pos.setY(desktop.bottom() - height());
move(pos);

View file

@ -774,7 +774,7 @@ void lcQPropertiesTree::slotSetValue(int Value)
if (Focus && Focus->IsPiece())
ColorIndex = ((lcPiece*)Focus)->mColorIndex;
quint32 ColorCode = lcGetColorCode(ColorIndex);
PreviewSelection(Info->mFileName, ColorCode);
gMainWindow->PreviewPiece(Info->mFileName, ColorCode);
}
}
}
@ -952,7 +952,7 @@ void lcQPropertiesTree::SetPiece(const lcArray<lcObject*>& Selection, lcObject*
if (Preferences.mPreviewEnabled && Preferences.mPreviewPosition != lcPreviewPosition::Floating)
{
quint32 ColorCode = lcGetColorCode(ColorIndex);
PreviewSelection(Info->mFileName, ColorCode);
gMainWindow->PreviewPiece(Info->mFileName, ColorCode);
}
}
else
@ -1138,28 +1138,3 @@ bool lcQPropertiesTree::lastColumn(int column) const
{
return header()->visualIndex(column) == columnCount() - 1;
}
void lcQPropertiesTree::PreviewSelection(const QString& PartType, int ColorCode)
{
lcPreferences& Preferences = lcGetPreferences();
if (Preferences.mPreviewPosition != lcPreviewPosition::Floating)
{
gMainWindow->PreviewPiece(PartType, ColorCode);
return;
}
lcPreviewWidget* Preview = new lcPreviewWidget();
lcViewWidget* ViewWidget = new lcViewWidget(nullptr, Preview);
if (Preview && ViewWidget)
{
if (!Preview->SetCurrentPiece(PartType, ColorCode))
QMessageBox::critical(gMainWindow, tr("Error"), tr("Part preview for %1 failed.").arg(PartType));
ViewWidget->SetPreviewPosition(rect());
}
else
{
QMessageBox::critical(gMainWindow, tr("Error"), tr("Preview %1 failed.").arg(PartType));
}
}

View file

@ -72,8 +72,6 @@ protected:
void getPartProperties(lcPartProperties *properties);
void PreviewSelection(const QString& PartType, int ColorCode);
lcPropertyWidgetMode mWidgetMode;
lcObject* mFocus;