Modified FriendListItem font metrics calculation.
authorJussi Laitinen <jupe@l3l7588.ixonos.local>
Thu, 8 Jul 2010 13:06:52 +0000 (16:06 +0300)
committerJussi Laitinen <jupe@l3l7588.ixonos.local>
Thu, 8 Jul 2010 13:06:52 +0000 (16:06 +0300)
src/ui/friendlistitem.cpp
src/ui/friendlistitemdelegate.cpp

index 1735cf1..6fc0d30 100644 (file)
@@ -71,72 +71,19 @@ FriendListItem::FriendListItem()
     qDebug() << __PRETTY_FUNCTION__;
 
     setData(Qt::SizeHintRole, QSize(ITEM_WIDTH, ITEM_MIN_HEIGHT));
-
-//    m_distanceTextLabel = new QLabel();
-//    m_distanceTextLabel->setFixedHeight(ICON_HEIGHT);
-
-//    m_distanceImageLabel = new QLabel();
-//    m_distanceImageLabel->setFixedSize(ICON_WIDTH, ICON_HEIGHT);
-
-//    m_findButton = new ImageButton(this);
-
-//    m_updatedLabel = new QLabel();
-//    m_updatedLabel->setWordWrap(true);
-//    m_statusTextLabel = new QLabel();
-//    m_statusTextLabel->setWordWrap(true);
-//    m_locationLabel = new QLabel();
-//    m_locationLabel->setWordWrap(true);
-
-//    distanceLayout->addWidget(m_distanceImageLabel, 0, Qt::AlignRight);
-//    distanceLayout->addWidget(m_distanceTextLabel, 0, Qt::AlignRight);
-
-//    infoLayout->addRow(envelopeLabel, m_statusTextLabel);
-//    infoLayout->addRow(compassLabel, m_locationLabel);
-//    infoLayout->addRow(clockLabel, m_updatedLabel);
-
-//    topLayout->addWidget(m_findButton);
-//    topLayout->addWidget(m_nameLabel, 1);
-//    topLayout->addLayout(distanceLayout);
-
-//    bottomLayout->addSpacing(IMAGE_WIDTH);
-//    bottomLayout->addLayout(infoLayout);
-
-//    layout->addLayout(topLayout, 0);
-//    layout->addLayout(bottomLayout, 1);
-
-//    setMinimumSize(ITEM_WIDTH, ITEM_MIN_HEIGHT);
-//    setMaximumWidth(ITEM_WIDTH);
-
-//    setFont(NOKIA_FONT_SMALL);
-//    m_nameLabel->setFont(NOKIA_FONT_NORMAL);
-//    QPalette itemPalette = palette();
-//    itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
-//    setPalette(itemPalette);
-//    QPalette namePalette = m_nameLabel->palette();
-//    namePalette.setColor(QPalette::Foreground, Qt::white);
-//    m_nameLabel->setPalette(namePalette);
-
-//    m_backgroundTopImage.load(":/res/images/list_item_top.png");
-//    m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
-//    m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
-
-//    connect(m_findButton, SIGNAL(clicked()),
-//        this, SLOT(findButtonClicked()));
 }
 
 void FriendListItem::calculateTextRects()
 {
-    QFontMetrics otherLabelsMetrics = QFontMetrics(NOKIA_FONT_SMALL);
+    QPixmap p = QPixmap(10, 10);
+    QPainter painter(&p);
+    painter.setFont(NOKIA_FONT_SMALL);
+    QFontMetrics smallFontMetrics = painter.fontMetrics();
 
-    QRect statusTextRect = otherLabelsMetrics.boundingRect(QRect(0, 0, LABEL_MAX_WIDTH,
-                                                                             1000), Qt::AlignLeft,
-                                                                       m_user->note());
-    QRect updatedRect = otherLabelsMetrics.boundingRect(QRect(0, 0, LABEL_MAX_WIDTH,
-                                                                             1000), Qt::AlignLeft,
-                                                                       m_user->timestamp());
-    QRect locationRect = otherLabelsMetrics.boundingRect(QRect(0, 0, LABEL_MAX_WIDTH,
-                                                                             1000), Qt::AlignLeft,
-                                                                       m_user->address());
+    QRect distanceRect = smallFontMetrics.boundingRect(m_distanceText);
+    QRect statusTextRect = smallFontMetrics.boundingRect(m_user->note());
+    QRect updatedRect = smallFontMetrics.boundingRect(m_user->timestamp());
+    QRect locationRect = smallFontMetrics.boundingRect(m_user->address());
 
 
     qDebug() << m_shortenedStatusText;
@@ -159,7 +106,13 @@ void FriendListItem::calculateTextRects()
     qDebug() << m_updatedRect;
     qDebug() << m_locationRect;
 
-    m_expandedHeight = ITEM_MIN_HEIGHT + ((statusTextFactor + updatedFactor + locationFactor - 3)*ICON_HEIGHT);
+    qWarning() << statusTextRect.width() << statusTextRect.height();
+    qWarning() << m_statusTextRect.width() << m_statusTextRect.height();
+
+    m_expandedHeight = ITEM_MIN_HEIGHT + ((statusTextFactor + updatedFactor + locationFactor - 3) *
+                                          ICON_HEIGHT);
+
+    setData(Qt::SizeHintRole + 1, distanceRect);
 }
 
 QPointF FriendListItem::coordinates()
