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