calendarwidgetplugin/src/calendarwidget.cpp
changeset 0 db1bf15cefff
child 1 f8e7eccf5f96
equal deleted inserted replaced
-1:000000000000 0:db1bf15cefff
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Home screen Calendar widget
       
    15 *
       
    16 */
       
    17 
       
    18 // System includes
       
    19 #include <QGraphicsProxyWidget>
       
    20 #include <HbDocumentLoader>
       
    21 #include <QGraphicsLinearLayout>
       
    22 #include <QGraphicsWidget>
       
    23 #include <QGraphicsSceneMouseEvent>
       
    24 #include <QPainter>
       
    25 #include <QDebug>
       
    26 #include <QTranslator>
       
    27 #include <QDir>
       
    28 #include <QDateTime>
       
    29 #include <xqservicerequest.h>
       
    30 #include <HbFrameDrawer>
       
    31 #include <HbFrameItem>
       
    32 #include <HbColorScheme>
       
    33 #include <HbEvent>
       
    34 #include <HbLabel>
       
    35 #include <QMetaMethod>
       
    36 #include <QCoreApplication>
       
    37 #include <QGesture>
       
    38 #include <qxml.h>
       
    39 
       
    40 // User includes
       
    41 #include "calendarwidget.h"
       
    42 #include "contentlayouthandler.h"
       
    43 #include "contentlayoutgesture.h"
       
    44 #include "calendarwidgetdebug.h"
       
    45 #include "saxhandler.h"
       
    46 
       
    47 //Local constants
       
    48 const char DOCML[] = ":/CalendarWidget";
       
    49 
       
    50 #define CALWIDGET_HIGHLIGHT_ACTIVE
       
    51 
       
    52 // ======== MEMBER FUNCTIONS ========
       
    53 
       
    54 /*
       
    55  * CalendarWidget::CalendarWidget()
       
    56  */
       
    57 CalendarWidget::CalendarWidget(QGraphicsItem* parent, Qt::WindowFlags flags)
       
    58     : HbWidget(parent, flags)
       
    59 {
       
    60     LOGS("CalendarWidget::CalendarWidget");
       
    61     grabGesture(Qt::TapGesture);
       
    62     grabGesture(Qt::PanGesture);
       
    63     grabGesture(Qt::PinchGesture);
       
    64     grabGesture(Qt::SwipeGesture);
       
    65 }
       
    66 
       
    67 /*
       
    68  * CalendarWidget::~CalendarWidget()
       
    69  */
       
    70 CalendarWidget::~CalendarWidget()
       
    71 {
       
    72     LOGS("CalendarWidget::~CalendarWidget");
       
    73 }
       
    74 
       
    75 /*
       
    76  * CalendarWidget::boundingRect()
       
    77  */
       
    78 QRectF CalendarWidget::boundingRect() const
       
    79 {
       
    80     LOGS("CalendarWidget::boundingRect");
       
    81     return childrenBoundingRect();
       
    82 }
       
    83 
       
    84 /*
       
    85  * CalendarWidget::shape()
       
    86  */
       
    87 QPainterPath CalendarWidget::shape() const
       
    88 {
       
    89     LOGS("CalendarWidget::shape");
       
    90 
       
    91     QPainterPath path;
       
    92     path.addRect(boundingRect());
       
    93     return path;
       
    94 }
       
    95 
       
    96 /*
       
    97  * CalendarWidget::loadWidget()
       
    98  */
       
    99 bool CalendarWidget::loadWidget()
       
   100 {
       
   101     LOGS("CalendarWidget::loadWidget");
       
   102 
       
   103     // Use document loader to load the contents
       
   104     HbDocumentLoader loader;
       
   105     bool loaded = true;
       
   106     bool ok = false;
       
   107     loader.load(DOCML, &ok);
       
   108 
       
   109     if (ok) {
       
   110         QObject* contentLayoutHandler = new ContentLayoutHandler();
       
   111         connectLayoutHandler(this, contentLayoutHandler, loader);
       
   112         
       
   113 		QString fileName( "C:/private/20022F35/gestures.xml" );
       
   114 		QFile xmlConfiguration(fileName);
       
   115 		bool fileExists = xmlConfiguration.open( QIODevice::ReadOnly );
       
   116 		if (fileExists) {
       
   117             xmlConfiguration.close();
       
   118         }
       
   119         else {
       
   120             QFile sourceFile("Z:/resource/gestures.xml");
       
   121             fileExists = sourceFile.open(QFile::ReadOnly);
       
   122             fileExists = xmlConfiguration.open(QFile::WriteOnly | QFile::Truncate);
       
   123             fileExists = xmlConfiguration.write(sourceFile.readAll()) >= 0;
       
   124             sourceFile.close();
       
   125             xmlConfiguration.close();
       
   126         }
       
   127 		
       
   128 		if (fileExists) {
       
   129             parseFile(fileName, loader);
       
   130         }
       
   131 
       
   132         if (dynamic_cast<QGraphicsLinearLayout*> (contentLayoutHandler)) {
       
   133             setPreferredSize(
       
   134                 dynamic_cast<QGraphicsLinearLayout*>(contentLayoutHandler)->preferredSize());
       
   135             parentWidget()->resize(preferredSize()); // workaround for layouting
       
   136             setLayout(dynamic_cast<QGraphicsLinearLayout*>(contentLayoutHandler));
       
   137         }
       
   138         else {
       
   139             loaded = false;
       
   140         }    
       
   141     }
       
   142     else {
       
   143         loaded = false;
       
   144     }
       
   145     
       
   146     return loaded;
       
   147 }
       
   148 
       
   149 /*
       
   150  * CalendarWidget::connectLayoutHandler()
       
   151  */
       
   152 void CalendarWidget::connectLayoutHandler(HbWidget *widget, QObject *layoutHandler, HbDocumentLoader& loader)
       
   153 {
       
   154     LOGS("CalendarWidget::connectLayoutHandler");
       
   155     if (!widget || !layoutHandler) {
       
   156         return;
       
   157     }
       
   158     int methodIndex = layoutHandler->metaObject()->
       
   159         indexOfSlot("initializeLayout(HbDocumentLoader,QObject*)");
       
   160     if (methodIndex >= 0) {
       
   161         layoutHandler->metaObject()->method(methodIndex).invoke(layoutHandler,
       
   162             Q_ARG(const HbDocumentLoader, loader), Q_ARG(QObject*, this));
       
   163     }
       
   164     else {
       
   165         return;
       
   166     }
       
   167 
       
   168     methodIndex = layoutHandler->metaObject()->indexOfSlot("onThemeChange()");
       
   169     if (methodIndex >= 0) {
       
   170         connect(this, SIGNAL(themeChanged()), layoutHandler, SLOT(onThemeChange()));
       
   171     }
       
   172 
       
   173     methodIndex = layoutHandler->metaObject()->indexOfSlot("highlightOn(QPointF&)");
       
   174     if (methodIndex >= 0) {
       
   175         connect(this, SIGNAL(mousePressed(QPointF&)),
       
   176             layoutHandler, SLOT(highlightOn(QPointF&)));
       
   177     }
       
   178     
       
   179     methodIndex = layoutHandler->metaObject()->indexOfSlot("highlightOff()");
       
   180     if (methodIndex >= 0) {
       
   181         connect(this, SIGNAL(mouseReleased()),
       
   182             layoutHandler, SLOT(highlightOff()));
       
   183     }
       
   184 
       
   185 }
       
   186 
       
   187 /*
       
   188  * CalendarWidget::loadTranslator
       
   189  */
       
   190 void CalendarWidget::loadTranslator()
       
   191 {
       
   192     LOGS("CalendarWidget::loadTranslator");
       
   193     QLocale::setDefault(QLocale::English);
       
   194     QString locale = QLocale::system().languageToString(QLocale::system().language());
       
   195     qDebug() << "locale: " << locale;
       
   196     QString lang = QLocale::system().name();
       
   197     mCommonTranslator = new QTranslator();
       
   198     QString path = "Z:/resource/qt/translations/";
       
   199     QString filename = "calwidget_" + lang + ".qm";
       
   200     qDebug() << "path: " << path;
       
   201     qDebug() << "filename: " << filename;
       
   202     bool loaded = mCommonTranslator->load(filename,path);
       
   203     qDebug() << "loaded = " << loaded;
       
   204     QCoreApplication::installTranslator(mCommonTranslator);
       
   205 }
       
   206 
       
   207 /*
       
   208  * CalendarWidget::event()
       
   209  */
       
   210 bool CalendarWidget::event(QEvent * event)
       
   211 {
       
   212     LOGS("CalendarWidget::event");
       
   213     bool consumed = false;
       
   214     QEvent::Type eventType = event->type();
       
   215 
       
   216     if (eventType == HbEvent::ThemeChanged) {
       
   217         consumed = true;
       
   218         emit themeChanged();
       
   219     }
       
   220     
       
   221     if (eventType == QEvent::Gesture) {
       
   222         QGestureEvent* gesture = static_cast<QGestureEvent*>(event);
       
   223         QList<QGesture*> gestureList = gesture->gestures();
       
   224         for (int i = 0; i < gestureList.count(); i++) {
       
   225             int type = gestureList.at(i)->gestureType();
       
   226             qDebug() << "type = " << type;
       
   227         }
       
   228         gestureEvent(static_cast<QGestureEvent*>(event));
       
   229         consumed = true;
       
   230     }
       
   231 
       
   232     return consumed;
       
   233 }
       
   234 
       
   235 /*
       
   236  * CalendarWidget::onInitialize()
       
   237  */
       
   238 void CalendarWidget::onInitialize()
       
   239 {
       
   240     LOGS("CalendarWidget::onInitialize");
       
   241     loadTranslator();
       
   242 
       
   243     if (loadWidget()) {
       
   244 #ifdef CALWIDGET_HIGHLIGHT_ACTIVE
       
   245         // Register event filter for highlighting
       
   246         QCoreApplication* coreApp = QCoreApplication::instance();
       
   247         if (coreApp) {
       
   248             coreApp->installEventFilter(this);
       
   249         }
       
   250 #endif
       
   251     }
       
   252     else {
       
   253         //loading failed
       
   254         emit finished();
       
   255     }
       
   256 }
       
   257 
       
   258 /*
       
   259  * CalendarWidget::onShow()
       
   260  */
       
   261 void CalendarWidget::onShow()
       
   262 {
       
   263     LOGS("CalendarWidget::onShow");
       
   264     emit mouseReleased();
       
   265 }
       
   266 
       
   267 /*
       
   268 * CalendarWidget::onHide()
       
   269 */
       
   270 void CalendarWidget::onHide()
       
   271 {
       
   272     LOGS("CalendarWidget::onHide");
       
   273 }
       
   274 
       
   275 /*
       
   276  * CalendarWidget::onUninitialize()
       
   277  */
       
   278 void CalendarWidget::onUninitialize()
       
   279 {
       
   280     LOGS("CalendarWidget::onUninitialize");
       
   281 #ifdef CALWIDGET_HIGHLIGHT_ACTIVE
       
   282     // Uninstall event filter
       
   283     QCoreApplication* coreApp = QCoreApplication::instance();
       
   284     if (coreApp) {
       
   285         coreApp->removeEventFilter(this);
       
   286     }
       
   287 #endif
       
   288 }
       
   289 
       
   290 /*
       
   291  * CalendarWidget::eventFilter()
       
   292  */
       
   293 bool CalendarWidget::eventFilter(QObject *obj, QEvent *event)
       
   294 {
       
   295     Q_UNUSED(obj);
       
   296     if (event->type() == QEvent::GraphicsSceneMousePress) {
       
   297         QGraphicsSceneMouseEvent* mouseEvent = (QGraphicsSceneMouseEvent*)event;
       
   298         QPointF pos = mouseEvent->scenePos();
       
   299 //        emit mousePressed(pos);
       
   300     }
       
   301     else if (event->type() == QEvent::GraphicsSceneMouseRelease) {
       
   302 //        emit mouseReleased();
       
   303     }
       
   304     return false;
       
   305 }
       
   306 
       
   307 /*
       
   308  * CalendarWidget::gestureEvent()
       
   309  */
       
   310 void CalendarWidget::gestureEvent(QGestureEvent *event)
       
   311 {
       
   312     LOGS("CalendarWidget::gestureEvent");
       
   313     if (QTapGesture *tap = (QTapGesture*)event->gesture(Qt::TapGesture)) {
       
   314         //QPointF pos = mapFromScene(event->mapToGraphicsScene(tap->position()));
       
   315         QPointF pos = tap->position();
       
   316         switch(tap->state()) {
       
   317             case Qt::GestureStarted:
       
   318                 emit mousePressed(pos);
       
   319                 LOGS("CalendarWidget::gestureEvent => gestureStarted");
       
   320                 break;
       
   321             case Qt::GestureUpdated:
       
   322                 LOGS("CalendarWidget::gestureEvent => gestureUpdated");
       
   323                 break;
       
   324             case Qt::GestureFinished:
       
   325                 emit tapGesture(pos);
       
   326                 emit mouseReleased();
       
   327                 LOGS("CalendarWidget::gestureEvent => gestureFinished");
       
   328                 break;
       
   329             case Qt::GestureCanceled:
       
   330                 LOGS("CalendarWidget::gestureEvent => gestureCanceled");
       
   331                 break;
       
   332             default:
       
   333                 break;
       
   334         }
       
   335     }
       
   336     
       
   337     if(QPanGesture *pan = (QPanGesture*)event->gesture(Qt::PanGesture)) {
       
   338         switch (pan->state()) {
       
   339             case Qt::GestureFinished:
       
   340                 emit mouseReleased();
       
   341                 break;
       
   342             default:
       
   343                 break;
       
   344         }
       
   345     }
       
   346         
       
   347     if (QSwipeGesture *pan = (QSwipeGesture*)event->gesture(Qt::SwipeGesture)) {
       
   348         switch (pan->state()) {
       
   349             case Qt::GestureFinished:
       
   350                 emit mouseReleased();
       
   351                 break;
       
   352             default:
       
   353                 break;
       
   354         }
       
   355     }
       
   356 
       
   357     if (QPinchGesture *pan = (QPinchGesture*)event->gesture(Qt::PinchGesture)) {
       
   358         switch (pan->state()) {
       
   359             case Qt::GestureFinished:
       
   360                 emit mouseReleased();
       
   361                 break;
       
   362             default:
       
   363                 break;
       
   364         }
       
   365     }
       
   366 }
       
   367 
       
   368 /*
       
   369  *  CalendarWidget::mousePressEvent()
       
   370  */
       
   371 void CalendarWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
       
   372 {
       
   373     Q_UNUSED(event);
       
   374     LOGS("CalendarWidget::mousePressEvent");
       
   375 }
       
   376 
       
   377 /*
       
   378  *  CalendarWidget::mouseReleaseEvent()
       
   379  */
       
   380 void CalendarWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
       
   381 {
       
   382     Q_UNUSED(event);
       
   383     LOGS("CalendarWidget::mouseReleaseEvent");
       
   384 }
       
   385 
       
   386 bool CalendarWidget::parseFile(QString& fileName, HbDocumentLoader& loader)
       
   387 {
       
   388     LOGS("CalendarWidget::ParseFile");
       
   389     QFile file(fileName);
       
   390     QXmlInputSource inputSource(&file);
       
   391     QXmlSimpleReader reader;
       
   392     SaxHandler xmlParser(this, loader);
       
   393     reader.setContentHandler(&xmlParser);
       
   394     reader.setErrorHandler(&xmlParser);
       
   395     return reader.parse(inputSource);
       
   396 }
       
   397 
       
   398 //End of file