Fabcoin Core  0.16.2
P2P Digital Currency
navigationbar.cpp
Go to the documentation of this file.
1 #include <navigationbar.h>
2 #include <QActionGroup>
3 #include <QToolButton>
4 #include <QLayout>
5 #include <QStylePainter>
6 #include <QStyleOptionToolButton>
7 #include <QStyle>
8 #include <styleSheet.h>
9 
11 {
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;
21 }
22 using namespace NavigationBar_NS;
23 
24 class NavToolButton : public QToolButton
25 {
26 public:
27  explicit NavToolButton(QWidget * parent, bool subBar):
28  QToolButton(parent),
29  m_subBar(subBar),
30  m_iconCached(false)
31  {}
32 
33 protected:
34  void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE
35  {
36  QStylePainter sp( this );
37  QStyleOptionToolButton opt;
38  initStyleOption( &opt );
39 
40  if(m_subBar)
41  {
42  const QString strText = opt.text;
43 
44  //draw background
45  opt.text.clear();
46  opt.icon = QIcon();
47  sp.drawComplexControl( QStyle::CC_ToolButton, opt );
48  opt.text = strText;
49 
50  //draw label
51  drawLabel(&opt, &sp);
52  }
53  else
54  {
55  //update icon
56  updateIcon(opt);
57 
58  //draw control
59  sp.drawComplexControl( QStyle::CC_ToolButton, opt );
60  }
61  }
62 
63  void drawLabel(const QStyleOption *opt, QPainter *p)
64  {
65  if (const QStyleOptionToolButton *toolbutton
66  = qstyleoption_cast<const QStyleOptionToolButton *>(opt)) {
67 
68  // Choose color
69  QColor color;
70  if(!(toolbutton->state & QStyle::State_Enabled))
71  {
72  color = 0x1a96ce;
73  }
74  else if(toolbutton->state & (QStyle::State_Sunken | QStyle::State_On))
75  {
76  color = 0xe5f3f9;
77  }
78  else if(toolbutton->state & QStyle::State_MouseOver)
79  {
80  color = 0xb3dcef;
81  }
82  else
83  {
84  color = 0x7fc4e3;
85  }
86 
87  // Determine area
88  QRect rect = toolbutton->rect;
89  int w = rect.width() - SubNavPaddingRight;
90  w = qMax(w, 0);
91  rect.setWidth(w);
92  int shiftX = 0;
93  int shiftY = 0;
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);
97  }
98 
99  // Draw text
100  if (!toolbutton->text.isEmpty()) {
101  int alignment = Qt::AlignRight | Qt::AlignVCenter | Qt::TextShowMnemonic;
102  rect.translate(shiftX, shiftY);
103  p->setFont(toolbutton->font);
104  p->setPen(color);
105  p->drawText(rect, alignment, toolbutton->text);
106  }
107  }
108  }
109 
110  void updateIcon(QStyleOptionToolButton &toolbutton)
111  {
112  // Update mouse over icon
113  if((toolbutton.state & QStyle::State_Enabled) &&
114  !(toolbutton.state & QStyle::State_On) &&
115  (toolbutton.state & QStyle::State_MouseOver))
116  {
117  if(!m_iconCached)
118  {
119  QIcon icon = toolbutton.icon;
120  QPixmap pixmap = icon.pixmap(toolbutton.iconSize, QIcon::Selected, QIcon::On);
121  m_hoverIcon = QIcon(pixmap);
122  m_iconCached = true;
123  }
124  toolbutton.icon = m_hoverIcon;
125  }
126  }
127 
128 private:
129  bool m_subBar;
131  QIcon m_hoverIcon;
132 };
133 
135  QWidget(parent),
136  m_toolStyle(Qt::ToolButtonTextBesideIcon),
137  m_subBar(false),
138  m_built(false)
139 {
140 }
141 
142 void NavigationBar::addAction(QAction *action)
143 {
144  // Add action to the list
145  m_actions.append(action);
146 }
147 
148 QAction *NavigationBar::addGroup(QList<QAction *> list, const QIcon &icon, const QString &text)
149 {
150  // Add new group
151  QAction* action = new QAction(icon, text, this);
152  mapGroup(action, list);
153  return action;
154 }
155 
156 QAction *NavigationBar::addGroup(QList<QAction *> list, const QString &text)
157 {
158  // Add new group
159  QAction* action = new QAction(text, this);
160  mapGroup(action, list);
161  return action;
162 }
163 
165 {
166  // Build the layout of the complex GUI component
167  if(!m_built)
168  {
169  // Set it visible if main component
170  setVisible(!m_subBar);
171 
172  // Create new layout for the bar
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,
178  m_subBar ? 0 : MarginTop,
179  m_subBar ? 0 : MarginRight,
180  m_subBar ? 0 : MarginBottom);
181  vboxLayout->setSpacing(m_subBar ? 0 : ButtonSpacing);
182 
183  // List all actions
184  for(int i = 0; i < m_actions.count(); i++)
185  {
186  // Add an action to the layout
187  QAction* action = m_actions[i];
188  action->setActionGroup(actionGroup);
189  action->setCheckable(true);
190  QToolButton* toolButton = new NavToolButton(this, m_subBar);
191  toolButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
192  toolButton->setToolButtonStyle(m_toolStyle);
193  toolButton->setDefaultAction(action);
194  toolButton->setIconSize(QSize(ToolButtonIconSize, ToolButtonIconSize));
195  if(m_subBar)
196  {
197  SetObjectStyleSheet(toolButton, StyleSheetNames::ToolSubBlack);
198  }
199  else
200  {
201  SetObjectStyleSheet(toolButton, StyleSheetNames::ToolBlack);
202  }
203 
204  if(m_groups.contains(action))
205  {
206  // Add the tool button
207  QVBoxLayout* vboxLayout2 = new QVBoxLayout();
208  vboxLayout->addLayout(vboxLayout2);
209  vboxLayout2->addWidget(toolButton);
210  vboxLayout2->setSpacing(0);
211  if(!m_subBar)
212  {
213  SetObjectStyleSheet(toolButton, StyleSheetNames::ToolGroupBlack);
214  }
215 
216  // Add sub-navigation bar for the group of actions
217  QList<QAction*> group = m_groups[action];
218  NavigationBar* subNavBar = new NavigationBar(this);
219  subNavBar->setSubBar(true);
220  for(int j = 0; j < group.count(); j++)
221  {
222  subNavBar->addAction(group[j]);
223  }
224  vboxLayout2->addWidget(subNavBar);
225  subNavBar->buildUi();
226  connect(action, SIGNAL(toggled(bool)), subNavBar, SLOT(onSubBarClick(bool)));
227  }
228  else
229  {
230 
231  vboxLayout->addWidget(toolButton);
232  }
233  }
234 
235  if(!m_subBar)
236  {
237  // Set specific parameters for for the main component
238  if(m_actions.count())
239  {
240  m_actions[0]->setChecked(true);
241  }
242  setMinimumWidth(defButtonWidth + MarginLeft + MarginRight);
243  vboxLayout->addStretch(1);
244  }
245 
246  // The component is built
247  m_built = true;
248  }
249 }
250 
252 {
253  // Expand/collapse the sub-navigation bar
254  setVisible(clicked);
255 
256 
257  if(clicked && m_actions.count() > 0)
258  {
259  // Activate the checked action
260  bool haveChecked = false;
261  for(int i = 0; i < m_actions.count(); i++)
262  {
263  QAction* action = m_actions[i];
264  if(action->isChecked())
265  {
266  action->trigger();
267  haveChecked = true;
268  break;
269  }
270  }
271  if(!haveChecked)
272  {
273  m_actions[0]->trigger();
274  }
275  }
276 }
277 
278 void NavigationBar::setToolButtonStyle(Qt::ToolButtonStyle toolButtonStyle)
279 {
280  // Set the tool button style
281  m_toolStyle = toolButtonStyle;
282 }
283 
284 void NavigationBar::resizeEvent(QResizeEvent *evt)
285 {
286  QWidget::resizeEvent(evt);
287  Q_EMIT resized(size());
288 }
289 
290 void NavigationBar::mapGroup(QAction *action, QList<QAction *> list)
291 {
292  // Map the group with the actions
293  addAction(action);
294  m_groups[action] = list;
295 }
296 
297 void NavigationBar::setSubBar(bool subBar)
298 {
299  // Set the component be sub-navigation bar
300  m_subBar = subBar;
301 }
302 
#define SetObjectStyleSheet(object, name)
Definition: styleSheet.h:10
Qt::ToolButtonStyle m_toolStyle
Definition: navigationbar.h:89
QList< QAction * > m_actions
Definition: navigationbar.h:87
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 updateIcon(QStyleOptionToolButton &toolbutton)
void onSubBarClick(bool)
onSubBarClick Activate the sub-navigation bar
void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE
void drawLabel(const QStyleOption *opt, QPainter *p)
QMap< QAction *, QList< QAction * > > m_groups
Definition: navigationbar.h:88
The NavigationBar class Custom control for navigation bar.
Definition: navigationbar.h:13
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
Definition: sha3.h:20
NavToolButton(QWidget *parent, bool subBar)
NavigationBar(QWidget *parent=0)
NavigationBar Constructor.
void resizeEvent(QResizeEvent *evt)