2 #include <QActionGroup> 5 #include <QStylePainter> 6 #include <QStyleOptionToolButton> 12 static const int ToolButtonWidth = 200;
13 static const int ToolButtonHeight = 54;
14 static const int ToolButtonIconSize = 32;
15 static const int MarginLeft = 0;
16 static const int MarginRight = 0;
17 static const int MarginTop = 0;
18 static const int MarginBottom = 8;
19 static const int ButtonSpacing = 2;
20 static const int SubNavPaddingRight = 40;
36 QStylePainter sp(
this );
37 QStyleOptionToolButton opt;
38 initStyleOption( &opt );
42 const QString strText = opt.text;
47 sp.drawComplexControl( QStyle::CC_ToolButton, opt );
59 sp.drawComplexControl( QStyle::CC_ToolButton, opt );
63 void drawLabel(
const QStyleOption *opt, QPainter *p)
65 if (
const QStyleOptionToolButton *toolbutton
66 = qstyleoption_cast<const QStyleOptionToolButton *>(opt)) {
70 if(!(toolbutton->state & QStyle::State_Enabled))
74 else if(toolbutton->state & (QStyle::State_Sunken | QStyle::State_On))
78 else if(toolbutton->state & QStyle::State_MouseOver)
88 QRect rect = toolbutton->rect;
89 int w = rect.width() - SubNavPaddingRight;
94 if (toolbutton->state & (QStyle::State_Sunken | QStyle::State_On)) {
95 shiftX = style()->pixelMetric(QStyle::PM_ButtonShiftHorizontal, toolbutton,
this);
96 shiftY = style()->pixelMetric(QStyle::PM_ButtonShiftVertical, toolbutton,
this);
100 if (!toolbutton->text.isEmpty()) {
101 int alignment = Qt::AlignRight | Qt::AlignVCenter | Qt::TextShowMnemonic;
102 rect.translate(shiftX, shiftY);
103 p->setFont(toolbutton->font);
105 p->drawText(rect, alignment, toolbutton->text);
113 if((toolbutton.state & QStyle::State_Enabled) &&
114 !(toolbutton.state & QStyle::State_On) &&
115 (toolbutton.state & QStyle::State_MouseOver))
119 QIcon icon = toolbutton.icon;
120 QPixmap pixmap = icon.pixmap(toolbutton.iconSize, QIcon::Selected, QIcon::On);
121 m_hoverIcon = QIcon(pixmap);
124 toolbutton.icon = m_hoverIcon;
136 m_toolStyle(Qt::ToolButtonTextBesideIcon),
151 QAction* action =
new QAction(icon, text,
this);
159 QAction* action =
new QAction(text,
this);
173 QActionGroup* actionGroup =
new QActionGroup(
this);
174 actionGroup->setExclusive(
true);
175 QVBoxLayout* vboxLayout =
new QVBoxLayout(
this);
176 int defButtonWidth = ToolButtonWidth;
177 vboxLayout->setContentsMargins(
m_subBar ? 0 : MarginLeft,
181 vboxLayout->setSpacing(
m_subBar ? 0 : ButtonSpacing);
184 for(
int i = 0; i <
m_actions.count(); i++)
188 action->setActionGroup(actionGroup);
189 action->setCheckable(
true);
191 toolButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
193 toolButton->setDefaultAction(action);
194 toolButton->setIconSize(QSize(ToolButtonIconSize, ToolButtonIconSize));
207 QVBoxLayout* vboxLayout2 =
new QVBoxLayout();
208 vboxLayout->addLayout(vboxLayout2);
209 vboxLayout2->addWidget(toolButton);
210 vboxLayout2->setSpacing(0);
217 QList<QAction*> group =
m_groups[action];
220 for(
int j = 0; j < group.count(); j++)
224 vboxLayout2->addWidget(subNavBar);
226 connect(action, SIGNAL(toggled(
bool)), subNavBar, SLOT(
onSubBarClick(
bool)));
231 vboxLayout->addWidget(toolButton);
242 setMinimumWidth(defButtonWidth + MarginLeft + MarginRight);
243 vboxLayout->addStretch(1);
260 bool haveChecked =
false;
261 for(
int i = 0; i <
m_actions.count(); i++)
264 if(action->isChecked())
286 QWidget::resizeEvent(evt);
#define SetObjectStyleSheet(object, name)
Qt::ToolButtonStyle m_toolStyle
QList< QAction * > m_actions
void setToolButtonStyle(Qt::ToolButtonStyle toolButtonStyle)
setToolButtonStyle Set the style for the tool buttons
void resized(const QSize &)
resized Signal that the size is changed
void buildUi()
buildUi Construct the layout of the composite GUI control
void addAction(QAction *action)
addAction Add action to the navigation bar
void setSubBar(bool subBar)
setSubBar Set the component be sub-navigation bar
void onSubBarClick(bool)
onSubBarClick Activate the sub-navigation bar
QMap< QAction *, QList< QAction * > > m_groups
The NavigationBar class Custom control for navigation bar.
void mapGroup(QAction *action, QList< QAction * > list)
mapGroup Map the action with group
QAction * addGroup(QList< QAction * > list, const QIcon &icon, const QString &text)
addGroup Add group of actions
uint8_t const size_t const size
NavigationBar(QWidget *parent=0)
NavigationBar Constructor.
void resizeEvent(QResizeEvent *evt)