parent
b824051438
commit
df8a8a5ec9
@ -0,0 +1,133 @@ |
|||||||
|
#include <QDebug> |
||||||
|
#include "UBWidgetList.h" |
||||||
|
|
||||||
|
UBWidgetList::UBWidgetList(QWidget* parent, eWidgetListOrientation orientation, const char* name):QScrollArea(parent) |
||||||
|
, mpLayout(NULL) |
||||||
|
, mpContainer(NULL) |
||||||
|
, mMargin(5) |
||||||
|
, mpEmptyLabel(NULL) |
||||||
|
{ |
||||||
|
setObjectName(name); |
||||||
|
mOrientation = orientation; |
||||||
|
mpContainer = new QWidget(this); |
||||||
|
mWidgets.clear(); |
||||||
|
|
||||||
|
if(eWidgetListOrientation_Vertical == orientation){ |
||||||
|
setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); |
||||||
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
||||||
|
mpLayout = new QVBoxLayout(mpContainer); |
||||||
|
mpContainer->resize(width(), mpContainer->height()); |
||||||
|
}else{ |
||||||
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); |
||||||
|
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
||||||
|
mpLayout = new QHBoxLayout(mpContainer); |
||||||
|
mpContainer->resize(mpContainer->width(), height()); |
||||||
|
} |
||||||
|
mpLayout->setContentsMargins(margin(), margin(), margin(), margin()); |
||||||
|
mpContainer->setLayout(mpLayout); |
||||||
|
setWidget(mpContainer); |
||||||
|
} |
||||||
|
|
||||||
|
UBWidgetList::~UBWidgetList() |
||||||
|
{ |
||||||
|
if(NULL != mpEmptyLabel){ |
||||||
|
delete mpEmptyLabel; |
||||||
|
mpEmptyLabel = NULL; |
||||||
|
} |
||||||
|
if(NULL != mpLayout){ |
||||||
|
delete mpLayout; |
||||||
|
mpLayout = NULL; |
||||||
|
} |
||||||
|
if(NULL != mpContainer){ |
||||||
|
delete mpContainer; |
||||||
|
mpContainer = NULL; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UBWidgetList::addWidget(QWidget *widget) |
||||||
|
{ |
||||||
|
if(NULL != mpLayout){ |
||||||
|
updateSize(true, widget); |
||||||
|
mpLayout->addWidget(widget); |
||||||
|
mWidgets << widget; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UBWidgetList::removeWidget(QWidget *widget) |
||||||
|
{ |
||||||
|
if(NULL != mpLayout){ |
||||||
|
mpLayout->removeWidget(widget); |
||||||
|
mWidgets.remove(mWidgets.indexOf(widget)); |
||||||
|
updateSize(false, widget); |
||||||
|
widget->setVisible(false); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UBWidgetList::updateSize(bool widgetAdded, QWidget *widget) |
||||||
|
{ |
||||||
|
float scaleFactor; |
||||||
|
int newWidgetWidth; |
||||||
|
int newWidgetHeight; |
||||||
|
|
||||||
|
if(eWidgetListOrientation_Vertical == mOrientation){ |
||||||
|
scaleFactor = (float)widget->width() / (float)mpContainer->width(); |
||||||
|
}else{ |
||||||
|
scaleFactor = (float)widget->height() / (float)mpContainer->height(); |
||||||
|
} |
||||||
|
|
||||||
|
newWidgetWidth = widget->width()*scaleFactor; |
||||||
|
newWidgetHeight = widget->height()*scaleFactor; |
||||||
|
|
||||||
|
widget->resize(newWidgetWidth, newWidgetHeight); |
||||||
|
|
||||||
|
// Now we have to update the container
|
||||||
|
if(eWidgetListOrientation_Vertical == mOrientation){ |
||||||
|
if(widgetAdded){ |
||||||
|
mpContainer->resize(mpContainer->width(), mpContainer->height() + newWidgetHeight); |
||||||
|
}else{ |
||||||
|
mpContainer->resize(mpContainer->width(), mpContainer->height() - newWidgetHeight); |
||||||
|
} |
||||||
|
}else{ |
||||||
|
if(widgetAdded){ |
||||||
|
mpContainer->resize(mpContainer->width() + newWidgetWidth, mpContainer->height()); |
||||||
|
}else{ |
||||||
|
mpContainer->resize(mpContainer->width() - newWidgetWidth, mpContainer->height()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UBWidgetList::resizeEvent(QResizeEvent *ev) |
||||||
|
{ |
||||||
|
if(ev->oldSize().width() >= 0 && ev->oldSize().height() >= 0){ |
||||||
|
float scale; |
||||||
|
if(eWidgetListOrientation_Vertical == mOrientation){ |
||||||
|
scale = (float)ev->size().width() / (float)ev->oldSize().width(); |
||||||
|
updateAllWidgetsize(scale); |
||||||
|
mpContainer->resize(width() - 2, mpContainer->height()*scale); |
||||||
|
}else{ |
||||||
|
scale = (float)ev->size().height() / (float)ev->oldSize().height(); |
||||||
|
updateAllWidgetsize(scale); |
||||||
|
mpContainer->resize(mpContainer->width()*scale, height() - 2); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UBWidgetList::updateAllWidgetsize(float scale) |
||||||
|
{ |
||||||
|
for(int i=0; i<mWidgets.size(); i++){ |
||||||
|
mWidgets.at(i)->resize(mWidgets.at(i)->width()*scale, mWidgets.at(i)->height()*scale); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UBWidgetList::setMargin(int margin) |
||||||
|
{ |
||||||
|
mMargin = margin; |
||||||
|
} |
||||||
|
|
||||||
|
int UBWidgetList::margin() |
||||||
|
{ |
||||||
|
return mMargin; |
||||||
|
} |
||||||
|
|
||||||
|
// TODO : - add onHover 'delete' button
|
||||||
|
// - add empty label
|
@ -0,0 +1,44 @@ |
|||||||
|
#ifndef UBWIDGETLIST_H |
||||||
|
#define UBWIDGETLIST_H |
||||||
|
|
||||||
|
#include <QWidget> |
||||||
|
#include <QScrollArea> |
||||||
|
#include <QLayout> |
||||||
|
#include <QVBoxLayout> |
||||||
|
#include <QHBoxLayout> |
||||||
|
#include <QResizeEvent> |
||||||
|
#include <QVector> |
||||||
|
#include <QLabel> |
||||||
|
|
||||||
|
typedef enum{ |
||||||
|
eWidgetListOrientation_Vertical, |
||||||
|
eWidgetListOrientation_Horizontal |
||||||
|
}eWidgetListOrientation; |
||||||
|
|
||||||
|
class UBWidgetList : public QScrollArea |
||||||
|
{ |
||||||
|
Q_OBJECT |
||||||
|
|
||||||
|
public: |
||||||
|
UBWidgetList(QWidget* parent=0, eWidgetListOrientation orientation = eWidgetListOrientation_Vertical, const char* name = "UBWidgetList"); |
||||||
|
~UBWidgetList(); |
||||||
|
void addWidget(QWidget* widget); |
||||||
|
void removeWidget(QWidget* widget); |
||||||
|
void setMargin(int margin); |
||||||
|
int margin(); |
||||||
|
|
||||||
|
protected: |
||||||
|
void resizeEvent(QResizeEvent* ev); |
||||||
|
|
||||||
|
private: |
||||||
|
void updateSize(bool widgetAdded, QWidget* widget); |
||||||
|
void updateAllWidgetsize(float scale); |
||||||
|
QLayout* mpLayout; |
||||||
|
QWidget* mpContainer; |
||||||
|
eWidgetListOrientation mOrientation; |
||||||
|
int mMargin; |
||||||
|
QVector<QWidget*> mWidgets; |
||||||
|
QLabel* mpEmptyLabel; |
||||||
|
}; |
||||||
|
|
||||||
|
#endif // UBWIDGETLIST_H
|
@ -0,0 +1,5 @@ |
|||||||
|
|
||||||
|
HEADERS += src/customWidgets/UBWidgetList.h |
||||||
|
|
||||||
|
SOURCES += src/customWidgets/UBWidgetList.cpp |
||||||
|
|
Loading…
Reference in new issue