1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#include "lc_global.h"
#include "lc_partselectionpopup.h"
#include "lc_partselectionwidget.h"
#include "pieceinf.h"
lcPartSelectionPopup::lcPartSelectionPopup(PieceInfo* InitialPart, QWidget* Parent)
: QWidget(Parent), mInitialPart(InitialPart)
{
QVBoxLayout* Layout = new QVBoxLayout(this);
mPartSelectionWidget = new lcPartSelectionWidget(this);
Layout->addWidget(mPartSelectionWidget);
mPartSelectionWidget->SetDragEnabled(false);
connect(mPartSelectionWidget, &lcPartSelectionWidget::PartPicked, this, &lcPartSelectionPopup::Accept);
QDialogButtonBox* ButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
Layout->addWidget(ButtonBox);
QObject::connect(ButtonBox, &QDialogButtonBox::accepted, this, &lcPartSelectionPopup::Accept);
QObject::connect(ButtonBox, &QDialogButtonBox::rejected, this, &lcPartSelectionPopup::Reject);
}
void lcPartSelectionPopup::showEvent(QShowEvent* ShowEvent)
{
QWidget::showEvent(ShowEvent);
mPartSelectionWidget->SetOrientation(Qt::Horizontal);
mPartSelectionWidget->SetCurrentPart(mInitialPart);
mPartSelectionWidget->FocusPartFilterWidget();
}
void lcPartSelectionPopup::Accept()
{
mPickedPiece = mPartSelectionWidget->GetCurrentPart();
mAccepted = true;
Close();
}
void lcPartSelectionPopup::Reject()
{
Close();
}
void lcPartSelectionPopup::Close()
{
QMenu* Menu = qobject_cast<QMenu*>(parent());
if (Menu)
Menu->close();
}
std::optional<PieceInfo*> lcShowPartSelectionPopup(PieceInfo* InitialPart, const std::vector<std::pair<PieceInfo*, std::string>>& CustomParts, int ColorIndex, QWidget* Parent, QPoint Position)
{
std::unique_ptr<QMenu> Menu(new QMenu(Parent));
QWidgetAction* Action = new QWidgetAction(Menu.get());
lcPartSelectionPopup* Popup = new lcPartSelectionPopup(InitialPart, Menu.get());
lcPartSelectionWidget* PartSelectionWidget = Popup->GetPartSelectionWidget();
if (CustomParts.empty())
{
PartSelectionWidget->SetCategory(lcPartCategoryType::AllParts, 0);
PartSelectionWidget->SetColorIndex(ColorIndex);
}
else
PartSelectionWidget->SetCustomParts(CustomParts, ColorIndex);
Action->setDefaultWidget(Popup);
Menu->addAction(Action);
Menu->exec(Position);
return Popup->GetPickedPart();
}
|