Enable customizing the save statistics time interval
[qBittorrent.git] / src / gui / log / loglistview.cpp
blob2fb4a1ce18c40d8f00067774c422f3838501303f
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2020 Prince Gupta <jagannatharjun11@gmail.com>
4 * Copyright (C) 2019 sledgehammer999 <hammered999@gmail.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "loglistview.h"
32 #include <QtSystemDetection>
33 #include <QApplication>
34 #include <QClipboard>
35 #include <QFontMetrics>
36 #include <QKeyEvent>
37 #include <QPainter>
38 #include <QStyle>
39 #include <QStyledItemDelegate>
41 #include "base/global.h"
42 #include "logmodel.h"
44 #ifdef Q_OS_WIN
45 #include "base/preferences.h"
46 #endif
48 namespace
50 const QString SEPARATOR = u" - "_s;
52 int horizontalAdvance(const QFontMetrics &fontMetrics, const QString &text)
54 return fontMetrics.horizontalAdvance(text);
57 QString logText(const QModelIndex &index)
59 return index.data(BaseLogModel::TimeRole).toString()
60 + SEPARATOR
61 + index.data(BaseLogModel::MessageRole).toString();
64 class LogItemDelegate final : public QStyledItemDelegate
66 public:
67 explicit LogItemDelegate(QObject *parent = nullptr)
68 : QStyledItemDelegate(parent)
69 #ifdef Q_OS_WIN
70 , m_useCustomUITheme(Preferences::instance()->useCustomUITheme())
71 #endif
75 private:
76 void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
78 painter->save();
79 QStyledItemDelegate::paint(painter, option, index); // paints background, focus rect and selection rect
81 const QStyle *style = option.widget ? option.widget->style() : QApplication::style();
82 const QRect textRect = option.rect.adjusted(1, 0, 0, 0); // shift 1 to avoid text being too close to focus rect
83 const bool isEnabled = option.state.testFlag(QStyle::State_Enabled);
85 #ifdef Q_OS_WIN
86 // Windows default theme do not use highlighted text color
87 const QPalette::ColorRole textRole = m_useCustomUITheme
88 ? (option.state.testFlag(QStyle::State_Selected) ? QPalette::HighlightedText : QPalette::WindowText)
89 : QPalette::WindowText;
90 #else
91 const QPalette::ColorRole textRole = option.state.testFlag(QStyle::State_Selected) ? QPalette::HighlightedText : QPalette::WindowText;
92 #endif
94 // for unknown reasons (fixme) painter won't accept some font properties
95 // until they are set explicitly, and we have to manually set some font properties
96 QFont font = option.font;
97 font.setFamily(option.font.family());
98 if (option.font.pointSizeF() > 0) // for better scaling we use floating point version
99 font.setPointSizeF(option.font.pointSizeF());
100 painter->setFont(font);
102 QPalette palette = option.palette;
104 const QString time = index.data(BaseLogModel::TimeRole).toString();
105 palette.setColor(QPalette::Active, QPalette::WindowText, index.data(BaseLogModel::TimeForegroundRole).value<QColor>());
106 style->drawItemText(painter, textRect, option.displayAlignment, palette, isEnabled, time, textRole);
108 const QFontMetrics fontMetrics = painter->fontMetrics(); // option.fontMetrics adds extra padding to QFontMetrics::width
109 const int separatorCoordinateX = horizontalAdvance(fontMetrics, time);
110 style->drawItemText(painter, textRect.adjusted(separatorCoordinateX, 0, 0, 0), option.displayAlignment, option.palette
111 , isEnabled, SEPARATOR, textRole);
113 const int messageCoordinateX = separatorCoordinateX + horizontalAdvance(fontMetrics, SEPARATOR);
114 const QString message = index.data(BaseLogModel::MessageRole).toString();
115 palette.setColor(QPalette::Active, QPalette::WindowText, index.data(BaseLogModel::MessageForegroundRole).value<QColor>());
116 style->drawItemText(painter, textRect.adjusted(messageCoordinateX, 0, 0, 0), option.displayAlignment, palette
117 , isEnabled, message, textRole);
119 painter->restore();
122 QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
124 const QSize minimumFontPadding(4, 4);
125 const QSize fontSize = option.fontMetrics.size(0, logText(index)) + minimumFontPadding;
126 const QSize defaultSize = QStyledItemDelegate::sizeHint(option, index);
127 const QSize margins = (defaultSize - fontSize).expandedTo({0, 0});
128 return fontSize + margins;
131 #ifdef Q_OS_WIN
132 const bool m_useCustomUITheme = false;
133 #endif
137 LogListView::LogListView(QWidget *parent)
138 : QListView(parent)
140 setSelectionMode(QAbstractItemView::ExtendedSelection);
141 setItemDelegate(new LogItemDelegate(this));
143 #ifdef Q_OS_MAC
144 setAttribute(Qt::WA_MacShowFocusRect, false);
145 #endif
148 void LogListView::keyPressEvent(QKeyEvent *event)
150 if (event->matches(QKeySequence::Copy))
151 copySelection();
152 else
153 QListView::keyPressEvent(event);
156 void LogListView::copySelection() const
158 QStringList list;
159 const QModelIndexList selectedIndexes = selectionModel()->selectedRows();
160 for (const QModelIndex &index : selectedIndexes)
161 list.append(logText(index));
162 QApplication::clipboard()->setText(list.join(u'\n'));