screensaver/screensaverplugins/snsrbigclockscreensaverplugin/src/snsroledanalogclockcontainer.cpp
changeset 77 4b195f3bea29
parent 61 2b1b11a301d2
child 86 e4f038c420f7
equal deleted inserted replaced
61:2b1b11a301d2 77:4b195f3bea29
    19 
    19 
    20 #include <QDebug>
    20 #include <QDebug>
    21 #include <QGraphicsLinearLayout>
    21 #include <QGraphicsLinearLayout>
    22 #include <QTimer>
    22 #include <QTimer>
    23 #include <QTime>
    23 #include <QTime>
    24 #include <hblabel.h>
    24 #include <HbExtendedLocale>
    25 #include <hbextendedlocale.h>
    25 #include <HbMainWindow>
    26 
    26 
    27 #include "snsroledclockwidget.h"
    27 #include "snsroledclockwidget.h"
    28 #include "snsrindicatorwidget.h"
    28 #include "snsrindicatorwidget.h"
       
    29 #include "snsrlabel.h"
    29 
    30 
    30 /*!
    31 /*!
    31     \class SnsrOledAnalogClockContainer
    32     \class SnsrOledAnalogClockContainer
    32     \ingroup group_snsrbigclockscreensaverplugin
    33     \ingroup group_snsrbigclockscreensaverplugin
    33     \brief Container used for preparing layout for oled analog clock.
    34     \brief Container used for preparing layout for oled analog clock.
    55 SnsrOledAnalogClockContainer::SnsrOledAnalogClockContainer() :
    56 SnsrOledAnalogClockContainer::SnsrOledAnalogClockContainer() :
    56     SnsrBigClockContainer(), mClockContainer(0), mOledClockWidget(0),
    57     SnsrBigClockContainer(), mClockContainer(0), mOledClockWidget(0),
    57     mDateLabel(0), mDestPosition(QPointF()), mInitialize(false)
    58     mDateLabel(0), mDestPosition(QPointF()), mInitialize(false)
    58 {
    59 {
    59     SCREENSAVER_TEST_FUNC_ENTRY("SnsrOledAnalogClockContainer::SnsrOledAnalogClockContainer")
    60     SCREENSAVER_TEST_FUNC_ENTRY("SnsrOledAnalogClockContainer::SnsrOledAnalogClockContainer")
    60 
    61     SCREENSAVER_TEST_FUNC_EXIT("SnsrOledAnalogClockContainer::SnsrOledAnalogClockContainer")
    61     bool ok(false);
    62 }
       
    63 
       
    64 /*!
       
    65     Destructs the class.
       
    66  */
       
    67 SnsrOledAnalogClockContainer::~SnsrOledAnalogClockContainer()
       
    68 {
       
    69     resetIndicatorConnections();
       
    70     //mOledClockWidget etc - deleted by the parent
       
    71 }
       
    72 
       
    73 /*!
       
    74     Updates OLED analog clock widget position.
       
    75  */
       
    76 void SnsrOledAnalogClockContainer::updatePosition()
       
    77 {
       
    78     QSizeF containerSize = mMainContainer->size();
       
    79     
       
    80     // Container must have a valid size to enable calculating the 
       
    81     // destination position for the clock.
       
    82     if ( containerSize.width() > 0 && containerSize.height() > 0 ) {
       
    83         containerSize -= mClockContainer->size();
       
    84         QRectF containerRect( mMainContainer->pos(), containerSize );
       
    85         if ( mInitialize ) {
       
    86             // disconnect container from parent layout,
       
    87             // connected container resets its position to the one defined in docml
       
    88             // after label text updates
       
    89             mClockContainer->setParentLayoutItem(0);
       
    90 
       
    91             QPointF clockPos = nextRandomPosition( mClockContainer->pos(), mDestPosition, containerRect );
       
    92             mClockContainer->setPos( clockPos );
       
    93         }
       
    94         else {
       
    95             mDestPosition = randomPosition( containerRect );
       
    96             mInitialize = true;
       
    97         }
       
    98         // the active area of power save mode needs to be updated when clock container is moved
       
    99         emit activeAreaMoved();
       
   100     }
       
   101 }
       
   102 
       
   103 /*!
       
   104     Updates displayed time.
       
   105  */
       
   106 void SnsrOledAnalogClockContainer::update()
       
   107 {
       
   108     SCREENSAVER_TEST_FUNC_ENTRY("SnsrOledAnalogClockContainer::update")
       
   109 
       
   110     // time
       
   111     mOledClockWidget->tick();
       
   112     
       
   113     // date
       
   114     if (mCurrentOrientation == Qt::Vertical) {
       
   115         mDateLabel->setPlainText(
       
   116             HbExtendedLocale().format(QDate::currentDate(), gDateFormatVerticalStr)
       
   117         );
       
   118     } else {
       
   119         mDateLabel->setPlainText(
       
   120             HbExtendedLocale().format(QDate::currentDate(), gDateFormatHorizontalStr)
       
   121         );
       
   122     }
       
   123     
       
   124     // position
       
   125     updatePosition();
       
   126     
       
   127     SCREENSAVER_TEST_FUNC_EXIT("SnsrOledAnalogClockContainer::update")
       
   128 }
       
   129 
       
   130 int SnsrOledAnalogClockContainer::updateIntervalInMilliseconds()
       
   131 {
       
   132     return 60*1000;
       
   133 }
       
   134 
       
   135 /*!
       
   136     @copydoc SnsrBigClockContainer::getActiveScreenRows()
       
   137  */
       
   138 void SnsrOledAnalogClockContainer::getActiveScreenRows(int *firstActiveRow, int *lastActiveRow)
       
   139 {
       
   140     if ( mClockContainer ) {
       
   141         QRect clockRect( mClockContainer->pos().toPoint(), 
       
   142                          mClockContainer->size().toSize() );
       
   143         if ( mCurrentOrientation == Qt::Vertical ) {
       
   144             *firstActiveRow = clockRect.top();
       
   145             *lastActiveRow = clockRect.bottom() + 1;
       
   146         }
       
   147         else {
       
   148             *firstActiveRow = clockRect.left();
       
   149             *lastActiveRow = clockRect.right() + 1;
       
   150         }
       
   151     }
       
   152 }
       
   153 
       
   154 /*!
       
   155     Orientation is locked in power save mode as sensors are off anyway,
       
   156     at least after some timeout.
       
   157  */
       
   158 bool SnsrOledAnalogClockContainer::isOrientationLocked()
       
   159 {
       
   160     return true;
       
   161 }
       
   162 
       
   163 void SnsrOledAnalogClockContainer::loadWidgets()
       
   164 {
       
   165     bool ok(true);
       
   166 
       
   167     // reset widget pointers, any previous widgets are already deleted by now
       
   168     mMainView = 0;
       
   169     mMainContainer = 0;
       
   170     mClockContainer = 0;
       
   171     mOledClockWidget = 0;
       
   172     mDateLabel = 0;
       
   173     mIndicatorWidget = 0;
       
   174     
       
   175     // load widgets from docml
    62     qDebug() << gOledAnalogLayoutDocml;
   176     qDebug() << gOledAnalogLayoutDocml;
    63 
       
    64     // load oled analog clock
       
    65     mDocumentObjects = mDocumentLoader.load(gOledAnalogLayoutDocml, &ok);
   177     mDocumentObjects = mDocumentLoader.load(gOledAnalogLayoutDocml, &ok);
    66     Q_ASSERT_X(ok, gOledAnalogLayoutDocml, "Invalid DocML file.");
   178     Q_ASSERT_X(ok, gOledAnalogLayoutDocml, "Invalid DocML file.");
    67     if (ok) {
   179     if (ok) {
    68         mMainView = mDocumentLoader.findWidget(gMainViewName);
   180         mMainView = mDocumentLoader.findWidget(gMainViewName);
    69         mMainContainer = mDocumentLoader.findWidget(gMainContainerName);
   181         mMainContainer = mDocumentLoader.findWidget(gMainContainerName);
    70         mClockContainer = mDocumentLoader.findWidget(gClockContainerName);
   182         mClockContainer = mDocumentLoader.findWidget(gClockContainerName);
    71         mOledClockWidget = qobject_cast<SnsrOledClockWidget *>(
   183         mOledClockWidget = qobject_cast<SnsrOledClockWidget *>(
    72               mDocumentLoader.findWidget(gOledAnalogClockWidgetName));
   184               mDocumentLoader.findWidget(gOledAnalogClockWidgetName));
    73         mDateLabel = qobject_cast<HbLabel *>(
   185         mDateLabel = qobject_cast<SnsrLabel *>(
    74               mDocumentLoader.findWidget(gDateLabelName));
   186               mDocumentLoader.findWidget(gDateLabelName));
    75         mIndicatorWidget = qobject_cast<SnsrIndicatorWidget *>(
   187         mIndicatorWidget = qobject_cast<SnsrIndicatorWidget *>(
    76               mDocumentLoader.findWidget(gIndicatorWidgetName));
   188               mDocumentLoader.findWidget(gIndicatorWidgetName));
    77         Q_ASSERT_X(
   189         Q_ASSERT_X(
    78                 mMainView && mMainContainer && mClockContainer
   190                 mMainView && mMainContainer && mClockContainer
    79                 && mOledClockWidget && mDateLabel && mIndicatorWidget,
   191                 && mOledClockWidget && mDateLabel && mIndicatorWidget,
    80                 gOledAnalogLayoutDocml, "Objects not found in DocML file."
   192                 gOledAnalogLayoutDocml, "Objects not found in DocML file."
    81                 );
   193                 );
    82         
   194         
       
   195         // In case of landscape layout, read also the landscape delta section
       
   196         if ( mCurrentOrientation == Qt::Horizontal ) {
       
   197             qDebug() << "loading: " << gOledAnalogLayoutDocml << ", section: " << gLandscapeSectionName;
       
   198             mDocumentLoader.load(gOledAnalogLayoutDocml, gLandscapeSectionName, &ok);
       
   199             Q_ASSERT_X(ok, gOledAnalogLayoutDocml, "Invalid section in DocML file.");
       
   200         }
       
   201 
    83         mIndicatorWidget->setLayoutType(SnsrIndicatorWidget::IndicatorsCentered);
   202         mIndicatorWidget->setLayoutType(SnsrIndicatorWidget::IndicatorsCentered);
       
   203         initIndicatorWidget();
    84         
   204         
    85         mBackgroundContainerLayout->addItem(mMainView);
   205         mBackgroundContainerLayout->addItem(mMainView);
    86     }
   206     }
    87 
       
    88     SCREENSAVER_TEST_FUNC_EXIT("SnsrOledAnalogClockContainer::SnsrOledAnalogClockContainer")
       
    89 }
       
    90 
       
    91 /*!
       
    92     Destructs the class.
       
    93  */
       
    94 SnsrOledAnalogClockContainer::~SnsrOledAnalogClockContainer()
       
    95 {
       
    96     //mOledClockWidget etc - deleted by the parent
       
    97 }
       
    98 
       
    99 /*!
       
   100     Updates OLED analog clock widget position.
       
   101  */
       
   102 void SnsrOledAnalogClockContainer::updatePosition()
       
   103 {
       
   104     QSizeF containerSize = mMainContainer->size();
       
   105     
       
   106     // Container must have a valid size to enable calculating the 
       
   107     // destination position for the clock.
       
   108     if ( containerSize.width() > 0 && containerSize.height() > 0 ) {
       
   109         containerSize -= mClockContainer->boundingRect().size();
       
   110         QRectF containerRect( mMainContainer->pos(), containerSize );
       
   111         if ( mInitialize ) {
       
   112             QPointF clockPos = nextRandomPosition( mClockContainer->pos(), mDestPosition, containerRect );
       
   113             mClockContainer->setPos( clockPos );
       
   114         }
       
   115         else {
       
   116             mDestPosition = randomPosition( containerRect );
       
   117             mInitialize = true;
       
   118         }
       
   119     }
       
   120 }
       
   121 
       
   122 /*!
       
   123     Updates displayed time.
       
   124  */
       
   125 void SnsrOledAnalogClockContainer::update()
       
   126 {
       
   127     SCREENSAVER_TEST_FUNC_ENTRY("SnsrOledAnalogClockContainer::update")
       
   128 
       
   129     // time
       
   130     mOledClockWidget->tick();
       
   131     
       
   132     // date
       
   133     if (mCurrentOrientation == Qt::Vertical) {
       
   134         mDateLabel->setPlainText(
       
   135             HbExtendedLocale().format(QDate::currentDate(), gDateFormatVerticalStr)
       
   136         );
       
   137     } else {
       
   138         mDateLabel->setPlainText(
       
   139             HbExtendedLocale().format(QDate::currentDate(), gDateFormatHorizontalStr)
       
   140         );
       
   141     }
       
   142     
       
   143     // position
       
   144     updatePosition();
       
   145     
       
   146     // TODO: Currently, both time and position are updated 10 times per second.
       
   147     // These should happen only once per minute in the power save mode.
       
   148 
       
   149     SCREENSAVER_TEST_FUNC_EXIT("SnsrOledAnalogClockContainer::update")
       
   150 }
       
   151 
       
   152 /*!
       
   153     Changes screensaver layout basing on orientation changes.
       
   154     \param orientation Current orientation.
       
   155  */
       
   156 void SnsrOledAnalogClockContainer::changeLayout(Qt::Orientation orientation)
       
   157 {
       
   158     SCREENSAVER_TEST_FUNC_ENTRY("SnsrOledAnalogClockContainer::changeLayout")
       
   159 
       
   160     bool ok(false);
       
   161     if (mCurrentOrientation != orientation) {
       
   162         mCurrentOrientation = orientation;
       
   163         // hide controls to avoid screen flickering
       
   164         mMainView->hide();
       
   165 
       
   166         QString sectionToLoad("");
       
   167         if (mCurrentOrientation == Qt::Horizontal) {
       
   168             sectionToLoad = gLandscapeSectionName;
       
   169         }
       
   170         qDebug() << "loading: " << gOledAnalogLayoutDocml << ", section: " << sectionToLoad;
       
   171         mDocumentLoader.load(gOledAnalogLayoutDocml, sectionToLoad, &ok);
       
   172         Q_ASSERT_X(ok, gOledAnalogLayoutDocml, "Invalid section in DocML file.");
       
   173 
       
   174         // disconnect container from parent layout,
       
   175         // connected container resets its position to the one defined in docml
       
   176         // after label text updates
       
   177         mClockContainer->setParentLayoutItem(0);
       
   178 
       
   179         update();
       
   180 
       
   181         // view is rebuilt and ready to show
       
   182         mMainView->show();
       
   183     }
       
   184 
       
   185     // update anyway - this is needed in situations when screensaver goes to
       
   186     // foreground but layout change did not occur
       
   187     if (!ok) {
       
   188         update();
       
   189     }
       
   190 
       
   191     SCREENSAVER_TEST_FUNC_EXIT("SnsrOledAnalogClockContainer::changeLayout")
       
   192 }
       
   193 
       
   194 int SnsrOledAnalogClockContainer::updateIntervalInMilliseconds()
       
   195 {
       
   196     return 60*1000;
       
   197 }
   207 }
   198 
   208 
   199 // end of file
   209 // end of file