2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2019 Mike Tzou (Chocobo1)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "tristatewidget.h"
31 #include <QApplication>
32 #include <QMouseEvent>
34 #include <QPaintEvent>
37 #include <QStyleOptionMenuItem>
39 TriStateWidget::TriStateWidget(const QString
&text
, QWidget
*parent
)
43 setMouseTracking(true); // for visual effects via mouse navigation
44 setFocusPolicy(Qt::TabFocus
); // for visual effects via keyboard navigation
47 void TriStateWidget::setCheckState(const Qt::CheckState checkState
)
49 m_checkState
= checkState
;
52 void TriStateWidget::setCloseOnInteraction(const bool enabled
)
54 m_closeOnInteraction
= enabled
;
57 QSize
TriStateWidget::minimumSizeHint() const
59 QStyleOptionMenuItem opt
;
61 opt
.menuHasCheckableItems
= true;
62 const QSize contentSize
= fontMetrics().size(Qt::TextSingleLine
, m_text
);
63 return style()->sizeFromContents(QStyle::CT_MenuItem
, &opt
, contentSize
, this);
66 void TriStateWidget::paintEvent(QPaintEvent
*)
68 QStyleOptionMenuItem opt
;
70 opt
.menuItemType
= QStyleOptionMenuItem::Normal
;
71 opt
.checkType
= QStyleOptionMenuItem::NonExclusive
;
72 opt
.menuHasCheckableItems
= true;
77 case Qt::PartiallyChecked
:
78 opt
.state
|= QStyle::State_NoChange
;
88 if ((opt
.state
& QStyle::State_HasFocus
)
89 || rect().contains(mapFromGlobal(QCursor::pos())))
91 opt
.state
|= QStyle::State_Selected
;
93 if (QApplication::mouseButtons() != Qt::NoButton
)
94 opt
.state
|= QStyle::State_Sunken
;
97 QPainter painter
{this};
98 style()->drawControl(QStyle::CE_MenuItem
, &opt
, &painter
, this);
101 void TriStateWidget::mouseReleaseEvent(QMouseEvent
*event
)
105 if (m_closeOnInteraction
)
107 // parent `triggered` signal will be emitted
108 QWidget::mouseReleaseEvent(event
);
113 // need to emit `triggered` signal manually
114 emit
triggered(m_checkState
== Qt::Checked
);
118 void TriStateWidget::keyPressEvent(QKeyEvent
*event
)
120 if ((event
->key() == Qt::Key_Return
)
121 || (event
->key() == Qt::Key_Enter
))
125 if (!m_closeOnInteraction
)
128 // need to emit parent `triggered` signal manually
129 emit
triggered(m_checkState
== Qt::Checked
);
134 QWidget::keyPressEvent(event
);
137 void TriStateWidget::toggleCheckState()
139 switch (m_checkState
)
142 case Qt::PartiallyChecked
:
143 m_checkState
= Qt::Checked
;
146 m_checkState
= Qt::Unchecked
;