2
This file is part of the KDE project
3
Copyright (C) 2008 David Faure <faure@kde.org>
4
Copyright (C) 2009 Christoph Feck <christoph@maxiom.de>
6
This program is free software; you can redistribute it and/or
7
modify it under the terms of the GNU General Public
8
License as published by the Free Software Foundation; either
9
version 2 of the License, or (at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; see the file COPYING. If not, write to
18
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
Boston, MA 02110-1301, USA.
22
#include "konqanimatedlogo_p.h"
24
#include <KDE/KIconLoader>
26
#include <QtCore/QEvent>
27
#include <QtGui/QMenuBar>
28
#include <QtGui/QStyle>
29
#include <QtGui/QStyleOptionMenuItem>
31
KonqAnimatedLogo::KonqAnimatedLogo(QWidget *parent)
32
: KAnimatedButton(parent)
35
setFocusPolicy(Qt::NoFocus);
36
setToolButtonStyle(Qt::ToolButtonIconOnly);
37
setAnimatedLogoSize(maxThrobberHeight());
38
if (qobject_cast<QMenuBar *>(parent))
39
parent->installEventFilter(this);
42
KonqAnimatedLogo::~KonqAnimatedLogo()
45
parentWidget()->removeEventFilter(this);
48
QSize KonqAnimatedLogo::sizeHint() const
53
void KonqAnimatedLogo::changeEvent(QEvent *event)
55
KAnimatedButton::changeEvent(event);
56
if (event->type() == QEvent::ParentAboutToChange) {
58
parentWidget()->removeEventFilter(this);
59
} else if (event->type() == QEvent::ParentChange) {
60
if (qobject_cast<QMenuBar *>(parentWidget()))
61
parentWidget()->installEventFilter(this);
65
bool KonqAnimatedLogo::eventFilter(QObject *watched, QEvent *event)
67
if (qobject_cast<QWidget *>(watched) == parentWidget()) {
68
if (event->type() == QEvent::StyleChange || event->type() == QEvent::FontChange
69
|| event->type() == QEvent::ApplicationFontChange) {
70
// make sure the logo is resized before the menu bar gets the
72
setAnimatedLogoSize(maxThrobberHeight());
75
return KAnimatedButton::eventFilter(watched, event);
78
int KonqAnimatedLogo::maxThrobberHeight()
80
QMenuBar *menuBar = qobject_cast<QMenuBar *>(parentWidget());
84
// This comes from QMenuBar::sizeHint and QMenuBarPrivate::calcActionRects
85
const QFontMetrics fm = menuBar->fontMetrics();
86
QSize sz(100, fm.height());
87
//let the style modify the above size..
88
QStyleOptionMenuItem opt;
90
opt.state = QStyle::State_Enabled;
91
opt.menuRect = menuBar->rect();
93
sz = menuBar->style()->sizeFromContents(QStyle::CT_MenuBarItem, &opt, sz, menuBar);
94
//kDebug() << "maxThrobberHeight=" << sz.height();
98
void KonqAnimatedLogo::setAnimatedLogoSize(int buttonHeight)
100
// This gives the best results: we force a bigger icon size onto the style, and it'll just have to eat up its margin.
101
// So we don't need to ask sizeFromContents at all.
102
int iconSize = buttonHeight - 4;
104
QStyleOptionToolButton opt;
105
opt.initFrom(m_paAnimatedLogo);
106
const QSize finalSize = style()->sizeFromContents(QStyle::CT_ToolButton, &opt, opt.iconSize, m_paAnimatedLogo);
107
//kDebug() << "throbberIconSize=" << buttonHeight << "-" << finalSize.height() - opt.iconSize.height();
108
int iconSize = buttonHeight - (finalSize.height() - opt.iconSize.height());
111
m_size = QSize(buttonHeight, buttonHeight);
112
setFixedSize(m_size);
114
//kDebug() << "buttonHeight=" << buttonHeight << "max iconSize=" << iconSize;
115
if ( iconSize < KIconLoader::SizeSmallMedium )
116
iconSize = KIconLoader::SizeSmall;
117
else if ( iconSize < KIconLoader::SizeMedium )
118
iconSize = KIconLoader::SizeSmallMedium;
119
else if ( iconSize < KIconLoader::SizeLarge )
120
iconSize = KIconLoader::SizeMedium ;
122
iconSize = KIconLoader::SizeLarge;
123
//kDebug() << "final iconSize=" << iconSize;
124
if (iconDimensions() != iconSize) {
125
setIconSize(QSize(iconSize, iconSize));
126
if (!icons().isEmpty()) {
132
#include "konqanimatedlogo_p.moc"