src/profileswidget.cpp
changeset 1 67589f86e56b
parent 0 dc71378f4890
equal deleted inserted replaced
0:dc71378f4890 1:67589f86e56b
    19 #include <HbPushButton>
    19 #include <HbPushButton>
    20 #include <QGraphicsLinearLayout>
    20 #include <QGraphicsLinearLayout>
    21 #include <HbStyle>
    21 #include <HbStyle>
    22 #include <HbDocumentLoader>
    22 #include <HbDocumentLoader>
    23 #include <HbFrameDrawer>
    23 #include <HbFrameDrawer>
       
    24 #include <QFontMetrics>
    24 
    25 
    25 // User includes
    26 // User includes
    26 #include "profileswidget.h"
    27 #include "profileswidget.h"
    27 #include "profileswidgetconsts.h"
    28 #include "profileswidgetconsts.h"
    28 #include "profilesmodel.h"
    29 #include "profilesmodel.h"
    58     if (!mButton) {
    59     if (!mButton) {
    59         // fail to find push button
    60         // fail to find push button
    60         return;
    61         return;
    61     }
    62     }
    62     
    63     
    63     mButton->setText(mModel->activeProfileName());
    64     QString text = mModel->activeProfileName();
       
    65     mButton->setText(text);   
       
    66     adjustButtonWidth(text);
    64     HbFrameDrawer *background = new HbFrameDrawer("qtg_fr_hsshortcut_normal", 
    67     HbFrameDrawer *background = new HbFrameDrawer("qtg_fr_hsshortcut_normal", 
    65             HbFrameDrawer::NinePieces);
    68             HbFrameDrawer::NinePieces);
    66     mButton->setFrameBackground(background);
    69     mButton->setFrameBackground(background);
    67     mainLayout->addItem(mButton);
    70     mainLayout->addItem(mButton);
    68     connect(mButton, SIGNAL(pressed()), this, SLOT(onButtonPressed()));    
    71     connect(mButton, SIGNAL(pressed()), this, SLOT(onButtonPressed()));    
    69     
    72     
    70     setLayout(mainLayout);
    73     setLayout(mainLayout);
       
    74 }
       
    75 
       
    76 /*!
       
    77     Adjust button width according to text length
       
    78 */
       
    79 void ProfilesWidget::adjustButtonWidth(const QString &text)
       
    80 { 
       
    81     HbFontSpec *sysFontSpec = new HbFontSpec(HbFontSpec::Secondary);
       
    82     QFont sysFont = sysFontSpec->font();
       
    83     QFontMetrics metrics( sysFont );
       
    84 
       
    85     int textWidth = 0;
       
    86     for (int i = 0; i < text.length(); i++) {
       
    87         textWidth += metrics.width(text.at(i));
       
    88     }
       
    89     mButton->setMaximumWidth((textWidth > ProfielsWidgetMinimumWidthInPx 
       
    90         ? textWidth : ProfielsWidgetMinimumWidthInPx) + 
       
    91                 ProfilesWidgetWidthSpaceInPx);
       
    92     mButton->setMinimumWidth((textWidth > ProfielsWidgetMinimumWidthInPx 
       
    93         ? textWidth : ProfielsWidgetMinimumWidthInPx) + 
       
    94                 ProfilesWidgetWidthSpaceInPx);
       
    95         
       
    96     delete sysFontSpec;
    71 }
    97 }
    72 
    98 
    73 /*!
    99 /*!
    74     Handle toggled event of button
   100     Handle toggled event of button
    75 */
   101 */
    79     if (profileId != ProfilesWidgetGeneralProfileId) {
   105     if (profileId != ProfilesWidgetGeneralProfileId) {
    80         mModel->activateProfile(ProfilesWidgetGeneralProfileId);
   106         mModel->activateProfile(ProfilesWidgetGeneralProfileId);
    81     } else {
   107     } else {
    82         mModel->activateProfile(ProfilesWidgetMeetingProfileId);
   108         mModel->activateProfile(ProfilesWidgetMeetingProfileId);
    83     }
   109     }
    84     mButton->setText(mModel->activeProfileName());
   110 
       
   111     QString text = mModel->activeProfileName();
       
   112     mButton->setText(text);
       
   113     adjustButtonWidth(text);
    85 }
   114 }
    86 
   115 
    87 /*!
   116 /*!
    88     Change icon of widget
   117     Change icon of widget
    89 */
   118 */
    90 void ProfilesWidget::changeIcon()
   119 void ProfilesWidget::changeIcon()
    91 {
   120 {
    92     mButton->setText(mModel->activeProfileName());
   121     QString text = mModel->activeProfileName();
       
   122     mButton->setText(text);
       
   123     adjustButtonWidth(text);
    93 }
   124 }
    94 
   125 
    95 /*!
   126 /*!
    96     Destroyer
   127     Destroyer
    97 */
   128 */