src/hbwidgets/sliders/hbslidertickmarkslabel.cpp
changeset 0 16d8024aca5e
child 2 06ff229162e9
equal deleted inserted replaced
-1:000000000000 0:16d8024aca5e
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbWidgets module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 
       
    26 #include "hbslidertickmarkslabel_p.h"
       
    27 #include "hbslidercontrol_p.h"
       
    28 #include "hbsliderhandle_p.h"
       
    29 #include "hbwidget_p.h"
       
    30 #include <hbinstance.h>
       
    31 #include <hbstyle.h>
       
    32 #include <hbstyleoptionslider.h>
       
    33 #include <hbapplication.h>
       
    34 #include <hbtextitem.h>
       
    35 #include <QList>
       
    36 #include <QStringList>
       
    37 
       
    38 
       
    39 class HbSliderTickmarksLabelPrivate : public HbWidgetPrivate
       
    40 {
       
    41     Q_DECLARE_PUBLIC( HbSliderTickmarksLabel )
       
    42 
       
    43 public:
       
    44     HbSliderTickmarksLabelPrivate();
       
    45     void createTickLabels( );
       
    46     HbStyleOptionSlider sliderOption;
       
    47     QList<QGraphicsWidget *> tickmarkmajorIconItemsLabel;
       
    48     QList<QGraphicsWidget *> tickmarkminorIconItemsLabel;
       
    49     HbSliderControl *sliderControl;
       
    50     Hb::SliderTickPositions tickPosition;
       
    51     bool createText;
       
    52 
       
    53 };
       
    54 
       
    55 
       
    56  HbSliderTickmarksLabelPrivate::HbSliderTickmarksLabelPrivate() :HbWidgetPrivate(){
       
    57     tickmarkmajorIconItemsLabel.clear();
       
    58     tickmarkminorIconItemsLabel.clear();
       
    59     sliderControl = 0;
       
    60     tickPosition = Hb::NoSliderTicks;
       
    61     createText = true;
       
    62 
       
    63 
       
    64 }
       
    65 
       
    66 
       
    67 void  HbSliderTickmarksLabelPrivate::createTickLabels(  )
       
    68 {
       
    69     Q_Q ( HbSliderTickmarksLabel );
       
    70     if (!createText) {
       
    71         return;
       
    72     }
       
    73     
       
    74     int minimum = sliderControl->minimum();
       
    75     int maximum = sliderControl->maximum();
       
    76     QStringList majorLabelList = sliderControl->majorTickLabels( );
       
    77     QStringList minorLabelList = sliderControl->minorTickLabels( );
       
    78     int majorTickInterval = sliderControl->majorTickInterval ( );
       
    79     int minorTickInterval = sliderControl->minorTickInterval ( );
       
    80     if (majorTickInterval) {
       
    81         int totalMajorTicksLabel = ((maximum-minimum)/majorTickInterval)+1;
       
    82         if (majorLabelList.length( ) < totalMajorTicksLabel ) {
       
    83             totalMajorTicksLabel = majorLabelList.length( );
       
    84         }
       
    85         int majorLabelListLength =  tickmarkmajorIconItemsLabel.length();
       
    86         for (int i=majorLabelListLength;i<totalMajorTicksLabel;i++) {
       
    87             QGraphicsItem *textItem = q->style()->createPrimitive(HbStyle::P_SliderTickMark_majorlabel, q);
       
    88             Q_ASSERT(textItem->isWidget());
       
    89             tickmarkmajorIconItemsLabel.append(static_cast<QGraphicsWidget *>(textItem));//add newly defind primitive
       
    90         }
       
    91         while ( totalMajorTicksLabel < tickmarkmajorIconItemsLabel.length() ) {
       
    92             QGraphicsWidget *textItem = tickmarkmajorIconItemsLabel.at(totalMajorTicksLabel);
       
    93             tickmarkmajorIconItemsLabel.removeAll(textItem);
       
    94             delete textItem;
       
    95         }
       
    96     }
       
    97     if ( minorTickInterval > 0) {
       
    98         int totalMinorTicksLabel = ((maximum-minimum)/minorTickInterval)+1;
       
    99         if ( majorTickInterval ) {
       
   100             int maximumMinorTicksLabel = totalMinorTicksLabel;
       
   101             for (int i=0;i< maximumMinorTicksLabel;i++ ) {
       
   102                 if ((i*minorTickInterval)%majorTickInterval==0) {
       
   103                     totalMinorTicksLabel--;
       
   104                 }
       
   105             }
       
   106         }
       
   107         if (minorLabelList.length( ) < totalMinorTicksLabel ) {
       
   108             totalMinorTicksLabel = minorLabelList.length( );
       
   109         }
       
   110 
       
   111         int minorIconLabelListLength =  tickmarkminorIconItemsLabel.length();
       
   112         for (int i=minorIconLabelListLength;i<totalMinorTicksLabel;i++) {
       
   113             QGraphicsItem *textItem = q->style()->createPrimitive(HbStyle::P_SliderTickMark_minorlabel, q);
       
   114             Q_ASSERT(textItem->isWidget());
       
   115             tickmarkminorIconItemsLabel.append(static_cast<QGraphicsWidget *>(textItem));//add newly defind primitive
       
   116         }
       
   117         while (totalMinorTicksLabel < tickmarkminorIconItemsLabel.length() ){
       
   118             QGraphicsWidget *textItem = tickmarkminorIconItemsLabel.at(totalMinorTicksLabel);
       
   119             tickmarkminorIconItemsLabel.removeAll(textItem);
       
   120             delete textItem;
       
   121         }
       
   122     }else {
       
   123         while (tickmarkminorIconItemsLabel.length() > 0){
       
   124             QGraphicsWidget *textItem = tickmarkminorIconItemsLabel.at(0);
       
   125             tickmarkminorIconItemsLabel.removeAll(textItem);
       
   126             delete textItem;
       
   127         }
       
   128     }
       
   129     q->setProperty("state","normal");
       
   130 }
       
   131 
       
   132 void HbSliderTickmarksLabel::resizeEvent(QGraphicsSceneResizeEvent *event)
       
   133 {
       
   134     Q_UNUSED (event);
       
   135     repolish ( );
       
   136     updateTickLabels();
       
   137     HbWidget::resizeEvent(event);
       
   138 }
       
   139 
       
   140 void HbSliderTickmarksLabel::updateTickLabels( )
       
   141 {
       
   142     Q_D ( HbSliderTickmarksLabel );
       
   143     if (!d->createText) {
       
   144         return;
       
   145     }
       
   146     d->createTickLabels();
       
   147     setLabelSize( );
       
   148     int minimum = d->sliderControl->minimum();
       
   149     int maximum = d->sliderControl->maximum();
       
   150     int majorTickInterval = d->sliderControl->majorTickInterval ( );
       
   151     int minorTickInterval = d->sliderControl->minorTickInterval ( );
       
   152     qreal span = 0;
       
   153     bool rtlLayout = (((d->sliderControl->orientation( ) != Qt::Vertical)
       
   154         &&(HbApplication::layoutDirection() == Qt::LeftToRight))?false:true);
       
   155     HbSliderHandle *handle = dynamic_cast <HbSliderHandle *> (d->sliderControl->primitive (HbStyle::P_Slider_thumb));
       
   156     if ( d->sliderControl->orientation() == Qt::Horizontal) {
       
   157         span = d->sliderControl->size().width();
       
   158         span-=handle->size().width();
       
   159     }
       
   160     if ( d->sliderControl->orientation() == Qt::Vertical) {
       
   161         span = d->sliderControl->size().height();
       
   162         span-=handle->size().height();
       
   163     }
       
   164     if (majorTickInterval) {
       
   165         int totalMajorTicksLabel = d->tickmarkmajorIconItemsLabel.length();
       
   166         for (int i=0;i<totalMajorTicksLabel;i++) {
       
   167              QGraphicsWidget *textItem = d->tickmarkmajorIconItemsLabel.at ( i);
       
   168              int pos = QStyle::sliderPositionFromValue( minimum, maximum,
       
   169                 minimum+majorTickInterval*i,static_cast<int>( span ), rtlLayout );
       
   170 
       
   171              if ( d->sliderControl->orientation() == Qt::Horizontal) {
       
   172                 textItem->setPreferredHeight(boundingRect().height());
       
   173                 textItem->update();
       
   174                 qreal correctedPosX = handle->size().width()/2+pos;
       
   175                 qreal tickWidth = textItem->boundingRect().size().width();
       
   176                 correctedPosX -= tickWidth/2;
       
   177                 qreal correctedPosY = 0;
       
   178                 textItem->setPos( correctedPosX,correctedPosY );
       
   179             } else {
       
   180                 qreal correctedPosY = handle->size().height()/2+pos;
       
   181                 qreal tickHeight = textItem->boundingRect().size().height();
       
   182                 correctedPosY-=tickHeight/2;
       
   183                 qreal correctedPosX =0;
       
   184                 textItem->setPos ( correctedPosX,correctedPosY );
       
   185                 textItem->setLayoutDirection (layoutDirection());
       
   186             }
       
   187         }
       
   188     }
       
   189     if (minorTickInterval) {
       
   190         int totalminorTicks = ((maximum-minimum)/minorTickInterval)+1;
       
   191         int minorIndex = 0;
       
   192         for (int i=0;i<totalminorTicks;i++) {
       
   193             if (majorTickInterval ) {
       
   194                 if (i*minorTickInterval%majorTickInterval== 0) {
       
   195                     continue;
       
   196                 }
       
   197             }
       
   198             if ( minorIndex >= d->tickmarkminorIconItemsLabel.length() ) {
       
   199                 continue;
       
   200             } else {
       
   201                 QGraphicsWidget *textItem = d->tickmarkminorIconItemsLabel.at ( minorIndex);
       
   202                 HbStyleOptionSlider opt;
       
   203                 initStyleOption(&opt);
       
   204                 opt.orientation = d->sliderControl->orientation();
       
   205                 opt.text = (d->sliderControl->minorTickLabels( )).at(minorIndex);
       
   206                 style()->updatePrimitive(textItem,HbStyle::P_SliderTickMark_minorlabel,&opt);
       
   207                 minorIndex++;
       
   208                 int pos = QStyle::sliderPositionFromValue( minimum, maximum,
       
   209                     minimum+minorTickInterval*i,static_cast<int>( span ), rtlLayout );
       
   210                  if ( d->sliderControl->orientation() == Qt::Horizontal) {
       
   211                     qreal correctedPosX = handle->size().width()/2+pos; 
       
   212                     qreal tickWidth = textItem->boundingRect().size().width();
       
   213                     correctedPosX -= tickWidth/2;
       
   214                     qreal correctedPosY = 0;
       
   215                     textItem->setPos ( correctedPosX,correctedPosY );
       
   216                 } else {
       
   217                     qreal correctedPosY = handle->size().height()/2+pos;
       
   218                     qreal tickHeight = textItem->boundingRect().size().height();
       
   219                     correctedPosY-=tickHeight/2;
       
   220                     textItem->setLayoutDirection (layoutDirection());
       
   221                     qreal correctedPosX =0;
       
   222                     textItem->setPos ( correctedPosX,correctedPosY );
       
   223                 }
       
   224             }
       
   225         }
       
   226     }
       
   227  }
       
   228 
       
   229 
       
   230 
       
   231  void HbSliderTickmarksLabel::setTickPosition(Hb::SliderTickPositions position)
       
   232 {
       
   233     Q_D(HbSliderTickmarksLabel);
       
   234     d->tickPosition = position;
       
   235     if( d->tickPosition & Hb::SliderTicksLeft ) {
       
   236          setProperty("TicksLeft",true);
       
   237          setProperty("TicksRight",false);
       
   238     }
       
   239     else if(d->tickPosition & Hb::SliderTicksRight) {
       
   240          setProperty("TicksRight",true);
       
   241          setProperty("TicksLeft",false);
       
   242     }
       
   243 }
       
   244 
       
   245 
       
   246 /*!
       
   247     This class is internal to slider ,
       
   248     this creates  ticks mark label widget in slider
       
   249 */
       
   250 
       
   251 /*!
       
   252     constructor
       
   253 */
       
   254 HbSliderTickmarksLabel::HbSliderTickmarksLabel( QGraphicsItem *parent )
       
   255     : HbWidget( *new HbSliderTickmarksLabelPrivate, parent )
       
   256 {
       
   257     Q_D( HbSliderTickmarksLabel );
       
   258     d->q_ptr = this;
       
   259     d->sliderControl=dynamic_cast<HbSliderControl*>( parentItem() );
       
   260     d->createTickLabels();
       
   261 }
       
   262 
       
   263 /*!
       
   264     destructor
       
   265 */
       
   266 HbSliderTickmarksLabel::~HbSliderTickmarksLabel()
       
   267 {
       
   268 }
       
   269 
       
   270 void HbSliderTickmarksLabel::setLabelSize()
       
   271 {
       
   272     Q_D (HbSliderTickmarksLabel);
       
   273 
       
   274     int minimum = d->sliderControl->minimum();
       
   275     int maximum = d->sliderControl->maximum();
       
   276     int majorTickInterval = d->sliderControl->majorTickInterval ( );
       
   277     int minorTickInterval = d->sliderControl->minorTickInterval ( );
       
   278     qreal span = 0;
       
   279     bool rtlLayout = (((d->sliderControl->orientation( ) != Qt::Vertical)
       
   280         &&(HbApplication::layoutDirection() == Qt::LeftToRight))?false:true);
       
   281     HbSliderHandle *handle = dynamic_cast <HbSliderHandle *> (d->sliderControl->primitive (HbStyle::P_Slider_thumb));
       
   282     if ( d->sliderControl->orientation() == Qt::Horizontal) {
       
   283         span = d->sliderControl->size().width();
       
   284         span-=handle->size().width();
       
   285     }
       
   286     if ( d->sliderControl->orientation() == Qt::Vertical) {
       
   287         span = d->sliderControl->size().height();
       
   288         span-=handle->size().height();
       
   289     }
       
   290     int minPos = QStyle::sliderPositionFromValue( minimum, maximum,
       
   291                 minimum,static_cast<int>( span ), rtlLayout );
       
   292     int firstMajorIntervalPos = QStyle::sliderPositionFromValue( minimum, maximum,
       
   293                 minimum+majorTickInterval,static_cast<int>( span ), rtlLayout );
       
   294 
       
   295     int firstMinorIntervalPos = QStyle::sliderPositionFromValue( minimum, maximum,
       
   296                 minimum+minorTickInterval,static_cast<int>( span ), rtlLayout );
       
   297 
       
   298     qreal totalMajorTextWidth = abs(firstMajorIntervalPos-minPos);
       
   299     qreal totalMinorTextWidth = abs(firstMinorIntervalPos-minPos);
       
   300 
       
   301     if (majorTickInterval) {
       
   302         int totalMajorTicksLabel = d->tickmarkmajorIconItemsLabel.length();
       
   303         for (int i=0;i<totalMajorTicksLabel;i++) {
       
   304              QGraphicsWidget *textItem = d->tickmarkmajorIconItemsLabel.at ( i);
       
   305              HbStyleOptionSlider opt;
       
   306              initStyleOption(&opt);
       
   307              opt.orientation = d->sliderControl->orientation();
       
   308              opt.text = (d->sliderControl->majorTickLabels( )).at(i);
       
   309              style()->updatePrimitive(textItem,HbStyle::P_SliderTickMark_majorlabel,&opt);
       
   310              if ( d->sliderControl->orientation() == Qt::Horizontal) {
       
   311                 textItem->setMaximumHeight (boundingRect().height());
       
   312                 textItem->setMinimumHeight (boundingRect().height());
       
   313                 textItem->setMinimumWidth(totalMajorTextWidth);
       
   314                 textItem->setMaximumWidth(totalMajorTextWidth);
       
   315                 textItem->update();
       
   316              } else {
       
   317                 textItem->setMinimumWidth(boundingRect().width());
       
   318                 textItem->setMaximumWidth(boundingRect().width());
       
   319                 textItem->update( );
       
   320              }
       
   321         }
       
   322     }
       
   323     if (minorTickInterval) {
       
   324         int totalminorTicks = ((maximum-minimum)/minorTickInterval)+1;
       
   325         int minorIndex = 0;
       
   326         for (int i=0;i<totalminorTicks;i++) {
       
   327             if (majorTickInterval ) {
       
   328                 if (i*minorTickInterval%majorTickInterval== 0) {
       
   329                     continue;
       
   330                 }
       
   331             }
       
   332             if ( minorIndex < d->tickmarkminorIconItemsLabel.length() ) {
       
   333                 QGraphicsWidget *textItem = d->tickmarkminorIconItemsLabel.at ( minorIndex);
       
   334                 minorIndex++;
       
   335                  if ( d->sliderControl->orientation() == Qt::Horizontal) {
       
   336                     textItem->setMaximumHeight (boundingRect().height());
       
   337                     textItem->setMinimumHeight (boundingRect().height());
       
   338                     textItem->setMinimumWidth(totalMinorTextWidth);
       
   339                     textItem->setMaximumWidth(totalMinorTextWidth);
       
   340                     textItem->update();
       
   341                  } else {
       
   342                     textItem->setMinimumWidth(boundingRect().width());
       
   343                     textItem->setMaximumWidth(boundingRect().width());
       
   344                     textItem->update( );
       
   345                  }
       
   346             }
       
   347         }
       
   348     }
       
   349 }
       
   350 
       
   351 void HbSliderTickmarksLabel::createText(bool create)
       
   352 {
       
   353     Q_D(HbSliderTickmarksLabel);
       
   354     d->createText = create;
       
   355     if (!create) {
       
   356         while ( d->tickmarkmajorIconItemsLabel.length() > 0) {
       
   357             QGraphicsWidget *textItem = d->tickmarkmajorIconItemsLabel.at(0);
       
   358             d->tickmarkmajorIconItemsLabel.removeAll(textItem);
       
   359             delete textItem;
       
   360         }
       
   361         while ( d->tickmarkminorIconItemsLabel.length() > 0) {
       
   362             QGraphicsWidget *textItem = d->tickmarkminorIconItemsLabel.at(0);
       
   363             d->tickmarkminorIconItemsLabel.removeAll(textItem);
       
   364             delete textItem;
       
   365         } 
       
   366     } else {
       
   367         d->createTickLabels();
       
   368     }
       
   369 
       
   370 
       
   371 }
       
   372 
       
   373 
       
   374 
       
   375 void HbSliderTickmarksLabel::polish( HbStyleParameters& params )
       
   376 {
       
   377     Q_D (HbSliderTickmarksLabel);
       
   378     d->createTickLabels();
       
   379     if( d->sliderControl->orientation( ) == Qt::Horizontal ) {
       
   380         setProperty("orientation",(Qt::Orientation)1);
       
   381     } else {
       
   382         setProperty("orientation",(Qt::Orientation)2);
       
   383     }
       
   384     HbWidget::polish(params);
       
   385     updateTickLabels();
       
   386 }
       
   387 
       
   388 /*!
       
   389     \reimp
       
   390 
       
   391     This api update the primitive when ever item enable has changed
       
   392  */
       
   393 QVariant HbSliderTickmarksLabel::itemChange( GraphicsItemChange change, const QVariant &value )
       
   394 {
       
   395     switch( change )
       
   396     {
       
   397         case ItemChildAddedChange:
       
   398         case ItemChildRemovedChange:
       
   399             repolish();
       
   400             break;
       
   401         case ItemPositionHasChanged:
       
   402             repolish( );
       
   403             updateTickLabels();
       
   404             break;
       
   405 
       
   406         default:
       
   407             break;
       
   408     }
       
   409     return HbWidget::itemChange( change, value );
       
   410 }
       
   411 
       
   412 
       
   413 void HbSliderTickmarksLabel::initStyleOption( HbStyleOptionSlider *option ) const
       
   414 {
       
   415     Q_D( const HbSliderTickmarksLabel );
       
   416     if ( !option ) {
       
   417         return;
       
   418     }
       
   419     HbWidget::initStyleOption( option );
       
   420     option->orientation = d->sliderControl->orientation( );
       
   421     option->tickPosition =  d->sliderControl->tickPosition( );
       
   422     option->upsideDown = ( d->sliderControl->orientation( ) == Qt::Horizontal ) 
       
   423         ? ( d->sliderControl->invertedAppearance( ) != ( option->direction == Qt::RightToLeft ) )
       
   424             : ( !d->sliderControl->invertedAppearance( ) );
       
   425     // we use the upsideDown option instead
       
   426 }
       
   427 
       
   428  //end of file
       
   429