1 #include "qnstgraphicscomposition.h"
3 #include "qnstgraphicsmedia.h"
4 #include "qnstgraphicscontext.h"
12 setnstType(Qnst::Compostion);
31 setTop(parent->getHeight()/2 - DEFAULT_CONTEXT_HEIGHT/2);
32 setLeft(parent->getWidth()/2 - DEFAULT_CONTEXT_WIDTH/2);
40 setWidth(DEFAULT_CONTEXT_WIDTH);
41 setHeight(DEFAULT_CONTEXT_HEIGHT);
44 QnstGraphicsComposition::~QnstGraphicsComposition()
49 QString QnstGraphicsComposition::getColor()
const
54 void QnstGraphicsComposition::setColor(QString color)
59 void QnstGraphicsComposition::setCollapsed(
bool collapsed)
61 this->collapsed = collapsed;
64 bool QnstGraphicsComposition::isCollapsed()
69 void QnstGraphicsComposition::updateToolTip()
72 QString name = (getnstId() !=
"" ? getnstId() :
"?");
74 if (getnstType() == Qnst::Context)
76 tip +=
"Context ("+name+
")";
78 else if (getnstType() == Qnst::Switch)
80 tip +=
"Switch ("+name+
")";
82 else if (getnstType() == Qnst::Body)
84 tip +=
"Body ("+name+
")";
88 tip +=
"Composition ("+name+
")";
92 tip +=
" - Error: " + erroMsg;
97 void QnstGraphicsComposition::draw(QPainter* painter)
108 painter->setRenderHint(QPainter::Antialiasing,
true);
110 painter->setBrush(QColor(color));
113 painter->setPen(QPen(QBrush(Qt::red), 2));
115 painter->setPen(QPen(QBrush(Qt::black), 0));
117 painter->drawEllipse(4, 4, getWidth()-1, getHeight()-1);
121 painter->setBrush(Qt::NoBrush);
122 painter->setPen(QPen(QBrush(Qt::black), 0));
124 painter->drawEllipse(getMoveLeft()+4-getLeft(), getMoveTop()+4-getTop(), getWidth()-1, getHeight()-1);
127 else if (isResizing())
129 painter->setBrush(Qt::NoBrush);
130 painter->setPen(QPen(QBrush(Qt::black), 0));
132 painter->drawEllipse(getResizeLeft()+4-getLeft(), getResizeTop()+4-getTop(), getResizeWidth()-1, getResizeHeight()-1);
137 painter->setRenderHint(QPainter::SmoothPixmapTransform,
true);
139 painter->drawPixmap(4 + 8/2, 4 + 8/2, getWidth()-8, getHeight()-16-8, QPixmap(
":/icon/context"));
141 if (!isSelected() && hasMouseHover())
143 painter->setBrush(Qt::NoBrush);
144 painter->setPen(QPen(QBrush(QColor(
"#999999")), 0, Qt::DashLine));
146 painter->drawRect(4, 4, getWidth(), getHeight());
149 painter->setPen(QPen(QBrush(Qt::black),0));
151 QString localid = (getnstId() !=
"" ? getnstId() :
"?");
153 if (localid.length() > 5)
155 localid = getnstId().replace(3,getnstId().length()-3,
"...");
158 painter->drawText(4 + 8/2, 4 + 8/2 + getHeight()-16-8, getWidth()-8, 16, Qt::AlignCenter, localid);
162 painter->setBrush(Qt::NoBrush);
163 painter->setPen(QPen(QBrush(Qt::black), 0));
165 painter->setRenderHint(QPainter::Antialiasing,
false);
166 painter->drawRect(getMoveLeft()+4-getLeft(), getMoveTop()+4-getTop(), getWidth()-1, getHeight()-1);
171 void QnstGraphicsComposition::delineate(QPainterPath* painter)
const
173 painter->addEllipse(4, 4, getWidth(), getHeight());
177 void QnstGraphicsComposition::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
179 foreach(QUrl url, event->mimeData()->urls())
181 event->acceptProposedAction();
187 void QnstGraphicsComposition::collapse()
189 mouseDoubleClickEvent(NULL);
192 void QnstGraphicsComposition::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
202 setTop(getTop() - (lasth/2 - DEFAULT_MEDIA_HEIGHT/2));
203 setLeft(getLeft() - (lastw/2 - DEFAULT_MEDIA_WIDTH/2));
209 if (e->getncgType() == Qncg::Interface)
211 e->setTop(((e->getTop()*lasth)/DEFAULT_MEDIA_HEIGHT));
212 e->setLeft(((e->getLeft()*lastw)/DEFAULT_MEDIA_WIDTH));
215 e->setnstGraphicsParent(
this);
219 if (e->getncgType() == Qncg::Interface)
221 qDebug() << e->getLeft() << e->getTop() << e->getWidth() << e->getHeight();
229 tmp->setTop(getTop());
230 tmp->setLeft(getLeft());
231 tmp->setWidth(getWidth());
232 tmp->setHeight(getHeight());
239 if (e->getncgType() != Qncg::Interface)
242 e->setnstGraphicsParent(tmp);
246 setTop(getTop() + lasth/2 - DEFAULT_MEDIA_HEIGHT/2);
247 setLeft(getLeft() + lastw/2 - DEFAULT_MEDIA_WIDTH/2);
248 setWidth(DEFAULT_MEDIA_WIDTH);
249 setHeight(DEFAULT_MEDIA_HEIGHT);
253 if (e->getncgType() == Qncg::Interface)
255 e->setTop(((e->getTop()*DEFAULT_MEDIA_HEIGHT)/lasth));
256 e->setLeft(((e->getLeft()*DEFAULT_MEDIA_WIDTH)/lastw));
265 if (getnstGraphicsParent() != NULL)
267 getnstGraphicsParent()->adjust();
270 setCollapsed(!collapsed);
273 void QnstGraphicsComposition::dropEvent(QGraphicsSceneDragDropEvent *event)
275 foreach(QUrl url, event->mimeData()->urls())
277 QString filename = url.toLocalFile();
278 QString suffix = QFileInfo(filename).suffix().toLower();
280 event->acceptProposedAction();
287 void QnstGraphicsComposition::adjustWithSpring()
291 while(it++ < SPRING_INTERATION)
293 int N = getnstGraphicsEntities().size();
299 for (
int I = 0; I < N; I++)
308 if (entity->getncgType() == Qncg::Node)
323 if (edge->getEntityA() != node)
325 entitya = edge->getEntityB();
326 entityb = edge->getEntityA();
331 entitya = edge->getEntityA();
332 entityb = edge->getEntityB();
337 line.setP1(QPointF(entitya->getLeft() + entitya->getWidth()/2,
338 entitya->getTop() + entitya->getHeight()/2));
340 line.setP2(QPointF(entityb->getLeft() + entityb->getWidth()/2,
341 entityb->getTop() + entityb->getHeight()/2));
343 qreal d = line.length();
349 attr = ((d - SPRING_LENGTH) > 0 ? 1 : -1);
351 cforce = SPRING_CONSTANT*(d - SPRING_LENGTH >= 0 ? d - SPRING_LENGTH : (d - SPRING_LENGTH)*(-1));
352 cangle = line.angle();
354 qreal fxa = rforce*cos(rangle*(PI / 180));
355 qreal fya = rforce*sin(rangle*(PI / 180));
356 qreal fxb = cforce*cos(cangle*(PI / 180));
357 qreal fyb = cforce*sin(cangle*(PI / 180));
359 qreal fxr = fxa + fxb;
360 qreal fyr = fya + fyb;
362 rforce = sqrt(pow(fxr,2) + pow(fyr,2));
368 else if (fabs(fxr) > fabs(fyr))
370 rangle = ((180 / PI)*tanh(fyr/fxr));
374 rangle = 90 - ((180 / PI)*tanh(fxr/fyr));
381 else if (fyr < 0 && rangle < 0)
387 qreal fxa = vf[I]*cos(va[I]*(PI / 180));
388 qreal fya = vf[I]*sin(va[I]*(PI / 180));
389 qreal fxb = rforce*cos(rangle*(PI / 180));
390 qreal fyb = rforce*sin(rangle*(PI / 180));
392 qreal fxr = fxa + fxb;
393 qreal fyr = fya + fyb;
395 rforce = sqrt(pow(fxr,2) + pow(fyr,2));
397 vf[I] = rforce*SPRING_DAMPING;
403 else if (fabs(fxr) > fabs(fyr))
405 rangle = ((180 / PI)*tanh(fyr/fxr));
409 rangle = 90 - ((180 / PI)*tanh(fxr/fyr));
416 else if (fyr < 0 && rangle < 0)
423 qreal rx = vf[I]*cos(va[I]*(PI / 180));
424 qreal ry = vf[I]*sin(va[I]*(PI / 180));
428 next[I] = QPointF(entity->getLeft() + rx*attr, entity->getTop() - ry*attr);
430 else if (rangle < 180)
432 next[I] = QPointF(entity->getLeft() + rx*attr, entity->getTop() - ry*attr);
434 else if (rangle < 270)
436 next[I] = QPointF(entity->getLeft() + rx*attr, entity->getTop() - ry*attr);
440 next[I] = QPointF(entity->getLeft() + rx*attr, entity->getTop() - ry*attr);
449 entity->setLeft(next[I].x());
450 entity->setTop(next[I].y());
457 qreal QnstGraphicsComposition::getLastW()
462 qreal QnstGraphicsComposition::getLastH()
467 void QnstGraphicsComposition::setLastW(qreal lastW)
472 void QnstGraphicsComposition::setLastH(qreal lastH)
477 void QnstGraphicsComposition::setProperties(
const QMap<QString, QString> &props)
479 QnstGraphicsNode::setProperties(props);
481 if (props[
"collapsed"] !=
"")
483 if (props[
"collapsed"] ==
"1")
485 if (props[
"width"] !=
"")
486 setLastW(props[
"width"].toDouble());
488 if (props[
"height"] !=
"")
489 setLastH(props[
"height"].toDouble());
491 if (props[
"expandHeight"] !=
"")
492 setHeight(props[
"expandHeight"].toDouble());
494 if (props[
"expandWidth"] !=
"")
495 setWidth(props[
"expandWidth"].toDouble());
499 if (props[
"width"] !=
"")
500 setWidth(props[
"width"].toDouble());
502 if (props[
"height"] !=
"")
503 setHeight(props[
"height"].toDouble());
505 if (props[
"expandHeight"] !=
"")
506 setLastH(props[
"expandHeight"].toDouble());
508 if (props[
"expandWidth"] !=
"")
509 setLastW(props[
"expandWidth"].toDouble());
514 void QnstGraphicsComposition::getProperties(QMap<QString, QString> &properties)
516 QnstGraphicsNode::getProperties(properties);
518 properties[
"collapsed"] = QString::number(isCollapsed());
519 properties[
"expandWidth"] = QString::number(getWidth());
520 properties[
"expandHeight"] = QString::number(getHeight());