Refactored part picker popups.
This commit is contained in:
@@ -172,178 +172,6 @@ QVariant lcPieceIdStringModel::data(const QModelIndex& Index, int Role) const
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
lcPieceIdPickerPopup::lcPieceIdPickerPopup(PieceInfo* Current, QWidget* Parent)
|
||||
: QWidget(Parent), mInitialPart(Current)
|
||||
{
|
||||
QVBoxLayout* Layout = new QVBoxLayout(this);
|
||||
|
||||
mPartSelectionWidget = new lcPartSelectionWidget(this);
|
||||
Layout->addWidget(mPartSelectionWidget);
|
||||
|
||||
mPartSelectionWidget->setMinimumWidth(450);
|
||||
|
||||
connect(mPartSelectionWidget, &lcPartSelectionWidget::PartPicked, this, &lcPieceIdPickerPopup::PartPicked);
|
||||
|
||||
QDialogButtonBox* ButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
|
||||
Layout->addWidget(ButtonBox);
|
||||
|
||||
QObject::connect(ButtonBox, &QDialogButtonBox::accepted, this, &lcPieceIdPickerPopup::Accept);
|
||||
QObject::connect(ButtonBox, &QDialogButtonBox::rejected, this, &lcPieceIdPickerPopup::Reject);
|
||||
}
|
||||
|
||||
void lcPieceIdPickerPopup::showEvent(QShowEvent* ShowEvent)
|
||||
{
|
||||
QWidget::showEvent(ShowEvent);
|
||||
|
||||
mPartSelectionWidget->SetOrientation(Qt::Horizontal);
|
||||
mPartSelectionWidget->SetCurrentPart(mInitialPart);
|
||||
|
||||
mPartSelectionWidget->FocusPartFilterWidget();
|
||||
}
|
||||
|
||||
void lcPieceIdPickerPopup::Accept()
|
||||
{
|
||||
PieceInfo* Info = mPartSelectionWidget->GetCurrentPart();
|
||||
|
||||
emit PieceIdSelected(Info);
|
||||
|
||||
Close();
|
||||
}
|
||||
|
||||
void lcPieceIdPickerPopup::Reject()
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
void lcPieceIdPickerPopup::PartPicked(PieceInfo* Info)
|
||||
{
|
||||
emit PieceIdSelected(Info);
|
||||
|
||||
Close();
|
||||
}
|
||||
|
||||
void lcPieceIdPickerPopup::Close()
|
||||
{
|
||||
QMenu* Menu = qobject_cast<QMenu*>(parent());
|
||||
|
||||
if (Menu)
|
||||
Menu->close();
|
||||
}
|
||||
|
||||
lcPieceListPickerPopup::lcPieceListPickerPopup(QWidget* Parent, PieceInfo* InitialPart, const std::vector<std::pair<PieceInfo*, std::string>>& Parts, int ColorIndex, bool Sort, bool ShowFilter)
|
||||
: QWidget(Parent), mInitialPart(InitialPart)
|
||||
{
|
||||
QVBoxLayout* Layout = new QVBoxLayout(this);
|
||||
|
||||
if (ShowFilter)
|
||||
{
|
||||
mFilterWidget = new QLineEdit(this);
|
||||
mFilterWidget->setPlaceholderText(tr("Filter Parts"));
|
||||
Layout->addWidget(mFilterWidget);
|
||||
|
||||
connect(mFilterWidget, &QLineEdit::textChanged, this, &lcPieceListPickerPopup::FilterChanged);
|
||||
}
|
||||
|
||||
mPartSelectionListView = new lcPartSelectionListView(this, nullptr);
|
||||
Layout->addWidget(mPartSelectionListView);
|
||||
|
||||
mPartSelectionListView->setMinimumWidth(450);
|
||||
mPartSelectionListView->setDragEnabled(false);
|
||||
|
||||
mPartSelectionListView->SetCustomParts(Parts, ColorIndex, Sort);
|
||||
|
||||
connect(mPartSelectionListView, &lcPartSelectionListView::PartPicked, this, &lcPieceListPickerPopup::Accept);
|
||||
|
||||
QDialogButtonBox* ButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
|
||||
Layout->addWidget(ButtonBox);
|
||||
|
||||
QObject::connect(ButtonBox, &QDialogButtonBox::accepted, this, &lcPieceListPickerPopup::Accept);
|
||||
QObject::connect(ButtonBox, &QDialogButtonBox::rejected, this, &lcPieceListPickerPopup::Reject);
|
||||
}
|
||||
|
||||
void lcPieceListPickerPopup::showEvent(QShowEvent* ShowEvent)
|
||||
{
|
||||
QWidget::showEvent(ShowEvent);
|
||||
|
||||
mPartSelectionListView->SetCurrentPart(mInitialPart);
|
||||
mPartSelectionListView->setFocus();
|
||||
}
|
||||
|
||||
void lcPieceListPickerPopup::Accept()
|
||||
{
|
||||
mPickedPiece = mPartSelectionListView->GetCurrentPart();
|
||||
mAccepted = true;
|
||||
|
||||
Close();
|
||||
}
|
||||
|
||||
void lcPieceListPickerPopup::Reject()
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
void lcPieceListPickerPopup::Close()
|
||||
{
|
||||
QMenu* Menu = qobject_cast<QMenu*>(parent());
|
||||
|
||||
if (Menu)
|
||||
Menu->close();
|
||||
}
|
||||
|
||||
void lcPieceListPickerPopup::FilterChanged(const QString& Text)
|
||||
{
|
||||
if (mFilterAction)
|
||||
{
|
||||
if (Text.isEmpty())
|
||||
{
|
||||
delete mFilterAction;
|
||||
mFilterAction = nullptr;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!Text.isEmpty())
|
||||
{
|
||||
mFilterAction = mFilterWidget->addAction(QIcon(":/stylesheet/close.svg"), QLineEdit::TrailingPosition);
|
||||
connect(mFilterAction, &QAction::triggered, this, &lcPieceListPickerPopup::FilterTriggered);
|
||||
}
|
||||
}
|
||||
|
||||
mPartSelectionListView->GetListModel()->SetFilter(Text);
|
||||
}
|
||||
|
||||
void lcPieceListPickerPopup::FilterTriggered()
|
||||
{
|
||||
mFilterWidget->clear();
|
||||
}
|
||||
|
||||
std::optional<PieceInfo*> lcShowPieceListPopup(QWidget* Parent, PieceInfo* InitialPart, const std::vector<std::pair<PieceInfo*, std::string>>& Parts, int ColorIndex, bool Sort, bool ShowFilter, QPoint Position)
|
||||
{
|
||||
std::unique_ptr<QMenu> Menu(new QMenu(Parent));
|
||||
QWidgetAction* Action = new QWidgetAction(Menu.get());
|
||||
lcPieceListPickerPopup* Popup = new lcPieceListPickerPopup(Menu.get(), InitialPart, Parts, ColorIndex, Sort, ShowFilter);
|
||||
|
||||
Action->setDefaultWidget(Popup);
|
||||
Menu->addAction(Action);
|
||||
|
||||
Menu->exec(Position);
|
||||
|
||||
return Popup->GetPickedPiece();
|
||||
}
|
||||
|
||||
std::optional<PieceInfo*> lcShowTrainTrackPopup(QWidget* Parent, const lcTrainTrackConnectionType& ConnectionType)
|
||||
{
|
||||
std::vector<PieceInfo*> TrainTrackParts = lcGetPiecesLibrary()->GetVisibleTrainTrackParts(ConnectionType);
|
||||
std::vector<std::pair<PieceInfo*, std::string>> Parts;
|
||||
|
||||
Parts.reserve(TrainTrackParts.size());
|
||||
|
||||
for (PieceInfo* Info : TrainTrackParts)
|
||||
Parts.emplace_back(Info, std::string());
|
||||
|
||||
return lcShowPieceListPopup(Parent, nullptr, Parts, gMainWindow->mColorIndex, true, false, QCursor::pos());
|
||||
}
|
||||
|
||||
lcColorDialogPopup::lcColorDialogPopup(const QColor& InitialColor, QWidget* Parent)
|
||||
: QWidget(Parent)
|
||||
{
|
||||
|
||||
@@ -169,63 +169,6 @@ protected:
|
||||
std::vector<PieceInfo*> mSortedPieces;
|
||||
};
|
||||
|
||||
class lcPieceIdPickerPopup : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
lcPieceIdPickerPopup(PieceInfo* Current, QWidget* Parent);
|
||||
|
||||
signals:
|
||||
void PieceIdSelected(PieceInfo* Info);
|
||||
|
||||
protected slots:
|
||||
void Accept();
|
||||
void Reject();
|
||||
void PartPicked(PieceInfo* Info);
|
||||
|
||||
protected:
|
||||
void showEvent(QShowEvent* ShowEvent) override;
|
||||
void Close();
|
||||
|
||||
lcPartSelectionWidget* mPartSelectionWidget = nullptr;
|
||||
PieceInfo* mInitialPart = nullptr;
|
||||
};
|
||||
|
||||
class lcPieceListPickerPopup : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
lcPieceListPickerPopup(QWidget* Parent, PieceInfo* InitialPart, const std::vector<std::pair<PieceInfo*, std::string>>& Parts, int ColorIndex, bool Sort, bool ShowFilter);
|
||||
virtual ~lcPieceListPickerPopup() = default;
|
||||
|
||||
std::optional<PieceInfo*> GetPickedPiece() const
|
||||
{
|
||||
return mAccepted ? std::optional<PieceInfo*>(mPickedPiece) : std::nullopt;
|
||||
}
|
||||
|
||||
protected slots:
|
||||
void Accept();
|
||||
void Reject();
|
||||
void FilterChanged(const QString& Text);
|
||||
void FilterTriggered();
|
||||
|
||||
protected:
|
||||
void showEvent(QShowEvent* ShowEvent) override;
|
||||
void Close();
|
||||
|
||||
lcPartSelectionListView* mPartSelectionListView = nullptr;
|
||||
QLineEdit* mFilterWidget = nullptr;
|
||||
QAction* mFilterAction = nullptr;
|
||||
PieceInfo* mInitialPart = nullptr;
|
||||
PieceInfo* mPickedPiece = nullptr;
|
||||
bool mAccepted = false;
|
||||
};
|
||||
|
||||
std::optional<PieceInfo*> lcShowPieceListPopup(QWidget* Parent, PieceInfo* InitialPart, const std::vector<std::pair<PieceInfo*, std::string>>& Parts, int ColorIndex, bool Sort, bool ShowFilter, QPoint Position);
|
||||
std::optional<PieceInfo*> lcShowTrainTrackPopup(QWidget* Parent, const lcTrainTrackConnectionType& ConnectionType);
|
||||
|
||||
class lcColorDialogPopup : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
Reference in New Issue
Block a user