/*
 * Copyright 2009-2010  Stefan Gehn <stefan@srcbox.net>
 * 
 * This program 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 2 of 
 * the License, or (at your option) any later version.
 * 
 * This program 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.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef PHOTOSCENE_H_
#define PHOTOSCENE_H_

#include "PhotoPage.hpp"
#include "TouchData.hpp"

#include <qgraphicsscene.h>
#include <qdatetime.h>
#include <qdatastream.h>

class QGraphicsItem;
class QGraphicsGridLayout;
class PhotoListView;
class PhotoDraggable;

class PhotoScene : public QGraphicsScene
{
Q_OBJECT

public:
  PhotoScene(QObject *parent, const QRectF &sceneRect);
  virtual ~PhotoScene();

	bool displayTouchDebug() const { return mDisplayTouchDebug; }
	void setDisplayTouchDebug(bool display);


	PhotoDraggable *photoDraggableAt(const QPointF &scenePos);

protected:
  virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
  virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
  virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
  virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
  virtual bool eventFilter(QObject *watched, QEvent *event);

private:
  bool loadScene(const QString &fileName);
  bool saveScene(const QString &fileName);

  void showPages(int leftPageIndex);
  PhotoPage *addPage();

	PhotoPage *pageAt(const QPointF &scenePos) const;
	void togglePageFixation(PhotoDraggable *pd, const QPointF &scenePos);

  void handleDualTouch(const IrTouch::TouchRect &r1, const IrTouch::TouchRect &r2);
  void resetTouchDebugItems();
  void updateTouchDebugItems(const IrTouch::TouchData &data);
  void updateMouseDebugItem(const QPointF &pos);
  void setTouchDebugVisible(bool b);


private Q_SLOTS:
	void createDraggable(const QPoint &globalPos, const QString &filePath);

	void slotPrevPage();
	void slotAddPage();
	void slotNextPage();
	void slotTurnPage();
	void slotPageDestroyed(QObject *pageObject);
	void slotSelectionChanged();

private:
  QGraphicsWidget *mPageParent;
  QGraphicsGridLayout *mPageLayout;

  QGraphicsWidget *mRightBar;
  QGraphicsWidget *mTopBar;

  QList<PhotoPage *> mPages;

  //! List of images, also the source for draggables
  QGraphicsProxyWidget *mPhotoLvProxy;
	QGraphicsWidget *mTrashWidget;

  //! Debug graphics for TouchRects
  QGraphicsItemGroup *mTouchDebugGroup;
	bool mDisplayTouchDebug;

  //! left page of photo-book
  PhotoPage *mCurrentLeftPage;

  //! right page of photo-book
  PhotoPage *mCurrentRightPage;

  IrTouch::TouchData mLastData;
  quint32 mTwoFingerId1, mTwoFingerId2;
  QTime mTwoFingerStamp;

  QRect mFirstSideRect;
};

#endif /* PHOTOSCENE_H_ */
