-less magic numbers on calculation of the position of IconAction

-don't paint IconAction when the icon is too tiny and there is not enough room

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=764014
This commit is contained in:
Marco Martin 2008-01-20 20:07:22 +00:00
parent 9d5697924d
commit 4b8a510837
2 changed files with 54 additions and 46 deletions

View File

@ -174,6 +174,11 @@ void IconAction::rebuildPixmap()
bool IconAction::event(QEvent::Type type, const QPointF &pos)
{
if (m_icon->size().width() < m_rect.width()*2.0 ||
m_icon->size().height() < m_rect.height()*2.0) {
return false;
}
switch (type) {
case QEvent::GraphicsSceneMousePress: {
setSelected(m_rect.contains(pos));
@ -230,6 +235,10 @@ QAction* IconAction::action() const
void IconAction::paint(QPainter *painter) const
{
if (m_icon->size().width() < m_rect.width()*2.0 ||
m_icon->size().height() < m_rect.height()*2.0) {
return;
}
painter->drawPixmap(m_rect.toRect(), Phase::self()->animationResult(m_animationId));
}
@ -295,22 +304,7 @@ void Icon::addAction(QAction *action)
d->cornerActions.append(iconAction);
connect(action, SIGNAL(destroyed(QObject*)), this, SLOT(actionDestroyed(QObject*)));
//TODO: fewer magic numbers, please =) 38, 32, 6, etc... needs to go.
// at the very least static const ints with nice names.
switch (count) {
case Private::TopLeft:
iconAction->setRect(QRectF(6, 6, 32, 32));
break;
case Private::TopRight:
iconAction->setRect(QRectF(size().width() - 38, 6, 32, 32));
break;
case Private::BottomLeft:
iconAction->setRect(QRectF(6, size().height() - 38, 32, 32));
break;
case Private::BottomRight:
iconAction->setRect(QRectF(size().width() - 38, size().height() - 38, 32, 32));
break;
}
iconAction->setRect(d->actionRect((Private::ActionPosition)count));
}
void Icon::actionDestroyed(QObject* action)
@ -470,24 +464,7 @@ void Icon::layoutIcons(const QStyleOptionGraphicsItem *option)
int count = 0;
foreach (IconAction* iconAction, d->cornerActions) {
//TODO: fewer magic numbers, please =) 38, 32, 6, etc... needs to go.
// at the very least static const ints with nice names.
switch (count) {
case Private::TopLeft:
// top left never changes, so don't bother setting it
//iconAction->setRect(QRectF(6, 6, 32, 32));
break;
case Private::TopRight:
iconAction->setRect(QRectF(d->currentSize.width() - 38, 6, 32, 32));
break;
case Private::BottomLeft:
iconAction->setRect(QRectF(6, d->currentSize.height() - 38, 32, 32));
break;
case Private::BottomRight:
iconAction->setRect(QRectF(d->currentSize.width() - 38, d->currentSize.height() - 38, 32, 32));
break;
}
iconAction->setRect(d->actionRect((Private::ActionPosition)count));
++count;
}

View File

@ -133,18 +133,6 @@ public:
inline Qt::LayoutDirection iconDirection(const QStyleOptionGraphicsItem *option) const;
// Margin functions
inline void setActiveMargins();
void setVerticalMargin(MarginType type, qreal left, qreal right, qreal top, qreal bottom);
void setHorizontalMargin(MarginType type, qreal left, qreal right, qreal top, qreal bottom);
inline void setVerticalMargin(MarginType type, qreal hor, qreal ver);
inline void setHorizontalMargin(MarginType type, qreal hor, qreal ver);
inline QRectF addMargin(const QRectF &rect, MarginType type) const;
inline QRectF subtractMargin(const QRectF &rect, MarginType type) const;
inline QSizeF addMargin(const QSizeF &size, MarginType type) const;
inline QSizeF subtractMargin(const QSizeF &size, MarginType type) const;
enum {
Minibutton = 64,
MinibuttonHover = 128,
@ -159,6 +147,20 @@ public:
LastIconPosition
};
// Margin functions
inline void setActiveMargins();
void setVerticalMargin(MarginType type, qreal left, qreal right, qreal top, qreal bottom);
void setHorizontalMargin(MarginType type, qreal left, qreal right, qreal top, qreal bottom);
inline void setVerticalMargin(MarginType type, qreal hor, qreal ver);
inline void setHorizontalMargin(MarginType type, qreal hor, qreal ver);
inline QRectF addMargin(const QRectF &rect, MarginType type) const;
inline QRectF subtractMargin(const QRectF &rect, MarginType type) const;
inline QSizeF addMargin(const QSizeF &size, MarginType type) const;
inline QSizeF subtractMargin(const QSizeF &size, MarginType type) const;
inline QRectF actionRect(ActionPosition position) const;
QString text;
QString infoText;
Svg *iconSvg;
@ -183,6 +185,8 @@ public:
Margin *activeMargins;
static const int maxDisplayLines = 5;
static const int iconActionSize = 26;
static const int iconActionMargin = 4;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(Icon::Private::IconStates)
@ -272,6 +276,33 @@ QSizeF Icon::Private::subtractMargin(const QSizeF &size, MarginType type) const
return QSizeF(size.width() - m.left - m.right, size.height() - m.top - m.bottom);
}
QRectF Icon::Private::actionRect(ActionPosition position) const
{
switch (position) {
case TopLeft:
return QRectF(iconActionMargin,
iconActionMargin,
iconActionSize,
iconActionSize);
case TopRight:
return QRectF(currentSize.width() - iconActionSize - iconActionMargin,
iconActionMargin,
iconActionSize,
iconActionSize);
case BottomLeft:
return QRectF(iconActionMargin,
currentSize.height() - iconActionSize - iconActionMargin,
iconActionSize,
iconActionSize);
//BottomRight
default:
return QRectF(currentSize.width() - iconActionSize - iconActionMargin,
currentSize.height() - iconActionSize - iconActionMargin,
iconActionSize,
iconActionSize);
}
}
} // Namespace
#endif