1 #pragma once
2 
3 #include "lc_context.h"
4 
5 class lcPartSelectionListModel;
6 class lcPartSelectionListView;
7 class lcPartSelectionWidget;
8 
9 enum class lcPartCategoryType
10 {
11 	AllParts,
12 	PartsInUse,
13 	Submodels,
14 	Palette,
15 	Category,
16 	Count
17 };
18 
19 enum class lcPartCategoryRole
20 {
21 	Type = Qt::UserRole,
22 	Index
23 };
24 
25 struct lcPartPalette
26 {
27 	QString Name;
28 	std::vector<std::string> Parts;
29 };
30 
31 class lcPartSelectionItemDelegate : public QStyledItemDelegate
32 {
33 	Q_OBJECT
34 
35 public:
lcPartSelectionItemDelegate(QObject * Parent,lcPartSelectionListModel * ListModel)36 	lcPartSelectionItemDelegate(QObject* Parent, lcPartSelectionListModel* ListModel)
37 		: QStyledItemDelegate(Parent), mListModel(ListModel)
38 	{
39 	}
40 
41 	void paint(QPainter* Painter, const QStyleOptionViewItem& Option, const QModelIndex& Index) const override;
42 	QSize sizeHint(const QStyleOptionViewItem& Option, const QModelIndex& Index) const override;
43 
44 protected:
45 	lcPartSelectionListModel* mListModel;
46 };
47 
48 class lcPartSelectionListModel : public QAbstractListModel
49 {
50 	Q_OBJECT
51 
52 public:
53 	lcPartSelectionListModel(QObject* Parent);
54 	~lcPartSelectionListModel();
55 
56 	int rowCount(const QModelIndex& Parent = QModelIndex()) const override;
57 	QVariant data(const QModelIndex& Index, int Role = Qt::DisplayRole) const override;
58 	QVariant headerData(int Section, Qt::Orientation Orientation, int Role = Qt::DisplayRole) const override;
59 	Qt::ItemFlags flags(const QModelIndex& Index) const override;
60 
GetPieceInfo(const QModelIndex & Index)61 	PieceInfo* GetPieceInfo(const QModelIndex& Index) const
62 	{
63 		return Index.isValid() ? mParts[Index.row()].first : nullptr;
64 	}
65 
GetPieceInfo(int Row)66 	PieceInfo* GetPieceInfo(int Row) const
67 	{
68 		return mParts[Row].first;
69 	}
70 
GetShowDecoratedParts()71 	bool GetShowDecoratedParts() const
72 	{
73 		return mShowDecoratedParts;
74 	}
75 
GetShowPartAliases()76 	bool GetShowPartAliases() const
77 	{
78 		return mShowPartAliases;
79 	}
80 
GetIconSize()81 	int GetIconSize() const
82 	{
83 		return mIconSize;
84 	}
85 
GetShowPartNames()86 	bool GetShowPartNames() const
87 	{
88 		return mShowPartNames;
89 	}
90 
GetColorIndex()91 	int GetColorIndex() const
92 	{
93 		return mColorIndex;
94 	}
95 
IsColorLocked()96 	bool IsColorLocked() const
97 	{
98 		return mColorLocked;
99 	}
100 
IsListMode()101 	bool IsListMode() const
102 	{
103 		return mListMode;
104 	}
105 
106 	void Redraw();
107 	void SetColorIndex(int ColorIndex);
108 	void ToggleColorLocked();
109 	void ToggleListMode();
110 	void SetCategory(int CategoryIndex);
111 	void SetModelsCategory();
112 	void SetPaletteCategory(int SetIndex);
113 	void SetCurrentModelCategory();
114 	void SetFilter(const QString& Filter);
115 	void RequestPreview(int InfoIndex);
116 	void SetShowDecoratedParts(bool Show);
117 	void SetShowPartAliases(bool Show);
118 	void SetIconSize(int Size);
119 	void SetShowPartNames(bool Show);
120 
121 protected slots:
122 	void PartLoaded(PieceInfo* Info);
123 
124 protected:
125 	void ClearRequests();
126 	void DrawPreview(int InfoIndex);
127 
128 	lcPartSelectionListView* mListView;
129 	std::vector<std::pair<PieceInfo*, QPixmap>> mParts;
130 	std::vector<int> mRequestedPreviews;
131 	int mIconSize;
132 	bool mColorLocked;
133 	int mColorIndex;
134 	bool mShowPartNames;
135 	bool mListMode;
136 	bool mShowDecoratedParts;
137 	bool mShowPartAliases;
138 	QByteArray mFilter;
139 	std::unique_ptr<lcView> mView;
140 	std::unique_ptr<lcModel> mModel;
141 };
142 
143 class lcPartSelectionListView : public QListView
144 {
145 	Q_OBJECT
146 
147 public:
148 	lcPartSelectionListView(QWidget* Parent, lcPartSelectionWidget* PartSelectionWidget);
149 
150 	void startDrag(Qt::DropActions SupportedActions) override;
151 
152 	void SetCategory(lcPartCategoryType Type, int Index);
153 
GetCurrentPart()154 	PieceInfo* GetCurrentPart() const
155 	{
156 		return mListModel->GetPieceInfo(currentIndex());
157 	}
158 
GetListModel()159 	lcPartSelectionListModel* GetListModel() const
160 	{
161 		return mListModel;
162 	}
163 
GetPartSelectionWidget()164 	lcPartSelectionWidget* GetPartSelectionWidget() const
165 	{
166 		return mPartSelectionWidget;
167 	}
168 
GetContextInfo()169 	PieceInfo* GetContextInfo() const
170 	{
171 		return mContextInfo;
172 	}
173 
174 	void UpdateViewMode();
175 
176 public slots:
177 	void CustomContextMenuRequested(QPoint Pos);
178 	void SetNoIcons();
179 	void SetSmallIcons();
180 	void SetMediumIcons();
181 	void SetLargeIcons();
182 	void SetExtraLargeIcons();
183 	void TogglePartNames();
184 	void ToggleDecoratedParts();
185 	void TogglePartAliases();
186 	void ToggleListMode();
187 	void ToggleFixedColor();
188 
189 protected:
190 	void SetIconSize(int Size);
191 	void PreviewSelection(int InfoIndex);
192 	void mouseDoubleClickEvent(QMouseEvent* MouseEvent) override;
193 
194 	lcPartSelectionListModel* mListModel;
195 	lcPartSelectionWidget* mPartSelectionWidget;
196 	PieceInfo* mContextInfo;
197 	lcPartCategoryType mCategoryType;
198 	int mCategoryIndex;
199 };
200 
201 class lcPartSelectionWidget : public QWidget
202 {
203 	Q_OBJECT
204 
205 public:
206 	lcPartSelectionWidget(QWidget* Parent);
207 
208 	void Redraw();
209 	void SetDefaultPart();
210 	void UpdateModels();
211 	void UpdateCategories();
212 	void LoadState(QSettings& Settings);
213 	void SaveState(QSettings& Settings);
214 	void DisableIconMode();
215 
SetColorIndex(int ColorIndex)216 	void SetColorIndex(int ColorIndex)
217 	{
218 		mPartsWidget->GetListModel()->SetColorIndex(ColorIndex);
219 	}
220 
GetPartPalettes()221 	const std::vector<lcPartPalette>& GetPartPalettes() const
222 	{
223 		return mPartPalettes;
224 	}
225 
226 public slots:
227 	void AddToPalette();
228 	void RemoveFromPalette();
229 
230 protected slots:
231 	void DockLocationChanged(Qt::DockWidgetArea Area);
232 	void FilterChanged(const QString& Text);
233 	void FilterTriggered();
234 	void CategoryChanged(QTreeWidgetItem* Current, QTreeWidgetItem* Previous);
235 	void PartChanged(const QModelIndex& Current, const QModelIndex& Previous);
236 	void OptionsMenuAboutToShow();
237 	void EditPartPalettes();
238 
239 protected:
240 	void LoadPartPalettes();
241 	void SavePartPalettes();
242 
243 	void resizeEvent(QResizeEvent* Event) override;
244 	bool event(QEvent* Event) override;
245 
246 	QTreeWidget* mCategoriesWidget;
247 	QLineEdit* mFilterWidget;
248 	QAction* mFilterAction;
249 	lcPartSelectionListView* mPartsWidget;
250 	QSplitter* mSplitter;
251 	std::vector<lcPartPalette> mPartPalettes;
252 };
253