Add AutoHide to RSImageBlockWidget

This commit is contained in:
Phenom 2016-08-15 12:00:44 +02:00
parent 368a429846
commit 9780f1ff78
5 changed files with 130 additions and 7 deletions

View File

@ -198,6 +198,7 @@ ChatWidget::ChatWidget(QWidget *parent) :
ui->textBrowser->resetImagesStatus(Settings->getChatLoadEmbeddedImages());
ui->textBrowser->setImageBlockWidget(ui->imageBlockWidget);
ui->imageBlockWidget->setAutoHide(true);
ui->textBrowser->installEventFilter(this);
ui->textBrowser->viewport()->installEventFilter(this);
ui->chatTextEdit->installEventFilter(this);

View File

@ -26,15 +26,28 @@
RSImageBlockWidget::RSImageBlockWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::RSImageBlockWidget)
ui(new Ui::RSImageBlockWidget),
mAutoHide(false), mAutoHideHeight(4), mAutoHideTimeToStart(3000), mAutoHideDuration(3000)
{
ui->setupUi(this);
mDefaultRect = this->geometry();
ui->infoFrame->installEventFilter(this);
mTimer = new RsProtectedTimer(this);
mTimer->setSingleShot(true);
connect(mTimer, SIGNAL(timeout()), this, SLOT(startAutoHide()));
mAnimation = new QPropertyAnimation(this, "geometry");
connect(ui->loadImagesButton, SIGNAL(clicked()), this, SIGNAL(showImages()));
}
RSImageBlockWidget::~RSImageBlockWidget()
{
delete mAnimation;
mTimer->stop();
delete mTimer;
delete ui;
}
@ -64,3 +77,61 @@ void RSImageBlockWidget::addButtonAction(const QString &text, const QObject *rec
connect(action, SIGNAL(triggered()), this, SIGNAL(showImages()));
}
}
bool RSImageBlockWidget::eventFilter(QObject *obj, QEvent *event)
{
if (mAutoHide) {
if (event->type() == QEvent::Show) {
mTimer->start(mAutoHideTimeToStart);
}
if (event->type() == QEvent::Hide) {
mTimer->stop();
}
if (event->type() == QEvent::Enter) {
mAnimation->stop();
this->setGeometry(mDefaultRect);
this->updateGeometry();
mTimer->start(mAutoHideTimeToStart);
mAnimation->setCurrentTime(0);
}
}
if (mAnimation->currentTime() == 0) {
mDefaultRect = this->geometry();
} else if (mAnimation->state() == QAbstractAnimation::Running) {
this->updateGeometry();
}
// pass the event on to the parent class
return QObject::eventFilter(obj, event);
}
void RSImageBlockWidget::setAutoHide(const bool value)
{
if(value && !mAutoHide) {
if (this->isVisible()) mTimer->start(mAutoHideTimeToStart);
} else if (!value && mAutoHide) {
mTimer->stop();
}
mAutoHide = value;
}
void RSImageBlockWidget::startAutoHide()
{
QRect r = mDefaultRect;
r.setHeight(mAutoHideHeight);
this->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Preferred);
mAnimation->setDuration(mAutoHideDuration);
mAnimation->setStartValue(mDefaultRect);
mAnimation->setEndValue(r);
mAnimation->start();
}
QSize RSImageBlockWidget::sizeHint() const
{
if (mAnimation->currentTime() == 0) {
return mDefaultRect.size();
} else {
return mAnimation->currentValue().toRect().size();
}
}

View File

@ -22,8 +22,11 @@
#ifndef RSIMAGEBLOCKWIDGET_H
#define RSIMAGEBLOCKWIDGET_H
#include <QPropertyAnimation>
#include <QWidget>
#include "util/RsProtectedTimer.h"
namespace Ui {
class RSImageBlockWidget;
}
@ -32,17 +35,48 @@ class RSImageBlockWidget : public QWidget
{
Q_OBJECT
Q_PROPERTY(bool autoHide READ autoHide WRITE setAutoHide)
Q_PROPERTY(int autoHideHeight READ autoHideHeight WRITE setAutoHideHeight)
Q_PROPERTY(int autoHideTimeToStart READ autoHideTimeToStart WRITE setAutoHideTimeToStart)
Q_PROPERTY(int autoHideDuration READ autoHideDuration WRITE setAutoHideDuration)
public:
explicit RSImageBlockWidget(QWidget *parent = 0);
~RSImageBlockWidget();
void addButtonAction(const QString &text, const QObject *receiver, const char *member, bool standardAction);
virtual QSize sizeHint() const;//To update parent layout.
virtual QSize minimumSizeHint() const { return sizeHint();}//To update parent layout.
bool autoHide() { return mAutoHide; }
int autoHideHeight() { return mAutoHideHeight; }
int autoHideTimeToStart() { return mAutoHideTimeToStart; }
int autoHideDuration() { return mAutoHideDuration; }
void setAutoHide(const bool value);
void setAutoHideHeight(const int value) { mAutoHideHeight = value; }
void setAutoHideTimeToStart(const int value) { mAutoHideTimeToStart = value; }
void setAutoHideDuration(const int value) { mAutoHideDuration = value; }
signals:
void showImages();
private slots:
void startAutoHide();
protected:
bool eventFilter(QObject *obj, QEvent *event);
private:
Ui::RSImageBlockWidget *ui;
QPropertyAnimation *mAnimation;
QRect mDefaultRect;
RsProtectedTimer *mTimer;
bool mAutoHide;
int mAutoHideHeight;
int mAutoHideTimeToStart;
int mAutoHideDuration;
};
#endif // RSIMAGEBLOCKWIDGET_H

View File

@ -13,11 +13,20 @@
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<layout class="QHBoxLayout" name="RSImageBlockWidgetHLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="margin">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
@ -92,8 +101,17 @@
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="margin">
<layout class="QHBoxLayout" name="infoFrameHLayout">
<property name="leftMargin">
<number>6</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>6</number>
</property>
<property name="bottomMargin">
<number>6</number>
</property>
<item>
@ -114,7 +132,7 @@
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<spacer name="infoFrameHSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>

View File

@ -3,7 +3,6 @@
#include "retroshare/rspeers.h"
#include "retroshare/rsservicecontrol.h"
#include "util/RsProtectedTimer.h"
#include "retroshare-gui/RsAutoUpdatePage.h"
#include "BandwidthStatsWidget.h"