Now I build graphic application, but I have issue with draw the dimension line, and dimension can move when I move mouse, as software: Auto Cad or solidwork software, as picture below:
https://i.stack.imgur.com/BXCBj.png
, I want to adjust the position of dimension line,Now, my method is use handle Item to move the dimension,
https://i.stack.imgur.com/34VfQ.png
This is my code:
{
// QCursor* cursor = nullptr;
switch (handlePosition) {
case dimension:
{
setCursor(Qt::SizeHorCursor);
DPS_ResizeableHandleRect * rectItem = dynamic_cast<DPS_ResizeableHandleRect *>( parentItem());
if(rectItem){
QRectF boundingFrameRect
= rectItem
->selectorFrameBounds
();
boundingFrameRect.setRight(event->pos().x() + 5);
rectItem->setSelectorFrameBounds(boundingFrameRect);
}
}
break;
}
}
void DPS_ResizeableHandleRect::drawHandlesDimension()
{
//Populate handles in list
if(handleListCenter.count() == 0){
// handleListCenter.append(new DPS_HandleItem(DPS_HandleItem::LeftCenter));
handleListCenter.append(new DPS_HandleItem(DPS_HandleItem::dimension));
}
//Set up pen
mPen.setWidth(1);
mPen.setColor(Qt::darkGreen);
//Right Center handle
QPointF centerRightCorner
= selectorFrameBounds
().
topRight() + QPointF(-4.5, selectorFrameBounds
().
height()/2.0 - 4.5);
centerRightHandleRect
= QRectF(centerRightCorner,
QSize(9,
9));
handleListCenter[0]->setRect(centerRightHandleRect);
if(!handleListCenter.isEmpty() && (!handlesAddedToScene)){
handleListCenter[0]->setPen(mPen);
handleListCenter
[0]->setBrush
(QBrush(Qt
::darkGreen));
ownerItem->scene()->addItem(handleListCenter[0]);
handleListCenter[0]->setParentItem(ownerItem);
}
handlesAddedToScene = true;
}
void DPS_ResizeableHandleRect::drawHandlesDimensionIfNecessary()
{
if(!ownerItem){
qWarning() << "ResizableHandleRect : No ownerItem set. Not drawing any\
handle. Please call setOwnerItem on your ResizableHandleRect subclass";
return;
}
if(ownerItem->isSelected()){
drawHandlesDimension();
handlesAddedToScene = true;
}else{
//Remove the handles
foreach (DPS_HandleItem * handleItem, handleListCenter) {
ownerItem->scene()->removeItem(handleItem);
}
qDeleteAll(handleListCenter);
handleListCenter.clear();
handlesAddedToScene = false;
}
}
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->save();
painter->setPen(pen());
painter->setBrush(brush());
painter->drawPath(starFromRect(boundingRect().adjusted(pen().width(), pen().width(),
-pen().width(),-pen().width())));
painter
->setPen
(QPen(Qt
::green,
1));
painter->drawRect(rect());
drawHandlesDimensionIfNecessary();
painter->restore();
}
void DPS_HandleItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
// QCursor* cursor = nullptr;
switch (handlePosition) {
case dimension:
{
setCursor(Qt::SizeHorCursor);
DPS_ResizeableHandleRect * rectItem = dynamic_cast<DPS_ResizeableHandleRect *>( parentItem());
if(rectItem){
QRectF boundingFrameRect = rectItem->selectorFrameBounds();
boundingFrameRect.setRight(event->pos().x() + 5);
rectItem->setSelectorFrameBounds(boundingFrameRect);
}
}
break;
}
}
void DPS_ResizeableHandleRect::drawHandlesDimension()
{
//Populate handles in list
if(handleListCenter.count() == 0){
// handleListCenter.append(new DPS_HandleItem(DPS_HandleItem::LeftCenter));
handleListCenter.append(new DPS_HandleItem(DPS_HandleItem::dimension));
}
//Set up pen
QPen mPen;
mPen.setWidth(1);
mPen.setColor(Qt::darkGreen);
//Right Center handle
QPointF centerRightCorner = selectorFrameBounds().topRight() + QPointF(-4.5, selectorFrameBounds().height()/2.0 - 4.5);
centerRightHandleRect = QRectF(centerRightCorner,QSize(9,9));
handleListCenter[0]->setRect(centerRightHandleRect);
if(!handleListCenter.isEmpty() && (!handlesAddedToScene)){
handleListCenter[0]->setPen(mPen);
handleListCenter[0]->setBrush(QBrush(Qt::darkGreen));
ownerItem->scene()->addItem(handleListCenter[0]);
handleListCenter[0]->setParentItem(ownerItem);
}
handlesAddedToScene = true;
}
void DPS_ResizeableHandleRect::drawHandlesDimensionIfNecessary()
{
if(!ownerItem){
qWarning() << "ResizableHandleRect : No ownerItem set. Not drawing any\
handle. Please call setOwnerItem on your ResizableHandleRect subclass";
return;
}
if(ownerItem->isSelected()){
drawHandlesDimension();
handlesAddedToScene = true;
}else{
//Remove the handles
foreach (DPS_HandleItem * handleItem, handleListCenter) {
ownerItem->scene()->removeItem(handleItem);
}
qDeleteAll(handleListCenter);
handleListCenter.clear();
handlesAddedToScene = false;
}
}
void D_MeasureSet_Dimension::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->save();
painter->setPen(pen());
painter->setBrush(brush());
painter->drawPath(starFromRect(boundingRect().adjusted(pen().width(), pen().width(),
-pen().width(),-pen().width())));
painter->setPen(QPen(Qt::green,1));
painter->drawRect(rect());
drawHandlesDimensionIfNecessary();
painter->restore();
}
To copy to clipboard, switch view to plain text mode
But I have issue: when I move the dimension to opposite direction (width of rect < 0 ) the dimension line disappeared. If I use method:
rectItem->setSelectorFrameBounds(boundingFrameRect.normalized());
rectItem->setSelectorFrameBounds(boundingFrameRect.normalized());
To copy to clipboard, switch view to plain text mode
The dimension can not move to opposite direction, and the rectangle also was moved.
So, could you help me fix this issue? or you have any idea or other way to do this job, please instruct me.
Bookmarks