数据类型列表:就是展现出可以绘制的数据类型,以图形或者文字的方式展现出来。而且展现出来的图形是能实时的反映出当前配置要素的显示风格。通过鼠标的单机实现类型的选择。
数据列表:用于显示当前用户已经绘制的内容,以树形列表的形式表现出来,鼠标打击其中元素可以高亮显示单机的对象,双击可以居中显示所选的图形要素;可以显示某个元素或者某类元素的显隐状态.
数据显示的要素定义文本(xml)
<?xml version="1.0" encoding="UTF-8"?>
<!--
用户自定义标绘的属性配置
-->
<UserDefineDraw>
<DrawPoint radius="3" outerWidth="2" outerColor="0,0,255" fillColor="255,255,0" alpha="255" showName="1">
<DrawLine lineWidth="2" lineStyle="1" lineColor="255,0,0" alpha="255" showName="1">
<DrawPath lineWidth="2" lineStyle="1" lineColor="255,0,0" alpha="255" showName="1">
<DrawRect lineWidth="2" lineStyle="1" lineColor="255,0,0" fillColor="255,255,0" useFillColor="1" alpha="255" showName="1">
<DrawPolygon lineWidth="2" lineStyle="1" lineColor="255,0,0" fillColor="255,255,0" useFillColor="1" alpha="255" showName="1">
<DrawCircle lineWidth="2" lineStyle="1" lineColor="255,0,0" fillColor="255,255,0" useFillColor="0" alpha="255" showName="1">
<DrawPie lineWidth="2" lineStyle="1" lineColor="255,0,0" fillColor="0,0,255" useFillColor="1" alpha="129" showName="1">
<DrawCircleArc lineWidth="2" lineStyle="1" lineColor="255,0,0" alpha="255" showName="1">
<DrawText fontName="宋体" fontSize="15" fontColor="255,0,0" fontBlod="0" fontItalics="0" fontAlpha="128" useBoder="1" lineWidth="2" lineStyle="1" lineColor="255,0,0" lineAlpha="158" useBKColor="0" bkColor="255,255,0" bkAlpha = "0">
</UserDefineDraw>
每添加一项数据内容就要添加一条文本内容,用来保存显示数据的显示风格。同时也会有对这些文本要素的读取和修改操作。
首先定义要素属性的基类:
//几何图形的基本属性
class GeoBaseProp {
public:
GeoBaseProp();
~GeoBaseProp();
virtual void cleanData();
};
GeoBaseProp::GeoBaseProp() {
}
GeoBaseProp::~GeoBaseProp() {
}
void GeoBaseProp::cleanData() {
}
具体的图形要素(点、线其他要素不在列出)
GeoBaseProp::GeoBaseProp() {
}
GeoBaseProp::~GeoBaseProp() {
}
void GeoBaseProp::cleanData() {
}
GeoPointProp::GeoPointProp() {
}
GeoPointProp::~GeoPointProp() {
}
void GeoPointProp::setRadius(int r) {
_radius = r;
}
int& GeoPointProp::getRadius() {
return _radius;
}
void GeoPointProp::setOuterWidth(int w) {
_outerWidth = w;
}
int& GeoPointProp::getOuterWidth() {
return _outerWidth;
}
void GeoPointProp::setOuterColor(QColor color) {
_clrOuter = color;
}
QColor& GeoPointProp::getOuterColor() {
return _clrOuter;
}
void GeoPointProp::setFillColor(QColor color) {
_clrFill = color;
}
QColor& GeoPointProp::getFillColor() {
return _clrFill;
}
void GeoPointProp::setAlpha(int a) {
_clrOuter.setAlpha(a);
_clrFill.setAlpha(a);
_alpha = a;
}
int& GeoPointProp::getAlpha() {
return _alpha;
}
void GeoPointProp::setShowName(bool b) {
_showName = b;
}
bool& GeoPointProp::getShowName() {
return _showName;
}
线要素风格:
//线的显示属性
class GeoLineProp : public GeoBaseProp {
public:
GeoLineProp();
~GeoLineProp();
void setLineWidth(int w);
int&getLineWidth();
void setLineType(int type);
int getLineType();
void setLineColor(QColor color);
QColor&getLineColor();
void setLineAlpha(int a);
int&getLineAlpha();
void setShowName(bool b);
bool&getShowName();
private:
int _lineWidth = 2;//线宽
Qt::PenStyle _lineType = Qt::SolidLine;//线型
QColor _lineColor = QColor(255, 0, 0);//线色
int _alpha = 128;//透明度
bool _showName = true;//是否显示文本
};
GeoLineProp::GeoLineProp() {
}
GeoLineProp::~GeoLineProp() {
}
void GeoLineProp::setLineWidth(int w) {
_lineWidth = w;
}
int& GeoLineProp::getLineWidth() {
return _lineWidth;
}
void GeoLineProp::setLineType(int type) {
_lineType = (Qt::PenStyle)type;
}
int GeoLineProp::getLineType() {
return int(_lineType);
}
void GeoLineProp::setLineColor(QColor color) {
_lineColor = color;
}
QColor& GeoLineProp::getLineColor() {
return _lineColor;
}
void GeoLineProp::setLineAlpha(int a) {
_lineColor.setAlpha(a);
_alpha = a;
}
int& GeoLineProp::getLineAlpha() {
return _alpha;
}
void GeoLineProp::setShowName(bool b) {
_showName = b;
}
bool& GeoLineProp::getShowName() {
return _showName;
}
要素文件的读取和保存
文件的基类:
class UserGeoCfg {
public:
UserGeoCfg();
~UserGeoCfg();
virtual void loadGeoCfg(CMarkup* markUp);
virtual void saveGeoCfg(CMarkup* markUp);
GeoBaseProp* getGeoProp() {
return _geoProp;
}
protected:
GeoBaseProp* _geoProp = nullptr;
};
UserGeoCfg::UserGeoCfg() {
}
UserGeoCfg::~UserGeoCfg() {
}
void UserGeoCfg::loadGeoCfg(CMarkup* markUp) {
}
void UserGeoCfg::saveGeoCfg(CMarkup* markUp) {
}
点要素属性的读写:
//用户自定义标绘 - 点的配置
class UserGeoPointCfg : public UserGeoCfg {
public:
UserGeoPointCfg();
~UserGeoPointCfg();
virtual void loadGeoCfg(CMarkup* markUp);
virtual void saveGeoCfg(CMarkup* markUp);
};
UserGeoPointCfg::UserGeoPointCfg() {
_geoProp = new GeoPointProp;
}
UserGeoPointCfg::~UserGeoPointCfg() {
}
void UserGeoPointCfg::loadGeoCfg(CMarkup* markUp) {
markUp->IntoElem();
if (markUp->FindElem("DrawPoint") && _geoProp != nullptr) {
//半径
std::string r = markUp->GetAttrib("radius");
GeoPointProp* pointProp = dynamic_cast<GeoPointProp*>(_geoProp);
pointProp->setRadius(atoi(r.c_str()));
//线宽
std::string w = markUp->GetAttrib("outerWidth");
pointProp->setOuterWidth(atoi(w.c_str()));
//圆点外圈颜色
std::string oc = markUp->GetAttrib("outerColor");
pointProp->setOuterColor(CommTools::string2Color(oc));
//圆圈的填充色
std::string fc = markUp->GetAttrib("fillColor");
pointProp->setFillColor(CommTools::string2Color(fc));
//透明度
std::string a = markUp->GetAttrib("alpha");
pointProp->setAlpha(atoi(a.c_str()));
//显示文本
std::string sn = markUp->GetAttrib("showName");
pointProp->setShowName(atoi(sn.c_str()));
}
markUp->OutOfElem();
}
void UserGeoPointCfg::saveGeoCfg(CMarkup* markUp) {
markUp->IntoElem();
if (markUp->FindElem("DrawPoint") && _geoProp != nullptr) {
GeoPointProp* pointProp = dynamic_cast<GeoPointProp*>(_geoProp);
markUp->SetAttrib("radius", pointProp->getRadius());
markUp->SetAttrib("outerWidth", pointProp->getOuterWidth());
markUp->SetAttrib("outerColor", CommTools::color2QString(pointProp->getOuterColor()));
markUp->SetAttrib("fillColor", CommTools::color2QString(pointProp->getFillColor()));
markUp->SetAttrib("alpha", pointProp->getAlpha());
markUp->SetAttrib("showName", pointProp->getShowName());
}
markUp->OutOfElem();
}
线要素属性的读写:
//用户自定义标绘 - 线的配置
class UserGeoLineCfg : public UserGeoCfg {
public:
UserGeoLineCfg();
~UserGeoLineCfg();
virtual void loadGeoCfg(CMarkup* markUp);
virtual void saveGeoCfg(CMarkup* markUp);
};
UserGeoLineCfg::UserGeoLineCfg() {
_geoProp = new GeoLineProp;
}
UserGeoLineCfg::~UserGeoLineCfg() {
}
void UserGeoLineCfg::loadGeoCfg(CMarkup* markUp) {
markUp->IntoElem();
if (markUp->FindElem("DrawLine") && _geoProp != nullptr) {
GeoLineProp* lineProp = dynamic_cast<GeoLineProp*>(_geoProp);
//线宽
std::string w = markUp->GetAttrib("lineWidth");
lineProp->setLineWidth(atoi(w.c_str()));
//线型
std::string s = markUp->GetAttrib("lineStyle");
lineProp->setLineType(atoi(s.c_str()));
//线色
std::string c = markUp->GetAttrib("lineColor");
lineProp->setLineColor(CommTools::string2Color(c));
//透明度
std::string a = markUp->GetAttrib("alpha");
lineProp->setLineAlpha(atoi(a.c_str()));
//显示文本
std::string sn = markUp->GetAttrib("showName");
lineProp->setShowName(atoi(sn.c_str()));
}
markUp->OutOfElem();
}
void UserGeoLineCfg::saveGeoCfg(CMarkup* markUp) {
markUp->IntoElem();
if (markUp->FindElem("DrawLine") && _geoProp != nullptr) {
GeoLineProp* lineProp = dynamic_cast<GeoLineProp*>(_geoProp);
markUp->SetAttrib("lineWidth", lineProp->getLineWidth());
markUp->SetAttrib("lineStyle", lineProp->getLineType());
markUp->SetAttrib("lineColor", CommTools::color2QString(lineProp->getLineColor()));
markUp->SetAttrib("alpha", lineProp->getLineAlpha());
markUp->SetAttrib("showName", lineProp->getShowName());
}
markUp->OutOfElem();
}
通过以上的数据信息可以保存显示要素的基本信息,然后通过文件读取获取要素的基本信息。修改之后的效果可以在类型列表中展现出来。
UserGeoLineCfg::UserGeoLineCfg() {
_geoProp = new GeoLineProp;
}
UserGeoLineCfg::~UserGeoLineCfg() {
}
void UserGeoLineCfg::loadGeoCfg(CMarkup* markUp) {
markUp->IntoElem();
if (markUp->FindElem("DrawLine") && _geoProp != nullptr) {
GeoLineProp* lineProp = dynamic_cast<GeoLineProp*>(_geoProp);
//线宽
std::string w = markUp->GetAttrib("lineWidth");
lineProp->setLineWidth(atoi(w.c_str()));
//线型
std::string s = markUp->GetAttrib("lineStyle");
lineProp->setLineType(atoi(s.c_str()));
//线色
std::string c = markUp->GetAttrib("lineColor");
lineProp->setLineColor(CommTools::string2Color(c));
//透明度
std::string a = markUp->GetAttrib("alpha");
lineProp->setLineAlpha(atoi(a.c_str()));
//显示文本
std::string sn = markUp->GetAttrib("showName");
lineProp->setShowName(atoi(sn.c_str()));
}
markUp->OutOfElem();
}
void UserGeoLineCfg::saveGeoCfg(CMarkup* markUp) {
markUp->IntoElem();
if (markUp->FindElem("DrawLine") && _geoProp != nullptr) {
GeoLineProp* lineProp = dynamic_cast<GeoLineProp*>(_geoProp);
markUp->SetAttrib("lineWidth", lineProp->getLineWidth());
markUp->SetAttrib("lineStyle", lineProp->getLineType());
markUp->SetAttrib("lineColor", CommTools::color2QString(lineProp->getLineColor()));
markUp->SetAttrib("alpha", lineProp->getLineAlpha());
markUp->SetAttrib("showName", lineProp->getShowName());
}
markUp->OutOfElem();
}
QtGuiUserDrawType::QtGuiUserDrawType(QWidget *parent)
: QWidget(parent) {
ui.setupUi(this);
CurrentDrawFinishMgr()->saveDrawFinish(this);
init();
}
QtGuiUserDrawType::~QtGuiUserDrawType() {
}
QPixmap& QtGuiUserDrawType::getGeoPixmap(USER_DRAW_TYPE&t) {
switch (t) {
case USER_DRAW_POINT:
return _pointIcon;
case USER_DRAW_LINE://线
return _lineIcon;
case USER_DRAW_PATH://路径
return _pathIcon;
case USER_DRAW_RECT://矩形
return _rectIcon;
case USER_DRAW_POLYGON://多边形
return _polygonIcon;
case USER_DRAW_CIRCLE://圆
return _circleIcon;
case USER_DRAW_PIE://扇形
return _pieIcon;
case USER_DRAW_CIRCLE_ARC://圆弧
return _circleArcIcon;
case USER_DRAW_TEXT://文本
return _textIcon;
default:
break;
}
return _pointIcon;
}
void QtGuiUserDrawType::updateIcon() {
setIcon();
}
void QtGuiUserDrawType::drawFinish(USER_DRAW_TYPE t) {
//保存当前绘制的类型
DrawGeoMapInst()->setCurrentUserDrawType(USER_DRAW_NULL);
//设置鼠标操作互斥
DrawGeoMapInst()->setEventUse(false);
//设置鼠标为箭头
setCursor(Qt::ArrowCursor);
//设置地图鼠标为箭头
GlobalUseInst()->load2DMapUpdate()->setCursor(Qt::ArrowCursor);
}
void QtGuiUserDrawType::init() {
_slm = new QStandardItemModel;
ui.listView->setViewMode(QListView::IconMode);
ui.listView->setMovement(QListView::Static);
ui.listView->setIconSize(QSize(100, 100));
ui.listView->setResizeMode(QListView::Adjust);
ui.listView->setWordWrap(true);
ui.listView->setGridSize(QSize(100, 100));
ui.listView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui.listView, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(slotContextMenu(const QPoint&)));
QObject::connect(ui.listView, SIGNAL(clicked(QModelIndex)), this, SLOT(slotItemClicked(QModelIndex)));
ui.listView->setModel(_slm);
setIcon();
_rightTypeMenuFactory = new RightTypeMenuFactory;
}
void QtGuiUserDrawType::setIcon() {
_slm->clear();
_slm->appendRow(iconPoint());
_slm->appendRow(iconLine());
_slm->appendRow(iconPath());
_slm->appendRow(iconRect());
_slm->appendRow(iconPolygon());
_slm->appendRow(iconCircle());
_slm->appendRow(iconPie());
_slm->appendRow(iconCircleArc());
_slm->appendRow(iconText());
}
QStandardItem* QtGuiUserDrawType::iconPoint() {
_pointIcon = QPixmap(_iconWidth, _iconHeight);
_pointIcon.fill(Qt::white);
QPainter painter(&_pointIcon);
painter.setRenderHint(QPainter::Antialiasing, true);
//根据属性配置显示内容
GeoPointProp*pointProp = dynamic_cast<GeoPointProp*>(UserGeoCfgMgr()->getGeoProp(USER_DRAW_POINT));
if (pointProp != nullptr){
painter.setPen(QPen(pointProp->getOuterColor(), pointProp->getOuterWidth(), Qt::SolidLine));
painter.setBrush(pointProp->getFillColor());
painter.drawEllipse(_iconWidth / 2 - pointProp->getRadius()
, _iconHeight / 2 - pointProp->getRadius()
, pointProp->getRadius() * 2
, pointProp->getRadius() * 2);
}
QStandardItem *item = new QStandardItem(QIcon(_pointIcon), QStringLiteral("点"));
item->setEditable(false);
item->setData(USER_DRAW_POINT);
return item;
}
QStandardItem* QtGuiUserDrawType::iconLine() {
_lineIcon = QPixmap(_iconWidth, _iconHeight);
_lineIcon.fill(Qt::white);
QPainter painter(&_lineIcon);
painter.setRenderHint(QPainter::Antialiasing, true);
GeoLineProp*lineProp = dynamic_cast<GeoLineProp*>(UserGeoCfgMgr()->getGeoProp(USER_DRAW_LINE));
if (lineProp != nullptr) {
QPen pen;
pen.setColor(lineProp->getLineColor());
pen.setWidth(lineProp->getLineWidth());
pen.setStyle((Qt::PenStyle)lineProp->getLineType());
painter.setPen(pen);
}
painter.drawLine(10, 10, 12, _iconHeight - 10);
painter.drawLine(12, _iconHeight - 10, 25, 20);
painter.drawLine(25, 20, _iconWidth - 10, _iconHeight - 10);
QStandardItem *item = new QStandardItem(QIcon(_lineIcon), QStringLiteral("线"));
item->setEditable(false);
item->setData(USER_DRAW_LINE);
return item;
}
QStandardItem* QtGuiUserDrawType::iconPath() {
_pathIcon = QPixmap(_iconWidth, _iconHeight);
_pathIcon.fill(Qt::white);
QPainter painter(&_pathIcon);
painter.setRenderHint(QPainter::Antialiasing, true);
GeoPathProp*pathProp = dynamic_cast<GeoPathProp*>(UserGeoCfgMgr()->getGeoProp(USER_DRAW_PATH));
if (pathProp != nullptr) {
QPen pen;
pen.setColor(pathProp->getLineColor());
pen.setWidth(pathProp->getLineWidth());
pen.setStyle((Qt::PenStyle)pathProp->getLineType());
painter.setPen(pen);
}
QPainterPath path(QPoint(5, 25));
path.cubicTo(QPointF(20, 10), QPointF(30, 40), QPointF(45,24));
painter.drawPath(path);
QStandardItem *item = new QStandardItem(QIcon(_pathIcon), QStringLiteral("曲线"));
item->setEditable(false);
item->setData(USER_DRAW_PATH);
return item;
}
QStandardItem* QtGuiUserDrawType::iconRect() {
_rectIcon = QPixmap(_iconWidth, _iconHeight);
_rectIcon.fill(Qt::white);
QPainter painter(&_rectIcon);
painter.setRenderHint(QPainter::Antialiasing, true);
GeoRectProp*rectProp = dynamic_cast<GeoRectProp*>(UserGeoCfgMgr()->getGeoProp(USER_DRAW_RECT));
if (rectProp != nullptr) {
painter.setPen(QPen(rectProp->getLineColor(), rectProp->getLineWidth(), (Qt::PenStyle)rectProp->getLineType()));
if (rectProp->getUseFillColor()){
painter.setBrush(rectProp->getFillColor());
} else {
painter.setBrush(Qt::NoBrush);
}
painter.drawRect(5, 10, _iconWidth - 10, _iconHeight - 20);
}
QStandardItem *item = new QStandardItem(QIcon(_rectIcon), QStringLiteral("矩形"));
item->setEditable(false);
item->setData(USER_DRAW_RECT);
return item;
}
QStandardItem* QtGuiUserDrawType::iconPolygon() {
_polygonIcon = QPixmap(_iconWidth, _iconHeight);
_polygonIcon.fill(Qt::white);
QPainter painter(&_polygonIcon);
painter.setRenderHint(QPainter::Antialiasing, true);
GeoPolygonProp*polygonProp = dynamic_cast<GeoPolygonProp*>(UserGeoCfgMgr()->getGeoProp(USER_DRAW_POLYGON));
if (polygonProp != nullptr) {
painter.setPen(QPen(polygonProp->getLineColor(), polygonProp->getLineWidth(), (Qt::PenStyle)polygonProp->getLineType()));
if (polygonProp->getUseFillColor()){
painter.setBrush(polygonProp->getFillColor());
} else {
painter.setBrush(Qt::NoBrush);
}
}
QPolygon poiygon;
poiygon.push_back(QPoint(5, 5));
poiygon.push_back(QPoint(7, _iconHeight-5));
poiygon.push_back(QPoint(_iconWidth-5, 20));
poiygon.push_back(QPoint(20, 5));
painter.drawPolygon(poiygon);
QStandardItem *item = new QStandardItem(QIcon(_polygonIcon), QStringLiteral("多边形"));
item->setEditable(false);
item->setData(USER_DRAW_POLYGON);
return item;
}
QStandardItem* QtGuiUserDrawType::iconCircle() {
_circleIcon = QPixmap(_iconWidth, _iconHeight);
_circleIcon.fill(Qt::white);
QPainter painter(&_circleIcon);
painter.setRenderHint(QPainter::Antialiasing, true);
GeoCircleProp*circleProp = dynamic_cast<GeoCircleProp*>(UserGeoCfgMgr()->getGeoProp(USER_DRAW_CIRCLE));
if (circleProp != nullptr) {
painter.setPen(QPen(circleProp->getLineColor(), circleProp->getLineWidth(), (Qt::PenStyle)circleProp->getLineType()));
if (circleProp->getUseFillColor()) {
painter.setBrush(circleProp->getFillColor());
} else {
painter.setBrush(Qt::NoBrush);
}
}
painter.drawEllipse(5, 5, _iconWidth - 10, _iconHeight - 10);
QStandardItem *item = new QStandardItem(QIcon(_circleIcon), QStringLiteral("圆"));
item->setEditable(false);
item->setData(USER_DRAW_CIRCLE);
return item;
}
QStandardItem* QtGuiUserDrawType::iconPie() {
_pieIcon = QPixmap(_iconWidth, _iconHeight);
_pieIcon.fill(Qt::white);
QPainter painter(&_pieIcon);
painter.setRenderHint(QPainter::Antialiasing, true);
GeoPieProp*pieProp = dynamic_cast<GeoPieProp*>(UserGeoCfgMgr()->getGeoProp(USER_DRAW_PIE));
if (pieProp != nullptr) {
painter.setPen(QPen(pieProp->getLineColor(), pieProp->getLineWidth(), (Qt::PenStyle)pieProp->getLineType()));
if (pieProp->getUseFillColor()) {
painter.setBrush(pieProp->getFillColor());
} else {
painter.setBrush(Qt::NoBrush);
}
}
painter.drawPie(5, 15, _iconWidth - 10, _iconHeight - 10, 30 * 16, 150 * 16);
QStandardItem *item = new QStandardItem(QIcon(_pieIcon), QStringLiteral("扇形"));
item->setEditable(false);
item->setData(USER_DRAW_PIE);
return item;
}
QStandardItem* QtGuiUserDrawType::iconCircleArc() {
_circleArcIcon = QPixmap(_iconWidth, _iconHeight);
_circleArcIcon.fill(Qt::white);
QPainter painter(&_circleArcIcon);
painter.setRenderHint(QPainter::Antialiasing, true);
GeoCircleArcProp*circlePieProp = dynamic_cast<GeoCircleArcProp*>(UserGeoCfgMgr()->getGeoProp(USER_DRAW_CIRCLE_ARC));
if (circlePieProp != nullptr) {
painter.setPen(QPen(circlePieProp->getLineColor(), circlePieProp->getLineWidth(), (Qt::PenStyle)circlePieProp->getLineType()));
}
painter.drawArc(0, 20, _iconWidth - 10, _iconHeight - 10, 0, 120 * 16);
QStandardItem *item = new QStandardItem(QIcon(_circleArcIcon), QStringLiteral("圆弧"));
item->setEditable(false);
item->setData(USER_DRAW_CIRCLE_ARC);
return item;
}
QStandardItem* QtGuiUserDrawType::iconText() {
_textIcon = QPixmap(_iconWidth, _iconHeight);
_textIcon.fill(Qt::white);
QPainter painter(&_textIcon);
painter.setRenderHint(QPainter::Antialiasing, true);
QRect rect;
rect.setX(3);
rect.setTop(3);
rect.setWidth(42);
rect.setHeight(42);
GeoTextProp*textProp = dynamic_cast<GeoTextProp*>(UserGeoCfgMgr()->getGeoProp(USER_DRAW_TEXT));
if (textProp->getUseBoder()) {//使用边框
QPen penLine(QBrush(textProp->getLineColor()), textProp->getLineWidth(), Qt::PenStyle(textProp->getLineType()));
painter.setPen(penLine);
painter.setBrush(Qt::NoBrush);
painter.drawRect(rect);
}
if (textProp->getUseBKColor()) {//绘制背景色
QBrush brush(textProp->getBKColor());
painter.setBrush(brush);
painter.setPen(Qt::NoPen);
painter.drawRect(rect);
}
if (textProp != nullptr) {
painter.setPen(QPen(textProp->getFontColor(), 2, Qt::SolidLine));
QFont font(textProp->getFontFamily(), textProp->getFontSize());
font.setBold(textProp->getBold());
font.setItalic(textProp->getItalics());
painter.setFont(font);
}
painter.drawText(5, 30, QStringLiteral("文本"));
QStandardItem *item = new QStandardItem(QIcon(_textIcon), QStringLiteral("文本"));
item->setEditable(false);
item->setData(USER_DRAW_TEXT);
return item;
}
void QtGuiUserDrawType::slotItemClicked(QModelIndex idx) {
QStandardItem *curItem = _slm->itemFromIndex(idx);
int userType = curItem->data().toInt();
//保存当前绘制的类型
DrawGeoMapInst()->setCurrentUserDrawType(USER_DRAW_TYPE(userType));
//设置鼠标操作互斥
MutualMouse()->setAllMouseEventUse(false);
DrawGeoMapInst()->setEventUse(true);
//鼠标选中几何图形
MouseSelectMgr()->useMouseSelect(false);
}
void QtGuiUserDrawType::slotContextMenu(const QPoint&pt) {
QModelIndex idx = ui.listView->indexAt(pt);
QStandardItem *curItem = _slm->itemFromIndex(idx);
if (curItem == nullptr){
return;
}
int userType = curItem->data().toInt();
QtRightTypeAction* menuList = _rightTypeMenuFactory->getGeoTypeMenu(USER_DRAW_TYPE(userType), this);
menuList->typeMenuList()->exec(QCursor::pos());//在当前鼠标位置显示
}
class QtGuiUserDrawList : public QWidget,public IMouseSelect {
Q_OBJECT
public:
QtGuiUserDrawList(QWidget *parent = Q_NULLPTR);
~QtGuiUserDrawList();
void registerItemIcon(IGeoIcon* icon);
virtual void cancelMouseSelect()override;
virtual void mouseSelectGeo(USER_DRAW_TYPE type, long long geoID)override;
private:
void init();
private slots:
void slotMouseSelect(int s);
void slotBntClearGeo();
void slotBntLoad();
void slotBntSaveAs();
private:
Ui::QtGuiUserDrawList ui;
UserDrawTree* _userDrawTree = nullptr;
};
QtGuiUserDrawList::QtGuiUserDrawList(QWidget *parent)
: QWidget(parent) {
ui.setupUi(this);
_userDrawTree = ui.treeWidget;
init();
MouseSelectMgr()->setMouseSelectUI(this);
}
QtGuiUserDrawList::~QtGuiUserDrawList() {
}
void QtGuiUserDrawList::registerItemIcon(IGeoIcon* icon) {
_userDrawTree->registerItemIcon(icon);
}
void QtGuiUserDrawList::cancelMouseSelect() {
ui.checkBoxMouseSelect->setChecked(false);
}
void QtGuiUserDrawList::mouseSelectGeo(USER_DRAW_TYPE type, long long geoID) {
_userDrawTree->selectItem(type, geoID);
}
void QtGuiUserDrawList::init() {
bool b = connect(ui.checkBoxMouseSelect,SIGNAL(stateChanged(int)), this, SLOT(slotMouseSelect(int)));
b = connect(ui.pushButtonClear, SIGNAL(clicked()), this, SLOT(slotBntClearGeo()));
b = connect(ui.pushButtonLoad, SIGNAL(clicked()), this, SLOT(slotBntLoad()));
b = connect(ui.pushButtonSaveAs, SIGNAL(clicked()), this, SLOT(slotBntSaveAs()));
}
void QtGuiUserDrawList::slotMouseSelect(int s) {
MouseSelectMgr()->useMouseSelect(s == Qt::Checked);
}
void QtGuiUserDrawList::slotBntClearGeo() {
if (0 == QMessageBox::question(nullptr, QStringLiteral("删除"), QStringLiteral("确定要清空绘制数据吗?"), QStringLiteral("确定"), QStringLiteral("取消"))) {
UserDefineDataMgr()->cleanData();
_userDrawTree->cleanTree();
GlobalUseInst()->load2DMapUpdate()->update2DMap();
}
}
void QtGuiUserDrawList::slotBntLoad() {
QString fileName = QFileDialog::getOpenFileName(this,
QStringLiteral("加载用户绘制数据"), "", tr("Draw Data (*.wbx)"));
if (!fileName.isNull()) {//fileName即是选择的文件名
GeoStorageMgr()->loadFromFile(fileName);
}
GlobalUseInst()->load2DMapUpdate()->update2DMap();
}
void QtGuiUserDrawList::slotBntSaveAs() {
QString fileName = QFileDialog::getSaveFileName(this, QStringLiteral("保存用户绘制数据"), "", tr("Draw Data (*.wbx)"));
GeoStorageMgr()->saveToFile(fileName);
QString msg = QStringLiteral("用户数据已保存,数据路径:");
msg += fileName;
QMessageBox::information(0, QStringLiteral("提示"), msg, QStringLiteral("确定"));
}
里面用到了一个自定义的树形控件:
class UserDrawTree : public QTreeWidget,public IUserDrawTreeOpt {
Q_OBJECT
public:
UserDrawTree(QWidget *parent);
~UserDrawTree();
void registerItemIcon(IGeoIcon* icon);
virtual void addGeoOnTree(BaseGeo* geo)override;
virtual void deleteGeoOnTree(BaseGeo* geo)override;
virtual void updateTreeItem(BaseGeo* geo);
void selectItem(USER_DRAW_TYPE type, long long geoID);
void cleanTree();
private:
void initTree();
QTreeWidgetItem * addTopItem(USER_DRAW_TYPE t);
QTreeWidgetItem * addTopItem(QString name, USER_DRAW_TYPE t);
QString rootItemText(USER_DRAW_TYPE t);
private slots :
void slotRClick(QPoint pt);
void slotPressItem(QTreeWidgetItem *item);
void slotDoubleClickItem(QTreeWidgetItem *item);
void slotCheckChange(QTreeWidgetItem *item, int col);
private:
IGeoIcon* _geoIcon = nullptr;
RightGeoMenuFactory* _rightGeoMenuFactory = nullptr;//根据绘制元素的类型和ID创建菜单
DoubleClickItemManager* _doubleClickItemManager = nullptr;
};
UserDrawTree::UserDrawTree(QWidget *parent)
: QTreeWidget(parent) {
UserDefineDataMgr()->registerTreeAdd(this);
initTree();
_rightGeoMenuFactory = new RightGeoMenuFactory;
_doubleClickItemManager = new DoubleClickItemManager;
}
UserDrawTree::~UserDrawTree() {
}
void UserDrawTree::registerItemIcon(IGeoIcon* icon) {
_geoIcon = icon;
}
void UserDrawTree::addGeoOnTree(BaseGeo* geo) {
USER_DRAW_TYPE t = geo->getUserDrawDataType();
if (t == USER_DRAW_NULL){//通过界面添加几何对象的时候,不要创建树形节点
return;
}
QTreeWidgetItem* rootItem = addTopItem(t);
rootItem->setExpanded(true);
//保存具体的数据
QTreeWidgetItem* childItem = new QTreeWidgetItem(rootItem, QStringList(geo->getGeoName()));
childItem->setCheckState(0, Qt::Checked);
childItem->setData(0, Qt::UserRole, 1);//保存当前层级
childItem->setData(1, Qt::UserRole, geo->getGeoID());
childItem->setExpanded(true);
}
void UserDrawTree::deleteGeoOnTree(BaseGeo* geo) {
QTreeWidgetItemIterator it(this);
while (*it) {
int level = (*it)->data(0, Qt::UserRole).toInt();
if (level == 1) {//第二层
long long id = (*it)->data(1, Qt::UserRole).toLongLong();
if (geo->getGeoID() == id) {
int idx = (*it)->parent()->indexOfChild(*it);
(*it)->parent()->takeChild(idx);
break;
}
}
it++;
}
}
void UserDrawTree::updateTreeItem(BaseGeo* geo) {
QTreeWidgetItemIterator it(this);
while (*it) {
int level = (*it)->data(0, Qt::UserRole).toInt();
if (level == 1) {//第二层
long long id = (*it)->data(1, Qt::UserRole).toLongLong();
if (geo->getGeoID() == id) {
(*it)->setText(0, geo->getGeoName());
break;
}
}
it++;
}
}
void UserDrawTree::selectItem(USER_DRAW_TYPE type, long long geoID) {
QTreeWidgetItemIterator it(this);
while (*it) {
int level = (*it)->data(0, Qt::UserRole).toInt();
if (level == 1) {//第二层
long long id = (*it)->data(1, Qt::UserRole).toLongLong();
if (geoID == id){
(*it)->setSelected(true);
} else {
(*it)->setSelected(false);
}
}
it++;
}
}
void UserDrawTree::cleanTree() {
this->clear();
}
void UserDrawTree::initTree() {
this->setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(slotRClick(QPoint)));
connect(this, SIGNAL(itemPressed(QTreeWidgetItem*, int)), this, SLOT(slotPressItem(QTreeWidgetItem*)));
connect(this, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this, SLOT(slotDoubleClickItem(QTreeWidgetItem*)));
connect(this, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(slotCheckChange(QTreeWidgetItem *, int)));
}
QTreeWidgetItem * UserDrawTree::addTopItem(USER_DRAW_TYPE t) {
int topNum = topLevelItemCount();
for (int i = 0; i < topNum; i++) {
QTreeWidgetItem *topItem = topLevelItem(i);
int type = topItem->data(1, Qt::UserRole).toInt();
if (type == t){//如果存在当前这个根节点
return topItem;
}
}
return addTopItem(rootItemText(t), t);
}
QTreeWidgetItem * UserDrawTree::addTopItem(QString name, USER_DRAW_TYPE t) {
QTreeWidgetItem* rootItem = new QTreeWidgetItem(this, QStringList(name));
rootItem->setData(0, Qt::UserRole, 0);//保存当前层级
rootItem->setData(1, Qt::UserRole, QVariant(t));//保存当前几何类型
if (_geoIcon != nullptr) {
rootItem->setIcon(0, QIcon(_geoIcon->getGeoPixmap(t)));
}
rootItem->setCheckState(0, Qt::Checked);
rootItem->setFlags(Qt::ItemIsTristate | rootItem->flags());
this->addTopLevelItem(rootItem);
return rootItem;
}
QString UserDrawTree::rootItemText(USER_DRAW_TYPE t) {
QString name = "";
switch (t) {
case USER_DRAW_NULL:
break;
case USER_DRAW_POINT:
name = QStringLiteral("点");
break;
case USER_DRAW_LINE:
name = QStringLiteral("线");
break;
case USER_DRAW_PATH:
name = QStringLiteral("路径");
break;
case USER_DRAW_RECT:
name = QStringLiteral("矩形");
break;
case USER_DRAW_POLYGON:
name = QStringLiteral("多边形");
break;
case USER_DRAW_CIRCLE:
name = QStringLiteral("圆");
break;
case USER_DRAW_PIE:
name = QStringLiteral("扇形");
break;
case USER_DRAW_CIRCLE_ARC:
name = QStringLiteral("圆弧");
break;
case USER_DRAW_TEXT:
name = QStringLiteral("文本");
break;
default:
break;
}
return name;
}
void UserDrawTree::slotRClick(QPoint pt) {
QTreeWidgetItem* clickItem = itemAt(pt);
if (clickItem == nullptr){
return;
}
int level = clickItem->data(0, Qt::UserRole).toInt();
if (level == 1){
int type = clickItem->parent()->data(1, Qt::UserRole).toInt();//几何类型
long long id = clickItem->data(1, Qt::UserRole).toLongLong();//几何ID
QtRightGeoAction*menu = _rightGeoMenuFactory->getGeoMenu((USER_DRAW_TYPE)type);
menu->popMenu((USER_DRAW_TYPE)type, id, QCursor::pos());//在当前鼠标位置显示
}
}
void UserDrawTree::slotPressItem(QTreeWidgetItem *item) {
int level = item->data(0, Qt::UserRole).toInt();
if (level == 1) {//第二层
long long id = item->data(1, Qt::UserRole).toLongLong();
UserDefineDataMgr()->setGeoHightLight(id);
GlobalUseInst()->load2DMapUpdate()->update2DMap();
}
}
void UserDrawTree::slotDoubleClickItem(QTreeWidgetItem *item) {
int level = item->data(0, Qt::UserRole).toInt();
if (level == 1) {//第二层
int type = item->parent()->data(1, Qt::UserRole).toInt();//几何类型
long long id = item->data(1, Qt::UserRole).toLongLong();//几何ID
//双击选中的GEO
BaseGeo* selectGeo = UserDefineDataMgr()->findGeo(USER_DRAW_TYPE(type), id);
_doubleClickItemManager->centerGeo(selectGeo);
GlobalUseInst()->load2DMapUpdate()->update2DMap();
}
}
void UserDrawTree::slotCheckChange(QTreeWidgetItem *item, int col) {
int level = item->data(0, Qt::UserRole).toInt();
if (level == 0) {//第一层
int type = item->data(1, Qt::UserRole).toInt();
if (item->checkState(0) != Qt::PartiallyChecked) {
UserDefineDataMgr()->setGeoShow((USER_DRAW_TYPE)type, item->checkState(0) == Qt::Checked);
}
} else if (level == 1) {//第二层
int type = item->parent()->data(1, Qt::UserRole).toInt();//几何类型
long long id = item->data(1, Qt::UserRole).toLongLong();//几何ID
if (item->checkState(0) != Qt::PartiallyChecked) {
UserDefineDataMgr()->setGeoShow((USER_DRAW_TYPE)type, id, item->checkState(0) == Qt::Checked);
}
}
GlobalUseInst()->load2DMapUpdate()->update2DMap();
}
aaa