Skip to content
Snippets Groups Projects
Commit 491527ab authored by Tobias Hunger's avatar Tobias Hunger
Browse files

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
parent e9a20d33
No related branches found
No related tags found
No related merge requests found
#include "detailsbutton.h"
#include <QtGui/QPaintEvent>
#include <QtGui/QPainter>
using namespace Utils;
DetailsButton::DetailsButton(QWidget *parent)
#ifdef Q_OS_MAC
: QPushButton(parent),
#else
: QToolButton(parent),
#endif
m_checked(false)
DetailsButton::DetailsButton(QWidget *parent) : QAbstractButton(parent)
{
#ifdef Q_OS_MAC
setAttribute(Qt::WA_MacSmallSize);
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
#else
setCheckable(true);
#endif
setText(tr("Show Details"));
connect(this, SIGNAL(clicked()),
this, SLOT(onClicked()));
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
}
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;
}
#ifndef DETAILSBUTTON_H
#define DETAILSBUTTON_H
#include <QtGui/QPushButton>
#include <QtGui/QToolButton>
#include <QtGui/QAbstractButton>
#include <QtGui/QPixmap>
#include "utils_global.h"
namespace Utils {
class QTCREATOR_UTILS_EXPORT DetailsButton
#ifdef Q_OS_MAC
: public QPushButton
#else
: public QToolButton
#endif
class QTCREATOR_UTILS_EXPORT DetailsButton : public QAbstractButton
{
Q_OBJECT
public:
DetailsButton(QWidget *parent=0);
bool isToggled();
public slots:
void onClicked();
DetailsButton(QWidget *parent = 0);
QSize sizeHint() const;
protected:
void paintEvent(QPaintEvent *e);
private:
bool m_checked;
QPixmap cacheRendering(const QSize &size, bool checked);
QPixmap m_checkedPixmap;
QPixmap m_uncheckedPixmap;
};
}
#endif // DETAILSBUTTON_H
......@@ -38,7 +38,7 @@ DetailsWidget::DetailsWidget(QWidget *parent) :
m_grid->setContentsMargins(0, 0, 0, 0);
m_grid->setSpacing(0);
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);
......@@ -179,8 +179,8 @@ QPixmap DetailsWidget::cacheBackground(const QSize &size, bool expanded)
p.drawRect(0, 0, size.width() - 1, size.height() - 1);
if (expanded) {
p.drawLine(0, m_summaryLabel->height(),
size.width(), m_summaryLabel->height());
p.drawLine(0, m_summaryLabel->height() - 1,
m_summaryLabel->width(), m_summaryLabel->height() - 1);
}
return pixmap;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment