forked from qt-creator/qt-creator
Improve visual appearance of DetailsWidget
* Make toolswidget appear on hover. * Use a cached gradient in the background. * Remove layout fixup as that does not help on Linux and Windows anymore. Reevaluate after testing on Mac. Reviewed-by: dt
This commit is contained in:
@@ -9,31 +9,41 @@
|
|||||||
|
|
||||||
using namespace Utils;
|
using namespace Utils;
|
||||||
|
|
||||||
DetailsWidget::DetailsWidget(QWidget *parent)
|
namespace {
|
||||||
: QWidget(parent),
|
const int MARGIN=8;
|
||||||
m_summaryLabel(new QLabel(this)),
|
}
|
||||||
m_detailsButton(new DetailsButton(this)),
|
|
||||||
m_widget(0),
|
|
||||||
m_toolWidget(0),
|
|
||||||
m_grid(new QGridLayout(this))
|
|
||||||
|
|
||||||
|
// This widget is using a grid layout and places the items
|
||||||
|
// in the following way:
|
||||||
|
//
|
||||||
|
// +------------+-------------------------+---------------+
|
||||||
|
// + toolWidget | summaryLabel | detailsButton |
|
||||||
|
// +------------+-------------------------+---------------+
|
||||||
|
// | | widget |
|
||||||
|
// +------------+-------------------------+---------------+
|
||||||
|
|
||||||
|
DetailsWidget::DetailsWidget(QWidget *parent) :
|
||||||
|
QWidget(parent),
|
||||||
|
m_detailsButton(new DetailsButton(this)),
|
||||||
|
m_grid(new QGridLayout(this)),
|
||||||
|
m_summaryLabel(new QLabel(this)),
|
||||||
|
m_toolWidget(0),
|
||||||
|
m_widget(0),
|
||||||
|
m_hovered(false)
|
||||||
{
|
{
|
||||||
m_grid->setContentsMargins(4, 3, 4, 3);
|
|
||||||
|
|
||||||
m_summaryLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
|
m_summaryLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
|
||||||
m_summaryLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
m_summaryLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||||||
|
m_summaryLabel->setContentsMargins(MARGIN, MARGIN, MARGIN, MARGIN);
|
||||||
|
|
||||||
m_grid->addWidget(m_summaryLabel, 0, 0);
|
m_grid->setContentsMargins(0, 0, 0, 0);
|
||||||
m_grid->addWidget(m_detailsButton, 0, 2, 1, 1, Qt::AlignBottom);
|
m_grid->setSpacing(0);
|
||||||
|
m_grid->addWidget(m_summaryLabel, 0, 1);
|
||||||
|
m_grid->addWidget(m_detailsButton, 0, 2, 1, 1, Qt::AlignCenter);
|
||||||
|
|
||||||
m_dummyWidget = new QWidget(this);
|
m_detailsButton->setEnabled(false);
|
||||||
m_dummyWidget->setMaximumHeight(4);
|
|
||||||
m_dummyWidget->setMaximumHeight(4);
|
|
||||||
m_dummyWidget->setVisible(false);
|
|
||||||
m_grid->addWidget(m_dummyWidget, 2, 0, 1, 1);
|
|
||||||
|
|
||||||
connect(m_detailsButton, SIGNAL(clicked()),
|
connect(m_detailsButton, SIGNAL(toggled(bool)),
|
||||||
this, SLOT(detailsButtonClicked()));
|
this, SLOT(setExpanded(bool)));
|
||||||
}
|
}
|
||||||
|
|
||||||
DetailsWidget::~DetailsWidget()
|
DetailsWidget::~DetailsWidget()
|
||||||
@@ -43,53 +53,36 @@ DetailsWidget::~DetailsWidget()
|
|||||||
|
|
||||||
void DetailsWidget::paintEvent(QPaintEvent *paintEvent)
|
void DetailsWidget::paintEvent(QPaintEvent *paintEvent)
|
||||||
{
|
{
|
||||||
//TL--> ___________ <-- TR
|
|
||||||
// | |
|
|
||||||
//ML-> ______________| <--MM | <--MR
|
|
||||||
// | |
|
|
||||||
//BL-> |_________________________| <-- BR
|
|
||||||
|
|
||||||
|
|
||||||
QWidget::paintEvent(paintEvent);
|
QWidget::paintEvent(paintEvent);
|
||||||
|
|
||||||
if (!m_detailsButton->isToggled())
|
|
||||||
return;
|
|
||||||
|
|
||||||
const QRect detailsGeometry = m_detailsButton->geometry();
|
|
||||||
const QRect widgetGeometry = m_widget ? m_widget->geometry() : QRect(x(), y() + height(), width(), 0);
|
|
||||||
|
|
||||||
QPoint tl(detailsGeometry.topLeft());
|
|
||||||
tl += QPoint(-3, -3);
|
|
||||||
|
|
||||||
QPoint tr(detailsGeometry.topRight());
|
|
||||||
tr += QPoint(3, -3);
|
|
||||||
|
|
||||||
QPoint mm(detailsGeometry.left() - 3, widgetGeometry.top() - 3);
|
|
||||||
|
|
||||||
QPoint ml(1, mm.y());
|
|
||||||
|
|
||||||
QPoint mr(tr.x(), mm.y());
|
|
||||||
|
|
||||||
int bottom = geometry().height() - 3;
|
|
||||||
QPoint bl(1, bottom);
|
|
||||||
QPoint br(tr.x(), bottom);
|
|
||||||
|
|
||||||
QPainter p(this);
|
QPainter p(this);
|
||||||
p.setRenderHint(QPainter::Antialiasing);
|
|
||||||
p.setPen(Qt::NoPen);
|
|
||||||
|
|
||||||
p.setBrush(palette().dark());
|
const QRect paintArea(m_summaryLabel->geometry().topLeft(),
|
||||||
p.drawRoundedRect(QRect(tl, br), 5, 5);
|
contentsRect().bottomRight());
|
||||||
p.drawRoundedRect(QRect(ml, br), 5, 5);
|
|
||||||
|
if (!isExpanded()) {
|
||||||
|
if (m_collapsedPixmap.isNull() ||
|
||||||
|
m_collapsedPixmap.size() != size())
|
||||||
|
m_collapsedPixmap = cacheBackground(paintArea.size(), false);
|
||||||
|
p.drawPixmap(paintArea, m_collapsedPixmap);
|
||||||
|
} else {
|
||||||
|
if (m_expandedPixmap.isNull() ||
|
||||||
|
m_expandedPixmap.size() != size())
|
||||||
|
m_expandedPixmap = cacheBackground(paintArea.size(), true);
|
||||||
|
p.drawPixmap(paintArea, m_expandedPixmap);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DetailsWidget::detailsButtonClicked()
|
void DetailsWidget::enterEvent(QEvent * event)
|
||||||
{
|
{
|
||||||
bool visible = m_detailsButton->isToggled();
|
QWidget::enterEvent(event);
|
||||||
if (m_widget)
|
changeHoverState(true);
|
||||||
m_widget->setVisible(visible);
|
}
|
||||||
m_dummyWidget->setVisible(visible);
|
|
||||||
fixUpLayout();
|
void DetailsWidget::leaveEvent(QEvent * event)
|
||||||
|
{
|
||||||
|
QWidget::leaveEvent(event);
|
||||||
|
changeHoverState(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DetailsWidget::setSummaryText(const QString &text)
|
void DetailsWidget::setSummaryText(const QString &text)
|
||||||
@@ -102,15 +95,21 @@ QString DetailsWidget::summaryText() const
|
|||||||
return m_summaryLabel->text();
|
return m_summaryLabel->text();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DetailsWidget::expanded() const
|
bool DetailsWidget::isExpanded() const
|
||||||
{
|
{
|
||||||
return m_detailsButton->isToggled();
|
if (!m_widget)
|
||||||
|
return false;
|
||||||
|
return m_widget->isVisible();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DetailsWidget::setExpanded(bool v)
|
void DetailsWidget::setExpanded(bool visible)
|
||||||
{
|
{
|
||||||
if (expanded() != v)
|
if (!m_widget)
|
||||||
m_detailsButton->animateClick();
|
return;
|
||||||
|
|
||||||
|
m_summaryLabel->setEnabled(!visible);
|
||||||
|
m_widget->setVisible(visible);
|
||||||
|
m_detailsButton->setChecked(visible);
|
||||||
}
|
}
|
||||||
|
|
||||||
QWidget *DetailsWidget::widget() const
|
QWidget *DetailsWidget::widget() const
|
||||||
@@ -122,31 +121,40 @@ void DetailsWidget::setWidget(QWidget *widget)
|
|||||||
{
|
{
|
||||||
if (m_widget == widget)
|
if (m_widget == widget)
|
||||||
return;
|
return;
|
||||||
if (m_widget) {
|
|
||||||
|
const bool wasExpanded(isExpanded());
|
||||||
|
|
||||||
|
if (m_widget)
|
||||||
m_grid->removeWidget(m_widget);
|
m_grid->removeWidget(m_widget);
|
||||||
m_widget = 0;
|
m_widget = widget;
|
||||||
}
|
|
||||||
if (widget) {
|
if (widget) {
|
||||||
m_grid->addWidget(widget, 1, 0, 1, 3);
|
m_widget->setContentsMargins(MARGIN, MARGIN, MARGIN, MARGIN);
|
||||||
m_widget = widget;
|
m_grid->addWidget(widget, 1, 1, 1, 2);
|
||||||
bool visible = m_detailsButton->isToggled();
|
setExpanded(wasExpanded);
|
||||||
m_widget->setVisible(visible);
|
} else {
|
||||||
m_dummyWidget->setVisible(visible);
|
m_detailsButton->setEnabled(false);
|
||||||
}
|
}
|
||||||
|
m_detailsButton->setEnabled(0 != m_widget);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DetailsWidget::setToolWidget(QWidget *widget)
|
void DetailsWidget::setToolWidget(QWidget *widget)
|
||||||
{
|
{
|
||||||
if (m_toolWidget == widget)
|
if (m_toolWidget == widget)
|
||||||
return;
|
return;
|
||||||
if (m_toolWidget) {
|
|
||||||
m_grid->removeWidget(m_toolWidget);
|
m_toolWidget = widget;
|
||||||
m_toolWidget = 0;
|
|
||||||
}
|
if (!m_toolWidget)
|
||||||
if (widget) {
|
return;
|
||||||
m_grid->addWidget(widget, 0, 1, 1, 1, Qt::AlignBottom);
|
|
||||||
m_toolWidget = widget;
|
m_toolWidget->adjustSize();
|
||||||
}
|
m_grid->addWidget(m_toolWidget, 0, 0, 1, 1, Qt::AlignCenter);
|
||||||
|
|
||||||
|
m_grid->setColumnMinimumWidth(0, m_toolWidget->width());
|
||||||
|
m_grid->setRowMinimumHeight(0, m_toolWidget->height());
|
||||||
|
|
||||||
|
changeHoverState(m_hovered);
|
||||||
}
|
}
|
||||||
|
|
||||||
QWidget *DetailsWidget::toolWidget() const
|
QWidget *DetailsWidget::toolWidget() const
|
||||||
@@ -154,25 +162,35 @@ QWidget *DetailsWidget::toolWidget() const
|
|||||||
return m_toolWidget;
|
return m_toolWidget;
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function works around a qt limitation.
|
QPixmap DetailsWidget::cacheBackground(const QSize &size, bool expanded)
|
||||||
// In a deeply nested widget structure, nested layouts
|
|
||||||
// tell their parents per a delayed invocation that they
|
|
||||||
// need to repaint. Thus hiding a widget triggers
|
|
||||||
// one relayout (and repaint) for each level of widget
|
|
||||||
// nesting. We circumvent that, by forcing a update()
|
|
||||||
// activate() on the widget after hiding.
|
|
||||||
void DetailsWidget::fixUpLayout()
|
|
||||||
{
|
{
|
||||||
if (!m_widget)
|
QLinearGradient lg;
|
||||||
return;
|
lg.setCoordinateMode(QGradient::ObjectBoundingMode);
|
||||||
QWidget *parent = m_widget;
|
lg.setFinalStop(0, 1);
|
||||||
QStack<QWidget *> widgets;
|
|
||||||
while((parent = parent->parentWidget()) && parent && parent->layout()) {
|
lg.setColorAt(0, palette().color(QPalette::Midlight));
|
||||||
widgets.push(parent);
|
lg.setColorAt(1, palette().color(QPalette::Button));
|
||||||
parent->layout()->update();
|
|
||||||
|
QPixmap pixmap(size);
|
||||||
|
QPainter p(&pixmap);
|
||||||
|
p.setBrush(lg);
|
||||||
|
p.setPen(QPen(palette().color(QPalette::Mid)));
|
||||||
|
|
||||||
|
p.drawRect(0, 0, size.width() - 1, size.height() - 1);
|
||||||
|
|
||||||
|
if (expanded) {
|
||||||
|
p.drawLine(0, m_summaryLabel->height(),
|
||||||
|
size.width(), m_summaryLabel->height());
|
||||||
}
|
}
|
||||||
|
|
||||||
while(!widgets.isEmpty()) {
|
return pixmap;
|
||||||
widgets.pop()->layout()->activate();
|
}
|
||||||
}
|
|
||||||
|
void DetailsWidget::changeHoverState(bool hovered)
|
||||||
|
{
|
||||||
|
m_hovered = hovered;
|
||||||
|
if (!m_toolWidget)
|
||||||
|
return;
|
||||||
|
|
||||||
|
m_toolWidget->setVisible(m_hovered);
|
||||||
}
|
}
|
||||||
|
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include "utils_global.h"
|
#include "utils_global.h"
|
||||||
|
|
||||||
|
#include <QtGui/QPixmap>
|
||||||
#include <QtGui/QWidget>
|
#include <QtGui/QWidget>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
@@ -17,7 +18,8 @@ class QTCREATOR_UTILS_EXPORT DetailsWidget : public QWidget
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_PROPERTY(QString summaryText READ summaryText WRITE setSummaryText DESIGNABLE true)
|
Q_PROPERTY(QString summaryText READ summaryText WRITE setSummaryText DESIGNABLE true)
|
||||||
Q_PROPERTY(bool expanded READ expanded WRITE setExpanded DESIGNABLE true)
|
Q_PROPERTY(bool expanded READ isExpanded WRITE setExpanded DESIGNABLE true)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DetailsWidget(QWidget *parent = 0);
|
DetailsWidget(QWidget *parent = 0);
|
||||||
~DetailsWidget();
|
~DetailsWidget();
|
||||||
@@ -25,8 +27,7 @@ public:
|
|||||||
void setSummaryText(const QString &text);
|
void setSummaryText(const QString &text);
|
||||||
QString summaryText() const;
|
QString summaryText() const;
|
||||||
|
|
||||||
bool expanded() const;
|
bool isExpanded() const;
|
||||||
void setExpanded(bool);
|
|
||||||
|
|
||||||
void setWidget(QWidget *widget);
|
void setWidget(QWidget *widget);
|
||||||
QWidget *widget() const;
|
QWidget *widget() const;
|
||||||
@@ -34,21 +35,28 @@ public:
|
|||||||
void setToolWidget(QWidget *widget);
|
void setToolWidget(QWidget *widget);
|
||||||
QWidget *toolWidget() const;
|
QWidget *toolWidget() const;
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void setExpanded(bool);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void paintEvent(QPaintEvent *paintEvent);
|
void paintEvent(QPaintEvent *paintEvent);
|
||||||
|
void enterEvent(QEvent *event);
|
||||||
private slots:
|
void leaveEvent(QEvent *event);
|
||||||
void detailsButtonClicked();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void fixUpLayout();
|
QPixmap cacheBackground(const QSize &size, bool expanded);
|
||||||
QLabel *m_summaryLabel;
|
void changeHoverState(bool hovered);
|
||||||
DetailsButton *m_detailsButton;
|
|
||||||
|
|
||||||
QWidget *m_widget;
|
DetailsButton *m_detailsButton;
|
||||||
QWidget *m_toolWidget;
|
|
||||||
QWidget *m_dummyWidget;
|
|
||||||
QGridLayout *m_grid;
|
QGridLayout *m_grid;
|
||||||
|
QLabel *m_summaryLabel;
|
||||||
|
QWidget *m_toolWidget;
|
||||||
|
QWidget *m_widget;
|
||||||
|
|
||||||
|
QPixmap m_collapsedPixmap;
|
||||||
|
QPixmap m_expandedPixmap;
|
||||||
|
|
||||||
|
bool m_hovered;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user