- Timestamp:
- Jan 29, 2025 12:29:24 PM (3 months ago)
- svn:sync-xref-src-repo-rev:
- 167244
- Location:
- trunk/src/VBox/Frontends/VirtualBox/src/manager/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VirtualBox/src/manager/tools/UIToolsItem.cpp
r107980 r107981 207 207 , m_iPreviousMinimumWidthHint(0) 208 208 , m_iPreviousMinimumHeightHint(0) 209 , m_iMaximumNameWidth(0)210 209 { 211 210 prepare(); … … 231 230 /* Update linked values: */ 232 231 m_strName = strName; 233 updateMinimumNameSize(); 234 updateVisibleName(); 232 updateNameSize(); 235 233 } 236 234 } … … 293 291 int iToolsItemWidth = m_pixmapSize.width(); 294 292 if (model()->tools()->isPopup()) 295 iToolsItemWidth += iSpacing + m_ minimumNameSize.width();293 iToolsItemWidth += iSpacing + m_nameSize.width(); 296 294 iProposedWidth += iToolsItemWidth; 297 295 … … 312 310 /* And Tools-item content to take into account: */ 313 311 int iToolsItemHeight = qMax(m_pixmapSize.height(), 314 m_ minimumNameSize.height());312 m_nameSize.height()); 315 313 iProposedHeight += iToolsItemHeight; 316 314 … … 335 333 /* Update pixmap: */ 336 334 updatePixmap(); 337 }338 339 void UIToolsItem::resizeEvent(QGraphicsSceneResizeEvent *pEvent)340 {341 /* Call to base-class: */342 QIGraphicsWidget::resizeEvent(pEvent);343 344 /* What is the new geometry? */345 const QRectF newGeometry = geometry();346 347 /* Should we update visible name? */348 if (previousGeometry().width() != newGeometry.width())349 updateMaximumNameWidth();350 351 /* Remember the new geometry: */352 setPreviousGeometry(newGeometry);353 335 } 354 336 … … 455 437 /* Init: */ 456 438 updatePixmap(); 457 updateMinimumNameSize(); 458 updateVisibleName(); 439 updateNameSize(); 459 440 } 460 441 … … 588 569 { 589 570 m_pixmapSize = pixmapSize; 590 updateMaximumNameWidth();591 571 updateGeometry(); 592 572 } … … 598 578 } 599 579 600 void UIToolsItem::update MinimumNameSize()580 void UIToolsItem::updateNameSize() 601 581 { 602 582 /* Prepare variables: */ 603 583 QPaintDevice *pPaintDevice = model()->paintDevice(); 604 584 605 /* Calculate new minimumname size: */585 /* Calculate new name size: */ 606 586 const QFontMetrics fm(m_nameFont, pPaintDevice); 607 const int iWidthOf15Letters = textWidthMonospace(m_nameFont, pPaintDevice, 15); 608 const QString strNameCompressedTo15Letters = compressText(m_nameFont, pPaintDevice, m_strName, iWidthOf15Letters); 609 const QSize minimumNameSize = QSize(fm.horizontalAdvance(strNameCompressedTo15Letters), fm.height()); 587 const QSize nameSize = QSize(fm.horizontalAdvance(m_strName), fm.height()); 610 588 611 589 /* Update linked values: */ 612 if (m_ minimumNameSize != minimumNameSize)613 { 614 m_ minimumNameSize = minimumNameSize;590 if (m_nameSize != nameSize) 591 { 592 m_nameSize = nameSize; 615 593 updateGeometry(); 616 594 } 617 }618 619 void UIToolsItem::updateMaximumNameWidth()620 {621 /* Prepare variables: */622 const int iMargin = data(ToolsItemData_Margin).toInt();623 const int iSpacing = data(ToolsItemData_Spacing).toInt();624 625 /* Calculate new maximum name width: */626 int iMaximumNameWidth = (int)geometry().width();627 iMaximumNameWidth -= iMargin; /* left margin */628 iMaximumNameWidth -= m_pixmapSize.width(); /* pixmap width */629 iMaximumNameWidth -= iSpacing; /* spacing between pixmap and name(s) */630 iMaximumNameWidth -= iMargin; /* right margin */631 632 /* Update linked values: */633 if (m_iMaximumNameWidth != iMaximumNameWidth)634 {635 m_iMaximumNameWidth = iMaximumNameWidth;636 updateVisibleName();637 }638 }639 640 void UIToolsItem::updateVisibleName()641 {642 /* Prepare variables: */643 QPaintDevice *pPaintDevice = model()->paintDevice();644 645 /* Calculate new visible name: */646 const QString strVisibleName = compressText(m_nameFont, pPaintDevice, m_strName, m_iMaximumNameWidth);647 648 /* Update linked values: */649 if (m_strVisibleName != strVisibleName)650 {651 m_strVisibleName = strVisibleName;652 update();653 }654 }655 656 /* static */657 int UIToolsItem::textWidthMonospace(const QFont &font, QPaintDevice *pPaintDevice, int iCount)658 {659 /* Return text width, based on font-metrics: */660 const QFontMetrics fm(font, pPaintDevice);661 QString strString;662 strString.fill('_', iCount);663 return fm.horizontalAdvance(strString);664 }665 666 /* static */667 QString UIToolsItem::compressText(const QFont &font, QPaintDevice *pPaintDevice, QString strText, int iWidth)668 {669 /* Check if passed text is empty: */670 if (strText.isEmpty())671 return strText;672 673 /* Check if passed text already fits maximum width: */674 const QFontMetrics fm(font, pPaintDevice);675 if (fm.horizontalAdvance(strText) <= iWidth)676 return strText;677 678 /* Truncate otherwise: */679 const QString strEllipsis = QString("...");680 const int iEllipsisWidth = fm.horizontalAdvance(strEllipsis + " ");681 while (!strText.isEmpty() && fm.horizontalAdvance(strText) + iEllipsisWidth > iWidth)682 strText.truncate(strText.size() - 1);683 return strText + strEllipsis;684 595 } 685 596 … … 895 806 /* Prepare variables: */ 896 807 int iNameX = iMargin + m_pixmapSize.width() + iSpacing; 897 int iNameY = (iFullHeight - m_ minimumNameSize.height()) / 2;808 int iNameY = (iFullHeight - m_nameSize.height()) / 2; 898 809 /* Paint name: */ 899 810 if (model()->tools()->isPopup()) … … 907 818 model()->paintDevice(), 908 819 /* Text to paint: */ 909 m_str VisibleName);820 m_strName); 910 821 } 911 822 } -
trunk/src/VBox/Frontends/VirtualBox/src/manager/tools/UIToolsItem.h
r107980 r107981 143 143 virtual void showEvent(QShowEvent *pEvent) RT_OVERRIDE; 144 144 145 /** Handles resize @a pEvent. */146 virtual void resizeEvent(QGraphicsSceneResizeEvent *pEvent) RT_OVERRIDE;147 148 145 /** Handles hover enter @a event. */ 149 146 virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *pEvent) RT_OVERRIDE; … … 209 206 /** @name Layout stuff. 210 207 * @{ */ 211 /** Defines previous @a geometry. */212 void setPreviousGeometry(const QRectF &geometry) { m_previousGeometry = geometry; }213 /** Returns previous geometry. */214 const QRectF &previousGeometry() const { return m_previousGeometry; }215 216 208 /** Updates pixmap. */ 217 209 void updatePixmap(); 218 /** Updates minimum name size. */ 219 void updateMinimumNameSize(); 220 /** Updates maximum name width. */ 221 void updateMaximumNameWidth(); 222 /** Updates visible name. */ 223 void updateVisibleName(); 224 225 /** Returns monospace text width of line containing @a iCount of chars calculated on the basis of certain @a font and @a pPaintDevice. */ 226 static int textWidthMonospace(const QFont &font, QPaintDevice *pPaintDevice, int iCount); 227 /** Compresses @a strText to @a iWidth on the basis of certain @a font and @a pPaintDevice. */ 228 static QString compressText(const QFont &font, QPaintDevice *pPaintDevice, QString strText, int iWidth); 210 /** Updates name size. */ 211 void updateNameSize(); 229 212 /** @} */ 230 213 … … 271 254 /** Holds the item name font. */ 272 255 QFont m_nameFont; 273 /** Holds the item visible name. */274 QString m_strVisibleName;275 256 276 257 /** Holds whether item is hovered. */ … … 307 288 /** @name Layout stuff. 308 289 * @{ */ 309 /** Holds previous geometry. */310 QRectF m_previousGeometry;311 312 290 /** Holds previous minimum width hint. */ 313 291 int m_iPreviousMinimumWidthHint; … … 317 295 /** Holds the pixmap size. */ 318 296 QSize m_pixmapSize; 319 /** Holds minimum name size. */ 320 QSize m_minimumNameSize; 321 322 /** Holds maximum name width. */ 323 int m_iMaximumNameWidth; 297 /** Holds the name size. */ 298 QSize m_nameSize; 324 299 /** @} */ 325 300 };
Note:
See TracChangeset
for help on using the changeset viewer.