// customTypePlugin.cpp #include "customTypePlugin/include/customTypePlugin.h" #include "customTypePlugin/include/reactorItem.h" #include #include #include #include #include CustomTypePlugin::CustomTypePlugin(QObject *parent) : IPlugin(parent) { qDebug() << "CustomTypePlugin constructor"; } CustomTypePlugin::~CustomTypePlugin() { qDebug() << "CustomTypePlugin destructor"; } PluginDescriptor CustomTypePlugin::descriptor() const { PluginDescriptor desc; desc.id = "com.diagramDesigner.plugin.customType"; desc.name = "自定义类型"; desc.version = "1.0.0"; desc.author = "by"; desc.description = "自定义类型插件"; return desc; } QList CustomTypePlugin::shapes() const { ShapeDescriptor reactor; reactor.id = "reactor"; reactor.name = "电抗器"; reactor.category = 18; //电抗器临时定为18 reactor.iconPath = ":/rectangle/rectangle.png"; reactor.defaults = { {"width", 100.0}, {"height", 60.0}, {"fillColor", QColor(100, 150, 255, 180)}, {"borderColor", QColor(0, 0, 0, 200)}, {"borderWidth", 2.0}, {"cornerRadius", 0.0} }; return {reactor}; } ICanvasItem* CustomTypePlugin::createShape(const QString &shapeId) { if (shapeId == "reactor") { ReactorItem *item = new ReactorItem; // 应用默认属性 ShapeDescriptor desc = shapes().first(); // 电抗器描述 for (auto it = desc.defaults.begin(); it != desc.defaults.end(); ++it) { item->setProperty(it.key(), it.value()); } return item; } qWarning() << "Unknown shape ID:" << shapeId; return nullptr; } bool CustomTypePlugin::initialize() { qDebug() << "Rectangle plugin initializing..."; // 每个实例加载自己的图标 m_reactorIcon = loadIcon(":/rectangle/rectangle.png"); // 如果资源中没有,创建默认图标 if (m_reactorIcon.isNull()) { m_reactorIcon = createDefaultIcon(); } qDebug() << "Rectangle plugin initialized successfully"; return true; } void CustomTypePlugin::shutdown() { qDebug() << "Rectangle plugin shutting down..."; // 清理资源 m_reactorIcon = QIcon(); qDebug() << "Rectangle plugin shutdown complete"; } QIcon CustomTypePlugin::shapeIcon(const QString &shapeId) const { QString iconPath; if (shapeId == "reactor") { iconPath = ":/custom/reactor.png"; } else { return QIcon(); } // 尝试加载图标 QIcon icon = loadIcon(iconPath); if (!icon.isNull()) { return icon; } return createDefaultIcon(); } QIcon CustomTypePlugin::loadIcon(const QString &path) const { if (QFile::exists(path)) { return QIcon(path); } return QIcon(); } QIcon CustomTypePlugin::createDefaultIcon() const { QPixmap pixmap(32, 32); pixmap.fill(Qt::transparent); QPainter painter(&pixmap); painter.setRenderHint(QPainter::Antialiasing, true); // 绘制背景 painter.setBrush(QBrush(QColor(240, 240, 240))); painter.setPen(Qt::NoPen); painter.drawRoundedRect(1, 1, 30, 30, 4, 4); // 绘制矩形 QRectF rect(6, 6, 20, 20); painter.setBrush(QBrush(QColor(100, 150, 255, 180))); painter.setPen(QPen(QColor(0, 0, 0, 200), 1.5)); painter.drawRect(rect); return QIcon(pixmap); }