Attempt to wrrap ClocksWidget for use in QML
authorHeli Hyvättinen <heli.hyvattinen@kymp.net>
Thu, 11 Aug 2011 21:11:12 +0000 (00:11 +0300)
committerHeli Hyvättinen <heli.hyvattinen@kymp.net>
Thu, 11 Aug 2011 21:11:12 +0000 (00:11 +0300)
Managed to create a plugin, but not to use it.

20 files changed:
chessclock.pro
chessclock.pro.user
classes/clockswidgetplugin.cpp [new file with mode: 0644]
classes/clockswidgetplugin.h [new file with mode: 0644]
classes/wrappedclockswidget.cpp [new file with mode: 0644]
classes/wrappedclockswidget.h [new file with mode: 0644]
clockswidgetplugin/classes/chessclock.cpp [new file with mode: 0644]
clockswidgetplugin/classes/chessclock.h [new file with mode: 0644]
clockswidgetplugin/classes/chessclockwidget.cpp [new file with mode: 0644]
clockswidgetplugin/classes/chessclockwidget.h [new file with mode: 0644]
clockswidgetplugin/classes/clockswidget.cpp [new file with mode: 0644]
clockswidgetplugin/classes/clockswidget.h [new file with mode: 0644]
clockswidgetplugin/classes/clockswidgetplugin.cpp [new file with mode: 0644]
clockswidgetplugin/classes/clockswidgetplugin.h [new file with mode: 0644]
clockswidgetplugin/classes/wrappedclockswidget.cpp [new file with mode: 0644]
clockswidgetplugin/classes/wrappedclockswidget.h [new file with mode: 0644]
clockswidgetplugin/clockswidgetplugin.pro [new file with mode: 0644]
clockswidgetplugin/qmldir [new file with mode: 0644]
qml/ClocksPage.qml
qml/ClocksWidgetWrapper.qml [new file with mode: 0644]

index 38e1a45..2b6e577 100644 (file)
@@ -42,7 +42,9 @@ SOURCES += main.cpp \
     classes/timecontrol/delayaftertimecontrol.cpp \
     classes/timecontrol/hourglassclock.cpp \
     classes/timecontrol/hourglasstimecontrol.cpp \
-    classes/screenlitkeeper.cpp
+    classes/screenlitkeeper.cpp \
+    classes/wrappedclockswidget.cpp \
+    classes/clockswidgetplugin.cpp
 
 HEADERS  += chessclockwindow.h \
     classes/turninformation.h \
@@ -64,7 +66,9 @@ HEADERS  += chessclockwindow.h \
     classes/timecontrol/delayaftertimecontrol.h \
     classes/timecontrol/hourglasstimecontrol.h \
     classes/timecontrol/hourglassclock.h \
-    classes/screenlitkeeper.h
+    classes/screenlitkeeper.h \
+    classes/wrappedclockswidget.h \
+    classes/clockswidgetplugin.h
 
 
 OTHER_FILES += \
@@ -80,7 +84,8 @@ OTHER_FILES += \
     qtc_packaging/debian_harmattan/compat \
     qtc_packaging/debian_harmattan/changelog \
     qml/ClocksPage.qml \
-    qml/AboutPage.qml
+    qml/AboutPage.qml \
+    qml/ClocksWidgetWrapper.qml
 
 RESOURCES += \
     res.qrc \
index 396795d..c1ff9c9 100644 (file)
    <value key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName" type="QString">Harmattan</value>
    <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString">Harmattan</value>
    <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">Qt4ProjectManager.Target.HarmattanDeviceTarget</value>
-   <value key="ProjectExplorer.Target.ActiveBuildConfiguration" type="int">0</value>
+   <value key="ProjectExplorer.Target.ActiveBuildConfiguration" type="int">3</value>
    <value key="ProjectExplorer.Target.ActiveDeployConfiguration" type="int">0</value>
    <value key="ProjectExplorer.Target.ActiveRunConfiguration" type="int">0</value>
    <valuemap key="ProjectExplorer.Target.BuildConfiguration.0" type="QVariantMap">
        <value type="QString"></value>
       </valuelist>
       <valuelist key="Qt4ProjectManager.MaemoRunConfiguration.LastDeployedTimes" type="QVariantList">
-       <value type="QDateTime">2011-07-14T21:19:35</value>
+       <value type="QDateTime">2011-08-10T16:11:53</value>
       </valuelist>
      </valuemap>
      <value key="ProjectExplorer.BuildStepList.StepsCount" type="int">2</value>
