Просмотр исходного кода

Relayout ctkThumbnailListWidget when thumbnails become visible

ctkThumbnailListWidget controls it's own scrollarea internal widget size.
The size is manually computed from the layout (ctkFlowLayout).
However, it fails at computing a correct size if the added widgets are
not visible at the time of the relayout.
ctkThumbnailListWidget now observes the showevent of such widgets to
trigger a relayout.
Closes #272
Julien Finet лет назад: 12
Родитель
Сommit
5a221161e9

+ 23 - 0
Libs/Widgets/ctkThumbnailListWidget.cpp

@@ -21,6 +21,7 @@
 // Qt include
 #include <QDateTime>
 #include <QDir>
+#include <QEvent>
 #include <QFile>
 #include <QFileInfo>
 #include <QGridLayout>
@@ -28,6 +29,7 @@
 #include <QPushButton>
 #include <QResizeEvent>
 #include <QScrollBar>
+#include <QTimer>
 
 // ctk includes
 #include "ctkLogger.h"
@@ -52,6 +54,7 @@ static ctkLogger logger("org.commontk.Widgets.ctkThumbnailListWidget");
 ctkThumbnailListWidgetPrivate
 ::ctkThumbnailListWidgetPrivate(ctkThumbnailListWidget* parent)
   : q_ptr(parent)
+  , RequestRelayout(false)
 {
 }
 
@@ -98,6 +101,8 @@ void ctkThumbnailListWidgetPrivate::addThumbnail(ctkThumbnailLabel* thumbnail)
   Q_Q(ctkThumbnailListWidget);
 
   this->ScrollAreaContentWidget->layout()->addWidget(thumbnail);
+  thumbnail->installEventFilter(q);
+  this->RequestRelayout = true;
 
   q->connect(thumbnail, SIGNAL(selected(ctkThumbnailLabel)),
     q, SLOT(onThumbnailSelected(ctkThumbnailLabel)));
@@ -302,6 +307,24 @@ void ctkThumbnailListWidget::resizeEvent(QResizeEvent* event)
   d->updateScrollAreaContentWidgetSize(event->size());
 }
 
+//----------------------------------------------------------------------------
+bool ctkThumbnailListWidget::eventFilter(QObject* watched, QEvent* event)
+{
+  Q_D(ctkThumbnailListWidget);
+  if (d->RequestRelayout &&
+      qobject_cast<ctkThumbnailLabel*>(watched) &&
+      event->type() == QEvent::Show)
     {
+    d->RequestRelayout = false;
+    watched->removeEventFilter(this);
+    QTimer::singleShot(0, this, SLOT(updateLayout()));
     }
+  return this->Superclass::eventFilter(watched, event);
+}
+
+//----------------------------------------------------------------------------
+void ctkThumbnailListWidget::updateLayout()
+{
+  Q_D(ctkThumbnailListWidget);
+  d->updateScrollAreaContentWidgetSize(this->size());
 }

+ 3 - 0
Libs/Widgets/ctkThumbnailListWidget.h

@@ -66,6 +66,8 @@ public:
   /// Get thumbnail width
   QSize thumbnailSize()const;
 
+  virtual bool eventFilter(QObject* watched, QEvent* event);
+
 public Q_SLOTS:
   /// Set thumbnail width
   void setThumbnailSize(QSize size);
@@ -76,6 +78,7 @@ Q_SIGNALS:
 
 protected Q_SLOTS:
   void onThumbnailSelected(const ctkThumbnailLabel& widget);
+  void updateLayout();
 
 protected:
   explicit ctkThumbnailListWidget(ctkThumbnailListWidgetPrivate* ptr, QWidget* parent=0);

+ 1 - 0
Libs/Widgets/ctkThumbnailListWidget_p.h

@@ -45,6 +45,7 @@ public:
 
   int CurrentThumbnail;
   QSize ThumbnailSize;
+  bool RequestRelayout;
 
 protected:
   ctkThumbnailListWidget* const q_ptr;