fixed UBWidgetList resize issue

preferencesAboutTextFull
Claudio Valerio 13 years ago
parent ca29e0d5e3
commit b731b302de
  1. 1
      src/core/UBApplicationController.cpp
  2. 106
      src/customWidgets/UBWidgetList.cpp
  3. 8
      src/customWidgets/UBWidgetList.h

@ -722,7 +722,6 @@ void UBApplicationController::importFile(const QString& pFilePath)
void UBApplicationController::useMultiScreen(bool use) void UBApplicationController::useMultiScreen(bool use)
{ {
mDisplayManager->setUseMultiScreen(use); mDisplayManager->setUseMultiScreen(use);
} }

@ -10,7 +10,6 @@ UBWidgetList::UBWidgetList(QWidget* parent, eWidgetListOrientation orientation,
setObjectName(name); setObjectName(name);
mOrientation = orientation; mOrientation = orientation;
mpContainer = new QWidget(this); mpContainer = new QWidget(this);
mWidgets.clear();
mpEmptyLabel = new QLabel(this); mpEmptyLabel = new QLabel(this);
mpEmptyLabel->setObjectName("emptyString"); mpEmptyLabel->setObjectName("emptyString");
@ -18,12 +17,13 @@ UBWidgetList::UBWidgetList(QWidget* parent, eWidgetListOrientation orientation,
setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
mpLayout = new QVBoxLayout(mpContainer); mpLayout = new QVBoxLayout(mpContainer);
mpContainer->resize(width(), mpContainer->height()); mpContainer->resize(mpContainer->width(), mpContainer->height());
}else{ }
else{
setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
mpLayout = new QHBoxLayout(mpContainer); mpLayout = new QHBoxLayout(mpContainer);
mpContainer->resize(mpContainer->width(), height()); mpContainer->resize(mpContainer->width(), mpContainer->height());
} }
mpLayout->setContentsMargins(margin(), margin(), margin(), margin()); mpLayout->setContentsMargins(margin(), margin(), margin(), margin());
mpContainer->setLayout(mpLayout); mpContainer->setLayout(mpLayout);
@ -50,8 +50,8 @@ void UBWidgetList::addWidget(QWidget *widget)
{ {
if(NULL != mpLayout){ if(NULL != mpLayout){
mpEmptyLabel->setVisible(false); mpEmptyLabel->setVisible(false);
mWidgets << widget; mWidgetInfo[widget] = widget->size();
updateSize(true, widget); updateView(size());
mpLayout->addWidget(widget); mpLayout->addWidget(widget);
} }
} }
@ -60,82 +60,68 @@ void UBWidgetList::removeWidget(QWidget *widget)
{ {
if(NULL != mpLayout){ if(NULL != mpLayout){
mpLayout->removeWidget(widget); mpLayout->removeWidget(widget);
mWidgets.remove(mWidgets.indexOf(widget)); mWidgetInfo.remove(widget);
updateSize(false, widget);
widget->setVisible(false); widget->setVisible(false);
updateView(size());
if(0 == mpLayout->count()){ if(0 == mpLayout->count()){
mpEmptyLabel->setVisible(true); mpEmptyLabel->setVisible(true);
} }
} }
} }
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);
int UBWidgetList::scaleWidgets(QSize pSize)
// Now we have to update the container {
int result = 0;
foreach(QWidget* eachWidget, mWidgetInfo.keys()){
qDebug() << __FUNCTION__ << "eachWidget : " << eachWidget;
qreal scaleFactor = 0;
int newWidgetWidth = pSize.width();
int newWidgetHeight = pSize.height();
if(eWidgetListOrientation_Vertical == mOrientation){ if(eWidgetListOrientation_Vertical == mOrientation){
if(widgetAdded){ scaleFactor = (float)mWidgetInfo[eachWidget].width() / (float)pSize.width();
mpContainer->resize(mpContainer->width(), mpContainer->height() + newWidgetHeight); qDebug() << __FUNCTION__ << "scale factor: " << scaleFactor;
newWidgetHeight = mWidgetInfo[eachWidget].height()/scaleFactor;
result += newWidgetHeight;
} }
else{ else{
mpContainer->resize(mpContainer->width(), mpContainer->height() - newWidgetHeight); scaleFactor = (float)mWidgetInfo[eachWidget].height() / (float)pSize.height();
newWidgetWidth = mWidgetInfo[eachWidget].width()/scaleFactor;
result += newWidgetWidth;
} }
} }
else{ return result;
if(widgetAdded){
mpContainer->resize(mpContainer->width() + newWidgetWidth, mpContainer->height());
}
else{
mpContainer->resize(mpContainer->width() - newWidgetWidth, mpContainer->height());
} }
void UBWidgetList::scaleContainer(QSize pSize, int updateValue)
{
if(eWidgetListOrientation_Vertical == mOrientation)
mpContainer->resize(pSize.width(), updateValue);
else
mpContainer->resize(updateValue, pSize.height());
} }
void UBWidgetList::updateView(QSize pSize)
{
// Widgets on list are resized automatically to fit the mpcontainer.
// so if you want to keep the aspect ratio you have to calculate
// the sum of the new widget height and give it to the mpContainer.
// The container resize will trig the widgets resize and the good
// height permits to respect the aspect ratio.
int updatedValue = scaleWidgets(pSize);
scaleContainer(pSize,updatedValue);
} }
void UBWidgetList::resizeEvent(QResizeEvent *ev) void UBWidgetList::resizeEvent(QResizeEvent *ev)
{ {
mpEmptyLabel->setGeometry((width() - mpEmptyLabel->width()) / 2, mpEmptyLabel->setGeometry((width() - mpEmptyLabel->width()) / 2,
(height() - mpEmptyLabel->height()) /2, (height() - mpEmptyLabel->height()) /2,
mpEmptyLabel->width(), mpEmptyLabel->width(),
mpEmptyLabel->height()); mpEmptyLabel->height());
if(ev->oldSize().width() >= 0 && ev->oldSize().height() >= 0){ updateView(size());
float scale;
if(eWidgetListOrientation_Vertical == mOrientation){
scale = (float)ev->size().width() / (float)ev->oldSize().width();
if(scale != 0 && scale < 10){
updateAllWidgetsize(scale);
mpContainer->resize(width() - 2, mpContainer->height()*scale);
}
}
else{
scale = (float)ev->size().height() / (float)ev->oldSize().height();
if(scale != 0 && scale < 10){
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) void UBWidgetList::setMargin(int margin)
@ -157,7 +143,7 @@ void UBWidgetList::setEmptyText(const QString &text)
bool UBWidgetList::empty() bool UBWidgetList::empty()
{ {
return mWidgets.empty(); return mWidgetInfo.empty();
} }
// TODO : - add onHover 'delete' button // TODO : - add onHover 'delete' button

@ -15,6 +15,7 @@ typedef enum{
eWidgetListOrientation_Horizontal eWidgetListOrientation_Horizontal
}eWidgetListOrientation; }eWidgetListOrientation;
class UBWidgetList : public QScrollArea class UBWidgetList : public QScrollArea
{ {
Q_OBJECT Q_OBJECT
@ -33,13 +34,14 @@ protected:
void resizeEvent(QResizeEvent* ev); void resizeEvent(QResizeEvent* ev);
private: private:
void updateSize(bool widgetAdded, QWidget* widget); int scaleWidgets(QSize pSize);
void updateAllWidgetsize(float scale); void scaleContainer(QSize pSize, int updateValue);
void updateView(QSize pSize);
QLayout* mpLayout; QLayout* mpLayout;
QWidget* mpContainer; QWidget* mpContainer;
eWidgetListOrientation mOrientation; eWidgetListOrientation mOrientation;
int mMargin; int mMargin;
QVector<QWidget*> mWidgets; QMap<QWidget*,QSize> mWidgetInfo;
QLabel* mpEmptyLabel; QLabel* mpEmptyLabel;
}; };

Loading…
Cancel
Save