Skip to content

Commit 2e50703

Browse files
committed
Refactored part picker popups.
1 parent 548b221 commit 2e50703

11 files changed

+185
-299
lines changed

common/lc_minifigdialog.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "camera.h"
1212
#include "lc_doublespinbox.h"
1313
#include "lc_qutils.h"
14+
#include "lc_partselectionpopup.h"
1415

1516
lcMinifigDialog::lcMinifigDialog(QWidget* Parent)
1617
: QDialog(Parent), ui(new Ui::lcMinifigDialog)
@@ -309,7 +310,9 @@ void lcMinifigDialog::PieceButtonClicked()
309310
if (Setting.Info)
310311
Parts.emplace_back(Setting.Info, Setting.Description);
311312

312-
std::optional<PieceInfo*> Result = lcShowPieceListPopup(PieceButton, CurrentInfo, Parts, mMinifigWizard->mMinifig.Colors[ItemIndex], false, true, Position);
313+
int ColorIndex = mMinifigWizard->mMinifig.Colors[ItemIndex];
314+
315+
std::optional<PieceInfo*> Result = lcShowPartSelectionPopup(CurrentInfo, Parts, ColorIndex, PieceButton, PieceButton->mapToGlobal(PieceButton->rect().bottomLeft()));
313316

314317
if (!Result.has_value())
315318
return;

common/lc_partselectionpopup.cpp

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

common/lc_partselectionpopup.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#pragma once
2+
3+
class PieceInfo;
4+
class lcPartSelectionWidget;
5+
6+
class lcPartSelectionPopup : public QWidget
7+
{
8+
Q_OBJECT
9+
10+
public:
11+
lcPartSelectionPopup(PieceInfo* InitialPart, QWidget* Parent);
12+
virtual ~lcPartSelectionPopup() = default;
13+
14+
std::optional<PieceInfo*> GetPickedPart() const
15+
{
16+
return mAccepted ? std::optional<PieceInfo*>(mPickedPiece) : std::nullopt;
17+
}
18+
19+
lcPartSelectionWidget* GetPartSelectionWidget() const
20+
{
21+
return mPartSelectionWidget;
22+
}
23+
24+
protected slots:
25+
void Accept();
26+
void Reject();
27+
28+
protected:
29+
void showEvent(QShowEvent* ShowEvent) override;
30+
void Close();
31+
32+
lcPartSelectionWidget* mPartSelectionWidget = nullptr;
33+
PieceInfo* mInitialPart = nullptr;
34+
PieceInfo* mPickedPiece = nullptr;
35+
bool mAccepted = false;
36+
};
37+
38+
std::optional<PieceInfo*> lcShowPartSelectionPopup(PieceInfo* InitialPart, const std::vector<std::pair<PieceInfo*, std::string>>& CustomParts, int ColorIndex, QWidget* Parent, QPoint Position);

common/lc_partselectionwidget.cpp

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ void lcPartSelectionListModel::SetCurrentModelCategory()
234234
SetFilter(mFilter);
235235
}
236236

237-
void lcPartSelectionListModel::SetCustomParts(const std::vector<std::pair<PieceInfo*, std::string>>& Parts, int ColorIndex, bool Sort)
237+
void lcPartSelectionListModel::SetCustomParts(const std::vector<std::pair<PieceInfo*, std::string>>& Parts, int ColorIndex)
238238
{
239239
beginResetModel();
240240

@@ -255,22 +255,6 @@ void lcPartSelectionListModel::SetCustomParts(const std::vector<std::pair<PieceI
255255
Entry.ColorIndex = mColorIndex;
256256
}
257257

258-
if (Sort)
259-
{
260-
auto lcPartSortFunc = [](const lcPartSelectionListModelEntry& a, const lcPartSelectionListModelEntry& b)
261-
{
262-
if (!a.Info)
263-
return true;
264-
265-
if (!b.Info)
266-
return false;
267-
268-
return strcmp(a.Info->m_strDescription, b.Info->m_strDescription) < 0;
269-
};
270-
271-
std::sort(mParts.begin(), mParts.end(), lcPartSortFunc);
272-
}
273-
274258
endResetModel();
275259

276260
SetFilter(mFilter);
@@ -694,11 +678,11 @@ void lcPartSelectionListView::SetCategory(lcPartCategoryType Type, int Index)
694678
}
695679
}
696680

