[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdepim/kaddressbook
From:       Tobias Koenig <tokoe () kde ! org>
Date:       2008-06-09 21:44:00
Message-ID: 1213047840.808711.30941.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 818872 by tokoe:

Fixed layout issues in jumpbuttonbar and simplified the
locale aware sorting.


 M  +51 -70    jumpbuttonbar.cpp  
 M  +3 -5      jumpbuttonbar.h  
 M  +2 -1      kabcore.cpp  


--- trunk/KDE/kdepim/kaddressbook/jumpbuttonbar.cpp #818871:818872
@@ -43,6 +43,27 @@
 
 #include "jumpbuttonbar.h"
 
+static bool localAwareLessThan( const QString &left, const QString &right )
+{
+  return ( QString::localeAwareCompare( left, right ) < 0 );
+}
+
+static QSize calculateButtonSize( QWidget *parent )
+{
+  QStyleOption opt;
+  QFontMetrics fm = parent->fontMetrics();
+
+  QPushButton *button = new QPushButton( "", parent );
+  button->hide();
+
+  const QSize buttonSize = parent->style()->sizeFromContents( QStyle::CT_PushButton, \
&opt, +                           fm.size( Qt::TextShowMnemonic, " X - X " \
).expandedTo( QApplication::globalStrut() ), +                           button );
+  delete button;
+
+  return buttonSize;
+}
+
 class JumpButton : public QPushButton
 {
   public:
@@ -60,10 +81,18 @@
   : QPushButton( "", parent ), mChar( firstChar )
 {
   setCheckable( true );
+
   if ( !lastChar.isEmpty() )
     setText( QString( "%1 - %2" ).arg( firstChar.toUpper() ).arg( lastChar.toUpper() \
) );  else
     setText( firstChar.toUpper() );
+
+  static QSize buttonSize;
+  if ( !buttonSize.isValid() )
+    buttonSize = calculateButtonSize( this );
+
+  setFixedWidth( buttonSize.width() + 10 );  // +10 for buggy oxygen style
+  setMinimumHeight( 1 );
 }
 
 
@@ -71,24 +100,13 @@
   : QWidget( parent ), mCore( core ), mButtonsUpdated( true )
 {
   setObjectName( name );
-  setMinimumSize( 1, 1 );
 
-  QVBoxLayout *layout = new QVBoxLayout( this );
-  layout->setMargin( 0 );
-  layout->setSpacing( 0 );
+  mLayout = new QVBoxLayout( this );
+  mLayout->setMargin( 5 );
+  mLayout->setSpacing( 5 );
 
-  mGroupBox = new QGroupBox( this );
-  mGroupBox->setLayout( new QVBoxLayout );
-  mGroupBox->layout()->setSpacing( 0 );
-  mGroupBox->layout()->setMargin( 0 );
-  mGroupBox->setFlat( true );
-
-  layout->addWidget( mGroupBox );
-
   mButtonGroup = new QButtonGroup;
   mButtonGroup->setExclusive( true );
-
-  setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Ignored );
 }
 
 JumpButtonBar::~JumpButtonBar()
@@ -108,18 +126,11 @@
   QStringList characters;
 
   // calculate how many buttons are possible
-  QFontMetrics fm = fontMetrics();
-  QPushButton *btn = new QPushButton( "", this );
-  btn->hide();
-  QStyleOption opt;
-  QSize buttonSize = style()->sizeFromContents( QStyle::CT_PushButton, &opt,
-                     fm.size( Qt::TextShowMnemonic, "X - X" ).expandedTo( \
                QApplication::globalStrut() ),
-                     btn );
-  delete btn;
 
-  int buttonHeight = buttonSize.height() + 8;
+  int buttonHeight = calculateButtonSize( this ).height() + 8;
   int possibleButtons = (height() / buttonHeight) - 1;
 
+  // collect all characters that could be appear
   QString character;
   KABC::AddressBook *ab = mCore->addressBook();
   KABC::AddressBook::Iterator it;
@@ -139,21 +150,27 @@
       characters.append( character );
   }
 
-  sortListLocaleAware( characters );
+  // sort the collected characters locale aware
+  qSort( characters.begin(), characters.end(), localAwareLessThan );
 
