valentina/tools/vtoolline.cpp
2013-07-28 01:18:06 +03:00

109 lines
3.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "vtoolline.h"
#include <QMenu>
#include <QDebug>
VToolLine::VToolLine(VDomDocument *doc, VContainer *data, qint64 id, qint64 firstPoint, qint64 secondPoint,
Tool::Enum typeCreation, QGraphicsItem *parent):VAbstractTool(doc, data, id),
QGraphicsLineItem(parent){
connect(this, &VToolLine::FullUpdateTree, this->doc, &VDomDocument::FullUpdateTree);
this->firstPoint = firstPoint;
this->secondPoint = secondPoint;
//Лінія
VPointF first = data->GetPoint(firstPoint);
VPointF second = data->GetPoint(secondPoint);
this->setLine(QLineF(first.toQPointF(), second.toQPointF()));
this->setFlag(QGraphicsItem::ItemStacksBehindParent, true);
this->setFlag(QGraphicsItem::ItemIsSelectable, true);
this->setAcceptHoverEvents(true);
AddLine(firstPoint, secondPoint);
if(typeCreation == Tool::FromGui){
AddToFile();
}
}
void VToolLine::FullUpdateFromFile(){
QDomElement domElement = doc->elementById(QString().setNum(id));
if(domElement.isElement()){
firstPoint = domElement.attribute("firstPoint", "").toLongLong();
secondPoint = domElement.attribute("secondPoint", "").toLongLong();
}
VPointF first = VAbstractTool::data->GetPoint(firstPoint);
VPointF second = VAbstractTool::data->GetPoint(secondPoint);
this->setLine(QLineF(first.toQPointF(), second.toQPointF()));
AddLine(firstPoint, secondPoint);
}
void VToolLine::FullUpdateFromGui(int result){
if(result == QDialog::Accepted){
QDomElement domElement = doc->elementById(QString().setNum(id));
if(domElement.isElement()){
domElement.setAttribute("firstPoint", QString().setNum(dialogLine->getFirstPoint()));
domElement.setAttribute("secondPoint", QString().setNum(dialogLine->getSecondPoint()));
emit FullUpdateTree();
}
}
dialogLine.clear();
}
void VToolLine::ChangedActivDraw(const QString newName){
if(nameActivDraw == newName){
this->setPen(QPen(Qt::black, widthHairLine));
this->setAcceptHoverEvents (true);
VAbstractTool::ChangedActivDraw(newName);
} else {
this->setPen(QPen(Qt::gray, widthHairLine));
this->setAcceptHoverEvents (false);
VAbstractTool::ChangedActivDraw(newName);
}
}
void VToolLine::contextMenuEvent(QGraphicsSceneContextMenuEvent *event){
if(!ignoreContextMenuEvent){
QMenu menu;
QAction *actionOption = menu.addAction("Властивості");
QAction *selectedAction = menu.exec(event->screenPos());
if(selectedAction == actionOption){
dialogLine = QSharedPointer<DialogLine>(new DialogLine(VAbstractTool::data));
connect(qobject_cast< VMainGraphicsScene * >(this->scene()), &VMainGraphicsScene::ChoosedObject,
dialogLine.data(), &DialogLine::ChoosedPoint);
connect(dialogLine.data(), &DialogLine::DialogClosed, this, &VToolLine::FullUpdateFromGui);
dialogLine->setFirstPoint(firstPoint);
dialogLine->setSecondPoint(secondPoint);
dialogLine->show();
}
}
}
void VToolLine::AddToFile(){
QDomElement domElement = doc->createElement("line");
AddAttribute(domElement, "id", id);
AddAttribute(domElement, "firstPoint", firstPoint);
AddAttribute(domElement, "secondPoint", secondPoint);
QDomElement calcElement;
bool ok = doc->GetActivCalculationElement(calcElement);
if(ok){
calcElement.appendChild(domElement);
} else {
qCritical()<<"Не можу знайти тег калькуляції."<< Q_FUNC_INFO;
}
}
void VToolLine::hoverMoveEvent(QGraphicsSceneHoverEvent *event){
Q_UNUSED(event);
this->setPen(QPen(Qt::black, widthMainLine));
}
void VToolLine::hoverLeaveEvent(QGraphicsSceneHoverEvent *event){
Q_UNUSED(event);
this->setPen(QPen(Qt::black, widthHairLine));
}