697-
void lcPartSelectionListView::SetCustomParts(const std::vector<std::pair<PieceInfo*, std::string>>& Parts, int ColorIndex, bool Sort)
681+
void lcPartSelectionListView::SetCustomParts(const std::vector<std::pair<PieceInfo*, std::string>>& Parts, int ColorIndex)
698682
{
699683
mCategoryType = lcPartCategoryType::Custom;
700684

701-
mListModel->SetCustomParts(Parts, ColorIndex, Sort);
685+
mListModel->SetCustomParts(Parts, ColorIndex);
702686

703687
setCurrentIndex(mListModel->index(0, 0));
704688
}
@@ -871,28 +855,11 @@ QSize lcPartSelectionListView::sizeHint() const
871855
if (mListModel->GetIconSize() == 0)
872856
return QSize(500, 350);
873857

874-
QRect CellRect1 = visualRect(model()->index(0, 0));
875-
QRect RightRect(CellRect1.topRight(), CellRect1.size());
876-
QRect BottomRect(CellRect1.bottomLeft(), CellRect1.size());
877-
878-
for (int Row = 1; Row < model()->rowCount(); Row++)
879-
{
880-
QRect Rect = visualRect(model()->index(Row, 0));
881-
882-
if (Row == 1)
883-
RightRect = Rect;
884-
885-
if (Rect.top() != CellRect1.top())
886-
{
887-
BottomRect = Rect;
888-
break;
889-
}
890-
}
891-
892858
int Columns = 5;
893859
int Rows = qMin(4, (model()->rowCount() + Columns - 1) / Columns);
894860

895-
QSize CellSize(RightRect.left() - CellRect1.left(), BottomRect.top() - CellRect1.top());
861+
QStyleOptionViewItem option = viewOptions();
862+
QSize CellSize = itemDelegate()->sizeHint(option, model()->index(0,0));
896863
QSize Size(CellSize.width() * Columns + frameWidth() * 2, CellSize.height() * Rows + frameWidth() * 2);
897864

898865
if (verticalScrollBar())
@@ -1042,11 +1009,22 @@ void lcPartSelectionWidget::DisableIconMode()
10421009

10431010
void lcPartSelectionWidget::SetCurrentPart(PieceInfo* Info)
10441011
{
1045-
mCategoriesWidget->setCurrentItem(mAllPartsCategoryItem);
10461012
mPartsWidget->SetCurrentPart(Info);
10471013
mPartsWidget->setFocus();
10481014
}
10491015