@@ -228,8 +181,13 @@ void FriendListItem::shortenTexts()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QFontMetrics nameLabelMetrics = QFontMetrics(NOKIA_FONT_NORMAL);
-    QFontMetrics otherLabelsMetrics = QFontMetrics(NOKIA_FONT_SMALL);
+    QPixmap p = QPixmap(10, 10);
+
+    QPainter painter(&p);
+    painter.setFont(NOKIA_FONT_NORMAL);
+    QFontMetrics nameLabelMetrics = painter.fontMetrics();
+    painter.setFont(NOKIA_FONT_SMALL);
+    QFontMetrics otherLabelsMetrics = painter.fontMetrics();
 
     QString name = m_user->name();
     QString updated = m_user->timestamp();
@@ -258,15 +216,17 @@ void FriendListItem::shortenTexts()
         location.append("...");
     }
 
-    int distanceLabelWidth = otherLabelsMetrics.width(m_distanceText) + 24;
+    int distanceLabelWidth = otherLabelsMetrics.width(m_distanceText) + 5;
     m_shortenedName = nameLabelMetrics.elidedText(name, Qt::ElideRight, NAME_LABEL_MAX_WIDTH
                                                   - distanceLabelWidth);
     m_shortenedStatusText = otherLabelsMetrics.elidedText(statusText, Qt::ElideRight,
-                                                          LABEL_MAX_WIDTH - 70);
-    m_shortenedUpdated = otherLabelsMetrics.elidedText(updated, Qt::ElideRight, LABEL_MAX_WIDTH - 70);
-    m_shortenedLocation = otherLabelsMetrics.elidedText(location, Qt::ElideRight, LABEL_MAX_WIDTH - 70);
+                                                          LABEL_MAX_WIDTH);
+    m_shortenedUpdated = otherLabelsMetrics.elidedText(updated, Qt::ElideRight, LABEL_MAX_WIDTH);
+    m_shortenedLocation = otherLabelsMetrics.elidedText(location, Qt::ElideRight, LABEL_MAX_WIDTH);
 
     setData(Qt::SizeHintRole + 4, QRect(0, 0, distanceLabelWidth, 24));
+
+    qWarning() << m_shortenedStatusText;
 }
 
 QString FriendListItem::elideText(int width, const QString &text)
@@ -301,18 +261,18 @@ void FriendListItem::setText(bool expanded)
         setData(Qt::UserRole + 1, m_user->timestamp());
         setData(Qt::UserRole + 2, m_user->address());
 
