Ignore:
Timestamp:
2012-10-20T20:27:24+02:00 (12 years ago)
Author:
adeas31
Message:
  • Removed some old classes.
  • Better connection support for array type connectors.
  • Don't call getClassNames every time for searching of Modelica classes.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/OMEdit/OMEditGUI/TextAnnotation.cpp

    r13405 r13537  
    3636#include "TextAnnotation.h"
    3737
    38 TextAnnotation_OMC::TextAnnotation_OMC(QString annotation, Component_OMC *pParent)
    39   : ShapeAnnotation_OMC(pParent), mpComponent(pParent)
     38TextAnnotation::TextAnnotation(QString annotation, Component *pParent)
     39  : ShapeAnnotation(pParent), mpComponent(pParent)
    4040{
    4141  parseShapeAnnotation(annotation);
    4242}
    4343
    44 void TextAnnotation_OMC::parseShapeAnnotation(QString annotation)
     44void TextAnnotation::parseShapeAnnotation(QString annotation)
    4545{
    4646  GraphicItem::parseShapeAnnotation(annotation);
     
    112112}
    113113
    114 QRectF TextAnnotation_OMC::boundingRect() const
     114QRectF TextAnnotation::boundingRect() const
    115115{
    116116  return shape().boundingRect();
    117117}
    118118
    119 QPainterPath TextAnnotation_OMC::shape() const
     119QPainterPath TextAnnotation::shape() const
    120120{
    121121  QPainterPath path;
     
    124124}
    125125
    126 void TextAnnotation_OMC::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
     126void TextAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    127127{
    128128  Q_UNUSED(option);
     
    234234}
    235235
    236 void TextAnnotation_OMC::checkNameString()
    237 {
    238   /* the name of the component can be present in any inherited class . So we start with the main class and then
    239        move up the hierarchy looking for the name.
    240     */
    241   if (mTextString.contains("%name"))
    242   {
    243     if (!mpComponent->getName().isEmpty())
    244       mTextString = mpComponent->getName();
    245     else
    246     {
    247       Component_OMC *pComponent = mpComponent;
    248       while (pComponent->getParentComponent())
    249       {
    250         pComponent = pComponent->getParentComponent();
    251         if (!pComponent->getName().isEmpty())
    252         {
    253           mTextString = pComponent->getName();
    254           break;
    255         }
    256       }
    257     }
    258   }
    259 }
    260 
    261 
    262 
    263 
    264 TextAnnotation::TextAnnotation(QString shape, Component *pParent)
    265   : ShapeAnnotation(pParent), mpComponent(pParent)
    266 {
    267   initializeFields();
    268   mFontWeight = QFont::Normal;
    269   mFontItalic = false;
    270   mFontName = StringHandler::getDefaultSystemFont().family();
    271   mHorizontalAlignment = Qt::AlignCenter;
    272   mFontUnderLine = false;
    273   mFontSize = 0;
    274   mCalculatedFontSize = 1;
    275   setLetterSpacing(0);
    276   parseShapeAnnotation(shape, mpComponent->getOMCProxy());
    277 }
    278 
    279 TextAnnotation::TextAnnotation(GraphicsView *graphicsView, QGraphicsItem *pParent)
    280   : ShapeAnnotation(graphicsView, pParent)
    281 {
    282   // initialize all fields with default values
    283   initializeFields();
    284   mFontWeight = QFont::Normal;
    285   mFontItalic = false;
    286   mFontName = StringHandler::getDefaultSystemFont().family();
    287   mHorizontalAlignment = Qt::AlignCenter;
    288   mFontUnderLine = false;
    289   mFontSize = 0;
    290   mCalculatedFontSize = 1;
    291   setLetterSpacing(0);
    292   mTextString = QString("Text Here");
    293   mIsCustomShape = true;
    294   mpComponent = 0;
    295   setAcceptHoverEvents(true);
    296   connect(this, SIGNAL(updateShapeAnnotation()), mpGraphicsView, SLOT(addClassAnnotation()));
    297   connect(this, SIGNAL(extentChanged()), SLOT(calculateFontSize()));
    298 }
    299 
    300 TextAnnotation::TextAnnotation(QString shape, GraphicsView *graphicsView, QGraphicsItem *pParent)
    301   : ShapeAnnotation(graphicsView, pParent)
    302 {
    303   // initialize all fields with default values
    304   initializeFields();
    305   mFontWeight = QFont::Normal;
    306   mFontItalic = false;
    307   mFontName = StringHandler::getDefaultSystemFont().family();
    308   mHorizontalAlignment = Qt::AlignCenter;
    309   mFontUnderLine = false;
    310   mIsCustomShape = true;
    311   mFontSize = 0;
    312   mCalculatedFontSize = 1;
    313   setLetterSpacing(0);
    314   mpComponent = 0;
    315   connect(this, SIGNAL(extentChanged()), SLOT(calculateFontSize()));
    316   parseShapeAnnotation(shape, mpGraphicsView->getModelWidget()->getModelWidgetContainer()->getMainWindow()->getOMCProxy());
    317   emit extentChanged();
    318   setAcceptHoverEvents(true);
    319   connect(this, SIGNAL(updateShapeAnnotation()), mpGraphicsView, SLOT(addClassAnnotation()));
    320 }
    321 
    322 QRectF TextAnnotation::boundingRect() const
    323 {
    324   return shape().boundingRect();
    325 }
    326 
    327 QPainterPath TextAnnotation::shape() const
    328 {
    329   QPainterPath path;
    330   path.addRoundedRect(getBoundingRect(), mCornerRadius, mCornerRadius);
    331   return path;
    332 }
    333 
    334 void TextAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    335 {
    336   Q_UNUSED(option);
    337   Q_UNUSED(widget);
    338   if (transformOriginPoint() != boundingRect().center())
    339   {
    340     setTransformOriginPoint(boundingRect().center());
    341   }
    342   QPen pen(mLineColor, mThickness, mLinePattern);
    343   pen.setCosmetic(true);
    344   painter->setPen(pen);
    345   //painter->setBrush(QBrush(mFillColor, Qt::SolidPattern));
    346   // create the font object
    347   QFont font;
    348   qreal calculatedFontSize;
    349   if (mFontSize == 0)
    350   {
    351     float factor = sceneBoundingRect().height() / painter->fontMetrics().boundingRect(mTextString).height();
    352     calculatedFontSize = painter->font().pointSizeF() * factor;
    353     if (calculatedFontSize < 18.0)
    354       calculatedFontSize = 18.0;
    355     font = QFont(mFontName, calculatedFontSize, mFontWeight, mFontItalic);
    356   }
    357   else
    358     font = QFont(mFontName, mFontSize, mFontWeight, mFontItalic);
    359   // set font underline
    360   if(mFontUnderLine)
    361     font.setUnderline(true);
    362   // set letter spacing for the font
    363   setLetterSpacingOnFont(&font, getLetterSpacing());
    364   painter->setFont(font);
    365   // draw the font
    366   // first we invert the painter since we have our coordinate system inverted.
    367   painter->scale(1.0, -1.0);
    368   painter->translate(0, ((-boundingRect().top()) - boundingRect().bottom()));
    369 
    370   if (sceneTransform().m11() < 0)
    371   {
    372     painter->scale(-1.0, 1.0);
    373     painter->translate(((-boundingRect().left()) - boundingRect().right()), 0);
    374   }
    375   if (sceneTransform().m22() < 0)
    376   {
    377     painter->scale(1.0, -1.0);
    378     painter->translate(0, ((-boundingRect().top()) - boundingRect().bottom()));
    379   }
    380   qreal radian = atan2(sceneTransform().m12(), sceneTransform().m11());
    381   qreal angle = (180 * radian) / M_PI;
    382   if (angle == 180 && angle == -180)
    383   {
    384     painter->scale(-1.0, -1.0);
    385     painter->translate(((-boundingRect().left()) - boundingRect().right()), ((-boundingRect().top()) - boundingRect().bottom()));
    386   }
    387   if (angle == 90 && angle == -90)
    388   {
    389     painter->scale(1.0, -1.0);
    390     painter->translate(0, ((-boundingRect().top()) - boundingRect().bottom()));
    391   }
    392   //  if (mpComponent)
    393   //  {
    394   //    // get the root component to check the rotation and transformations.
    395   //    Component *pComponent = mpComponent->getRootParentComponent();
    396   //    pComponent = mpComponent->getRootParentComponent(true);
    397   //    if (pComponent->rotation() == -180 || pComponent->rotation() == 180)
    398   //    {
    399   //      painter->scale(-1.0, -1.0);
    400   //      painter->translate(((-boundingRect().left()) - boundingRect().right()), ((-boundingRect().top()) - boundingRect().bottom()));
    401   //    }
    402   //    // check transformations.
    403   //    if (pComponent->getTransformation())
    404   //    {
    405   //      // the item could be flipped horizontally
    406   //      if (pComponent->getTransformation()->getFlipHorizontalIcon())
    407   //      {
    408   //        painter->scale(-1.0, 1.0);
    409   //        painter->translate(((-boundingRect().left()) - boundingRect().right()), 0);
    410   //      }
    411   //      // the item could be flipped vertically
    412   //      if (pComponent->getTransformation()->getFlipVerticalIcon())
    413   //      {
    414   //        painter->scale(1.0, -1.0);
    415   //        painter->translate(0, ((-boundingRect().top()) - boundingRect().bottom()));
    416   //      }
    417   //      // the item could be rotated
    418   //      if (pComponent->getTransformation()->getRotateAngleIcon() == -180 || pComponent->getTransformation()->getRotateAngleIcon() == 180)
    419   //      {
    420   //        painter->scale(-1.0, -1.0);
    421   //        painter->translate(((-boundingRect().left()) - boundingRect().right()), ((-boundingRect().top()) - boundingRect().bottom()));
    422   //      }
    423   //    }
    424   //  }
    425   //  else if (mIsCustomShape)
    426   //  {
    427   //    if (rotation() == -180 || rotation() == 180)
    428   //    {
    429   //      painter->scale(-1.0, -1.0);
    430   //      painter->translate(((-boundingRect().left()) - boundingRect().right()), ((-boundingRect().top()) - boundingRect().bottom()));
    431   //    }
    432   //  }
    433   //  qDebug() << "topleft :: " << sceneBoundingRect().topLeft();
    434   //  qDebug() << "topright :: " << sceneBoundingRect().topRight();
    435   //  qDebug() << "bottomleft :: " << sceneBoundingRect().bottomLeft();
    436   //  qDebug() << "bottomright :: " << sceneBoundingRect().bottomRight();
    437 
    438   //  painter->drawRect(boundingRect());
    439   painter->drawText(boundingRect(), mHorizontalAlignment | Qt::AlignJustify | Qt::TextDontClip, mTextString);
    440   //  if (boundingRect().width() > 0)
    441   //  {
    442   //    painter->drawText(boundingRect(), mHorizontalAlignment | Qt::AlignJustify | Qt::TextDontClip, mTextString);
    443   //  }
    444 }
    445 
    446236void TextAnnotation::checkNameString()
    447237{
     
    468258  }
    469259}
    470 
    471 void TextAnnotation::checkParameterString()
    472 {
    473   // look for the string in parameters list
    474   //    foreach (IconParameters *parameter, mpComponent->mIconParametersList)
    475   //    {
    476   //        if (updateParameterString(parameter))
    477   //            break;
    478   //    }
    479 }
    480 
    481 bool TextAnnotation::updateParameterString(IconParameters *pParamter)
    482 {
    483   QString parameterString;
    484   // paramter can be in form R=%R
    485   parameterString = QString(pParamter->getName()).append("=%").append(pParamter->getName());
    486   if (parameterString == mTextString)
    487   {
    488     mTextString = QString(pParamter->getName()).append("=").append(pParamter->getDefaultValue());
    489     return true;
    490   }
    491   // paramter can be in form %R
    492   parameterString = QString("%").append(pParamter->getName());
    493   if (parameterString == mTextString)
    494   {
    495     mTextString = QString(pParamter->getDefaultValue());
    496     return true;
    497   }
    498   return false;
    499 }
    500 
    501 QString TextAnnotation::getTextString()
    502 {
    503   return mTextString.trimmed();
    504 }
    505 
    506 void TextAnnotation::setTextString(QString text)
    507 {
    508   mTextString = text;
    509   update(boundingRect());
    510 }
    511 
    512 void TextAnnotation::setFontName(QString fontName)
    513 {
    514   mFontName = fontName;
    515 }
    516 
    517 QString TextAnnotation::getFontName()
    518 {
    519   return mFontName;
    520 }
    521 
    522 void TextAnnotation::setFontSize(double fontSize)
    523 {
    524   mFontSize = fontSize;
    525 }
    526 
    527 double TextAnnotation::getFontSize()
    528 {
    529   return mFontSize;
    530 }
    531 
    532 void TextAnnotation::setItalic(bool italic)
    533 {
    534   mFontItalic = italic;
    535 }
    536 
    537 bool TextAnnotation::getItalic()
    538 {
    539   return mFontItalic;
    540 }
    541 
    542 void TextAnnotation::setWeight(int bold)
    543 {
    544   mFontWeight = bold;
    545 }
    546 
    547 bool TextAnnotation::getWeight()
    548 {
    549   if (mFontWeight == QFont::Bold)
    550     return true;
    551   else
    552     return false;
    553 }
    554 
    555 void TextAnnotation::setUnderLine(bool underLine)
    556 {
    557   mFontUnderLine = underLine;
    558 }
    559 
    560 bool TextAnnotation::getUnderLine()
    561 {
    562   return mFontUnderLine;
    563 }
    564 
    565 void TextAnnotation::setAlignment(Qt::Alignment alignment)
    566 {
    567   mHorizontalAlignment = alignment;
    568 }
    569 
    570 QString TextAnnotation::getAlignment()
    571 {
    572   switch (mHorizontalAlignment)
    573   {
    574     case Qt::AlignLeft:
    575       return Helper::left;
    576     case Qt::AlignCenter:
    577       return Helper::center;
    578     case Qt::AlignRight:
    579       return Helper::right;
    580     default:
    581       return Helper::left;
    582   }
    583 }
    584 
    585 void TextAnnotation::drawRectangleCornerItems()
    586 {
    587   mIsFinishedCreatingShape = true;
    588   for (int i = 0 ; i < mExtent.size() ; i++)
    589   {
    590     QPointF point = mExtent.at(i);
    591     RectangleCornerItem *rectangleCornerItem = new RectangleCornerItem(point.x(), point.y(), i, this);
    592     mRectangleCornerItemsList.append(rectangleCornerItem);
    593   }
    594   emit updateShapeAnnotation();
    595 }
    596 
    597 void TextAnnotation::addPoint(QPointF point)
    598 {
    599   mExtent.append(point);
    600   if (mExtent.size() < 2)
    601     return;
    602   emit extentChanged();
    603 }
    604 
    605 void TextAnnotation::updatePoint(int index, QPointF point)
    606 {
    607   mExtent.replace(index, point);
    608   emit extentChanged();
    609 }
    610 
    611 void TextAnnotation::updateEndPoint(QPointF point)
    612 {
    613   mExtent.back() = point;
    614   emit extentChanged();
    615 }
    616 
    617 void TextAnnotation::updateAnnotation()
    618 {
    619   emit updateShapeAnnotation();
    620 }
    621 
    622 QString TextAnnotation::getShapeAnnotation()
    623 {
    624   QString annotationString;
    625   annotationString.append("Text(");
    626 
    627   if (!mVisible)
    628   {
    629     annotationString.append("visible=false,");
    630   }
    631 
    632   annotationString.append("rotation=").append(QString::number(rotation())).append(",");
    633 
    634   annotationString.append("lineColor={");
    635   annotationString.append(QString::number(mLineColor.red())).append(",");
    636   annotationString.append(QString::number(mLineColor.green())).append(",");
    637   annotationString.append(QString::number(mLineColor.blue()));
    638   annotationString.append("},");
    639 
    640   annotationString.append("fillColor={");
    641   annotationString.append(QString::number(mFillColor.red())).append(",");
    642   annotationString.append(QString::number(mFillColor.green())).append(",");
    643   annotationString.append(QString::number(mFillColor.blue()));
    644   annotationString.append("},");
    645 
    646   QMap<QString, Qt::PenStyle>::iterator it;
    647   for (it = mLinePatternsMap.begin(); it != mLinePatternsMap.end(); ++it)
    648   {
    649     if (it.value() == mLinePattern)
    650     {
    651       annotationString.append("pattern=LinePattern.").append(it.key()).append(",");
    652       break;
    653     }
    654   }
    655 
    656   QMap<QString, Qt::BrushStyle>::iterator fill_it;
    657   for (fill_it = mFillPatternsMap.begin(); fill_it != mFillPatternsMap.end(); ++fill_it)
    658   {
    659     if (fill_it.value() == mFillPattern)
    660     {
    661       annotationString.append("fillPattern=FillPattern.").append(fill_it.key()).append(",");
    662       break;
    663     }
    664   }
    665 
    666   annotationString.append("lineThickness=").append(QString::number(mThickness)).append(",");
    667   annotationString.append("extent={{");
    668   annotationString.append(QString::number(mapToScene(mExtent.at(0)).x())).append(",");
    669   annotationString.append(QString::number(mapToScene(mExtent.at(0)).y())).append("},{");
    670   annotationString.append(QString::number(mapToScene(mExtent.at(1)).x())).append(",");
    671   annotationString.append(QString::number(mapToScene(mExtent.at(1)).y()));
    672   annotationString.append("}}");
    673 
    674   annotationString.append(",textString=");
    675   annotationString.append('"');
    676   annotationString.append(getTextString());
    677   annotationString.append('"');
    678 
    679   if(mFontSize != 0)
    680   {
    681     annotationString.append(",fontSize=");
    682     annotationString.append(QString::number(mFontSize));
    683 
    684   }
    685 
    686   if(!(mFontName == StringHandler::getDefaultSystemFont().family()))
    687   {
    688     annotationString.append(",fontName=");
    689     annotationString.append('"');
    690     annotationString.append(mFontName);
    691     annotationString.append('"');
    692   }
    693 
    694   //Annotation for text style bold italic underline
    695   if(mFontItalic || (mFontWeight == QFont::Bold) || mFontUnderLine)
    696   {
    697     annotationString.append(",textStyle={");
    698     {
    699       if(mFontItalic)
    700         annotationString.append("TextStyle.Italic");
    701 
    702       if(mFontWeight == QFont::Bold)
    703       {
    704         if(mFontItalic)
    705           annotationString.append(",TextStyle.Bold");
    706         else
    707           annotationString.append("TextStyle.Bold");
    708       }
    709       if(mFontUnderLine)
    710       {
    711         if(mFontItalic || mFontWeight == QFont::Bold)
    712           annotationString.append(",TextStyle.UnderLine");
    713         else
    714           annotationString.append("TextStyle.UnderLine");
    715       }
    716     }
    717     annotationString.append("}");
    718   }
    719 
    720   annotationString.append(")");
    721   return annotationString;
    722 }
    723 
    724 QRectF TextAnnotation::getDrawingRect()
    725 {
    726   mDrawingRect = QRect (boundingRect().left(), -(boundingRect().top()), boundingRect().width(),
    727                         -(boundingRect().height()));
    728   return mDrawingRect;
    729 }
    730 
    731 //! Sets the letter spacing for the Text Annotations.
    732 //! @param letterSpacing is the new letter spacing.
    733 //! @see setLetterSpacingOnFont(QFont *font, qreal letterSpacing)
    734 //! @see getLetterSpacing()
    735 void TextAnnotation::setLetterSpacing(qreal letterSpacing)
    736 {
    737   mLetterSpacing = letterSpacing;
    738 }
    739 
    740 //! Sets the letter spacing of the Text Annotations for the given Font.
    741 //! @param font
    742 //! @param letterSpacing is the new letter spacing.
    743 //! @see setLetterSpacing(qreal letterSpacing)
    744 //! @see getLetterSpacing()
    745 void TextAnnotation::setLetterSpacingOnFont(QFont *font, qreal letterSpacing)
    746 {
    747   //    if (font->letterSpacing() < letterSpacing)
    748   //        font->setLetterSpacing(QFont::AbsoluteSpacing, letterSpacing);
    749 }
    750 
    751 //! Returns the letter spacing used by the Text Annotations.
    752 //! @param font
    753 //! @param letterSpacing is the new letter spacing.
    754 //! @see setLetterSpacing(qreal letterSpacing)
    755 //! @see setLetterSpacingOnFont(QFont *font, qreal letterSpacing)
    756 qreal TextAnnotation::getLetterSpacing()
    757 {
    758   return mLetterSpacing;
    759 }
    760 
    761 void TextAnnotation::parseShapeAnnotation(QString shape, OMCProxy *omc)
    762 {
    763   shape = shape.replace("{", "");
    764   shape = shape.replace("}", "");
    765   // parse the shape to get the list of attributes of Text Annotation.
    766   QStringList list = StringHandler::getStrings(shape);
    767   if (list.size() < 17)
    768   {
    769     return;
    770   }
    771   // if first item of list is true then the Text Annotation should be visible.
    772   mVisible = static_cast<QString>(list.at(0)).contains("true");
    773   int index = 0;
    774   mOrigin.setX(static_cast<QString>(list.at(1)).toFloat());
    775   mOrigin.setY(static_cast<QString>(list.at(2)).toFloat());
    776   mRotation = static_cast<QString>(list.at(3)).toFloat();
    777   index = 3;
    778   // 2,3,4 items of list contains the line color.
    779   index = index + 1;
    780   int red, green, blue;
    781   red = static_cast<QString>(list.at(index)).toInt();
    782   index = index + 1;
    783   green = static_cast<QString>(list.at(index)).toInt();
    784   index = index + 1;
    785   blue = static_cast<QString>(list.at(index)).toInt();
    786   mLineColor = QColor (red, green, blue);
    787   // 5,6,7 items of list contains the fill color.
    788   index = index + 1;
    789   red = static_cast<QString>(list.at(index)).toInt();
    790   index = index + 1;
    791   green = static_cast<QString>(list.at(index)).toInt();
    792   index = index + 1;
    793   blue = static_cast<QString>(list.at(index)).toInt();
    794   mFillColor = QColor (red, green, blue);
    795   // 8 item of the list contains the line pattern.
    796   index = index + 1;
    797   QString linePattern = StringHandler::getLastWordAfterDot(list.at(index));
    798   QMap<QString, Qt::PenStyle>::iterator it;
    799   for (it = mLinePatternsMap.begin(); it != mLinePatternsMap.end(); ++it)
    800   {
    801     if (it.key().compare(linePattern) == 0)
    802     {
    803       mLinePattern = it.value();
    804       break;
    805     }
    806   }
    807 
    808   // 9 item of the list contains the fill pattern.
    809   index = index + 1;
    810   QString fillPattern = StringHandler::getLastWordAfterDot(list.at(index));
    811   QMap<QString, Qt::BrushStyle>::iterator fill_it;
    812   for (fill_it = mFillPatternsMap.begin(); fill_it != mFillPatternsMap.end(); ++fill_it)
    813   {
    814     if (fill_it.key().compare(fillPattern) == 0)
    815     {
    816       mFillPattern = fill_it.value();
    817       break;
    818     }
    819   }
    820 
    821   // 10 item of the list contains the thickness.
    822   index = index + 1;
    823   mThickness = static_cast<QString>(list.at(index)).toFloat();
    824 
    825   // 11, 12, 13, 14 items of the list contains the extent points of Text.
    826   index = index + 1;
    827   qreal x = static_cast<QString>(list.at(index)).toFloat();
    828   index = index + 1;
    829   qreal y = static_cast<QString>(list.at(index)).toFloat();
    830   QPointF p1 (x, y);
    831   index = index + 1;
    832   x = static_cast<QString>(list.at(index)).toFloat();
    833   index = index + 1;
    834   y = static_cast<QString>(list.at(index)).toFloat();
    835   QPointF p2 (x, y);
    836   mExtent.append(p1);
    837   mExtent.append(p2);
    838   // 15 item of the list contains the text string.
    839   index = index + 1;
    840   if(mIsCustomShape)
    841     mTextString = StringHandler::removeFirstLastQuotes(list.at(index));
    842   else
    843   {
    844     mTextString = StringHandler::removeFirstLastQuotes(list.at(index));
    845     checkNameString();
    846     checkParameterString();
    847   }
    848   index++;
    849   //Now comes the optional parameters.
    850   while(index < list.size())
    851   {
    852     QString line = StringHandler::removeFirstLastQuotes(list.at(index));
    853     if(line == "TextStyle.Italic")
    854     {
    855       mFontItalic = true;
    856       index++;
    857     }
    858     else if(line == "TextStyle.Bold")
    859     {
    860       mFontWeight = QFont::Bold;
    861       index++;
    862     }
    863     else if(line == "TextStyle.UnderLine")
    864     {
    865       mFontUnderLine = true;
    866       index++;
    867     }
    868     else if(line.length() < 3)
    869     {
    870       mFontSize = line.toInt();
    871       index++;
    872     }
    873     else if(line == "TextAlignment.Center" )
    874     {
    875       mHorizontalAlignment = Qt::AlignCenter;
    876       index++;
    877     }
    878     else if(line == "TextAlignment.Left" )
    879     {
    880       mHorizontalAlignment = Qt::AlignLeft;
    881       index++;
    882     }
    883     else if(line == "TextAlignment.Right" )
    884     {
    885       mHorizontalAlignment = Qt::AlignRight;
    886       index++;
    887     }
    888     else if(line.length() > 3)
    889     {
    890       mFontName = line;
    891       index++;
    892     }
    893     else
    894     {
    895       index++;
    896     }
    897   }
    898 }
    899 
    900 void TextAnnotation::calculateFontSize()
    901 {
    902   QFont font(mFontName, mCalculatedFontSize, mFontWeight, mFontItalic);
    903   if(mFontUnderLine)
    904     font.setUnderline(true);
    905   // set letter spacing for the font
    906   setLetterSpacingOnFont(&font, getLetterSpacing());
    907   QFontMetricsF fontMetric (font);
    908   QRectF fontBoundingRect (boundingRect().left(), boundingRect().top(), fontMetric.boundingRect(mTextString).width(),
    909                            fontMetric.boundingRect(mTextString).height());
    910   // if font boundingrect is within original boundingrect
    911   if (boundingRect().contains(fontBoundingRect))
    912   {
    913     while (boundingRect().contains(fontBoundingRect))
    914     {
    915       mCalculatedFontSize += 1;
    916       font = QFont(mFontName, mCalculatedFontSize, mFontWeight, mFontItalic);
    917       if (mFontUnderLine)
    918         font.setUnderline(true);
    919       // set letter spacing for the font
    920       setLetterSpacingOnFont(&font, getLetterSpacing());
    921       fontMetric = QFontMetricsF(font);
    922       fontBoundingRect = QRectF(boundingRect().left(), boundingRect().top(), fontMetric.boundingRect(mTextString).width(),
    923                                 fontMetric.boundingRect(mTextString).height());
    924     }
    925     mCalculatedFontSize -= 1;
    926   }
    927   // if font boundingrect is not within original boundingrect
    928   else
    929   {
    930     while (!boundingRect().contains(fontBoundingRect))
    931     {
    932       mCalculatedFontSize -= 1;
    933       // make sure calculated font doesn't go in negative.
    934       // if calculated font size becomes zero then we have problems because Qt automatically assigns font size if your given font size is 0.
    935       if (mCalculatedFontSize <= 0)
    936         break;
    937       font = QFont(mFontName, mCalculatedFontSize, mFontWeight, mFontItalic);
    938       if(mFontUnderLine)
    939         font.setUnderline(true);
    940       // set letter spacing for the font
    941       setLetterSpacingOnFont(&font, getLetterSpacing());
    942       fontMetric = QFontMetricsF(font);
    943       fontBoundingRect = QRectF(boundingRect().left(), boundingRect().top(), fontMetric.boundingRect(mTextString).width(),
    944                                 fontMetric.boundingRect(mTextString).height());
    945     }
    946     mCalculatedFontSize += 1;
    947   }
    948 }
    949 
    950 //TextWidget declarations
    951 
    952 TextWidget::TextWidget(TextAnnotation *pTextShape, MainWindow *parent)
    953   : QDialog(parent, Qt::WindowTitleHint)
    954 {
    955   setWindowTitle(QString(Helper::applicationName).append(" - Text Properties"));
    956   setAttribute(Qt::WA_DeleteOnClose);
    957   setModal(true);
    958   setMinimumSize(300, 300);
    959   mpMainWindow = parent;
    960   mpTextAnnotation = pTextShape;
    961   setUpForm();
    962 }
    963 
    964 void TextWidget::setUpForm()
    965 {
    966   // heading
    967   mpHeading = new Label(tr("Text Properties"));
    968   mpHeading->setFont(QFont("", Helper::headingFontSize));
    969   mpHeading->setAlignment(Qt::AlignTop);
    970 
    971   QHBoxLayout *horizontalLayout = new QHBoxLayout;
    972   horizontalLayout->addWidget(mpHeading);
    973 
    974   mHorizontalLine = new QFrame();
    975   mHorizontalLine->setFrameShape(QFrame::HLine);
    976   mHorizontalLine->setFrameShadow(QFrame::Sunken);
    977 
    978   //Text Label
    979   mpTextLabel = new Label(tr("Text of Label:"));
    980   mpTextBox = new QLineEdit(mpTextAnnotation->getTextString());
    981 
    982   //Font Name
    983   mpFontLabel = new Label(tr("Font Name:"));
    984   mpFontFamilyComboBox = new QFontComboBox;
    985   int currentIndex;
    986   currentIndex = mpFontFamilyComboBox->findText(mpTextAnnotation->getFontName(), Qt::MatchExactly);
    987   mpFontFamilyComboBox->setCurrentIndex(currentIndex);
    988   //Font Size
    989   mpFontSizeLabel = new Label(tr("Font Size:"));
    990   mpFontSizeComboBox = new QComboBox;
    991   QStringList sizesList;
    992   sizesList << "0" << "2" << "4";
    993   mpFontSizeComboBox->addItems(sizesList);
    994   mpFontSizeComboBox->addItems(Helper::fontSizes.split(","));
    995   currentIndex = mpFontSizeComboBox->findText(QString::number(mpTextAnnotation->getFontSize()), Qt::MatchExactly);
    996   mpFontSizeComboBox->setCurrentIndex(currentIndex);
    997   //Cursive Bold Underline Checkboxes
    998   mpCursive = new QCheckBox("Italic", this);
    999   mpCursive->setChecked(mpTextAnnotation->getItalic());
    1000   mpBold = new QCheckBox("Bold", this);
    1001   mpBold->setChecked(mpTextAnnotation->getWeight());
    1002   mpUnderline = new QCheckBox("Underline", this);
    1003   mpUnderline->setChecked(mpTextAnnotation->getUnderLine());
    1004   mpStylesGroup = new QGroupBox(tr("Styles"));
    1005   QVBoxLayout *verticalPropertiesLayout = new QVBoxLayout;
    1006   verticalPropertiesLayout->addWidget(mpCursive);
    1007   verticalPropertiesLayout->addWidget(mpBold);
    1008   verticalPropertiesLayout->addWidget(mpUnderline);
    1009   mpStylesGroup->setLayout(verticalPropertiesLayout);
    1010   //Alignment
    1011   mpAlignmentLabel = new Label(tr("Alignment:"));
    1012   mpAlignmentComboBox = new QComboBox;
    1013   QStringList alignmentList;
    1014   alignmentList << Helper::left << Helper::center << Helper::right;
    1015   mpAlignmentComboBox->addItems(alignmentList);
    1016   currentIndex = mpAlignmentComboBox->findText(mpTextAnnotation->getAlignment(), Qt::MatchExactly);
    1017   mpAlignmentComboBox->setCurrentIndex(currentIndex);
    1018 
    1019   //Buttons
    1020   mpEditButton = new QPushButton(tr("Ok"));
    1021   mpEditButton->setAutoDefault(true);
    1022   connect(mpEditButton, SIGNAL(clicked()), this, SLOT(edit()));
    1023   mpCancelButton = new QPushButton(tr("Cancel"));
    1024   mpCancelButton->setAutoDefault(false);
    1025   connect(mpCancelButton, SIGNAL(clicked()), this, SLOT(reject()));
    1026   mpButtonBox = new QDialogButtonBox(Qt::Horizontal);
    1027   mpButtonBox->addButton(mpEditButton, QDialogButtonBox::ActionRole);
    1028   mpButtonBox->addButton(mpCancelButton, QDialogButtonBox::ActionRole);
    1029 
    1030   // Create a layout
    1031   QGridLayout *mainLayout = new QGridLayout;
    1032   mainLayout->addLayout(horizontalLayout, 0, 0, 1, 2);
    1033   mainLayout->addWidget(mHorizontalLine, 1, 0, 1, 2);
    1034   mainLayout->addWidget(mpTextLabel, 2, 0);
    1035   mainLayout->addWidget(mpTextBox, 2, 1);
    1036   mainLayout->addWidget(mpFontLabel, 3, 0);
    1037   mainLayout->addWidget(mpFontFamilyComboBox, 3, 1);
    1038   mainLayout->addWidget(mpFontSizeLabel, 4, 0);
    1039   mainLayout->addWidget(mpFontSizeComboBox, 4, 1);
    1040   mainLayout->addWidget(mpStylesGroup, 5, 0, 1, 2);
    1041   mainLayout->addWidget(mpAlignmentLabel, 6, 0);
    1042   mainLayout->addWidget(mpAlignmentComboBox, 6, 1);
    1043   mainLayout->addWidget(mpButtonBox, 7, 0, 1, 2);
    1044 
    1045   setLayout(mainLayout);
    1046 }
    1047 
    1048 void TextWidget::edit()
    1049 {
    1050   if(mpTextBox->text().isEmpty())
    1051     return;
    1052 
    1053   mpTextAnnotation->setTextString(mpTextBox->text());
    1054   mpTextAnnotation->setFontName(mpFontFamilyComboBox->currentText());
    1055   mpTextAnnotation->setFontSize(mpFontSizeComboBox->currentText().toDouble());
    1056   mpTextAnnotation->setItalic(mpCursive->isChecked());
    1057   if (mpBold->isChecked())
    1058     mpTextAnnotation->setWeight(QFont::Bold);
    1059   else
    1060     mpTextAnnotation->setWeight(QFont::Normal);
    1061   mpTextAnnotation->setUnderLine(mpUnderline->isChecked());
    1062 
    1063   if (mpAlignmentComboBox->currentText() == "Left")
    1064     mpTextAnnotation->setAlignment(Qt::AlignLeft);
    1065   else if (mpAlignmentComboBox->currentText() == "Center")
    1066     mpTextAnnotation->setAlignment(Qt::AlignCenter);
    1067   else if (mpAlignmentComboBox->currentText() == "Right")
    1068     mpTextAnnotation->setAlignment(Qt::AlignRight);
    1069 
    1070   mpTextAnnotation->updateAnnotation();
    1071   accept();
    1072 }
    1073 
    1074 void TextWidget::show()
    1075 {
    1076   setVisible(true);
    1077 }
Note: See TracChangeset for help on using the changeset viewer.