diff --git a/src/customWidgets/UBActionableWidget.cpp b/src/customWidgets/UBActionableWidget.cpp new file mode 100644 index 00000000..47af2621 --- /dev/null +++ b/src/customWidgets/UBActionableWidget.cpp @@ -0,0 +1,70 @@ +#include +#include + +#include "UBActionableWidget.h" + +UBActionableWidget::UBActionableWidget(QWidget *parent, const char *name):QWidget(parent) + , mShowActions(false) +{ + setObjectName(name); + mActions.clear(); +} + +UBActionableWidget::~UBActionableWidget() +{ + +} + +void UBActionableWidget::addAction(eAction act) +{ + if(!mActions.contains(act)){ + mActions << act; + } +} + +void UBActionableWidget::removeAction(eAction act) +{ + if(mActions.contains(act)){ + mActions.remove(mActions.indexOf(act)); + } +} + +void UBActionableWidget::removeAllActions() +{ + mActions.clear(); +} + +void UBActionableWidget::setActionsVisible(bool bVisible) +{ + mShowActions = bVisible; +} + +bool UBActionableWidget::shouldClose(QPoint p) +{ + qDebug() << "Should close: " << p.x() << "," << p.y(); + bool close = false; + + if(mShowActions && + p.x() >= 0 && + p.x() <= ACTIONSIZE && + p.y() >= 0 && + p.y() <= ACTIONSIZE){ + close = true; + } + + return close; +} + +void UBActionableWidget::paintEvent(QPaintEvent* 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 + } + } +} diff --git a/src/customWidgets/UBActionableWidget.h b/src/customWidgets/UBActionableWidget.h new file mode 100644 index 00000000..12b2a2fb --- /dev/null +++ b/src/customWidgets/UBActionableWidget.h @@ -0,0 +1,35 @@ +#ifndef UBACTIONABLEWIDGET_H +#define UBACTIONABLEWIDGET_H + +#include +#include + +#define ACTIONSIZE 16 + +typedef enum{ + eAction_Close, + eAction_MoveUp, + eAction_MoveDown +}eAction; + +class UBActionableWidget : public QWidget +{ + Q_OBJECT +public: + UBActionableWidget(QWidget* parent=0, const char* name="UBActionableWidget"); + ~UBActionableWidget(); + void addAction(eAction act); + void removeAction(eAction act); + void removeAllActions(); + void setActionsVisible(bool bVisible); + bool shouldClose(QPoint p); + +protected: + void paintEvent(QPaintEvent* ev); + QVector mActions; + +private: + bool mShowActions; +}; + +#endif // UBACTIONABLEWIDGET_H diff --git a/src/customWidgets/UBMediaWidget.cpp b/src/customWidgets/UBMediaWidget.cpp index c1809d93..ee71a664 100644 --- a/src/customWidgets/UBMediaWidget.cpp +++ b/src/customWidgets/UBMediaWidget.cpp @@ -22,7 +22,7 @@ * @param parent as the parent widget * @param name as the object name */ -UBMediaWidget::UBMediaWidget(eMediaType type, QWidget *parent, const char *name):QWidget(parent) +UBMediaWidget::UBMediaWidget(eMediaType type, QWidget *parent, const char *name):UBActionableWidget(parent, name) , mpMediaObject(NULL) , mpVideoWidget(NULL) , mpAudioOutput(NULL) @@ -35,11 +35,10 @@ UBMediaWidget::UBMediaWidget(eMediaType type, QWidget *parent, const char *name) , mpMediaContainer(NULL) , mpCover(NULL) { - setObjectName(name); - setAttribute(Qt::WA_StyledBackground, true); setStyleSheet(UBApplication::globalStyleSheet()); + addAction(eAction_Close); mType = type; setLayout(&mLayout); diff --git a/src/customWidgets/UBMediaWidget.h b/src/customWidgets/UBMediaWidget.h index 5192c3c7..01b0a0b6 100644 --- a/src/customWidgets/UBMediaWidget.h +++ b/src/customWidgets/UBMediaWidget.h @@ -27,6 +27,7 @@ #include #include "interfaces/IResizeable.h" +#include "UBActionableWidget.h" #define UBMEDIABUTTON_SIZE 32 #define TICK_INTERVAL 1000 @@ -58,7 +59,7 @@ private: bool mPressed; }; -class UBMediaWidget : public QWidget +class UBMediaWidget : public UBActionableWidget { Q_OBJECT public: diff --git a/src/customWidgets/UBWidgetList.cpp b/src/customWidgets/UBWidgetList.cpp index 8b099b5a..62ed6677 100644 --- a/src/customWidgets/UBWidgetList.cpp +++ b/src/customWidgets/UBWidgetList.cpp @@ -1,5 +1,7 @@ #include #include +#include +#include #include "UBGlobals.h" #include "UBWidgetList.h" @@ -135,8 +137,47 @@ void UBWidgetList::resizeEvent(QResizeEvent *ev) void UBWidgetList::mousePressEvent(QMouseEvent *ev) { if(mCanRemove){ + 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; + } + + }else{ + mpCurrentWidget->setActionsVisible(false); + } + } + mpCurrentWidget = dynamic_cast(pWAt); + if(NULL != mpCurrentWidget){ + mpCurrentWidget->setActionsVisible(true); + } + } + update(); +} +QWidget* UBWidgetList::widgetAt(QPoint p) +{ + QWidget* pW = NULL; + pW = childAt(p); + if(NULL != pW){ + do{ + if( "UBTeacherStudentAction" == pW->objectName() || + "UBUrlWidget" == pW->objectName() || + "UBTBMediaPicture" == pW->objectName() || + "UBMediaWidget" == pW->objectName()){ + return pW; + }else{ + pW = pW->parentWidget(); + } + }while(NULL != pW && this != pW); } + + return pW; } void UBWidgetList::updateSizes() diff --git a/src/customWidgets/UBWidgetList.h b/src/customWidgets/UBWidgetList.h index c4137178..d2cd3a6c 100644 --- a/src/customWidgets/UBWidgetList.h +++ b/src/customWidgets/UBWidgetList.h @@ -11,6 +11,7 @@ #include #include "interfaces/IResizeable.h" +#include "customWidgets/UBActionableWidget.h" typedef enum{ eWidgetListOrientation_Vertical, @@ -40,6 +41,9 @@ public: void setListElementSpacing(int margin) { mListElementsSpacing = margin; } int listElementsSpacing() {return mListElementsSpacing; } +signals: + void closeWidget(QWidget* w); + protected: bool mCanRemove; @@ -47,6 +51,7 @@ protected: void mousePressEvent(QMouseEvent* ev); private: + QWidget* widgetAt(QPoint p); int scaleWidgets(QSize pSize); void scaleContainer(QSize pSize, int updateValue); void updateView(QSize pSize); @@ -58,7 +63,7 @@ private: int mListElementsSpacing; QMap mWidgetInfo; QLabel* mpEmptyLabel; - QWidget* mpCurrentWidget; + UBActionableWidget* mpCurrentWidget; }; #endif // UBWIDGETLIST_H diff --git a/src/customWidgets/customWidgets.pri b/src/customWidgets/customWidgets.pri index aa7b0255..5fe74b8c 100644 --- a/src/customWidgets/customWidgets.pri +++ b/src/customWidgets/customWidgets.pri @@ -3,9 +3,11 @@ HEADERS += src/customWidgets/UBWidgetList.h \ src/customWidgets/UBDraggableLabel.h \ src/customWidgets/UBMediaWidget.h \ src/customWidgets/UBGlobals.h \ - src/customWidgets/UBDraggableMedia.h + src/customWidgets/UBDraggableMedia.h \ + src/customWidgets/UBActionableWidget.h SOURCES += src/customWidgets/UBWidgetList.cpp \ src/customWidgets/UBDraggableLabel.cpp \ src/customWidgets/UBMediaWidget.cpp \ - src/customWidgets/UBDraggableMedia.cpp + src/customWidgets/UBDraggableMedia.cpp \ + src/customWidgets/UBActionableWidget.cpp diff --git a/src/gui/UBTBPageEditWidget.cpp b/src/gui/UBTBPageEditWidget.cpp index e2142b13..12424a1a 100644 --- a/src/gui/UBTBPageEditWidget.cpp +++ b/src/gui/UBTBPageEditWidget.cpp @@ -100,6 +100,9 @@ UBTBPageEditWidget::UBTBPageEditWidget(UBTeacherBarDataMgr *pDataMgr, QWidget *p connect(mpDocumentEditbutton, SIGNAL(clicked()), this, SLOT(onDocumentEditClicked())); connect(mpPagePreviewButton, SIGNAL(clicked()), this, SLOT(onPagePreviewClicked())); connect(mpMediaContainer, SIGNAL(mediaDropped(QString)), this, SLOT(onMediaDropped(QString))); + connect(mpActions, SIGNAL(closeWidget(QWidget*)), this, SLOT(onCloseWidget(QWidget*))); + connect(mpLinks, SIGNAL(closeWidget(QWidget*)), this, SLOT(onCloseWidget(QWidget*))); + connect(mpMediaContainer, SIGNAL(closeWidget(QWidget*)), this, SLOT(onCloseWidget(QWidget*))); } UBTBPageEditWidget::~UBTBPageEditWidget() @@ -271,15 +274,36 @@ void UBTBPageEditWidget::clearFields() mClearingFields = false; } +void UBTBPageEditWidget::onCloseWidget(QWidget *w) +{ + if(NULL != w){ + if("UBTeacherStudentAction" == w->objectName()){ + UBTeacherStudentAction* pW = dynamic_cast(w); + mpActions->removeWidget(pW); + mActions.remove(mActions.indexOf(pW)); + DELETEPTR(w); + }else if("UBUrlWidget" == w->objectName()){ + UBUrlWidget* pW = dynamic_cast(w); + mpLinks->removeWidget(pW); + mUrls.remove(mUrls.indexOf(pW)); + DELETEPTR(w); + }else if("UBTBMediaPicture" == w->objectName() || "UBMediaWidget" == w->objectName()){ + mpMediaContainer->removeWidget(w); + mMedias.remove(mMedias.indexOf(w)); + DELETEPTR(w); + } + } +} + // --------------------------------------------------------------------------------------------- -UBUrlWidget::UBUrlWidget(QWidget *parent, const char *name):QWidget(parent) +UBUrlWidget::UBUrlWidget(QWidget *parent, const char *name):UBActionableWidget(parent, name) , mpLayout(NULL) , mpUrlLabel(NULL) , mpUrl(NULL) { - setObjectName(name); setAttribute(Qt::WA_StyledBackground, true); setStyleSheet(UBApplication::globalStyleSheet()); + addAction(eAction_Close); mpLayout = new QVBoxLayout(this); setLayout(mpLayout); @@ -435,12 +459,13 @@ QWidget* UBTBMediaContainer::generateMediaWidget(const QString& url) QString mimeType = UBFileSystemUtils::mimeTypeFromFileName(url); if(mimeType.contains("image")){ QPixmap pix = QPixmap(url); - QLabel* label = new QLabel(); - pix.scaledToWidth(label->width()); - label->resize(pix.width(), pix.height()); - label->setPixmap(pix); - label->setScaledContents(true); - pW = label; + UBPictureWidget* pic = new UBPictureWidget(); + pix.scaledToWidth(pic->label()->width()); + pic->label()->resize(pix.width(), pix.height()); + pic->label()->setPixmap(pix); + pic->label()->setScaledContents(true); + pic->setObjectName("UBTBMediaPicture"); + pW = pic; } else if(mimeType.contains("video") || mimeType.contains("audio")){ UBMediaWidget* mediaPlayer = new UBMediaWidget(mimeType.contains("audio")?eMediaType_Audio:eMediaType_Video); @@ -455,16 +480,15 @@ QWidget* UBTBMediaContainer::generateMediaWidget(const QString& url) return pW; } -UBTeacherStudentAction::UBTeacherStudentAction(QWidget *parent, const char *name):QWidget(parent) +UBTeacherStudentAction::UBTeacherStudentAction(QWidget *parent, const char *name):UBActionableWidget(parent, name) , mpText(NULL) , mpLayout(NULL) , mpComboLayout(NULL) , mpCombo(NULL) { - setObjectName(name); - setAttribute(Qt::WA_StyledBackground, true); setStyleSheet(UBApplication::globalStyleSheet()); + addAction(eAction_Close); // Create the GUI mpLayout = new QHBoxLayout(this); @@ -532,3 +556,27 @@ void UBTeacherStudentAction::setText(const QString& text) } } +// ------------------------------------------------------------- +UBPictureWidget::UBPictureWidget(QWidget *parent, const char *name):UBActionableWidget(parent, name) + , mpLayout(NULL) + , mpLabel(NULL) +{ + addAction(eAction_Close); + mpLayout = new QVBoxLayout(this); + setLayout(mpLayout); + mpLayout->setContentsMargins(10, 0, 10, 0); + mpLabel = new QLabel(this); + mpLayout->addWidget(mpLabel); + mpLabel->setGeometry( 10, 10, width()-2*10, height()); +} + +UBPictureWidget::~UBPictureWidget() +{ + DELETEPTR(mpLabel); + DELETEPTR(mpLayout); +} + +void UBPictureWidget::resizeEvent(QResizeEvent *ev) +{ + mpLabel->setGeometry( 10, 10, width()-2*10, height()); +} diff --git a/src/gui/UBTBPageEditWidget.h b/src/gui/UBTBPageEditWidget.h index 0f3c61b9..0521c847 100644 --- a/src/gui/UBTBPageEditWidget.h +++ b/src/gui/UBTBPageEditWidget.h @@ -11,10 +11,11 @@ #include "core/UBPersistenceManager.h" #include "customWidgets/UBWidgetList.h" +#include "customWidgets/UBActionableWidget.h" #include "interfaces/IDropable.h" #include "UBTeacherBarDataMgr.h" -class UBTeacherStudentAction : public QWidget +class UBTeacherStudentAction : public UBActionableWidget { Q_OBJECT @@ -33,7 +34,7 @@ private: QComboBox* mpCombo; }; -class UBUrlWidget : public QWidget +class UBUrlWidget : public UBActionableWidget { public: UBUrlWidget(QWidget* parent=0, const char* name="UBUrlWidget"); @@ -56,6 +57,22 @@ private: QLineEdit* mpTitle; }; +class UBPictureWidget : public UBActionableWidget +{ +public: + UBPictureWidget(QWidget* parent=0, const char* name="UBPictureWidget"); + ~UBPictureWidget(); + + QLabel* label(){return mpLabel;} + +protected: + void resizeEvent(QResizeEvent* ev); + +private: + QVBoxLayout* mpLayout; + QLabel* mpLabel; +}; + class UBTBMediaContainer : public UBWidgetList , public IDropable { @@ -104,6 +121,7 @@ private slots: void onMediaDropped(const QString& url); void onDocumentEditClicked(); void onPagePreviewClicked(); + void onCloseWidget(QWidget* w); private: QVBoxLayout mLayout;