Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / gui / tristatewidget.cpp
blobbc4cb0db95acfa0fcf35dd9c3601814166fe4136
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_closeOnInteraction {true}
42 , m_checkState {Qt::Unchecked}
43 , m_text {text}
45 setMouseTracking(true); // for visual effects via mouse navigation
46 setFocusPolicy(Qt::TabFocus); // for visual effects via keyboard navigation
49 void TriStateWidget::setCheckState(const Qt::CheckState checkState)
51 m_checkState = checkState;
54 void TriStateWidget::setCloseOnInteraction(const bool enabled)
56 m_closeOnInteraction = enabled;
59 QSize TriStateWidget::minimumSizeHint() const
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);
68 void TriStateWidget::paintEvent(QPaintEvent *)
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;
77 switch (m_checkState)
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;
90 if ((opt.state & QStyle::State_HasFocus)
91 || rect().contains(mapFromGlobal(QCursor::pos())))
93 opt.state |= QStyle::State_Selected;
95 if (QApplication::mouseButtons() != Qt::NoButton)
96 opt.state |= QStyle::State_Sunken;
99 QPainter painter {this};
100 style()->drawControl(QStyle::CE_MenuItem, &opt, &painter, this);
103 void TriStateWidget::mouseReleaseEvent(QMouseEvent *event)
105 toggleCheckState();
107 if (m_closeOnInteraction)
109 // parent `triggered` signal will be emitted
110 QWidget::mouseReleaseEvent(event);
112 else
114 update();
115 // need to emit `triggered` signal manually
116 emit triggered(m_checkState == Qt::Checked);
120 void TriStateWidget::keyPressEvent(QKeyEvent *event)
122 if ((event->key() == Qt::Key_Return)
123 || (event->key() == Qt::Key_Enter))
125 toggleCheckState();
127 if (!m_closeOnInteraction)
129 update();
130 // need to emit parent `triggered` signal manually
131 emit triggered(m_checkState == Qt::Checked);
132 return;
136 QWidget::keyPressEvent(event);
139 void TriStateWidget::toggleCheckState()
141 switch (m_checkState)
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;