[Marble-commits] KDE/kdeedu/marble/src

Bastian Holst bastianholst at gmx.de
Tue Aug 18 09:57:43 CEST 2009


SVN commit 1012841 by bholst:

MarbleGraphicsView: Better LabelGraphicsItem API

 M  +61 -13    lib/graphicsview/LabelGraphicsItem.cpp  
 M  +9 -1      lib/graphicsview/LabelGraphicsItem.h  
 M  +11 -1     lib/graphicsview/LabelGraphicsItem_p.h  
 M  +6 -2      plugins/render/weather/WeatherItem.cpp  


--- trunk/KDE/kdeedu/marble/src/lib/graphicsview/LabelGraphicsItem.cpp #1012840:1012841
@@ -23,8 +23,9 @@
 
 using namespace Marble;
 
-LabelGraphicsItemPrivate::LabelGraphicsItemPrivate()
-    : m_text()
+LabelGraphicsItemPrivate::LabelGraphicsItemPrivate( LabelGraphicsItem *parent )
+    : m_text(),
+      m_parent( parent )
 {
 }
 
@@ -33,58 +34,105 @@
     return QApplication::font();
 }
 
+void LabelGraphicsItemPrivate::updateSize()
+{
+    QSizeF updatedSize = m_calculatedSize;
+    if ( updatedSize.isEmpty() ) {
+        updatedSize.setHeight( 0 );
+        updatedSize.setWidth( 0 );
+    }
+    else {
+        if ( m_minimumSize.width() > updatedSize.width() ) {
+            updatedSize.setWidth( m_minimumSize.width() );
+        }
+        if ( m_minimumSize.height() > updatedSize.height() ) {
+            updatedSize.setHeight( m_minimumSize.height() );
+        }
+    }
+
+    m_parent->setContentSize( updatedSize );
+}
+
 // ----------------------------------------------------------------
 
 LabelGraphicsItem::LabelGraphicsItem( MarbleGraphicsItem *parent )
     : FrameGraphicsItem( parent ),
-      d( new LabelGraphicsItemPrivate() )
+      d( new LabelGraphicsItemPrivate( this ) )
 {
 }
 
-void LabelGraphicsItem::setText( const QString& text, int minWidth, int minHeight )
+QString LabelGraphicsItem::text() const
 {
+    return d->m_text;
+}
+
+void LabelGraphicsItem::setText( const QString& text )
+{
     clear();
     d->m_text = text;
     QFontMetrics metrics( d->font() );
     QSizeF size = metrics.boundingRect( text ).size() + QSizeF( 14, 2 );
-    if ( size.width() < minWidth )
-        size.setWidth( minWidth );
-    if ( size.height() < minHeight )
-        size.setHeight( minHeight );
-    setContentSize( size );
+    d->m_calculatedSize = size;
+    d->updateSize();
 
     update();
 }
 
+QImage LabelGraphicsItem::image() const
+{
+    return d->m_image;
+}
+
 void LabelGraphicsItem::setImage( const QImage& image, const QSize& size )
 {
     clear();
     d->m_image = image;
     if ( size.isEmpty() ) {
-        setContentSize( image.size() );
+        d->m_calculatedSize = image.size();
     }
     else {
-        setContentSize( size );
+        d->m_calculatedSize = size;
     }
+    d->updateSize();
 
     update();
 }
 
+QIcon LabelGraphicsItem::icon() const
+{
+    return d->m_icon;
+}
+
 void LabelGraphicsItem::setIcon( const QIcon& icon, const QSize& size )
 {
     clear();
     d->m_icon = icon;
-    setContentSize( size );
+    d->m_calculatedSize = size;
+    d->updateSize();
 
     update();
 }
 
+QSizeF LabelGraphicsItem::minimumSize() const
+{
+    return d->m_minimumSize;
+}
+
+void LabelGraphicsItem::setMinimumSize( const QSizeF& size )
+{
+    d->m_minimumSize = size;
+    d->updateSize();
+
+    update();
+}
+
 void LabelGraphicsItem::clear()
 {
     d->m_text.clear();
     d->m_image = QImage();
     d->m_icon = QIcon();
-    setSize( QSizeF( 0.0, 0.0 ) );
+    d->m_calculatedSize = QSizeF( 0.0, 0.0 );
+    d->updateSize();
 
     update();
 }
--- trunk/KDE/kdeedu/marble/src/lib/graphicsview/LabelGraphicsItem.h #1012840:1012841
@@ -33,10 +33,18 @@
  public:
     explicit LabelGraphicsItem( MarbleGraphicsItem *parent = 0 );
 
-    void setText( const QString& text, int minWidth = 0, int minHeight = 0 );
+    QString text() const;
+    void setText( const QString& text );
+
+    QImage image() const;
     void setImage( const QImage& image, const QSize& size = QSize() );
+
+    QIcon icon() const;
     void setIcon( const QIcon& icon, const QSize& size );
 
+    void setMinimumSize( const QSizeF& size );
+    QSizeF minimumSize() const;
+
     void clear();
 
  protected:
--- trunk/KDE/kdeedu/marble/src/lib/graphicsview/LabelGraphicsItem_p.h #1012840:1012841
@@ -12,6 +12,7 @@
 #define LABELGRAPHICSITEMPRIVATE_H
 
 #include <QtCore/QString>
+#include <QtCore/QSizeF>
 #include <QtGui/QImage>
 #include <QtGui/QIcon>
 
@@ -20,17 +21,26 @@
 namespace Marble
 {
 
+class LabelGraphicsItem;
+
 class LabelGraphicsItemPrivate
 {
  public:
-    LabelGraphicsItemPrivate();
+    LabelGraphicsItemPrivate( LabelGraphicsItem *parent );
 
+    void updateSize();
+
     // TODO: This has to go up to MarbleGraphicsItem
     QFont font() const;
 
     QString m_text;
     QImage m_image;
     QIcon m_icon;
+
+    QSizeF m_minimumSize;
+    QSizeF m_calculatedSize;
+
+    LabelGraphicsItem *m_parent;
 };
 
 } // namespace Marble
--- trunk/KDE/kdeedu/marble/src/plugins/render/weather/WeatherItem.cpp #1012840:1012841
@@ -56,6 +56,10 @@
           m_windDirectionLabel( new LabelGraphicsItem( m_frameItem ) ),
           m_windSpeedLabel( new LabelGraphicsItem( m_frameItem ) )
     {
+        // Setting minimum sizes
+        m_temperatureLabel->setMinimumSize( QSizeF( 0, imageSize.height() ) );
+        m_windSpeedLabel->setMinimumSize( QSizeF( 0, imageSize.height() ) );
+
         MarbleGraphicsGridLayout *topLayout = new MarbleGraphicsGridLayout( 1, 1 );
         parent->setLayout( topLayout );
         topLayout->addItem( m_frameItem, 0, 0 );
@@ -146,7 +150,7 @@
         }
 
         if ( isTemperatureShown() ) {
-            m_temperatureLabel->setText( temperatureString(), 0, imageSize.height() );
+            m_temperatureLabel->setText( temperatureString() );
         }
         else {
             m_temperatureLabel->clear();
@@ -177,7 +181,7 @@
         }
 
         if ( isWindSpeedShown() ) {
-            m_windSpeedLabel->setText( windSpeedString(), 0, imageSize.height() );
+            m_windSpeedLabel->setText( windSpeedString() );
         }
         else {
             m_windSpeedLabel->clear();


More information about the Marble-commits mailing list