// genericPluginAdapter.cpp #include "genericPluginAdapter.h" #include "pluginCommon/ICanvasItem.h" #include #include #include GenericPluginAdapter::GenericPluginAdapter(ICanvasItem *pluginItem, QObject *parent) : IPluginAdapter(parent) , m_pluginItem(pluginItem) { if (!m_pluginItem) { qWarning() << "GenericPluginAdapter: pluginItem is null!"; return; } connectSignals(); updateCache(); qDebug() << "GenericPluginAdapter created for plugin:" << m_pluginItem->typeId(); } GenericPluginAdapter::~GenericPluginAdapter() { if (m_pluginItem) { m_pluginItem->deleteLater(); } } QString GenericPluginAdapter::pluginType() const { return m_pluginItem ? m_pluginItem->typeId() : QString(); } QRectF GenericPluginAdapter::bounds() const { return m_cachedBounds; } void GenericPluginAdapter::setBounds(const QRectF &bounds) { if (m_pluginItem && bounds.isValid()) { m_pluginItem->setBounds(bounds); } } void GenericPluginAdapter::paint(QPainter *painter, const QRectF &bounds) { if (m_pluginItem && painter) { m_pluginItem->draw(painter, bounds); } } QPainterPath GenericPluginAdapter::shape() const { // 返回简单的矩形路径 QPainterPath path; path.addRect(m_cachedBounds); return path; } QVariant GenericPluginAdapter::property(const QString &key) const { return m_pluginItem ? m_pluginItem->property(key) : QVariant(); } void GenericPluginAdapter::setProperty(const QString &key, const QVariant &value) { if (m_pluginItem) { m_pluginItem->setProperty(key, value); } } void GenericPluginAdapter::move(const QPointF &delta) { if (m_pluginItem && !delta.isNull()) { QRectF bounds = m_pluginItem->bounds(); bounds.translate(delta); m_pluginItem->setBounds(bounds); } } void GenericPluginAdapter::resize(const QRectF &newBounds) { setBounds(newBounds); } bool GenericPluginAdapter::isSelected() const { return m_selected; } void GenericPluginAdapter::setSelected(bool selected) { if (m_selected != selected) { m_selected = selected; emit selectionChanged(selected); } } QVariantMap GenericPluginAdapter::saveState() const { QVariantMap state; if (!m_pluginItem) { return state; } // 保存基本信息 state["type"] = m_pluginItem->typeId(); state["selected"] = m_selected; // 保存几何信息 QRectF bounds = m_pluginItem->bounds(); state["x"] = bounds.x(); state["y"] = bounds.y(); state["width"] = bounds.width(); state["height"] = bounds.height(); // 保存属性 QVariantMap properties; // 这里可以保存插件的关键属性 // 例如:properties["fillColor"] = m_pluginItem->property("fillColor"); state["properties"] = properties; return state; } bool GenericPluginAdapter::loadState(const QVariantMap &state) { if (!m_pluginItem) { return false; } // 加载几何信息 if (state.contains("x") && state.contains("y") && state.contains("width") && state.contains("height")) { QRectF bounds( state["x"].toReal(), state["y"].toReal(), state["width"].toReal(), state["height"].toReal() ); m_pluginItem->setBounds(bounds); } // 加载选择状态 if (state.contains("selected")) { setSelected(state["selected"].toBool()); } // 加载属性 if (state.contains("properties")) { QVariantMap properties = state["properties"].toMap(); for (auto it = properties.begin(); it != properties.end(); ++it) { m_pluginItem->setProperty(it.key(), it.value()); } } return true; } ICanvasItem* GenericPluginAdapter::pluginItem() const { return m_pluginItem; } void GenericPluginAdapter::updateCache() { if (m_pluginItem) { m_cachedBounds = m_pluginItem->bounds(); } } void GenericPluginAdapter::connectSignals() { if (!m_pluginItem) { return; } connect(m_pluginItem, &ICanvasItem::boundsChanged, this, &GenericPluginAdapter::onPluginBoundsChanged); connect(m_pluginItem, &ICanvasItem::propertyChanged, this, &GenericPluginAdapter::onPluginPropertyChanged); } void GenericPluginAdapter::onPluginBoundsChanged(const QRectF &newBounds) { m_cachedBounds = newBounds; emit boundsChanged(newBounds); emit adapterChanged(); } void GenericPluginAdapter::onPluginPropertyChanged(const QString &key, const QVariant &value) { emit propertyChanged(key, value); emit adapterChanged(); }