diff --git a/krita/ui/widgets/kis_size_group_p.cpp b/krita/ui/widgets/kis_size_group_p.cpp index 5e0e4fec4e..5d43da5aa5 100644 --- a/krita/ui/widgets/kis_size_group_p.cpp +++ b/krita/ui/widgets/kis_size_group_p.cpp @@ -1,298 +1,296 @@ /* * Copyright (C) 2013 Juan Palacios * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "kis_size_group_p.h" #include #include #include #include #include #include KisSizeGroupPrivate::KisSizeGroupPrivate(KisSizeGroup *q_ptr, KisSizeGroup::mode mode, bool ignoreHidden) : QObject() , q(q_ptr) , m_mode(mode) , m_ignoreHidden(ignoreHidden) , m_updateTimer(new QTimer(q)) - , m_maxSizeHint(0, 0) + , m_sizeHint(0, 0) { Q_ASSERT(q_ptr); m_updateTimer->setSingleShot(true); m_updateTimer->setInterval(0); QObject::connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(updateSize())); } void KisSizeGroupPrivate::addWidget(QWidget *widget) { Q_ASSERT(widget); QWidget *parent = widget->parentWidget(); if (parent) { QLayout *layout = parent->layout(); if (layout) { // Search for the widget index and the QLayoutItem inside of the layout QLayoutItem *layoutItem = NULL; int layoutWidgetIndex = 0; for(int i = 0; i < layout->count(); ++i) { layoutItem = layout->itemAt(layoutWidgetIndex); if (layoutItem->widget() == widget) break; ++layoutWidgetIndex; } // We need to replace the layoutItem with an instance of GroupItem GroupItem *groupItem = dynamic_cast(layoutItem); if (groupItem) { // This widget is already inside of a group // One widget inside multiple groups is not supported Q_ASSERT(groupItem->getGroup() == this); } else { // LayoutItem is not an instance of WidgetItem // We need to create a new one groupItem = new GroupItem(widget); groupItem->setGroup(this); // Now we need to replace the layoutItem with the groupItem. // This step depends on the actual layout specialization. // Widget within a QFormLayout QFormLayout* formLayout = qobject_cast(layout); if (formLayout) { int row; QFormLayout::ItemRole role; formLayout->getItemPosition(layoutWidgetIndex, &row, &role); formLayout->removeItem(layoutItem); delete layoutItem; formLayout->setItem(row, role, groupItem); m_groupItems.append(groupItem); return; } // Widget within a QGridLayout QGridLayout *gridLayout = qobject_cast(layout); if (gridLayout) { int row, column, rowspan, columnspan; gridLayout->getItemPosition(layoutWidgetIndex, &row, &column, &rowspan, &columnspan); gridLayout->removeItem(layoutItem); delete layoutItem; gridLayout->addItem(groupItem, row, column, rowspan, columnspan); m_groupItems.append(groupItem); return; } // Widget within a QBoxLayout QBoxLayout *boxLayout = qobject_cast(layout); if (boxLayout) { boxLayout->removeItem(layoutItem); delete layoutItem; boxLayout->insertItem(layoutWidgetIndex, groupItem); m_groupItems.append(groupItem); return; } } } } } void KisSizeGroupPrivate::removeWidget(QWidget *widget) { Q_ASSERT(widget); QWidget *parent = widget->parentWidget(); if (parent) { QLayout *layout = parent->layout(); if (layout) { // Search the GroupItem of the widget inside of the GroupItem list GroupItem *widgetGroupItem = NULL; Q_FOREACH(GroupItem * groupItem, m_groupItems) { if (groupItem->widget() == widget) { widgetGroupItem = groupItem; break; } } if (widgetGroupItem) { m_groupItems.removeAll(widgetGroupItem); int layoutWidgetIndex = layout->indexOf(widget); // Now we need to replace the GroupItem with a QWidgetItem for the widget. // This step depends on the actual layout specialization. // Widget within a QFormLayout QFormLayout* formLayout = qobject_cast(layout); if (formLayout) { int row; QFormLayout::ItemRole role; formLayout->getItemPosition(layoutWidgetIndex, &row, &role); formLayout->removeItem(widgetGroupItem); delete widgetGroupItem; formLayout->setWidget(row, role, widget); return; } // Widget within a QGridLayout QGridLayout *gridLayout = qobject_cast(layout); if (gridLayout) { int row, column, rowspan, columnspan; gridLayout->getItemPosition(layoutWidgetIndex, &row, &column, &rowspan, &columnspan); gridLayout->removeItem(widgetGroupItem); delete widgetGroupItem; QWidgetItem *widgetItem = new QWidgetItem(widget); gridLayout->addItem(widgetItem, row, column, rowspan, columnspan); return; } // Widget within a QBoxLayout QBoxLayout *boxLayout = qobject_cast(layout); if (boxLayout) { boxLayout->removeItem(widgetGroupItem); delete widgetGroupItem; QWidgetItem *widgetItem = new QWidgetItem(widget); boxLayout->insertItem(layoutWidgetIndex, widgetItem); return; } } } } } void KisSizeGroupPrivate::scheduleSizeUpdate() { m_updateTimer->start(); } void KisSizeGroupPrivate::updateSize() { if (m_mode == KisSizeGroup::KIS_SIZE_GROUP_NONE) { // restore original widget size in each GroupItem Q_FOREACH(GroupItem *groupItem, m_groupItems) { groupItem->setSize(groupItem->widget()->sizeHint()); groupItem->widget()->updateGeometry(); } } else { // compute widgets size int width = 0; int height = 0; Q_FOREACH(GroupItem *groupItem, m_groupItems) { if (m_ignoreHidden && groupItem->hidden()) continue; const QWidget *widget = groupItem->widget(); width = qMax(widget->sizeHint().width(), width); height = qMax(widget->sizeHint().height(), height); } - m_maxSizeHint.setWidth(width); - m_maxSizeHint.setHeight(height); + m_sizeHint.setWidth(width); + m_sizeHint.setHeight(height); // update groupItem size Q_FOREACH(GroupItem *groupItem, m_groupItems) { if (m_ignoreHidden && groupItem->hidden()) continue; switch(m_mode) { case KisSizeGroup::KIS_SIZE_GROUP_HORIZONTAL: groupItem->setWidth(width); break; case KisSizeGroup::KIS_SIZE_GROUP_VERTICAL: groupItem->setHeight(height); break; case KisSizeGroup::KIS_SIZE_GROUP_BOTH: groupItem->setWidth(width); groupItem->setHeight(height); break; default: break; } // update layout to the new groupItem size groupItem->widget()->updateGeometry(); } } } GroupItem::GroupItem(QWidget* widget) : QObject() , QWidgetItem(widget) { Q_ASSERT(widget); m_size = widget->sizeHint(); m_hidden = !widget->isVisible(); widget->installEventFilter(this); } QSize GroupItem::sizeHint() const { return m_size; } QSize GroupItem::minimumSize() const { QSize size = QWidgetItem::minimumSize(); if (m_group->m_mode != KisSizeGroup::KIS_SIZE_GROUP_NONE) { - size = m_group->getMaxSizeHint(); + size = m_group->getSizeHint(); } return size; } bool GroupItem::eventFilter(QObject*, QEvent *event) { switch (event->type()) { case QEvent::Hide: if (!event->spontaneous()) { m_hidden = true; m_group->scheduleSizeUpdate(); } break; case QEvent::Show: if (!event->spontaneous()) { m_hidden = false; m_group->scheduleSizeUpdate(); } break; case QEvent::Resize: m_group->scheduleSizeUpdate(); break; default: break; } - - return false; } diff --git a/krita/ui/widgets/kis_size_group_p.h b/krita/ui/widgets/kis_size_group_p.h index 9f59e52c2c..84ba74dcbe 100644 --- a/krita/ui/widgets/kis_size_group_p.h +++ b/krita/ui/widgets/kis_size_group_p.h @@ -1,99 +1,99 @@ /* * Copyright (C) 2013 Juan Palacios * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #ifndef KISSIZEGROUPPRIVATE_H #define KISSIZEGROUPPRIVATE_H #include #include #include #include #include "kis_size_group.h" class QWidget; class QEvent; class QTimer; class GroupItem; class KisSizeGroupPrivate : public QObject { Q_OBJECT public: KisSizeGroupPrivate(KisSizeGroup *q_ptr, KisSizeGroup::mode mode, bool ignoreHidden); void addWidget(QWidget *widget); void removeWidget(QWidget *widget); /// Schedules an update of all widgets size void scheduleSizeUpdate(); - /// Returns the current maximunt size hint of all widgets inside the size group. - const QSize getMaxSizeHint() const { return m_maxSizeHint; } + /// Returns the size hint for the widgets contained in the size group. + const QSize getSizeHint() const { return m_sizeHint; } private Q_SLOTS: void updateSize(); public: KisSizeGroup* q; KisSizeGroup::mode m_mode; bool m_ignoreHidden; private: QTimer* m_updateTimer; // used to filter multiple calls to scheduleSizeUpdate() into one single updateSize() QList m_groupItems; - QSize m_maxSizeHint; + QSize m_sizeHint; }; class GroupItem : public QObject, public QWidgetItem { Q_OBJECT public: explicit GroupItem(QWidget* widget); ~GroupItem() {} void setSize(const QSize &size) { m_size = size; } int getWidth() const { return m_size.width(); } void setWidth(int width) { m_size.setWidth(width); } int getHeight() const { return m_size.height(); } void setHeight(int height) { m_size.setHeight(height); } bool hidden() const { return m_hidden; } KisSizeGroupPrivate* getGroup() { return m_group; } void setGroup(KisSizeGroupPrivate* group) { m_group = group; } QSize sizeHint() const; QSize minimumSize() const; bool eventFilter(QObject*, QEvent *event); private: bool m_hidden; QSize m_size; KisSizeGroupPrivate* m_group; }; #endif // KISSIZEGROUPPRIVATE_H