diff options
Diffstat (limited to 'doc/codesnippets/examples')
40 files changed, 4581 insertions, 0 deletions
diff --git a/doc/codesnippets/examples/dialogs/classwizard/classwizard.cpp b/doc/codesnippets/examples/dialogs/classwizard/classwizard.cpp new file mode 100644 index 000000000..5814d5f44 --- /dev/null +++ b/doc/codesnippets/examples/dialogs/classwizard/classwizard.cpp @@ -0,0 +1,250 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (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 http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] //! [1] +def __init__(self, parent): + QWizard.__init__(self, parent): + self.addPage(IntroPage()) + self.addPage(ClassInfoPage()) + self.addPage(CodeStylePage()) + self.addPage(OutputFilesPage()) + self.addPage(ConclusionPage()) +//! [0] + + self.setPixmap(QWizard.BannerPixmap, QPixmap(":/images/banner.png")) + self.setPixmap(QWizard.BackgroundPixmap, QPixmap(":/images/background.png")) + + self.setWindowTitle(self.tr("Class Wizard")) +//! [2] + +//! [1] //! [2] + +//! [3] +def accept(self): +//! [3] //! [4] + className = self.field("className").toByteArray() + baseClass = self.field("baseClass").toByteArray() + macroName = self.field("macroName").toByteArray() + baseInclude = self.field("baseInclude").toByteArray() + + outputDir = self.field("outputDir").toString() + header = self.field("header").toString() + implementation = self.field("implementation").toString() +//! [4] + +... + +//! [5] + QDialog.accept(self) +//! [5] //! [6] +} +//! [6] + +//! [7] +class IntroPage (QWizardPage): + + def __init__(self, parent): + QWizardPage.__init__(self, parent) + + self.setTitle(tr("Introduction")) + self.setPixmap(QWizard.WatermarkPixmap, QPixmap(":/images/watermark1.png")) + + label = QLabel(self.tr("This wizard will generate a skeleton C++ class " \ + "definition, including a few functions. You simply " \ + "need to specify the class name and set a few " \ + "options to produce a header file and an " \ + "implementation file for your new C++ class.")) + label.setWordWrap(True) + + layout = QVBoxLayout() + layout.addWidget(label) + self.setLayout(layout) +} +//! [7] + +//! [8] //! [9] +class ClassInfoPage(QWizardPage): + + def __init__(self, parent): + QWizardPage.__init__(self, parent) +//! [8] + self.setTitle(self.tr("Class Information")) + self.setSubTitle(self.tr("Specify basic information about the class for which you " \ + "want to generate skeleton source code files.")) + self.setPixmap(QWizard.LogoPixmap, QPixmap(":/images/logo1.png")) + +//! [10] + classNameLabel = QLabel(self.tr("&Class name:")) + classNameLineEdit = QLineEdit() + classNameLabel.setBuddy(classNameLineEdit) + + baseClassLabel = QLabel(self.tr("B&ase class:")) + baseClassLineEdit = QLineEdit() + baseClassLabel.setBuddy(baseClassLineEdit) + + qobjectMacroCheckBox = QCheckBox(self.tr("Generate Q_OBJECT ¯o")) + +//! [10] + groupBox = QGroupBox(self.tr("C&onstructor")) +//! [9] + + qobjectCtorRadioButton = QRadioButton(self.tr("&QObject-style constructor")) + qwidgetCtorRadioButton = QRadioButton(self.tr("Q&Widget-style constructor")) + defaultCtorRadioButton = QRadioButton(self.tr("&Default constructor")) + copyCtorCheckBox = QCheckBox(self.tr("&Generate copy constructor and operator=")) + + defaultCtorRadioButton.setChecked(True) + + self.connect(defaultCtorRadioButton, SIGNAL("toggled(bool)"), + copyCtorCheckBox, SLOT("setEnabled(bool)")) + +//! [11] //! [12] + registerField("className*", classNameLineEdit) + registerField("baseClass", baseClassLineEdit) + registerField("qobjectMacro", qobjectMacroCheckBox) +//! [11] + registerField("qobjectCtor", qobjectCtorRadioButton) + registerField("qwidgetCtor", qwidgetCtorRadioButton) + registerField("defaultCtor", defaultCtorRadioButton) + registerField("copyCtor", copyCtorCheckBox) + + groupBoxLayout = QVBoxLayout() +//! [12] + groupBoxLayout.addWidget(qobjectCtorRadioButton) + groupBoxLayout.addWidget(qwidgetCtorRadioButton) + groupBoxLayout.addWidget(defaultCtorRadioButton) + groupBoxLayout.addWidget(copyCtorCheckBox) + groupBox.setLayout(groupBoxLayout) + + layout = QGridLayout() + layout.addWidget(classNameLabel, 0, 0) + layout.addWidget(classNameLineEdit, 0, 1) + layout.addWidget(baseClassLabel, 1, 0) + layout.addWidget(baseClassLineEdit, 1, 1) + layout.addWidget(qobjectMacroCheckBox, 2, 0, 1, 2) + layout.addWidget(groupBox, 3, 0, 1, 2) + self.setLayout(layout) +//! [13] + +//! [13] + +//! [14] +class CodeStylePage(QWizardPage): + + def __init__(self, parent): + QWizardPage.__init__(self, parent) + self.setTitle(tr("Code Style Options")) + self.setSubTitle(tr("Choose the formatting of the generated code.")) + self.setPixmap(QWizard.LogoPixmap, QPixmap(":/images/logo2.png")) + + commentCheckBox = QCheckBox(self.tr("&Start generated files with a comment")) +//! [14] + commentCheckBox.setChecked(True) + + protectCheckBox = QCheckBox(self.tr("&Protect header file against multiple " \ + "inclusions")) + protectCheckBox.setChecked(True) + + macroNameLabel = QLabel(self.tr("&Macro name:")) + macroNameLineEdit = QLineEdit() + macroNameLabel.setBuddy(macroNameLineEdit) + + includeBaseCheckBox = QCheckBox(self.tr("&Include base class definition")) + baseIncludeLabel = QLabel(self.tr("Base class include:")) + baseIncludeLineEdit = QLineEdit() + baseIncludeLabel.setBuddy(baseIncludeLineEdit) + + self.connect(protectCheckBox, SIGNAL("toggled(bool)"), + macroNameLabel, SLOT("setEnabled(bool)")) + self.connect(protectCheckBox, SIGNAL("toggled(bool)"), + macroNameLineEdit, SLOT("setEnabled(bool)")) + self.connect(includeBaseCheckBox, SIGNAL("toggled(bool)"), + baseIncludeLabel, SLOT("setEnabled(bool)")) + self.connect(includeBaseCheckBox, SIGNAL(toggled(bool)), + baseIncludeLineEdit, SLOT("setEnabled(bool)")) + + self.registerField("comment", commentCheckBox) + self.registerField("protect", protectCheckBox) + self.registerField("macroName", macroNameLineEdit) + self.registerField("includeBase", includeBaseCheckBox) + self.registerField("baseInclude", baseIncludeLineEdit) + + layout = QGridLayout() + layout.setColumnMinimumWidth(0, 20) + layout.addWidget(commentCheckBox, 0, 0, 1, 3) + layout.addWidget(protectCheckBox, 1, 0, 1, 3) + layout.addWidget(macroNameLabel, 2, 1) + layout.addWidget(macroNameLineEdit, 2, 2) + layout.addWidget(includeBaseCheckBox, 3, 0, 1, 3) + layout.addWidget(baseIncludeLabel, 4, 1) + layout.addWidget(baseIncludeLineEdit, 4, 2) +//! [15] + self.setLayout(layout) +} +//! [15] + +//! [16] + def initializePage(self): + className = self.field("className").toString() + self.macroNameLineEdit.setText(className.toUpper() + "_H") + + baseClass = self.field("baseClass").toString() + + self.includeBaseCheckBox.setChecked(not baseClass.isEmpty()) + self.includeBaseCheckBox.setEnabled(not baseClass.isEmpty()) + self.baseIncludeLabel.setEnabled(not baseClass.isEmpty()) + self.baseIncludeLineEdit.setEnabled(not baseClass.isEmpty()) + + if baseClass.isEmpty(): + self.baseIncludeLineEdit.clear() + elsif QRegExp("Q[A-Z].*").exactMatch(baseClass): + baseIncludeLineEdit.setText("<" + baseClass + ">") + else: + baseIncludeLineEdit.setText("\"" + baseClass.toLower() + ".h\"") +//! [16] + +//! [17] + def initializePage(self): + className = field("className").toString() + self.headerLineEdit.setText(className.toLower() + ".h") + self.implementationLineEdit.setText(className.toLower() + ".cpp") + self.outputDirLineEdit.setText(QDir.convertSeparators(QDir.tempPath())) +//! [17] diff --git a/doc/codesnippets/examples/dialogs/classwizard/classwizard.qrc b/doc/codesnippets/examples/dialogs/classwizard/classwizard.qrc new file mode 100644 index 000000000..41a5ddc7d --- /dev/null +++ b/doc/codesnippets/examples/dialogs/classwizard/classwizard.qrc @@ -0,0 +1,11 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>images/background.png</file> + <file>images/banner.png</file> + <file>images/logo1.png</file> + <file>images/logo2.png</file> + <file>images/logo3.png</file> + <file>images/watermark1.png</file> + <file>images/watermark2.png</file> +</qresource> +</RCC> diff --git a/doc/codesnippets/examples/dialogs/classwizard/images/background.png b/doc/codesnippets/examples/dialogs/classwizard/images/background.png Binary files differnew file mode 100644 index 000000000..44c7badb8 --- /dev/null +++ b/doc/codesnippets/examples/dialogs/classwizard/images/background.png diff --git a/doc/codesnippets/examples/dialogs/classwizard/images/banner.png b/doc/codesnippets/examples/dialogs/classwizard/images/banner.png Binary files differnew file mode 100644 index 000000000..3169152b8 --- /dev/null +++ b/doc/codesnippets/examples/dialogs/classwizard/images/banner.png diff --git a/doc/codesnippets/examples/dialogs/classwizard/images/logo1.png b/doc/codesnippets/examples/dialogs/classwizard/images/logo1.png Binary files differnew file mode 100644 index 000000000..f9b594aaf --- /dev/null +++ b/doc/codesnippets/examples/dialogs/classwizard/images/logo1.png diff --git a/doc/codesnippets/examples/dialogs/classwizard/images/logo2.png b/doc/codesnippets/examples/dialogs/classwizard/images/logo2.png Binary files differnew file mode 100644 index 000000000..5dcbd4669 --- /dev/null +++ b/doc/codesnippets/examples/dialogs/classwizard/images/logo2.png diff --git a/doc/codesnippets/examples/dialogs/classwizard/images/logo3.png b/doc/codesnippets/examples/dialogs/classwizard/images/logo3.png Binary files differnew file mode 100644 index 000000000..9fd3ea235 --- /dev/null +++ b/doc/codesnippets/examples/dialogs/classwizard/images/logo3.png diff --git a/doc/codesnippets/examples/dialogs/classwizard/images/watermark1.png b/doc/codesnippets/examples/dialogs/classwizard/images/watermark1.png Binary files differnew file mode 100644 index 000000000..0091f5c17 --- /dev/null +++ b/doc/codesnippets/examples/dialogs/classwizard/images/watermark1.png diff --git a/doc/codesnippets/examples/dialogs/classwizard/images/watermark2.png b/doc/codesnippets/examples/dialogs/classwizard/images/watermark2.png Binary files differnew file mode 100644 index 000000000..3b88f2e36 --- /dev/null +++ b/doc/codesnippets/examples/dialogs/classwizard/images/watermark2.png diff --git a/doc/codesnippets/examples/dialogs/classwizard/main.cpp b/doc/codesnippets/examples/dialogs/classwizard/main.cpp new file mode 100644 index 000000000..b6107dd6f --- /dev/null +++ b/doc/codesnippets/examples/dialogs/classwizard/main.cpp @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (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 http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> +#include <QTranslator> +#include <QLocale> +#include <QLibraryInfo> + +#include "classwizard.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(classwizard); + + QApplication app(argc, argv); + + QString translatorFileName = QLatin1String("qt_"); + translatorFileName += QLocale::system().name(); + QTranslator *translator = new QTranslator(&app); + if (translator->load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) + app.installTranslator(translator); + + ClassWizard wizard; + wizard.show(); + return app.exec(); +} diff --git a/doc/codesnippets/examples/dialogs/extension/finddialog.cpp b/doc/codesnippets/examples/dialogs/extension/finddialog.cpp new file mode 100644 index 000000000..3ccf8471e --- /dev/null +++ b/doc/codesnippets/examples/dialogs/extension/finddialog.cpp @@ -0,0 +1,110 @@ +############################################################################ +## +## Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +## Contact: Qt Software Information (qt-info@nokia.com) +## +## This file is part of the example classes of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:LGPL$ +## Commercial Usage +## Licensees holding valid Qt Commercial licenses may use self 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, self 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 self 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 self +## package. +## +## GNU General Public License Usage +## Alternatively, self 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 self 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$ +## +############################################################################ + + +from PySide.QtGui import * + +//! [0] +def __init__(self, parent): + QDialog.__init__(self, parent) + label = QLabel(self.tr("Find &what:")) + lineEdit = QLineEdit() + label.setBuddy(lineEdit) + + caseCheckBox = QCheckBox(self.tr("Match &case")) + fromStartCheckBox = QCheckBox(self.tr("Search from &start")) + fromStartCheckBox.setChecked(True) + +//! [1] + findButton = QPushButton(self.tr("&Find")) + findButton.setDefault(True) + + moreButton = QPushButton(self.tr("&More")) + moreButton.setCheckable(True) +//! [0] + moreButton.setAutoDefault(False) + + buttonBox = QDialogButtonBox(Qt.Vertical) + buttonBox.addButton(findButton, QDialogButtonBox.ActionRole) + buttonBox.addButton(moreButton, QDialogButtonBox.ActionRole) +//! [1] + +//! [2] + extension = QWidget() + + wholeWordsCheckBox = QCheckBox(self.tr("&Whole words")) + backwardCheckBox = QCheckBox(self.tr("Search &backward")) + searchSelectionCheckBox = QCheckBox(self.tr("Search se&lection")) +//! [2] + +//! [3] + connect(moreButton, SIGNAL("toggled(bool)"), extension, SLOT("setVisible(bool)")) + + QVBoxLayout *extensionLayout = QVBoxLayout + extensionLayout.setMargin(0) + extensionLayout.addWidget(wholeWordsCheckBox) + extensionLayout.addWidget(backwardCheckBox) + extensionLayout.addWidget(searchSelectionCheckBox) + extension.setLayout(extensionLayout) +//! [3] + +//! [4] + topLeftLayout = QHBoxLayout + topLeftLayout.addWidget(label) + topLeftLayout.addWidget(lineEdit) + + leftLayout = QVBoxLayout + leftLayout.addLayout(topLeftLayout) + leftLayout.addWidget(caseCheckBox) + leftLayout.addWidget(fromStartCheckBox) + leftLayout.addSself.tretch(1) + + mainLayout = QGridLayout + mainLayout.setSizeConsself.traint(QLayout.SetFixedSize) + mainLayout.addLayout(leftLayout, 0, 0) + mainLayout.addWidget(buttonBox, 0, 1) + mainLayout.addWidget(extension, 1, 0, 1, 2) + setLayout(mainLayout) + + setWindowTitle(self.tr("Extension")) +//! [4] //! [5] + extension.hide() +//! [5] diff --git a/doc/codesnippets/examples/dialogs/licensewizard/images/logo.png b/doc/codesnippets/examples/dialogs/licensewizard/images/logo.png Binary files differnew file mode 100644 index 000000000..56f17a3e5 --- /dev/null +++ b/doc/codesnippets/examples/dialogs/licensewizard/images/logo.png diff --git a/doc/codesnippets/examples/dialogs/licensewizard/images/watermark.png b/doc/codesnippets/examples/dialogs/licensewizard/images/watermark.png Binary files differnew file mode 100644 index 000000000..124a05068 --- /dev/null +++ b/doc/codesnippets/examples/dialogs/licensewizard/images/watermark.png diff --git a/doc/codesnippets/examples/dialogs/licensewizard/licensewizard.cpp b/doc/codesnippets/examples/dialogs/licensewizard/licensewizard.cpp new file mode 100644 index 000000000..5229acdf3 --- /dev/null +++ b/doc/codesnippets/examples/dialogs/licensewizard/licensewizard.cpp @@ -0,0 +1,349 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (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 http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "licensewizard.h" + +//! [0] //! [1] //! [2] +# class LicenseWizard +def __init__(self, parent) + QWizard(self, parent) +//! [0] + self.setPage(self.Page_Intro, IntroPage()) + self.setPage(self.Page_Evaluate, EvaluatePage()) + self.setPage(self.Page_Register, RegisterPage()) + self.setPage(self.Page_Details, DetailsPage()) + self.setPage(self.Page_Conclusion, ConclusionPage()) +//! [1] + + self.setStartId(self.Page_Intro); +//! [2] + +//! [3] + +//! [3] //! [4] + self.setWizardStyle(QWizard.ModernStyle); + +//! [4] //! [5] + self.setOption(HaveHelpButton, True); +//! [5] //! [6] + self.setPixmap(QWizard.LogoPixmap, QPixmap(":/images/logo.png")); + +//! [7] + connect(this, SIGNAL(helpRequested()), this, SLOT(showHelp())); +//! [7] + + setWindowTitle(tr("License Wizard")); +//! [8] +} +//! [6] //! [8] + +//! [9] //! [10] +void LicenseWizard::showHelp() +//! [9] //! [11] +{ + static QString lastHelpMessage; + + QString message; + + switch (currentId()) { + case Page_Intro: + message = tr("The decision you make here will affect which page you " + "get to see next."); + break; +//! [10] //! [11] + case Page_Evaluate: + message = tr("Make sure to provide a valid email address, such as " + "toni.buddenbrook@example.de."); + break; + case Page_Register: + message = tr("If you don't provide an upgrade key, you will be " + "asked to fill in your details."); + break; + case Page_Details: + message = tr("Make sure to provide a valid email address, such as " + "thomas.gradgrind@example.co.uk."); + break; + case Page_Conclusion: + message = tr("You must accept the terms and conditions of the " + "license to proceed."); + break; +//! [12] //! [13] + default: + message = tr("This help is likely not to be of any help."); + } +//! [12] + + if (lastHelpMessage == message) + message = tr("Sorry, I already gave what help I could. " + "Maybe you should try asking a human?"); + +//! [14] + QMessageBox::information(this, tr("License Wizard Help"), message); +//! [14] + + lastHelpMessage = message; +//! [15] +} +//! [13] //! [15] + +//! [16] +IntroPage::IntroPage(QWidget *parent) + : QWizardPage(parent) +{ + setTitle(tr("Introduction")); + setPixmap(QWizard::WatermarkPixmap, QPixmap(":/images/watermark.png")); + + topLabel = new QLabel(tr("This wizard will help you register your copy of " + "<i>Super Product One</i>™ or start " + "evaluating the product.")); + topLabel->setWordWrap(true); + + registerRadioButton = new QRadioButton(tr("&Register your copy")); + evaluateRadioButton = new QRadioButton(tr("&Evaluate the product for 30 " + "days")); + registerRadioButton->setChecked(true); + + QVBoxLayout *layout = new QVBoxLayout; + layout->addWidget(topLabel); + layout->addWidget(registerRadioButton); + layout->addWidget(evaluateRadioButton); + setLayout(layout); +} +//! [16] //! [17] + +//! [18] +# class IntroPage +def nextId(self): +//! [17] //! [19] + if evaluateRadioButton.isChecked(): + return LicenseWizard.Page_Evaluate + else: + return LicenseWizard.Page_Register +//! [18] //! [19] + +//! [20] +EvaluatePage::EvaluatePage(QWidget *parent) + : QWizardPage(parent) +{ + setTitle(tr("Evaluate <i>Super Product One</i>™")); + setSubTitle(tr("Please fill both fields. Make sure to provide a valid " + "email address (e.g., john.smith@example.com).")); + + nameLabel = new QLabel(tr("N&ame:")); + nameLineEdit = new QLineEdit; +//! [20] + nameLabel->setBuddy(nameLineEdit); + + emailLabel = new QLabel(tr("&Email address:")); + emailLineEdit = new QLineEdit; + emailLineEdit->setValidator(new QRegExpValidator(QRegExp(".*@.*"), this)); + emailLabel->setBuddy(emailLineEdit); + +//! [21] + registerField("evaluate.name*", nameLineEdit); + registerField("evaluate.email*", emailLineEdit); +//! [21] + + QGridLayout *layout = new QGridLayout; + layout->addWidget(nameLabel, 0, 0); + layout->addWidget(nameLineEdit, 0, 1); + layout->addWidget(emailLabel, 1, 0); + layout->addWidget(emailLineEdit, 1, 1); + setLayout(layout); +//! [22] +} +//! [22] + +//! [23] +# class EvaluatePage +def nextId(self): + return LicenseWizard.Page_Conclusion +//! [23] + +RegisterPage::RegisterPage(QWidget *parent) + : QWizardPage(parent) +{ + setTitle(tr("Register Your Copy of <i>Super Product One</i>™")); + setSubTitle(tr("If you have an upgrade key, please fill in " + "the appropriate field.")); + + nameLabel = new QLabel(tr("N&ame:")); + nameLineEdit = new QLineEdit; + nameLabel->setBuddy(nameLineEdit); + + upgradeKeyLabel = new QLabel(tr("&Upgrade key:")); + upgradeKeyLineEdit = new QLineEdit; + upgradeKeyLabel->setBuddy(upgradeKeyLineEdit); + + registerField("register.name*", nameLineEdit); + registerField("register.upgradeKey", upgradeKeyLineEdit); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(nameLabel, 0, 0); + layout->addWidget(nameLineEdit, 0, 1); + layout->addWidget(upgradeKeyLabel, 1, 0); + layout->addWidget(upgradeKeyLineEdit, 1, 1); + setLayout(layout); +} + +//! [24] +# class RegisterPage +def nextId(self): + if self.upgradeKeyLineEdit.text().isEmpty(): + return LicenseWizard::Page_Details + else: + return LicenseWizard::Page_Conclusion +//! [24] + +DetailsPage::DetailsPage(QWidget *parent) + : QWizardPage(parent) +{ + setTitle(tr("Fill In Your Details")); + setSubTitle(tr("Please fill all three fields. Make sure to provide a valid " + "email address (e.g., tanaka.aya@example.co.jp).")); + + companyLabel = new QLabel(tr("&Company name:")); + companyLineEdit = new QLineEdit; + companyLabel->setBuddy(companyLineEdit); + + emailLabel = new QLabel(tr("&Email address:")); + emailLineEdit = new QLineEdit; + emailLineEdit->setValidator(new QRegExpValidator(QRegExp(".*@.*"), this)); + emailLabel->setBuddy(emailLineEdit); + + postalLabel = new QLabel(tr("&Postal address:")); + postalLineEdit = new QLineEdit; + postalLabel->setBuddy(postalLineEdit); + + registerField("details.company*", companyLineEdit); + registerField("details.email*", emailLineEdit); + registerField("details.postal*", postalLineEdit); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(companyLabel, 0, 0); + layout->addWidget(companyLineEdit, 0, 1); + layout->addWidget(emailLabel, 1, 0); + layout->addWidget(emailLineEdit, 1, 1); + layout->addWidget(postalLabel, 2, 0); + layout->addWidget(postalLineEdit, 2, 1); + setLayout(layout); +} + +//! [25] +# class DetailsPage +def nextId(self): + return LicenseWizard.Page_Conclusion +//! [25] + +ConclusionPage::ConclusionPage(QWidget *parent) + : QWizardPage(parent) +{ + setTitle(tr("Complete Your Registration")); + setPixmap(QWizard::WatermarkPixmap, QPixmap(":/images/watermark.png")); + + bottomLabel = new QLabel; + bottomLabel->setWordWrap(true); + + agreeCheckBox = new QCheckBox(tr("I agree to the terms of the license")); + + registerField("conclusion.agree*", agreeCheckBox); + + QVBoxLayout *layout = new QVBoxLayout; + layout->addWidget(bottomLabel); + layout->addWidget(agreeCheckBox); + setLayout(layout); +} + +//! [26] +#class ConclusionPage +def nextId(self): + return -1 +//! [26] + +//! [27] +# class ConclusionPage +def initializePage(self): + if wizard().hasVisitedPage(LicenseWizard::Page_Evaluate): + licenseText = self.tr("<u>Evaluation License Agreement:</u> " \ + "You can use this software for 30 days and make one " \ + "backup, but you are not allowed to distribute it.") + elsif wizard().hasVisitedPage(LicenseWizard.Page_Details): + licenseText = self.tr("<u>First-Time License Agreement:</u> " \ + "You can use this software subject to the license " \ + "you will receive by email.") + else: + licenseText = self.tr("<u>Upgrade License Agreement:</u> " \ + "This software is licensed under the terms of your " \ + "current license.") + } + bottomLabel.setText(licenseText) +//! [27] + +//! [28] +void ConclusionPage::setVisible(bool visible) +{ + QWizardPage::setVisible(visible); + + if (visible) { +//! [29] + self.wizard().setButtonText(QWizard.CustomButton1, self.tr("&Print")) + self.wizard().setOption(QWizard.HaveCustomButton1, True) + self.connect(wizard(), SIGNAL("customButtonClicked(int)"), self, SLOT("printButtonClicked()")) +//! [29] + } else { + wizard()->setOption(QWizard::HaveCustomButton1, false); + disconnect(wizard(), SIGNAL(customButtonClicked(int)), + this, SLOT(printButtonClicked())); + } +} +//! [28] + +void ConclusionPage::printButtonClicked() +{ + QPrinter printer; + QPrintDialog dialog(&printer, this); + if (dialog.exec()) + QMessageBox::warning(this, tr("Print License"), + tr("As an environmentally friendly measure, the " + "license text will not actually be printed.")); +} diff --git a/doc/codesnippets/examples/dialogs/licensewizard/licensewizard.h b/doc/codesnippets/examples/dialogs/licensewizard/licensewizard.h new file mode 100644 index 000000000..b35337715 --- /dev/null +++ b/doc/codesnippets/examples/dialogs/licensewizard/licensewizard.h @@ -0,0 +1,164 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (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 http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef LICENSEWIZARD_H +#define LICENSEWIZARD_H + +#include <QWizard> + +QT_BEGIN_NAMESPACE +class QCheckBox; +class QLabel; +class QLineEdit; +class QRadioButton; +QT_END_NAMESPACE + +//! [0] //! [1] +class LicenseWizard (QWizard): +//! [0] +//! [2] + Page_Intro = 1 + Page_Evaluate = 2 + Page_Register = 3 + Page_Details = 4 + Page_Conclusion = 5 +//! [2] + + def __init__(self, parent): + ... + + def showHelp(self): + ... +//! [3] + +//! [1] //! [3] + +//! [4] +class IntroPage : public QWizardPage +{ + Q_OBJECT + +public: + IntroPage(QWidget *parent = 0); + + int nextId() const; + +private: + QLabel *topLabel; + QRadioButton *registerRadioButton; + QRadioButton *evaluateRadioButton; +}; +//! [4] + +//! [5] +class EvaluatePage : public QWizardPage +{ + Q_OBJECT + +public: + EvaluatePage(QWidget *parent = 0); + + int nextId() const; + +private: + QLabel *nameLabel; + QLabel *emailLabel; + QLineEdit *nameLineEdit; + QLineEdit *emailLineEdit; +}; +//! [5] + +class RegisterPage : public QWizardPage +{ + Q_OBJECT + +public: + RegisterPage(QWidget *parent = 0); + + int nextId() const; + +private: + QLabel *nameLabel; + QLabel *upgradeKeyLabel; + QLineEdit *nameLineEdit; + QLineEdit *upgradeKeyLineEdit; +}; + +class DetailsPage : public QWizardPage +{ + Q_OBJECT + +public: + DetailsPage(QWidget *parent = 0); + + int nextId() const; + +private: + QLabel *companyLabel; + QLabel *emailLabel; + QLabel *postalLabel; + QLineEdit *companyLineEdit; + QLineEdit *emailLineEdit; + QLineEdit *postalLineEdit; +}; + +//! [6] +class ConclusionPage : public QWizardPage +{ + Q_OBJECT + +public: + ConclusionPage(QWidget *parent = 0); + + void initializePage(); + int nextId() const; + void setVisible(bool visible); + +private slots: + void printButtonClicked(); + +private: + QLabel *bottomLabel; + QCheckBox *agreeCheckBox; +}; +//! [6] + +#endif diff --git a/doc/codesnippets/examples/dialogs/licensewizard/licensewizard.pro b/doc/codesnippets/examples/dialogs/licensewizard/licensewizard.pro new file mode 100644 index 000000000..fd5e37ccf --- /dev/null +++ b/doc/codesnippets/examples/dialogs/licensewizard/licensewizard.pro @@ -0,0 +1,10 @@ +HEADERS = licensewizard.h +SOURCES = licensewizard.cpp \ + main.cpp +RESOURCES = licensewizard.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/dialogs/licensewizard +sources.files = $$SOURCES $$HEADERS $$FORMS $$RESOURCES *.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/dialogs/licensewizard +INSTALLS += target sources diff --git a/doc/codesnippets/examples/dialogs/licensewizard/licensewizard.qrc b/doc/codesnippets/examples/dialogs/licensewizard/licensewizard.qrc new file mode 100644 index 000000000..b06993851 --- /dev/null +++ b/doc/codesnippets/examples/dialogs/licensewizard/licensewizard.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>images/logo.png</file> + <file>images/watermark.png</file> +</qresource> +</RCC> diff --git a/doc/codesnippets/examples/dialogs/licensewizard/main.cpp b/doc/codesnippets/examples/dialogs/licensewizard/main.cpp new file mode 100644 index 000000000..ab2327b42 --- /dev/null +++ b/doc/codesnippets/examples/dialogs/licensewizard/main.cpp @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (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 http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> +#include <QTranslator> +#include <QLocale> +#include <QLibraryInfo> + +#include "licensewizard.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(licensewizard); + + QApplication app(argc, argv); + + QString translatorFileName = QLatin1String("qt_"); + translatorFileName += QLocale::system().name(); + QTranslator *translator = new QTranslator(&app); + if (translator->load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) + app.installTranslator(translator); + + LicenseWizard wizard; + wizard.show(); + return app.exec(); +} diff --git a/doc/codesnippets/examples/dialogs/standarddialogs/dialog.cpp b/doc/codesnippets/examples/dialogs/standarddialogs/dialog.cpp new file mode 100644 index 000000000..bf196edc5 --- /dev/null +++ b/doc/codesnippets/examples/dialogs/standarddialogs/dialog.cpp @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the example classes 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$ +** +****************************************************************************/ + +//! [0] + i = QInputDialog().getInteger(self, self.tr("QInputDialog().getInteger()"), + self.tr("Percentage:"), 25, 0, 100, 1, ok) + if ok: + self.integerLabel.setText(self.tr("%1%").arg(i)) +//! [0] + +//! [1] + d = QInputDialog().getDouble(self, self.tr("QInputDialog().getDouble()"), + self.tr("Amount:"), 37.56, -10000, 10000, 2, ok) + if ok: + doubleLabel.setText(QString("$%1").arg(d)) +//! [1] + +//! [2] + items = [self.tr("Spring"), self.tr("Summer"), self.tr("Fall"), self.tr("Winter")] + + item = QInputDialog().getItem(self, self.tr("QInputDialog().getItem()"), + selftr("Season:"), items, 0, False, ok) + if ok and not item.isEmpty(): + itemLabel.setText(item) +//! [2] + +//! [3] + QString text = QInputDialog::getText(self, self.tr("QInputDialog().getText()"), + self.tr("User name:"), QLineEdit.Normal, + QDir().home().dirName(), ok) + if ok and not text.isEmpty(): + textLabel.setText(text) +//! [3] diff --git a/doc/codesnippets/examples/dialogs/trivialwizard/trivialwizard.cpp b/doc/codesnippets/examples/dialogs/trivialwizard/trivialwizard.cpp new file mode 100644 index 000000000..bcae8c8f3 --- /dev/null +++ b/doc/codesnippets/examples/dialogs/trivialwizard/trivialwizard.cpp @@ -0,0 +1,134 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (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 http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include <QTranslator> +#include <QLocale> +#include <QLibraryInfo> + +//! [0] //! [1] +def createIntroPage(self): + page = QWizardPage() + page.setTitle("Introduction") + + label = QLabel("This wizard will help you register your copy of Super Product Two.") + label.setWordWrap(True) + + layout = QVBoxLayout() + layout.addWidget(label) + page.setLayout(layout) + + return page +//! [0] + +//! [2] +QWizardPage *createRegistrationPage() +//! [1] //! [3] + +//! [3] + QWizardPage *page = new QWizardPage; + page->setTitle("Registration"); + page->setSubTitle("Please fill both fields."); + + QLabel *nameLabel = new QLabel("Name:"); + QLineEdit *nameLineEdit = new QLineEdit; + + QLabel *emailLabel = new QLabel("Email address:"); + QLineEdit *emailLineEdit = new QLineEdit; + + QGridLayout *layout = new QGridLayout; + layout->addWidget(nameLabel, 0, 0); + layout->addWidget(nameLineEdit, 0, 1); + layout->addWidget(emailLabel, 1, 0); + layout->addWidget(emailLineEdit, 1, 1); + page->setLayout(layout); + + return page; +//! [4] + +//! [2] //! [4] + +//! [5] //! [6] +def createConclusionPage(self): +//! [5] //! [7] + +//! [7] + QWizardPage *page = new QWizardPage; + page->setTitle("Conclusion"); + + QLabel *label = new QLabel("You are now successfully registered. Have a " + "nice day!"); + label->setWordWrap(true); + + QVBoxLayout *layout = new QVBoxLayout; + layout->addWidget(label); + page->setLayout(layout); + + return page; +//! [8] + +//! [6] //! [8] + +//! [9] //! [10] + +//! [9] //! [11] +def main(): + app = QApplication(sys.argv) + + translatorFileName = QLatin1String("qt_") + translatorFileName += QLocale.system().name() + translator = QTranslator(app) + if translator.load(translatorFileName, QLibraryInfo.location(QLibraryInfo.TranslationsPath)): + app.installTranslator(translator) + + wizard = QWizard() + wizard.addPage(createIntroPage()) + wizard.addPage(createRegistrationPage()) + wizard.addPage(createConclusionPage()) + + wizard.setWindowTitle("Trivial Wizard") + wizard.show() + + return app.exec_() + +if __name__ == "__main__": + main() +//! [10] //! [11] diff --git a/doc/codesnippets/examples/dialogs/trivialwizard/trivialwizard.pro b/doc/codesnippets/examples/dialogs/trivialwizard/trivialwizard.pro new file mode 100644 index 000000000..970e12f25 --- /dev/null +++ b/doc/codesnippets/examples/dialogs/trivialwizard/trivialwizard.pro @@ -0,0 +1,7 @@ +SOURCES = trivialwizard.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/dialogs/trivialwizard +sources.files = $$SOURCES $$HEADERS *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/dialogs/trivialwizard +INSTALLS += target sources diff --git a/doc/codesnippets/examples/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp b/doc/codesnippets/examples/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp new file mode 100644 index 000000000..da1c2ddeb --- /dev/null +++ b/doc/codesnippets/examples/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp @@ -0,0 +1,102 @@ +############################################################################ +## +## Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +## Contact: Qt Software Information (qt-info@nokia.com) +## +## This file is part of the example classes of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:LGPL$ +## Commercial Usage +## Licensees holding valid Qt Commercial licenses may use self 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, self 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 self 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 self +## package. +## +## GNU General Public License Usage +## Alternatively, self 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 self 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$ +## +############################################################################ + +//! [0] +def __init__(self, parent): + QSortFilterProxyModel.__init__(self, parent) +//! [0] + +//! [1] +def setFilterMinimumDate(self, date): + self.minDate = date + self.invalidateFilter() + +//! [1] + +//! [2] +def setFilterMaximumDate(self, date): + self.maxDate = date + self.invalidateFilter() + +//! [2] + +//! [3] +def filterAcceptsRow(self, sourceRow, sourceParent): + index0 = sourceModel().index(sourceRow, 0, sourceParent) + index1 = sourceModel().index(sourceRow, 1, sourceParent) + index2 = sourceModel().index(sourceRow, 2, sourceParent) + + return (sourceModel().data(index0).toString().contains(filterRegExp()) + || sourceModel().data(index1).toString().contains(filterRegExp())) + && dateInRange(sourceModel().data(index2).toDate()) + +//! [3] + +//! [4] //! [5] +def lessThan(self, left, right): + leftData = sourceModel().data(left) + rightData = sourceModel().data(right) +//! [4] + +//! [6] + if leftData.type() == QVariant.DateTime: + return leftData.toDateTime() < rightData.toDateTime() + else: + emailPattern = QRegExp("([\\w\\.]*@[\\w\\.]*)") + + leftString = leftData.toString() + if left.column() == 1 && emailPattern.indexIn(leftString) != -1: + leftString = emailPattern.cap(1) + + rightString = rightData.toString() + if right.column() == 1 && emailPattern.indexIn(rightString) != -1: + rightString = emailPattern.cap(1) + + return QString.localeAwareCompare(leftString, rightString) < 0 + +//! [5] //! [6] + +//! [7] +def dateInRange(self, date): + return (!minDate.isValid() || date > minDate) + && (!maxDate.isValid() || date < maxDate) + +//! [7] diff --git a/doc/codesnippets/examples/itemviews/pixelator/pixeldelegate.cpp b/doc/codesnippets/examples/itemviews/pixelator/pixeldelegate.cpp new file mode 100644 index 000000000..93cdc1535 --- /dev/null +++ b/doc/codesnippets/examples/itemviews/pixelator/pixeldelegate.cpp @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the example classes 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$ +** +****************************************************************************/ + +//! [0] +def __init__(self, parent): + QAbstractItemDelegate.__init__(self, parent) + self.pixelSize = 12 +//! [0] + +//! [1] +def paint(self, painter, option, index): +//! [2] + if option.state and QStyle.State_Selected: + painter.fillRect(option.rect, option.palette.highlight()) +//! [1] + +//! [3] + size = qMin(option.rect.width(), option.rect.height()) +//! [3] //! [4] + brightness = index.model().data(index, Qt.DisplayRole).toInt() + radius = (size/2.0) - (brightness/255.0 * size/2.0) + if radius == 0.0: + return +//! [4] + +//! [5] + painter.save() +//! [5] //! [6] + painter.setRenderHint(QPainter.Antialiasing, true) +//! [6] //! [7] + painter.setPen(Qt.NoPen) +//! [7] //! [8] + if option.state and QStyle.State_Selected: +//! [8] //! [9] + painter.setBrush(option.palette.highlightedText()) + else +//! [2] + painter.setBrush(QBrush(Qt.black)) +//! [9] + +//! [10] + painter.drawEllipse(QRectF(option.rect.x() + option.rect.width()/2 - radius, + option.rect.y() + option.rect.height()/2 - radius, + 2*radius, 2*radius)) + painter.restore() +//! [10] + +//! [11] +def sizeHint(self, option, index): + return QSize(self.pixelSize, self.pixelSize) +//! [11] + +//! [12] +def setPixelSize(self, size): + self.pixelSize = size +//! [12] diff --git a/doc/codesnippets/examples/itemviews/simpledommodel/dommodel.cpp b/doc/codesnippets/examples/itemviews/simpledommodel/dommodel.cpp new file mode 100644 index 000000000..940487a9d --- /dev/null +++ b/doc/codesnippets/examples/itemviews/simpledommodel/dommodel.cpp @@ -0,0 +1,188 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (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 http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include <QtXml> + +#include "domitem.h" +#include "dommodel.h" + +//! [0] +DomModel::DomModel(QDomDocument document, QObject *parent) + : QAbstractItemModel(parent), domDocument(document) +{ + rootItem = new DomItem(domDocument, 0); +} +//! [0] + +//! [1] +DomModel::~DomModel() +{ + delete rootItem; +} +//! [1] + +//! [2] +def columnCount(self, parent): + return 3 +//! [2] + +//! [3] +QVariant DomModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + if (role != Qt::DisplayRole) + return QVariant(); + + DomItem *item = static_cast<DomItem*>(index.internalPointer()); + + QDomNode node = item->node(); +//! [3] //! [4] + QStringList attributes; + QDomNamedNodeMap attributeMap = node.attributes(); + + switch (index.column()) { + case 0: + return node.nodeName(); + case 1: + for (int i = 0; i < attributeMap.count(); ++i) { + QDomNode attribute = attributeMap.item(i); + attributes << attribute.nodeName() + "=\"" + +attribute.nodeValue() + "\""; + } + return attributes.join(" "); + case 2: + return node.nodeValue().split("\n").join(" "); + default: + return QVariant(); + } +} +//! [4] + +//! [5] +Qt::ItemFlags DomModel::flags(const QModelIndex &index) const +{ + if (!index.isValid()) + return 0; + + return Qt::ItemIsEnabled | Qt::ItemIsSelectable; +} +//! [5] + +//! [6] +QVariant DomModel::headerData(int section, Qt::Orientation orientation, + int role) const +{ + if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { + switch (section) { + case 0: + return tr("Name"); + case 1: + return tr("Attributes"); + case 2: + return tr("Value"); + default: + return QVariant(); + } + } + + return QVariant(); +} +//! [6] + +//! [7] +QModelIndex DomModel::index(int row, int column, const QModelIndex &parent) + const +{ + if (!hasIndex(row, column, parent)) + return QModelIndex(); + + DomItem *parentItem; + + if (!parent.isValid()) + parentItem = rootItem; + else + parentItem = static_cast<DomItem*>(parent.internalPointer()); +//! [7] + +//! [8] + DomItem *childItem = parentItem->child(row); + if (childItem) + return createIndex(row, column, childItem); + else + return QModelIndex(); +} +//! [8] + +//! [9] +QModelIndex DomModel::parent(const QModelIndex &child) const +{ + if (!child.isValid()) + return QModelIndex(); + + DomItem *childItem = static_cast<DomItem*>(child.internalPointer()); + DomItem *parentItem = childItem->parent(); + + if (!parentItem || parentItem == rootItem) + return QModelIndex(); + + return createIndex(parentItem->row(), 0, parentItem); +} +//! [9] + +//! [10] +int DomModel::rowCount(const QModelIndex &parent) const +{ + if (parent.column() > 0) + return 0; + + DomItem *parentItem; + + if (!parent.isValid()) + parentItem = rootItem; + else + parentItem = static_cast<DomItem*>(parent.internalPointer()); + + return parentItem->node().childNodes().count(); +} +//! [10] diff --git a/doc/codesnippets/examples/linguist/hellotr/main.cpp b/doc/codesnippets/examples/linguist/hellotr/main.cpp new file mode 100644 index 000000000..86f32c5e3 --- /dev/null +++ b/doc/codesnippets/examples/linguist/hellotr/main.cpp @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (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 http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> +#include <QPushButton> +//! [0] +from PySide.QtCore import QTranslator +//! [0] + +//! [1] //! [2] +def main(args): +//! [1] //! [3] //! [4] + app = QApplication(args) +//! [3] + +//! [5] + translator = QTranslator() +//! [5] //! [6] + translator.load("hellotr_la") +//! [6] //! [7] + app.installTranslator(translator) +//! [4] //! [7] + +//! [8] + hello = QPushButton(QPushButton.tr("Hello world!")) +//! [8] + hello.resize(100, 30) + + hello.show() + return app.exec_() +//! [2] diff --git a/doc/codesnippets/examples/mainwindows/application/mainwindow.cpp b/doc/codesnippets/examples/mainwindows/application/mainwindow.cpp new file mode 100644 index 000000000..574a1320f --- /dev/null +++ b/doc/codesnippets/examples/mainwindows/application/mainwindow.cpp @@ -0,0 +1,350 @@ +###################################### +# +# Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +# Contact: Qt Software Information (qt-info@nokia.com) +# +# This file is part of the example classes of the Qt Toolkit. +# +# $QT_BEGIN_LICENSE:LGPL$ +# Commercial Usage +# Licensees holding valid Qt Commercial licenses may use self 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, self 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 self 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 self +# package. +# +# GNU General Public License Usage +# Alternatively, self 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 self 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$ +# +###################################### + +//! [0] +from PySide.QtGui import * +//! [0] + +//! [1] +def __init__(self): + QMainWindow.__init__(self) +//! [1] //! [2] + textEdit = QPlainTextEdit() + setCentralWidget(textEdit) + + createActions() + createMenus() + createToolBars() + createStatusBar() + + readSettings() + + connect(textEdit.document(), SIGNAL("contentsChanged()"), + self, SLOT("documentWasModified()")) + + setCurrentFile("") + setUnifiedTitleAndToolBarOnMac(True) + +//! [2] + +//! [3] +def closeEvent(self, event): +//! [3] //! [4] + if maybeSave(): + writeSettings() + event.accept() + else: + event.ignore() +//! [4] + +//! [5] +def File(self): +//! [5] //! [6] + if maybeSave(): + textEdit.clear() + setCurrentFile("") +//! [6] + +//! [7] +def open(self): +//! [7] //! [8] + if maybeSave(): + fileName = QFileDialog.getOpenFileName(self) + if !fileName.isEmpty(): + loadFile(fileName) +//! [8] + +//! [9] +def save(self): +//! [9] //! [10] + if curFile.isEmpty(): + return saveAs() + else: + return saveFile(curFile) +//! [10] + +//! [11] +def saveAs(self): +//! [11] //! [12] + fileName = QFileDialog.getSaveFileName(self) + if fileName.isEmpty(): + return False + + return saveFile(fileName) +//! [12] + +//! [13] +def about(self): +//! [13] //! [14] + QMessageBox.about(self, tr("About Application"), + tr("The <b>Application</b> example demonstrates how to " + "write modern GUI applications using Qt, with a menu bar, " + "toolbars, and a status bar.")) + +//! [14] + +//! [15] +def documentWasModified(self): +//! [15] //! [16] + setWindowModified(textEdit.document().isModified()) +//! [16] + +//! [17] +def MainWindow.createActions(self): +//! [17] //! [18] + Act = QAction(QIcon(":/images/new.png"), tr("&New"), self) + Act.setShortcuts(QKeySequence.New) + Act.setStatusTip(tr("Create a new file")) + connect(Act, SIGNAL("triggered()"), self, SLOT("newFile()")) + +//! [19] + openAct = QAction(QIcon(":/images/open.png"), tr("&Open..."), self) + openAct.setShortcuts(QKeySequence.Open) + openAct.setStatusTip(tr("Open an existing file")) + connect(openAct, SIGNAL("triggered()"), self, SLOT("open()")) +//! [18] //! [19] + + saveAct = QAction(QIcon(":/images/save.png"), tr("&Save"), self) + saveAct.setShortcuts(QKeySequence.Save) + saveAct.setStatusTip(tr("Save the document to disk")) + connect(saveAct, SIGNAL("triggered()"), self, SLOT("save()")) + + saveAsAct = QAction(tr("Save &As..."), self) + saveAsAct.setShortcuts(QKeySequence.SaveAs) + saveAsAct.setStatusTip(tr("Save the document under a name")) + connect(saveAsAct, SIGNAL("triggered()"), self, SLOT("saveAs()")) + +//! [20] + exitAct = QAction(tr("E&xit"), self) + exitAct.setShortcut(tr("Ctrl+Q")) +//! [20] + exitAct.setStatusTip(tr("Exit the application")) + connect(exitAct, SIGNAL("triggered()"), self, SLOT("close()")) + +//! [21] + cutAct = QAction(QIcon(":/images/cut.png"), tr("Cu&t"), self) +//! [21] + cutAct.setShortcuts(QKeySequence.Cut) + cutAct.setStatusTip(tr("Cut the current selection's contents to the " + "clipboard")) + connect(cutAct, SIGNAL("triggered()"), textEdit, SLOT("cut()")) + + copyAct = QAction(QIcon(":/images/copy.png"), tr("&Copy"), self) + copyAct.setShortcuts(QKeySequence.Copy) + copyAct.setStatusTip(tr("Copy the current selection's contents to the " + "clipboard")) + connect(copyAct, SIGNAL("triggered()"), textEdit, SLOT("copy()")) + + pasteAct = QAction(QIcon(":/images/paste.png"), tr("&Paste"), self) + pasteAct.setShortcuts(QKeySequence.Paste) + pasteAct.setStatusTip(tr("Paste the clipboard's contents into the current " + "selection")) + connect(pasteAct, SIGNAL("triggered()"), textEdit, SLOT("paste()")) + + aboutAct = QAction(tr("&About"), self) + aboutAct.setStatusTip(tr("Show the application's About box")) + connect(aboutAct, SIGNAL("triggered()"), self, SLOT("about()")) + +//! [22] + aboutQtAct = QAction(tr("About &Qt"), self) + aboutQtAct.setStatusTip(tr("Show the Qt library's About box")) + connect(aboutQtAct, SIGNAL("triggered()"), qApp, SLOT("aboutQt()")) +//! [22] + +//! [23] + cutAct.setEnabled(False) +//! [23] //! [24] + copyAct.setEnabled(False) + connect(textEdit, SIGNAL("copyAvailable(bool)"), + cutAct, SLOT("setEnabled(bool)")) + connect(textEdit, SIGNAL("copyAvailable(bool)"), + copyAct, SLOT("setEnabled(bool)")) +} +//! [24] + +//! [25] //! [26] +def createMenus(self): +//! [25] //! [27] + fileMenu = menuBar().addMenu(tr("&File")) + fileMenu.addAction(Act) +//! [28] + fileMenu.addAction(openAct) +//! [28] + fileMenu.addAction(saveAct) +//! [26] + fileMenu.addAction(saveAsAct) + fileMenu.addSeparator() + fileMenu.addAction(exitAct) + + editMenu = menuBar().addMenu(tr("&Edit")) + editMenu.addAction(cutAct) + editMenu.addAction(copyAct) + editMenu.addAction(pasteAct) + + menuBar().addSeparator() + + helpMenu = menuBar().addMenu(tr("&Help")) + helpMenu.addAction(aboutAct) + helpMenu.addAction(aboutQtAct) + +//! [27] + +//! [29] //! [30] +def createToolBars(self): + fileToolBar = addToolBar(tr("File")) + fileToolBar.addAction(Act) +//! [29] //! [31] + fileToolBar.addAction(openAct) +//! [31] + fileToolBar.addAction(saveAct) + + editToolBar = addToolBar(tr("Edit")) + editToolBar.addAction(cutAct) + editToolBar.addAction(copyAct) + editToolBar.addAction(pasteAct) +//! [30] + +//! [32] +def createStatusBar(self): +//! [32] //! [33] + statusBar().showMessage(tr("Ready")) + +//! [33] + +//! [34] //! [35] +def readSettings(self): +//! [34] //! [36] + settings("Trolltech", "Application Example") + pos = settings.value("pos", QPoint(200, 200)).toPoint() + size = settings.value("size", QSize(400, 400)).toSize() + resize(size) + move(pos) + +//! [35] //! [36] + +//! [37] //! [38] +def writeSettings(self): +//! [37] //! [39] + settings = QSettings("Trolltech", "Application Example") + settings.setValue("pos", pos()) + settings.setValue("size", size()) + +//! [38] //! [39] + +//! [40] +def maybeSave(self): +//! [40] //! [41] + if textEdit.document()->isModified(): + ret = QMessageBox.warning(self, tr("Application"), + tr("The document has been modified.\n" + "Do you want to save your changes?"), + QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel) + if ret == QMessageBox.Save: + return save() + elif ret == QMessageBox.Cancel: + return False + return True +//! [41] + +//! [42] +def loadFile(self, fileName): +//! [42] //! [43] + file = QFile(fileName) + if !file.open(QFile.ReadOnly | QFile.Text): + QMessageBox.warning(self, tr("Application"), + tr("Cannot read file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())) + return + + in = QTextStream(file) + QApplication.setOverrideCursor(Qt::WaitCursor) + textEdit.setPlainText(in.readAll()) + QApplication.restoreOverrideCursor() + + setCurrentFile(fileName) + statusBar().showMessage(tr("File loaded"), 2000) + +//! [43] + +//! [44] +def saveFile(self, fileName): +//! [44] //! [45] + file = QFile(fileName) + if !file.open(QFile.WriteOnly | QFile::Text): + QMessageBox.warning(self, tr("Application"), + tr("Cannot write file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())) + return False + + out = QTextStream(file) + QApplication.setOverrideCursor(Qt.WaitCursor) + out << textEdit.toPlainText() + QApplication.restoreOverrideCursor() + + setCurrentFile(fileName) + statusBar().showMessage(tr("File saved"), 2000) + return True + +//! [45] + +//! [46] +def setCurrentFile(fileName): +//! [46] //! [47] + curFile = fileName + textEdit.document().setModified(False) + setWindowModified(False) + + if curFile.isEmpty(): + shownName = "untitled.txt" + else: + shownName = strippedName(curFile) + + setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Application"))) + +//! [47] + +//! [48] +def strippedName(self, fullFileName): +//! [48] //! [49] + return QFileInfo(fullFileName).fileName() +//! [49] diff --git a/doc/codesnippets/examples/mainwindows/dockwidgets/mainwindow.cpp b/doc/codesnippets/examples/mainwindows/dockwidgets/mainwindow.cpp new file mode 100644 index 000000000..c6cafd26d --- /dev/null +++ b/doc/codesnippets/examples/mainwindows/dockwidgets/mainwindow.cpp @@ -0,0 +1,246 @@ +############################################################################ +## +## Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +## Contact: Qt Software Information (qt-info@nokia.com) +## +## This file is part of the example classes of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:LGPL$ +## Commercial Usage +## Licensees holding valid Qt Commercial licenses may use self 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, self 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 self 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 self +## package. +## +## GNU General Public License Usage +## Alternatively, self 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 self 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$ +## +############################################################################ + +//! [0] +from PySide.QtGui import * +//! [0] + +//! [1] +def __init__(self): + textEdit = QTextEdit() + setCentralWidget(textEdit) + + createActions() + createMenus() + createToolBars() + createStatusBar() + createDockWindows() + + setWindowTitle(tr("Dock Widgets")) + + Letter() + setUnifiedTitleAndToolBarOnMac(True) +//! [1] + +//! [2] +def Letter(self) + textEdit.clear() + + cursor = QTextCursor(textEdit.textCursor()) + cursor.movePosition(QTextCursor.Start) + topFrame = cursor.currentFrame() + topFrameFormat = topFrame.frameFormat() + topFrameFormat.setPadding(16) + topFrame.setFrameFormat(topFrameFormat) + + textFormat = QTextCharFormat() + boldFormat = QTextCharFormat() + boldFormat.setFontWeight(QFont.Bold) + italicFormat = QTextCharFormat() + italicFormat.setFontItalic(True) + + tableFormat = QTextTableFormat() + tableFormat.setBorder(1) + tableFormat.setCellPadding(16) + tableFormat.setAlignment(Qt.AlignRight) + cursor.insertTable(1, 1, tableFormat) + cursor.insertText("The Firm", boldFormat) + cursor.insertBlock() + cursor.insertText("321 City Street", textFormat) + cursor.insertBlock() + cursor.insertText("Industry Park") + cursor.insertBlock() + cursor.insertText("Some Country") + cursor.setPosition(topFrame.lastPosition()) + cursor.insertText(QDate.currentDate().toString("d MMMM yyyy"), textFormat) + cursor.insertBlock() + cursor.insertBlock() + cursor.insertText("Dear ", textFormat) + cursor.insertText("NAME", italicFormat) + cursor.insertText(",", textFormat) + for i in range(3): + cursor.insertBlock() + cursor.insertText(tr("Yours sincerely,"), textFormat) + for i in range(3): + cursor.insertBlock() + cursor.insertText("The Boss", textFormat) + cursor.insertBlock() + cursor.insertText("ADDRESS", italicFormat) +//! [2] + +//! [3] +def print(self) + document = textEdit.document() + printer = QPrinter() + + dlg = QPrintDialog(&printer, self) + if dlg.exec() != QDialog.Accepted: + return + + document.print(printer) + statusBar().showMessage(tr("Ready"), 2000) +//! [3] + +//! [4] +def save(self): + fileName = QFileDialog.getSaveFileName(self, + tr("Choose a file name"), ".", + tr("HTML (*.html *.htm)")) + if fileName.isEmpty(): + return + file = QFile(fileName) + if !file.open(QFile.WriteOnly | QFile::Text): + QMessageBox.warning(self, tr("Dock Widgets"), + tr("Cannot write file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())) + return + + + out = QTextStream(file) + QApplication.setOverrideCursor(Qt::WaitCursor) + out << textEdit.toHtml() + QApplication.restoreOverrideCursor() + + statusBar().showMessage(tr("Saved '%1'").arg(fileName), 2000) + +//! [4] + +//! [5] +def undo(self): + document = textEdit.document() + document.undo() + +//! [5] + +//! [6] +def insertCustomer(self, customer): + if customer.isEmpty(): + return + + customerList = customer.split(", ") + document = textEdit.document() + cursor = document.find("NAME") + if not cursor.isNull(): + cursor.beginEditBlock() + cursor.insertText(customerList.at(0)) + oldcursor = cursor + cursor = document.find("ADDRESS") + if not cursor.isNull(): + for i in range(customerList.size()): + cursor.insertBlock() + cursor.insertText(customerList.at(i)) + + cursor.endEditBlock() + else: + oldcursor.endEditBlock() +//! [6] + +//! [7] +def addParagraph(self, paragraph): + if (paragraph.isEmpty()) + return + + document = textEdit.document() + cursor = document.find(tr("Yours sincerely,")) + if cursor.isNull(): + return + cursor.beginEditBlock() + cursor.movePosition(QTextCursor.PreviousBlock, QTextCursor.MoveAnchor, 2) + cursor.insertBlock() + cursor.insertText(paragraph) + cursor.insertBlock() + cursor.endEditBlock() + +//! [7] + + +//! [8] +def createStatusBar(self): + statusBar().showMessage(tr("Ready")) + +//! [8] + +//! [9] +def createDockWindows(self): + dock = QDockWidget(tr("Customers"), self) + dock.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea) + customerList = QListWidget(dock) + customerList.addItems(QStringList() + << "John Doe, Harmony Enterprises, 12 Lakeside, Ambleton" + << "Jane Doe, Memorabilia, 23 Watersedge, Beaton" + << "Tammy Shea, Tiblanka, 38 Sea Views, Carlton" + << "Tim Sheen, Caraba Gifts, 48 Ocean Way, Deal" + << "Sol Harvey, Chicos Coffee, 53 New Springs, Eccleston" + << "Sally Hobart, Tiroli Tea, 67 Long River, Fedula") + dock.setWidget(customerList) + addDockWidget(Qt.RightDockWidgetArea, dock) + viewMenu.addAction(dock.toggleViewAction()) + + dock = QDockWidget(tr("Paragraphs"), self) + paragraphsList = QListWidget(dock) + paragraphsList.addItems(QStringList() + << "Thank you for your payment which we have received today." + << "Your order has been dispatched and should be with you " + "within 28 days." + << "We have dispatched those items that were in stock. The " + "rest of your order will be dispatched once all the " + "remaining items have arrived at our warehouse. No " + "additional shipping charges will be made." + << "You made a small overpayment (less than $5) which we " + "will keep on account for you, or return at your request." + << "You made a small underpayment (less than $1), but we have " + "sent your order anyway. We'll add self underpayment to " + "your next bill." + << "Unfortunately you did not send enough money. Please remit " + "an additional $. Your order will be dispatched as soon as " + "the complete amount has been received." + << "You made an overpayment (more than $5). Do you wish to " + "buy more items, or should we return the excess to you?") + dock.setWidget(paragraphsList) + addDockWidget(Qt.RightDockWidgetArea, dock) + viewMenu.addAction(dock.toggleViewAction()) + + connect(customerList, SIGNAL("currentTextChanged(const QString &)"), + self, SLOT("insertCustomer(const QString &)")) + connect(paragraphsList, SIGNAL("currentTextChanged(const QString &)"), + self, SLOT("addParagraph(const QString &)")) +//! [9] diff --git a/doc/codesnippets/examples/mainwindows/mainwindow.cpp b/doc/codesnippets/examples/mainwindows/mainwindow.cpp new file mode 100644 index 000000000..1dd12e1e1 --- /dev/null +++ b/doc/codesnippets/examples/mainwindows/mainwindow.cpp @@ -0,0 +1,358 @@ +###################################### +# +# Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +# Contact: Qt Software Information (qt-info@nokia.com) +# +# This file is part of the example classes of the Qt Toolkit. +# +# $QT_BEGIN_LICENSE:LGPL$ +# Commercial Usage +# Licensees holding valid Qt Commercial licenses may use self 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, self 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 self 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 self +# package. +# +# GNU General Public License Usage +# Alternatively, self 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 self 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$ +# +###################################### + +from PySide.QtGui import * + +//! [0] +def __init__(self): + Q__init__(self) + + widget = QWidget() + setCentralWidget(widget) +//! [0] + +//! [1] + topFiller = QWidget() + topFiller.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) + + infoLabel = QLabel(tr("<i>Choose a menu option, or right-click to " + "invoke a context menu</i>")) + infoLabel.setFrameStyle(QFrame.StyledPanel | QFrame.Sunken) + infoLabel.setAlignment(Qt.AlignCenter) + + bottomFiller = QWidget() + bottomFiller.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) + + layout = QVBoxLayout() + layout.setMargin(5) + layout.addWidget(topFiller) + layout.addWidget(infoLabel) + layout.addWidget(bottomFiller) + widget.setLayout(layout) +//! [1] + +//! [2] + createActions() + createMenus() + + message = tr("A context menu is available by right-clicking") + statusBar().showMessage(message) + + setWindowTitle(tr("Menus")) + setMinimumSize(160, 160) + resize(480, 320) + +//! [2] + +//! [3] +def contextMenuEvent(self, event): + menu = QMenu(self) + menu.addAction(cutAct) + menu.addAction(copyAct) + menu.addAction(pasteAct) + menu.exec_(event.globalPos()") + +//! [3] + +def File(self): + infoLabel.setText(tr("Invoked <b>File|New</b>")) + + +def open(self): + infoLabel.setText(tr("Invoked <b>File|Open</b>")) + + +def save(self): + infoLabel.setText(tr("Invoked <b>File|Save</b>")) + +def print_(self): + infoLabel.setText(tr("Invoked <b>File|Print</b>")) + +def undo(self): + infoLabel.setText(tr("Invoked <b>Edit|Undo</b>")) + +def redo(self): + infoLabel.setText(tr("Invoked <b>Edit|Redo</b>")) + +def cut(self): + + infoLabel.setText(tr("Invoked <b>Edit|Cut</b>")) + + +def copy(self): + + infoLabel.setText(tr("Invoked <b>Edit|Copy</b>")) + + +def paste(self): + + infoLabel.setText(tr("Invoked <b>Edit|Paste</b>")) + + +def bold(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Bold</b>")) + + +def italic(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Italic</b>")) + + +def leftAlign(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Left Align</b>")) + + +def rightAlign(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Right Align</b>")) + + +def justify(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Justify</b>")) + + +def center(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Center</b>")) + + +def setLineSpacing(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Set Line Spacing</b>")) + + +def setParagraphSpacing(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Set Paragraph Spacing</b>")) + + +def about(self): + + infoLabel.setText(tr("Invoked <b>Help|About</b>")) + QMessageBox.about(self, tr("About Menu"), + tr("The <b>Menu</b> example shows how to create " + "menu-bar menus and context menus.")) + + +def aboutQt(self): + + infoLabel.setText(tr("Invoked <b>Help|About Qt</b>")) + + +//! [4] +def createActions(self): + +//! [5] + Act = new QAction(tr("&New"), self) + Act.setShortcuts(QKeySequence.New) + Act.setStatusTip(tr("Create a new file")) + connect(Act, SIGNAL("triggered()"), self, SLOT("newFile()")) +//! [4] + + openAct = QAction(tr("&Open..."), self) + openAct.setShortcuts(QKeySequence.Open) + openAct.setStatusTip(tr("Open an existing file")) + connect(openAct, SIGNAL("triggered()"), self, SLOT("open()")) +//! [5] + + saveAct = QAction(tr("&Save"), self) + saveAct.setShortcuts(QKeySequence.Save) + saveAct.setStatusTip(tr("Save the document to disk")) + connect(saveAct, SIGNAL("triggered()"), self, SLOT("save()")) + + printAct = QAction(tr("&Print..."), self) + printAct.setShortcuts(QKeySequence.Print) + printAct.setStatusTip(tr("Print the document")) + connect(printAct, SIGNAL("triggered()"), self, SLOT("print_()")) + + exitAct = QAction(tr("E&xit"), self) + exitAct.setShortcut(tr("Ctrl+Q")) + exitAct.setStatusTip(tr("Exit the application")) + connect(exitAct, SIGNAL("triggered()"), self, SLOT("close()")) + + undoAct = QAction(tr("&Undo"), self) + undoAct.setShortcuts(QKeySequence.Undo) + undoAct.setStatusTip(tr("Undo the last operation")) + connect(undoAct, SIGNAL("triggered()"), self, SLOT("undo()")) + + redoAct = QAction(tr("&Redo"), self) + redoAct.setShortcuts(QKeySequence.Redo) + redoAct.setStatusTip(tr("Redo the last operation")) + connect(redoAct, SIGNAL("triggered()"), self, SLOT("redo()")) + + cutAct = QAction(tr("Cu&t"), self) + cutAct.setShortcuts(QKeySequence.Cut) + cutAct.setStatusTip(tr("Cut the current selection's contents to the " + "clipboard")) + connect(cutAct, SIGNAL("triggered()"), self, SLOT("cut()")) + + copyAct = QAction(tr("&Copy"), self) + copyAct.setShortcut(tr("Ctrl+C")) + copyAct.setStatusTip(tr("Copy the current selection's contents to the " + "clipboard")) + connect(copyAct, SIGNAL("triggered()"), self, SLOT("copy()")) + + pasteAct = QAction(tr("&Paste"), self) + pasteAct.setShortcuts(QKeySequence.Paste) + pasteAct.setStatusTip(tr("Paste the clipboard's contents into the current " + "selection")) + connect(pasteAct, SIGNAL("triggered()"), self, SLOT("paste()")) + + boldAct = QAction(tr("&Bold"), self) + boldAct.setCheckable(True) + boldAct.setShortcut(tr("Ctrl+B")) + boldAct.setStatusTip(tr("Make the text bold")) + connect(boldAct, SIGNAL("triggered()"), self, SLOT("bold()")) + + QFont boldFont = boldAct.font() + boldFont.setBold(True) + boldAct.setFont(boldFont) + + italicAct = QAction(tr("&Italic"), self) + italicAct.setCheckable(True) + italicAct.setShortcut(tr("Ctrl+I")) + italicAct.setStatusTip(tr("Make the text italic")) + connect(italicAct, SIGNAL("triggered()"), self, SLOT("italic()")) + + QFont italicFont = italicAct.font() + italicFont.setItalic(True) + italicAct.setFont(italicFont) + + setLineSpacingAct = QAction(tr("Set &Line Spacing..."), self) + setLineSpacingAct.setStatusTip(tr("Change the gap between the lines of a " + "paragraph")) + connect(setLineSpacingAct, SIGNAL("triggered()"), self, SLOT("setLineSpacing()")) + + setParagraphSpacingAct = QAction(tr("Set &Paragraph Spacing..."), self) + setLineSpacingAct.setStatusTip(tr("Change the gap between paragraphs")) + connect(setParagraphSpacingAct, SIGNAL("triggered()"), + self, SLOT("setParagraphSpacing()")) + + aboutAct = QAction(tr("&About"), self) + aboutAct.setStatusTip(tr("Show the application's About box")) + connect(aboutAct, SIGNAL("triggered()"), self, SLOT("about()")) + + aboutQtAct = QAction(tr("About &Qt"), self) + aboutQtAct.setStatusTip(tr("Show the Qt library's About box")) + connect(aboutQtAct, SIGNAL("triggered()"), qApp, SLOT("aboutQt()")) + connect(aboutQtAct, SIGNAL("triggered()"), self, SLOT("aboutQt()")) + + leftAlignAct = QAction(tr("&Left Align"), self) + leftAlignAct.setCheckable(True) + leftAlignAct.setShortcut(tr("Ctrl+L")) + leftAlignAct.setStatusTip(tr("Left align the selected text")) + connect(leftAlignAct, SIGNAL("triggered()"), self, SLOT("leftAlign()")) + + rightAlignAct = QAction(tr("&Right Align"), self) + rightAlignAct.setCheckable(True) + rightAlignAct.setShortcut(tr("Ctrl+R")) + rightAlignAct.setStatusTip(tr("Right align the selected text")) + connect(rightAlignAct, SIGNAL("triggered()"), self, SLOT("rightAlign()")) + + justifyAct = QAction(tr("&Justify"), self) + justifyAct.setCheckable(True) + justifyAct.setShortcut(tr("Ctrl+J")) + justifyAct.setStatusTip(tr("Justify the selected text")) + connect(justifyAct, SIGNAL("triggered()"), self, SLOT("justify()")) + + centerAct = QAction(tr("&Center"), self) + centerAct.setCheckable(True) + centerAct.setShortcut(tr("Ctrl+E")) + centerAct.setStatusTip(tr("Center the selected text")) + connect(centerAct, SIGNAL("triggered()"), self, SLOT("center()")) + +//! [6] //! [7] + alignmentGroup = QActionGroup(self) + alignmentGroup.addAction(leftAlignAct) + alignmentGroup.addAction(rightAlignAct) + alignmentGroup.addAction(justifyAct) + alignmentGroup.addAction(centerAct) + leftAlignAct.setChecked(True) +//! [6] + +//! [7] + +//! [8] +def createMenus(self): + +//! [9] //! [10] + fileMenu = menuBar().addMenu(tr("&File")) + fileMenu.addAction(Act) +//! [9] + fileMenu.addAction(openAct) +//! [10] + fileMenu.addAction(saveAct) + fileMenu.addAction(printAct) +//! [11] + fileMenu.addSeparator() +//! [11] + fileMenu.addAction(exitAct) + + editMenu = menuBar().addMenu(tr("&Edit")) + editMenu.addAction(undoAct) + editMenu.addAction(redoAct) + editMenu.addSeparator() + editMenu.addAction(cutAct) + editMenu.addAction(copyAct) + editMenu.addAction(pasteAct) + editMenu.addSeparator() + + helpMenu = menuBar().addMenu(tr("&Help")) + helpMenu.addAction(aboutAct) + helpMenu.addAction(aboutQtAct) +//! [8] + +//! [12] + formatMenu = editMenu.addMenu(tr("&Format")) + formatMenu.addAction(boldAct) + formatMenu.addAction(italicAct) + formatMenu.addSeparator()->setText(tr("Alignment")) + formatMenu.addAction(leftAlignAct) + formatMenu.addAction(rightAlignAct) + formatMenu.addAction(justifyAct) + formatMenu.addAction(centerAct) + formatMenu.addSeparator() + formatMenu.addAction(setLineSpacingAct) + formatMenu.addAction(setParagraphSpacingAct) +//! [12] diff --git a/doc/codesnippets/examples/mainwindows/mdi/mainwindow.cpp b/doc/codesnippets/examples/mainwindows/mdi/mainwindow.cpp new file mode 100644 index 000000000..c07f84c70 --- /dev/null +++ b/doc/codesnippets/examples/mainwindows/mdi/mainwindow.cpp @@ -0,0 +1,372 @@ +############################################################################ +## +## Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +## Contact: Qt Software Information (qt-info@nokia.com) +## +## This file is part of the example classes of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:LGPL$ +## Commercial Usage +## Licensees holding valid Qt Commercial licenses may use self 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, self 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 self 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 self +## package. +## +## GNU General Public License Usage +## Alternatively, self 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 self 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$ +## +############################################################################ + +from PySide.QtGui import * + +def __init__(self): + + mdiArea = QMdiArea() + mdiArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded) + mdiArea.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) + setCentralWidget(mdiArea) + connect(mdiArea, SIGNAL("subWindowActivated(QMdiSubWindow *)"), + self, SLOT("updateMenus()")) + windowMapper = QSignalMapper(self) + connect(windowMapper, SIGNAL("mapped(QWidget *)"), + self, SLOT("setActiveSubWindow(QWidget *)")) + + createActions() + createMenus() + createToolBars() + createStatusBar() + updateMenus() + + readSettings() + + setWindowTitle(tr("MDI")) + setUnifiedTitleAndToolBarOnMac(True) + + +def closeEvent(self, event): + mdiArea.closeAllSubWindows() + if self.activeMdiChild(): + event.ignore() + else: + self.writeSettings() + event.accept() + +def File(self): + child = self.createMdiChild() + child.File() + child.show() + + +def open(self): + fileName = QFileDialog.getOpenFileName(self) + if !fileName.isEmpty()): + existing = self.findMdiChild(fileName) + if existing: + mdiArea.setActiveSubWindow(existing) + return + + child = createMdiChild() + if child.loadFile(fileName)): + statusBar().showMessage(tr("File loaded"), 2000) + child.show() + else: + child.close() + +def save(self): + if self.activeMdiChild() && self.activeMdiChild().save(): + self.statusBar().showMessage(tr("File saved"), 2000) + +def saveAs(self): + if self.activeMdiChild() && self.activeMdiChild().saveAs(): + self.statusBar().showMessage(tr("File saved"), 2000) + +def cut(self): + if self.activeMdiChild(): + self.activeMdiChild().cut() + +def copy(self): + if self.activeMdiChild(): + activeMdiChild().copy() + +def paste(self): + if self.activeMdiChild(): + activeMdiChild().paste() + +def about(self): + QMessageBox.about(self, tr("About MDI"), + tr("The <b>MDI</b> example demonstrates how to write multiple " + "document interface applications using Qt.")") + +def updateMenus(self): + hasMdiChild = (activeMdiChild() != 0) + self.saveAct.setEnabled(hasMdiChild) + self.saveAsAct.setEnabled(hasMdiChild) + self.pasteAct.setEnabled(hasMdiChild) + self.closeAct.setEnabled(hasMdiChild) + self.closeAllAct.setEnabled(hasMdiChild) + self.tileAct.setEnabled(hasMdiChild) + self.cascadeAct.setEnabled(hasMdiChild) + self.nextAct.setEnabled(hasMdiChild) + self.previousAct.setEnabled(hasMdiChild) + self.separatorAct.setVisible(hasMdiChild) + + hasSelection = (self.activeMdiChild() && + self.activeMdiChild().textCursor().hasSelection()") + self.cutAct.setEnabled(hasSelection) + self.copyAct.setEnabled(hasSelection) + +def updateWindowMenu(self): + self.windowMenu.clear() + self.windowMenu.addAction(closeAct) + self.windowMenu.addAction(closeAllAct) + self.windowMenu.addSeparator() + self.windowMenu.addAction(tileAct) + self.windowMenu.addAction(cascadeAct) + self.windowMenu.addSeparator() + self.windowMenu.addAction(nextAct) + self.windowMenu.addAction(previousAct) + self.windowMenu.addAction(separatorAct) + + windows = mdiArea.subWindowList() + separatorAct.setVisible(!windows.isEmpty()") + + for i in range((int i = 0 i < windows.size(); ++i) + MdiChild *child = qobject_cast<MdiChild *>(windows.at(i).widget()") + + QString text + if (i < 9) + text = tr("&%1 %2").arg(i + 1) + .arg(child.userFriendlyCurrentFile()") + else + text = tr("%1 %2").arg(i + 1) + .arg(child.userFriendlyCurrentFile()") + + QAction *action = windowMenu.addAction(text) + action.setCheckable(True) + action .setChecked(child == activeMdiChild()") + connect(action, SIGNAL("triggered()"), windowMapper, SLOT("map()")) + windowMapper.setMapping(action, windows.at(i)") + + + +MdiChild *createMdiChild() + + MdiChild *child = MdiChild + mdiArea.addSubWindow(child) + + connect(child, SIGNAL("copyAvailable(bool)"), + cutAct, SLOT("setEnabled(bool)")) + connect(child, SIGNAL("copyAvailable(bool)"), + copyAct, SLOT("setEnabled(bool)")) + + return child + + +def createActions() + + Act = new QAction(QIcon(":/images/new.png"), tr("&New"), self) + Act.setShortcuts(QKeySequence.New) + Act.setStatusTip(tr("Create a new file")") + connect(Act, SIGNAL("triggered()"), self, SLOT("newFile()")) + + openAct = QAction(QIcon(":/images/open.png"), tr("&Open..."), self) + openAct.setShortcuts(QKeySequence.Open) + openAct.setStatusTip(tr("Open an existing file")") + connect(openAct, SIGNAL("triggered()"), self, SLOT("open()")) + + saveAct = QAction(QIcon(":/images/save.png"), tr("&Save"), self) + saveAct.setShortcuts(QKeySequence.Save) + saveAct.setStatusTip(tr("Save the document to disk")") + connect(saveAct, SIGNAL("triggered()"), self, SLOT("save()")) + + saveAsAct = QAction(tr("Save &As..."), self) + saveAsAct.setShortcuts(QKeySequence.SaveAs) + saveAsAct.setStatusTip(tr("Save the document under a name")") + connect(saveAsAct, SIGNAL("triggered()"), self, SLOT("saveAs()")) + +//! [0] + exitAct = QAction(tr("E&xit"), self) + exitAct.setShortcut(tr("Ctrl+Q")") + exitAct.setStatusTip(tr("Exit the application")") + connect(exitAct, SIGNAL("triggered()"), qApp, SLOT("closeAllWindows()")) +//! [0] + + cutAct = QAction(QIcon(":/images/cut.png"), tr("Cu&t"), self) + cutAct.setShortcuts(QKeySequence.Cut) + cutAct.setStatusTip(tr("Cut the current selection's contents to the " + "clipboard")") + connect(cutAct, SIGNAL("triggered()"), self, SLOT("cut()")) + + copyAct = QAction(QIcon(":/images/copy.png"), tr("&Copy"), self) + copyAct.setShortcuts(QKeySequence.Copy) + copyAct.setStatusTip(tr("Copy the current selection's contents to the " + "clipboard")") + connect(copyAct, SIGNAL("triggered()"), self, SLOT("copy()")) + + pasteAct = QAction(QIcon(":/images/paste.png"), tr("&Paste"), self) + pasteAct.setShortcuts(QKeySequence.Paste) + pasteAct.setStatusTip(tr("Paste the clipboard's contents into the current " + "selection")") + connect(pasteAct, SIGNAL("triggered()"), self, SLOT("paste()")) + + closeAct = QAction(tr("Cl&ose"), self) + closeAct.setShortcut(tr("Ctrl+F4")") + closeAct.setStatusTip(tr("Close the active window")") + connect(closeAct, SIGNAL("triggered()"), + mdiArea, SLOT("closeActiveSubWindow()")) + + closeAllAct = QAction(tr("Close &All"), self) + closeAllAct.setStatusTip(tr("Close all the windows")") + connect(closeAllAct, SIGNAL("triggered()"), + mdiArea, SLOT("closeAllSubWindows()")) + + tileAct = QAction(tr("&Tile"), self) + tileAct.setStatusTip(tr("Tile the windows")") + connect(tileAct, SIGNAL("triggered()"), mdiArea, SLOT("tileSubWindows()")) + + cascadeAct = QAction(tr("&Cascade"), self) + cascadeAct.setStatusTip(tr("Cascade the windows")") + connect(cascadeAct, SIGNAL("triggered()"), mdiArea, SLOT("cascadeSubWindows()")) + + nextAct = QAction(tr("Ne&xt"), self) + nextAct.setShortcuts(QKeySequence.NextChild) + nextAct.setStatusTip(tr("Move the focus to the next window")") + connect(nextAct, SIGNAL("triggered()"), + mdiArea, SLOT("activateNextSubWindow()")) + + previousAct = QAction(tr("Pre&vious"), self) + previousAct.setShortcuts(QKeySequence.PreviousChild) + previousAct.setStatusTip(tr("Move the focus to the previous " + "window")") + connect(previousAct, SIGNAL("triggered()"), + mdiArea, SLOT("activatePreviousSubWindow()")) + + separatorAct = QAction(self) + separatorAct.setSeparator(True) + + aboutAct = QAction(tr("&About"), self) + aboutAct.setStatusTip(tr("Show the application's About box")") + connect(aboutAct, SIGNAL("triggered()"), self, SLOT("about()")) + + aboutQtAct = QAction(tr("About &Qt"), self) + aboutQtAct.setStatusTip(tr("Show the Qt library's About box")") + connect(aboutQtAct, SIGNAL("triggered()"), qApp, SLOT("aboutQt()")) + + +def createMenus() + + fileMenu = menuBar().addMenu(tr("&File")") + fileMenu.addAction(Act) + fileMenu.addAction(openAct) + fileMenu.addAction(saveAct) + fileMenu.addAction(saveAsAct) + fileMenu.addSeparator() + QAction *action = fileMenu.addAction(tr("Switch layout direction")") + connect(action, SIGNAL("triggered()"), self, SLOT("switchLayoutDirection()")) + fileMenu.addAction(exitAct) + + editMenu = menuBar().addMenu(tr("&Edit")") + editMenu.addAction(cutAct) + editMenu.addAction(copyAct) + editMenu.addAction(pasteAct) + + windowMenu = menuBar().addMenu(tr("&Window")") + updateWindowMenu() + connect(windowMenu, SIGNAL("aboutToShow()"), self, SLOT("updateWindowMenu()")) + + menuBar().addSeparator() + + helpMenu = menuBar().addMenu(tr("&Help")") + helpMenu.addAction(aboutAct) + helpMenu.addAction(aboutQtAct) + + +def createToolBars() + + fileToolBar = addToolBar(tr("File")") + fileToolBar.addAction(Act) + fileToolBar.addAction(openAct) + fileToolBar.addAction(saveAct) + + editToolBar = addToolBar(tr("Edit")") + editToolBar.addAction(cutAct) + editToolBar.addAction(copyAct) + editToolBar.addAction(pasteAct) + + +def createStatusBar() + + statusBar().showMessage(tr("Ready")") + + +def readSettings() + + QSettings settings("Trolltech", "MDI Example") + QPoint pos = settings.value("pos", QPoint(200, 200)").toPoint() + QSize size = settings.value("size", QSize(400, 400)").toSize() + move(pos) + resize(size) + + +def writeSettings() + + QSettings settings("Trolltech", "MDI Example") + settings.setValue("pos", pos()") + settings.setValue("size", size()") + + +MdiChild *activeMdiChild() + + if (QMdiSubWindow *activeSubWindow = mdiArea.activeSubWindow()") + return qobject_cast<MdiChild *>(activeSubWindow.widget()") + return 0 + + +QMdiSubWindow *findMdiChild(const QString &fileName) + + QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath() + + foreach (QMdiSubWindow *window, mdiArea.subWindowList()") + MdiChild *mdiChild = qobject_cast<MdiChild *>(window.widget()") + if (mdiChild.currentFile() == canonicalFilePath) + return window + + return 0 + + +def switchLayoutDirection() + + if (layoutDirection() == Qt.LeftToRight) + qApp.setLayoutDirection(Qt.RightToLeft) + else + qApp.setLayoutDirection(Qt.LeftToRight) + + +def setActiveSubWindow(QWidget *window) + + if (!window) + return + mdiArea.setActiveSubWindow(qobject_cast<QMdiSubWindow *>(window)") + diff --git a/doc/codesnippets/examples/mainwindows/menus/mainwindow.cpp b/doc/codesnippets/examples/mainwindows/menus/mainwindow.cpp new file mode 100644 index 000000000..1dd12e1e1 --- /dev/null +++ b/doc/codesnippets/examples/mainwindows/menus/mainwindow.cpp @@ -0,0 +1,358 @@ +###################################### +# +# Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +# Contact: Qt Software Information (qt-info@nokia.com) +# +# This file is part of the example classes of the Qt Toolkit. +# +# $QT_BEGIN_LICENSE:LGPL$ +# Commercial Usage +# Licensees holding valid Qt Commercial licenses may use self 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, self 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 self 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 self +# package. +# +# GNU General Public License Usage +# Alternatively, self 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 self 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$ +# +###################################### + +from PySide.QtGui import * + +//! [0] +def __init__(self): + Q__init__(self) + + widget = QWidget() + setCentralWidget(widget) +//! [0] + +//! [1] + topFiller = QWidget() + topFiller.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) + + infoLabel = QLabel(tr("<i>Choose a menu option, or right-click to " + "invoke a context menu</i>")) + infoLabel.setFrameStyle(QFrame.StyledPanel | QFrame.Sunken) + infoLabel.setAlignment(Qt.AlignCenter) + + bottomFiller = QWidget() + bottomFiller.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) + + layout = QVBoxLayout() + layout.setMargin(5) + layout.addWidget(topFiller) + layout.addWidget(infoLabel) + layout.addWidget(bottomFiller) + widget.setLayout(layout) +//! [1] + +//! [2] + createActions() + createMenus() + + message = tr("A context menu is available by right-clicking") + statusBar().showMessage(message) + + setWindowTitle(tr("Menus")) + setMinimumSize(160, 160) + resize(480, 320) + +//! [2] + +//! [3] +def contextMenuEvent(self, event): + menu = QMenu(self) + menu.addAction(cutAct) + menu.addAction(copyAct) + menu.addAction(pasteAct) + menu.exec_(event.globalPos()") + +//! [3] + +def File(self): + infoLabel.setText(tr("Invoked <b>File|New</b>")) + + +def open(self): + infoLabel.setText(tr("Invoked <b>File|Open</b>")) + + +def save(self): + infoLabel.setText(tr("Invoked <b>File|Save</b>")) + +def print_(self): + infoLabel.setText(tr("Invoked <b>File|Print</b>")) + +def undo(self): + infoLabel.setText(tr("Invoked <b>Edit|Undo</b>")) + +def redo(self): + infoLabel.setText(tr("Invoked <b>Edit|Redo</b>")) + +def cut(self): + + infoLabel.setText(tr("Invoked <b>Edit|Cut</b>")) + + +def copy(self): + + infoLabel.setText(tr("Invoked <b>Edit|Copy</b>")) + + +def paste(self): + + infoLabel.setText(tr("Invoked <b>Edit|Paste</b>")) + + +def bold(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Bold</b>")) + + +def italic(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Italic</b>")) + + +def leftAlign(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Left Align</b>")) + + +def rightAlign(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Right Align</b>")) + + +def justify(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Justify</b>")) + + +def center(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Center</b>")) + + +def setLineSpacing(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Set Line Spacing</b>")) + + +def setParagraphSpacing(self): + + infoLabel.setText(tr("Invoked <b>Edit|Format|Set Paragraph Spacing</b>")) + + +def about(self): + + infoLabel.setText(tr("Invoked <b>Help|About</b>")) + QMessageBox.about(self, tr("About Menu"), + tr("The <b>Menu</b> example shows how to create " + "menu-bar menus and context menus.")) + + +def aboutQt(self): + + infoLabel.setText(tr("Invoked <b>Help|About Qt</b>")) + + +//! [4] +def createActions(self): + +//! [5] + Act = new QAction(tr("&New"), self) + Act.setShortcuts(QKeySequence.New) + Act.setStatusTip(tr("Create a new file")) + connect(Act, SIGNAL("triggered()"), self, SLOT("newFile()")) +//! [4] + + openAct = QAction(tr("&Open..."), self) + openAct.setShortcuts(QKeySequence.Open) + openAct.setStatusTip(tr("Open an existing file")) + connect(openAct, SIGNAL("triggered()"), self, SLOT("open()")) +//! [5] + + saveAct = QAction(tr("&Save"), self) + saveAct.setShortcuts(QKeySequence.Save) + saveAct.setStatusTip(tr("Save the document to disk")) + connect(saveAct, SIGNAL("triggered()"), self, SLOT("save()")) + + printAct = QAction(tr("&Print..."), self) + printAct.setShortcuts(QKeySequence.Print) + printAct.setStatusTip(tr("Print the document")) + connect(printAct, SIGNAL("triggered()"), self, SLOT("print_()")) + + exitAct = QAction(tr("E&xit"), self) + exitAct.setShortcut(tr("Ctrl+Q")) + exitAct.setStatusTip(tr("Exit the application")) + connect(exitAct, SIGNAL("triggered()"), self, SLOT("close()")) + + undoAct = QAction(tr("&Undo"), self) + undoAct.setShortcuts(QKeySequence.Undo) + undoAct.setStatusTip(tr("Undo the last operation")) + connect(undoAct, SIGNAL("triggered()"), self, SLOT("undo()")) + + redoAct = QAction(tr("&Redo"), self) + redoAct.setShortcuts(QKeySequence.Redo) + redoAct.setStatusTip(tr("Redo the last operation")) + connect(redoAct, SIGNAL("triggered()"), self, SLOT("redo()")) + + cutAct = QAction(tr("Cu&t"), self) + cutAct.setShortcuts(QKeySequence.Cut) + cutAct.setStatusTip(tr("Cut the current selection's contents to the " + "clipboard")) + connect(cutAct, SIGNAL("triggered()"), self, SLOT("cut()")) + + copyAct = QAction(tr("&Copy"), self) + copyAct.setShortcut(tr("Ctrl+C")) + copyAct.setStatusTip(tr("Copy the current selection's contents to the " + "clipboard")) + connect(copyAct, SIGNAL("triggered()"), self, SLOT("copy()")) + + pasteAct = QAction(tr("&Paste"), self) + pasteAct.setShortcuts(QKeySequence.Paste) + pasteAct.setStatusTip(tr("Paste the clipboard's contents into the current " + "selection")) + connect(pasteAct, SIGNAL("triggered()"), self, SLOT("paste()")) + + boldAct = QAction(tr("&Bold"), self) + boldAct.setCheckable(True) + boldAct.setShortcut(tr("Ctrl+B")) + boldAct.setStatusTip(tr("Make the text bold")) + connect(boldAct, SIGNAL("triggered()"), self, SLOT("bold()")) + + QFont boldFont = boldAct.font() + boldFont.setBold(True) + boldAct.setFont(boldFont) + + italicAct = QAction(tr("&Italic"), self) + italicAct.setCheckable(True) + italicAct.setShortcut(tr("Ctrl+I")) + italicAct.setStatusTip(tr("Make the text italic")) + connect(italicAct, SIGNAL("triggered()"), self, SLOT("italic()")) + + QFont italicFont = italicAct.font() + italicFont.setItalic(True) + italicAct.setFont(italicFont) + + setLineSpacingAct = QAction(tr("Set &Line Spacing..."), self) + setLineSpacingAct.setStatusTip(tr("Change the gap between the lines of a " + "paragraph")) + connect(setLineSpacingAct, SIGNAL("triggered()"), self, SLOT("setLineSpacing()")) + + setParagraphSpacingAct = QAction(tr("Set &Paragraph Spacing..."), self) + setLineSpacingAct.setStatusTip(tr("Change the gap between paragraphs")) + connect(setParagraphSpacingAct, SIGNAL("triggered()"), + self, SLOT("setParagraphSpacing()")) + + aboutAct = QAction(tr("&About"), self) + aboutAct.setStatusTip(tr("Show the application's About box")) + connect(aboutAct, SIGNAL("triggered()"), self, SLOT("about()")) + + aboutQtAct = QAction(tr("About &Qt"), self) + aboutQtAct.setStatusTip(tr("Show the Qt library's About box")) + connect(aboutQtAct, SIGNAL("triggered()"), qApp, SLOT("aboutQt()")) + connect(aboutQtAct, SIGNAL("triggered()"), self, SLOT("aboutQt()")) + + leftAlignAct = QAction(tr("&Left Align"), self) + leftAlignAct.setCheckable(True) + leftAlignAct.setShortcut(tr("Ctrl+L")) + leftAlignAct.setStatusTip(tr("Left align the selected text")) + connect(leftAlignAct, SIGNAL("triggered()"), self, SLOT("leftAlign()")) + + rightAlignAct = QAction(tr("&Right Align"), self) + rightAlignAct.setCheckable(True) + rightAlignAct.setShortcut(tr("Ctrl+R")) + rightAlignAct.setStatusTip(tr("Right align the selected text")) + connect(rightAlignAct, SIGNAL("triggered()"), self, SLOT("rightAlign()")) + + justifyAct = QAction(tr("&Justify"), self) + justifyAct.setCheckable(True) + justifyAct.setShortcut(tr("Ctrl+J")) + justifyAct.setStatusTip(tr("Justify the selected text")) + connect(justifyAct, SIGNAL("triggered()"), self, SLOT("justify()")) + + centerAct = QAction(tr("&Center"), self) + centerAct.setCheckable(True) + centerAct.setShortcut(tr("Ctrl+E")) + centerAct.setStatusTip(tr("Center the selected text")) + connect(centerAct, SIGNAL("triggered()"), self, SLOT("center()")) + +//! [6] //! [7] + alignmentGroup = QActionGroup(self) + alignmentGroup.addAction(leftAlignAct) + alignmentGroup.addAction(rightAlignAct) + alignmentGroup.addAction(justifyAct) + alignmentGroup.addAction(centerAct) + leftAlignAct.setChecked(True) +//! [6] + +//! [7] + +//! [8] +def createMenus(self): + +//! [9] //! [10] + fileMenu = menuBar().addMenu(tr("&File")) + fileMenu.addAction(Act) +//! [9] + fileMenu.addAction(openAct) +//! [10] + fileMenu.addAction(saveAct) + fileMenu.addAction(printAct) +//! [11] + fileMenu.addSeparator() +//! [11] + fileMenu.addAction(exitAct) + + editMenu = menuBar().addMenu(tr("&Edit")) + editMenu.addAction(undoAct) + editMenu.addAction(redoAct) + editMenu.addSeparator() + editMenu.addAction(cutAct) + editMenu.addAction(copyAct) + editMenu.addAction(pasteAct) + editMenu.addSeparator() + + helpMenu = menuBar().addMenu(tr("&Help")) + helpMenu.addAction(aboutAct) + helpMenu.addAction(aboutQtAct) +//! [8] + +//! [12] + formatMenu = editMenu.addMenu(tr("&Format")) + formatMenu.addAction(boldAct) + formatMenu.addAction(italicAct) + formatMenu.addSeparator()->setText(tr("Alignment")) + formatMenu.addAction(leftAlignAct) + formatMenu.addAction(rightAlignAct) + formatMenu.addAction(justifyAct) + formatMenu.addAction(centerAct) + formatMenu.addSeparator() + formatMenu.addAction(setLineSpacingAct) + formatMenu.addAction(setParagraphSpacingAct) +//! [12] diff --git a/doc/codesnippets/examples/painting/svggenerator/window.cpp b/doc/codesnippets/examples/painting/svggenerator/window.cpp new file mode 100644 index 000000000..79242e848 --- /dev/null +++ b/doc/codesnippets/examples/painting/svggenerator/window.cpp @@ -0,0 +1,98 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (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 http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QColorDialog> +#include <QFileDialog> +#include <QPainter> +#include <QSvgGenerator> +#include "window.h" +#include "displaywidget.h" + +Window::Window(QWidget *parent) + : QWidget(parent) +{ + setupUi(this); +} + +void Window::updateBackground(int background) +{ + displayWidget->setBackground(DisplayWidget::Background(background)); +} + +void Window::updateColor() +{ + QColor color = QColorDialog::getColor(displayWidget->color()); + if (color.isValid()) + displayWidget->setColor(color); +} + +void Window::updateShape(int shape) +{ + displayWidget->setShape(DisplayWidget::Shape(shape)); +} + +//! [save SVG] +def saveSvg(self): + newPath = QFileDialog.getSaveFileName(self, QObject.tr("Save SVG"), path, QObject.tr("SVG files (*.svg)")) + + if newPath.isEmpty(): + return + + path = newPath + +//![configure SVG generator] + generator = QSvgGenerator() + generator.setFileName(path) + generator.setSize(QSize(200, 200)) + generator.setViewBox(QRect(0, 0, 200, 200)) + generator.setTitle(QObject.tr("SVG Generator Example Drawing")) + generator.setDescription(QObject.tr("An SVG drawing created by the SVG Generator Example provided with Qt.")) +//![configure SVG generator] +//![begin painting] + painter = QPainter() + painter.begin(generator) +//![begin painting] + displayWidget->paint(painter) +//![end painting] + painter.end() +//![end painting] + +//! [save SVG] diff --git a/doc/codesnippets/examples/richtext/textobject/svgtextobject.h b/doc/codesnippets/examples/richtext/textobject/svgtextobject.h new file mode 100644 index 000000000..8fcd11411 --- /dev/null +++ b/doc/codesnippets/examples/richtext/textobject/svgtextobject.h @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the example classes 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$ +** +****************************************************************************/ + +#ifndef SVGTEXTOBJECT_H +#define SVGTEXTOBJECT_H + +#include <QTextObjectInterface> + +QT_BEGIN_NAMESPACE +class QTextDocument; +class QTextFormat; +class QPainter; +class QRectF; +class QSizeF; +QT_END_NAMESPACE + +//![0] //![1] +class SvgTextObject(QObject, QTextObjectInterface): + def __init__(self,...): + super(SvgTextObject, self).__init__(...) + ... +//![1] + +public: + QSizeF intrinsicSize(QTextDocument *doc, int posInDocument, + const QTextFormat &format); + void drawObject(QPainter *painter, const QRectF &rect, QTextDocument *doc, + int posInDocument, const QTextFormat &format); +}; +//![0] + +#endif diff --git a/doc/codesnippets/examples/sql/querymodel/editablesqlmodel.cpp b/doc/codesnippets/examples/sql/querymodel/editablesqlmodel.cpp new file mode 100644 index 000000000..b1d14de07 --- /dev/null +++ b/doc/codesnippets/examples/sql/querymodel/editablesqlmodel.cpp @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (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 http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtSql> + +#include "editablesqlmodel.h" + +EditableSqlModel::EditableSqlModel(QObject *parent) + : QSqlQueryModel(parent) +{ +} + +//! [0] +def flags(self, index): + flags = QSqlQueryModel.flags(index) + if index.column() == 1 or index.column() == 2: + flags |= Qt.ItemIsEditable + return flags +//! [0] + +//! [1] +def setData(self, index, value, role): + if index.column() < 1 or index.column() > 2: + return False + + primaryKeyIndex = QSqlQueryModel.index(index.row(), 0) + id = self.data(primaryKeyIndex).toInt() + + self.clear() + + ok = False + if index.column() == 1: + ok = self.setFirstName(id, value.toString()) + else: + ok = self.setLastName(id, value.toString()) + self.refresh() + return ok +} +//! [1] + +void EditableSqlModel::refresh() +{ + setQuery("select * from person"); + setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); + setHeaderData(1, Qt::Horizontal, QObject::tr("First name")); + setHeaderData(2, Qt::Horizontal, QObject::tr("Last name")); +} + +//! [2] +def setFirstName(self, personId, firstName): + query = QSqlQuery() + query.prepare("update person set firstname = ? where id = ?") + query.addBindValue(firstName) + query.addBindValue(personId) + return query.exec() +//! [2] + +bool EditableSqlModel::setLastName(int personId, const QString &lastName) +{ + QSqlQuery query; + query.prepare("update person set lastname = ? where id = ?"); + query.addBindValue(lastName); + query.addBindValue(personId); + return query.exec(); +} diff --git a/doc/codesnippets/examples/sql/relationaltablemodel/relationaltablemodel.cpp b/doc/codesnippets/examples/sql/relationaltablemodel/relationaltablemodel.cpp new file mode 100644 index 000000000..93c5209b2 --- /dev/null +++ b/doc/codesnippets/examples/sql/relationaltablemodel/relationaltablemodel.cpp @@ -0,0 +1,111 @@ +###################################### +# +# Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +# Contact: Qt Software Information (qt-info@nokia.com) +# +# This file is part of the example classes of the Qt Toolkit. +# +# $QT_BEGIN_LICENSE:LGPL$ +# Commercial Usage +# Licensees holding valid Qt Commercial licenses may use self 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, self 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 self 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 self +# package. +# +# GNU General Public License Usage +# Alternatively, self 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 self 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$ +# +###################################### + +from PySide.QtGui import * +from PySide.QtSql import * + +def initializeModel(model): +//! [0] + model.setTable("employee") +//! [0] + + model.setEditStrategy(QSqlTableModel.OnManualSubmit) +//! [1] + model.setRelation(2, QSqlRelation("city", "id", "name")) +//! [1] //! [2] + model.setRelation(3, QSqlRelation("country", "id", "name")) +//! [2] + +//! [3] + model.setHeaderData(0, Qt.Horizontal, QObject::tr("ID")) + model.setHeaderData(1, Qt.Horizontal, QObject::tr("Name")) + model.setHeaderData(2, Qt.Horizontal, QObject::tr("City")) + model.setHeaderData(3, Qt.Horizontal, QObject::tr("Country")) +//! [3] + + model.select() + + +def createView(title, model): +//! [4] + view = QTableView() + view.setModel(model) + view.setItemDelegate(QSqlRelationalDelegate(view)) +//! [4] + view.setWindowTitle(title) + return view + + +def createRelationalTables(): + query = QSqlQuery() + query.exec_("create table employee(id int primary key, name varchar(20), city int, country int)") + query.exec_("insert into employee values(1, 'Espen', 5000, 47)") + query.exec_("insert into employee values(2, 'Harald', 80000, 49)") + query.exec_("insert into employee values(3, 'Sam', 100, 1)") + + query.exec_("create table city(id int, name varchar(20))") + query.exec_("insert into city values(100, 'San Jose')") + query.exec_("insert into city values(5000, 'Oslo')") + query.exec_("insert into city values(80000, 'Munich')") + + query.exec_("create table country(id int, name varchar(20))") + query.exec_("insert into country values(1, 'USA')") + query.exec_("insert into country values(47, 'Norway')") + query.exec_("insert into country values(49, 'Germany')") + + +def main(): + + app = QApplication([]) + if !createConnection(): + return 1 + + createRelationalTables() + + QSqlRelationalTableModel model + + initializeModel(&model) + + view = createView(QObject.tr("Relational Table Model"), &model) + view.show() + + return app.exec_() + diff --git a/doc/codesnippets/examples/widgets/analogclock/analogclock.cpp b/doc/codesnippets/examples/widgets/analogclock/analogclock.cpp new file mode 100644 index 000000000..2e631a2dd --- /dev/null +++ b/doc/codesnippets/examples/widgets/analogclock/analogclock.cpp @@ -0,0 +1,146 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (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 http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "analogclock.h" + +//! [0] //! [1] +AnalogClock::AnalogClock(QWidget *parent) +//! [0] //! [2] + : QWidget(parent) +//! [2] //! [3] +{ +//! [3] //! [4] + timer = QTimer(self) +//! [4] //! [5] + self.connect(timer, SIGNAL("timeout()"), self.update) +//! [5] //! [6] + timer.start(1000) +//! [6] + + setWindowTitle(tr("Analog Clock")); + resize(200, 200); +//! [7] +} +//! [1] //! [7] + +//! [8] //! [9] +void AnalogClock::paintEvent(QPaintEvent *) +//! [8] //! [10] +{ + static const QPoint hourHand[3] = { + QPoint(7, 8), + QPoint(-7, 8), + QPoint(0, -40) + }; + static const QPoint minuteHand[3] = { + QPoint(7, 8), + QPoint(-7, 8), + QPoint(0, -70) + }; + + QColor hourColor(127, 0, 127); + QColor minuteColor(0, 127, 127, 191); + + int side = qMin(width(), height()); + QTime time = QTime::currentTime(); +//! [10] + +//! [11] + QPainter painter(this); +//! [11] //! [12] + painter.setRenderHint(QPainter::Antialiasing); +//! [12] //! [13] + painter.translate(width() / 2, height() / 2); +//! [13] //! [14] + painter.scale(side / 200.0, side / 200.0); +//! [9] //! [14] + +//! [15] + painter.setPen(Qt::NoPen); +//! [15] //! [16] + painter.setBrush(hourColor); +//! [16] + +//! [17] //! [18] + painter.save(); +//! [17] //! [19] + painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0))); + painter.drawConvexPolygon(hourHand, 3); + painter.restore(); +//! [18] //! [19] + +//! [20] + painter.setPen(hourColor); +//! [20] //! [21] + + for (int i = 0; i < 12; ++i) { + painter.drawLine(88, 0, 96, 0); + painter.rotate(30.0); + } +//! [21] + +//! [22] + painter.setPen(Qt::NoPen); +//! [22] //! [23] + painter.setBrush(minuteColor); + +//! [24] + painter.save(); + painter.rotate(6.0 * (time.minute() + time.second() / 60.0)); + painter.drawConvexPolygon(minuteHand, 3); + painter.restore(); +//! [23] //! [24] + +//! [25] + painter.setPen(minuteColor); +//! [25] //! [26] + +//! [27] + for (int j = 0; j < 60; ++j) { + if ((j % 5) != 0) + painter.drawLine(92, 0, 96, 0); + painter.rotate(6.0); + } +//! [27] +} +//! [26] diff --git a/doc/codesnippets/examples/widgets/icons/iconsizespinbox.cpp b/doc/codesnippets/examples/widgets/icons/iconsizespinbox.cpp new file mode 100644 index 000000000..2a518b7c1 --- /dev/null +++ b/doc/codesnippets/examples/widgets/icons/iconsizespinbox.cpp @@ -0,0 +1,63 @@ +############################################################################ +## +## Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +## Contact: Qt Software Information (qt-info@nokia.com) +## +## This file is part of the example classes of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:LGPL$ +## Commercial Usage +## Licensees holding valid Qt Commercial licenses may use self 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, self 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 self 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 self +## package. +## +## GNU General Public License Usage +## Alternatively, self 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 self 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$ +## +############################################################################ + + +//! [0] +def __init__(self, parent): + QSpinBox.__init__(self, parent) + +//! [0] + +//! [1] +def valueFromText(self, text): + regExp = QRegExp(tr("(\\d+)(\\s*[xx]\\s*\\d+)?")) + + if regExp.exactMatch(text): + return regExp.cap(1).toInt() + else: + return 0 +//! [1] + +//! [2] +def textFromValue(self, value): + return self.tr("%1 x %1").arg(value) + +//! [2] diff --git a/doc/codesnippets/examples/widgets/spinboxes/window.cpp b/doc/codesnippets/examples/widgets/spinboxes/window.cpp new file mode 100644 index 000000000..03df1421d --- /dev/null +++ b/doc/codesnippets/examples/widgets/spinboxes/window.cpp @@ -0,0 +1,240 @@ +###################################### +# +# Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +# Contact: Qt Software Information (qt-info@nokia.com) +# +# This file is part of the example classes of the Qt Toolkit. +# +# $QT_BEGIN_LICENSE:LGPL$ +# Commercial Usage +# Licensees holding valid Qt Commercial licenses may use self 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, self 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 self 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 self +# package. +# +# GNU General Public License Usage +# Alternatively, self 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 self 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$ +# +###################################### + +from PySide.QtGui import * + +//! [0] +def __init__(self): + createSpinBoxes() + createDateTimeEdits() + createDoubleSpinBoxes() + + layout = QHBoxLayout() + layout.addWidget(spinBoxesGroup) + layout.addWidget(editsGroup) + layout.addWidget(doubleSpinBoxesGroup) + setLayout(layout) + + setWindowTitle(tr("Spin Boxes")) +//! [0] + +//! [1] +def createSpinBoxes(self): + spinBoxesGroup = QGroupBox(tr("Spinboxes")) + + integerLabel = QLabel(tr("Enter a value between " + "%1 and %2:").arg(-20).arg(20)) + integerSpinBox = QSpinBox() + integerSpinBox.setRange(-20, 20) + integerSpinBox.setSingleStep(1) + integerSpinBox.setValue(0) +//! [1] + +//! [2] + zoomLabel = QLabel(tr("Enter a zoom value between " + "%1 and %2:").arg(0).arg(1000)) +//! [3] + zoomSpinBox = QSpinBox() + zoomSpinBox.setRange(0, 1000) + zoomSpinBox.setSingleStep(10) + zoomSpinBox.setSuffix("%") + zoomSpinBox.setSpecialValueText(tr("Automatic")) + zoomSpinBox.setValue(100) +//! [2] //! [3] + +//! [4] + priceLabel = QLabel(tr("Enter a price between " + "%1 and %2:").arg(0).arg(999)) + priceSpinBox = QSpinBox() + priceSpinBox.setRange(0, 999) + priceSpinBox.setSingleStep(1) + priceSpinBox.setPrefix("$") + priceSpinBox.setValue(99) +//! [4] //! [5] + + spinBoxLayout = QVBoxLayout() + spinBoxLayout.addWidget(integerLabel) + spinBoxLayout.addWidget(integerSpinBox) + spinBoxLayout.addWidget(zoomLabel) + spinBoxLayout.addWidget(zoomSpinBox) + spinBoxLayout.addWidget(priceLabel) + spinBoxLayout.addWidget(priceSpinBox) + spinBoxesGroup.setLayout(spinBoxLayout) + +//! [5] + +//! [6] +def createDateTimeEdits(self): + editsGroup = QGroupBox(tr("Date and time spin boxes")) + + dateLabel = QLabel() + dateEdit = QDateEdit(QDate.currentDate()) + dateEdit.setDateRange(QDate(2005, 1, 1), QDate(2010, 12, 31)) + dateLabel.setText(tr("Appointment date (between %0 and %1):") + .arg(dateEdit.minimumDate().toString(Qt.ISODate)) + .arg(dateEdit.maximumDate().toString(Qt.ISODate))) +//! [6] + +//! [7] + timeLabel = QLabel() + timeEdit = QTimeEdit(QTime.currentTime()) + timeEdit.setTimeRange(QTime(9, 0, 0, 0), QTime(16, 30, 0, 0)) + timeLabel.setText(tr("Appointment time (between %0 and %1):") + .arg(timeEdit.minimumTime().toString(Qt.ISODate)) + .arg(timeEdit.maximumTime().toString(Qt.ISODate))) +//! [7] + +//! [8] + meetingLabel = QLabel() + meetingEdit = QDateTimeEdit(QDateTime.currentDateTime()) +//! [8] + +//! [9] + formatLabel = QLabel(tr("Format string for the meeting date " + "and time:")) + formatComboBox = QComboBox() + formatComboBox.addItem("yyyy-MM-dd hh:mm:ss (zzz 'ms')") + formatComboBox.addItem("hh:mm:ss MM/dd/yyyy") + formatComboBox.addItem("hh:mm:ss dd/MM/yyyy") + formatComboBox.addItem("hh:mm:ss") + formatComboBox.addItem("hh:mm ap") +//! [9] //! [10] + + connect(formatComboBox, SIGNAL("activated(const QString &)"), + self, SLOT("setFormatString(const QString &)")) +//! [10] + + setFormatString(formatComboBox.currentText()) + +//! [11] + editsLayout = QVBoxLayout() + editsLayout.addWidget(dateLabel) + editsLayout.addWidget(dateEdit) + editsLayout.addWidget(timeLabel) + editsLayout.addWidget(timeEdit) + editsLayout.addWidget(meetingLabel) + editsLayout.addWidget(meetingEdit) + editsLayout.addWidget(formatLabel) + editsLayout.addWidget(formatComboBox) + editsGroup.setLayout(editsLayout) +//! [11] + +//! [12] +def setFormatString(self, formatString): + meetingEdit.setDisplayFormat(formatString) +//! [12] //! [13] + if meetingEdit.displayedSections() & QDateTimeEdit.DateSections_Mask: + meetingEdit.setDateRange(QDate(2004, 11, 1), QDate(2005, 11, 30)) + meetingLabel.setText(tr("Meeting date (between %0 and %1):") + .arg(meetingEdit.minimumDate().toString(Qt.ISODate)) + .arg(meetingEdit.maximumDate().toString(Qt.ISODate))) + else: + meetingEdit.setTimeRange(QTime(0, 7, 20, 0), QTime(21, 0, 0, 0)) + meetingLabel.setText(tr("Meeting time (between %0 and %1):") + .arg(meetingEdit.minimumTime().toString(Qt.ISODate)) + .arg(meetingEdit.maximumTime().toString(Qt.ISODate))) +//! [13] + +//! [14] +def createDoubleSpinBoxes(): + doubleSpinBoxesGroup = QGroupBox(tr("Double precision spinboxes")) + + precisionLabel = QLabel(tr("Number of decimal places " + "to show:")) + precisionSpinBox = QSpinBox() + precisionSpinBox.setRange(0, 100) + precisionSpinBox.setValue(2) +//! [14] + +//! [15] + doubleLabel = QLabel(tr("Enter a value between " + "%1 and %2:").arg(-20).arg(20)) + doubleSpinBox = QDoubleSpinBox () + doubleSpinBox.setRange(-20.0, 20.0) + doubleSpinBox.setSingleStep(1.0) + doubleSpinBox.setValue(0.0) +//! [15] + +//! [16] + scaleLabel = QLabel(tr("Enter a scale factor between " + "%1 and %2:").arg(0).arg(1000.0)) + scaleSpinBox = QDoubleSpinBox() + scaleSpinBox.setRange(0.0, 1000.0) + scaleSpinBox.setSingleStep(10.0) + scaleSpinBox.setSuffix("%") + scaleSpinBox.setSpecialValueText(tr("No scaling")) + scaleSpinBox.setValue(100.0) +//! [16] + +//! [17] + priceLabel = QLabel(tr("Enter a price between " + "%1 and %2:").arg(0).arg(1000)) + priceSpinBox = QDoubleSpinBox() + priceSpinBox.setRange(0.0, 1000.0) + priceSpinBox.setSingleStep(1.0) + priceSpinBox.setPrefix("$") + priceSpinBox.setValue(99.99) + + connect(precisionSpinBox, SIGNAL("valueChanged(int)"), +//! [17] + self, SLOT("changePrecision(int))") + +//! [18] + spinBoxLayout = QVBoxLayout() + spinBoxLayout.addWidget(precisionLabel) + spinBoxLayout.addWidget(precisionSpinBox) + spinBoxLayout.addWidget(doubleLabel) + spinBoxLayout.addWidget(doubleSpinBox) + spinBoxLayout.addWidget(scaleLabel) + spinBoxLayout.addWidget(scaleSpinBox) + spinBoxLayout.addWidget(priceLabel) + spinBoxLayout.addWidget(priceSpinBox) + doubleSpinBoxesGroup.setLayout(spinBoxLayout) +} +//! [18] + +//! [19] +def changePrecision(self, int) + doubleSpinBox.setDecimals(decimals) + scaleSpinBox.setDecimals(decimals) + priceSpinBox.setDecimals(decimals) + +//! [19] diff --git a/doc/codesnippets/examples/xml/streambookmarks/xbelreader.cpp b/doc/codesnippets/examples/xml/streambookmarks/xbelreader.cpp new file mode 100644 index 000000000..c6fa9d193 --- /dev/null +++ b/doc/codesnippets/examples/xml/streambookmarks/xbelreader.cpp @@ -0,0 +1,201 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (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 http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "xbelreader.h" + +//! [0] +XbelReader::XbelReader(QTreeWidget *treeWidget) + : treeWidget(treeWidget) +{ + QStyle *style = treeWidget->style(); + + folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirClosedIcon), + QIcon::Normal, QIcon::Off); + folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirOpenIcon), + QIcon::Normal, QIcon::On); + bookmarkIcon.addPixmap(style->standardPixmap(QStyle::SP_FileIcon)); +} +//! [0] + +//! [1] +def read(self, device): + self.setDevice(device) + + while not atEnd(): + readNext() + + if isStartElement(): + if self.name() == "xbel" and self.attributes().value("version") == "1.0": + self.readXBEL() + else: + self.raiseError(QObject.tr("The file is not an XBEL version 1.0 file.")); + + return not self.error(); +//! [1] + +//! [2] +void XbelReader::readUnknownElement() +{ + Q_ASSERT(isStartElement()); + + while (!atEnd()) { + readNext(); + + if (isEndElement()) + break; + + if (isStartElement()) + readUnknownElement(); + } +} +//! [2] + +//! [3] +void XbelReader::readXBEL() +{ + Q_ASSERT(isStartElement() && name() == "xbel"); + + while (!atEnd()) { + readNext(); + + if (isEndElement()) + break; + + if (isStartElement()) { + if (name() == "folder") + readFolder(0); + else if (name() == "bookmark") + readBookmark(0); + else if (name() == "separator") + readSeparator(0); + else + readUnknownElement(); + } + } +} +//! [3] + +//! [4] +void XbelReader::readTitle(QTreeWidgetItem *item) +{ + Q_ASSERT(isStartElement() && name() == "title"); + + QString title = readElementText(); + item->setText(0, title); +} +//! [4] + +//! [5] +void XbelReader::readSeparator(QTreeWidgetItem *item) +{ + QTreeWidgetItem *separator = createChildItem(item); + separator->setFlags(item->flags() & ~Qt::ItemIsSelectable); + separator->setText(0, QString(30, 0xB7)); + readElementText(); +} +//! [5] + +void XbelReader::readFolder(QTreeWidgetItem *item) +{ + Q_ASSERT(isStartElement() && name() == "folder"); + + QTreeWidgetItem *folder = createChildItem(item); + bool folded = (attributes().value("folded") != "no"); + treeWidget->setItemExpanded(folder, !folded); + + while (!atEnd()) { + readNext(); + + if (isEndElement()) + break; + + if (isStartElement()) { + if (name() == "title") + readTitle(folder); + else if (name() == "folder") + readFolder(folder); + else if (name() == "bookmark") + readBookmark(folder); + else if (name() == "separator") + readSeparator(folder); + else + readUnknownElement(); + } + } +} + +void XbelReader::readBookmark(QTreeWidgetItem *item) +{ + Q_ASSERT(isStartElement() && name() == "bookmark"); + + QTreeWidgetItem *bookmark = createChildItem(item); + bookmark->setFlags(bookmark->flags() | Qt::ItemIsEditable); + bookmark->setIcon(0, bookmarkIcon); + bookmark->setText(0, QObject::tr("Unknown title")); + bookmark->setText(1, attributes().value("href").toString()); + while (!atEnd()) { + readNext(); + + if (isEndElement()) + break; + + if (isStartElement()) { + if (name() == "title") + readTitle(bookmark); + else + readUnknownElement(); + } + } +} + +QTreeWidgetItem *XbelReader::createChildItem(QTreeWidgetItem *item) +{ + QTreeWidgetItem *childItem; + if (item) { + childItem = new QTreeWidgetItem(item); + } else { + childItem = new QTreeWidgetItem(treeWidget); + } + childItem->setData(0, Qt::UserRole, name().toString()); + return childItem; +} diff --git a/doc/codesnippets/examples/xml/streambookmarks/xbelreader.h b/doc/codesnippets/examples/xml/streambookmarks/xbelreader.h new file mode 100644 index 000000000..666261443 --- /dev/null +++ b/doc/codesnippets/examples/xml/streambookmarks/xbelreader.h @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (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 http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef XBELREADER_H +#define XBELREADER_H + +#include <QIcon> +#include <QXmlStreamReader> + +QT_BEGIN_NAMESPACE +class QTreeWidget; +class QTreeWidgetItem; +QT_END_NAMESPACE + +//! [0] +class XbelReader (QXmlStreamReader): +//! [1] + def __init__(self, treeWidget): + ... +//! [1] + + def read(self, device); + ... + +//! [2] + def readUnknownElement(self): + ... + def readXBEL(self): + ... + def readTitle(self, item): + ... + def readSeparator(self, item): + ... + def readFolder(self, item): + ... + def readBookmark(self, item): + ... + def createChildItem(self, item): + ... +//! [2] +//! [0] + +#endif diff --git a/doc/codesnippets/examples/xml/streambookmarks/xbelwriter.cpp b/doc/codesnippets/examples/xml/streambookmarks/xbelwriter.cpp new file mode 100644 index 000000000..ada7f9f87 --- /dev/null +++ b/doc/codesnippets/examples/xml/streambookmarks/xbelwriter.cpp @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (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 http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "xbelwriter.h" + +//! [0] +XbelWriter::XbelWriter(QTreeWidget *treeWidget) + : treeWidget(treeWidget) +{ + setAutoFormatting(true); +} +//! [0] + +//! [1] +def writeFile(self, device): + self.setDevice(device) + + self.writeStartDocument() + self.writeDTD("<!DOCTYPE xbel>") + self.writeStartElement("xbel") + self.writeAttribute("version", "1.0") + for i in range(0, self.treeWidget.topLevelItemCount()): + self.writeItem(self.treeWidget.topLevelItem(i)) + + self.writeEndDocument() + return True; +//! [1] + +//! [2] +void XbelWriter::writeItem(QTreeWidgetItem *item) +{ + QString tagName = item->data(0, Qt::UserRole).toString(); + if (tagName == "folder") { + bool folded = !treeWidget->isItemExpanded(item); + writeStartElement(tagName); + writeAttribute("folded", folded ? "yes" : "no"); + writeTextElement("title", item->text(0)); + for (int i = 0; i < item->childCount(); ++i) + writeItem(item->child(i)); + writeEndElement(); + } else if (tagName == "bookmark") { + writeStartElement(tagName); + if (!item->text(1).isEmpty()) + writeAttribute("href", item->text(1)); + writeTextElement("title", item->text(0)); + writeEndElement(); + } else if (tagName == "separator") { + writeEmptyElement(tagName); + } +} +//! [2] |