26 #include "kmultitabbar_p.h"
27 #include "kmultitabbar.moc"
28 #include "kmultitabbar_p.moc"
30 #include <QtGui/QActionEvent>
31 #include <QtGui/QLayout>
32 #include <QtGui/QPainter>
33 #include <QtGui/QFontMetrics>
34 #include <QtGui/QStyle>
35 #include <QStyleOptionButton>
39 #include <QtGui/QApplication>
49 class KMultiTabBarPrivate
52 class KMultiTabBarInternal *m_internal;
64 mainLayout=
new QVBoxLayout(
this);
66 mainLayout=
new QHBoxLayout(
this);
67 mainLayout->setMargin(0);
68 mainLayout->setSpacing(0);
69 mainLayout->addStretch();
70 setFrameStyle(NoFrame);
71 setBackgroundRole(QPalette::Background);
74 KMultiTabBarInternal::~KMultiTabBarInternal()
83 for (
int i=0;i<m_tabs.count();i++)
84 m_tabs.at(i)->setStyle(m_style);
89 void KMultiTabBarInternal::contentsMousePressEvent(QMouseEvent *ev)
94 void KMultiTabBarInternal::mousePressEvent(QMouseEvent *ev)
101 QListIterator<KMultiTabBarTab*> it(m_tabs);
102 while (it.hasNext()) {
104 if (tab->
id()==id)
return tab;
109 int KMultiTabBarInternal::appendTab(
const QPixmap &pic,
int id,
const QString& text)
112 m_tabs.append(tab=
new KMultiTabBarTab(pic,text,
id,
this,m_position,m_style));
115 mainLayout->insertWidget(m_tabs.size()-1, tab);
120 void KMultiTabBarInternal::removeTab(
int id)
122 for (
int pos=0;pos<m_tabs.count();pos++)
124 if (m_tabs.at(pos)->id()==id)
127 delete m_tabs.takeAt(pos);
136 for (
int i=0;i<m_tabs.count();i++)
146 :
QPushButton(QIcon(pic), text, parent), m_id(id), d(0)
153 setFocusPolicy(Qt::NoFocus);
162 QPushButton::setText(text);
177 QPushButton::hideEvent(he);
183 QPushButton::showEvent(he);
189 QStyleOptionButton opt;
192 opt.iconSize = iconSize();
194 opt.features = QStyleOptionButton::Flat;
195 QPainter painter(
this);
196 style()->drawControl(QStyle::CE_PushButton, &opt, &painter,
this);
202 KMultiTabBarTab::KMultiTabBarTab(
const QPixmap& pic,
const QString& text,
211 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
231 QPixmap KMultiTabBarTab::iconPixmap()
const
233 int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize, 0,
this);
234 return icon().pixmap(iconSize);
237 void KMultiTabBarTab::initStyleOption(QStyleOptionToolButton* opt)
const
242 if (!icon().isNull()) {
243 opt->iconSize = iconPixmap().size();
248 if (shouldDrawText())
252 opt->state |= QStyle::State_AutoRaise | QStyle::State_MouseOver | QStyle::State_Raised;
255 opt->state |= QStyle::State_Sunken | QStyle::State_On;
258 opt->toolButtonStyle = shouldDrawText() ? Qt::ToolButtonTextBesideIcon : Qt::ToolButtonIconOnly;
259 opt->subControls = QStyle::SC_ToolButton;
264 return computeSizeHint(shouldDrawText());
269 return computeSizeHint(
false);
272 void KMultiTabBarTab::computeMargins(
int* hMargin,
int* vMargin)
const
276 QStyleOptionToolButton opt;
277 initStyleOption(&opt);
279 QPixmap iconPix = iconPixmap();
280 QSize trialSize = iconPix.size();
281 QSize expandSize = style()->sizeFromContents(QStyle::CT_ToolButton, &opt, trialSize,
this);
283 *hMargin = (expandSize.width() - trialSize.width())/2;
284 *vMargin = (expandSize.height() - trialSize.height())/2;
287 QSize KMultiTabBarTab::computeSizeHint(
bool withText)
const
290 QStyleOptionToolButton opt;
291 initStyleOption(&opt);
293 int hMargin, vMargin;
294 computeMargins(&hMargin, &vMargin);
297 QPixmap iconPix = iconPixmap();
298 QSize size = iconPix.size();
302 QSize textSize = fontMetrics().size(0, text());
303 size.setHeight(qMax(size.height(), textSize.height()));
306 int majorMargin = isVertical() ? vMargin : hMargin;
307 int minorMargin = isVertical() ? hMargin : vMargin;
309 size.setWidth (size.width() + 2*majorMargin);
310 size.setHeight(size.height() + 2*minorMargin);
314 size.setWidth(size.width() + textSize.width() + majorMargin);
318 return QSize(size.height(), size.width());
325 setChecked(newState);
337 QPushButton::setIcon(icon);
340 bool KMultiTabBarTab::shouldDrawText()
const
345 bool KMultiTabBarTab::isVertical()
const
351 QPainter painter(
this);
353 QStyleOptionToolButton opt;
354 initStyleOption(&opt);
357 if (underMouse() || isChecked()) {
360 style()->drawComplexControl(QStyle::CC_ToolButton, &opt, &painter,
this);
363 int hMargin, vMargin;
364 computeMargins(&hMargin, &vMargin);
369 QPixmap icon = iconPixmap();
374 if (shouldDrawText()) {
376 iconRoom = icon.height() + 2*vMargin;
377 textRoom = height() - iconRoom - vMargin;
379 iconRoom = icon.width() + 2*hMargin;
380 textRoom = width() - iconRoom - hMargin;
383 t = painter.fontMetrics().elidedText(text(), Qt::ElideRight, textRoom);
387 if (t == QLatin1String(
"...") || t == QChar(0x2026))
394 if (!shouldDrawText()) {
395 style()->drawItemPixmap(&painter, rect(), Qt::AlignCenter | Qt::AlignVCenter, icon);
403 bool bottomIcon =
false;
404 bool rtl = layoutDirection() == Qt::RightToLeft;
415 labelArea =
QRect(0, vMargin, width(), textRoom);
416 iconArea =
QRect(0, vMargin + textRoom, width(), iconRoom);
418 labelArea =
QRect(0, iconRoom, width(), textRoom);
419 iconArea =
QRect(0, 0, width(), iconRoom);
424 labelArea =
QRect(hMargin, 0, textRoom, height());
425 iconArea =
QRect(hMargin + textRoom, 0, iconRoom, height());
427 labelArea =
QRect(iconRoom, 0, textRoom, height());
428 iconArea =
QRect(0, 0, iconRoom, height());
432 style()->drawItemPixmap(&painter, iconArea, Qt::AlignCenter | Qt::AlignVCenter, icon);
437 QRect labelPaintArea = labelArea;
442 labelPaintArea =
QRect(0, 0, labelArea.height(), labelArea.width());
447 tr.translate(labelArea.x(), labelPaintArea.width() + labelArea.y());
450 tr.translate(labelPaintArea.height() + labelArea.x(), labelArea.y());
453 painter.setTransform(tr);
456 style()->drawItemText(&painter, labelPaintArea, Qt::AlignLeading | Qt::AlignVCenter,
457 palette(),
true, t, QPalette::ButtonText);
465 d(new KMultiTabBarPrivate)
469 d->m_l=
new QVBoxLayout(
this);
470 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
474 d->m_l=
new QHBoxLayout(
this);
475 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
477 d->m_l->setMargin(0);
478 d->m_l->setSpacing(0);
480 d->m_internal=
new KMultiTabBarInternal(
this, pos);
483 d->m_l->insertWidget(0,d->m_internal);
484 d->m_l->insertWidget(0,d->m_btnTabSep=
new QFrame(
this));
485 d->m_btnTabSep->setFixedHeight(4);
486 d->m_btnTabSep->setFrameStyle(QFrame::Panel | QFrame::Sunken);
487 d->m_btnTabSep->setLineWidth(2);
488 d->m_btnTabSep->hide();
495 qDeleteAll( d->m_buttons );
496 d->m_buttons.clear();
504 btn->setFixedWidth(btn->height());
506 d->m_buttons.append(btn);
507 d->m_l->insertWidget(0,btn);
509 d->m_btnTabSep->show();
515 QListIterator<KMultiTabBarButton*> it(d->m_buttons);
516 while (it.hasNext()){
517 if (it.next()->isVisibleTo(
this)) {
523 d->m_btnTabSep->hide();
525 d->m_btnTabSep->show();
530 d->m_internal->appendTab(pic,
id,text);
536 QListIterator<KMultiTabBarButton*> it(d->m_buttons);
537 while ( it.hasNext() ) {
539 if ( button->
id() == id )
548 return d->m_internal->tab(
id);
553 for (
int pos=0;pos<d->m_buttons.count();pos++)
555 if (d->m_buttons.at(pos)->id()==id)
557 d->m_buttons.takeAt(pos)->deleteLater();
562 if (d->m_buttons.count()==0)
563 d->m_btnTabSep->hide();
568 d->m_internal->removeTab(
id);
582 return ttab->isChecked();
589 d->m_internal->setStyle(style);
594 return d->m_internal->m_style;
600 d->m_internal->setPosition(pos);
605 return d->m_position;
class KMultiTabBarTab * tab(int id) const
get a pointer to a tab within the tab area, identiifed by its ID
virtual ~KMultiTabBarTab()
class KMultiTabBarButton * button(int id) const
get a pointer to a button within the button area identified by its ID
friend class KMultiTabBarButton
bool isTabRaised(int id) const
return the state of a tab, identified by its ID
void setPosition(KMultiTabBar::KMultiTabBarPosition)
this is used internaly, but can be used by the user, if (s)he wants to It the according call of KMult...
Use KMultiTabBar::appendTab to append a tab, which creates a KMultiTabBarTab instance.
KMultiTabBarStyle tabStyle() const
get the display style of the tabs
void setPosition(KMultiTabBarPosition pos)
set the real position of the widget.
A Widget for horizontal and vertical tabs.
virtual void paintEvent(QPaintEvent *)
int appendButton(const QPixmap &pic, int id=-1, QMenu *popup=0, const QString ¬_used_yet=QString())
append a new button to the button area.
void setIcon(const QString &)
KMultiTabBarPosition position() const
get the tabbar position.
int appendTab(const QPixmap &pic, int id=-1, const QString &text=QString())
append a new tab to the tab area.
void setState(bool state)
set the active state of the tab
virtual QSize sizeHint() const
void setStyle(KMultiTabBar::KMultiTabBarStyle)
this is used internaly, but can be used by the user, if (s)he wants to It the according call of KMult...
KMultiTabBar(KMultiTabBarPosition pos, QWidget *parent=0)
void removeTab(int id)
remove a tab with a given ID
QPixmap SmallIcon(const QString &name, int force_size, int state, const QStringList &overlays)
KMultiTabBarStyle
The list of available styles for KMultiTabBar.
virtual QSize minimumSizeHint() const
void setTab(int id, bool state)
set a tab to "raised"
void setStyle(KMultiTabBarStyle style)
set the display style of the tabs
void removeButton(int id)
remove a button with the given ID
virtual void fontChange(const QFont &)