improved click handling on thumbnails view

preferencesAboutTextFull
Clément Fauconnier 5 years ago
parent 9e4dfba31b
commit e60416f3fa
  1. 80
      src/gui/UBDocumentNavigator.cpp
  2. 3
      src/gui/UBDocumentNavigator.h
  3. 89
      src/gui/UBThumbnailWidget.cpp
  4. 40
      src/gui/UBThumbnailWidget.h

@ -59,6 +59,7 @@ UBDocumentNavigator::UBDocumentNavigator(QWidget *parent, const char *name):QGra
, mThumbnailWidth(0) , mThumbnailWidth(0)
, mThumbnailMinWidth(100) , mThumbnailMinWidth(100)
, mSelectedThumbnail(NULL) , mSelectedThumbnail(NULL)
, mLastClickedThumbnail(NULL)
, mDropSource(NULL) , mDropSource(NULL)
, mDropTarget(NULL) , mDropTarget(NULL)
, mDropBar(new QGraphicsRectItem()) , mDropBar(new QGraphicsRectItem())
@ -285,50 +286,65 @@ void UBDocumentNavigator::resizeEvent(QResizeEvent *event)
*/ */
void UBDocumentNavigator::mousePressEvent(QMouseEvent *event) void UBDocumentNavigator::mousePressEvent(QMouseEvent *event)
{ {
mLongPressTimer.start(); QGraphicsView::mousePressEvent(event);
mLastPressedMousePos = event->pos();
QGraphicsItem* pClickedItem = itemAt(event->pos()); if (!event->isAccepted())
if(NULL != pClickedItem)
{ {
mLongPressTimer.start();
mLastPressedMousePos = event->pos();
// First, select the clicked item mLastClickedThumbnail = clickedThumbnail(mLastPressedMousePos);
UBSceneThumbnailNavigPixmap* pCrntItem = dynamic_cast<UBSceneThumbnailNavigPixmap*>(pClickedItem);
if(NULL == pCrntItem) if(mLastClickedThumbnail)
{
int index = 0;
for(int i = 0; i < mThumbsWithLabels.size(); i++)
{
if (mThumbsWithLabels.at(i).getThumbnail() == mLastClickedThumbnail)
{
mSelectedThumbnail = mLastClickedThumbnail;
index = i;
break;
}
}
UBApplication::boardController->persistViewPositionOnCurrentScene();
UBApplication::boardController->persistCurrentScene();
UBApplication::boardController->setActiveDocumentScene(index);
UBApplication::boardController->centerOn(UBApplication::boardController->activeScene()->lastCenter());
}
}
}
UBSceneThumbnailNavigPixmap* UBDocumentNavigator::clickedThumbnail(const QPoint pos) const
{
UBSceneThumbnailNavigPixmap* clickedThumbnail = NULL;
QGraphicsItem* clickedItem = itemAt(pos);
if(clickedItem)
{
clickedThumbnail = dynamic_cast<UBSceneThumbnailNavigPixmap*>(clickedItem);
if(!clickedThumbnail)
{ {
// If we fall here we may have clicked on the label instead of the thumbnail // If we fall here we may have clicked on the label instead of the thumbnail
UBThumbnailTextItem* pTextItem = dynamic_cast<UBThumbnailTextItem*>(pClickedItem); UBThumbnailTextItem* clickedTextItem = dynamic_cast<UBThumbnailTextItem*>(clickedItem);
if(NULL != pTextItem) if(clickedTextItem)
{ {
for(int i = 0; i < mThumbsWithLabels.size(); i++) for(int i = 0; i < mThumbsWithLabels.size(); i++)
{ {
const UBImgTextThumbnailElement& el = mThumbsWithLabels.at(i); const UBImgTextThumbnailElement& el = mThumbsWithLabels.at(i);
if(el.getCaption() == pTextItem) if(el.getCaption() == clickedTextItem)
{ {
pCrntItem = el.getThumbnail(); clickedThumbnail = el.getThumbnail();
break; 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->persistViewPositionOnCurrentScene();
UBApplication::boardController->persistCurrentScene();
UBApplication::boardController->setActiveDocumentScene(index);
UBApplication::boardController->centerOn(UBApplication::boardController->activeScene()->lastCenter());
} }
QGraphicsView::mousePressEvent(event);
return clickedThumbnail;
} }
void UBDocumentNavigator::mouseReleaseEvent(QMouseEvent *event) void UBDocumentNavigator::mouseReleaseEvent(QMouseEvent *event)
@ -346,13 +362,13 @@ void UBDocumentNavigator::longPressTimeout()
void UBDocumentNavigator::mousePressAndHoldEvent(QPoint pos) void UBDocumentNavigator::mousePressAndHoldEvent(QPoint pos)
{ {
UBSceneThumbnailNavigPixmap* item = dynamic_cast<UBSceneThumbnailNavigPixmap*>(itemAt(pos)); // UBSceneThumbnailNavigPixmap* item = dynamic_cast<UBSceneThumbnailNavigPixmap*>(itemAt(pos));
if (item) if (mLastClickedThumbnail)
{ {
mDropSource = item; mDropSource = mLastClickedThumbnail;
mDropTarget = item; mDropTarget = mLastClickedThumbnail;
QPixmap pixmap = item->pixmap().scaledToWidth(mThumbnailWidth/2); QPixmap pixmap = mLastClickedThumbnail->pixmap().scaledToWidth(mThumbnailWidth/2);
QDrag *drag = new QDrag(this); QDrag *drag = new QDrag(this);
drag->setMimeData(new QMimeData()); drag->setMimeData(new QMimeData());

@ -53,6 +53,7 @@ public:
int nbColumns(); int nbColumns();
void setThumbnailMinWidth(int width); void setThumbnailMinWidth(int width);
int thumbnailMinWidth(); int thumbnailMinWidth();
UBSceneThumbnailNavigPixmap* clickedThumbnail(const QPoint pos) const;
public slots: public slots:
void onScrollToSelectedPage(int index);// { if (mCrntItem) centerOn(mCrntItem); } void onScrollToSelectedPage(int index);// { if (mCrntItem) centerOn(mCrntItem); }
@ -94,7 +95,7 @@ private:
int mThumbnailMinWidth; int mThumbnailMinWidth;
/** The selected thumbnail */ /** The selected thumbnail */
UBSceneThumbnailNavigPixmap* mSelectedThumbnail; UBSceneThumbnailNavigPixmap* mSelectedThumbnail;
UBSceneThumbnailNavigPixmap* mLastClickedThumbnail;
UBSceneThumbnailNavigPixmap* mDropSource; UBSceneThumbnailNavigPixmap* mDropSource;
UBSceneThumbnailNavigPixmap* mDropTarget; UBSceneThumbnailNavigPixmap* mDropTarget;
QGraphicsRectItem *mDropBar; QGraphicsRectItem *mDropBar;

@ -773,10 +773,7 @@ UBThumbnail::~UBThumbnail()
UBSceneThumbnailNavigPixmap::UBSceneThumbnailNavigPixmap(const QPixmap& pix, UBDocumentProxy* proxy, int pSceneIndex) UBSceneThumbnailNavigPixmap::UBSceneThumbnailNavigPixmap(const QPixmap& pix, UBDocumentProxy* proxy, int pSceneIndex)
: UBSceneThumbnailPixmap(pix, proxy, pSceneIndex) : UBSceneThumbnailPixmap(pix, proxy, pSceneIndex)
, bButtonsVisible(false) , mEditable(false)
, bCanDelete(false)
, bCanMoveUp(false)
, bCanMoveDown(false)
{ {
if(0 <= UBDocumentContainer::pageFromSceneIndex(pSceneIndex)){ if(0 <= UBDocumentContainer::pageFromSceneIndex(pSceneIndex)){
setAcceptHoverEvents(true); setAcceptHoverEvents(true);
@ -792,49 +789,30 @@ UBSceneThumbnailNavigPixmap::~UBSceneThumbnailNavigPixmap()
void UBSceneThumbnailNavigPixmap::hoverEnterEvent(QGraphicsSceneHoverEvent *event) void UBSceneThumbnailNavigPixmap::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{ {
event->accept(); event->accept();
bButtonsVisible = true; showUI();
bCanDelete = true;
bCanMoveDown = false;
bCanMoveUp = false;
if(sceneIndex() < proxy()->pageCount() - 1)
bCanMoveDown = true;
if(sceneIndex() > 0)
bCanMoveUp = true;
if(proxy()->pageCount() == 1)
bCanDelete = false;
update(); update();
} }
void UBSceneThumbnailNavigPixmap::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) void UBSceneThumbnailNavigPixmap::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{ {
event->accept(); event->accept();
bButtonsVisible = false; hideUI();
update(); update();
} }
void UBSceneThumbnailNavigPixmap::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) void UBSceneThumbnailNavigPixmap::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{ {
Q_UNUSED(option);
Q_UNUSED(widget);
UBSceneThumbnailPixmap::paint(painter, option, widget); UBSceneThumbnailPixmap::paint(painter, option, widget);
if(bButtonsVisible) using namespace UBThumbnailUI;
if (editable())
{ {
if(bCanDelete) if(deletable())
painter->drawPixmap(0, 0, BUTTONSIZE, BUTTONSIZE, QPixmap(":images/close.svg")); draw(painter, *getIcon("close"));
else else
painter->drawPixmap(0, 0, BUTTONSIZE, BUTTONSIZE, QPixmap(":images/closeDisabled.svg")); draw(painter, *getIcon("closeDisabled"));
painter->drawPixmap(BUTTONSIZE + BUTTONSPACING, 0, BUTTONSIZE, BUTTONSIZE, QPixmap(":images/duplicate.svg")); draw(painter, *getIcon("duplicate"));
// if(bCanMoveUp)
// painter->drawPixmap(2*(BUTTONSIZE + BUTTONSPACING), 0, BUTTONSIZE, BUTTONSIZE, QPixmap(":images/moveUp.svg"));
// else
// painter->drawPixmap(2*(BUTTONSIZE + BUTTONSPACING), 0, BUTTONSIZE, BUTTONSIZE, QPixmap(":images/moveUpDisabled.svg"));
// if(bCanMoveDown)
// painter->drawPixmap(3*(BUTTONSIZE + BUTTONSPACING), 0, BUTTONSIZE, BUTTONSIZE, QPixmap(":images/menu.svg"));
// else
// painter->drawPixmap(3*(BUTTONSIZE + BUTTONSPACING), 0, BUTTONSIZE, BUTTONSIZE, QPixmap(":images/menuDisabled.svg"));
} }
} }
@ -842,18 +820,41 @@ void UBSceneThumbnailNavigPixmap::mousePressEvent(QGraphicsSceneMouseEvent *even
{ {
QPointF p = event->pos(); QPointF p = event->pos();
// Here we check the position of the click and verify if it has to trig an action or not. using namespace UBThumbnailUI;
if(bCanDelete && p.x() >= 0 && p.x() <= BUTTONSIZE && p.y() >= 0 && p.y() <= BUTTONSIZE)
deletePage();
if(p.x() >= BUTTONSIZE + BUTTONSPACING && p.x() <= 2*BUTTONSIZE + BUTTONSPACING && p.y() >= 0 && p.y() <= BUTTONSIZE)
duplicatePage();
// if(bCanMoveUp && p.x() >= 2*(BUTTONSIZE + BUTTONSPACING) && p.x() <= 3*BUTTONSIZE + 2*BUTTONSPACING && p.y() >= 0 && p.y() <= BUTTONSIZE)
// moveUpPage();
// if(bCanMoveDown && p.x() >= 3*(BUTTONSIZE + BUTTONSPACING) && p.x() <= 4*BUTTONSIZE + 3*BUTTONSPACING && p.y() >= 0 && p.y() <= BUTTONSIZE)
// moveDownPage();
event->accept(); if (triggered(p.y()))
{
if(deletable() && getIcon("close")->triggered(p.x()))
{
event->accept();
deletePage();
}
else if(getIcon("duplicate")->triggered(p.x()))
{
event->accept();
duplicatePage();
}
else
{
event->ignore();
}
/*
else if(movableUp() && getIcon("moveUp")->triggered(p.x()))
{
event->accept();
moveUpPage();
}
else if (movableDown() && getIcon("moveDown")->triggered(p.x()))
{
event->accept();
moveDownPage();
}
*/
}
else
{
event->ignore();
}
} }
void UBSceneThumbnailNavigPixmap::deletePage() void UBSceneThumbnailNavigPixmap::deletePage()

@ -355,6 +355,41 @@ class UBSceneThumbnailNavigPixmap : public UBSceneThumbnailPixmap
UBSceneThumbnailNavigPixmap(const QPixmap& pix, UBDocumentProxy* proxy, int pSceneIndex); UBSceneThumbnailNavigPixmap(const QPixmap& pix, UBDocumentProxy* proxy, int pSceneIndex);
~UBSceneThumbnailNavigPixmap(); ~UBSceneThumbnailNavigPixmap();
bool editable()
{
return mEditable;
}
bool deletable()
{
return proxy()->pageCount() > 1;
}
bool movableUp()
{
return sceneIndex() > 0;
}
bool movableDown()
{
return sceneIndex() < (proxy()->pageCount() -1);
}
void showUI()
{
setEditable(true);
}
void hideUI()
{
setEditable(false);
}
void setEditable(bool editable)
{
mEditable = editable;
}
protected: protected:
void hoverEnterEvent(QGraphicsSceneHoverEvent *event); void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
@ -367,10 +402,7 @@ class UBSceneThumbnailNavigPixmap : public UBSceneThumbnailPixmap
void moveUpPage(); void moveUpPage();
void moveDownPage(); void moveDownPage();
bool bButtonsVisible; bool mEditable;
bool bCanDelete;
bool bCanMoveUp;
bool bCanMoveDown;
}; };
class UBImgTextThumbnailElement class UBImgTextThumbnailElement

Loading…
Cancel
Save