valentina_old/tools/vtoolline.cpp
2013-08-05 11:37:56 +03:00

99 lines
3.5 KiB
C++

#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){
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);
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()));
}
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::ChoosedObject);
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);
AddToCalculation(domElement);
}
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));
}