HAL
dock_button.cpp
Go to the documentation of this file.
2 
5 #include "gui/gui_globals.h"
7 
8 #include <QPainter>
9 
10 namespace hal
11 {
12  DockButton::DockButton(ContentWidget* widget, button_orientation orientation, QObject* eventFilter, QWidget* parent) : QToolButton(parent), mWidget(widget), mOrientation(orientation)
13  {
14  setText(mWidget->name());
15  setCheckable(true);
16  setStyleSheet("QToolButton { font-family : \"Iosevka\";padding: 0; margin: 0; }");
17  setIcon(mWidget->icon());
19 
20  mIconSize = 14;
21  mWidthPadding = 16;
22  mHeightPadding = 0;
23  mRelativeHeight = 18;
24  adjustSize();
25 
27  }
28 
30  {
31  Q_UNUSED(event)
32  auto font = property("font").value<QFont>();
33  QPainter painter(this);
34  painter.setFont(font);
35  painter.setPen(mTextColor);
36  if (underMouse())
37  {
38  painter.fillRect(0, 0, mWidth, mHeight, mHoverColor);
39  }
40  else if (isChecked())
41  {
42  painter.fillRect(0, 0, mWidth, mHeight, mCheckedColor);
43  }
44 
45  switch (mOrientation)
46  {
48  painter.translate(0, -1);
49  break;
51  painter.translate(-1, mHeight);
52  painter.rotate(270);
53  break;
55  painter.translate(mWidth + 1, 0);
56  painter.rotate(90);
57  break;
58  }
59  icon().paint(&painter, 0, (mRelativeHeight / 2) - (mIconSize / 2), mIconSize, (mRelativeHeight / 2) - (mIconSize / 2) + mIconSize);
60  painter.drawText(QRectF(QRect(mIconSize, 0, mRelativeWidth, mRelativeHeight)), Qt::AlignVCenter, text());
61  }
62 
64  {
65  return mTextColor;
66  }
67 
69  {
70  return mCheckedColor;
71  }
72 
74  {
75  return mHoverColor;
76  }
77 
78  void DockButton::setTextColor(const QColor& color)
79  {
80  mTextColor = color;
81  }
82 
84  {
85  mCheckedColor = color;
86  }
87 
88  void DockButton::setHoverColor(const QColor& color)
89  {
90  mHoverColor = color;
91  }
92 
94  {
95  auto font = property("font").value<QFont>();
96  //, QFont::PreferAntialias);
97  QFontMetrics fm(font);
98  int textwidth = fm.width(text());
99  mRelativeWidth = mIconSize + textwidth + mWidthPadding;
100 
101  if (mOrientation == button_orientation::horizontal)
102  {
103  mWidth = mRelativeWidth;
104  mHeight = mRelativeHeight;
105  }
106  else
107  {
108  mWidth = mRelativeHeight;
109  mHeight = mRelativeWidth;
110  }
111 
112  setFixedHeight(mHeight);
113  setFixedWidth(mWidth);
114  }
115 
117  {
118  return mRelativeWidth;
119  }
120 
121  void DockButton::handleClicked(bool mChecked)
122  {
123  if (mChecked)
124  mWidget->open();
125  else
126  mWidget->close();
127  }
128 
130  {
131  return mWidget;
132  }
133 
135  {
136  QWidget::hide();
137  mHidden = true;
138  }
139 
141  {
142  QWidget::show();
143  mHidden = false;
144  }
145 
147  {
148  return mHidden;
149  }
150 
152  {
153  return mAvailable;
154  }
155 
156  void DockButton::setAvailable(bool available)
157  {
158  mAvailable = available;
159  }
160 
162  {
163  mRelativeHeight = height;
164  adjustSize();
165  }
166 }
Abstract class for Widgets within HAL's ContentArea.
void setCheckedColor(const QColor &color)
Definition: dock_button.cpp:83
QColor hoverColor
Definition: dock_button.h:60
void setAvailable(bool available)
DockButton(ContentWidget *widget, button_orientation orientation, QObject *eventFilter, QWidget *parent)
Definition: dock_button.cpp:12
QColor textColor
Definition: dock_button.h:58
void setRelativeHeight(int height)
QColor checkedColor
Definition: dock_button.h:59
void handleClicked(bool mChecked)
void paintEvent(QPaintEvent *event) override
Definition: dock_button.cpp:29
void setHoverColor(const QColor &color)
Definition: dock_button.cpp:88
ContentWidget * widget()
void setTextColor(const QColor &color)
Definition: dock_button.cpp:78
button_orientation
Definition: dock_button.h:41
void setCheckable(bool)
bool isChecked() const const
void clicked(bool checked)
void setIcon(const QIcon &icon)
void setText(const QString &text)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
virtual bool eventFilter(QObject *watched, QEvent *event)
void installEventFilter(QObject *filterObj)
QVariant property(const char *name) const const
AlignVCenter
virtual bool event(QEvent *event) override
T value() const const
void hide()
void setFixedHeight(int h)
void setFixedWidth(int w)
void show()
void setStyleSheet(const QString &styleSheet)
bool underMouse() const const