diff --git a/src/customWidgets/UBActionableWidget.cpp b/src/customWidgets/UBActionableWidget.cpp index 20ddaf69..65e440a3 100644 --- a/src/customWidgets/UBActionableWidget.cpp +++ b/src/customWidgets/UBActionableWidget.cpp @@ -8,6 +8,10 @@ UBActionableWidget::UBActionableWidget(QWidget *parent, const char *name):QWidge { setObjectName(name); mActions.clear(); + mCloseButtons.setIcon(QIcon(QPixmap(":images/close.svg"))); + mCloseButtons.setGeometry(0, 0, 2*ACTIONSIZE, ACTIONSIZE); + mCloseButtons.setVisible(false); + connect(&mCloseButtons, SIGNAL(clicked()), this, SLOT(onCloseClicked())); } UBActionableWidget::~UBActionableWidget() @@ -36,36 +40,26 @@ void UBActionableWidget::removeAllActions() void UBActionableWidget::setActionsVisible(bool bVisible) { - mShowActions = bVisible; + if(!mActions.empty() && mActions.contains(eAction_Close)){ + mCloseButtons.setVisible(bVisible); + } } -bool UBActionableWidget::shouldClose(QPoint p) +void UBActionableWidget::onCloseClicked() { - qDebug() << "Should close: " << p.x() << "," << p.y(); - bool close = false; + emit close(this); +} - if(mShowActions && - p.x() >= 0 && - p.x() <= ACTIONSIZE && - p.y() >= 0 && - p.y() <= ACTIONSIZE){ - close = true; +void UBActionableWidget::setActionsParent(QWidget *parent) +{ + if(mActions.contains(eAction_Close)){ + mCloseButtons.setParent(parent); } - - return close; } -void UBActionableWidget::paintEvent(QPaintEvent* ev) +void UBActionableWidget::unsetActionsParent() { - Q_UNUSED(ev); - if(mShowActions){ - QPainter p(this); - if(mActions.contains(eAction_Close)){ - p.drawPixmap(0, 0, 16, 16, QPixmap(":images/close.svg")); - }else if(mActions.contains(eAction_MoveUp)){ - // Implement me later - }else if(mActions.contains(eAction_MoveDown)){ - // Implement me later - } + if(mActions.contains(eAction_Close)){ + mCloseButtons.setParent(this); } } diff --git a/src/customWidgets/UBActionableWidget.h b/src/customWidgets/UBActionableWidget.h index 12b2a2fb..aac74b3f 100644 --- a/src/customWidgets/UBActionableWidget.h +++ b/src/customWidgets/UBActionableWidget.h @@ -3,6 +3,8 @@ #include #include +#include +#include #define ACTIONSIZE 16 @@ -24,12 +26,22 @@ public: void setActionsVisible(bool bVisible); bool shouldClose(QPoint p); +signals: + void close(QWidget* w); + protected: + void setActionsParent(QWidget* parent); + void unsetActionsParent(); void paintEvent(QPaintEvent* ev); QVector mActions; + QPushButton mCloseButtons; + +private slots: + void onCloseClicked(); private: bool mShowActions; + }; #endif // UBACTIONABLEWIDGET_H diff --git a/src/customWidgets/UBDraggableMedia.cpp b/src/customWidgets/UBDraggableMedia.cpp index 7b976707..4136f742 100644 --- a/src/customWidgets/UBDraggableMedia.cpp +++ b/src/customWidgets/UBDraggableMedia.cpp @@ -5,7 +5,7 @@ UBDraggableMedia::UBDraggableMedia(eMediaType type, QWidget *parent, const char *name):UBMediaWidget(type, parent, name) { - + removeAllActions(); } UBDraggableMedia::~UBDraggableMedia() diff --git a/src/customWidgets/UBMediaWidget.cpp b/src/customWidgets/UBMediaWidget.cpp index 8fb69cc3..4bef693b 100644 --- a/src/customWidgets/UBMediaWidget.cpp +++ b/src/customWidgets/UBMediaWidget.cpp @@ -67,6 +67,7 @@ UBMediaWidget::UBMediaWidget(eMediaType type, QWidget *parent, const char *name) */ UBMediaWidget::~UBMediaWidget() { + unsetActionsParent(); DELETEPTR(mpSlider); DELETEPTR(mpPauseButton); DELETEPTR(mpPlayStopButton); @@ -152,6 +153,7 @@ void UBMediaWidget::createMediaPlayer() } mLayout.addWidget(mpMediaContainer, 1); mLayout.addLayout(&mSeekerLayout, 0); + setActionsParent(mpMediaContainer); } /** diff --git a/src/customWidgets/UBMediaWidget.h b/src/customWidgets/UBMediaWidget.h index 01b0a0b6..dbc85b78 100644 --- a/src/customWidgets/UBMediaWidget.h +++ b/src/customWidgets/UBMediaWidget.h @@ -69,6 +69,8 @@ public: eMediaType mediaType(); int border(); void setAudioCover(const QString& coverPath); + void setUrl(const QString& url){mUrl = url;} + QString url(){return mUrl;} protected: void resizeEvent(QResizeEvent* ev); @@ -118,6 +120,8 @@ private: QHBoxLayout mMediaLayout; /** The audio cover */ QLabel* mpCover; + /** The media url */ + QString mUrl; }; #endif // UBMEDIAWIDGET_H diff --git a/src/customWidgets/UBWidgetList.cpp b/src/customWidgets/UBWidgetList.cpp index bc0d89d4..fd8ddb03 100644 --- a/src/customWidgets/UBWidgetList.cpp +++ b/src/customWidgets/UBWidgetList.cpp @@ -10,7 +10,7 @@ UBWidgetList::UBWidgetList(QWidget* parent, eWidgetListOrientation orientation, , mCanRemove(true) , mpLayout(NULL) , mpContainer(NULL) - , mMargin(5) + , mMargin(10) , mListElementsSpacing(10) , mpEmptyLabel(NULL) , mpCurrentWidget(NULL) @@ -69,6 +69,9 @@ void UBWidgetList::removeWidget(QWidget *widget) if(0 == mpLayout->count()){ mpEmptyLabel->setVisible(true); } + if(mpCurrentWidget == widget){ + mpCurrentWidget = NULL; + } } } @@ -141,13 +144,19 @@ void UBWidgetList::mousePressEvent(QMouseEvent *ev) QWidget* pWAt = widgetAt(ev->pos()); if(NULL != mpCurrentWidget){ if(pWAt == mpCurrentWidget){ - QPoint p; - p.setX(ev->x()); - p.setY(ev->y()); - if(mpCurrentWidget->shouldClose(p)){ - emit closeWidget(mpCurrentWidget); - return; - } + +// qDebug() << ev->x() << "," << ev->y(); +// qDebug() << "mpCurrentWidget->pos() = " << mpCurrentWidget->pos().x() << "," << mpCurrentWidget->pos().y(); +// qDebug() << "viewport position: " << visibleRegion().boundingRect().x() << "," << visibleRegion().boundingRect().y(); + +// QPoint p; +// p.setX(ev->x() - mpCurrentWidget->pos().x()); +// p.setY(ev->y() - mpCurrentWidget->pos().y()); + +// if(mpCurrentWidget->shouldClose(p)){ +// emit closeWidget(mpCurrentWidget); +// return; +// } }else{ mpCurrentWidget->setActionsVisible(false); diff --git a/src/gui/UBTBPageEditWidget.cpp b/src/gui/UBTBPageEditWidget.cpp index 7957d6ab..d840ad39 100644 --- a/src/gui/UBTBPageEditWidget.cpp +++ b/src/gui/UBTBPageEditWidget.cpp @@ -142,6 +142,7 @@ void UBTBPageEditWidget::onActionButton() UBTeacherStudentAction* pAction = new UBTeacherStudentAction(this); mActions << pAction; mpActions->addWidget(pAction); + connectActions(pAction); emit valueChanged(); } @@ -150,6 +151,7 @@ void UBTBPageEditWidget::onLinkButton() UBUrlWidget* pUrl = new UBUrlWidget(this); mUrls << pUrl; mpLinks->addWidget(pUrl); + connectActions(pUrl); emit valueChanged(); } @@ -163,11 +165,20 @@ void UBTBPageEditWidget::onMediaDropped(const QString &url) //mpDataMgr->medias()->append(pMedia); //mpDataMgr->addMediaUrl(url); mpMediaContainer->addWidget(pMedia); + connectActions(pMedia); emit valueChanged(); } } } +void UBTBPageEditWidget::connectActions(QWidget* w) +{ + UBActionableWidget* pActionable = dynamic_cast(w); + if(NULL != pActionable){ + connect(pActionable, SIGNAL(close(QWidget*)), this, SLOT(onCloseWidget(QWidget*))); + } +} + void UBTBPageEditWidget::onDocumentEditClicked() { emit changeTBState(eTeacherBarState_DocumentEdit); @@ -217,6 +228,7 @@ void UBTBPageEditWidget::updateFields() pAction->setText(action.content); mActions << pAction; mpActions->addWidget(pAction); + connectActions(pAction); } // Medias foreach(QString url, *mpDataMgr->mediaUrls()){ @@ -226,6 +238,7 @@ void UBTBPageEditWidget::updateFields() if(pWidget != NULL){ mMedias << pWidget; mpMediaContainer->addWidget(pWidget); + connectActions(pWidget); } } } @@ -237,6 +250,7 @@ void UBTBPageEditWidget::updateFields() urlWidget->setUrl(link.link); mUrls << urlWidget; mpLinks->addWidget(urlWidget); + connectActions(urlWidget); } // Comments mpComments->document()->setPlainText(mpDataMgr->comments()); @@ -287,7 +301,15 @@ void UBTBPageEditWidget::onCloseWidget(QWidget *w) mpLinks->removeWidget(pW); mUrls.remove(mUrls.indexOf(pW)); DELETEPTR(w); - }else if("UBTBMediaPicture" == w->objectName() || "UBMediaWidget" == w->objectName()){ + }else if("UBTBMediaPicture" == w->objectName()){ + UBPictureWidget* pW = dynamic_cast(w); + mMediaUrls.removeOne(pW->url()); + mpMediaContainer->removeWidget(w); + mMedias.remove(mMedias.indexOf(w)); + DELETEPTR(w); + }else if("UBMediaWidget" == w->objectName()){ + UBMediaWidget* pW = dynamic_cast(w); + mMediaUrls.removeOne(pW->url()); mpMediaContainer->removeWidget(w); mMedias.remove(mMedias.indexOf(w)); DELETEPTR(w); @@ -326,10 +348,12 @@ UBUrlWidget::UBUrlWidget(QWidget *parent, const char *name):UBActionableWidget(p mpLayout->addLayout(mpTitleLayout); mpLayout->addLayout(mpLabelLayout); + setActionsParent(this); } UBUrlWidget::~UBUrlWidget() { + unsetActionsParent(); DELETEPTR(mpTitle); DELETEPTR(mpTitleLabel); DELETEPTR(mpUrlLabel); @@ -460,6 +484,7 @@ QWidget* UBTBMediaContainer::generateMediaWidget(const QString& url) if(mimeType.contains("image")){ QPixmap pix = QPixmap(url); UBPictureWidget* pic = new UBPictureWidget(); + pic->setUrl(url); pix.scaledToWidth(pic->label()->width()); pic->label()->resize(pix.width(), pix.height()); pic->label()->setPixmap(pix); @@ -470,6 +495,7 @@ QWidget* UBTBMediaContainer::generateMediaWidget(const QString& url) else if(mimeType.contains("video") || mimeType.contains("audio")){ UBMediaWidget* mediaPlayer = new UBMediaWidget(mimeType.contains("audio")?eMediaType_Audio:eMediaType_Video); mediaPlayer->setFile(url); + mediaPlayer->setUrl(url); pW = mediaPlayer; } else{ @@ -511,11 +537,12 @@ UBTeacherStudentAction::UBTeacherStudentAction(QWidget *parent, const char *name mpText->setStyleSheet("background:white;"); mpLayout->addWidget(mpText, 1); - + setActionsParent(this); } UBTeacherStudentAction::~UBTeacherStudentAction() { + unsetActionsParent(); DELETEPTR(mpCombo); DELETEPTR(mpText); DELETEPTR(mpComboLayout); @@ -568,10 +595,12 @@ UBPictureWidget::UBPictureWidget(QWidget *parent, const char *name):UBActionable mpLabel = new QLabel(this); mpLayout->addWidget(mpLabel); mpLabel->setGeometry( 10, 10, width()-2*10, height()); + setActionsParent(mpLabel); } UBPictureWidget::~UBPictureWidget() { + unsetActionsParent(); DELETEPTR(mpLabel); DELETEPTR(mpLayout); } diff --git a/src/gui/UBTBPageEditWidget.h b/src/gui/UBTBPageEditWidget.h index 0521c847..64d28473 100644 --- a/src/gui/UBTBPageEditWidget.h +++ b/src/gui/UBTBPageEditWidget.h @@ -64,6 +64,8 @@ public: ~UBPictureWidget(); QLabel* label(){return mpLabel;} + void setUrl(const QString& url){mUrl = url;} + QString url(){return mUrl;} protected: void resizeEvent(QResizeEvent* ev); @@ -71,6 +73,7 @@ protected: private: QVBoxLayout* mpLayout; QLabel* mpLabel; + QString mUrl; }; class UBTBMediaContainer : public UBWidgetList @@ -124,6 +127,7 @@ private slots: void onCloseWidget(QWidget* w); private: + void connectActions(QWidget* w); QVBoxLayout mLayout; QHBoxLayout mTitleLayout; QVBoxLayout mContainerLayout;