view examples/layouts/dynamiclayouts/dialog.d @ 101:4f909ae70e76

add dynamiclayouts example
author mandel
date Fri, 29 May 2009 13:25:41 +0000
parents
children 236401001115
line wrap: on
line source

/****************************************************************************
**
** 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 dialog;


import qt.gui.QDialog;
import qt.gui.QGroupBox;
import qt.gui.QGridLayout;
import qt.gui.QLabel;
import qt.gui.QComboBox;
import qt.gui.QDialogButtonBox;
import qt.gui.QPushButton;
import qt.gui.QMessageBox;
import qt.gui.QSpinBox;
import qt.gui.QDial;
import qt.gui.QProgressBar;


class Dialog : public QDialog
{

public:

	this(QWidget parent = null)
	{
		super(parent);
		createRotableGroupBox();
		createOptionsGroupBox();
		createButtonBox();

		mainLayout = new QGridLayout;
		mainLayout.addWidget(rotableGroupBox, 0, 0);
		mainLayout.addWidget(optionsGroupBox, 1, 0);
		mainLayout.addWidget(buttonBox, 2, 0);
		setLayout(mainLayout);

		mainLayout.setSizeConstraint(QLayout.SetMinimumSize);

		setWindowTitle(tr("Dynamic Layouts"));
	}

private:

	void buttonsOrientationChanged(int index)
	{
		mainLayout.setSizeConstraint(QLayout.SetNoConstraint);
		setMinimumSize(0, 0);

		Qt_Orientation orientation = cast(Qt_Orientation) buttonsOrientationComboBox.itemData(index).toInt();

		if (orientation == buttonBox.orientation())
			return;

		mainLayout.removeWidget(buttonBox);

		int spacing = mainLayout.spacing();

		QSize oldSizeHint = buttonBox.sizeHint() + QSize(spacing, spacing);
		buttonBox.setOrientation(orientation);
		QSize newSizeHint = buttonBox.sizeHint() + QSize(spacing, spacing);

		if (orientation == Qt_Orientation.Horizontal) {
			mainLayout.addWidget(buttonBox, 2, 0);
			resize(size() + QSize(-1 * oldSizeHint.width(), newSizeHint.height()));
		} else {
			mainLayout.addWidget(buttonBox, 0, 3, 2, 1);
			resize(size() + QSize(newSizeHint.width(), -1 * oldSizeHint.height()));
		}

		mainLayout.setSizeConstraint(QLayout.SetDefaultConstraint);
	}

	void rotateWidgets()
	{
		assert(rotableWidgets.length % 2 == 0);

		foreach (QWidget widget; rotableWidgets)
			rotableLayout.removeWidget(widget);

		rotableWidgets = rotableWidgets[1..$] ~ rotableWidgets[0];

		int n = rotableWidgets.length;
		for (int i = 0; i < n / 2; ++i) {
			rotableLayout.addWidget(rotableWidgets[n - i - 1], 0, i);
			rotableLayout.addWidget(rotableWidgets[i], 1, i);
		}
	}

	void help()
	{
		QMessageBox.information(this, tr("Dynamic Layouts Help"),
			tr("This example shows how to change layouts dynamically."));
	}

private:
	
	void createRotableGroupBox()
	{
		rotableGroupBox = new QGroupBox(tr("Rotable Widgets"));

		rotableWidgets ~= new QSpinBox;
		rotableWidgets ~= new QSlider;
		rotableWidgets ~= new QDial;
		rotableWidgets ~= new QProgressBar;

		int n = rotableWidgets.length;
		for (int i = 0; i < n; ++i) {
			rotableWidgets[i].valueChanged.connect(&rotableWidgets[(i + 1) % n].setValue);
		}

		rotableLayout = new QGridLayout;
		rotableGroupBox.setLayout(rotableLayout);

		rotateWidgets();
	}

	void createOptionsGroupBox()
	{
		optionsGroupBox = new QGroupBox(tr("Options"));

		buttonsOrientationLabel = new QLabel(tr("Orientation of buttons:"));

		buttonsOrientationComboBox = new QComboBox;
		buttonsOrientationComboBox.addItem(tr("Horizontal"), new QVariant(cast(ulong) Qt.Horizontal));
		buttonsOrientationComboBox.addItem(tr("Vertical"), new QVariant(cast(ulong) Qt.Vertical));

		buttonsOrientationComboBox.currentIndexChanged.connect(&this.buttonsOrientationChanged);

		optionsLayout = new QGridLayout;
		optionsLayout.addWidget(buttonsOrientationLabel, 0, 0);
		optionsLayout.addWidget(buttonsOrientationComboBox, 0, 1);
		optionsLayout.setColumnStretch(2, 1);
		optionsGroupBox.setLayout(optionsLayout);
	}

	void createButtonBox()
	{
		buttonBox = new QDialogButtonBox;

		closeButton = buttonBox.addButton(QDialogButtonBox.Close);
		helpButton = buttonBox.addButton(QDialogButtonBox.Help);
		rotateWidgetsButton = buttonBox.addButton(tr("Rotate &Widgets"), QDialogButtonBox.ActionRole);

		rotateWidgetsButton.clicked.connect(&this.rotateWidgets);
		closeButton.clicked.connect(&this.close);
		helpButton.clicked.connect(&this.help);
	}

	QGroupBox rotableGroupBox;
	QWidget[] rotableWidgets;

	QGroupBox optionsGroupBox;
	QLabel buttonsOrientationLabel;
	QComboBox buttonsOrientationComboBox;

	QDialogButtonBox buttonBox;
	QPushButton closeButton;
	QPushButton helpButton;
	QPushButton rotateWidgetsButton;

	QGridLayout mainLayout;
	QGridLayout rotableLayout;
	QGridLayout optionsLayout;
}