00001 #include "thumbnailitem.h"
00002 #include "thumbnailscene.h"
00003 #include <QWidget>
00004 #include <QPainter>
00005 #include <QApplication>
00006 #include <QDrag>
00007 #include <QMimeData>
00008
00009 ThumbnailItem::ThumbnailItem(const QPixmap& pm, int pageNum, QString lbl) :
00010 QGraphicsPixmapItem(pm),
00011
00012 pageIdx(pageNum),
00013 label(lbl)
00014 {
00015
00016
00017 setFlag(QGraphicsItem::ItemIsMovable, true);
00018 setFlag(QGraphicsItem::ItemIsSelectable, true);
00019 }
00020
00021 void ThumbnailItem::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget )
00022 {
00023
00024
00025 QGraphicsPixmapItem::paint(painter, option, widget);
00026
00027
00028
00029
00030
00031 QString pageStr = label;
00032 if (pageStr.isEmpty())
00033 pageStr = QString::number(pageIdx);
00034 QRect pageBounds = painter->boundingRect(0, 0, width(), height() - 1, Qt::AlignHCenter| Qt::AlignBottom, pageStr);
00035 QPainterPath pp;
00036 pp.moveTo(pageBounds.bottomRight());
00037 pp.arcTo(pageBounds.topRight().x(), pageBounds.topRight().y(), pageBounds.height() / 2, pageBounds.height() - 1, 270, 180);
00038
00039 pp.arcTo(pageBounds.topLeft().x(), pageBounds.topLeft().y(), pageBounds.height() / -2, pageBounds.height() - 1, 90, -180);
00040 pp.lineTo(pageBounds.bottomRight());
00041 pp.closeSubpath();
00042 QColor pageLabelFill(Qt::lightGray);
00043 if (((ThumbnailScene*)scene())->currentPage() == pageIdx)
00044 pageLabelFill = Qt::red;
00045 pageLabelFill.setAlpha(220);
00046 painter->fillPath(pp, QBrush(pageLabelFill));
00047
00048 painter->drawText(pageBounds, Qt::AlignCenter, pageStr);
00049 }
00050
00051 void ThumbnailItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
00052 {
00053 if (QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton))
00054 .length() < QApplication::startDragDistance())
00055 return;
00056
00057 QDrag *drag = new QDrag(event->widget());
00058 QMimeData *mime = new QMimeData;
00059 drag->setMimeData(mime);
00060 drag->exec();
00061 }
00062
00063 bool lessThan( const QGraphicsItem * s1 , const QGraphicsItem * s2 )
00064 {
00065 return ((ThumbnailItem*)s1)->pageIdx < ((ThumbnailItem*)s2)->pageIdx;
00066 }