Enable customizing the save statistics time interval
[qBittorrent.git] / src / gui / progressbarpainter.cpp
blob8788f5f7aaddd8f2f23837094ce34aa86fb95a00
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2020 Prince Gupta <jagannatharjun11@gmail.com>
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 "progressbarpainter.h"
31 #include <QPainter>
32 #include <QPalette>
33 #include <QStyleOptionProgressBar>
34 #include <QStyleOptionViewItem>
36 #if (defined(Q_OS_WIN) || defined(Q_OS_MACOS))
37 #include <QProxyStyle>
38 #endif
40 #include "base/global.h"
42 ProgressBarPainter::ProgressBarPainter()
44 #if (defined(Q_OS_WIN) || defined(Q_OS_MACOS))
45 auto *fusionStyle = new QProxyStyle {u"fusion"_s};
46 fusionStyle->setParent(&m_dummyProgressBar);
47 m_dummyProgressBar.setStyle(fusionStyle);
48 #endif
51 void ProgressBarPainter::paint(QPainter *painter, const QStyleOptionViewItem &option, const QString &text, const int progress) const
53 QStyleOptionProgressBar styleOption;
54 styleOption.initFrom(&m_dummyProgressBar);
55 // QStyleOptionProgressBar fields
56 styleOption.maximum = 100;
57 styleOption.minimum = 0;
58 styleOption.progress = progress;
59 styleOption.text = text;
60 styleOption.textVisible = true;
61 // QStyleOption fields
62 styleOption.rect = option.rect;
63 // Qt 6 requires QStyle::State_Horizontal to be set for correctly drawing horizontal progress bar
64 styleOption.state = option.state | QStyle::State_Horizontal;
66 const bool isEnabled = option.state.testFlag(QStyle::State_Enabled);
67 styleOption.palette.setCurrentColorGroup(isEnabled ? QPalette::Active : QPalette::Disabled);
69 painter->save();
70 const QStyle *style = m_dummyProgressBar.style();
71 style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, option.widget);
72 style->drawControl(QStyle::CE_ProgressBar, &styleOption, painter, &m_dummyProgressBar);
73 painter->restore();