parent
f1f96fd3ac
commit
7b1f64eabf
After Width: | Height: | Size: 518 B |
After Width: | Height: | Size: 1.8 KiB |
@ -0,0 +1,128 @@ |
|||||||
|
#include "UBDragableThumbnail.h" |
||||||
|
|
||||||
|
#include <QPainter> |
||||||
|
#include <QMimeData> |
||||||
|
#include <QDrag> |
||||||
|
|
||||||
|
UBDraggableThumbnail::UBDraggableThumbnail(QWidget* parent, const QPixmap& pixmap) : |
||||||
|
QFrame(parent) |
||||||
|
, mThumbnail(new QLabel(this)) |
||||||
|
, mHBoxLayout(new QHBoxLayout(this)) |
||||||
|
{
|
||||||
|
setFrameStyle(QFrame::Sunken | QFrame::StyledPanel); |
||||||
|
setAcceptDrops(true); |
||||||
|
|
||||||
|
//set stylesheet
|
||||||
|
setObjectName("DockPaletteWidgetBox"); |
||||||
|
setStyleSheet("background:white"); |
||||||
|
|
||||||
|
mHBoxLayout->setAlignment(Qt::AlignHCenter); |
||||||
|
|
||||||
|
setThumbnail(pixmap); |
||||||
|
|
||||||
|
setLayout(mHBoxLayout); |
||||||
|
} |
||||||
|
|
||||||
|
void UBDraggableThumbnail::setThumbnail(const QPixmap& pixmap) |
||||||
|
{ |
||||||
|
mThumbnail->setAttribute(Qt::WA_DeleteOnClose); |
||||||
|
setPixmap(pixmap); |
||||||
|
|
||||||
|
mHBoxLayout->addWidget(mThumbnail); |
||||||
|
} |
||||||
|
|
||||||
|
void UBDraggableThumbnail::setPixmap(const QPixmap& pixmap) |
||||||
|
{ |
||||||
|
mThumbnail->setPixmap(pixmap); |
||||||
|
} |
||||||
|
|
||||||
|
void UBDraggableThumbnail::dragEnterEvent(QDragEnterEvent *event) |
||||||
|
{ |
||||||
|
if (event->mimeData()->hasFormat("application/x-dnditemdata")) { |
||||||
|
if (event->source() == this) { |
||||||
|
event->setDropAction(Qt::MoveAction); |
||||||
|
event->accept(); |
||||||
|
} else { |
||||||
|
event->acceptProposedAction(); |
||||||
|
} |
||||||
|
} else { |
||||||
|
event->ignore(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UBDraggableThumbnail::dragMoveEvent(QDragMoveEvent *event) |
||||||
|
{ |
||||||
|
if (event->mimeData()->hasFormat("application/x-dnditemdata")) { |
||||||
|
if (event->source() == this) { |
||||||
|
event->setDropAction(Qt::MoveAction); |
||||||
|
event->accept(); |
||||||
|
} else { |
||||||
|
event->acceptProposedAction(); |
||||||
|
} |
||||||
|
} else { |
||||||
|
event->ignore(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UBDraggableThumbnail::dropEvent(QDropEvent *event) |
||||||
|
{ |
||||||
|
if (event->mimeData()->hasFormat("application/x-dnditemdata")) { |
||||||
|
QByteArray itemData = event->mimeData()->data("application/x-dnditemdata"); |
||||||
|
QDataStream dataStream(&itemData, QIODevice::ReadOnly); |
||||||
|
|
||||||
|
QPixmap pixmap; |
||||||
|
QPoint offset; |
||||||
|
dataStream >> pixmap >> offset; |
||||||
|
|
||||||
|
//don't have to delete previous Thumbnail (Drag'n'Drop behavior with WA_DeleteOnClose attribute will do it)
|
||||||
|
mThumbnail = new QLabel(this); |
||||||
|
setThumbnail(pixmap); |
||||||
|
|
||||||
|
if (event->source() == this) { |
||||||
|
event->setDropAction(Qt::MoveAction); |
||||||
|
event->accept(); |
||||||
|
} else { |
||||||
|
event->acceptProposedAction(); |
||||||
|
} |
||||||
|
} else { |
||||||
|
event->ignore(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
void UBDraggableThumbnail::mousePressEvent(QMouseEvent *event) |
||||||
|
{ |
||||||
|
QLabel *child = static_cast<QLabel*>(childAt(event->pos())); |
||||||
|
if (!child) |
||||||
|
return; |
||||||
|
|
||||||
|
QPixmap pixmap = *child->pixmap(); |
||||||
|
|
||||||
|
QByteArray itemData; |
||||||
|
QDataStream dataStream(&itemData, QIODevice::WriteOnly); |
||||||
|
dataStream << pixmap << QPoint(event->pos() - child->pos()); |
||||||
|
|
||||||
|
QMimeData *mimeData = new QMimeData; |
||||||
|
mimeData->setData("application/x-dnditemdata", itemData); |
||||||
|
|
||||||
|
QDrag *drag = new QDrag(this); |
||||||
|
drag->setMimeData(mimeData); |
||||||
|
drag->setPixmap(pixmap); |
||||||
|
drag->setHotSpot(event->pos() - child->pos()); |
||||||
|
|
||||||
|
QPixmap tempPixmap = pixmap; |
||||||
|
QPainter painter; |
||||||
|
painter.begin(&tempPixmap); |
||||||
|
painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127)); |
||||||
|
painter.end(); |
||||||
|
|
||||||
|
child->setPixmap(tempPixmap); |
||||||
|
|
||||||
|
if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction) |
||||||
|
child->close(); |
||||||
|
else { |
||||||
|
child->show(); |
||||||
|
child->setPixmap(pixmap); |
||||||
|
} |
||||||
|
} |
||||||
|
|
@ -0,0 +1,31 @@ |
|||||||
|
#ifndef UBDRAGABLELABEL_H |
||||||
|
#define UBDRAGABLELABEL_H |
||||||
|
|
||||||
|
#include <QFrame> |
||||||
|
#include <QLabel> |
||||||
|
#include <QDragEnterEvent> |
||||||
|
#include <QDragMoveEvent> |
||||||
|
#include <QDropEvent> |
||||||
|
#include <QHBoxLayout> |
||||||
|
#include <QPixmap> |
||||||
|
|
||||||
|
class UBDraggableThumbnail : public QFrame |
||||||
|
{ |
||||||
|
public: |
||||||
|
UBDraggableThumbnail(QWidget* parent =0, const QPixmap& pixmap = QPixmap(":images/libpalette/notFound.png")); |
||||||
|
|
||||||
|
void setThumbnail(const QPixmap &pixmap); |
||||||
|
void setPixmap(const QPixmap & pixmap); |
||||||
|
|
||||||
|
protected: |
||||||
|
void dragEnterEvent(QDragEnterEvent *event); |
||||||
|
void dragMoveEvent(QDragMoveEvent *event); |
||||||
|
void dropEvent(QDropEvent *event); |
||||||
|
void mousePressEvent(QMouseEvent *event); |
||||||
|
|
||||||
|
private: |
||||||
|
QLabel* mThumbnail; |
||||||
|
QHBoxLayout* mHBoxLayout; |
||||||
|
}; |
||||||
|
|
||||||
|
#endif // UBDRAGABLELABEL_H
|
@ -0,0 +1,301 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (C) 2015-2016 Département de l'Instruction Publique (DIP-SEM) |
||||||
|
* |
||||||
|
* Copyright (C) 2013 Open Education Foundation |
||||||
|
* |
||||||
|
* Copyright (C) 2010-2013 Groupement d'Intérêt Public pour |
||||||
|
* l'Education Numérique en Afrique (GIP ENA) |
||||||
|
* |
||||||
|
* This file is part of OpenBoard. |
||||||
|
* |
||||||
|
* OpenBoard 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, version 3 of the License, |
||||||
|
* with a specific linking exception for the OpenSSL project's |
||||||
|
* "OpenSSL" library (or with modified versions of it that use the |
||||||
|
* same license as the "OpenSSL" library). |
||||||
|
* |
||||||
|
* OpenBoard 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 OpenBoard. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/ |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#include <QList> |
||||||
|
#include <QPointF> |
||||||
|
#include <QPixmap> |
||||||
|
#include <QTransform> |
||||||
|
#include <QScrollBar> |
||||||
|
#include <QFontMetrics> |
||||||
|
#include <QGraphicsItem> |
||||||
|
#include <QGraphicsPixmapItem> |
||||||
|
|
||||||
|
#include "core/UBApplication.h" |
||||||
|
#include "UBBoardThumbnailsView.h" |
||||||
|
#include "board/UBBoardController.h" |
||||||
|
#include "adaptors/UBThumbnailAdaptor.h" |
||||||
|
#include "adaptors/UBSvgSubsetAdaptor.h" |
||||||
|
#include "document/UBDocumentController.h" |
||||||
|
#include "domain/UBGraphicsScene.h" |
||||||
|
#include "board/UBBoardPaletteManager.h" |
||||||
|
#include "core/UBApplicationController.h" |
||||||
|
#include "core/UBPersistenceManager.h" |
||||||
|
#include "UBThumbnailView.h" |
||||||
|
|
||||||
|
UBBoardThumbnailsView::UBBoardThumbnailsView(QWidget *parent, const char *name) |
||||||
|
: QGraphicsView(parent) |
||||||
|
, mThumbnailWidth(0) |
||||||
|
, mThumbnailMinWidth(100) |
||||||
|
, mMargin(20) |
||||||
|
, mDropSource(NULL) |
||||||
|
, mDropTarget(NULL) |
||||||
|
, mDropBar(new QGraphicsRectItem(0)) |
||||||
|
, mLongPressInterval(150) |
||||||
|
{ |
||||||
|
setScene(new QGraphicsScene(this));
|
||||||
|
|
||||||
|
mDropBar->setPen(QPen(Qt::darkGray)); |
||||||
|
mDropBar->setBrush(QBrush(Qt::lightGray)); |
||||||
|
scene()->addItem(mDropBar); |
||||||
|
mDropBar->hide(); |
||||||
|
|
||||||
|
setObjectName(name); |
||||||
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
||||||
|
setFrameShadow(QFrame::Plain); |
||||||
|
|
||||||
|
mThumbnailWidth = width() - 2*mMargin; |
||||||
|
|
||||||
|
mLongPressTimer.setInterval(mLongPressInterval); |
||||||
|
mLongPressTimer.setSingleShot(true); |
||||||
|
|
||||||
|
connect(UBApplication::boardController, SIGNAL(initThumbnailsRequired(UBDocumentContainer*)), this, SLOT(initThumbnails(UBDocumentContainer*)), Qt::UniqueConnection); |
||||||
|
connect(UBApplication::boardController, SIGNAL(addThumbnailRequired(UBDocumentContainer*, int)), this, SLOT(addThumbnail(UBDocumentContainer*, int)), Qt::UniqueConnection); |
||||||
|
connect(UBApplication::boardController, SIGNAL(moveThumbnailRequired(int, int)), this, SLOT(moveThumbnail(int, int)), Qt::UniqueConnection); |
||||||
|
connect(this, SIGNAL(moveThumbnailRequired(int, int)), this, SLOT(moveThumbnail(int, int)), Qt::UniqueConnection); |
||||||
|
connect(UBApplication::boardController, SIGNAL(removeThumbnailRequired(int)), this, SLOT(removeThumbnail(int)), Qt::UniqueConnection); |
||||||
|
|
||||||
|
connect(&mLongPressTimer, SIGNAL(timeout()), this, SLOT(longPressTimeout()), Qt::UniqueConnection); |
||||||
|
|
||||||
|
connect(this, SIGNAL(mousePressAndHoldEventRequired(QPoint)), this, SLOT(mousePressAndHoldEvent(QPoint)), Qt::UniqueConnection); |
||||||
|
|
||||||
|
connect(UBApplication::boardController, SIGNAL(pageSelectionChanged(int)), this, SLOT(scrollToSelectedPage(int)), Qt::UniqueConnection); |
||||||
|
} |
||||||
|
|
||||||
|
void UBBoardThumbnailsView::moveThumbnail(int from, int to) |
||||||
|
{ |
||||||
|
mThumbnails.move(from, to); |
||||||
|
|
||||||
|
updateThumbnailsPos(); |
||||||
|
} |
||||||
|
|
||||||
|
void UBBoardThumbnailsView::removeThumbnail(int i) |
||||||
|
{ |
||||||
|
UBDraggableThumbnailView* item = mThumbnails.at(i); |
||||||
|
|
||||||
|
scene()->removeItem(item->pageNumber()); |
||||||
|
scene()->removeItem(item); |
||||||
|
|
||||||
|
mThumbnails.removeAt(i); |
||||||
|
|
||||||
|
updateThumbnailsPos(); |
||||||
|
} |
||||||
|
|
||||||
|
UBDraggableThumbnailView* UBBoardThumbnailsView::createThumbnail(UBDocumentContainer* source, int i) |
||||||
|
{ |
||||||
|
UBGraphicsScene* pageScene = UBPersistenceManager::persistenceManager()->loadDocumentScene(source->selectedDocument(), i); |
||||||
|
UBThumbnailView* pageView = new UBThumbnailView(pageScene); |
||||||
|
|
||||||
|
return new UBDraggableThumbnailView(pageView, source->selectedDocument(), i); |
||||||
|
} |
||||||
|
|
||||||
|
void UBBoardThumbnailsView::addThumbnail(UBDocumentContainer* source, int i) |
||||||
|
{ |
||||||
|
UBDraggableThumbnailView* item = createThumbnail(source, i); |
||||||
|
mThumbnails.insert(i, item); |
||||||
|
|
||||||
|
scene()->addItem(item); |
||||||
|
scene()->addItem(item->pageNumber()); |
||||||
|
|
||||||
|
updateThumbnailsPos(); |
||||||
|
} |
||||||
|
|
||||||
|
void UBBoardThumbnailsView::clearThumbnails() |
||||||
|
{ |
||||||
|
qDeleteAll(mThumbnails); |
||||||
|
mThumbnails.clear(); |
||||||
|
} |
||||||
|
|
||||||
|
void UBBoardThumbnailsView::initThumbnails(UBDocumentContainer* source) |
||||||
|
{ |
||||||
|
clearThumbnails(); |
||||||
|
|
||||||
|
for(int i = 0; i < source->selectedDocument()->pageCount(); i++) |
||||||
|
{ |
||||||
|
mThumbnails.append(createThumbnail(source, i)); |
||||||
|
|
||||||
|
scene()->addItem(mThumbnails.last()); |
||||||
|
scene()->addItem(mThumbnails.last()->pageNumber()); |
||||||
|
} |
||||||
|
|
||||||
|
updateThumbnailsPos(); |
||||||
|
} |
||||||
|
|
||||||
|
void UBBoardThumbnailsView::scrollToSelectedPage(int index) |
||||||
|
{ |
||||||
|
centerOn(mThumbnails.at(index)); |
||||||
|
} |
||||||
|
|
||||||
|
void UBBoardThumbnailsView::updateThumbnailsPos() |
||||||
|
{
|
||||||
|
qreal thumbnailHeight = mThumbnailWidth / UBSettings::minScreenRatio; |
||||||
|
|
||||||
|
for (int i=0; i < mThumbnails.length(); i++) |
||||||
|
{ |
||||||
|
mThumbnails.at(i)->setSceneIndex(i); |
||||||
|
mThumbnails.at(i)->setPageNumber(i); |
||||||
|
mThumbnails.at(i)->updatePos(mThumbnailWidth, thumbnailHeight); |
||||||
|
} |
||||||
|
|
||||||
|
scene()->setSceneRect(scene()->itemsBoundingRect()); |
||||||
|
} |
||||||
|
|
||||||
|
void UBBoardThumbnailsView::resizeEvent(QResizeEvent *event) |
||||||
|
{ |
||||||
|
Q_UNUSED(event); |
||||||
|
|
||||||
|
// Update the thumbnails width
|
||||||
|
mThumbnailWidth = (width() > mThumbnailMinWidth) ? width() - 2*mMargin : mThumbnailMinWidth; |
||||||
|
|
||||||
|
// Refresh the scene
|
||||||
|
updateThumbnailsPos(); |
||||||
|
} |
||||||
|
|
||||||
|
void UBBoardThumbnailsView::mousePressEvent(QMouseEvent *event) |
||||||
|
{
|
||||||
|
mLongPressTimer.start(); |
||||||
|
mLastPressedMousePos = event->pos(); |
||||||
|
QGraphicsView::mousePressEvent(event); |
||||||
|
} |
||||||
|
|
||||||
|
void UBBoardThumbnailsView::mouseMoveEvent(QMouseEvent *event) |
||||||
|
{ |
||||||
|
mLastPressedMousePos = event->pos(); |
||||||
|
QGraphicsView::mouseMoveEvent(event); |
||||||
|
} |
||||||
|
|
||||||
|
void UBBoardThumbnailsView::longPressTimeout() |
||||||
|
{ |
||||||
|
emit mousePressAndHoldEventRequired(mLastPressedMousePos); |
||||||
|
mLongPressTimer.stop(); |
||||||
|
} |
||||||
|
|
||||||
|
void UBBoardThumbnailsView::mousePressAndHoldEvent(QPoint pos) |
||||||
|
{ |
||||||
|
UBDraggableThumbnailView* item = dynamic_cast<UBDraggableThumbnailView*>(itemAt(pos)); |
||||||
|
if (item) |
||||||
|
{ |
||||||
|
mDropSource = item; |
||||||
|
mDropTarget = item; |
||||||
|
|
||||||
|
QPixmap pixmap = item->widget()->grab().scaledToWidth(UBSettings::defaultThumbnailWidth); |
||||||
|
|
||||||
|
QDrag *drag = new QDrag(this); |
||||||
|
drag->setMimeData(new QMimeData()); |
||||||
|
drag->setPixmap(pixmap); |
||||||
|
drag->setHotSpot(QPoint(pixmap.width()/2, pixmap.height()/2)); |
||||||
|
|
||||||
|
drag->exec(); |
||||||
|
}
|
||||||
|
} |
||||||
|
|
||||||
|
void UBBoardThumbnailsView::mouseReleaseEvent(QMouseEvent *event) |
||||||
|
{ |
||||||
|
mLongPressTimer.stop(); |
||||||
|
|
||||||
|
UBDraggableThumbnailView* item = dynamic_cast<UBDraggableThumbnailView*>(itemAt(event->pos())); |
||||||
|
|
||||||
|
if (item) |
||||||
|
UBApplication::boardController->setActiveDocumentScene(item->sceneIndex()); |
||||||
|
|
||||||
|
QGraphicsView::mouseReleaseEvent(event); |
||||||
|
} |
||||||
|
|
||||||
|
void UBBoardThumbnailsView::dragEnterEvent(QDragEnterEvent *event) |
||||||
|
{ |
||||||
|
mDropBar->show(); |
||||||
|
|
||||||
|
if (event->source() == this) |
||||||
|
{ |
||||||
|
event->setDropAction(Qt::MoveAction); |
||||||
|
event->accept(); |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
event->acceptProposedAction(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UBBoardThumbnailsView::dragMoveEvent(QDragMoveEvent *event) |
||||||
|
{
|
||||||
|
QPointF position = event->pos(); |
||||||
|
|
||||||
|
//autoscroll during drag'n'drop
|
||||||
|
QPointF scenePos = mapToScene(position.toPoint()); |
||||||
|
int thumbnailHeight = mThumbnailWidth / UBSettings::minScreenRatio; |
||||||
|
QRectF thumbnailArea(0, scenePos.y() - thumbnailHeight/2, mThumbnailWidth, thumbnailHeight); |
||||||
|
|
||||||
|
ensureVisible(thumbnailArea); |
||||||
|
|
||||||
|
UBDraggableThumbnailView* item = dynamic_cast<UBDraggableThumbnailView*>(itemAt(position.toPoint())); |
||||||
|
if (item) |
||||||
|
{ |
||||||
|
if (item != mDropTarget) |
||||||
|
mDropTarget = item; |
||||||
|
|
||||||
|
qreal scale = item->transform().m11(); |
||||||
|
|
||||||
|
QPointF itemCenter(item->pos().x() + item->boundingRect().width() * scale / 2, |
||||||
|
item->pos().y() + item->boundingRect().height() * scale / 2); |
||||||
|
|
||||||
|
bool dropAbove = mapToScene(position.toPoint()).y() < itemCenter.y(); |
||||||
|
bool movingUp = mDropSource->sceneIndex() > item->sceneIndex(); |
||||||
|
qreal y = 0; |
||||||
|
|
||||||
|
if (movingUp) |
||||||
|
{ |
||||||
|
if (dropAbove) |
||||||
|
{ |
||||||
|
y = item->pos().y() - UBSettings::thumbnailSpacing / 2; |
||||||
|
if (mDropBar->y() != y) |
||||||
|
mDropBar->setRect(QRectF(item->pos().x(), y, item->boundingRect().width() * scale, 3)); |
||||||
|
} |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
if (!dropAbove) |
||||||
|
{ |
||||||
|
y = item->pos().y() + item->boundingRect().height() * scale + UBSettings::thumbnailSpacing / 2; |
||||||
|
if (mDropBar->y() != y) |
||||||
|
mDropBar->setRect(QRectF(item->pos().x(), y, item->boundingRect().width() * scale, 3)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
event->acceptProposedAction(); |
||||||
|
} |
||||||
|
|
||||||
|
void UBBoardThumbnailsView::dropEvent(QDropEvent *event) |
||||||
|
{ |
||||||
|
Q_UNUSED(event); |
||||||
|
|
||||||
|
UBApplication::boardController->moveSceneToIndex(mDropSource->sceneIndex(), mDropTarget->sceneIndex()); |
||||||
|
|
||||||
|
mDropSource = NULL; |
||||||
|
mDropTarget = NULL; |
||||||
|
mDropBar->hide(); |
||||||
|
} |
@ -1,311 +0,0 @@ |
|||||||
/*
|
|
||||||
* Copyright (C) 2015-2016 Département de l'Instruction Publique (DIP-SEM) |
|
||||||
* |
|
||||||
* Copyright (C) 2013 Open Education Foundation |
|
||||||
* |
|
||||||
* Copyright (C) 2010-2013 Groupement d'Intérêt Public pour |
|
||||||
* l'Education Numérique en Afrique (GIP ENA) |
|
||||||
* |
|
||||||
* This file is part of OpenBoard. |
|
||||||
* |
|
||||||
* OpenBoard 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, version 3 of the License, |
|
||||||
* with a specific linking exception for the OpenSSL project's |
|
||||||
* "OpenSSL" library (or with modified versions of it that use the |
|
||||||
* same license as the "OpenSSL" library). |
|
||||||
* |
|
||||||
* OpenBoard 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 OpenBoard. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/ |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#include <QList> |
|
||||||
#include <QPointF> |
|
||||||
#include <QPixmap> |
|
||||||
#include <QTransform> |
|
||||||
#include <QScrollBar> |
|
||||||
#include <QFontMetrics> |
|
||||||
#include <QGraphicsItem> |
|
||||||
#include <QGraphicsPixmapItem> |
|
||||||
|
|
||||||
#include "core/UBApplication.h" |
|
||||||
#include "UBDocumentNavigator.h" |
|
||||||
#include "board/UBBoardController.h" |
|
||||||
#include "adaptors/UBThumbnailAdaptor.h" |
|
||||||
#include "adaptors/UBSvgSubsetAdaptor.h" |
|
||||||
#include "document/UBDocumentController.h" |
|
||||||
#include "domain/UBGraphicsScene.h" |
|
||||||
#include "board/UBBoardPaletteManager.h" |
|
||||||
#include "core/UBApplicationController.h" |
|
||||||
|
|
||||||
#include "core/memcheck.h" |
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Constructor |
|
||||||
* @param parent as the parent widget |
|
||||||
* @param name as the object name |
|
||||||
*/ |
|
||||||
UBDocumentNavigator::UBDocumentNavigator(QWidget *parent, const char *name):QGraphicsView(parent) |
|
||||||
, mScene(NULL) |
|
||||||
, mNbColumns(1) |
|
||||||
, mThumbnailWidth(0) |
|
||||||
, mThumbnailMinWidth(100) |
|
||||||
, mSelectedThumbnail(NULL) |
|
||||||
{ |
|
||||||
setObjectName(name); |
|
||||||
mScene = new QGraphicsScene(this); |
|
||||||
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
||||||
setScene(mScene); |
|
||||||
mThumbnailWidth = width() - 2*border(); |
|
||||||
|
|
||||||
setFrameShadow(QFrame::Plain); |
|
||||||
|
|
||||||
connect(UBApplication::boardController, SIGNAL(documentThumbnailsUpdated(UBDocumentContainer*)), this, SLOT(generateThumbnails(UBDocumentContainer*))); |
|
||||||
connect(UBApplication::boardController, SIGNAL(documentPageUpdated(int)), this, SLOT(updateSpecificThumbnail(int))); |
|
||||||
connect(UBApplication::boardController, SIGNAL(pageSelectionChanged(int)), this, SLOT(onScrollToSelectedPage(int))); |
|
||||||
} |
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Destructor |
|
||||||
*/ |
|
||||||
UBDocumentNavigator::~UBDocumentNavigator() |
|
||||||
{ |
|
||||||
if(NULL != mScene) |
|
||||||
{ |
|
||||||
delete mScene; |
|
||||||
mScene = NULL; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Generate the thumbnails |
|
||||||
*/ |
|
||||||
void UBDocumentNavigator::generateThumbnails(UBDocumentContainer* source) |
|
||||||
{ |
|
||||||
mThumbsWithLabels.clear(); |
|
||||||
int selectedIndex = -1; |
|
||||||
QList<QGraphicsItem*> graphicsItemList = mScene->items(); |
|
||||||
for(int i = 0; i < graphicsItemList.size(); i+=1) |
|
||||||
{ |
|
||||||
QGraphicsItem* item = graphicsItemList.at(i); |
|
||||||
if(item->isSelected()) |
|
||||||
selectedIndex = i; |
|
||||||
mScene->removeItem(item); |
|
||||||
delete item; |
|
||||||
item = NULL; |
|
||||||
} |
|
||||||
|
|
||||||
for(int i = 0; i < source->selectedDocument()->pageCount(); i++) |
|
||||||
{ |
|
||||||
//claudio This is a very bad hack and shows a architectural problem
|
|
||||||
// source->selectedDocument()->pageCount() != source->pageCount()
|
|
||||||
if(i>=source->pageCount() || source->pageAt(i)->isNull()) |
|
||||||
source->insertThumbPage(i); |
|
||||||
|
|
||||||
const QPixmap* pix = source->pageAt(i); |
|
||||||
Q_ASSERT(!pix->isNull()); |
|
||||||
int pageIndex = UBDocumentContainer::pageFromSceneIndex(i); |
|
||||||
|
|
||||||
UBSceneThumbnailNavigPixmap* pixmapItem = new UBSceneThumbnailNavigPixmap(*pix, source->selectedDocument(), i); |
|
||||||
|
|
||||||
QString label = tr("Page %0").arg(pageIndex); |
|
||||||
UBThumbnailTextItem *labelItem = new UBThumbnailTextItem(label); |
|
||||||
|
|
||||||
UBImgTextThumbnailElement thumbWithText(pixmapItem, labelItem); |
|
||||||
thumbWithText.setBorder(border()); |
|
||||||
mThumbsWithLabels.append(thumbWithText); |
|
||||||
|
|
||||||
mScene->addItem(pixmapItem); |
|
||||||
mScene->addItem(labelItem); |
|
||||||
} |
|
||||||
|
|
||||||
if (selectedIndex >= 0 && selectedIndex < mThumbsWithLabels.count()) |
|
||||||
mSelectedThumbnail = mThumbsWithLabels.at(selectedIndex).getThumbnail(); |
|
||||||
else |
|
||||||
mSelectedThumbnail = NULL; |
|
||||||
|
|
||||||
// Draw the items
|
|
||||||
refreshScene(); |
|
||||||
} |
|
||||||
|
|
||||||
void UBDocumentNavigator::onScrollToSelectedPage(int index) |
|
||||||
{ |
|
||||||
int c = 0; |
|
||||||
foreach(UBImgTextThumbnailElement el, mThumbsWithLabels) |
|
||||||
{ |
|
||||||
if (c==index) |
|
||||||
{ |
|
||||||
el.getThumbnail()->setSelected(true); |
|
||||||
mSelectedThumbnail = el.getThumbnail(); |
|
||||||
} |
|
||||||
else |
|
||||||
{ |
|
||||||
el.getThumbnail()->setSelected(false); |
|
||||||
} |
|
||||||
c++; |
|
||||||
} |
|
||||||
if(NULL != mSelectedThumbnail) |
|
||||||
centerOn(mSelectedThumbnail); |
|
||||||
} |
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Refresh the given thumbnail |
|
||||||
* @param iPage as the given page related thumbnail |
|
||||||
*/ |
|
||||||
void UBDocumentNavigator::updateSpecificThumbnail(int iPage) |
|
||||||
{ |
|
||||||
const QPixmap* pix = UBApplication::boardController->pageAt(iPage); |
|
||||||
UBSceneThumbnailNavigPixmap* newItem = new UBSceneThumbnailNavigPixmap(*pix, UBApplication::boardController->selectedDocument(), iPage); |
|
||||||
|
|
||||||
// Get the old thumbnail
|
|
||||||
UBSceneThumbnailNavigPixmap* oldItem = mThumbsWithLabels.at(iPage).getThumbnail(); |
|
||||||
if(NULL != oldItem) |
|
||||||
{ |
|
||||||
mScene->removeItem(oldItem); |
|
||||||
mScene->addItem(newItem); |
|
||||||
mThumbsWithLabels[iPage].setThumbnail(newItem); |
|
||||||
delete oldItem; |
|
||||||
oldItem = NULL; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Put the element in the right place in the scene. |
|
||||||
*/ |
|
||||||
void UBDocumentNavigator::refreshScene() |
|
||||||
{ |
|
||||||
qreal thumbnailHeight = mThumbnailWidth / UBSettings::minScreenRatio; |
|
||||||
|
|
||||||
for(int i = 0; i < mThumbsWithLabels.size(); i++) |
|
||||||
{ |
|
||||||
// Get the item
|
|
||||||
UBImgTextThumbnailElement& item = mThumbsWithLabels[i]; |
|
||||||
int columnIndex = i % mNbColumns; |
|
||||||
int rowIndex = i / mNbColumns; |
|
||||||
item.Place(rowIndex, columnIndex, mThumbnailWidth, thumbnailHeight); |
|
||||||
} |
|
||||||
scene()->setSceneRect(scene()->itemsBoundingRect()); |
|
||||||
} |
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Set the number of thumbnails columns |
|
||||||
* @param nbColumns as the number of columns |
|
||||||
*/ |
|
||||||
void UBDocumentNavigator::setNbColumns(int nbColumns) |
|
||||||
{ |
|
||||||
mNbColumns = nbColumns; |
|
||||||
} |
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Get the number of columns |
|
||||||
* @return the number of thumbnails columns |
|
||||||
*/ |
|
||||||
int UBDocumentNavigator::nbColumns() |
|
||||||
{ |
|
||||||
return mNbColumns; |
|
||||||
} |
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Set the thumbnails minimum width |
|
||||||
* @param width as the minimum width |
|
||||||
*/ |
|
||||||
void UBDocumentNavigator::setThumbnailMinWidth(int width) |
|
||||||
{ |
|
||||||
mThumbnailMinWidth = width; |
|
||||||
} |
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Get the thumbnails minimum width |
|
||||||
* @return the minimum thumbnails width |
|
||||||
*/ |
|
||||||
int UBDocumentNavigator::thumbnailMinWidth() |
|
||||||
{ |
|
||||||
return mThumbnailMinWidth; |
|
||||||
} |
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Get the border size |
|
||||||
* @return the border size in pixels |
|
||||||
*/ |
|
||||||
int UBDocumentNavigator::border() |
|
||||||
{ |
|
||||||
return 20; |
|
||||||
} |
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Handle the resize event |
|
||||||
* @param event as the resize event |
|
||||||
*/ |
|
||||||
void UBDocumentNavigator::resizeEvent(QResizeEvent *event) |
|
||||||
{ |
|
||||||
Q_UNUSED(event); |
|
||||||
|
|
||||||
// Update the thumbnails width
|
|
||||||
mThumbnailWidth = (width() > mThumbnailMinWidth) ? width() - 2*border() : mThumbnailMinWidth; |
|
||||||
|
|
||||||
if(mSelectedThumbnail) |
|
||||||
centerOn(mSelectedThumbnail); |
|
||||||
|
|
||||||
// Refresh the scene
|
|
||||||
refreshScene(); |
|
||||||
} |
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Handle the mouse press event |
|
||||||
* @param event as the mouse event |
|
||||||
*/ |
|
||||||
void UBDocumentNavigator::mousePressEvent(QMouseEvent *event) |
|
||||||
{ |
|
||||||
QGraphicsItem* pClickedItem = itemAt(event->pos()); |
|
||||||
if(NULL != pClickedItem) |
|
||||||
{ |
|
||||||
|
|
||||||
// First, select the clicked item
|
|
||||||
UBSceneThumbnailNavigPixmap* pCrntItem = dynamic_cast<UBSceneThumbnailNavigPixmap*>(pClickedItem); |
|
||||||
|
|
||||||
if(NULL == pCrntItem) |
|
||||||
{ |
|
||||||
// If we fall here we may have clicked on the label instead of the thumbnail
|
|
||||||
UBThumbnailTextItem* pTextItem = dynamic_cast<UBThumbnailTextItem*>(pClickedItem); |
|
||||||
if(NULL != pTextItem) |
|
||||||
{ |
|
||||||
for(int i = 0; i < mThumbsWithLabels.size(); i++) |
|
||||||
{ |
|
||||||
const UBImgTextThumbnailElement& el = mThumbsWithLabels.at(i); |
|
||||||
if(el.getCaption() == pTextItem) |
|
||||||
{ |
|
||||||
pCrntItem = el.getThumbnail(); |
|
||||||
break; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
int index = 0; |
|
||||||
for(int i = 0; i < mThumbsWithLabels.size(); i++) |
|
||||||
{ |
|
||||||
if (mThumbsWithLabels.at(i).getThumbnail() == pCrntItem) |
|
||||||
{ |
|
||||||
mSelectedThumbnail = pCrntItem; |
|
||||||
index = i; |
|
||||||
break; |
|
||||||
} |
|
||||||
} |
|
||||||
UBApplication::boardController->setActiveDocumentScene(index); |
|
||||||
} |
|
||||||
QGraphicsView::mousePressEvent(event); |
|
||||||
} |
|
||||||
|
|
||||||
void UBDocumentNavigator::mouseReleaseEvent(QMouseEvent *event) |
|
||||||
{ |
|
||||||
event->accept(); |
|
||||||
} |
|
Loading…
Reference in new issue