File indexing completed on 2024-04-21 05:51:23

0001 /*
0002     SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com>
0003     SPDX-FileCopyrightText: 2018 Harald Sitter <sitter@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 // Own
0009 #include "LabelsAligner.h"
0010 
0011 // Qt
0012 #include <QGridLayout>
0013 #include <QWidget>
0014 
0015 using namespace Konsole;
0016 
0017 LabelsAligner::LabelsAligner(QWidget *refWidget)
0018     : _refWidget(refWidget)
0019 {
0020 }
0021 
0022 void LabelsAligner::addLayout(QGridLayout *layout)
0023 {
0024     _layouts.append(layout);
0025 }
0026 
0027 void LabelsAligner::addLayouts(const QVector<QGridLayout *> &layouts)
0028 {
0029     _layouts.append(layouts);
0030 }
0031 
0032 void LabelsAligner::setReferenceWidget(QWidget *refWidget)
0033 {
0034     _refWidget = refWidget;
0035 }
0036 
0037 void LabelsAligner::updateLayouts()
0038 {
0039     for (const auto *layout : std::as_const(_layouts)) {
0040         QWidget *widget = layout->parentWidget();
0041         Q_ASSERT(widget);
0042         do {
0043             QLayout *widgetLayout = widget->layout();
0044             if (widgetLayout != nullptr) {
0045                 widgetLayout->update();
0046                 widgetLayout->activate();
0047             }
0048             widget = widget->parentWidget();
0049         } while (widget != _refWidget && widget != nullptr);
0050     }
0051 }
0052 
0053 void LabelsAligner::align()
0054 {
0055     Q_ASSERT(_refWidget);
0056 
0057     if (_layouts.count() <= 1) {
0058         return;
0059     }
0060 
0061     int maxRight = 0;
0062     for (const auto *layout : std::as_const(_layouts)) {
0063         int left = getLeftMargin(layout);
0064         for (int row = 0; row < layout->rowCount(); ++row) {
0065             QLayoutItem *layoutItem = layout->itemAtPosition(row, LABELS_COLUMN);
0066             if (layoutItem == nullptr) {
0067                 continue;
0068             }
0069             QWidget *widget = layoutItem->widget();
0070             if (widget == nullptr) {
0071                 continue;
0072             }
0073             const int idx = layout->indexOf(widget);
0074             int rows, cols, rowSpan, colSpan;
0075             layout->getItemPosition(idx, &rows, &cols, &rowSpan, &colSpan);
0076             if (colSpan > 1) {
0077                 continue;
0078             }
0079 
0080             const int right = left + widget->sizeHint().width();
0081             if (maxRight < right) {
0082                 maxRight = right;
0083             }
0084         }
0085     }
0086 
0087     for (auto *l : std::as_const(_layouts)) {
0088         int left = getLeftMargin(l);
0089         l->setColumnMinimumWidth(LABELS_COLUMN, maxRight - left);
0090     }
0091 }
0092 
0093 int LabelsAligner::getLeftMargin(const QGridLayout *layout)
0094 {
0095     int left = layout->contentsMargins().left();
0096 
0097     if (layout->parent()->isWidgetType()) {
0098         auto *parentWidget = layout->parentWidget();
0099         Q_ASSERT(parentWidget);
0100         left += parentWidget->contentsMargins().left();
0101     } else {
0102         auto *parentLayout = qobject_cast<QLayout *>(layout->parent());
0103         Q_ASSERT(parentLayout);
0104         left += parentLayout->contentsMargins().left();
0105     }
0106 
0107     QWidget *parent = layout->parentWidget();
0108     while (parent != _refWidget && parent != nullptr) {
0109         left = parent->mapToParent(QPoint(left, 0)).x();
0110         parent = parent->parentWidget();
0111     }
0112     return left;
0113 }
0114 
0115 #include "moc_LabelsAligner.cpp"