emailuis/nmailuiwidgets/src/nmattachmentlistitem.cpp
branchGCC_SURGE
changeset 55 cdd802add233
parent 53 bf7eb7911fc5
child 56 15bc1d5d6267
equal deleted inserted replaced
28:011f79704660 55:cdd802add233
    21 static const QString FILE_PATH_CSS = ":nmattachmentlistitem.css";
    21 static const QString FILE_PATH_CSS = ":nmattachmentlistitem.css";
    22 
    22 
    23 static const int PROGRESSBAR_MIN = 0; 
    23 static const int PROGRESSBAR_MIN = 0; 
    24 static const int PROGRESSBAR_MAX = 100;
    24 static const int PROGRESSBAR_MAX = 100;
    25 static const int PROGRESSBAR_HIDE_COUNTDOWN = 500;
    25 static const int PROGRESSBAR_HIDE_COUNTDOWN = 500;
    26 static const int LONGPRESS_TIMER = 2000;
       
    27 
       
    28 // Hardcoded file size length. Maximum (999.9 Mb) fits into size field.
       
    29 static const int FILE_SIZE_FIELD_LENGTH = 120;
       
    30 
    26 
    31 /*!
    27 /*!
    32  @nmailuiwidgets
    28  @nmailuiwidgets
    33  \class NmAttachmentListItem
    29  \class NmAttachmentListItem
    34 
    30 
    45  */
    41  */
    46 NmAttachmentListItem::NmAttachmentListItem(QGraphicsItem *parent)
    42 NmAttachmentListItem::NmAttachmentListItem(QGraphicsItem *parent)
    47     : HbWidget( parent ),
    43     : HbWidget( parent ),
    48       mFileNameText(NULL),
    44       mFileNameText(NULL),
    49       mFileSizeText(NULL),
    45       mFileSizeText(NULL),
    50       mProgressBar(NULL),
    46       mProgressBar(NULL)
    51       mTimer(NULL),
    47 {
    52       mButtonPressed(false),
    48     NM_FUNCTION;
    53       mLongPressedPoint(0,0)
    49     
    54 {
       
    55     init( );
    50     init( );
       
    51 	
       
    52     // Informs GestureFramework that NmAttachmentListItem widget is interested 
       
    53     // Tap gesture and TapAndHold gesture.
       
    54     grabGesture(Qt::TapGesture);
    56 }
    55 }
    57 
    56 
    58 /*!
    57 /*!
    59     Setter for items text color override. This fucntion can be used
    58     Setter for items text color override. This fucntion can be used
    60     if theme background is not used and text needs to be shown in diferent color.
    59     if theme background is not used and text needs to be shown in diferent color.
    61  */
    60  */
    62 void NmAttachmentListItem::setTextColor(const QColor color)
    61 void NmAttachmentListItem::setTextColor(const QColor color)
    63 {
    62 {
       
    63     NM_FUNCTION;
       
    64     
    64     mTextColor=color;
    65     mTextColor=color;
    65 }
    66 }
    66 
    67 
    67 /*!
    68 /*!
    68     Destructor.
    69     Destructor.
    69  */
    70  */
    70 NmAttachmentListItem::~NmAttachmentListItem( )
    71 NmAttachmentListItem::~NmAttachmentListItem( )
    71 {
    72 {
       
    73     NM_FUNCTION;
       
    74     
    72     HbStyleLoader::unregisterFilePath(FILE_PATH_WIDGETML);
    75     HbStyleLoader::unregisterFilePath(FILE_PATH_WIDGETML);
    73     HbStyleLoader::unregisterFilePath(FILE_PATH_CSS);
    76     HbStyleLoader::unregisterFilePath(FILE_PATH_CSS);
    74     
       
    75     delete mTimer;
       
    76     mTimer = NULL; 
       
    77 }
    77 }
    78 
    78 
    79 /*!
    79 /*!
    80     Set the text to be displayed in the file name item.
    80     Set the text to be displayed in the file name item.
    81  */
    81  */
    82 void NmAttachmentListItem::setFileNameText(const QString &fileName)
    82 void NmAttachmentListItem::setFileNameText(const QString &fileName)
    83 {
    83 {
       
    84     NM_FUNCTION;
       
    85     
    84     if (mFileNameText){
    86     if (mFileNameText){
    85         if (mTextColor.isValid()){
    87         if (mTextColor.isValid()){
    86             mFileNameText->setTextColor(mTextColor);
    88             mFileNameText->setTextColor(mTextColor);
    87         }
    89         }
    88         mFileNameText->setTextWrapping(Hb::TextNoWrap);
    90         mFileNameText->setTextWrapping(Hb::TextNoWrap);
    93 /*!
    95 /*!
    94     Set the text to be displayed in the file size item
    96     Set the text to be displayed in the file size item
    95  */
    97  */
    96 void NmAttachmentListItem::setFileSizeText(const QString &fileSize)
    98 void NmAttachmentListItem::setFileSizeText(const QString &fileSize)
    97 {
    99 {
       
   100     NM_FUNCTION;
       
   101     
    98     if (mFileSizeText){
   102     if (mFileSizeText){
    99         if (mTextColor.isValid()){
   103         if (mTextColor.isValid()){
   100             mFileSizeText->setTextColor(mTextColor);
   104             mFileSizeText->setTextColor(mTextColor);
   101         }
   105         }
   102         mFileSizeText->setTextWrapping(Hb::TextNoWrap);
   106         mFileSizeText->setTextWrapping(Hb::TextNoWrap);
   103         mFileSizeText->setText(fileSize);
   107         mFileSizeText->setText(fileSize);
   104     }
   108     }
   105 }
   109 }
   106 
   110 
   107 /*!
   111 /*!
   108     Set the length of the filename field.
       
   109  */
       
   110 void NmAttachmentListItem::resetFileNameLength(Qt::Orientation orientation)
       
   111 {
       
   112 	QSizeF reso = screenSize(orientation);
       
   113 	
       
   114 	if (orientation == Qt::Horizontal) {
       
   115         mFileNameText->setPreferredWidth(reso.width() / 2 - FILE_SIZE_FIELD_LENGTH);
       
   116 	}
       
   117 	else {		
       
   118         mFileNameText->setPreferredWidth(reso.width() - FILE_SIZE_FIELD_LENGTH);
       
   119 	}
       
   120 }
       
   121 
       
   122 /*!
       
   123     Set the download progress bar value (0-100)%, if value is 0 progress bar is hidden
   112     Set the download progress bar value (0-100)%, if value is 0 progress bar is hidden
   124  */
   113  */
   125 void NmAttachmentListItem::setProgressBarValue(const int value)
   114 void NmAttachmentListItem::setProgressBarValue(const int value)
   126 {
   115 {
       
   116     NM_FUNCTION;
       
   117     
   127     //first check if value is 0 or below -> hide progressbar
   118     //first check if value is 0 or below -> hide progressbar
   128     if ( 0 >= value ){
   119     if ( 0 >= value ){
   129         removeProgressBar();
   120         removeProgressBar();
   130         return;
   121         return;
   131     }
   122     }
   148 /*!
   139 /*!
   149     Get the download progress bar value
   140     Get the download progress bar value
   150 */
   141 */
   151 int NmAttachmentListItem::progressBarValue() const
   142 int NmAttachmentListItem::progressBarValue() const
   152 {
   143 {
       
   144     NM_FUNCTION;
       
   145     
   153     int ret = 0;
   146     int ret = 0;
   154     if ( mProgressBar ){
   147     if ( mProgressBar ){
   155         ret = mProgressBar->progressValue();
   148         ret = mProgressBar->progressValue();
   156     }
   149     }
   157 
   150 
   161 /*!
   154 /*!
   162     Hides progress bar, used if download is cancelled before 100 precent is reached
   155     Hides progress bar, used if download is cancelled before 100 precent is reached
   163 */
   156 */
   164 void NmAttachmentListItem::hideProgressBar()
   157 void NmAttachmentListItem::hideProgressBar()
   165 {
   158 {
       
   159     NM_FUNCTION;
       
   160     
   166     QTimer::singleShot(PROGRESSBAR_HIDE_COUNTDOWN,this, SLOT(removeProgressBar()));
   161     QTimer::singleShot(PROGRESSBAR_HIDE_COUNTDOWN,this, SLOT(removeProgressBar()));
   167 }
   162 }
   168 
   163 
   169 /*!
   164 /*!
   170     Initialize
   165     Initialize
   171 */
   166 */
   172 void NmAttachmentListItem::init( )
   167 void NmAttachmentListItem::init( )
   173 {
   168 {
       
   169     NM_FUNCTION;
       
   170     
   174     constructUi();
   171     constructUi();
   175 
   172 
   176     //set default values
   173     //set default values
   177     setFlag(QGraphicsItem::ItemIsFocusable);
   174     setFlag(QGraphicsItem::ItemIsFocusable);
   178     setFlag(QGraphicsItem::ItemIsSelectable);
   175     setFlag(QGraphicsItem::ItemIsSelectable);
   179 
       
   180     //set temporary longpress timer
       
   181     mTimer = new QTimer(this);
       
   182     mTimer->setSingleShot(true);
       
   183     connect(mTimer, SIGNAL(timeout()), this, SLOT(longPressedActivated()));
       
   184 }
   176 }
   185 
   177 
   186 /*!
   178 /*!
   187     Constructs the UI, sets style itemnames etc.
   179     Constructs the UI, sets style itemnames etc.
   188 */
   180 */
   189 void NmAttachmentListItem::constructUi()
   181 void NmAttachmentListItem::constructUi()
   190 {
   182 {
       
   183     NM_FUNCTION;
       
   184     
   191     //construct default ui.    
   185     //construct default ui.    
   192     HbStyleLoader::registerFilePath(FILE_PATH_WIDGETML);
   186     HbStyleLoader::registerFilePath(FILE_PATH_WIDGETML);
   193     HbStyleLoader::registerFilePath(FILE_PATH_CSS);
   187     HbStyleLoader::registerFilePath(FILE_PATH_CSS);
   194     
   188     
   195     mFileNameText = new HbTextItem(this); 
   189     mFileNameText = new HbTextItem(this); 
   203     mFileSizeText->setElideMode(Qt::ElideNone);
   197     mFileSizeText->setElideMode(Qt::ElideNone);
   204 }
   198 }
   205 
   199 
   206 
   200 
   207 /*!
   201 /*!
   208     \reimp
       
   209  */
       
   210 void NmAttachmentListItem::mousePressEvent( QGraphicsSceneMouseEvent *event )
       
   211 {
       
   212     NMLOG("NmAttachmentListItem::mousePressEvent");
       
   213 
       
   214     mButtonPressed = true;
       
   215     mLongPressedPoint = event->scenePos();
       
   216     if(mTimer){
       
   217         mTimer->start(LONGPRESS_TIMER);        
       
   218     }
       
   219 }
       
   220 
       
   221 /*!
       
   222     \reimp
       
   223  */
       
   224 void NmAttachmentListItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
       
   225 {
       
   226     Q_UNUSED(event);
       
   227     NMLOG("NmAttachmentListItem::mouseReleasedEvent");
       
   228     if ( mTimer && mButtonPressed ){
       
   229         emit itemActivated();
       
   230         mButtonPressed = false;
       
   231         mTimer->stop();
       
   232     }
       
   233 }
       
   234 
       
   235 /*!
       
   236     Hides the download progress bar
   202     Hides the download progress bar
   237  */
   203  */
   238 void NmAttachmentListItem::removeProgressBar()
   204 void NmAttachmentListItem::removeProgressBar()
   239 {
   205 {
       
   206     NM_FUNCTION;
       
   207     
   240 	if ( mProgressBar ){
   208 	if ( mProgressBar ){
   241 	    HbStyle::setItemName( mProgressBar, "" );
   209 	    HbStyle::setItemName( mProgressBar, "" );
   242 	    mProgressBar->deleteLater();
   210 	    mProgressBar->deleteLater();
   243 	    mProgressBar = 0;
   211 	    mProgressBar = 0;
   244 	    repolish();
   212 	    repolish();
   245 	}
   213 	}
   246 }
   214 }
   247 
   215 
   248 /*!
   216 
   249 
   217 /*!
   250  */
   218     This function handles gestures
   251 void NmAttachmentListItem::longPressedActivated()
   219  */
   252 {
   220 void NmAttachmentListItem::gestureEvent(QGestureEvent *event)
   253     //check first if button is not released already
   221 {
   254     if ( mButtonPressed ){
   222     NM_FUNCTION;
   255         NMLOG("NmAttachmentListItem::longPressedActivated");
   223     
   256         emit itemLongPressed(mLongPressedPoint);
   224     if (HbTapGesture *tap = qobject_cast<HbTapGesture *>(event->gesture(Qt::TapGesture))) {
   257         mButtonPressed = false;
   225         switch(tap->tapStyleHint()) {
   258     }
   226         case HbTapGesture::Tap:
   259 }
   227             {
   260 
   228                 if (tap->state() == Qt::GestureFinished) {
   261 /*!
   229                     emit itemActivated();
   262     This function returns screen size depending on the orientation.
   230                 }
   263     Function is copied from NmApplication.
   231              }
   264  */
   232              break;
   265 QSize NmAttachmentListItem::screenSize(Qt::Orientation orientation)
   233             
   266 {
   234          case HbTapGesture::TapAndHold:
   267     QSize ret(0,0);
   235              {
   268     HbDeviceProfile currentP = HbDeviceProfile::current();
   236                  if (tap->state() == Qt::GestureFinished) {
   269     HbDeviceProfile altP(currentP.alternateProfileName());
   237                  emit itemLongPressed(event->mapToGraphicsScene(tap->position()));
   270     QSize curPSize = currentP.logicalSize();
   238                  }
   271     QSize altPSize = altP.logicalSize();
   239              }    
   272     if (orientation == Qt::Horizontal) {
   240              break;
   273         // Get wide profile size in landscape
       
   274         if (curPSize.width() > altPSize.width()) {
       
   275             ret = curPSize;
       
   276         }
   241         }
   277         else{
       
   278             ret = altPSize;
       
   279         }
       
   280     }
   242     }
   281     else {
   243     else {
   282         // Get narrow profile size in portrait
   244            HbWidget::gestureEvent(event);
   283         if (curPSize.width() < altPSize.width()) {
   245     }
   284             ret = curPSize;
   246 }
   285         }
   247 
   286         else{
   248 
   287             ret = altPSize;
       
   288         }
       
   289     }
       
   290     return ret;
       
   291 }
       
   292