diff --git a/src/utils/headerview.cpp b/src/utils/headerview.cpp index 9e7fc6e44..701135dec 100644 --- a/src/utils/headerview.cpp +++ b/src/utils/headerview.cpp @@ -1,218 +1,126 @@ /* -*- mode: c++; c-basic-offset:4 -*- utils/headerview.cpp This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB SPDX-License-Identifier: GPL-2.0-or-later */ #include #include "headerview.h" #include #include #include "kleopatra_debug.h" //#define ENABLE_HEADERVIEW_DEBUG #ifdef ENABLE_HEADERVIEW_DEBUG # define hvDebug qDebug #else # define hvDebug if ( true ) {} else qDebug #endif using namespace Kleo; static std::vector section_sizes(const QHeaderView *view) { Q_ASSERT(view); std::vector result; result.reserve(view->count()); for (int i = 0, end = view->count(); i != end; ++i) { result.push_back(view->sectionSize(i)); } return result; } static void apply_section_sizes(QHeaderView *view, const std::vector &newSizes) { Q_ASSERT(view); for (unsigned int i = 0, end = newSizes.size(); i != end; ++i) { view->resizeSection(i, newSizes[i]); } } namespace { template inline typename T_container::value_type lookup(const T_container &c, unsigned int i, const typename T_container::value_type &defaultValue) { return i < c.size() ? c[i] : defaultValue; } } class HeaderView::Private { friend class ::Kleo::HeaderView; HeaderView *const q; public: Private(HeaderView *qq) : q(qq), mousePressed(false), sizes() { connect(q, SIGNAL(sectionCountChanged(int,int)), q, SLOT(_klhv_slotSectionCountChanged(int,int))); connect(q, SIGNAL(sectionResized(int,int,int)), q, SLOT(_klhv_slotSectionResized(int,int,int))); } void _klhv_slotSectionCountChanged(int oldCount, int newCount) { if (newCount == oldCount) { return; } hvDebug() << oldCount << "->" << newCount; if (newCount < oldCount) { return; } ensureNumSections(newCount); apply_section_sizes(q, sizes); } void _klhv_slotSectionResized(int idx, int oldSize, int newSize) { hvDebug() << idx << ':' << oldSize << "->" << newSize; ensureNumSections(idx + 1); sizes[idx] = newSize; } void ensureNumSections(unsigned int num) { if (num > sizes.size()) { sizes.resize(num, q->defaultSectionSize()); } } bool mousePressed : 1; std::vector sizes; }; HeaderView::HeaderView(Qt::Orientation o, QWidget *p) : QHeaderView(o, p), d(new Private(this)) { } HeaderView::~HeaderView() {} -#if 0 -static std::vector calculate_section_sizes(const std::vector &oldSizes, int newLength, const std::vector &modes, int minSize) -{ - - if (oldSizes.empty()) { - hvDebug() << "no existing sizes"; - return std::vector(); - } - - int oldLength = 0, fixedLength = 0, stretchLength = 0; - int numStretchSections = 0; - for (unsigned int i = 0, end = oldSizes.size(); i != end; ++i) { - oldLength += oldSizes[i]; - if (lookup(modes, i, QHeaderView::Fixed) == QHeaderView::Stretch) { - stretchLength += oldSizes[i]; - ++numStretchSections; - } else { - fixedLength += oldSizes[i]; - } - } - - if (oldLength <= 0) { - hvDebug() << "no existing lengths - returning equidistant sizes"; - return std::vector(oldSizes.size(), newLength / oldSizes.size()); - } - - const int stretchableSpace = std::max(newLength - fixedLength, 0); - - std::vector newSizes; - newSizes.reserve(oldSizes.size()); - for (unsigned int i = 0, end = oldSizes.size(); i != end; ++i) - newSizes.push_back(std::max(minSize, - lookup(modes, i, QHeaderView::Fixed) == QHeaderView::Stretch - ? stretchLength ? stretchableSpace * oldSizes[i] / stretchLength : stretchableSpace / numStretchSections - : oldSizes[i])); - - hvDebug() << "\noldSizes = " << oldSizes << "/" << oldLength - << "\nnewSizes = " << newSizes << "/" << newLength; - - return newSizes; -} -#endif - void HeaderView::setSectionSizes(const std::vector &sizes) { hvDebug() << sizes; d->ensureNumSections(sizes.size()); d->sizes = sizes; apply_section_sizes(this, sizes); hvDebug() << "->" << sectionSizes(); } std::vector HeaderView::sectionSizes() const { return section_sizes(this); } -#if 0 -void HeaderView::setModel(QAbstractItemModel *model) -{ - - hvDebug() << "before" << section_sizes(this); - - QHeaderView::setModel(model); - - hvDebug() << "after " << section_sizes(this); - -} - -void HeaderView::setRootIndex(const QModelIndex &idx) -{ - hvDebug() << "before" << section_sizes(this); - QHeaderView::setRootIndex(idx); - hvDebug() << "after " << section_sizes(this); -} - -void HeaderView::mousePressEvent(QMouseEvent *e) -{ - d->mousePressed = true; - QHeaderView::mousePressEvent(e); -} - -void HeaderView::mouseReleaseEvent(QMouseEvent *e) -{ - d->mousePressed = false; - QHeaderView::mouseReleaseEvent(e); -} - -void HeaderView::updateGeometries() -{ - - const std::vector oldSizes = d->mousePressed ? section_sizes(this) : d->sizes; - - hvDebug() << "before" << section_sizes(this) << '(' << d->sizes << ')'; - - QHeaderView::updateGeometries(); - - hvDebug() << "after " << section_sizes(this); - - const std::vector newSizes = calculate_section_sizes(oldSizes, width(), d->modes, minimumSectionSize()); - d->sizes = newSizes; - - apply_section_sizes(this, newSizes); -} -#endif - #include "moc_headerview.cpp"