File indexing completed on 2024-05-12 15:53:55
0001 /** 0002 * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved. 0003 * 0004 * This file is part of the KD Chart library. 0005 * 0006 * This program is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU General Public License as 0008 * published by the Free Software Foundation; either version 2 of 0009 * the License, or (at your option) any later version. 0010 * 0011 * This program is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 * GNU General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU General Public License 0017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 0018 */ 0019 0020 #include <qapplication.h> 0021 #include <QWidget> 0022 #include <QDebug> 0023 #include <QBoxLayout> 0024 #include <QStyle> 0025 #include <QtTest/QtTest> 0026 #include <QGridLayout> 0027 #include <QLineEdit> 0028 0029 #define KDAB_REIMP 0030 0031 class MyLegendWidget : public QWidget 0032 { 0033 Q_OBJECT 0034 0035 public: 0036 explicit MyLegendWidget( QWidget* parent ) : QWidget( parent ) { 0037 } 0038 0039 void makeSizeFixed() { 0040 setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ); // like Legend 0041 setFixedSize( 50, 50 ); // hmm Legend has a layout instead. 0042 //QGridLayout* layout = new QGridLayout( this ); 0043 // ... 0044 } 0045 0046 protected: 0047 KDAB_REIMP void resizeEvent( QResizeEvent* ) override { 0048 // Note that this is never called unless the widget is shown. 0049 qDebug() << "resizeEvent " << size(); 0050 } 0051 }; 0052 0053 class MyWidgetItem : public QWidgetItem 0054 { 0055 public: 0056 explicit MyWidgetItem(QWidget *w) : QWidgetItem(w) { } 0057 0058 KDAB_REIMP bool isEmpty() const override { return false; } 0059 }; 0060 0061 class TestQLayout : public QObject 0062 { 0063 Q_OBJECT 0064 0065 private Q_SLOTS: 0066 0067 // This is very much like KChart::Chart does with legends 0068 void testBoxLayoutHiddenWidget() { 0069 QBoxLayout* vLayout = new QVBoxLayout; 0070 MyLegendWidget* widget1 = new MyLegendWidget( nullptr ); 0071 widget1->resize( 10, 10 ); 0072 0073 // Adding a hidden widget doesn't work, the layout ignores it 0074 // This is why we created MyWidgetItem above, then the layout works 0075 //vLayout->addWidget( widget1 ); 0076 MyWidgetItem* widgetItem = new MyWidgetItem( widget1 ); 0077 vLayout->addItem( widgetItem ); 0078 0079 QRect geom( 100, 100, 800, 800 ); 0080 vLayout->setGeometry( geom ); 0081 0082 //vLayout->activate(); // not needed 0083 0084 QCOMPARE( vLayout->geometry(), geom ); 0085 qDebug() << "widget1: " << widget1->geometry(); 0086 QCOMPARE( widget1->geometry(), geom ); 0087 0088 delete widget1; 0089 delete vLayout; 0090 } 0091 0092 void testBoxLayoutChildWidget() { 0093 QWidget* topLevelWidget = new QWidget( nullptr ); 0094 // This time the layout is associated with a widget, like d->layout in KChart::Chart. 0095 QBoxLayout* vLayout = new QVBoxLayout( topLevelWidget ); 0096 MyLegendWidget* widget1 = new MyLegendWidget( topLevelWidget ); 0097 MyWidgetItem* widgetItem = new MyWidgetItem( widget1 ); 0098 vLayout->addItem( widgetItem ); 0099 //vLayout->activate(); 0100 0101 QRect geom( 100, 100, 800, 800 ); 0102 vLayout->setGeometry( geom ); 0103 qDebug() << "widget1: " << widget1->geometry(); 0104 // int marg = topLevelWidget->style()->pixelMetric( QStyle::PM_DefaultTopLevelMargin ); 0105 int marg = vLayout->margin(); 0106 QCOMPARE( widget1->geometry(), geom.adjusted(marg,marg,-marg,-marg) ); 0107 0108 geom = QRect( 10, 10, 80, 80 ); 0109 vLayout->setGeometry( geom ); 0110 qDebug() << "widget1: " << widget1->geometry(); 0111 QCOMPARE( widget1->geometry(), geom.adjusted(marg,marg,-marg,-marg) ); 0112 0113 // And now let's show the widget for real 0114 geom = QRect( 0, 0, 500, 100 ); 0115 topLevelWidget->resize( geom.size() ); 0116 topLevelWidget->show(); 0117 QApplication::sendPostedEvents(); 0118 QRect expected = geom.adjusted(marg,marg,-marg,-marg); 0119 qDebug() << "widget1: " << widget1->frameGeometry() << "expecting" << expected; 0120 // this test is quite useless... 0121 //QCOMPARE( widget1->frameGeometry(), expected ); 0122 QVERIFY( widget1->isVisible() ); 0123 0124 delete topLevelWidget; 0125 } 0126 0127 void testSubGridLayout() { 0128 QWidget* topLevelWidget = new QWidget( nullptr ); 0129 QBoxLayout* vLayout = new QVBoxLayout( topLevelWidget ); 0130 QGridLayout* gridLayout = new QGridLayout(); 0131 0132 QLineEdit* lineEdit = new QLineEdit( topLevelWidget ); 0133 MyWidgetItem* lineEditWidgetItem = new MyWidgetItem( lineEdit ); 0134 gridLayout->addItem( lineEditWidgetItem, 0, 0 ); 0135 0136 MyLegendWidget* widget1 = new MyLegendWidget( topLevelWidget ); 0137 widget1->makeSizeFixed(); 0138 0139 MyWidgetItem* widgetItem = new MyWidgetItem( widget1 ); 0140 gridLayout->addItem( widgetItem, 1, 1 ); 0141 vLayout->addLayout( gridLayout ); 0142 0143 QRect geom( 100, 100, 800, 800 ); 0144 vLayout->setGeometry( geom ); 0145 qDebug() << "widget1: " << widget1->geometry(); 0146 QVERIFY( widget1->width() > 0 ); 0147 0148 delete topLevelWidget; 0149 } 0150 }; 0151 0152 QTEST_MAIN(TestQLayout) 0153 0154 #include "main.moc"