-        setData(Qt::SizeHintRole + 1, m_statusTextRect);
-        setData(Qt::SizeHintRole + 2, m_updatedRect);
-        setData(Qt::SizeHintRole + 3, m_locationRect);
+        setData(Qt::SizeHintRole + 2, m_statusTextRect);
+        setData(Qt::SizeHintRole + 3, m_updatedRect);
+        setData(Qt::SizeHintRole + 4, m_locationRect);
     }
     else {
         setData(Qt::UserRole, m_shortenedStatusText);
         setData(Qt::UserRole + 1, m_shortenedUpdated);
         setData(Qt::UserRole + 2, m_shortenedLocation);
 
-        setData(Qt::SizeHintRole + 1, QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT));
         setData(Qt::SizeHintRole + 2, QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT));
         setData(Qt::SizeHintRole + 3, QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT));
+        setData(Qt::SizeHintRole + 4, QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT));
     }
 }
 
index ad5dce3..77605e9 100644 (file)
@@ -58,36 +58,36 @@ void FriendListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem
     QRect itemRect = option.rect;
 
     QPoint distanceIconPoint = QPoint(BACKGROUND_WIDTH - ICON_WIDTH - 15, itemRect.top() + 5);
-    QRect distanceRect = index.data(Qt::SizeHintRole + 4).toRect();
-    distanceRect.translate(BACKGROUND_WIDTH - distanceRect.width(), itemRect.top() + ICON_HEIGHT);
-    qDebug() << __PRETTY_FUNCTION__ << distanceRect << distance;
+    QRect distanceRect = index.data(Qt::SizeHintRole + 1).toRect();
+    distanceRect.translate(BACKGROUND_WIDTH - distanceRect.width() - 5, itemRect.top() + ICON_HEIGHT + 10);
+
     painter->drawPixmap(distanceIconPoint, distanceIcon);
-    painter->drawText(distanceRect, Qt::TextWordWrap, distance);
+    painter->drawText(distanceRect, Qt::TextSingleLine, distance);
 
     QRect envelopeRect = QRect(itemRect.left() + IMAGE_WIDTH + 15, itemRect.top() + IMAGE_HEIGHT - 10,
                                ICON_WIDTH, ICON_HEIGHT);
-    QRect statusTextRect = index.data(Qt::SizeHintRole + 1).toRect();
+    QRect statusTextRect = index.data(Qt::SizeHintRole + 2).toRect();
     statusTextRect.translate(envelopeRect.right() + 5, envelopeRect.top());
 
     painter->drawPixmap(envelopeRect, m_envelopeImage);
-    painter->drawText(statusTextRect, Qt::TextWordWrap, statusText);
+    painter->drawText(statusTextRect, Qt::TextWrapAnywhere, statusText);
 
 
     QRect compassRect = QRect(envelopeRect.left(), statusTextRect.bottom() + 2, ICON_WIDTH,
                               ICON_HEIGHT);
-    QRect locationRect = index.data(Qt::SizeHintRole + 2).toRect();
+    QRect locationRect = index.data(Qt::SizeHintRole + 3).toRect();
     locationRect.translate(compassRect.right() + 5, compassRect.top());
 
     painter->drawPixmap(compassRect, m_compassImage);
-    painter->drawText(locationRect, Qt::TextWordWrap, address);
+    painter->drawText(locationRect, Qt::TextWrapAnywhere, address);
 
 
     QRect clockRect = QRect(envelopeRect.left(), locationRect.bottom() + 2, ICON_WIDTH, ICON_HEIGHT);
-    QRect timestampRect = index.data(Qt::SizeHintRole + 3).toRect();
+    QRect timestampRect = index.data(Qt::SizeHintRole + 4).toRect();
     timestampRect.translate(clockRect.right() + 5, clockRect.top());
 
     painter->drawPixmap(clockRect, m_clockImage);
-    painter->drawText(timestampRect, Qt::TextWordWrap, timestamp);
+    painter->drawText(timestampRect, Qt::TextWrapAnywhere, timestamp);
 }
 
 QSize FriendListItemDelegate::sizeHint(const QStyleOptionViewItem &option,