mirror of
https://github.com/transmission/transmission.git
synced 2025-12-24 12:28:52 +00:00
There're places where manual intervention is still required as uncrustify is not ideal (unfortunately), but at least one may rely on it to do the right thing most of the time (e.g. when sending in a patch). The style itself is quite different from what we had before but making it uniform across all the codebase is the key. I also hope that it'll make the code more readable (YMMV) and less sensitive to further changes.
105 lines
2.5 KiB
C++
105 lines
2.5 KiB
C++
/*
|
|
* 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"
|
|
|
|
class TorrentView::HeaderWidget : public QWidget
|
|
{
|
|
public:
|
|
HeaderWidget(TorrentView* parent) :
|
|
QWidget(parent),
|
|
myText()
|
|
{
|
|
setFont(qApp->font("QMiniFont"));
|
|
}
|
|
|
|
void setText(const QString& text)
|
|
{
|
|
myText = text;
|
|
update();
|
|
}
|
|
|
|
// QWidget
|
|
virtual QSize sizeHint() const
|
|
{
|
|
QStyleOptionHeader option;
|
|
option.rect = QRect(0, 0, 100, 100);
|
|
|
|
const QRect labelRect = style()->subElementRect(QStyle::SE_HeaderLabel, &option, this);
|
|
|
|
return QSize(100, fontMetrics().height() + (option.rect.height() - labelRect.height()));
|
|
}
|
|
|
|
protected:
|
|
// QWidget
|
|
virtual void paintEvent(QPaintEvent* /*event*/)
|
|
{
|
|
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, myText, QPalette::ButtonText);
|
|
}
|
|
|
|
virtual void mouseDoubleClickEvent(QMouseEvent* /*event*/)
|
|
{
|
|
emit static_cast<TorrentView*>(parent())->headerDoubleClicked();
|
|
}
|
|
|
|
private:
|
|
QString myText;
|
|
};
|
|
|
|
TorrentView::TorrentView(QWidget* parent) :
|
|
QListView(parent),
|
|
myHeaderWidget(new HeaderWidget(this))
|
|
{
|
|
}
|
|
|
|
void TorrentView::setHeaderText(const QString& text)
|
|
{
|
|
const bool headerVisible = !text.isEmpty();
|
|
|
|
myHeaderWidget->setText(text);
|
|
myHeaderWidget->setVisible(headerVisible);
|
|
|
|
if (headerVisible)
|
|
{
|
|
adjustHeaderPosition();
|
|
}
|
|
|
|
setViewportMargins(0, headerVisible ? myHeaderWidget->height() : 0, 0, 0);
|
|
}
|
|
|
|
void TorrentView::resizeEvent(QResizeEvent* event)
|
|
{
|
|
QListView::resizeEvent(event);
|
|
|
|
if (myHeaderWidget->isVisible())
|
|
{
|
|
adjustHeaderPosition();
|
|
}
|
|
}
|
|
|
|
void TorrentView::adjustHeaderPosition()
|
|
{
|
|
QRect headerWidgetRect = contentsRect();
|
|
headerWidgetRect.setWidth(viewport()->width());
|
|
headerWidgetRect.setHeight(myHeaderWidget->sizeHint().height());
|
|
myHeaderWidget->setGeometry(headerWidgetRect);
|
|
}
|