1016+
void lcPartSelectionWidget::SetCategory(lcPartCategoryType Type, int Index)
1017+
{
1018+
mPartsWidget->SetCategory(Type, Index);
1019+
}
1020+
1021+
void lcPartSelectionWidget::SetCustomParts(const std::vector<std::pair<PieceInfo*, std::string>>& Parts, int ColorIndex)
1022+
{
1023+
mSplitter->widget(0)->setVisible(false);
1024+
1025+
mPartsWidget->SetCustomParts(Parts, ColorIndex);
1026+
}
1027+
10501028
void lcPartSelectionWidget::SetOrientation(Qt::Orientation Orientation)
10511029
{
10521030
mSplitter->setOrientation(Orientation);
@@ -1175,8 +1153,7 @@ void lcPartSelectionWidget::PartViewSelectionChanged(const QModelIndex& Current,
11751153

11761154
void lcPartSelectionWidget::PartViewPartPicked(PieceInfo* Info)
11771155
{
1178-
if (Info)
1179-
emit PartPicked(Info);
1156+
emit PartPicked(Info);
11801157
}
11811158

11821159
void lcPartSelectionWidget::OptionsMenuAboutToShow()

common/lc_partselectionwidget.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class lcPartSelectionListModel : public QAbstractListModel
151151
void SetModelsCategory();
152152
void SetPaletteCategory(int SetIndex);
153153
void SetCurrentModelCategory();
154-
void SetCustomParts(const std::vector<std::pair<PieceInfo*, std::string>>& Parts, int ColorIndex, bool Sort);
154+
void SetCustomParts(const std::vector<std::pair<PieceInfo*, std::string>>& Parts, int ColorIndex);
155155
void SetFilter(const QString& Filter);
156156
void RequestThumbnail(int PartIndex);
157157
void SetShowDecoratedParts(bool Show);
@@ -196,7 +196,7 @@ class lcPartSelectionListView : public QListView
196196
QSize sizeHint() const override;
197197

198198
void SetCategory(lcPartCategoryType Type, int Index);
199-
void SetCustomParts(const std::vector<std::pair<PieceInfo*, std::string>>& Parts, int ColorIndex, bool Sort);
199+
void SetCustomParts(const std::vector<std::pair<PieceInfo*, std::string>>& Parts, int ColorIndex);
200200
void SetCurrentPart(PieceInfo* Info);
201201

202202
PieceInfo* GetCurrentPart() const
@@ -277,6 +277,8 @@ class lcPartSelectionWidget : public QWidget
277277
void DisableIconMode();
278278
void SetOrientation(Qt::Orientation Orientation);
279279
void SetCurrentPart(PieceInfo* Info);
280+
void SetCategory(lcPartCategoryType Type, int Index);
281+
void SetCustomParts(const std::vector<std::pair<PieceInfo*, std::string>>& Parts, int ColorIndex);
280282

281283
int GetColorIndex() const
282284
{
@@ -298,6 +300,11 @@ class lcPartSelectionWidget : public QWidget
298300
return mPartsWidget->GetCurrentPart();
299301
}
300302

303+
void SetDragEnabled(bool Enabled)
304+
{
305+
mPartsWidget->setDragEnabled(Enabled);
306+
}
307+
301308
void FocusPartFilterWidget() const
302309
{
303310
mFilterWidget->setFocus();

common/lc_propertieswidget.cpp

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "lc_collapsiblewidget.h"
1212
#include "lc_colorpicker.h"
1313
#include "lc_qutils.h"
14+
#include "lc_partselectionpopup.h"
1415

1516
lcPropertiesWidget::lcPropertiesWidget(QWidget* Parent)
1617
: QWidget(Parent)
@@ -1088,35 +1089,24 @@ void lcPropertiesWidget::PieceIdButtonClicked()
10881089

10891090
std::tie(Value, Partial) = GetUpdateValue(PropertyId);
10901091

1091-
PieceInfo* Info = static_cast<PieceInfo*>(Value.value<void*>());
1092-
1093-
QMenu* Menu = new QMenu(PieceIdButton);
1094-
1095-
QWidgetAction* Action = new QWidgetAction(Menu);
1096-
lcPieceIdPickerPopup* Popup = new lcPieceIdPickerPopup(Partial ? nullptr : Info, Menu);
1097-
Action->setDefaultWidget(Popup);
1098-
Menu->addAction(Action);
1099-
1100-
connect(Popup, &lcPieceIdPickerPopup::PieceIdSelected, this, &lcPropertiesWidget::PieceIdChanged);
1101-
1102-
Menu->exec(PieceIdButton->mapToGlobal(PieceIdButton->rect().bottomLeft()));
1092+
PieceInfo* Info = Partial ? nullptr : static_cast<PieceInfo*>(Value.value<void*>());
11031093

1104-
delete Menu;
1105-
}
1094+
std::tie(Value, Partial) = GetUpdateValue(lcObjectPropertyId::PieceColor);
1095+
1096+
int ColorIndex = Partial ? gDefaultColor : Value.toInt();
11061097

1107-
void lcPropertiesWidget::PieceIdChanged(PieceInfo* Info)
1108-
{
1109-
lcPieceIdPickerPopup* Popup = qobject_cast<lcPieceIdPickerPopup*>(sender());
1110-
QMenu* Menu = qobject_cast<QMenu*>(Popup->parent());
1111-
QToolButton* PieceIdButton = qobject_cast<QToolButton*>(Menu->parent());
1112-
lcObjectPropertyId PropertyId = GetEditorWidgetPropertyId(PieceIdButton);
1098+
std::optional<PieceInfo*> Result = lcShowPartSelectionPopup(Info, std::vector<std::pair<PieceInfo*, std::string>>(), ColorIndex, PieceIdButton, PieceIdButton->mapToGlobal(PieceIdButton->rect().bottomLeft()));
11131099

1114-
lcModel* Model = gMainWindow->GetActiveModel();
1100+
if (Result.has_value())
1101+
{
1102+
lcModel* Model = gMainWindow->GetActiveModel();
1103+
Info = Result.value();
11151104

1116-
if (!Model || !Info)
1117-
return;
1105+
if (!Model || !Info)
1106+
return;
11181107

1119-
Model->SetObjectsProperty(mFocusObject ? std::vector<lcObject*>{ mFocusObject } : mSelection, PropertyId, QVariant::fromValue<void*>(Info));
1108+
Model->SetObjectsProperty(mFocusObject ? std::vector<lcObject*>{ mFocusObject } : mSelection, PropertyId, QVariant::fromValue<void*>(Info));
1109+
}
11201110
}
11211111

11221112
void lcPropertiesWidget::AddPieceIdProperty(lcObjectPropertyId PropertyId, const QString& Text, const QString& ToolTip, bool SupportsKeyFrames)

common/lc_propertieswidget.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ protected slots:
3030
void PieceColorButtonClicked();
3131
void PieceColorChanged(int ColorIndex);
3232
void PieceIdButtonClicked();
33-
void PieceIdChanged(PieceInfo* Info);
3433

3534
protected:
3635
enum class CategoryIndex

common/lc_view.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "lc_findreplacewidget.h"
1919
#include "lc_library.h"
2020
#include "lc_qutils.h"
21+
#include "lc_partselectionpopup.h"
2122

2223
lcFindReplaceParams lcView::mFindReplaceParams;
2324
QPointer<lcFindReplaceWidget> lcView::mFindWidget;
@@ -330,8 +331,29 @@ void lcView::ShowTrainTrackPopup()
330331

331332
int ConnectionIndex = mTrackToolSection - LC_PIECE_SECTION_TRAIN_TRACK_CONNECTION_FIRST;
332333
const lcTrainTrackConnectionType& ConnectionType = TrainTrackInfo->GetConnections()[ConnectionIndex].Type;
334+
std::vector<PieceInfo*> TrainTrackParts = lcGetPiecesLibrary()->GetVisibleTrainTrackParts(ConnectionType);
333335

334-
std::optional<PieceInfo*> Result = lcShowTrainTrackPopup(mWidget, ConnectionType);
336+
auto PartSortFunc = [](const PieceInfo* a, const PieceInfo* b)
337+
{
338+
if (!a)
339+
return true;
340+
341+
if (!b)
342+
return false;
343+
344+
return strcmp(a->m_strDescription, b->m_strDescription) < 0;
345+
};
346+
347+
std::sort(TrainTrackParts.begin(), TrainTrackParts.end(), PartSortFunc);
348+
349+
std::vector<std::pair<PieceInfo*, std::string>> Parts;
350+
351+
Parts.reserve(TrainTrackParts.size());
352+
353+
for (PieceInfo* Info : TrainTrackParts)
354+
Parts.emplace_back(Info, std::string());
355+
356+
std::optional<PieceInfo*> Result = lcShowPartSelectionPopup(nullptr, Parts, gMainWindow->mColorIndex, mWidget, QCursor::pos());
335357
PieceInfo* Info = Result.has_value() ? Result.value() : nullptr;
336358

337359
if (Info)

0 commit comments

Comments
 (0)