From a85b6df314de972b2528c79611af93a8987fdce6 Mon Sep 17 00:00:00 2001 From: sledgehammer999 Date: Sun, 22 Mar 2015 01:59:24 +0200 Subject: [PATCH] Fix side panel resizing of elements. --- src/gui/transferlistfilterswidget.cpp | 70 ++++++++++++++++++--------- src/gui/transferlistfilterswidget.h | 26 +++++++--- 2 files changed, 66 insertions(+), 30 deletions(-) diff --git a/src/gui/transferlistfilterswidget.cpp b/src/gui/transferlistfilterswidget.cpp index 7fa99ce0b..613bd045f 100644 --- a/src/gui/transferlistfilterswidget.cpp +++ b/src/gui/transferlistfilterswidget.cpp @@ -51,28 +51,52 @@ #include "downloadthread.h" #include "logger.h" -LabelFiltersList::LabelFiltersList(QWidget *parent): QListWidget(parent) +FiltersBase::FiltersBase(QWidget *parent) + : QListWidget(parent) { - itemHover = 0; - // Accept drop - setAcceptDrops(true); - setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); setStyleSheet("QListWidget { background: transparent; border: 0 }"); #if defined(Q_OS_MAC) setAttribute(Qt::WA_MacShowFocusRect, false); #endif } +QSize FiltersBase::sizeHint() const +{ + QSize size = QListWidget::sizeHint(); + // Height should be exactly the height of the content + size.setHeight((sizeHintForRow(0) * count()) + (2 * frameWidth()) + 6); + return size; +} + +QSize FiltersBase::minimumSizeHint() const +{ + QSize size = QListWidget::minimumSizeHint(); + // Minimum height should be exactly the sticky labels height + size.setHeight((sizeHintForRow(0) * 2) + (2 * frameWidth()) + 6); + return size; +} + +LabelFiltersList::LabelFiltersList(QWidget *parent) + : FiltersBase(parent) +{ + itemHover = 0; + // Accept drop + setAcceptDrops(true); + setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); +} + void LabelFiltersList::addItem(QListWidgetItem *it) { Q_ASSERT(count() >= 2); for (int i = 2; itext().localeAwareCompare(it->text()) >= 0) { insertItem(i, it); + updateGeometry(); return; } } QListWidget::addItem(it); + updateGeometry(); } QString LabelFiltersList::labelFromRow(int row) const @@ -150,33 +174,21 @@ void LabelFiltersList::setItemHover(bool hover) } } -StatusFiltersWidget::StatusFiltersWidget(QWidget *parent): QListWidget(parent), m_shown(false) +StatusFiltersWidget::StatusFiltersWidget(QWidget *parent) + : FiltersBase(parent) + , m_shown(false) { setUniformItemSizes(true); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // Height is fixed (sizeHint().height() is used) setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); - setStyleSheet("QListWidget { background: transparent; border: 0 }"); -#if defined(Q_OS_MAC) - setAttribute(Qt::WA_MacShowFocusRect, false); -#endif -} - -QSize StatusFiltersWidget::sizeHint() const -{ - QSize size = QListWidget::sizeHint(); - // Height should be exactly the height of the content - size.setHeight(contentsSize().height() + 2 * frameWidth() + 6); - return size; } -TrackerFiltersList::TrackerFiltersList(QWidget *parent): QListWidget(parent), m_downloader(new DownloadThread(this)) +TrackerFiltersList::TrackerFiltersList(QWidget *parent) + : FiltersBase(parent) + , m_downloader(new DownloadThread(this)) { setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); - setStyleSheet("QListWidget { background: transparent; border: 0 }"); -#if defined(Q_OS_MAC) - setAttribute(Qt::WA_MacShowFocusRect, false); -#endif QListWidgetItem *allTrackers = new QListWidgetItem(this); allTrackers->setData(Qt::DisplayRole, QVariant(tr("All trackers (0)"))); allTrackers->setData(Qt::DecorationRole, IconProvider::instance()->getIcon("network-server")); @@ -387,7 +399,7 @@ TransferListFiltersWidget::TransferListFiltersWidget(QWidget *parent, TransferLi QFont font; font.setBold(true); font.setCapitalization(QFont::SmallCaps); - QLabel *torrentsLabel = new QLabel(tr("Torrents")); + torrentsLabel = new QLabel(tr("Torrents")); torrentsLabel->setIndent(2); torrentsLabel->setFont(font); vLayout->addWidget(torrentsLabel); @@ -478,6 +490,13 @@ TransferListFiltersWidget::~TransferListFiltersWidget() delete vLayout; } +void TransferListFiltersWidget::resizeEvent(QResizeEvent *event) +{ + int height = event->size().height(); + int minHeight = statusFilters->height() + (3 * torrentsLabel->height()); + trackerFilters->setMinimumHeight((height - minHeight) / 2); +} + StatusFiltersWidget* TransferListFiltersWidget::getStatusFilters() const { return statusFilters; @@ -639,6 +658,7 @@ void TransferListFiltersWidget::removeSelectedLabel() applyLabelFilter(0); // Un display filter delete labelFilters->takeItem(row); + labelFilters->updateGeometry(); // Save custom labels to remember it was deleted Preferences::instance()->removeTorrentLabel(label); } @@ -655,6 +675,8 @@ void TransferListFiltersWidget::removeUnusedLabels() delete labelFilters->takeItem(labelFilters->rowFromLabel(label)); Preferences::instance()->removeTorrentLabel(label); } + if (!unusedLabels.empty()) + labelFilters->updateGeometry(); } void TransferListFiltersWidget::applyLabelFilter(int row) diff --git a/src/gui/transferlistfilterswidget.h b/src/gui/transferlistfilterswidget.h index 829c90c7f..215a95e8c 100644 --- a/src/gui/transferlistfilterswidget.h +++ b/src/gui/transferlistfilterswidget.h @@ -38,6 +38,7 @@ QT_BEGIN_NAMESPACE class QListWidgetItem; class QVBoxLayout; class QDragMoveEvent; +class QLabel; QT_END_NAMESPACE class TransferListWidget; @@ -45,7 +46,18 @@ class TorrentModelItem; class QTorrentHandle; class DownloadThread; -class LabelFiltersList: public QListWidget +class FiltersBase: public QListWidget +{ + Q_OBJECT + +public: + FiltersBase(QWidget *parent); + + QSize sizeHint() const; + QSize minimumSizeHint() const; +}; + +class LabelFiltersList: public FiltersBase { Q_OBJECT @@ -71,21 +83,18 @@ protected: void setItemHover(bool hover); }; -class StatusFiltersWidget: public QListWidget +class StatusFiltersWidget: public FiltersBase { Q_OBJECT public: StatusFiltersWidget(QWidget *parent); -protected: - QSize sizeHint() const; - private: bool m_shown; }; -class TrackerFiltersList: public QListWidget +class TrackerFiltersList: public FiltersBase { Q_OBJECT @@ -127,6 +136,8 @@ private: TransferListWidget *transferList; int nb_labeled; int nb_torrents; + //for use in resizeEvent() + QLabel *torrentsLabel; public: TransferListFiltersWidget(QWidget *parent, TransferListWidget *transferList); @@ -141,6 +152,9 @@ public slots: void addTracker(const QString &tracker, const QString &hash); void removeTracker(const QString &tracker, const QString &hash); +protected: + virtual void resizeEvent(QResizeEvent *event); + protected slots: void updateTorrentNumbers(); void torrentDropped(int row);