forked from qt-creator/qt-creator
Use a custom details button
* Use a custom details button rendering. This should work better on the mac ans looks nicer, too:-) Reviewed-by: Roberto Raggi
This commit is contained in:
@@ -1,32 +1,75 @@
|
|||||||
#include "detailsbutton.h"
|
#include "detailsbutton.h"
|
||||||
|
|
||||||
|
#include <QtGui/QPaintEvent>
|
||||||
|
#include <QtGui/QPainter>
|
||||||
|
|
||||||
using namespace Utils;
|
using namespace Utils;
|
||||||
|
|
||||||
DetailsButton::DetailsButton(QWidget *parent)
|
DetailsButton::DetailsButton(QWidget *parent) : QAbstractButton(parent)
|
||||||
#ifdef Q_OS_MAC
|
|
||||||
: QPushButton(parent),
|
|
||||||
#else
|
|
||||||
: QToolButton(parent),
|
|
||||||
#endif
|
|
||||||
m_checked(false)
|
|
||||||
{
|
{
|
||||||
#ifdef Q_OS_MAC
|
|
||||||
setAttribute(Qt::WA_MacSmallSize);
|
|
||||||
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
|
||||||
#else
|
|
||||||
setCheckable(true);
|
setCheckable(true);
|
||||||
#endif
|
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
|
||||||
setText(tr("Show Details"));
|
|
||||||
connect(this, SIGNAL(clicked()),
|
|
||||||
this, SLOT(onClicked()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DetailsButton::onClicked()
|
QSize DetailsButton::sizeHint() const
|
||||||
{
|
{
|
||||||
m_checked = !m_checked;
|
// TODO: Adjust this when icons become available!
|
||||||
|
return QSize(40, 22);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DetailsButton::isToggled()
|
|
||||||
|
void DetailsButton::paintEvent(QPaintEvent *e)
|
||||||
{
|
{
|
||||||
return m_checked;
|
QWidget::paintEvent(e);
|
||||||
|
|
||||||
|
QPainter p(this);
|
||||||
|
if (isChecked()) {
|
||||||
|
if (m_checkedPixmap.isNull() || m_checkedPixmap.size() != contentsRect().size())
|
||||||
|
m_checkedPixmap = cacheRendering(contentsRect().size(), true);
|
||||||
|
p.drawPixmap(contentsRect(), m_checkedPixmap);
|
||||||
|
} else {
|
||||||
|
if (m_uncheckedPixmap.isNull() || m_uncheckedPixmap.size() != contentsRect().size())
|
||||||
|
m_uncheckedPixmap = cacheRendering(contentsRect().size(), false);
|
||||||
|
p.drawPixmap(contentsRect(), m_uncheckedPixmap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QPixmap DetailsButton::cacheRendering(const QSize &size, bool checked)
|
||||||
|
{
|
||||||
|
QLinearGradient lg;
|
||||||
|
lg.setCoordinateMode(QGradient::ObjectBoundingMode);
|
||||||
|
lg.setFinalStop(0, 1);
|
||||||
|
|
||||||
|
if (checked) {
|
||||||
|
lg.setColorAt(0, palette().color(QPalette::Midlight));
|
||||||
|
lg.setColorAt(1, palette().color(QPalette::Button));
|
||||||
|
} else {
|
||||||
|
lg.setColorAt(0, palette().color(QPalette::Button));
|
||||||
|
lg.setColorAt(1, palette().color(QPalette::Midlight));
|
||||||
|
}
|
||||||
|
|
||||||
|
QPixmap pixmap(size);
|
||||||
|
QPainter p(&pixmap);
|
||||||
|
p.setBrush(lg);
|
||||||
|
p.setPen(Qt::NoPen);
|
||||||
|
|
||||||
|
p.drawRect(0, 0, size.width() - 1, size.height() - 1);
|
||||||
|
|
||||||
|
p.setPen(QPen(palette().color(QPalette::Mid)));
|
||||||
|
p.drawLine(0, size.height() - 1, 0, 0);
|
||||||
|
p.drawLine(0, 0, size.width() - 1, 0);
|
||||||
|
p.drawLine(size.width() - 1, 0, size.width() - 1, size.height() - 1);
|
||||||
|
if (!checked)
|
||||||
|
p.drawLine(size.width() - 1, size.height() - 1, 0, size.height() - 1);
|
||||||
|
|
||||||
|
p.setPen(palette().color(QPalette::Text));
|
||||||
|
|
||||||
|
// TODO: This should actually use some icons instead...
|
||||||
|
if (checked) {
|
||||||
|
p.drawText(0, 0, size.width(), size.height(), Qt::AlignCenter, tr("Less"));
|
||||||
|
} else {
|
||||||
|
p.drawText(0, 0, size.width(), size.height(), Qt::AlignCenter, tr("More"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return pixmap;
|
||||||
}
|
}
|
||||||
|
@@ -1,28 +1,28 @@
|
|||||||
#ifndef DETAILSBUTTON_H
|
#ifndef DETAILSBUTTON_H
|
||||||
#define DETAILSBUTTON_H
|
#define DETAILSBUTTON_H
|
||||||
|
|
||||||
#include <QtGui/QPushButton>
|
#include <QtGui/QAbstractButton>
|
||||||
#include <QtGui/QToolButton>
|
#include <QtGui/QPixmap>
|
||||||
|
|
||||||
#include "utils_global.h"
|
#include "utils_global.h"
|
||||||
|
|
||||||
namespace Utils {
|
namespace Utils {
|
||||||
|
|
||||||
class QTCREATOR_UTILS_EXPORT DetailsButton
|
class QTCREATOR_UTILS_EXPORT DetailsButton : public QAbstractButton
|
||||||
#ifdef Q_OS_MAC
|
|
||||||
: public QPushButton
|
|
||||||
#else
|
|
||||||
: public QToolButton
|
|
||||||
#endif
|
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
DetailsButton(QWidget *parent=0);
|
DetailsButton(QWidget *parent = 0);
|
||||||
bool isToggled();
|
|
||||||
public slots:
|
QSize sizeHint() const;
|
||||||
void onClicked();
|
|
||||||
|
protected:
|
||||||
|
void paintEvent(QPaintEvent *e);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_checked;
|
QPixmap cacheRendering(const QSize &size, bool checked);
|
||||||
|
QPixmap m_checkedPixmap;
|
||||||
|
QPixmap m_uncheckedPixmap;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif // DETAILSBUTTON_H
|
#endif // DETAILSBUTTON_H
|
||||||
|
@@ -38,7 +38,7 @@ DetailsWidget::DetailsWidget(QWidget *parent) :
|
|||||||
m_grid->setContentsMargins(0, 0, 0, 0);
|
m_grid->setContentsMargins(0, 0, 0, 0);
|
||||||
m_grid->setSpacing(0);
|
m_grid->setSpacing(0);
|
||||||
m_grid->addWidget(m_summaryLabel, 0, 1);
|
m_grid->addWidget(m_summaryLabel, 0, 1);
|
||||||
m_grid->addWidget(m_detailsButton, 0, 2, 1, 1, Qt::AlignCenter);
|
m_grid->addWidget(m_detailsButton, 0, 2);
|
||||||
|
|
||||||
m_detailsButton->setEnabled(false);
|
m_detailsButton->setEnabled(false);
|
||||||
|
|
||||||
@@ -179,8 +179,8 @@ QPixmap DetailsWidget::cacheBackground(const QSize &size, bool expanded)
|
|||||||
p.drawRect(0, 0, size.width() - 1, size.height() - 1);
|
p.drawRect(0, 0, size.width() - 1, size.height() - 1);
|
||||||
|
|
||||||
if (expanded) {
|
if (expanded) {
|
||||||
p.drawLine(0, m_summaryLabel->height(),
|
p.drawLine(0, m_summaryLabel->height() - 1,
|
||||||
size.width(), m_summaryLabel->height());
|
m_summaryLabel->width(), m_summaryLabel->height() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return pixmap;
|
return pixmap;
|
||||||
|
Reference in New Issue
Block a user