screensaver/screensaverplugins/snsrbigclockscreensaverplugin/src/snsroleddigitalclockcontainer.cpp
changeset 69 87476091b3f5
parent 62 341166945d65
child 86 e4f038c420f7
equal deleted inserted replaced
67:474929a40a0f 69:87476091b3f5
    20 #include <QDebug>
    20 #include <QDebug>
    21 #include <QGraphicsLinearLayout>
    21 #include <QGraphicsLinearLayout>
    22 #include <QTime>
    22 #include <QTime>
    23 #include <QTimer>
    23 #include <QTimer>
    24 
    24 
    25 #include <hblabel.h>
    25 
    26 #include <hbextendedlocale.h>
    26 #include <HbExtendedLocale>
    27 
    27 #include <HbMainWindow>
       
    28 
       
    29 #include "snsrlabel.h"
    28 #include "snsrindicatorwidget.h"
    30 #include "snsrindicatorwidget.h"
       
    31 #include "snsroledtimelabel.h"
       
    32 
    29 
    33 
    30 /*!
    34 /*!
    31     \class SnsrOledDigitalClockContainer
    35     \class SnsrOledDigitalClockContainer
    32     \ingroup group_snsrbigclockscreensaverplugin
    36     \ingroup group_snsrbigclockscreensaverplugin
    33     \brief Container used for preparing layout for oled digital clock.
    37     \brief Container used for preparing layout for oled digital clock.
    39 
    43 
    40 extern const char *gMainViewName;
    44 extern const char *gMainViewName;
    41 extern const char *gMainContainerName;
    45 extern const char *gMainContainerName;
    42 extern const char *gClockContainerName;
    46 extern const char *gClockContainerName;
    43 
    47 
    44 extern const char *gTimeLabelName;
    48 extern const char *gOledTimeLabelName = "oledTimeLabel";
    45 extern const char *gAmPmLabelName;
    49 extern const char *gAmPmLabelName;
    46 extern const char *gDateLabelName;
    50 extern const char *gDateLabelName;
    47 
    51 
    48 extern const char *gDateFormatVerticalStr;
    52 extern const char *gDateFormatVerticalStr;
    49 extern const char *gDateFormatHorizontalStr;
    53 extern const char *gDateFormatHorizontalStr;
    52 extern const char *gAmPmFormatStr;
    56 extern const char *gAmPmFormatStr;
    53 
    57 
    54 extern const char *gIndicatorWidgetName;
    58 extern const char *gIndicatorWidgetName;
    55 
    59 
    56 
    60 
       
    61 
    57 /*!
    62 /*!
    58     Constructs a new SnsrOledDigitalClockContainer.
    63     Constructs a new SnsrOledDigitalClockContainer.
    59  */
    64  */
    60 SnsrOledDigitalClockContainer::SnsrOledDigitalClockContainer() :
    65 SnsrOledDigitalClockContainer::SnsrOledDigitalClockContainer() :
    61     SnsrBigClockContainer(), mClockContainer(0), mTimeLabel(0),
    66     SnsrBigClockContainer(), mClockContainer(0), mAmPmLabel(0),
    62     mDateLabel(0), mAmPmLabel(0), mDestPosition(QPointF()), mInitialize(false)
    67     mTimeLabel(0), mDateLabel(0), mDestPosition(QPointF()),
       
    68     mInitialize(false)
    63 {
    69 {
    64     SCREENSAVER_TEST_FUNC_ENTRY("SnsrOledDigitalClockContainer::SnsrOledDigitalClockContainer")
    70     SCREENSAVER_TEST_FUNC_ENTRY("SnsrOledDigitalClockContainer::SnsrOledDigitalClockContainer")
    65 
       
    66     bool ok(false);
       
    67     qDebug() << gOledDigitalLayoutDocml;
       
    68 
       
    69     // load digital clock
       
    70     mDocumentObjects = mDocumentLoader.load(gOledDigitalLayoutDocml, &ok);
       
    71     Q_ASSERT_X(ok, gOledDigitalLayoutDocml, "Invalid DocML file.");
       
    72 
       
    73     if (ok) {
       
    74         mMainView = mDocumentLoader.findWidget(gMainViewName);
       
    75         mMainContainer = mDocumentLoader.findWidget(gMainContainerName);
       
    76         mClockContainer = mDocumentLoader.findWidget(gClockContainerName);
       
    77         mDateLabel = qobject_cast<HbLabel *>(
       
    78                 mDocumentLoader.findWidget(gDateLabelName));
       
    79         mTimeLabel = qobject_cast<HbLabel *>(
       
    80                 mDocumentLoader.findWidget(gTimeLabelName));
       
    81         mAmPmLabel = qobject_cast<HbLabel *>(
       
    82                 mDocumentLoader.findWidget(gAmPmLabelName));
       
    83         mIndicatorWidget = qobject_cast<SnsrIndicatorWidget *>(
       
    84                 mDocumentLoader.findWidget(gIndicatorWidgetName));
       
    85         Q_ASSERT_X(
       
    86                 mMainView && mMainContainer && mClockContainer 
       
    87                 && mDateLabel && mTimeLabel && mAmPmLabel
       
    88                 && mIndicatorWidget,
       
    89                 gOledDigitalLayoutDocml, "Objects not found in DocML file."
       
    90                 );
       
    91 
       
    92         mIndicatorWidget->setLayoutType(SnsrIndicatorWidget::IndicatorsCentered);
       
    93         
       
    94         mBackgroundContainerLayout->addItem(mMainView);
       
    95     }
       
    96 
       
    97     SCREENSAVER_TEST_FUNC_EXIT("SnsrOledDigitalClockContainer::SnsrOledDigitalClockContainer")
    71     SCREENSAVER_TEST_FUNC_EXIT("SnsrOledDigitalClockContainer::SnsrOledDigitalClockContainer")
    98 }
    72 }
    99 
    73 
   100 /*!
    74 /*!
   101     Destructs the class.
    75     Destructs the class.
   102  */
    76  */
   103 SnsrOledDigitalClockContainer::~SnsrOledDigitalClockContainer()
    77 SnsrOledDigitalClockContainer::~SnsrOledDigitalClockContainer()
   104 {
    78 {
       
    79     resetIndicatorConnections();
   105     //mTimeLabel, mAmPmLabel, mDateLabel, mMoveTimer - deleted by the parent
    80     //mTimeLabel, mAmPmLabel, mDateLabel, mMoveTimer - deleted by the parent
   106 }
    81 }
   107 
    82 
   108 /*!
    83 /*!
   109     Updates OLED digital clock widget position.
    84     Updates OLED digital clock widget position.
   116     // destination position for the clock.
    91     // destination position for the clock.
   117     if ( containerSize.width() > 0 && containerSize.height() > 0 ) {
    92     if ( containerSize.width() > 0 && containerSize.height() > 0 ) {
   118         containerSize -= mClockContainer->boundingRect().size();
    93         containerSize -= mClockContainer->boundingRect().size();
   119         QRectF containerRect( mMainContainer->pos(), containerSize );
    94         QRectF containerRect( mMainContainer->pos(), containerSize );
   120         if ( mInitialize ) {
    95         if ( mInitialize ) {
       
    96             // disconnect container from parent layout,
       
    97             // connected container resets its position to the one defined in docml
       
    98             // after label text updates
       
    99             mClockContainer->setParentLayoutItem(0);
       
   100 
   121             QPointF clockPos = nextRandomPosition( mClockContainer->pos(), mDestPosition, containerRect );
   101             QPointF clockPos = nextRandomPosition( mClockContainer->pos(), mDestPosition, containerRect );
   122             mClockContainer->setPos( clockPos );
   102             mClockContainer->setPos( clockPos );
   123         }
   103         }
   124         else {
   104         else {
   125             mDestPosition = randomPosition( containerRect );
   105             mDestPosition = randomPosition( containerRect );
   126             mInitialize = true;
   106             mInitialize = true;
   127         }
   107         }
       
   108         // the active area of power save mode needs to be updated when clock container is moved
       
   109         emit activeAreaMoved();
   128     }
   110     }
   129 }
   111 }
   130 
   112 
   131 /*!
   113 /*!
   132     Updates displayed time and date.
   114     Updates displayed time and date.
   134 void SnsrOledDigitalClockContainer::update()
   116 void SnsrOledDigitalClockContainer::update()
   135 {
   117 {
   136     SCREENSAVER_TEST_FUNC_ENTRY("SnsrOledDigitalClockContainer::update")
   118     SCREENSAVER_TEST_FUNC_ENTRY("SnsrOledDigitalClockContainer::update")
   137 
   119 
   138     // time
   120     // time
   139     mTimeLabel->setPlainText(
   121     mTimeLabel->setText(
   140         HbExtendedLocale().format(QTime::currentTime(), gTimeFormatStr)
   122         HbExtendedLocale().format(QTime::currentTime(), gTimeFormatStr)
   141         );
   123         );
       
   124     
       
   125     // if clock type is 24h, this will return an empty string.
   142     mAmPmLabel->setPlainText(
   126     mAmPmLabel->setPlainText(
   143         HbExtendedLocale().format(QTime::currentTime(), gAmPmFormatStr)
   127         HbExtendedLocale().format(QTime::currentTime(), gAmPmFormatStr)
   144         );
   128         );
   145 
   129 
   146     // date
   130     // date
   159     updatePosition();
   143     updatePosition();
   160 
   144 
   161     SCREENSAVER_TEST_FUNC_EXIT("SnsrOledDigitalClockContainer::update")
   145     SCREENSAVER_TEST_FUNC_EXIT("SnsrOledDigitalClockContainer::update")
   162 }
   146 }
   163 
   147 
   164 /*!
       
   165     Changes screensaver layout basing on orientation changes.
       
   166     \param orientation Current orientation.
       
   167  */
       
   168 void SnsrOledDigitalClockContainer::changeLayout(Qt::Orientation orientation)
       
   169 {
       
   170     SCREENSAVER_TEST_FUNC_ENTRY("SnsrOledDigitalClockContainer::changeLayout")
       
   171 
       
   172     if (mCurrentOrientation != orientation) {
       
   173         mCurrentOrientation = orientation;
       
   174 
       
   175         // hide controls to avoid screen flickering
       
   176         mMainView->hide();
       
   177 
       
   178         QString sectionToLoad("");
       
   179         if (mCurrentOrientation == Qt::Horizontal) {
       
   180             sectionToLoad = gLandscapeSectionName;
       
   181         }
       
   182         qDebug() << "loading: " << gOledDigitalLayoutDocml << ", section: "
       
   183             << sectionToLoad;
       
   184         bool ok(false);
       
   185         mDocumentLoader.load(gOledDigitalLayoutDocml, sectionToLoad, &ok);
       
   186         Q_ASSERT_X(ok, gOledDigitalLayoutDocml, "Invalid section in DocML file.");
       
   187 
       
   188         // disconnect container from parent layout,
       
   189         // connected container resets its position to the one defined in docml
       
   190         // after label text updates
       
   191         mClockContainer->setParentLayoutItem(0);
       
   192 
       
   193         // update labels before showing the view
       
   194         update();
       
   195 
       
   196         // view is rebuilt and ready to show
       
   197         mMainView->show();
       
   198     }
       
   199 
       
   200     SCREENSAVER_TEST_FUNC_EXIT("SnsrOledDigitalClockContainer::changeLayout")
       
   201 }
       
   202 
       
   203 int SnsrOledDigitalClockContainer::updateIntervalInMilliseconds()
   148 int SnsrOledDigitalClockContainer::updateIntervalInMilliseconds()
   204 {
   149 {
   205     return 60*1000;
   150     return 60*1000;
   206 }
   151 }
   207 
   152 
       
   153 /*!
       
   154     @copydoc SnsrBigClockContainer::getActiveScreenRows()
       
   155  */
       
   156 void SnsrOledDigitalClockContainer::getActiveScreenRows(int *firstActiveRow, int *lastActiveRow)
       
   157 {
       
   158     if ( mClockContainer ) {
       
   159         QRect clockRect( mClockContainer->pos().toPoint(), 
       
   160                          mClockContainer->size().toSize() );
       
   161         if ( mCurrentOrientation == Qt::Vertical ) {
       
   162             *firstActiveRow = clockRect.top();
       
   163             *lastActiveRow = clockRect.bottom() + 1;
       
   164         }
       
   165         else {
       
   166             *firstActiveRow = clockRect.left();
       
   167             *lastActiveRow = clockRect.right() + 1;
       
   168         }
       
   169     }
       
   170 }
       
   171 
       
   172 /*!
       
   173     Orientation is locked in power save mode as sensors are off anyway,
       
   174     at least after some timeout.
       
   175  */
       
   176 bool SnsrOledDigitalClockContainer::isOrientationLocked()
       
   177 {
       
   178     return true;
       
   179 }
       
   180 
       
   181 void SnsrOledDigitalClockContainer::loadWidgets()
       
   182 {
       
   183     // reset widget pointers, any previous widgets are already deleted by now
       
   184     mMainView = 0;
       
   185     mMainContainer = 0;
       
   186     mClockContainer = 0;
       
   187     mDateLabel = 0;
       
   188     mTimeLabel = 0;
       
   189     mAmPmLabel = 0;
       
   190     mIndicatorWidget = 0;
       
   191     
       
   192     // load widgets from docml
       
   193     bool ok(false);
       
   194     qDebug() << gOledDigitalLayoutDocml;
       
   195     mDocumentObjects = mDocumentLoader.load(gOledDigitalLayoutDocml, &ok);
       
   196     Q_ASSERT_X(ok, gOledDigitalLayoutDocml, "Invalid DocML file.");
       
   197 
       
   198     if (ok) {
       
   199         mMainView = mDocumentLoader.findWidget(gMainViewName);
       
   200         mMainContainer = mDocumentLoader.findWidget(gMainContainerName);
       
   201         mClockContainer = mDocumentLoader.findWidget(gClockContainerName);
       
   202         mDateLabel = qobject_cast<SnsrLabel *>(
       
   203                 mDocumentLoader.findWidget(gDateLabelName));
       
   204         mTimeLabel = qobject_cast<SnsrOledTimeLabel *>(
       
   205                 mDocumentLoader.findWidget(gOledTimeLabelName));
       
   206         mAmPmLabel = qobject_cast<SnsrLabel *>(
       
   207                 mDocumentLoader.findWidget(gAmPmLabelName));
       
   208         mIndicatorWidget = qobject_cast<SnsrIndicatorWidget *>(
       
   209                 mDocumentLoader.findWidget(gIndicatorWidgetName));
       
   210         Q_ASSERT_X(
       
   211                 mMainView && mMainContainer && mClockContainer 
       
   212                 && mDateLabel && mTimeLabel && mAmPmLabel
       
   213                 && mIndicatorWidget,
       
   214                 gOledDigitalLayoutDocml, "Objects not found in DocML file."
       
   215                 );
       
   216 
       
   217         // In case of landscape layout, read also the landscape delta section
       
   218         if ( mCurrentOrientation == Qt::Horizontal ) {
       
   219             qDebug() << "loading: " << gOledDigitalLayoutDocml << ", section: " << gLandscapeSectionName;
       
   220             mDocumentLoader.load(gOledDigitalLayoutDocml, gLandscapeSectionName, &ok);
       
   221             Q_ASSERT_X(ok, gOledDigitalLayoutDocml, "Invalid section in DocML file.");
       
   222         }
       
   223 
       
   224         mIndicatorWidget->setLayoutType(SnsrIndicatorWidget::IndicatorsCentered);
       
   225         initIndicatorWidget();
       
   226                
       
   227         mBackgroundContainerLayout->addItem(mMainView);
       
   228     }
       
   229 }
       
   230 
   208 // end of file
   231 // end of file