diff --git a/classes/clockswidgetplugin.cpp b/classes/clockswidgetplugin.cpp
new file mode 100644 (file)
index 0000000..1fc6c1b
--- /dev/null
@@ -0,0 +1,17 @@
+#include "clockswidgetplugin.h"
+#include "wrappedclockswidget.h"
+#include <qdeclarative.h>
+
+ClocksWidgetPlugin::ClocksWidgetPlugin(QObject *parent) :
+    QDeclarativeExtensionPlugin(parent)
+{
+
+
+}
+
+void ClocksWidgetPlugin::registerTypes(const char *uri)
+{
+    qmlRegisterType<WrappedClocksWidget>(uri,1,0,"ChessClocks");
+}
+
+Q_EXPORT_PLUGIN2(clockswidgetplugin,ClocksWidgetPlugin);
diff --git a/classes/clockswidgetplugin.h b/classes/clockswidgetplugin.h
new file mode 100644 (file)
index 0000000..16e58ab
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef CLOCKSWIDGETPLUGIN_H
+#define CLOCKSWIDGETPLUGIN_H
+
+#include <QDeclarativeExtensionPlugin>
+
+class ClocksWidgetPlugin : public QDeclarativeExtensionPlugin
+{
+    Q_OBJECT
+public:
+    explicit ClocksWidgetPlugin(QObject *parent = 0);
+
+    void registerTypes(const char *uri);
+
+signals:
+
+public slots:
+
+};
+
+#endif // CLOCKSWIDGETPLUGIN_H
diff --git a/classes/wrappedclockswidget.cpp b/classes/wrappedclockswidget.cpp
new file mode 100644 (file)
index 0000000..09417ef
--- /dev/null
@@ -0,0 +1,87 @@
+/**************************************************************************
+
+   Chess Clock
+
+   This file is part of Chess Clock software.
+
+   (This file) Copyright (c) Heli Hyvättinen 2011
+
+   Chess Clock is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   Chess Clock is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+
+**************************************************************************/
+
+
+#include "wrappedclockswidget.h"
+
+
+WrappedClocksWidget::WrappedClocksWidget(QObject *parent) :
+    QGraphicsProxyWidget(parent)
+{
+    pClocksWidget_ = NULL;
+    pWhiteClock_ = NULL;
+    pBlackClock_ = NULL;
+}
+
+WrappedClocksWidget::startGame(QString timeControl, int whiteInitialTime, int whiteAdditionalTime, int whiteTurnsPerAddition, int blackInitialTime, int blackAdditionalTime, int blackTurnsPerAddition)
+{
+
+    deleteOldWidgets();
+
+    pWhiteClock_ = new ChessClockWidget(true);
+    pWhiteClock_->setTimeAvailable(whiteInitialTime);
+
+    pBlackClock_ = new ChessClockWidget(false);
+    pBlackClock_->setTimeAvailable(blackInitialTime);
+
+    pClocksWidget_ = new ClocksWidget(pWhiteClock_, pBlackClock_);
+
+
+    pClocksWidget_->setAttribute(Qt::WA_NoSystemBackground);
+    setWidget(pClocksWidget_);
+
+
+}
+
+WrappedClocksWidget::~WrappedClocksWidget()
+{
+    deleteOldWidgets;
+}
+
+WrappedClocksWidget::isPlayStarted()
+{
+    if (!pClocksWidget_)
+        return false;
+
+    return  pClocksWidget_->isPlayStarted();
+}
+
+WrappedClocksWidget::deleteOldWidgets()
+{
+    if (pClocksWidget_)
+    {
+        delete pClocksWidget_;
+        pClocksWidget_ = NULL;
+    }
+
+    if (pWhiteClock_)
+    {
+        delete pWhiteClock_;
+        pWhiteClock_ = NULL;
+    }
+
+    if (pBlackClock_)
+    {
+        delete pBlackClock_;
+        pBlackClock_ = NULL;
+    }
+}
+
diff --git a/classes/wrappedclockswidget.h b/classes/wrappedclockswidget.h
new file mode 100644 (file)
index 0000000..389aae1
--- /dev/null
@@ -0,0 +1,67 @@
+/**************************************************************************
+
+   Chess Clock
+
+   This file is part of Chess Clock software.
+
+   (This file) Copyright (c) Heli Hyvättinen 2011
+
+   Chess Clock is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   Chess Clock is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+
+**************************************************************************/
+
+#ifndef WRAPPEDCLOCKSWIDGET_H
+#define WRAPPEDCLOCKSWIDGET_H
+
+#include <QGraphicsProxyWidget>
+#include "clockswidget.h"
+#include "chessclock.h"
+
+/*! A proxy for ClocksWidget for using it from QML
+
+    @author Heli Hyvättinen
+    @date 2011-08-11
+
+
+    SOLVE:
+    Needed to create new ClocksWidget to
+    every new game.
+
+  */
+
+class WrappedClocksWidget : public QGraphicsProxyWidget
+{
+    Q_OBJECT
+public:
+    explicit WrappedClocksWidget(QObject *parent = 0);
+
+    virtual ~WrappedClocksWidget();
+
+    void startGame(QString timeControl, int whiteInitialTime, int whiteAdditionalTime, int whiteTurnsPerAddition, int blackInitialTime, int blackAdditionalTime, int blackTurnsPerAddition);
+
+    bool isPlayStarted();
+
+signals:
+
+public slots:
+
+private:
+
+    deleteOldWidgets();
+
+    ClocksWidget * pClocksWidget_;
+    ChessClock * pWhiteClock_;
+    ChessClock * pBlackClock_;
+
+};
+
+#endif // WRAPPEDCLOCKSWIDGET_H
diff --git a/clockswidgetplugin/classes/chessclock.cpp b/clockswidgetplugin/classes/chessclock.cpp
new file mode 100644 (file)
index 0000000..aedf531
--- /dev/null
@@ -0,0 +1,184 @@
+ /**************************************************************************
+
+    Chess Clock
+
+    Copyright (c) Arto Hyvättinen 2010
+
+    This file is part of Chess Clock software.
+
+    Chess Clock is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    Chess Clock is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+
+**************************************************************************/
+
+#include "chessclock.h"
+
+#include "turninformation.h"
+
+const int ChessClock::UPDATEINTERVAL;
+const int ChessClock::DONTEATBATTERYTIME;
+
+ChessClock::ChessClock(bool white, QWidget *parent) :
+    QWidget(parent)
+{
+    isWhite_ = white;
+    loser_ = false;
+    turn_ = 0;
+    dontEatBatteryEmitted_ = false;
+    timePlayedBeforeTurn_ = 0;
+    status_ = NotRunning;
+    another_ = 0;
+    currentTurn_ = 0;
+
+    // Set clock timer calculating played time
+    clockTime_.start();
+
+    // Set updating timer
+    updateTimer_.setInterval( UPDATEINTERVAL );
+    connect( &updateTimer_, SIGNAL(timeout()),this,SLOT(updateClock()));
+}
+
+void ChessClock::startTurn()
+{
+    turn_++;
+    dontEatBatteryEmitted_ = false;
+
+    // Turn information for this new turn
+    currentTurn_ = new TurnInformation(turn_, isWhite_);
+    clockTime_.restart();
+    updateTimer_.start();
+    status_=Running;
+
+    // Repaint clock
+    updateClock();
+}
+
+void ChessClock::pauseTurn()
+{
+    updateTimer_.stop();
+    // Update turn time
+    currentTurn_->addTime( clockTime_.restart() );
+    status_ = Paused;
+    updateClock();
+}
+
+void ChessClock::continueTurn()
+{
+    // Continue paused game
+    // Add pause duration to information object
+    currentTurn_->addPause( clockTime_.restart() );
+    status_ = Running;
+    updateTimer_.start();
+    updateClock();
+}
+
+
+TurnInformation* ChessClock::endTurn()
+{
+    updateTimer_.stop();
+    status_ = NotRunning;
+
+    updateClock();
+    // Count time played
+    timePlayedBeforeTurn_ = getTimePlayed();
+
+    // Count time available
+    // This update current turn
+    timeAvailableBeforeTurn_ = getTimeAvailable();
+
+
+    // Close and return turn information
+    currentTurn_->turnReady(timeAvailableBeforeTurn_ );
+    TurnInformation* information = currentTurn_;
+    currentTurn_ = 0;
+
+    emit turnEnded();
+    return information;
+}
+
+void ChessClock::setAnother(ChessClock *another)
+{
+    another_ = another;
+}
+
+int ChessClock::getTimeAvailable()
+{
+    // Most simple - will be overwritten in more complex time controls:
+    // subtract duration time!
+    if( currentTurn_)
+    {
+        // Update turn time
+        return timeAvailableBeforeTurn_-currentTurnPlayed();
+    }
+    else
+        return timeAvailableBeforeTurn_;
+}
+
+
+int ChessClock::getTimePlayed()
+{
+     return timePlayedBeforeTurn_ + currentTurnPlayed();
+}
+
+
+void ChessClock::setTimeAvailable(int msecs)
+{
+    timeAvailableBeforeTurn_ = msecs;
+}
+
+
+void ChessClock::addTime(int msecs)
+{
+   timeAvailableBeforeTurn_ += msecs;
+}
+
+int ChessClock::currentTurnPlayed()
+{
+    if( currentTurn_ )
+    {
+        // Update current time
+        if( status_ == Running )
+        {
+            currentTurn_->addTime( clockTime_.restart());
+
+            // since 1.1.2
+            // emit dontEatBattery signal when screen should not to be keeped on
+            if ( currentTurn_->getDuration() > DONTEATBATTERYTIME  && dontEatBatteryEmitted_ == false )
+            {
+                dontEatBatteryEmitted_ = true;
+                emit dontEatBattery();
+            }
+        }
+
+        // Return current time
+        return currentTurn_->getDuration();
+     }
+    else
+        // No current turn!
+        return 0;
+}
+
+void ChessClock::updateClock()
+{
+    // Check loser
+    if( another_ && !another_->isLoser())
+    {
+        if( getTimeAvailable() < 0 && !loser_)
+        {
+            loser_ = true;
+            emit timeOutLoser();
+        }
+
+    }
+    repaintClock();
+
+}
+
diff --git a/clockswidgetplugin/classes/chessclock.h b/clockswidgetplugin/classes/chessclock.h
new file mode 100644 (file)
index 0000000..1f8f262
--- /dev/null
@@ -0,0 +1,149 @@
+ /**************************************************************************
+
+    Chess Clock
+
+    Copyright (c) Arto Hyvättinen 2010
+
+    This file is part of Chess Clock software.
+
+    Chess Clock is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    Chess Clock is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+
+**************************************************************************/
+
+#ifndef CHESSCLOCK_H
+#define CHESSCLOCK_H
+
+#include <QWidget>
+#include <QTime>
+#include <QTimer>
+
+class TurnInformation;
+
+
+/*! Clock of a individual player
+
+  @author Arto Hyvättinen
+  @date 2010-08-13
+
+  Base class of chess clock.
+
+  ChessClock has not GUI itself, but it is subclass of QWidget
+  avoiding polymorphism.
+
+  */
+class ChessClock : public QWidget
+{
+    Q_OBJECT
+public:
+    ChessClock(bool white, QWidget *parent = 0);
+
+public:
+    enum RunningStatus {
+        NotRunning /*! Not turn  */ = 0,
+        Running /*! Turn running */ = 1,
+        Paused /*! Turn paused */ = 2
+    };
+
+    bool isLoser() const  { return loser_; }
+    int getTurn() const  { return turn_; }
+    bool isWhite() const { return isWhite_; }
+    RunningStatus getStatus() const { return status_ ; }
+
+
+    /*! Start new turn */
+    virtual void startTurn();
+
+    /*! End this turn.
+
+      Player has done his move.
+      @return Locked turn information */
+    virtual TurnInformation* endTurn();
+
+    /*! Pause clock */
+    virtual void pauseTurn();
+
+    /*! Continue paused game */
+    virtual void continueTurn();
+
+    /*! Set another chess clock for connecting
+      @param another Clock of opposite player */
+    void setAnother( ChessClock* another);
+
+    /*! Get total time available
+
+      Time does't contain delays.
+
+      @return Time available in msecs */
+    virtual int getTimeAvailable();
+
+    /*! Get total time played
+      @return Time played in msecs */
+    virtual int getTimePlayed();
+
+    /*! Set time available
+
+      @param msecs Time available in msecs */
+    void setTimeAvailable(int msecs);
+
+    /*! Add time
+
+      Add time to timeAvailableBeforeTurn_ total available time
+      counter.
+
+      @param msecs Time to add in msecs */
+    void addTime(int msecs);
+
+    /*! Get time played current turn.
+      @return Time in msecs */
+    int currentTurnPlayed();
+
+
+signals:
+    void timeOutLoser();
+    void turnEnded();
+    /*! Emitted after 30 min played one turn.
+
+      Will cause screen not to keeped lit.
+      @since 1.1.2
+      */
+    void dontEatBattery();
+
+public slots:    
+
+    /*! Refresh clock information */
+    virtual void repaintClock() = 0;
+
+    /*! Update clock information, check looser state and refresh */
+    virtual void updateClock();
+
+protected:
+    ChessClock* another_; /*! Another player's clock */
+
+    bool loser_;        /*! Is player losed because of timeout */
+    int turn_;          /*! Current turn */
+    RunningStatus status_;
+    TurnInformation* currentTurn_;
+    bool dontEatBatteryEmitted_;
+
+    int timePlayedBeforeTurn_;    /*! Time played in this game BEFORE this turn msecs */
+    int timeAvailableBeforeTurn_; /*! Time available for play BEFORE this turn msecs !*/
+
+    bool isWhite_;      /*! True if white player */
+
+    QTime clockTime_;
+    QTimer updateTimer_;
+
+    static const int UPDATEINTERVAL = 1000; /** Clock updating interval in msecs */
+    static const int DONTEATBATTERYTIME = 30 * 60 * 1000; /*! Time after screen not to keep lit */
+};
+
+#endif // CHESSCLOCK_H
diff --git a/clockswidgetplugin/classes/chessclockwidget.cpp b/clockswidgetplugin/classes/chessclockwidget.cpp
new file mode 100644 (file)
index 0000000..cfba058
--- /dev/null
@@ -0,0 +1,177 @@
+ /**************************************************************************
+
+    Chess Clock
+
+    Copyright (c) Arto Hyvättinen 2010
+
+    This file is part of Chess Clock software.
+
+    Chess Clock is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    Chess Clock is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+
+**************************************************************************/
+
+#include "chessclockwidget.h"
+
+#include <QLabel>
+#include <QFont>
+#include <QVBoxLayout>
+#include <QHBoxLayout>
+
+ChessClockWidget::ChessClockWidget(bool white, QWidget *parent) :
+    ChessClock(white, parent)
+{
+    greenTime_ = 0;
+
+    mainLayout = new QVBoxLayout;
+
+    initPictures();
+    initLabels();
+    initTop();
+    initBottom();
+
+    setLayout( mainLayout );
+}
+
+
+void ChessClockWidget::setGreenTime(int msecs)
+{
+    greenTime_ = msecs;
+}
+
+void ChessClockWidget::initPictures()
+{
+    // Load pictures from resources.
+    if(  isWhite() )
+      {
+          picActive_.load(":/rc/pic/white_blue.png");
+          picPassive_.load(":/rc/pic/white_gray.png");
+      }
+      else
+      {
+          picActive_.load(":/rc/pic/black_blue.png");
+          picPassive_.load(":/rc/pic/black_gray.png");
+      }
+      picLoser_.load(":/rc/pic/loser.png");
+}
+
+void ChessClockWidget::initLabels()
+{
+    QFont normalFont("Helvetica",24);
+
+    pictureLabel_ = new QLabel;
+    pictureLabel_->setPixmap( picPassive_ );
+
+    timeUsedLabel_ = new QLabel;
+    timeAverageLabel_ = new QLabel;
+    turnLabel_=new QLabel;
+    turnTimeLabel_=new QLabel;
+
+    timeUsedLabel_->setFont(normalFont);
+    timeAverageLabel_->setFont(normalFont);
+    turnLabel_->setFont(normalFont);
+
+    QFont turnTimeFont("Helvetica",36,QFont::Bold);
+    turnTimeLabel_->setFont(turnTimeFont);
+
+    loserLabel_ = new QLabel;
+    loserLabel_->setPixmap(picLoser_);
+    loserLabel_->setVisible(false);
+
+}
+
+void ChessClockWidget::initTop()
+{
+    QVBoxLayout* details = new QVBoxLayout();
+    details->addWidget(timeUsedLabel_);
+    details->addWidget(timeAverageLabel_);
+    details->addWidget(turnLabel_);
+    details->addWidget(turnTimeLabel_);
+
+    QHBoxLayout* topLayout = new QHBoxLayout();
+    if( isWhite() )
+    {
+        // White player
+        //  Picture  |  Details | .. | LOSER
+        topLayout->addWidget( pictureLabel_ );
+        topLayout->addLayout( details );
+        topLayout->addStretch();
+        topLayout->addWidget(loserLabel_);
+    }
+    else
+    {
+        // Black player
+        // LOSER | ... | Details | Picture
+        topLayout->addWidget(loserLabel_);
+        topLayout->addStretch();
+        topLayout->addLayout( details );
+        topLayout->addWidget( pictureLabel_ );
+    }
+    mainLayout->addLayout(topLayout);
+}
+
+void ChessClockWidget::initBottom()
+{
+    // At bottom, time left in BIG font!
+    QFont bigfont("Helvetica",65,QFont::Bold);
+    leftLabel_ = new QLabel("0.00.00");
+    leftLabel_->setFont(bigfont);
+    // Black player: right alignment
+    if( !isWhite() )
+       leftLabel_->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
+    mainLayout->addWidget(leftLabel_);
+}
+
+void ChessClockWidget::repaintClock()
+{
+    // Set picture
+    if( getStatus() == Running || getStatus() == Paused)
+        pictureLabel_->setPixmap( picActive_);
+    else
+        pictureLabel_->setPixmap(picPassive_);
+
+    timeUsedLabel_->setText( timeString( getTimePlayed() ));
+
+    // Time average per turn
+    int timeAverage;
+    if( getTurn() == 0)
+        timeAverage = 0;
+    else
+        timeAverage = getTimePlayed() / getTurn();
+    timeAverageLabel_->setText( tr("Avg %1").arg( timeString( timeAverage ) ) );
+
+    turnLabel_->setText( tr("Turn %1").arg(getTurn()));
+
+    // Current turn played
+    // Extra time of this turn is shown in green.
+    if( currentTurnPlayed() < greenTime_ )
+        turnTimeLabel_->setText( QString("<font color=green> %1 </font>") .arg(timeString( currentTurnPlayed()) ) );
+    else
+        turnTimeLabel_->setText( timeString( currentTurnPlayed() ) );
+
+    // Loser flag
+    loserLabel_->setVisible( isLoser());
+
+    leftLabel_->setText( timeString(getTimeAvailable()));
+
+}
+
+QString ChessClockWidget::timeString(int msecs)
+{
+    int secs = msecs / 1000 % 60;
+    int mins = msecs / ( 60 * 1000) % 60;
+    int hours = msecs / ( 60 * 60 * 1000 );
+    if( msecs < 0)
+        return QString(tr("<font color=red> %1:%2:%3 <font>").arg(0-hours).arg(0-mins,2,10,QChar('0')).arg(0-secs,2,10,QChar('0')));
+    else
+        return QString(tr("%1:%2:%3").arg(hours).arg(mins,2,10,QChar('0')).arg(secs,2,10,QChar('0')));
+
+}
diff --git a/clockswidgetplugin/classes/chessclockwidget.h b/clockswidgetplugin/classes/chessclockwidget.h
new file mode 100644 (file)
index 0000000..d8f9e4c
--- /dev/null
@@ -0,0 +1,91 @@
+ /**************************************************************************
+
+    Chess Clock
+
+    Copyright (c) Arto Hyvättinen 2010
+
+    This file is part of Chess Clock software.
+
+    Chess Clock is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    Chess Clock is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+
+**************************************************************************/
+
+#ifndef CHESSCLOCKWIDGET_H
+#define CHESSCLOCKWIDGET_H
+
+#include "chessclock.h"
+#include <QPixmap>
+#include <QString>
+
+class QVBoxLayout;
+class QLabel;
+
+/*!  Simple chess clock widget
+
+  @author Arto Hyvättinen
+  @date 2010-08-14
+
+
+  */
+class ChessClockWidget : public ChessClock
+{
+    Q_OBJECT
+public:
+    ChessClockWidget(bool white, QWidget *parent = 0);
+    /*! Set time begin of turn shown green.
+
+      Extra time of turn is shown green.
+      @param msecs Green time in msecs
+      */
+    void setGreenTime(int msecs);
+
+signals:
+
+public slots:
+    virtual void repaintClock();
+
+protected:
+    /*! Init pictures from resources */
+    virtual void initPictures();
+    /*! Init QLabels */
+    virtual void initLabels();
+    /*! Init top: picture, details, loser mark */
+    virtual void initTop();
+    /*! Init time left in bottom.
+      Override to make different bottom area */
+    virtual void initBottom();
+
+    /*! Convert msecs to h:mm:ss QString */
+   static QString timeString(int msecs);
+
+protected:
+
+    QPixmap picActive_;
+    QPixmap picPassive_;
+    QPixmap picLoser_;
+
+    QLabel* pictureLabel_;
+    QLabel* timeUsedLabel_;
+    QLabel* timeAverageLabel_;
+    QLabel* turnLabel_;
+    QLabel* turnTimeLabel_;
+    QLabel* loserLabel_;
+    QLabel* leftLabel_;
+
+    QVBoxLayout* mainLayout;
+
+    int greenTime_; /*! Green time in begin of turn */
+
+
+};
+
+#endif // CHESSCLOCKWIDGET_H
diff --git a/clockswidgetplugin/classes/clockswidget.cpp b/clockswidgetplugin/classes/clockswidget.cpp
new file mode 100644 (file)
index 0000000..0daf329
--- /dev/null
@@ -0,0 +1,233 @@
+ /**************************************************************************
+
+    Chess Clock
+
+    Copyright (c) Arto Hyvättinen 2010
+
+    This file is part of Chess Clock software.
+
+    Chess Clock is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    Chess Clock is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+
+**************************************************************************/
+
+#include "clockswidget.h"
+#include "chessclock.h"
+
+#include "screenlitkeeper.h"
+
+#include <QLabel>
+#include <QPixmap>
+#include <QApplication>
+#include <QHBoxLayout>
+#include <QVBoxLayout>
+#include <QFont>
+#include <cstdlib>
+#include <QMouseEvent>
+#include <QToolButton>
+#include <QSize>
+#include <QWidget>
+
+ClocksWidget::ClocksWidget(ChessClock *white, ChessClock *black, QWidget *parent):
+    QWidget(parent)
+{
+    // Set up clocks
+    white_ = white;
+    black_ = black;
+
+    // SET UP UI
+    // Make layout for clocks
+    QHBoxLayout* clockLayout = new QHBoxLayout;
+    clockLayout->addWidget(white_ );
+    clockLayout->addWidget( black_ );
+
+    // Pause information label
+    pauseLabel_ = new QLabel( tr("<font color=yellow>Paused. Touch to continue.</font>"),this);
+    pauseLabel_->setFont( QFont("Helvetica",25));
+    pauseLabel_->setAlignment( Qt::AlignCenter);
+    pauseLabel_->setVisible( false );
+
+    // Welcome label for first touch
+    welcomeLabel_ = new QLabel( tr("<font color=green>Welcome! Please touch to start game.<br>"
+                                   "Then touch to end turn.</font>"),this);
+    welcomeLabel_->setFont( QFont("Helvetica",25));
+    welcomeLabel_->setAlignment( Qt::AlignCenter);
+    welcomeLabel_->setVisible( true );  // Show welcome message
+
+    // Pause button
+    pauseButton_ = new QToolButton;
+    pauseButton_->setIcon( QIcon(":/rc/pic/pausebutton.png"));
+    pauseButton_->setIconSize(QSize(75,75));
+    connect(pauseButton_, SIGNAL(clicked()), this, SLOT(pause()));
+    pauseButton_->setVisible(false);
+
+    // Put all in layout
+    QVBoxLayout* mainLayout = new QVBoxLayout;
+    mainLayout->addLayout(clockLayout);
+
+    // Extra layout and widget for information
+    QVBoxLayout* extraLayout = new QVBoxLayout;
+    extraLayout->addWidget(pauseLabel_);
+    extraLayout->addWidget(welcomeLabel_);
+
+    QHBoxLayout* pbLayout = new QHBoxLayout;
+    pbLayout->addStretch();
+    pbLayout->addWidget(pauseButton_);
+    pbLayout->addStretch();
+    extraLayout->addLayout(pbLayout);
+
+    QWidget* extraWidget = new QWidget(this);
+    extraWidget->setLayout(extraLayout);
+    // Some fun event filtering to grap clicking welcome and pause labels...
+    extraWidget->installEventFilter(this);
+    pauseLabel_->installEventFilter(this);
+    welcomeLabel_->installEventFilter(this);
+
+    mainLayout->addWidget(extraWidget);
+    setLayout( mainLayout);
+    status_ = Welcome;
+
+    // First paint
+    white_->repaintClock();
+    black_->repaintClock();
+
+    // Set up others
+    white_->setAnother(black_);
+    black_->setAnother(white_);
+
+    delayTimer_.start(); // Initial start
+
+    recentX = recentY = -1;
+
+    // ScreenLitKeeper to keep screen lit when playing
+    keeper_ = new ScreenLitKeeper(this);
+
+    connect( white, SIGNAL(dontEatBattery()), this, SLOT(saveScreen()));
+    connect( black, SIGNAL(dontEatBattery()), this, SLOT(saveScreen()));
+}
+
+ClocksWidget::~ClocksWidget()
+{
+    delete white_;
+    delete black_;
+}
+
+void ClocksWidget::pause()
+{
+    if(status_ == WhiteTurn)
+    {
+        status_= WhitePause;
+        white_->pauseTurn();
+        pauseLabel_->setVisible(true);
+        pauseButton_->setVisible(false);
+        keeper_->keepScreenLit(false);
+
+    }
+    else if( status_ == BlackTurn)
+    {
+        status_ = BlackPause;
+        black_->pauseTurn();
+        pauseLabel_->setVisible(true);
+        pauseButton_->setVisible(false);
+        keeper_->keepScreenLit(false);
+    }
+}
+
+void ClocksWidget::stopPlay()
+{
+    if( status_ == BlackTurn || status_ == BlackPause )
+       emit TurnFinished( black_->endTurn());
+    else if( status_ == WhiteTurn || status_ == WhitePause )
+        emit TurnFinished( white_->endTurn());
+    status_ = Stopped;
+}
+
+void ClocksWidget::saveScreen()
+{
+    keeper_->keepScreenLit(false);
+}
+
+
+void ClocksWidget::mouseReleaseEvent(QMouseEvent *event)
+{
+
+    // To avoid double clicks
+    // a) delay (default 1,2 secs) OR
+    // b) distance more than 90 pixels in axis.
+    if( delayTimer_.elapsed() > CLICKDELAY ||
+        std::abs( event->x() - recentX ) > 90 ||
+        std::abs( event->y() - recentY ) > 90
+        )
+    {
+        delayTimer_.start();    // to reset delay timer!
+        switch( status_)
+        {
+        case Welcome :
+            // Start game!
+            welcomeLabel_->setVisible(false);
+            pauseButton_->setVisible(true);
+            keeper_->keepScreenLit(true);
+            white_->startTurn();
+            status_ = WhiteTurn;
+            break;
+        case WhiteTurn:
+            // White turn finished, then black
+            emit TurnFinished( white_->endTurn());
+            black_->startTurn();
+            status_=BlackTurn;
+            break;
+        case BlackTurn:
+            // Black finished, then white
+            emit TurnFinished( black_->endTurn());
+            white_->startTurn();
+            status_=WhiteTurn;
+            break;
+        case WhitePause:
+            // Continue play
+            keeper_->keepScreenLit(true);
+            pauseLabel_->setVisible(false);
+            pauseButton_->setVisible(true);
+            white_->continueTurn();
+            status_=WhiteTurn;
+            break;
+        case BlackPause:
+            // Continue play
+            keeper_->keepScreenLit(true);
+            pauseLabel_->setVisible(false);
+            pauseButton_->setVisible(true);
+            black_->continueTurn();
+            status_=BlackTurn;
+            break;
+        case Stopped:
+            emit ClickedWhenStopped();
+
+
+        }
+    }
+    recentX = event->x();
+    recentY = event->y();
+}
+
+// to grap clicking pause or welcome label
+bool ClocksWidget::eventFilter(QObject *obj, QEvent *event)
+{
+    if (event->type() == QEvent::MouseButtonRelease) {
+        QMouseEvent *mEvent = static_cast<QMouseEvent *>(event);
+        mouseReleaseEvent( mEvent );
+        return true;
+    } else {
+        // standard event processing
+        return QObject::eventFilter(obj, event);
+    }
+}
+
+
+int const ClocksWidget::CLICKDELAY;
diff --git a/clockswidgetplugin/classes/clockswidget.h b/clockswidgetplugin/classes/clockswidget.h
new file mode 100644 (file)
index 0000000..d7810c1
--- /dev/null
@@ -0,0 +1,102 @@
+ /**************************************************************************
+
+    Chess Clock
+
+    Copyright (c) Arto Hyvättinen 2010
+
+    This file is part of Chess Clock software.
+
+    Chess Clock is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    Chess Clock is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+
+**************************************************************************/
+
+#ifndef CLOCKSWIDGET_H
+#define CLOCKSWIDGET_H
+
+#include <QWidget>
+#include <QTime>
+
+class QHBoxLayout;
+class QVBoxLayout;
+class QLabel;
+class TurnInformation;
+class QToolButton;
+class ScreenLitKeeper;
+
+class ChessClock;
+
+/*! Widget with two clocks
+
+    @author Arto Hyvättinen
+    @date 2010-08-14
+
+    Central widget of Chess Clock.
+    Needed to create new ClocksWidget to
+    every new game.
+
+  */
+class ClocksWidget : public QWidget
+{
+    Q_OBJECT
+public:
+    ClocksWidget(ChessClock* white, ChessClock* black, QWidget *parent = 0);
+    ~ClocksWidget();
+    bool isPlayStarted()  { return (status_!=Welcome); }
+
+protected:
+    void mouseReleaseEvent(QMouseEvent *event);
+    bool eventFilter(QObject *obj, QEvent *event);
+
+
+signals:
+   void TurnFinished(TurnInformation* turnInfo);
+   void ClickedWhenStopped();
+
+public slots:
+   /*! Pause game */
+    void pause();
+    /*! End the game */
+    void stopPlay();
+    /*! Not to keep screen lit */
+    void saveScreen();
+
+protected:
+    ChessClock* white_;
+    ChessClock* black_;
+
+    QLabel* pauseLabel_;
+    QLabel* welcomeLabel_;
+    QToolButton* pauseButton_;
+    ScreenLitKeeper* keeper_;
+
+    enum GameStatus {
+        Stopped  /*! Not running */,
+        WhiteTurn,
+        BlackTurn,
+        WhitePause,
+        BlackPause,
+        Welcome
+        };
+
+    GameStatus status_;
+    QTime delayTimer_; /*! To avoid double clicks */
+
+    int recentX;
+    int recentY;
+
+    static int const CLICKDELAY = 1500 ; /*! Click delay in msecs */
+
+
+
+};
+
+#endif // CLOCKSWIDGET_H
diff --git a/clockswidgetplugin/classes/clockswidgetplugin.cpp b/clockswidgetplugin/classes/clockswidgetplugin.cpp
new file mode 100644 (file)
index 0000000..0fdd915
--- /dev/null
@@ -0,0 +1,17 @@
+#include "clockswidgetplugin.h"
+#include "wrappedclockswidget.h"
+#include <qdeclarative.h>
+
+ClocksWidgetPlugin::ClocksWidgetPlugin(QObject *parent) :
+    QDeclarativeExtensionPlugin(parent)
+{
+}
+
+
+void ClocksWidgetPlugin::registerTypes(const char *uri)
+{
+    qmlRegisterType<WrappedClocksWidget>(uri,1,0,"ChessClocks");
+}
+
+Q_EXPORT_PLUGIN2(clockswidgetplugin,ClocksWidgetPlugin);
+
diff --git a/clockswidgetplugin/classes/clockswidgetplugin.h b/clockswidgetplugin/classes/clockswidgetplugin.h
new file mode 100644 (file)
index 0000000..9dd259f
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef CLOCKSWIDGETPLUGIN_H
+#define CLOCKSWIDGETPLUGIN_H
+
+#include <QDeclarativeExtensionPlugin>
+
+class ClocksWidgetPlugin : public QDeclarativeExtensionPlugin
+{
+    Q_OBJECT
+public:
+    explicit ClocksWidgetPlugin(QObject *parent = 0);
+
+        void registerTypes(const char *uri);
+
+signals:
+
+public slots:
+
+
+
+};
+
+#endif // CLOCKSWIDGETPLUGIN_H
diff --git a/clockswidgetplugin/classes/wrappedclockswidget.cpp b/clockswidgetplugin/classes/wrappedclockswidget.cpp
new file mode 100644 (file)
index 0000000..89e48c0
--- /dev/null
@@ -0,0 +1,87 @@
+/**************************************************************************
+
+   Chess Clock
+
+   This file is part of Chess Clock software.
+
+   (This file) Copyright (c) Heli Hyvättinen 2011
+
+   Chess Clock is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   Chess Clock is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+
+**************************************************************************/
+
+
+#include "wrappedclockswidget.h"
+
+
+WrappedClocksWidget::WrappedClocksWidget(QObject *parent) :
+    QGraphicsProxyWidget()
+{
+    pClocksWidget_ = NULL;
+    pWhiteClock_ = NULL;
+    pBlackClock_ = NULL;
+}
+
+void WrappedClocksWidget::startGame(QString timeControl, int whiteInitialTime, int whiteAdditionalTime, int whiteTurnsPerAddition, int blackInitialTime, int blackAdditionalTime, int blackTurnsPerAddition)
+{
+
+    deleteOldWidgets();
+
+    pWhiteClock_ = new ChessClockWidget(true);
+    pWhiteClock_->setTimeAvailable(whiteInitialTime);
+
+    pBlackClock_ = new ChessClockWidget(false);
+    pBlackClock_->setTimeAvailable(blackInitialTime);
+
+    pClocksWidget_ = new ClocksWidget(pWhiteClock_, pBlackClock_);
+
+
+    pClocksWidget_->setAttribute(Qt::WA_NoSystemBackground);
+    setWidget(pClocksWidget_);
+
+
+}
+
+ WrappedClocksWidget::~WrappedClocksWidget()
+{
+     deleteOldWidgets();
+}
+
+bool WrappedClocksWidget::isPlayStarted()
+{
+    if (!pClocksWidget_)
+        return false;
+
+    return  pClocksWidget_->isPlayStarted();
+}
+
+void WrappedClocksWidget::deleteOldWidgets()
+{
+    if (pClocksWidget_)
+    {
+        delete pClocksWidget_;
+        pClocksWidget_ = NULL;
+    }
+
+    if (pWhiteClock_)
+    {
+        delete pWhiteClock_;
+        pWhiteClock_ = NULL;
+    }
+
+    if (pBlackClock_)
+    {
+        delete pBlackClock_;
+        pBlackClock_ = NULL;
+    }
+}
+
diff --git a/clockswidgetplugin/classes/wrappedclockswidget.h b/clockswidgetplugin/classes/wrappedclockswidget.h
new file mode 100644 (file)
index 0000000..aa25167
--- /dev/null
@@ -0,0 +1,65 @@
+/**************************************************************************
+
+   Chess Clock
+
+   This file is part of Chess Clock software.
+
+   (This file) Copyright (c) Heli Hyvättinen 2011
+
+   Chess Clock is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   Chess Clock is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+
+**************************************************************************/
+
+#ifndef WRAPPEDCLOCKSWIDGET_H
+#define WRAPPEDCLOCKSWIDGET_H
+
+#include <QGraphicsProxyWidget>
+#include "clockswidget.h"
+#include "chessclockwidget.h"
+
+/*! A proxy for ClocksWidget for using it from QML
+
+    @author Heli Hyvättinen
+    @date 2011-08-11
+
+
+    SOLVE:
+    Needed to create new ClocksWidget to
+    every new game.
+
+  */
+
+class WrappedClocksWidget : public QGraphicsProxyWidget
+{
+    Q_OBJECT
+public:
+    explicit WrappedClocksWidget(QObject *parent = 0);
+    virtual ~WrappedClocksWidget();
+
+    void startGame(QString timeControl, int whiteInitialTime, int whiteAdditionalTime, int whiteTurnsPerAddition, int blackInitialTime, int blackAdditionalTime, int blackTurnsPerAddition);
+
+    bool isPlayStarted();
+
+signals:
+
+public slots:
+
+private:
+
+    void deleteOldWidgets();
+    ClocksWidget * pClocksWidget_;
+    ChessClock * pWhiteClock_;
+    ChessClock * pBlackClock_;
+
+};
+
+#endif // WRAPPEDCLOCKSWIDGET_H
diff --git a/clockswidgetplugin/clockswidgetplugin.pro b/clockswidgetplugin/clockswidgetplugin.pro
new file mode 100644 (file)
index 0000000..5c749a0
--- /dev/null
@@ -0,0 +1,31 @@
+ TEMPLATE = lib
+ CONFIG += qt plugin
+ QT += declarative
+ CONFIG += mobility
+ MOBILITY += systeminfo
+
+ DESTDIR = lib
+ OBJECTS_DIR = tmp
+ MOC_DIR = tmp
+
+HEADERS += \
+    classes/wrappedclockswidget.h \
+    classes/clockswidget.h \
+    classes/chessclockwidget.h \
+    classes/chessclock.h \
+    classes/clockswidgetplugin.h
+
+SOURCES += \
+    classes/wrappedclockswidget.cpp \
+    classes/clockswidget.cpp \
+    classes/chessclockwidget.cpp \
+    classes/chessclock.cpp \
+    classes/clockswidgetplugin.cpp
+
+OTHER_FILES += \
+    qmldir
+
+unix:!symbian:!maemo5 {
+    target.path = /opt/clockswidgetplugin/lib
+    INSTALLS += target
+}
diff --git a/clockswidgetplugin/qmldir b/clockswidgetplugin/qmldir
new file mode 100644 (file)
index 0000000..8ad5e73
--- /dev/null
@@ -0,0 +1 @@
+plugin chessclocks lib
index 65a0cd0..96e27d4 100644 (file)
 
 import QtQuick 1.0
 import com.meego 1.0
-import QtQuick 1.1
+import QtQuick 1.1 
+
+
 
 Page
 {
     id: clocksPage
     tools:  commonTools
 
+    property int whiteInitialTime
+    property int blackInitialTime
+    property int whiteAdditionalTime
+    property int blackAdditionalTime
+    property int whiteTurnsPerAddition
+    property int blackTurnsPerAddition
+
+
+
+    ChessClocks
+    {
+
+    }
+
+
 
-Rectangle {
-    width: 100
-    height: 62
-}
 }
diff --git a/qml/ClocksWidgetWrapper.qml b/qml/ClocksWidgetWrapper.qml
new file mode 100644 (file)
index 0000000..83ddcf4
--- /dev/null
@@ -0,0 +1,27 @@
+/**************************************************************************
+
+   Chess Clock
+
+   This file is part of Chess Clock software.
+
+   (This file) Copyright (c) Heli Hyvättinen 2011
+
+   Chess Clock is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   Chess Clock is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+
+**************************************************************************/
+
+import QtQuick 1.0
+
+ClocksWidgetWrapper {
+    width: 400
+    height: 200
+}