Remove incorrect assertions
[qBittorrent.git] / src / gui / tristatewidget.cpp
blob2219dcb84b6f725a478bf41bb50eba9f952d0862
1 /*
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>
33 #include <QPainter>
34 #include <QPaintEvent>
35 #include <QString>
36 #include <QStyle>
37 #include <QStyleOptionMenuItem>
39 TriStateWidget::TriStateWidget(const QString &text, QWidget *parent)
40 : QWidget {parent}
41 , m_text {text}
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;
60 opt.initFrom(this);
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;
69 opt.initFrom(this);
70 opt.menuItemType = QStyleOptionMenuItem::Normal;
71 opt.checkType = QStyleOptionMenuItem::NonExclusive;
72 opt.menuHasCheckableItems = true;
73 opt.text = m_text;
75 switch (m_checkState)
77 case Qt::PartiallyChecked:
78 opt.state |= QStyle::State_NoChange;
79 break;
80 case Qt::Checked:
81 opt.checked = true;
82 break;
83 case Qt::Unchecked:
84 opt.checked = false;
85 break;
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)
103 toggleCheckState();
105 if (m_closeOnInteraction)
107 // parent `triggered` signal will be emitted
108 QWidget::mouseReleaseEvent(event);
110 else
112 update();
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))
123 toggleCheckState();
125 if (!m_closeOnInteraction)
127 update();
128 // need to emit parent `triggered` signal manually
129 emit triggered(m_checkState == Qt::Checked);
130 return;
134 QWidget::keyPressEvent(event);
137 void TriStateWidget::toggleCheckState()
139 switch (m_checkState)
141 case Qt::Unchecked:
142 case Qt::PartiallyChecked:
143 m_checkState = Qt::Checked;
144 break;
145 case Qt::Checked:
146 m_checkState = Qt::Unchecked;
147 break;