changeset 118:c28c0340fdf3

add borderlayout example
author mandel
date Thu, 04 Jun 2009 21:29:08 +0000
parents 670414f31f5f
children 97650cedf5d7
files examples/layouts/CMakeLists.txt examples/layouts/borderlayout/CMakeLists.txt examples/layouts/borderlayout/borderlayout.d examples/layouts/borderlayout/build examples/layouts/borderlayout/build.bat examples/layouts/borderlayout/main.d examples/layouts/borderlayout/window.d
diffstat 7 files changed, 385 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/examples/layouts/CMakeLists.txt	Thu Jun 04 15:07:48 2009 +0000
+++ b/examples/layouts/CMakeLists.txt	Thu Jun 04 21:29:08 2009 +0000
@@ -1,2 +1,3 @@
+add_subdirectory(borderlayout)
 add_subdirectory(basiclayouts)
 add_subdirectory(dynamiclayouts)
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/layouts/borderlayout/CMakeLists.txt	Thu Jun 04 21:29:08 2009 +0000
@@ -0,0 +1,1 @@
+build_example(borderlayout main.d borderlayout.d window.d)
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/layouts/borderlayout/borderlayout.d	Thu Jun 04 21:29:08 2009 +0000
@@ -0,0 +1,244 @@
+/****************************************************************************
+**
+** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: Qt Software Information (qt-info@nokia.com)
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial Usage
+** Licensees holding valid Qt Commercial licenses may use this file in
+** accordance with the Qt Commercial License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Nokia.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain
+** additional rights. These rights are described in the Nokia Qt LGPL
+** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
+** package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+** If you are unsure which license is appropriate for your use, please
+** contact the sales department at qt-sales@nokia.com.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+module borderlayout;
+
+
+import qt.gui.QLayout;
+import qt.gui.QWidgetItem;
+import qt.gui.QTextBrowser;
+import qt.core.QSize;
+import qt.core.QRect;
+
+
+class BorderLayout : public QLayout
+{
+public:
+
+	enum Position { West, North, South, East, Center };
+
+	this(QWidget parent, int margin = 0, int spacing = -1)
+	{
+		super(parent);
+		setMargin(margin);
+		setWidgetSpacing(spacing);
+	}
+
+	this(int spacing = -1)
+	{
+		setWidgetSpacing(spacing);
+	}
+
+	~this()
+	{
+		QLayoutItem l = takeAt(0);
+		while (l) {
+			delete l;
+			l = takeAt(0);
+		}
+	}
+
+	void addItem(IQLayoutItem item)
+	{
+		add(item, Position.West);
+	}
+
+	void addWidget(QWidget widget, Position position)
+	{
+		add(cast(IQLayoutItem) new QWidgetItem(widget), position);
+	}
+
+	int expandingDirections()
+	{
+		return Qt.Horizontal | Qt.Vertical;
+	}
+
+	bool hasHeightForWidth()
+	{
+		return false;
+	}
+
+	int count()
+	{
+		return list.length;
+	}
+
+	QLayoutItem itemAt(int index)
+	{
+		if(index >= 0 && index < list.length) 
+			return list[index].item;
+		else
+			return null;
+	}
+
+	QSize minimumSize()
+	{
+		return calculateSize(SizeType.MinimumSize);
+	}
+
+	void setGeometry(QRect rect)
+	{
+		ItemWrapper center = null;
+		int eastWidth = 0;
+		int westWidth = 0;
+		int northHeight = 0;
+		int southHeight = 0;
+		int centerHeight = 0;
+		int i;
+
+		super.setGeometry(rect);
+
+		for (i = 0; i < list.length; ++i) {
+			ItemWrapper wrapper = list[i];
+			IQLayoutItem item = wrapper.item;
+			Position position = wrapper.position;
+
+			if (position == Position.North) {
+				item.setGeometry(QRect(rect.x(), northHeight, rect.width(),
+					    item.sizeHint().height()));
+
+				northHeight += item.geometry().height() + widgetSpacing();
+			} else if (position == Position.South) {
+				item.setGeometry(QRect(item.geometry().x(),
+					    item.geometry().y(), rect.width(),
+					    item.sizeHint().height()));
+
+				southHeight += item.geometry().height() + widgetSpacing();
+
+				item.setGeometry(QRect(rect.x(),
+				      rect.y() + rect.height() - southHeight + widgetSpacing(),
+				      item.geometry().width(),
+				      item.geometry().height()));
+			} else if (position == Position.Center) {
+				center = wrapper;
+			}
+		}
+
+		centerHeight = rect.height() - northHeight - southHeight;
+
+		for (i = 0; i < list.length; ++i) {
+			ItemWrapper wrapper = list[i];
+			IQLayoutItem item = wrapper.item;
+			Position position = wrapper.position;
+
+			if (position == Position.West) {
+				item.setGeometry(QRect(rect.x() + westWidth, northHeight,
+					    item.sizeHint().width(), centerHeight));
+
+				westWidth += item.geometry().width() + widgetSpacing();
+			} else if (position == Position.East) {
+				item.setGeometry(QRect(item.geometry().x(), item.geometry().y(),
+					    item.sizeHint().width(), centerHeight));
+
+				eastWidth += item.geometry().width() + widgetSpacing();
+
+				item.setGeometry(QRect(
+				      rect.x() + rect.width() - eastWidth + widgetSpacing(),
+				      northHeight, item.geometry().width(),
+				      item.geometry().height()));
+			}
+		}
+
+		if (center)
+			center.item.setGeometry(QRect(westWidth, northHeight,
+				rect.width() - eastWidth - westWidth,
+				centerHeight));
+	}
+
+	QSize sizeHint()
+	{
+		return calculateSize(SizeType.SizeHint);
+	}
+
+	QLayoutItem takeAt(int index)
+	{
+		if (index >= 0 && index < list.length) {
+			ItemWrapper layoutStruct = list[index];
+			return layoutStruct.item;
+		}
+		return null;
+	}
+
+	void add(IQLayoutItem item, Position position)
+	{
+		list ~= new ItemWrapper(item, position);
+	}
+
+private:
+
+	class ItemWrapper
+	{
+		this(IQLayoutItem i, Position p)
+		{
+			item = i;
+			position = p;
+		}
+
+		IQLayoutItem item;
+		Position position;
+	};
+
+	enum SizeType { MinimumSize, SizeHint };
+
+	QSize calculateSize(SizeType sizeType)
+	{
+		QSize totalSize;
+
+		for (int i = 0; i < list.length; ++i) {
+			ItemWrapper wrapper = list[i];
+			Position position = wrapper.position;
+			QSize itemSize;
+
+			if (sizeType == SizeType.MinimumSize)
+				itemSize = wrapper.item.minimumSize();
+			else // (sizeType == SizeHint)
+				itemSize = wrapper.item.sizeHint();
+
+			if (position == Position.North || position == Position.South || position == Position.Center)
+				totalSize.setHeight(totalSize.height + itemSize.height);
+			
+			if (position == Position.West || position == Position.East || position == Position.Center)
+				totalSize.setWidth(totalSize.width + itemSize.width);
+		}
+		return totalSize;
+	}
+
+	ItemWrapper[] list;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/layouts/borderlayout/build	Thu Jun 04 21:29:08 2009 +0000
@@ -0,0 +1,3 @@
+#! /bin/bash
+
+dmd main.d borderlayout.d window.d -I../../../ -I../../../qt/d1 -L-L../../../lib -L-lqtdgui -L-lqtdcore -L-lQtCore -L-lQtGui -ofborderlayout
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/layouts/borderlayout/build.bat	Thu Jun 04 21:29:08 2009 +0000
@@ -0,0 +1,1 @@
+dmd main.d borderlayout.d window.d libqtdcore.lib libqtdgui.lib -I../../../ -I../../../qt/d1 -ofborderlayout
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/layouts/borderlayout/main.d	Thu Jun 04 21:29:08 2009 +0000
@@ -0,0 +1,55 @@
+/****************************************************************************
+**
+** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: Qt Software Information (qt-info@nokia.com)
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial Usage
+** Licensees holding valid Qt Commercial licenses may use this file in
+** accordance with the Qt Commercial License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Nokia.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain
+** additional rights. These rights are described in the Nokia Qt LGPL
+** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
+** package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+** If you are unsure which license is appropriate for your use, please
+** contact the sales department at qt-sales@nokia.com.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+module main;
+
+
+import qt.gui.QApplication;
+import window;
+
+
+int main(string[] args)
+{
+	scope app = new QApplication(args);
+	scope window = new Window;
+	window.show();
+	return app.exec();
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/layouts/borderlayout/window.d	Thu Jun 04 21:29:08 2009 +0000
@@ -0,0 +1,80 @@
+/****************************************************************************
+**
+** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: Qt Software Information (qt-info@nokia.com)
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial Usage
+** Licensees holding valid Qt Commercial licenses may use this file in
+** accordance with the Qt Commercial License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Nokia.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain
+** additional rights. These rights are described in the Nokia Qt LGPL
+** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
+** package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+** If you are unsure which license is appropriate for your use, please
+** contact the sales department at qt-sales@nokia.com.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+module window;
+
+
+import qt.gui.QTextBrowser;
+import qt.gui.QWidget;
+import qt.gui.QLabel;
+
+import borderlayout;
+
+
+class Window : public QWidget
+{
+public:
+
+	this()
+	{
+		QTextBrowser centralWidget = new QTextBrowser;
+		centralWidget.setPlainText(tr("Central widget"));
+
+		BorderLayout layout = new BorderLayout;
+		layout.addWidget(centralWidget, BorderLayout.Position.Center);
+		layout.addWidget(createLabel("North"), BorderLayout.Position.North);
+		layout.addWidget(createLabel("West"), BorderLayout.Position.West);
+		layout.addWidget(createLabel("East 1"), BorderLayout.Position.East);
+		layout.addWidget(createLabel("East 2") , BorderLayout.Position.East);
+		layout.addWidget(createLabel("South"), BorderLayout.Position.South);
+		setLayout(layout);
+
+		setWindowTitle(tr("Border Layout"));
+	}
+
+private:
+
+	QLabel createLabel(char[] text)
+	{
+		QLabel label = new QLabel(text);
+		label.setFrameStyle(QFrame.Box | QFrame.Raised);
+		return label;
+	}
+}