2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2015-10-19 20:30:26 +00:00
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QStyleOptionHeader>
|
|
|
|
#include <QStylePainter>
|
|
|
|
|
|
|
|
#include "TorrentView.h"
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
class TorrentView::HeaderWidget : public QWidget
|
2015-10-19 20:30:26 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
public:
|
2021-08-15 09:41:48 +00:00
|
|
|
explicit HeaderWidget(TorrentView* parent)
|
2023-07-18 15:20:17 +00:00
|
|
|
: QWidget{ parent }
|
2015-10-19 20:30:26 +00:00
|
|
|
{
|
2020-11-02 01:13:32 +00:00
|
|
|
setFont(QApplication::font("QMiniFont"));
|
2015-10-19 20:30:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void setText(QString const& text)
|
2015-10-19 20:30:26 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
text_ = text;
|
2017-04-19 12:04:45 +00:00
|
|
|
update();
|
2015-10-19 20:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// QWidget
|
2020-06-05 19:02:11 +00:00
|
|
|
[[nodiscard]] QSize sizeHint() const override
|
2015-10-19 20:30:26 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QStyleOptionHeader option;
|
2023-07-18 15:20:17 +00:00
|
|
|
option.rect = QRect{ 0, 0, 100, 100 };
|
2015-10-19 20:30:26 +00:00
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
QRect const label_rect = style()->subElementRect(QStyle::SE_HeaderLabel, &option, this);
|
2015-10-19 20:30:26 +00:00
|
|
|
|
2022-02-08 03:56:04 +00:00
|
|
|
return { 100, fontMetrics().height() + (option.rect.height() - label_rect.height()) };
|
2015-10-19 20:30:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
protected:
|
2015-10-19 20:30:26 +00:00
|
|
|
// QWidget
|
2020-05-20 01:32:51 +00:00
|
|
|
void paintEvent(QPaintEvent* /*event*/) override
|
2015-10-19 20:30:26 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QStyleOptionHeader option;
|
|
|
|
option.initFrom(this);
|
|
|
|
option.state = QStyle::State_Enabled;
|
|
|
|
option.position = QStyleOptionHeader::OnlyOneSection;
|
2015-10-19 20:30:26 +00:00
|
|
|
|
2023-07-18 15:20:17 +00:00
|
|
|
QStylePainter painter{ this };
|
2017-04-19 12:04:45 +00:00
|
|
|
painter.drawControl(QStyle::CE_HeaderSection, option);
|
2015-10-19 20:30:26 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
option.rect = style()->subElementRect(QStyle::SE_HeaderLabel, &option, this);
|
2020-05-27 21:53:12 +00:00
|
|
|
painter.drawItemText(option.rect, Qt::AlignCenter, option.palette, true, text_, QPalette::ButtonText);
|
2015-10-19 20:30:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 01:32:51 +00:00
|
|
|
void mouseDoubleClickEvent(QMouseEvent* /*event*/) override
|
2015-10-24 20:56:45 +00:00
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
emit dynamic_cast<TorrentView*>(parent())->headerDoubleClicked();
|
2015-10-24 20:56:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
private:
|
2020-05-27 21:53:12 +00:00
|
|
|
QString text_;
|
2015-10-19 20:30:26 +00:00
|
|
|
};
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
TorrentView::TorrentView(QWidget* parent)
|
2023-11-21 15:02:03 +00:00
|
|
|
: QListView{ parent }
|
|
|
|
, header_widget_{ new HeaderWidget{ this } }
|
2015-10-19 20:30:26 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void TorrentView::setHeaderText(QString const& text)
|
2015-10-19 20:30:26 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
bool const header_visible = !text.isEmpty();
|
2015-10-19 20:30:26 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
header_widget_->setText(text);
|
|
|
|
header_widget_->setVisible(header_visible);
|
2015-10-19 20:30:26 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (header_visible)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
adjustHeaderPosition();
|
|
|
|
}
|
2015-10-19 20:30:26 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
setViewportMargins(0, header_visible ? header_widget_->height() : 0, 0, 0);
|
2015-10-19 20:30:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void TorrentView::resizeEvent(QResizeEvent* event)
|
2015-10-19 20:30:26 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QListView::resizeEvent(event);
|
2015-10-19 20:30:26 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (header_widget_->isVisible())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
adjustHeaderPosition();
|
|
|
|
}
|
2015-10-19 20:30:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void TorrentView::adjustHeaderPosition()
|
2015-10-19 20:30:26 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
QRect header_widget_rect = contentsRect();
|
|
|
|
header_widget_rect.setWidth(viewport()->width());
|
|
|
|
header_widget_rect.setHeight(header_widget_->sizeHint().height());
|
|
|
|
header_widget_->setGeometry(header_widget_rect);
|
2015-10-19 20:30:26 +00:00
|
|
|
}
|