File indexing completed on 2024-05-19 04:36:40
0001 /* This file is part of the TikZKit project. 0002 * 0003 * Copyright (C) 2014 Dominik Haumann <dhaumann@kde.org> 0004 * 0005 * This library is free software; you can redistribute it and/or modify 0006 * it under the terms of the GNU Library General Public License as published 0007 * by the Free Software Foundation, either version 2 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This library is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU Library General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU Library General Public License 0016 * along with this library; see the file COPYING.LIB. If not, see 0017 * <http://www.gnu.org/licenses/>. 0018 */ 0019 0020 #include "ToolLayout.h" 0021 0022 #include <cmath> 0023 0024 #include <QWidget> 0025 #include <QDebug> 0026 0027 ToolLayout::ToolLayout(QWidget *parent) 0028 : QLayout(parent) 0029 { 0030 // by default, set margins to 0 0031 setContentsMargins(0, 0, 0, 0); 0032 } 0033 0034 ToolLayout::~ToolLayout() 0035 { 0036 qDeleteAll(m_items); 0037 m_items.clear(); 0038 } 0039 0040 void ToolLayout::addItem(QLayoutItem *item) 0041 { 0042 m_items.append(item); 0043 } 0044 0045 int ToolLayout::count() const 0046 { 0047 return m_items.size(); 0048 } 0049 0050 QLayoutItem *ToolLayout::itemAt(int index) const 0051 { 0052 return m_items.value(index); 0053 } 0054 0055 QLayoutItem *ToolLayout::takeAt(int index) 0056 { 0057 if (index < 0 || index >= m_items.size()) { 0058 return nullptr; 0059 } 0060 0061 return m_items.takeAt(index); 0062 } 0063 0064 Qt::Orientations ToolLayout::expandingDirections() const 0065 { 0066 return Qt::Orientations(); 0067 } 0068 0069 bool ToolLayout::hasHeightForWidth() const 0070 { 0071 return true; 0072 } 0073 0074 int ToolLayout::heightForWidth(int width) const 0075 { 0076 return doLayout(QRect(0, 0, width, 0), true); 0077 } 0078 0079 // find size containing all widgets 0080 static QSize sizeForItems(const QList<QLayoutItem *>& items) 0081 { 0082 QSize size; 0083 for (QLayoutItem * item : items) { 0084 size = size.expandedTo(item->minimumSize()); 0085 } 0086 return size; 0087 } 0088 0089 QSize ToolLayout::minimumSize() const 0090 { 0091 // in case the user set margins 0092 const auto margins = contentsMargins(); 0093 return sizeForItems(m_items) + QSize(margins.left() + margins.right(), margins.top() + margins.bottom()); 0094 } 0095 0096 QSize ToolLayout::sizeHint() const 0097 { 0098 return minimumSize(); 0099 } 0100 0101 void ToolLayout::setGeometry(const QRect &rect) 0102 { 0103 QLayout::setGeometry(rect); 0104 doLayout(rect); 0105 } 0106 0107 int ToolLayout::doLayout(const QRect &rect, bool dryRun) const 0108 { 0109 int left, top, right, bottom; 0110 getContentsMargins(&left, &top, &right, &bottom); 0111 const QRect contentsRect = rect.adjusted(left, top, -right, -bottom); 0112 const QSize itemSize = sizeForItems(m_items); 0113 const int itemsPerLine = qMax(1, contentsRect.width() / itemSize.width()); 0114 const qreal itemWidth = static_cast<qreal>(contentsRect.width()) / itemsPerLine; 0115 0116 if (!dryRun) { 0117 qreal x = contentsRect.left(); 0118 int y = contentsRect.top(); 0119 int i = 0; 0120 while (i < m_items.size()) { 0121 0122 m_items[i]->widget()->setGeometry(QRect(QPoint(x, y), QSize(itemWidth, itemSize.height()))); 0123 x += itemWidth; 0124 0125 ++i; 0126 0127 if (i % itemsPerLine == 0) { 0128 y += itemSize.height(); 0129 x = contentsRect.left(); 0130 } 0131 } 0132 } 0133 0134 if (m_items.size()) { 0135 return top + bottom + itemSize.height() * std::ceil(static_cast<qreal>(m_items.size()) / itemsPerLine); 0136 } 0137 return sizeHint().height(); 0138 } 0139 0140 // kate: indent-width 4; replace-tabs on;