[Uml-devel] KDE/kdesdk/umbrello/umbrello

Andi Fischer andi.fischer at hispeed.ch
Sun Jul 25 15:27:19 UTC 2010


SVN commit 1154482 by fischer:

Changing the drawing of text.

 M  +0 -5      controller/notewidgetcontroller.cpp  
 M  +53 -96    widgets/notewidget.cpp  


--- trunk/KDE/kdesdk/umbrello/umbrello/controller/notewidgetcontroller.cpp #1154481:1154482
@@ -55,12 +55,7 @@
 void NoteWidgetController::mouseReleaseEvent(QMouseEvent *me)
 {
     UMLWidgetController::mouseReleaseEvent(me);
-    //TODO why is it needed? drawText is already called in draw,
-    //and draw is (well, I think that is) called when the canvas rectangle is resized
-    if (m_resized) {
-        m_noteWidget->drawText();
     }
-}
 
 /**
  * Overridden from UMLWidgetController.
--- trunk/KDE/kdesdk/umbrello/umbrello/widgets/notewidget.cpp #1154481:1154482
@@ -32,7 +32,6 @@
 {
     init();
     m_NoteType = noteType;
-    setSize(100, 80);
     setZ(20); //make sure always on top.
 }
 
@@ -83,7 +82,6 @@
     }
     QString linkText("Diagram: " + view->getName());
     setDocumentation(linkText);
-    update();
     m_DiagramLink = viewID;
 }
 
@@ -92,7 +90,6 @@
     return m_DiagramLink;
 }
 
-
 void NoteWidget::setFont(QFont font)
 {
     UMLWidget::setFont(font);
@@ -116,16 +113,13 @@
 void NoteWidget::setDocumentation(const QString &newText)
 {
     m_Text = newText;
-    QSize size = calculateSize();
-    setSize(size.width(), size.height());
-    //uDebug() << "new size [" << size.width() << ", " << size.height() << "]";
+    update();
 }
 
 void NoteWidget::draw(QPainter & p, int offsetX, int offsetY)
 {
-    int margin = 10;
+    const int margin = 10;
     int w = width()-1;
-
     int h = height()-1;
     const QFontMetrics &fm = getFontMetrics(FT_NORMAL);
     const int fontHeight  = fm.lineSpacing();
@@ -151,16 +145,15 @@
     case NoteWidget::PreCondition :
         p.drawText(offsetX, offsetY + margin ,w, fontHeight, Qt::AlignCenter, "<< precondition >>");
         break;
-
     case NoteWidget::PostCondition :
         p.drawText(offsetX, offsetY + margin ,w, fontHeight, Qt::AlignCenter, "<< postcondition >>");
         break;
-
     case NoteWidget::Transformation :
         p.drawText(offsetX, offsetY + margin ,w, fontHeight, Qt::AlignCenter, "<< transformation >>");
         break;
         case NoteWidget::Normal :
-    default :  break;
+    default :
+        break;
     }
 
     if(m_bSelected) {
@@ -172,8 +165,8 @@
 
 QSize NoteWidget::calculateSize()
 {
-    int width = 50;
-    int height = 50;
+    int width = 60;
+    int height = 30;
     const QFontMetrics &fm = getFontMetrics(FT_NORMAL);
     const int textWidth = fm.width(m_Text);
     if (m_NoteType == PreCondition) {
@@ -192,18 +185,8 @@
         width += 10;
     }
     else {
-        if (!m_Text.isEmpty()) {
-            QSize textSize = fm.size(Qt::TextExpandTabs, m_Text);
-            int textWidth = textSize.width() + 45;
-            if (width < textWidth) {
-                width = textWidth;
+        // do nothing, keep width and height unchanged for resizing
             }
-            int textHeight = textSize.height() + 10;
-            if (height < textHeight) {
-                height = textHeight;
-            }
-        }
-    }
     return QSize(width, height);
 }
 
@@ -230,98 +213,72 @@
     }
 }
 
-void NoteWidget::drawText(QPainter * p /*=NULL*/, int offsetX /*=0*/, int offsetY /*=0*/)
+/**
+ * Implemented without word wrap.
+ */
+void NoteWidget::drawText(QPainter * p, int offsetX, int offsetY)
 {
-    if (p == NULL)
+    if (p == NULL) {
         return;
-    /*
-    Implement word wrap for text as follows:
-    wrap at width on whole words.
-    if word is wider than width then clip word
-    if reach height exit and don't print anymore
-    start new line on \n character
-    */
+    }
+
+    QString text = documentation();
+    if (text.length() == 0) {
+        return;
+    }
+
     p->setPen( Qt::black );
     QFont font = UMLWidget::font();
     p->setFont( font );
+
     const QFontMetrics &fm = getFontMetrics(FT_NORMAL);
     const int fontHeight  = fm.lineSpacing();
-    QString text = documentation();
-    if( text.length() == 0 )
-        return;
-    QString word = "";
-    QString fullLine = "";
-    QString testCombineLine = "";
     const int margin = fm.width( "W" );
-    int textY = fontHeight / 2;
-    int textX = margin;
+    const QSize textSize  = fm.size(Qt::TextExpandTabs, text);
+
     const int width = this -> width() - margin * 2;
     const int height = this -> height() - fontHeight;
-    QChar returnChar('\n');
-    QChar c;
+    int textY = fontHeight / 2;
+    int textX = margin;
 
-    if( text.length() == 0 )
-        return;
-
-    for (int i = 0; i <= text.length(); ++i) {
-        if (i < text.length()) {
-            c = text[i];
-        } else {
-            // all chars of text have been handled already ->
-            // perform this last run to spool current content of "word"
-            c = returnChar;
+    if ((textSize.width() < width) && (textSize.height() < height)) {
+        // the entire text is small enough - draw it
+        p->drawText(offsetX + textX, offsetY + textY,
+                    textSize.width(), textSize.height(),
+                    Qt::AlignLeft, text);
         }
-        if (c == returnChar || c.isSpace()) {
-            // new word delimiter found -> it is time to decide on word wrap
-            testCombineLine = fullLine + ' ' + word;
-            int textWidth = fm.width( testCombineLine );
-            if (textX + textWidth > width) {
-                // combination of "fullLine" and "word" doesn't fit into one line ->
-                // print "fullLine" in current line, update write position to next line
-                // and decide then on following actions
+    else {
+        // not all text can be drawn
+        QStringList lines = text.split(QChar('\n'));
+        foreach(QString line, lines) {
+            int lineWidth = fm.width(line);
+            if (lineWidth < width) {
+                // line is small enough - draw it
                 p->drawText(offsetX + textX, offsetY + textY,
-                            textWidth, fontHeight, Qt::AlignLeft, fullLine );
-                fullLine = word;
-                word = "";
-                // update write position
-                textX = margin;
-                textY += fontHeight;
-                if (textY > height)
-                    return;
-                // in case of c==newline ->
-                // print "word" and set write position one additional line lower
-                if (c == returnChar) {
-                    // print "word" - which is now "fullLine" and set to next line
+                            textSize.width(), fontHeight,
+                            Qt::AlignLeft, line);
+            }
+            else {
+                // draw a smaller line
+                for(int len = line.length(); len > 0; --len) {
+                    QString smallerLine = line.left(len);
+                    int smallerLineWidth = fm.width(smallerLine);
+                    if (smallerLineWidth < width) {
+                        // line is small enough - draw it
                     p->drawText(offsetX + textX, offsetY + textY,
-                                textWidth, fontHeight, Qt::AlignLeft, fullLine);
-                    fullLine = "";
-                    textX = margin;
-                    textY += fontHeight;
-                    if( textY > height ) return;
+                                    width, fontHeight,
+                                    Qt::AlignLeft, smallerLine);
                 }
             }
-            else if ( c == returnChar ) {
-                // newline found and combination of "fullLine" and "word" fits
-                // in one line
-                p->drawText(offsetX + textX, offsetY + textY,
-                            textWidth, fontHeight, Qt::AlignLeft, testCombineLine);
-                fullLine = word = "";
-                textX = margin;
+            }
                 textY += fontHeight;
-                if (textY > height)
-                    return;
-            } else {
-                // word delimiter found, and combination of "fullLine", space and "word" fits into one line
-                fullLine = testCombineLine;
-                word = "";
+            if (textY > height) {
+                // skip the next lines - size is not enough
+                break;
             }
-        } else {
-            // no word delimiter found --> add current char to "word"
-            if (c != '\0')
-                word += c;
         }
-    }//end for
 }
+}
 
 void NoteWidget::askForNoteType(UMLWidget* &targetWidget)
 {




More information about the umbrello-devel mailing list