2015-10-19 20:30:26 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2015 Mnemosyne LLC
|
|
|
|
*
|
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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:
|
2020-06-05 19:02:11 +00:00
|
|
|
explicit HeaderWidget(TorrentView* parent) :
|
2017-04-19 12:04:45 +00:00
|
|
|
QWidget(parent),
|
2020-05-27 21:53:12 +00:00
|
|
|
text_()
|
2015-10-19 20:30:26 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
setFont(qApp->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;
|
|
|
|
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
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
return QSize(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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QStylePainter painter(this);
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
emit static_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
|
|
|
};
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
TorrentView::TorrentView(QWidget* parent) :
|
|
|
|
QListView(parent),
|
2020-05-27 21:53:12 +00:00
|
|
|
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
|
|
|
}
|