+  // create a new button for every character
   if ( characters.count() <= possibleButtons ) {
-    // at first the easy case: all buttons fits in window
+    // at first the easy case: all buttons fits in window, so we assign one \
character per button  for ( int i = 0; i < characters.count(); ++i ) {
       JumpButton *button = new JumpButton( characters[ i ], QString(),
-                                           mGroupBox );
-      mGroupBox->layout()->addWidget( button );
+                                           this );
+      mLayout->addWidget( button );
       mButtonGroup->addButton( button, mButtonGroup->buttons().size() );
       connect( button, SIGNAL( clicked() ), this, SLOT( letterClicked() ) );
       mButtons.append( button );
     }
   } else {
+    // there is not enough space to put every character on its own button,
+    // so we create sequences (e.g. 'A - C', 'D - F')
+
     if ( possibleButtons == 0 ) // to avoid crashes on startup
       return;
+
     int offset = characters.count() / possibleButtons;
     int odd = characters.count() % possibleButtons;
     if ( odd )
@@ -165,8 +182,8 @@
         continue;
       if ( characters.count() - current <= possibleButtons - i ) {
         JumpButton *button = new JumpButton( characters[ current ],
-                                             QString(), mGroupBox );
-        mGroupBox->layout()->addWidget( button );
+                                             QString(), this );
+        mLayout->addWidget( button );
         mButtonGroup->addButton( button, mButtonGroup->buttons().size() );
         connect( button, SIGNAL( clicked() ), this, SLOT( letterClicked() ) );
         mButtons.append( button );
@@ -177,9 +194,10 @@
         QString range;
         for ( int j = current; j < pos + 1; ++j )
           range.append( characters[ j ] );
+
         JumpButton *button = new JumpButton( characters[ current ],
-                                             characters[ pos ], mGroupBox );
-        mGroupBox->layout()->addWidget( button );
+                                             characters[ pos ], this );
+        mLayout->addWidget( button );
         mButtonGroup->addButton( button, mButtonGroup->buttons().size() );
         connect( button, SIGNAL( clicked() ), this, SLOT( letterClicked() ) );
         mButtons.append( button );
@@ -192,6 +210,8 @@
     mButtonGroup->button( currentButton )->setChecked( true );
   else if ( mButtonGroup->buttons().size() )
     mButtonGroup->button( 0 )->setChecked( true );
+
+  mLayout->activate();
 }
 
 void JumpButtonBar::letterClicked()
@@ -210,43 +230,4 @@
   }
 }
 
-class SortContainer
-{
-  public:
-    SortContainer() {}
-    SortContainer( const QString &string )
-      : mString( string )
-    {
-    }
-
-    bool operator< ( const SortContainer &cnt ) const
-    {
-      return ( QString::localeAwareCompare( mString, cnt.mString ) < 0 );
-    }
-
-    QString data() const
-    {
-      return mString;
-    }
-
-  private:
-    QString mString;
-};
-
-void JumpButtonBar::sortListLocaleAware( QStringList &list )
-{
-  QList<SortContainer> sortList;
-
-  QStringList::ConstIterator it;
-  for ( it = list.begin(); it != list.end(); ++it )
-    sortList.append( SortContainer( *it ) );
-
-  qSort( sortList.begin(), sortList.end() );
-  list.clear();
-
-  QList<SortContainer>::ConstIterator sortIt;
-  for ( sortIt = sortList.begin(); sortIt != sortList.end(); ++sortIt )
-    list.append( (*sortIt).data() );
-}
-
 #include "jumpbuttonbar.moc"
--- trunk/KDE/kdepim/kaddressbook/jumpbuttonbar.h #818871:818872
@@ -24,10 +24,9 @@
 #ifndef JUMPBUTTONBAR_H
 #define JUMPBUTTONBAR_H
 
-#include <QWidget>
-#include <QStringList>
-//Added by qt3to4:
 #include <QResizeEvent>
+#include <QStringList>
+#include <QWidget>
 
 class QButtonGroup;
 class QGroupBox;
@@ -67,10 +66,9 @@
     virtual void resizeEvent( QResizeEvent* );
 
   private:
-    void sortListLocaleAware( QStringList &list );
-
     KAB::Core *mCore;
 
+    QLayout *mLayout;
     QButtonGroup *mButtonGroup;
     QGroupBox *mGroupBox;
     QList<JumpButton*> mButtons;
--- trunk/KDE/kdepim/kaddressbook/kabcore.cpp #818871:818872
@@ -1129,6 +1129,7 @@
 
   mDetailsPage = new QWidget;
   mDetailsLayout->addWidget( mDetailsPage );
+  mDetailsLayout->setStretchFactor( mDetailsPage, 1 );
 
   QHBoxLayout *detailsPageLayout = new QHBoxLayout( mDetailsPage );
   detailsPageLayout->setSpacing( 0 );
@@ -1161,7 +1162,7 @@
 {
   mJumpButtonBar = new JumpButtonBar( this, mDetailsWidget );
   mDetailsLayout->addWidget( mJumpButtonBar );
-  mDetailsLayout->setStretchFactor( mJumpButtonBar, 1 );
+  mDetailsLayout->setStretchFactor( mJumpButtonBar, 0 );
 
   connect( mJumpButtonBar, SIGNAL( jumpToLetter( const QString& ) ),
            SLOT( incrementalJumpButtonSearch( const QString& ) ) );


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic