Core: Allow showing/hiding details widget in info bar

There might be multiple InfoBarDisplay instances, so create the details
widget for each separately.

Also, remember the "Show Details" state.

Change-Id: I6ee982a9a04373c35d3d1106bf049c0346c50525
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Nikolai Kosjar
2016-10-05 13:39:17 +02:00
parent eb55c1a4e4
commit a9e0c9e57d
2 changed files with 45 additions and 8 deletions

View File

@@ -32,6 +32,7 @@
#include <QFrame>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QToolButton>
@@ -67,6 +68,10 @@ void InfoBarEntry::setCancelButtonInfo(const QString &_cancelButtonText, CallBac
m_cancelButtonCallBack = callBack;
}
void InfoBarEntry::setDetailsWidgetCreator(const InfoBarEntry::DetailsWidgetCreator &creator)
{
m_detailsWidgetCreator = creator;
}
void InfoBar::addInfo(const InfoBarEntry &info)
{
@@ -151,9 +156,6 @@ bool InfoBar::anyGloballySuppressed()
InfoBarDisplay::InfoBarDisplay(QObject *parent)
: QObject(parent)
, m_infoBar(0)
, m_boxLayout(0)
, m_boxIndex(0)
{
}
@@ -209,13 +211,43 @@ void InfoBarDisplay::update()
infoWidget->setLineWidth(1);
infoWidget->setAutoFillBackground(true);
QHBoxLayout *hbox = new QHBoxLayout(infoWidget);
QHBoxLayout *hbox = new QHBoxLayout;
hbox->setMargin(2);
auto *vbox = new QVBoxLayout(infoWidget);
vbox->setMargin(0);
vbox->addLayout(hbox);
QLabel *infoWidgetLabel = new QLabel(info.infoText);
infoWidgetLabel->setWordWrap(true);
hbox->addWidget(infoWidgetLabel);
if (info.m_detailsWidgetCreator) {
if (m_isShowingDetailsWidget) {
QWidget *detailsWidget = info.m_detailsWidgetCreator();
vbox->addWidget(detailsWidget);
}
auto *showDetailsButton = new QToolButton;
showDetailsButton->setCheckable(true);
showDetailsButton->setChecked(m_isShowingDetailsWidget);
showDetailsButton->setText(tr("&Show Details"));
connect(showDetailsButton, &QToolButton::clicked, [this, vbox, info] (bool) {
QWidget *detailsWidget = vbox->count() == 2 ? vbox->itemAt(1)->widget() : nullptr;
if (!detailsWidget) {
detailsWidget = info.m_detailsWidgetCreator();
vbox->addWidget(detailsWidget);
}
m_isShowingDetailsWidget = !m_isShowingDetailsWidget;
detailsWidget->setVisible(m_isShowingDetailsWidget);
});
hbox->addWidget(showDetailsButton);
} else {
m_isShowingDetailsWidget = false;
}
if (!info.buttonText.isEmpty()) {
QToolButton *infoWidgetButton = new QToolButton;
infoWidgetButton->setText(info.buttonText);

View File

@@ -54,11 +54,14 @@ public:
InfoBarEntry(Id _id, const QString &_infoText, GlobalSuppressionMode _globalSuppression = GlobalSuppressionDisabled);
InfoBarEntry(const InfoBarEntry &other) { *this = other; }
typedef std::function<void()> CallBack;
using CallBack = std::function<void()>;
void setCustomButtonInfo(const QString &_buttonText, CallBack callBack);
void setCancelButtonInfo(CallBack callBack);
void setCancelButtonInfo(const QString &_cancelButtonText, CallBack callBack);
using DetailsWidgetCreator = std::function<QWidget*()>;
void setDetailsWidgetCreator(const DetailsWidgetCreator &creator);
private:
Id id;
QString infoText;
@@ -67,6 +70,7 @@ private:
QString cancelButtonText;
CallBack m_cancelButtonCallBack;
GlobalSuppressionMode globalSuppression;
DetailsWidgetCreator m_detailsWidgetCreator;
friend class InfoBar;
friend class InfoBarDisplay;
};
@@ -113,9 +117,10 @@ private:
void widgetDestroyed();
QList<QWidget *> m_infoWidgets;
InfoBar *m_infoBar;
QBoxLayout *m_boxLayout;
int m_boxIndex;
InfoBar *m_infoBar = nullptr;
QBoxLayout *m_boxLayout = nullptr;
int m_boxIndex = 0;
bool m_isShowingDetailsWidget = false;
};
} // namespace Core