NCL Composer
0.1
|
00001 #include "qnstgraphicscondition.h" 00002 00003 QnstGraphicsCondition::QnstGraphicsCondition(QnstGraphicsEntity* parent) 00004 : QnstGraphicsEdge(parent) 00005 { 00006 setnstType(Qnst::Condition); 00007 00008 setCondition(Qnst::NoConditionType); 00009 00010 dialog = new QnstGraphicsBindDialog(); 00011 } 00012 00013 QnstGraphicsCondition::~QnstGraphicsCondition() 00014 { 00015 00016 } 00017 00018 QnstCondition QnstGraphicsCondition::getCondition() 00019 { 00020 return condition; 00021 } 00022 00023 void QnstGraphicsCondition::setCondition(QnstCondition condition) 00024 { 00025 this->condition = condition; 00026 00027 switch(condition){ 00028 case Qnst::onBegin: 00029 setConditionIcon(QnstConditionIcon(":/icon/onbegin")); 00030 break; 00031 00032 case Qnst::onEnd: 00033 setConditionIcon(QnstConditionIcon(":/icon/onend")); 00034 break; 00035 00036 case Qnst::onResume: 00037 setConditionIcon(QnstConditionIcon(":/icon/onresume")); 00038 break; 00039 00040 case Qnst::onPause: 00041 setConditionIcon(QnstConditionIcon(":/icon/onpause")); 00042 break; 00043 00044 case Qnst::onSelection: 00045 setConditionIcon(QnstConditionIcon(":/icon/onselection")); 00046 break; 00047 00048 case Qnst::NoConditionType: 00049 setConditionIcon(QnstConditionIcon(":/icon/nocondition")); 00050 break; 00051 } 00052 } 00053 00054 QnstConditionIcon QnstGraphicsCondition::getConditionIcon() 00055 { 00056 return conditionIcon; 00057 } 00058 00059 void QnstGraphicsCondition::setConditionIcon(QnstConditionIcon conditionIcon) 00060 { 00061 this->conditionIcon = conditionIcon; 00062 } 00063 00064 void QnstGraphicsCondition::adjust() 00065 { 00066 if (getEntityA() != NULL && getEntityB() != NULL){ 00067 QLineF line = QLineF(QPointF(getEntityA()->getLeft() + getEntityA()->getWidth()/2, 00068 getEntityA()->getTop() + getEntityA()->getHeight()/2), 00069 QPointF(getEntityB()->getLeft() + getEntityB()->getWidth()/2, 00070 getEntityB()->getTop() + getEntityB()->getHeight()/2)); 00071 00072 if (getEntityA()->getncgType() == Qncg::Interface){ 00073 line.setP1(getnstGraphicsParent()->mapFromItem(getEntityA()->getnstGraphicsParent(), line.p1())); 00074 } 00075 00076 if (getEntityB()->getncgType() == Qncg::Interface){ 00077 line.setP2(getnstGraphicsParent()->mapFromItem(getEntityB()->getnstGraphicsParent(), line.p2())); 00078 } 00079 00080 QPointF pointa = line.p1(); 00081 QPointF pointb = line.p2(); 00082 00083 aux_adjust(pointa, pointb); 00084 00085 getEntityA()->setSelectable(false); 00086 00087 qreal index; 00088 00089 if (pointa != pointb){ 00090 00091 index = 0; 00092 00093 int n = 0; 00094 00095 while(getEntityA()->collidesWithItem(this)){ 00096 index += 0.01; 00097 00098 if (getAngle() == 0) 00099 pointa = line.pointAt(index); 00100 else 00101 pointa = arcPointAt(line , index, false); 00102 00103 aux_adjust(pointa, pointb); 00104 00105 if (++n > 100){ // avoiding infinity loop 00106 break; 00107 } 00108 } 00109 } 00110 00111 getEntityA()->setSelectable(true); 00112 00113 if (scene() != NULL){ 00114 scene()->update(); 00115 } 00116 } 00117 } 00118 00119 void QnstGraphicsCondition::aux_adjust(QPointF pointa, QPointF pointb) 00120 { 00121 if (pointa.x() <= pointb.x() && pointa.y() <= pointb.y()){ 00122 setTop(pointa.y()-4); 00123 setLeft(pointa.x()-4); 00124 setWidth((pointb.x()-4)-(pointa.x()-4) + 8); 00125 setHeight((pointb.y()-4)-(pointa.y()-4) + 8); 00126 00127 }else if (pointa.x() > pointb.x() && pointa.y() < pointb.y()){ 00128 setTop(pointa.y()-4); 00129 setLeft(pointb.x()-4); 00130 setWidth((pointa.x()-4)-(pointb.x()-4) + 8); 00131 setHeight((pointb.y()-4)-(pointa.y()-4) + 8); 00132 00133 }else if (pointa.x() < pointb.x() && pointa.y() > pointb.y()){ 00134 setTop(pointb.y()-4); 00135 setLeft((pointa.x()-4)); 00136 setWidth((pointb.x()-4)-(pointa.x()-4) + 8); 00137 setHeight((pointa.y()-4)-(pointb.y()-4) + 8); 00138 00139 }else if (pointa.x() > pointb.x() && pointa.y() > pointb.y()){ 00140 setTop(pointb.y()-4); 00141 setLeft(pointb.x()-4); 00142 setWidth((pointa.x()-4)-(pointb.x()-4) + 8); 00143 setHeight((pointa.y()-4)-(pointb.y()-4) + 8); 00144 } 00145 } 00146 00147 00148 void QnstGraphicsCondition::draw(QPainter* painter) 00149 { 00150 if (getEntityA() != NULL && getEntityB() != NULL){ 00151 painter->setRenderHint(QPainter::Antialiasing, true); 00152 painter->setRenderHint(QPainter::SmoothPixmapTransform, true); 00153 00154 QLineF line = QLineF(QPointF(getEntityA()->getLeft() + getEntityA()->getWidth()/2, 00155 getEntityA()->getTop() + getEntityA()->getHeight()/2), 00156 QPointF(getEntityB()->getLeft() + getEntityB()->getWidth()/2, 00157 getEntityB()->getTop() + getEntityB()->getHeight()/2)); 00158 00159 if (getEntityA()->getncgType() == Qncg::Interface){ 00160 line.setP1(getnstGraphicsParent()->mapFromItem(getEntityA()->getnstGraphicsParent(), line.p1())); 00161 } 00162 00163 if (getEntityB()->getncgType() == Qncg::Interface){ 00164 line.setP2(getnstGraphicsParent()->mapFromItem(getEntityB()->getnstGraphicsParent(), line.p2())); 00165 } 00166 00167 QPointF pointa = line.p1(); 00168 QPointF pointb = line.p2(); 00169 00170 if (!isInvalid()){ 00171 painter->setPen(QPen(QBrush(QColor("#000000")), 1)); 00172 }else{ 00173 painter->setPen(QPen(QBrush(QColor(255,0,0,200)), 1, Qt::DashLine)); 00174 } 00175 00176 if (pointa.x() <= pointb.x() && pointa.y() <= pointb.y()){ 00177 00178 if (getAngle() != 0){ 00179 qreal drawangle = getAdjAngle(); 00180 00181 QLineF localline(4+8,4+8, 4+getWidth()-4, 4+getHeight()-4); 00182 00183 if (drawangle < 0){ 00184 drawangle = -drawangle; 00185 } 00186 00187 qreal R = localline.length()/(::sin(((drawangle/2)*PI)/180)*2); 00188 00189 qreal delta = (180-drawangle)/2 + (360 - localline.angle()); 00190 00191 QPointF center_a(localline.p2().x() - ::cos((180-delta-drawangle)*PI/180)*R, 00192 localline.p2().y() + ::sin((180-delta-drawangle)*PI/180)*R); 00193 00194 QPointF center_b(localline.p1().x() + ::cos((180-delta-drawangle)*PI/180)*R, 00195 localline.p1().y() - ::sin((180-delta-drawangle)*PI/180)*R); 00196 00197 if (getAdjAngle() < 0){ 00198 painter->drawArc(center_b.x()-R,center_b.y()-R,2*R,2*R, 00199 16*((180-delta-drawangle)+180),16*drawangle); 00200 }else{ 00201 painter->drawArc(center_a.x()-R,center_a.y()-R,2*R,2*R 00202 ,16*(180-delta-drawangle),16*drawangle); 00203 } 00204 00205 }else{ 00206 painter->drawLine(4+8,4+8, 4+getWidth()-4, 4+getHeight()-4); 00207 } 00208 00209 painter->setPen(Qt::NoPen); 00210 00211 painter->drawPixmap(4,4,16,16, QPixmap(conditionIcon)); 00212 00213 if (!isInvalid()){ 00214 painter->setBrush(QBrush(QColor("#000000"))); 00215 }else{ 00216 painter->setBrush(QBrush(QColor(255,0,0,75))); 00217 painter->drawEllipse(3,3, 18, 18); 00218 } 00219 00220 }else if (pointa.x() >= pointb.x() && pointa.y() <= pointb.y()){ 00221 00222 if (getAngle() != 0){ 00223 qreal drawangle = getAdjAngle(); 00224 00225 QLineF localline(4+getWidth()-8,4+8, 4+4, 4+getHeight()-4); 00226 00227 if (drawangle < 0){ 00228 drawangle = -drawangle; 00229 } 00230 00231 qreal R = localline.length()/(::sin(((drawangle/2)*PI)/180)*2); 00232 00233 qreal delta = (180-drawangle)/2 + (360 - localline.angle()); 00234 00235 QPointF center_a(localline.p2().x() - ::cos((180-delta-drawangle)*PI/180)*R, 00236 localline.p2().y() + ::sin((180-delta-drawangle)*PI/180)*R); 00237 00238 QPointF center_b(localline.p1().x() + ::cos((180-delta-drawangle)*PI/180)*R, 00239 localline.p1().y() - ::sin((180-delta-drawangle)*PI/180)*R); 00240 00241 if (getAdjAngle() < 0){ 00242 painter->drawArc(center_b.x()-R,center_b.y()-R,2*R,2*R, 00243 16*((180-delta-drawangle)+180),16*drawangle); 00244 }else{ 00245 painter->drawArc(center_a.x()-R,center_a.y()-R,2*R,2*R 00246 ,16*(180-delta-drawangle),16*drawangle); 00247 } 00248 00249 }else{ 00250 painter->drawLine(4+getWidth()-8,4+8, 4+4, 4+getHeight()-4); 00251 } 00252 00253 painter->setPen(Qt::NoPen); 00254 00255 painter->drawPixmap(4+getWidth()-16,4,16,16, QPixmap(conditionIcon)); 00256 00257 if (!isInvalid()){ 00258 painter->setBrush(QBrush(QColor("#000000"))); 00259 }else{ 00260 painter->setBrush(QBrush(QColor(255,0,0,75))); 00261 painter->drawEllipse(3+getWidth()-16,3, 18, 18); 00262 } 00263 00264 }else if (pointa.x() < pointb.x() && pointa.y() > pointb.y()){ 00265 00266 if (getAngle() != 0){ 00267 qreal drawangle = getAdjAngle(); 00268 00269 QLineF localline(4+8, 4+getHeight()-8, 4+getWidth()-4, 4+4); 00270 00271 if (drawangle < 0){ 00272 drawangle = -drawangle; 00273 } 00274 00275 qreal R = localline.length()/(::sin(((drawangle/2)*PI)/180)*2); 00276 00277 qreal delta = (180-drawangle)/2 + (360 - localline.angle()); 00278 00279 QPointF center_a(localline.p2().x() - ::cos((180-delta-drawangle)*PI/180)*R, 00280 localline.p2().y() + ::sin((180-delta-drawangle)*PI/180)*R); 00281 00282 QPointF center_b(localline.p1().x() + ::cos((180-delta-drawangle)*PI/180)*R, 00283 localline.p1().y() - ::sin((180-delta-drawangle)*PI/180)*R); 00284 00285 if (getAdjAngle() < 0){ 00286 painter->drawArc(center_b.x()-R,center_b.y()-R,2*R,2*R, 00287 16*((180-delta-drawangle)+180),16*drawangle); 00288 }else{ 00289 painter->drawArc(center_a.x()-R,center_a.y()-R,2*R,2*R 00290 ,16*(180-delta-drawangle),16*drawangle); 00291 } 00292 00293 }else{ 00294 painter->drawLine(4+8, 4+getHeight()-8, 4+getWidth()-4, 4+4); 00295 } 00296 00297 painter->setPen(Qt::NoPen); 00298 00299 painter->drawPixmap(4, 4+getHeight()-16, 16, 16, QPixmap(conditionIcon)); 00300 00301 if (!isInvalid()){ 00302 painter->setBrush(QBrush(QColor("#000000"))); 00303 }else{ 00304 painter->setBrush(QBrush(QColor(255,0,0,75))); 00305 painter->drawEllipse(3, 3+getHeight()-16, 18, 18); 00306 } 00307 00308 }else if (pointa.x() > pointb.x() && pointa.y() > pointb.y()){ 00309 00310 if (getAngle() != 0){ 00311 qreal drawangle = getAdjAngle(); 00312 00313 QLineF localline(4+getWidth()-8, 4+getHeight()-8, 4+4, 4+4); 00314 00315 if (drawangle < 0){ 00316 drawangle = -drawangle; 00317 } 00318 00319 qreal R = localline.length()/(::sin(((drawangle/2)*PI)/180)*2); 00320 00321 qreal delta = (180-drawangle)/2 + (360 - localline.angle()); 00322 00323 QPointF center_a(localline.p2().x() - ::cos((180-delta-drawangle)*PI/180)*R, 00324 localline.p2().y() + ::sin((180-delta-drawangle)*PI/180)*R); 00325 00326 QPointF center_b(localline.p1().x() + ::cos((180-delta-drawangle)*PI/180)*R, 00327 localline.p1().y() - ::sin((180-delta-drawangle)*PI/180)*R); 00328 00329 if (getAdjAngle() < 0){ 00330 painter->drawArc(center_b.x()-R,center_b.y()-R,2*R,2*R, 00331 16*((180-delta-drawangle)+180),16*drawangle); 00332 }else{ 00333 painter->drawArc(center_a.x()-R,center_a.y()-R,2*R,2*R 00334 ,16*(180-delta-drawangle),16*drawangle); 00335 } 00336 00337 }else{ 00338 painter->drawLine(4+getWidth()-8, 4+getHeight()-8, 4+4, 4+4); 00339 } 00340 00341 painter->setPen(Qt::NoPen); 00342 00343 painter->drawPixmap(4+getWidth()-16, 4+getHeight()-16, 16, 16, QPixmap(conditionIcon)); 00344 00345 if (!isInvalid()){ 00346 painter->setBrush(QBrush(QColor("#000000"))); 00347 }else{ 00348 painter->setBrush(QBrush(QColor(255,0,0,75))); 00349 painter->drawEllipse(3+getWidth()-16, 3+getHeight()-16, 18, 18); 00350 } 00351 } 00352 } 00353 } 00354 00355 void QnstGraphicsCondition::delineate(QPainterPath* painter) const 00356 { 00357 if (getEntityA() != NULL && getEntityB() != NULL){ 00358 QLineF line = QLineF(QPointF(getEntityA()->getLeft() + getEntityA()->getWidth()/2, 00359 getEntityA()->getTop() + getEntityA()->getHeight()/2), 00360 QPointF(getEntityB()->getLeft() + getEntityB()->getWidth()/2, 00361 getEntityB()->getTop() + getEntityB()->getHeight()/2)); 00362 00363 if (getEntityA()->getncgType() == Qncg::Interface){ 00364 line.setP1(getnstGraphicsParent()->mapFromItem(getEntityA()->getnstGraphicsParent(), line.p1())); 00365 } 00366 00367 if (getEntityB()->getncgType() == Qncg::Interface){ 00368 line.setP2(getnstGraphicsParent()->mapFromItem(getEntityB()->getnstGraphicsParent(), line.p2())); 00369 } 00370 00371 QPointF pointa = line.p1(); 00372 QPointF pointb = line.p2(); 00373 00374 if (pointa.x() <= pointb.x() && pointa.y() <= pointb.y()){ 00375 // painter->drawLine(4+4,4+4, 4+4+getWidth()-8-2, 4+4+getHeight()-8-2); 00376 00377 painter->addEllipse(4,4,16,16); 00378 00379 }else if (pointa.x() > pointb.x() && pointa.y() < pointb.y()){ 00380 // painter->drawLine(4+4+getWidth()-8,4+4, 4+4, 4+4+getHeight()-8); 00381 00382 painter->addEllipse(4+getWidth()-16,4,16,16); 00383 00384 00385 }else if (pointa.x() < pointb.x() && pointa.y() > pointb.y()){ 00386 // painter->drawLine(4+4, 4+4+getHeight()-8, 4+4+getWidth()-8, 4+4); 00387 00388 painter->addEllipse(4, 4+getHeight()-16, 16, 16); 00389 00390 }else if (pointa.x() > pointb.x() && pointa.y() > pointb.y()){ 00391 // painter->drawLine(4+4+getWidth()-8, 4+4+getHeight()-8, 4+4, 4+4); 00392 00393 painter->addEllipse(4+getWidth()-16, 4+getHeight()-16, 16, 16); 00394 } 00395 } 00396 } 00397 00398 void QnstGraphicsCondition::setConn(QnstConncetor* conn) 00399 { 00400 this->conn = conn; 00401 } 00402 00403 void QnstGraphicsCondition::addParam(QString uid, QString name, QString value) 00404 { 00405 if (name != ""){ 00406 params[name] = value; 00407 name_uid[name] = uid; 00408 00409 emit bindParamUpdated(getnstUid() ,params, name_uid); 00410 } 00411 } 00412 00413 void QnstGraphicsCondition::setParam(QString name, QString value) 00414 { 00415 if (name != ""){ 00416 params[name] = value; 00417 00418 emit bindParamUpdated(getnstUid() ,params, name_uid); 00419 } 00420 } 00421 00422 void QnstGraphicsCondition::removeParam(QString name) 00423 { 00424 if (name != ""){ 00425 params.remove(name); 00426 name_uid.remove(name); 00427 00428 emit bindParamUpdated(getnstUid() ,params, name_uid); 00429 } 00430 } 00431 00432 void QnstGraphicsCondition::setParams(QMap<QString, QString> params) 00433 { 00434 this->params = params; 00435 } 00436 00437 void QnstGraphicsCondition::setNameUids(QMap<QString, QString> nameUids) 00438 { 00439 this->name_uid = nameUids; 00440 } 00441 00442 void QnstGraphicsCondition::removeUId(QString uid) 00443 { 00444 QString name = name_uid.key(uid); 00445 00446 if (params.contains(name)){ 00447 params.remove(name); 00448 name_uid.remove(name); 00449 00450 emit bindParamUpdated(getnstUid() ,params, name_uid); 00451 } 00452 } 00453 00454 void QnstGraphicsCondition::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) 00455 { 00456 00457 if (conn != NULL){ 00458 QList<QString> names = conn->getParams().values(); 00459 00460 QMap<QString, QString> values; 00461 00462 foreach(QString name, names){ 00463 values[name] = params[name]; 00464 00465 if (!name_uid.contains(name)){ 00466 name_uid[name] = QUuid::createUuid().toString(); 00467 } 00468 } 00469 00470 dialog->init(values); 00471 00472 if (dialog->exec()){ 00473 params = dialog->getProperties(); 00474 00475 foreach(QString name, params.keys()){ 00476 QMap<QString, QString> p; 00477 00478 p["name"] = name; 00479 p["value"] = params[name]; 00480 00481 emit bindParamAdded(name_uid[name], getnstUid(), p); 00482 } 00483 00484 emit bindParamUpdated(getnstUid() ,params, name_uid); 00485 } 00486 } 00487 }