2025-10-17 18:14:44 +08:00
|
|
|
|
#include "projectIconSetting.h"
|
|
|
|
|
|
#include "ui_projectIconSetting.h"
|
|
|
|
|
|
#include "graphicsDataModel/fixedPortsModel.h"
|
|
|
|
|
|
#include "projectIconSelectionDlg.h"
|
|
|
|
|
|
#include "graphicsItem/graphicsBaseItem.h"
|
|
|
|
|
|
#include "baseProperty.h"
|
|
|
|
|
|
#include "projectModelManager.h"
|
|
|
|
|
|
#include <QSvgRenderer>
|
|
|
|
|
|
#include <QPainter>
|
2025-11-05 18:14:31 +08:00
|
|
|
|
#include "global.h"
|
2025-10-17 18:14:44 +08:00
|
|
|
|
|
|
|
|
|
|
ProjectIconSetting::ProjectIconSetting(QWidget *parent)
|
|
|
|
|
|
: QDialog(parent)
|
|
|
|
|
|
, ui(new Ui::projectIconSetting)
|
|
|
|
|
|
,_controller(nullptr)
|
|
|
|
|
|
{
|
|
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | windowFlags());
|
|
|
|
|
|
initial();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ProjectIconSetting::~ProjectIconSetting()
|
|
|
|
|
|
{
|
|
|
|
|
|
delete ui;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ProjectIconSetting::showDlg(GraphicsProjectModelItem* pItem)
|
|
|
|
|
|
{
|
|
|
|
|
|
ui->tableWidget->clear();
|
|
|
|
|
|
ui->tableWidget->setRowCount(0);
|
|
|
|
|
|
show();
|
|
|
|
|
|
ModelProperty* p = pItem->getProperty();
|
|
|
|
|
|
BaseProperty* pro = dynamic_cast<BaseProperty*>(p);
|
|
|
|
|
|
if(pro)
|
|
|
|
|
|
{
|
|
|
|
|
|
QString sName = pro->name();
|
|
|
|
|
|
QString sMetaModel = pro->metaModelName();
|
|
|
|
|
|
QString sModel = pro->modelName();
|
|
|
|
|
|
_sMetaModel = sMetaModel;
|
|
|
|
|
|
_sModel = sModel;
|
|
|
|
|
|
|
|
|
|
|
|
bool bExist = ProjectModelManager::instance().ifProjectExsit(sModel);
|
|
|
|
|
|
if(bExist){
|
|
|
|
|
|
auto mapUsed = ProjectModelManager::instance().getData()[sMetaModel][sModel].modelSetting.mapUsedSvg;
|
|
|
|
|
|
addItems(mapUsed);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ProjectIconSetting::initial()
|
|
|
|
|
|
{
|
|
|
|
|
|
QStringList headerText;
|
|
|
|
|
|
headerText<<"类别"<<"图标";
|
|
|
|
|
|
ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
|
|
ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
|
|
|
|
|
|
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectItems);
|
|
|
|
|
|
ui->tableWidget->setColumnCount(headerText.count());
|
|
|
|
|
|
ui->tableWidget->setHorizontalHeaderLabels(headerText);
|
|
|
|
|
|
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
|
|
|
|
|
|
|
|
|
|
|
|
connect(ui->tableWidget, &QTableWidget::cellClicked, this, &ProjectIconSetting::onCellClicked);
|
|
|
|
|
|
connect(ui->btn_ok,&QPushButton::clicked,this,&ProjectIconSetting::onOkClicked);
|
|
|
|
|
|
_iconSize = QSize(32,32);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ProjectIconSetting::addItems(QMap<QString,QByteArray> mapSvg)
|
|
|
|
|
|
{
|
|
|
|
|
|
for(auto iter = mapSvg.begin();iter != mapSvg.end();++iter){
|
|
|
|
|
|
int row = ui->tableWidget->rowCount();
|
|
|
|
|
|
ui->tableWidget->insertRow(row);
|
|
|
|
|
|
|
|
|
|
|
|
QTableWidgetItem* tagItem = new QTableWidgetItem(iter.key());
|
|
|
|
|
|
ui->tableWidget->setItem(row, 0, tagItem);
|
|
|
|
|
|
|
|
|
|
|
|
QTableWidgetItem* iconItem = new QTableWidgetItem();
|
|
|
|
|
|
ui->tableWidget->setItem(row, 1, iconItem);
|
|
|
|
|
|
QByteArray arr = iter.value();
|
|
|
|
|
|
iconItem->setData(Qt::UserRole, arr);
|
|
|
|
|
|
if(!arr.isEmpty()){
|
|
|
|
|
|
QSvgRenderer renderer(arr);
|
|
|
|
|
|
|
|
|
|
|
|
// 创建目标大小的pixmap
|
|
|
|
|
|
QPixmap pixmap(_iconSize);
|
2025-11-05 18:14:31 +08:00
|
|
|
|
pixmap.fill(Qt::transparent); // 透明背景
|
2025-10-17 18:14:44 +08:00
|
|
|
|
|
|
|
|
|
|
// 创建画家并在pixmap上绘制SVG
|
|
|
|
|
|
QPainter painter(&pixmap);
|
|
|
|
|
|
renderer.render(&painter, pixmap.rect());
|
|
|
|
|
|
|
|
|
|
|
|
iconItem->setIcon(pixmap);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ProjectIconSetting::selectImage(int row)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!_sMetaModel.isEmpty() && !_sModel.isEmpty()){
|
|
|
|
|
|
auto mapAllSvg = ProjectModelManager::instance().getData()[_sMetaModel][_sModel].modelSetting.mapSvg;
|
|
|
|
|
|
ProjectIconSelectionDlg dialog(mapAllSvg, this);
|
|
|
|
|
|
if (dialog.exec() == QDialog::Accepted) {
|
|
|
|
|
|
QByteArray selectedSVG = dialog.selectedSVG();
|
|
|
|
|
|
if (!selectedSVG.isEmpty()) {
|
|
|
|
|
|
QSvgRenderer renderer(selectedSVG);
|
|
|
|
|
|
QPixmap pixmap(32, 32);
|
2025-11-05 18:14:31 +08:00
|
|
|
|
//pixmap.fill(Qt::transparent);
|
2025-10-17 18:14:44 +08:00
|
|
|
|
QPainter painter(&pixmap);
|
|
|
|
|
|
renderer.render(&painter);
|
|
|
|
|
|
|
|
|
|
|
|
ui->tableWidget->item(row, 1)->setIcon(QIcon(pixmap));
|
|
|
|
|
|
// 如果需要保存原始SVG数据,可以这样:
|
|
|
|
|
|
ui->tableWidget->item(row, 1)->setData(Qt::UserRole, selectedSVG);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ProjectIconSetting::onOkClicked()
|
|
|
|
|
|
{
|
|
|
|
|
|
QMap<QString,QByteArray> mapUsedSvg;
|
|
|
|
|
|
for (int row = 0; row < ui->tableWidget->rowCount(); ++row) {
|
|
|
|
|
|
QTableWidgetItem *itemTag = ui->tableWidget->item(row, 0);
|
|
|
|
|
|
if (itemTag) {
|
|
|
|
|
|
QTableWidgetItem *itemSvg = ui->tableWidget->item(row, 1);
|
|
|
|
|
|
if(itemSvg){
|
|
|
|
|
|
QByteArray svg = itemSvg->data(Qt::UserRole).toByteArray();
|
|
|
|
|
|
mapUsedSvg.insert(itemTag->text(),svg);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
ProjectModelManager::instance().getData()[_sMetaModel][_sModel].modelSetting.mapUsedSvg = mapUsedSvg;
|
|
|
|
|
|
_controller->updateItemIcon(_sMetaModel,_sModel);
|
|
|
|
|
|
hide();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ProjectIconSetting::onCellClicked(int row,int col)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (col == 1) { // 如果是按钮列
|
|
|
|
|
|
selectImage(row);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|