1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-27 18:18:10 +00:00
transmission/qt/TorrentView.cc
Charles Kerr 8afbfecadb
refactor: re-enable some clang-tidy checks in qt/ (#2585)
* chore: re-enable readability-static-accessed-through-instance test in qt

* chore: re-enable clang-diagnostic-nonportable-system-include-path check in qt/

* chore: re-enable clang-diagnostic-undefined-reinterpret-cast test in qt/

* chore: re-enable cert-err58-cpp check in qt/

* chore: re-enable clang-diagnostic-switch-enum check in qt/

* chore: re-enable modernize-return-braced-init-list check in qt/

* chore: re-enable cppcoreguidelines-pro-type-static-cast-downcast check in qt/

* chore: re-enable cppcoreguidelines-pro-type-cstyle-cast check in qt/

* refactor: re-enable cppcoreguidelines-init-variables check in qt/

* chore: re-enable cppcoreguidelines-pro-type-vararg check in qt/

* chore: remove explicit disable of clang-diagnostic-old-style-cast check in qt/

* chore: re-enable bugprone-implicit-widening-of-multiplication-result check in qt/
2022-02-07 21:56:04 -06:00

100 lines
2.6 KiB
C++

// This file Copyright © 2015-2022 Mnemosyne LLC.
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
#include <QApplication>
#include <QStyleOptionHeader>
#include <QStylePainter>
#include "TorrentView.h"
class TorrentView::HeaderWidget : public QWidget
{
public:
explicit HeaderWidget(TorrentView* parent)
: QWidget(parent)
{
setFont(QApplication::font("QMiniFont"));
}
void setText(QString const& text)
{
text_ = text;
update();
}
// QWidget
[[nodiscard]] QSize sizeHint() const override
{
QStyleOptionHeader option;
option.rect = QRect(0, 0, 100, 100);
QRect const label_rect = style()->subElementRect(QStyle::SE_HeaderLabel, &option, this);
return { 100, fontMetrics().height() + (option.rect.height() - label_rect.height()) };
}
protected:
// QWidget
void paintEvent(QPaintEvent* /*event*/) override
{
QStyleOptionHeader option;
option.initFrom(this);
option.state = QStyle::State_Enabled;
option.position = QStyleOptionHeader::OnlyOneSection;
QStylePainter painter(this);
painter.drawControl(QStyle::CE_HeaderSection, option);
option.rect = style()->subElementRect(QStyle::SE_HeaderLabel, &option, this);
painter.drawItemText(option.rect, Qt::AlignCenter, option.palette, true, text_, QPalette::ButtonText);
}
void mouseDoubleClickEvent(QMouseEvent* /*event*/) override
{
emit dynamic_cast<TorrentView*>(parent())->headerDoubleClicked();
}
private:
QString text_;
};
TorrentView::TorrentView(QWidget* parent)
: QListView(parent)
, header_widget_(new HeaderWidget(this))
{
}
void TorrentView::setHeaderText(QString const& text)
{
bool const header_visible = !text.isEmpty();
header_widget_->setText(text);
header_widget_->setVisible(header_visible);
if (header_visible)
{
adjustHeaderPosition();
}
setViewportMargins(0, header_visible ? header_widget_->height() : 0, 0, 0);
}
void TorrentView::resizeEvent(QResizeEvent* event)
{
QListView::resizeEvent(event);
if (header_widget_->isVisible())
{
adjustHeaderPosition();
}
}
void TorrentView::adjustHeaderPosition()
{
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);
}