• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

C:/CVUT/diplomka/Automata_editor/sources/transitionArc.cpp

Go to the documentation of this file.
00001 #include "constants.h"
00002 
00003 #include "transitionArc.h"
00004 #include "state.h"
00005 #include "label.h"
00006 #include "transforms.h"
00007 
00008 #include <math.h>
00009 
00010 #include <QtDebug>
00011 
00012 TransitionArc::TransitionArc(Editor *parent, State *ss, State *es,
00013                              const QString &labelText, bool leftO,
00014                              bool dimmed, bool radius)
00015 :   TwoStatesTransition(parent, ss, es, leftO, dimmed), radius(radius)
00016 {
00017   setZValue(Z_TRANSITION);
00018   label = new LabelX(this, labelText, leftOriented, m_labelFontSize,
00019         dimmed ? QColor(dimEdgeLabelColor) : QColor(edgeLabelColor));
00020   label->setZValue(Z_TR_LABEL);
00021   label->setPosParam(DEF_ARC_LAB_POS);
00022   adjust();
00023   setLabelPosition();
00024 }
00025 
00026 TransitionArc::~TransitionArc()
00027 {
00028     DBGLOG("called");
00029 }
00030 
00031 void TransitionArc::adjust()
00032 {
00033     prepareGeometryChange();
00034     if (!startState || !endState) return;       
00035     
00036     startPoint = startState->pos();
00037     endPoint = endState->pos();
00038     
00039     QLineF s_v(startPoint, endPoint);
00040     float angle = s_v.angle(QLineF(0,0,1,0));
00041     if (startPoint.y() < endPoint.y()){
00042         angle *= -1;
00043     }
00044 
00045     QPolygonF anglePol, statePol, iPol;
00046     int arcangle = radius ? DEF_ARC_ANGLE : DEF_ARC_ANGLE/2;
00047 
00048     if (!leftOriented) arcangle *= -1; // difference from VArc
00049 
00050     anglePol << startState->pos() << startState->pos() + 
00051                 QPointF(cos(M_PI/180*(arcangle+angle)), -sin(M_PI/180*(arcangle+angle)))*startState->getWidth();
00052     statePol << mapFromItem(startState, startState->getMyPolygon());
00053     iPol = (findIntersectedPoints(anglePol, statePol, eFIND_FIRST));
00054     if (iPol.empty())
00055     {
00056         qWarning("TransitionVArc::adjust -> startPoint is set to startState->pos()!");
00057         startPoint = startState->pos();
00058     }
00059     else
00060     {
00061         startPoint = iPol[0];
00062     }
00063 
00064     anglePol.clear();
00065     statePol.clear();
00066     anglePol << endState->pos() << endState->pos() +
00067                 QPointF(cos(M_PI/180*(180-arcangle+angle)), -sin(M_PI/180*(180-arcangle+angle)))*endState->getWidth();
00068     statePol << mapFromItem(endState, endState->getMyPolygon());
00069     iPol = (findIntersectedPoints(anglePol, statePol, eFIND_FIRST));
00070     if (iPol.empty())
00071     {
00072         qWarning("TransitionVArc::adjust -> endPoint is set to endState->pos()!");
00073         endPoint  = endState->pos();
00074     }
00075     else
00076     {
00077         endPoint = iPol[0];
00078     }    
00079     
00080     if (startPoint == endPoint) {
00081         setLabelPosition();
00082         return;
00083     }
00084   
00085     QPainterPath path;
00086   
00087     s_v.setPoints(startPoint, endPoint);
00088     float len = s_v.length()/2;
00089 
00090     angle = s_v.angle(QLineF(0,0,1,0));
00091     if (startPoint.y() < endPoint.y()){
00092         angle *= -1;
00093     }
00094   
00095     float angleA, angleB; // left or right orientation
00096     angleA = arcangle + angle;
00097     angleB = 180 - arcangle + angle; // the same angle as angelA, to seems like arc
00098   
00099     float x,y;  // compute vector form start point
00100     x = cos(M_PI/180*(angleA));
00101     y = sin(M_PI/180*(angleA)); // compute new unitVector
00102     QPointF point = QPointF(x,-y); 
00103     point += startPoint; // place to startPoint
00104 
00105     QLineF a_v(startPoint,point); // vector from startPoint
00106   
00107     // now compute vector from end point
00108     x = cos(M_PI/180*(angleB));
00109     y = sin(M_PI/180*(angleB)); // compute new unitVector
00110     point = QPointF(x,-y); 
00111     point += endPoint; // place to endPoint
00112       
00113     QLineF b_v(s_v.p2(),point); // vector from endPoint
00114     
00115     // set length by ncurv
00116     float current_ncurv = len * DEF_ARC_CURV; // experiment
00117     a_v.setLength(current_ncurv);
00118     b_v.setLength(current_ncurv);
00119 
00120     a_point = a_v.p2(); b_point = b_v.p2();
00121     path.moveTo(startPoint);
00122     path.cubicTo(a_point, b_point, endPoint);
00123     
00124     p = path;
00125 
00126     QList<QPolygonF> pathList = p.toSubpathPolygons();
00127     if (pathList.size() > 1)
00128         RELLOG("not only one subpath polygon!");
00129 
00130     iPol = pathList[0];
00131     iPol << endState->pos();
00132     pa = getArrowPolygon(p);
00133         
00134     setLabelPosition();
00135     createStrokes(p);
00136 }
00137 
00138 void TransitionArc::setLabelPosition()
00139 {
00140     if (label)
00141     {
00142         Transition::setLabelPosition(label, label->posParam(), leftOriented, label->getWidth(), label->getHeight());
00143     }
00144     Transition::setLabelPosition(); 
00145 }
00146 
00147 QString TransitionArc::getTypeName() const
00148 {
00149     return radius ? "LArc" : "Arc";
00150 }
00151 
00152 /*void TransitionArc::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
00153 {
00154     if (startPoint == endPoint) return;
00155   
00156     QColor lineC;   
00157     Qt::PenStyle lineS;
00158 
00159     if (dimmed)
00160     {
00161         lineS = dimEdgeLineStyle;
00162         lineC = dimEdgeLineColor;
00163     }
00164     else
00165     {
00166         lineS = edgeLineStyle;
00167         lineC = edgeLineColor;
00168     }
00169   
00170     if (checked)
00171     {
00172         painter->setPen(QPen(checkedColor, m_lineWidth, lineS));
00173     } 
00174     else
00175     {
00176         painter->setPen(QPen(lineC, m_lineWidth, lineS));
00177     }
00178   
00179     painter->setBrush(Qt::NoBrush);
00180     painter->strokePath(p, painter->pen());
00181 
00182     // draw arrow
00183     if (checked)
00184         painter->setBrush(checkedColor);
00185     else
00186         painter->setBrush(lineC);
00187       
00188     painter->drawPolygon(pa); // computed in adjust
00189 
00190     paintSelectionDecoration(painter);
00191 
00192 #ifdef TESTING_PAINTING
00193 #   ifdef TESTING_BOUNDING_RECT_PAINTING
00194         painter->setBrush(QBrush(QColor(50,255,0,100)));
00195         painter->fillRect(boundingRect(), painter->brush());
00196 #   endif
00197 
00198     painter->setBrush(QBrush(QColor(0,0,255,150)));
00199     painter->fillPath(shape(), painter->brush());    
00200 #endif
00201 }*/
00202 
00203 QString TransitionArc::getVCCommand() const
00204 {
00205     QString command = "\\";
00206     
00207     command += getTypeName();
00208     command += getTypeNameSuffix();
00209 
00210     Q_ASSERT(label != NULL);
00211     if (label->posParam() != DEF_ARC_LAB_POS)
00212         command += QString("[%1]").arg(label->posParam());
00213 
00214     command += "{" + startState->getName() + "}";
00215     command += "{" + endState->getName() + "}";
00216 
00217     command += "{" + label->text() + "}";
00218 
00219     command += getNextLabelsVCCommand();
00220 
00221     return command;
00222 }
00223 
00224 /*
00225 QString TransitionArc::getEPS() const
00226 {
00227   QString s = "";
00228   
00229   QColor lineC;
00230   Qt::PenStyle lineS;
00231   
00232   int r, g, b; float rf, gf, bf;
00233   
00234   if (dimmed)
00235   {
00236     lineS = dimEdgeLineStyle;
00237     lineC = dimEdgeLineColor;
00238   }
00239   else
00240   {
00241     lineS = edgeLineStyle;
00242     lineC = edgeLineColor;
00243   }
00244   
00245   lineC.getRgb(&r,&g,&b);
00246   rf = r / 255.; gf = g / 255.; bf = b / 255.;
00247   // % r g b width x0 y0 x1 y1 x2 y2 x y trArc
00248   s += trLineStyle(lineS);
00249   s += QString(" %1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11 %12 trArc\n")
00250          .arg(rf).arg(gf).arg(bf)
00251          .arg(m_lineWidth)
00252          .arg(a_point.x()).arg(scene()->height()-2 - a_point.y())
00253          .arg(b_point.x()).arg(scene()->height()-2 - b_point.y())
00254          .arg(endPoint.x()).arg(scene()->height()-2 - endPoint.y())
00255          .arg(startPoint.x()).arg(scene()->height()-2 - startPoint.y());
00256   
00257   s += Transition::getEPS();   // finally get label command if some
00258   
00259   return s;
00260 }
00261 */

Generated on Tue Jan 4 2011 03:03:24 for Autoamata editor by  doxygen 1.7.0