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>
35 #include <QFontMetrics>
39 #include <QStyledItemDelegate>
41 #include "base/global.h"
45 #include "base/preferences.h"
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()
61 + index
.data(BaseLogModel::MessageRole
).toString();
64 class LogItemDelegate final
: public QStyledItemDelegate
67 explicit LogItemDelegate(QObject
*parent
= nullptr)
68 : QStyledItemDelegate(parent
)
70 , m_useCustomUITheme(Preferences::instance()->useCustomUITheme())
76 void paint(QPainter
*painter
, const QStyleOptionViewItem
&option
, const QModelIndex
&index
) const override
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
);
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
;
91 const QPalette::ColorRole textRole
= option
.state
.testFlag(QStyle::State_Selected
) ? QPalette::HighlightedText
: QPalette::WindowText
;
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
);
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
;
132 const bool m_useCustomUITheme
= false;
137 LogListView::LogListView(QWidget
*parent
)
140 setSelectionMode(QAbstractItemView::ExtendedSelection
);
141 setItemDelegate(new LogItemDelegate(this));
144 setAttribute(Qt::WA_MacShowFocusRect
, false);
148 void LogListView::keyPressEvent(QKeyEvent
*event
)
150 if (event
->matches(QKeySequence::Copy
))
153 QListView::keyPressEvent(event
);
156 void LogListView::copySelection() const
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'));