Commit a1913362 authored by David Jarvie's avatar David Jarvie
Browse files

Shrink calendar list to remove empty space when too large

Encapsulate this functionality better.
parent 34616df9
Pipeline #126564 passed with stage
in 2 minutes and 28 seconds
......@@ -163,7 +163,7 @@ MainWindow::MainWindow(bool restored)
DataModel::widgetNeedsDatabase(this);
mResourceSelector = new ResourceSelector(this, panelContents);
connect(mResourceSelector, &ResourceSelector::calendarCountChanged, this, [this]() { mResourceSelector->adjustSize(); });
mResourceSelector->setResizeToList();
mPanelLayout->addWidget(mResourceSelector);
mPanelLayout->addStretch();
......
......@@ -644,7 +644,7 @@ QSize ResourceView::sizeHint() const
const QRect lastItem = visualRect(model()->index(model()->rowCount() - 1, 0));
const int border = height() - viewport()->height();
QSize sz = QListView::sizeHint();
const int itemsHeight = lastItem.bottom() + lastItem.height()*0.7 + border;
const int itemsHeight = lastItem.bottom() + lastItem.height() + border;
if (itemsHeight < sz.height())
sz.setHeight(itemsHeight);
return sz;
......
......@@ -64,11 +64,7 @@ ResourceSelector::ResourceSelector(MainWindow* parentWindow, QWidget* parent)
connect(mListView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &ResourceSelector::selectionChanged);
mListView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(mListView, &ResourceView::customContextMenuRequested, this, &ResourceSelector::contextMenuRequested);
connect(mListView, &ResourceView::rowCountChanged, this, [this]()
{
mListViewAdjustment = mListView->sizeHint().height() - mListView->height();
Q_EMIT calendarCountChanged();
});
connect(mListView, &ResourceView::rowCountChanged, this, &ResourceSelector::resizeToList);
mListView->setWhatsThis(i18nc("@info:whatsthis",
"List of available calendars of the selected type. The checked state shows whether a calendar "
"is enabled (checked) or disabled (unchecked). The default calendar is shown in bold."));
......@@ -557,14 +553,17 @@ CalEvent::Type ResourceSelector::currentResourceType() const
}
/******************************************************************************
* Resize this widget to fit its list view size hint.
* This will adjust the size according to the change in list view height when
* the signal calendarCountChanged() was last emitted.
* Called when the size hint for the list view has changed.
* Resize the list view and this widget to fit the new list view size hint.
*/
void ResourceSelector::adjustSize()
void ResourceSelector::resizeToList()
{
mListView->resize(mListView->width(), mListView->height() + mListViewAdjustment);
resize(width(), height() + mListViewAdjustment);
if (mResizeToList)
{
const int change = mListView->sizeHint().height() - mListView->height();
mListView->resize(mListView->width(), mListView->height() + change);
resize(width(), height() + change);
}
}
void ResourceSelector::resizeEvent(QResizeEvent* re)
......
......@@ -40,19 +40,15 @@ public:
explicit ResourceSelector(MainWindow* parentWindow, QWidget* parent = nullptr);
void initActions(KActionCollection*);
/** Resize this widget to fit its list view size hint.
* This will adjust the size according to the change in list view height
* when the signal calendarCountChanged() was last emitted.
/** Automatically resize this widget to fit the number of calendars in the
* list, whenever the number changes.
*/
void adjustSize();
void setResizeToList() { mResizeToList = true; }
Q_SIGNALS:
/** Emitted when the widget has been resized. */
void resized(const QSize& oldSize, const QSize& newSize);
/** Emitted when the number of calendars has changed. */
void calendarCountChanged();
protected:
void resizeEvent(QResizeEvent*) override;
......@@ -75,6 +71,7 @@ private Q_SLOTS:
void archiveDaysChanged(int days);
void slotResourceAdded(Resource&, CalEvent::Type);
void reinstateAlarmTypeScrollBars();
void resizeToList();
private:
CalEvent::Type currentResourceType() const;
......@@ -98,7 +95,7 @@ private:
QAction* mActionImport {nullptr};
QAction* mActionExport {nullptr};
KToggleAction* mActionSetDefault {nullptr};
int mListViewAdjustment {0}; // adjustment in mListView height when calendarCountChanged() was last emitted
bool mResizeToList {false}; // widget should be resized when calendar list changes
};
// vim: et sw=4:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment