1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
/***************************************************************************
* *
* This file is part of the Fotowall project, *
* http://www.enricoros.com/opensource/fotowall *
* *
* Copyright (C) 2009 by Enrico Ros <enrico.ros@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "GroupBoxWidget.h"
#include <QLayout>
#include <QFontMetrics>
#include <QPainter>
#include <QStyleOptionButton>
#include <QStyle>
#include <QTimer>
#include <QVariant>
#if QT_VERSION >= 0x040600
#include <QPropertyAnimation>
#define ANIMATE_PARAM(propName, duration, endValue, finalizeLayout) \
{QPropertyAnimation * ani = new QPropertyAnimation(this, propName, this); \
if (finalizeLayout) connect(ani, SIGNAL(finished()), this, SLOT(slotFinalizeDesign())); \
ani->setEasingCurve(QEasingCurve::OutQuint); \
ani->setDuration(duration); \
ani->setEndValue(endValue); \
ani->start(QPropertyAnimation::DeleteWhenStopped);}
#else
#define ANIMATE_PARAM(propName, duration, endValue, finalizeLayout) \
{setProperty(propName, endValue); \
if (finalizeLayout) slotFinalizeDesign();}
#endif
GroupBoxWidget::GroupBoxWidget(QWidget * parent)
: QWidget(parent)
, m_redesignTimer(0)
, m_collapsed(false)
, m_checkable(false)
, m_checked(true)
, m_borderFlags(0)
, m_checkValue(1.0)
, m_hoverValue(0.0)
{
// setup groupbox text
m_titleFont = font();
m_titleFont.setPixelSize(10);
// using a fixed HSizePolicy we better integrate with auto-layouting
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
// hide junk before initial layouting
hide();
}
QString GroupBoxWidget::title() const
{
return m_titleText;
}
void GroupBoxWidget::setTitle(const QString & title)
{
m_titleText = title;
updateDesign();
}
int GroupBoxWidget::titleSize() const
{
return m_titleFont.pixelSize();
}
void GroupBoxWidget::setTitleSize(int titleSize)
{
m_titleFont.setPointSize(titleSize);
updateDesign();
}
bool GroupBoxWidget::isCheckable() const
{
return m_checkable;
}
void GroupBoxWidget::setCheckable(bool checkable)
{
if (m_checkable == checkable)
return;
m_checkable = checkable;
updateDesign();
}
bool GroupBoxWidget::isChecked() const
{
return m_checkable ? m_checked : false;
}
void GroupBoxWidget::setChecked(bool checked)
{
if (!m_checkable || m_checked == checked)
return;
m_checked = checked;
emit toggled(checked);
updateDesign();
}
int GroupBoxWidget::borderFlags() const
{
return m_borderFlags;
}
void GroupBoxWidget::setBorderFlags(int flags)
{
m_borderFlags = flags;
update();
}
void GroupBoxWidget::collapse()
{
if (!m_collapsed) {
m_collapsed = true;
updateDesign();
}
}
void GroupBoxWidget::expand()
{
if (m_collapsed) {
m_collapsed = false;
updateDesign();
}
}
void GroupBoxWidget::enterEvent(QEvent *)
{
ANIMATE_PARAM("hAnim", 400, 1.0, false);
}
void GroupBoxWidget::leaveEvent(QEvent *)
{
ANIMATE_PARAM("hAnim", 400, 0.0, false);
}
void GroupBoxWidget::mousePressEvent(QMouseEvent * /*event*/)
{
setChecked(!isChecked());
}
void GroupBoxWidget::paintEvent(QPaintEvent * /*event*/)
{
// skip the rest of the painting if no text
if (m_titleText.isEmpty())
return;
// draw hovering
QPainter p(this);
if (m_hoverValue > 0 && (m_checkValue == 0.0 || m_checkValue == 1.0)) {
QRadialGradient rg = m_checkValue == 1.0 ? QRadialGradient(0.5, 0.2, 0.8) : QRadialGradient(0.5, 1.0, 1.5);
QColor startColor(Qt::white);
startColor.setAlpha((int)(255.0 * m_hoverValue));
rg.setColorAt(0.0, startColor);
rg.setColorAt(1.0, Qt::transparent);
rg.setColorAt(0.0, startColor);
rg.setColorAt(1.0, Qt::transparent);
rg.setCoordinateMode(QGradient::ObjectBoundingMode);
p.fillRect(0, 0, width(), height() - m_checkValue * (height() - 12) , rg);
}
// draw left/right lines
if (m_borderFlags & 0x0001)
p.fillRect(0, 0, 1, height(), QColor(230, 230, 230));
if (m_borderFlags & 0x0002)
p.fillRect(width() - 1, 0, 1, height(), Qt::white);
// precalc text position and move painter
QStyle * ss = m_checkable ? style() : 0;
int indW = ss ? ss->pixelMetric(QStyle::PM_IndicatorWidth, 0, 0) : 0;
int indH = ss ? ss->pixelMetric(QStyle::PM_IndicatorHeight, 0, 0) : 0;
p.save();
p.setFont(m_titleFont);
QFontMetrics metrics(m_titleFont);
QRect textRect = metrics.boundingRect(m_titleText);
// int textHeight = textRect.height();
int dx = 0;
if (m_checkValue < 1.0) {
qreal x1 = -textRect.top() + 2,
x2 = (width() - textRect.width() - indW - 4) / 2;
qreal y1 = height() - 2, //height() + textRect.width()) / 2,
y2 = -textRect.top();
p.translate(x1 + m_checkValue * (x2 - x1), y1 + m_checkValue * (y2 - y1));
p.rotate(m_checkValue * 90 - 90);
} else
p.translate((width() - textRect.width() - indW - 4) / 2, -textRect.top());
// draw checkbox indicator
if (m_checkable && indW && indH) {
QStyleOptionButton opt;
opt.state = QStyle::State_Enabled;
int styleOffset = (textRect.height() - indH) / 2;
opt.rect = QRect(0, -indH + 4 - styleOffset, indW, indH);
dx = indW + 4;
opt.state |= m_checked ? QStyle::State_On : QStyle::State_Off;
if (underMouse())
opt.state |= QStyle::State_MouseOver;
//p.setRenderHint(QPainter::Antialiasing, true);
style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &opt, &p, this);
}
// draw text
p.drawText(dx, 0, m_titleText);
p.restore();
}
qreal GroupBoxWidget::checkValue() const
{
return m_checkValue;
}
void GroupBoxWidget::setCheckValue(qreal value)
{
m_checkValue = value;
update();
}
qreal GroupBoxWidget::hoverValue() const
{
return m_hoverValue;
}
void GroupBoxWidget::setHoverValue(qreal value)
{
m_hoverValue = value;
update();
}
int GroupBoxWidget::calcMinWidth() const
{
return qMax(QFontMetrics(m_titleFont).width(m_titleText) + 12, QWidget::sizeHint().width());
}
void GroupBoxWidget::updateDesign()
{
if (m_collapsed) {
// full collapse: no pre-work
} else if (m_checkable && !m_checked) {
// checkable off: label on left
setContentsMargins(m_titleFont.pixelSize() + 8, 0, 0, 0);
setCursor(Qt::PointingHandCursor);
} else {
// normal: label on top
setContentsMargins(0, m_titleFont.pixelSize() + 8, 0, 0);
setCursor(Qt::ArrowCursor);
}
// defer and accumulate layout calculations
if (!m_redesignTimer) {
m_redesignTimer = new QTimer(this);
m_redesignTimer->setSingleShot(true);
connect(m_redesignTimer, SIGNAL(timeout()), SLOT(slotAnimateDesign()));
}
m_redesignTimer->start();
}
void GroupBoxWidget::slotAnimateDesign()
{
if (m_collapsed) {
// full collapse: shrink to zero
ANIMATE_PARAM("fixedWidth", 200, 0, true)
} else if (m_checkable && !m_checked) {
// checkable off: shrink to the target width
ANIMATE_PARAM("fixedWidth", 200, m_titleFont.pixelSize() + 8, true);
ANIMATE_PARAM("cAnim", 200, 0.0, false);
} else {
// normal: expand to the full size
ANIMATE_PARAM("fixedWidth", 300, calcMinWidth(), true);
ANIMATE_PARAM("cAnim", 200, 1.0, false);
}
// it was hidden in the constructor for not showing junk before initial layouting
show();
}
void GroupBoxWidget::slotFinalizeDesign()
{
if (m_collapsed) {
// full collapse: zero width
setFixedWidth(0);
} else if (m_checkable && !m_checked) {
// checkable off: left label
setFixedWidth(m_titleFont.pixelSize() + 8);
setCheckValue(0.0);
} else {
// normal: layout driven
setMinimumWidth(calcMinWidth());
setMaximumWidth(QWIDGETSIZE_MAX);
setCheckValue(1.0);
}
}
|