| 
									
										
										
										
											2025-03-29 18:05:12 +08:00
										 |  |  | #ifndef CONFIG_MGR_H
 | 
					
						
							|  |  |  | #define CONFIG_MGR_H
 | 
					
						
							|  |  |  | #include <memory>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							|  |  |  | #include "cardbase.h"
 | 
					
						
							| 
									
										
										
										
											2025-04-19 16:25:33 +08:00
										 |  |  | #include <QDrag>
 | 
					
						
							|  |  |  | #include <QMimeData>
 | 
					
						
							|  |  |  | #include <QDragEnterEvent>
 | 
					
						
							|  |  |  | #include <QDebug>
 | 
					
						
							|  |  |  | #include <QListWidget>
 | 
					
						
							| 
									
										
										
										
											2025-04-25 14:28:27 +08:00
										 |  |  | #include <QStandardItemModel>   //数据模型类
 | 
					
						
							|  |  |  | #include <QTreeView>
 | 
					
						
							| 
									
										
										
										
											2025-03-29 18:05:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-19 16:25:33 +08:00
										 |  |  | class DraggableListWidget : public QListWidget { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     DraggableListWidget(QWidget *parent = nullptr) : QListWidget(parent) { | 
					
						
							|  |  |  |         setDragEnabled(true); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  |     void startDrag(Qt::DropActions supportedActions) override { | 
					
						
							|  |  |  |         QListWidgetItem *item = currentItem(); | 
					
						
							|  |  |  |         if (!item) return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         QMimeData *mimeData = new QMimeData; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-23 17:13:05 +08:00
										 |  |  |         // 获取显示文本和 UserRole 数据
 | 
					
						
							|  |  |  |         QString visibleText = item->text(); | 
					
						
							|  |  |  |         QString userData = item->data(Qt::UserRole).toString(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // 使用 QDataStream 打包数据
 | 
					
						
							|  |  |  |         QByteArray data; | 
					
						
							|  |  |  |         QDataStream stream(&data, QIODevice::WriteOnly); | 
					
						
							|  |  |  |         stream << visibleText << userData;  // 包含显示文本和 UserRole 数据
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         mimeData->setData("application/x-custom", data);  // 设置自定义 MIME 数据
 | 
					
						
							| 
									
										
										
										
											2025-04-19 16:25:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         QDrag *drag = new QDrag(this); | 
					
						
							|  |  |  |         drag->setMimeData(mimeData); | 
					
						
							|  |  |  |         drag->exec(Qt::CopyAction); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2025-04-25 14:28:27 +08:00
										 |  |  | class DropTreeModel : public QStandardItemModel { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     using QStandardItemModel::QStandardItemModel; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QStringList mimeTypes() const override { | 
					
						
							|  |  |  |         // 支持自定义类型和 QListWidget 默认类型
 | 
					
						
							|  |  |  |         return { "application/x-custom", "application/x-qabstractitemmodeldatalist" }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool dropMimeData(const QMimeData *data, Qt::DropAction action, | 
					
						
							|  |  |  |                           int row, int column, const QModelIndex &parent) override { | 
					
						
							|  |  |  |             QStandardItem *parentItem = this->itemFromIndex(parent); | 
					
						
							|  |  |  |             if (!parentItem) | 
					
						
							|  |  |  |                 parentItem = this->invisibleRootItem(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (data->hasFormat("application/x-custom")) { | 
					
						
							|  |  |  |                 QByteArray rawData = data->data("application/x-custom"); | 
					
						
							|  |  |  |                 QString customText = QString::fromUtf8(rawData); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 QStandardItem *newItem = new QStandardItem(customText); | 
					
						
							|  |  |  |                 newItem->setData(customText, Qt::UserRole);  // 假设 data 也就是内容
 | 
					
						
							|  |  |  |                 newItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | | 
					
						
							|  |  |  |                                   Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (row < 0) | 
					
						
							|  |  |  |                     parentItem->appendRow(newItem); | 
					
						
							|  |  |  |                 else | 
					
						
							|  |  |  |                     parentItem->insertRow(row, newItem); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (data->hasFormat("application/x-qabstractitemmodeldatalist")) { | 
					
						
							|  |  |  |                 QByteArray encoded = data->data("application/x-qabstractitemmodeldatalist"); | 
					
						
							|  |  |  |                 QDataStream stream(&encoded, QIODevice::ReadOnly); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 while (!stream.atEnd()) { | 
					
						
							|  |  |  |                     int r, c; | 
					
						
							|  |  |  |                     QMap<int, QVariant> roleDataMap; | 
					
						
							|  |  |  |                     stream >> r >> c >> roleDataMap; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     QString text = roleDataMap.value(Qt::DisplayRole).toString(); | 
					
						
							|  |  |  |                     QVariant userData = roleDataMap.value(Qt::UserRole); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     QStandardItem *newItem = new QStandardItem(text); | 
					
						
							|  |  |  |                     newItem->setData(userData, Qt::UserRole);  // 保留附加数据
 | 
					
						
							|  |  |  |                     newItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | | 
					
						
							|  |  |  |                                       Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if (row < 0) | 
					
						
							|  |  |  |                         parentItem->appendRow(newItem); | 
					
						
							|  |  |  |                     else | 
					
						
							|  |  |  |                         parentItem->insertRow(row, newItem); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Qt::DropActions supportedDropActions() const override { | 
					
						
							|  |  |  |             return Qt::CopyAction | Qt::MoveAction; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2025-03-29 18:05:12 +08:00
										 |  |  | class ConfigMgr { | 
					
						
							|  |  |  |   private: | 
					
						
							|  |  |  |     static ConfigMgr *instance; | 
					
						
							|  |  |  |     ConfigMgr() { | 
					
						
							|  |  |  |         for (int i = 0; i < SLOT_NUM; ++i) { | 
					
						
							|  |  |  |             card_type_[i] = kCardNone; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   public: | 
					
						
							| 
									
										
										
										
											2025-04-10 16:45:20 +08:00
										 |  |  |     int card_type_[15]; | 
					
						
							| 
									
										
										
										
											2025-03-29 18:05:12 +08:00
										 |  |  |     static ConfigMgr *Instance() { | 
					
						
							|  |  |  |         if (instance == nullptr) { | 
					
						
							|  |  |  |             instance = new ConfigMgr(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return instance; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     ~ConfigMgr(); | 
					
						
							| 
									
										
										
										
											2025-04-17 14:06:21 +08:00
										 |  |  |     void Save(QString & file_path); | 
					
						
							| 
									
										
										
										
											2025-03-29 18:05:12 +08:00
										 |  |  |     void Load(QString filename); | 
					
						
							| 
									
										
										
										
											2025-04-21 20:30:12 +08:00
										 |  |  |     std::shared_ptr<CardBase> GetSlotPtr(int slot);//1-15
 | 
					
						
							| 
									
										
										
										
											2025-04-01 15:03:59 +08:00
										 |  |  |     void AddCard(std::shared_ptr<CardBase> ptr); | 
					
						
							| 
									
										
										
										
											2025-04-21 20:30:12 +08:00
										 |  |  |     void RemoveCard(std::shared_ptr<CardBase> ptr); | 
					
						
							| 
									
										
										
										
											2025-03-29 18:05:12 +08:00
										 |  |  |   private: | 
					
						
							| 
									
										
										
										
											2025-04-01 15:03:59 +08:00
										 |  |  |     QString filename_; | 
					
						
							| 
									
										
										
										
											2025-04-10 16:45:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-29 18:05:12 +08:00
										 |  |  |     std::vector<std::shared_ptr<CardBase>> cards_; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif // CONFIG_MGR_H
 |