qBittorrent
TriStateWidget Class Referencefinal

#include <tristatewidget.h>

Inheritance diagram for TriStateWidget:
Collaboration diagram for TriStateWidget:

Signals

void triggered (bool checked) const
 

Public Member Functions

 TriStateWidget (const QString &text, QWidget *parent)
 
void setCheckState (Qt::CheckState checkState)
 
void setCloseOnInteraction (bool enabled)
 

Private Member Functions

QSize minimumSizeHint () const override
 
void paintEvent (QPaintEvent *) override
 
void mouseReleaseEvent (QMouseEvent *event) override
 
void keyPressEvent (QKeyEvent *event) override
 
void toggleCheckState ()
 

Private Attributes

bool m_closeOnInteraction
 
Qt::CheckState m_checkState
 
const QString m_text
 

Detailed Description

Definition at line 35 of file tristatewidget.h.

Constructor & Destructor Documentation

◆ TriStateWidget()

TriStateWidget::TriStateWidget ( const QString &  text,
QWidget *  parent 
)

Definition at line 39 of file tristatewidget.cpp.

40  : QWidget {parent}
41  , m_closeOnInteraction {true}
42  , m_checkState {Qt::Unchecked}
43  , m_text {text}
44 {
45  setMouseTracking(true); // for visual effects via mouse navigation
46  setFocusPolicy(Qt::TabFocus); // for visual effects via keyboard navigation
47 }
Qt::CheckState m_checkState
bool m_closeOnInteraction
const QString m_text

Member Function Documentation

◆ keyPressEvent()

void TriStateWidget::keyPressEvent ( QKeyEvent *  event)
overrideprivate

Definition at line 120 of file tristatewidget.cpp.

121 {
122  if ((event->key() == Qt::Key_Return)
123  || (event->key() == Qt::Key_Enter))
124  {
126 
128  {
129  update();
130  // need to emit parent `triggered` signal manually
131  emit triggered(m_checkState == Qt::Checked);
132  return;
133  }
134  }
135 
136  QWidget::keyPressEvent(event);
137 }
void triggered(bool checked) const

References m_checkState, m_closeOnInteraction, toggleCheckState(), and triggered().

Here is the call graph for this function:

◆ minimumSizeHint()

QSize TriStateWidget::minimumSizeHint ( ) const
overrideprivate

Definition at line 59 of file tristatewidget.cpp.

60 {
61  QStyleOptionMenuItem opt;
62  opt.initFrom(this);
63  opt.menuHasCheckableItems = true;
64  const QSize contentSize = fontMetrics().size(Qt::TextSingleLine, m_text);
65  return style()->sizeFromContents(QStyle::CT_MenuItem, &opt, contentSize, this);
66 }

References m_text.

◆ mouseReleaseEvent()

void TriStateWidget::mouseReleaseEvent ( QMouseEvent *  event)
overrideprivate

Definition at line 103 of file tristatewidget.cpp.

104 {
106 
108  {
109  // parent `triggered` signal will be emitted
110  QWidget::mouseReleaseEvent(event);
111  }
112  else
113  {
114  update();
115  // need to emit `triggered` signal manually
116  emit triggered(m_checkState == Qt::Checked);
117  }
118 }

References m_checkState, m_closeOnInteraction, toggleCheckState(), and triggered().

Here is the call graph for this function:

◆ paintEvent()

void TriStateWidget::paintEvent ( QPaintEvent *  )
overrideprivate

Definition at line 68 of file tristatewidget.cpp.

69 {
70  QStyleOptionMenuItem opt;
71  opt.initFrom(this);
72  opt.menuItemType = QStyleOptionMenuItem::Normal;
73  opt.checkType = QStyleOptionMenuItem::NonExclusive;
74  opt.menuHasCheckableItems = true;
75  opt.text = m_text;
76 
77  switch (m_checkState)
78  {
79  case Qt::PartiallyChecked:
80  opt.state |= QStyle::State_NoChange;
81  break;
82  case Qt::Checked:
83  opt.checked = true;
84  break;
85  case Qt::Unchecked:
86  opt.checked = false;
87  break;
88  };
89 
90  if ((opt.state & QStyle::State_HasFocus)
91  || rect().contains(mapFromGlobal(QCursor::pos())))
92  {
93  opt.state |= QStyle::State_Selected;
94 
95  if (QApplication::mouseButtons() != Qt::NoButton)
96  opt.state |= QStyle::State_Sunken;
97  }
98 
99  QPainter painter {this};
100  style()->drawControl(QStyle::CE_MenuItem, &opt, &painter, this);
101 }

References m_checkState, and m_text.

◆ setCheckState()

void TriStateWidget::setCheckState ( Qt::CheckState  checkState)

Definition at line 49 of file tristatewidget.cpp.

50 {
51  m_checkState = checkState;
52 }

References m_checkState.

Referenced by TriStateAction::setCheckState(), and TriStateAction::TriStateAction().

Here is the caller graph for this function:

◆ setCloseOnInteraction()

void TriStateWidget::setCloseOnInteraction ( bool  enabled)

Definition at line 54 of file tristatewidget.cpp.

55 {
56  m_closeOnInteraction = enabled;
57 }

References m_closeOnInteraction.

Referenced by TriStateAction::setCloseOnInteraction().

Here is the caller graph for this function:

◆ toggleCheckState()

void TriStateWidget::toggleCheckState ( )
private

Definition at line 139 of file tristatewidget.cpp.

140 {
141  switch (m_checkState)
142  {
143  case Qt::Unchecked:
144  case Qt::PartiallyChecked:
145  m_checkState = Qt::Checked;
146  break;
147  case Qt::Checked:
148  m_checkState = Qt::Unchecked;
149  break;
150  };
151 }

References m_checkState.

Referenced by keyPressEvent(), and mouseReleaseEvent().

Here is the caller graph for this function:

◆ triggered

void TriStateWidget::triggered ( bool  checked) const
signal

Referenced by keyPressEvent(), mouseReleaseEvent(), and TriStateAction::TriStateAction().

Here is the caller graph for this function:

Member Data Documentation

◆ m_checkState

Qt::CheckState TriStateWidget::m_checkState
private

◆ m_closeOnInteraction

bool TriStateWidget::m_closeOnInteraction
private

Definition at line 58 of file tristatewidget.h.

Referenced by keyPressEvent(), mouseReleaseEvent(), and setCloseOnInteraction().

◆ m_text

const QString TriStateWidget::m_text
private

Definition at line 60 of file tristatewidget.h.

Referenced by minimumSizeHint(), and paintEvent().


The documentation for this class was generated from the following files: