From ed843bf548273c87e5363aa6844a377f439d98e2 Mon Sep 17 00:00:00 2001
From: Friedemann Kleint <Friedemann.Kleint@nokia.com>
Date: Wed, 7 Jul 2010 17:20:54 +0200
Subject: [PATCH] Custom wizards: Add a checkbox.

Add a checkbox with configureable 'true'/'false' strings and illustrate
how to comment out a profile line using it in helloworld-example.
---
 .../templates/wizards/helloworld/project.pro  |  4 +-
 .../wizards/helloworld/wizard_sample.xml      | 10 +++-
 .../customwizard/customwizardpage.cpp         | 51 ++++++++++++++++++-
 .../customwizard/customwizardpage.h           | 35 ++++++++++++-
 4 files changed, 95 insertions(+), 5 deletions(-)

diff --git a/share/qtcreator/templates/wizards/helloworld/project.pro b/share/qtcreator/templates/wizards/helloworld/project.pro
index 4385bce0d4b..4b791337a1b 100644
--- a/share/qtcreator/templates/wizards/helloworld/project.pro
+++ b/share/qtcreator/templates/wizards/helloworld/project.pro
@@ -1,4 +1,6 @@
-QT       -= core
+QT       = core
+%NETWORK%QT += network
+
 CONFIG   += console
 CONFIG   -= app_bundle
 
diff --git a/share/qtcreator/templates/wizards/helloworld/wizard_sample.xml b/share/qtcreator/templates/wizards/helloworld/wizard_sample.xml
index 7f343b666a4..fe5c1fbaad0 100644
--- a/share/qtcreator/templates/wizards/helloworld/wizard_sample.xml
+++ b/share/qtcreator/templates/wizards/helloworld/wizard_sample.xml
@@ -29,9 +29,9 @@
 **
 **************************************************************************/
 
-Custom project wizard configuration example file. Note that by convention, 
+Custom project wizard configuration example file. Note that by convention,
 the project file goes last.
-The "class" and "firstpage" attributes specify that it is a Qt 4 wizard and 
+The "class" and "firstpage" attributes specify that it is a Qt 4 wizard and
 leave room for the Qt 4 target page.
 -->
 <wizard version="1" kind="project"
@@ -55,5 +55,11 @@ leave room for the Qt 4 target page.
             <fielddescription>Hello world message:</fielddescription>
             <fielddescription xml:lang="de">Hallo-Welt-Nachricht:</fielddescription>
         </field>
+	<!-- Comment out network in profile according to user's wishes -->
+        <field name="NETWORK">
+            <fieldcontrol class="QCheckBox" truevalue="" falsevalue="# "/>
+            <fielddescription>Include network module</fielddescription>
+            <fielddescription xml:lang="de">Netzwerk-Modul verwenden</fielddescription>
+        </field>
     </fields>
 </wizard>
diff --git a/src/plugins/projectexplorer/customwizard/customwizardpage.cpp b/src/plugins/projectexplorer/customwizard/customwizardpage.cpp
index 1fbd764ffa9..3be26446674 100644
--- a/src/plugins/projectexplorer/customwizard/customwizardpage.cpp
+++ b/src/plugins/projectexplorer/customwizard/customwizardpage.cpp
@@ -49,6 +49,7 @@ enum { debug = 0 };
 namespace ProjectExplorer {
 namespace Internal {
 
+// ----------- TextFieldComboBox
 TextFieldComboBox::TextFieldComboBox(QWidget *parent) :
     QComboBox(parent)
 {
@@ -63,6 +64,29 @@ void TextFieldComboBox::setText(const QString &s)
         setCurrentIndex(index);
 }
 
+// -------------- TextCheckBox
+TextFieldCheckBox::TextFieldCheckBox(const QString &text, QWidget *parent) :
+        QCheckBox(text, parent),
+        m_trueText(QLatin1String("true")), m_falseText(QLatin1String("false"))
+{
+    connect(this, SIGNAL(stateChanged(int)), this, SLOT(slotStateChanged(int)));
+}
+
+QString TextFieldCheckBox::text() const
+{
+    return isChecked() ? m_trueText : m_falseText;
+}
+
+void TextFieldCheckBox::setText(const QString &s)
+{
+    setChecked(s == m_trueText);
+}
+
+void TextFieldCheckBox::slotStateChanged(int cs)
+{
+    emit textChanged(cs == Qt::Checked ? m_trueText : m_falseText);
+}
+
 // --------------- CustomWizardFieldPage
 
 CustomWizardFieldPage::LineEditData::LineEditData(QLineEdit* le, const QString &defText) :
@@ -107,6 +131,7 @@ void CustomWizardFieldPage::addField(const CustomWizardField &field)\
     QString fieldName = field.name;
     if (field.mandatory)
         fieldName += QLatin1Char('*');
+    bool spansRow = false;
     // Check known classes: QComboBox
     const QString className = field.controlAttributes.value(QLatin1String("class"));
     QWidget *fieldWidget = 0;
@@ -116,10 +141,17 @@ void CustomWizardFieldPage::addField(const CustomWizardField &field)\
         fieldWidget = registerTextEdit(fieldName, field);
     } else if (className == QLatin1String("Utils::PathChooser")) {
         fieldWidget = registerPathChooser(fieldName, field);
+    } else if (className == QLatin1String("QCheckBox")) {
+        fieldWidget = registerCheckBox(fieldName, field.description, field);
+        spansRow = true; // Do not create a label for the checkbox.
     } else {
         fieldWidget = registerLineEdit(fieldName, field);
     }
-    addRow(field.description, fieldWidget);
+    if (spansRow) {
+        m_formLayout->addRow(fieldWidget);
+    } else {
+        addRow(field.description, fieldWidget);
+    }
 }
 
 QWidget *CustomWizardFieldPage::registerComboBox(const QString &fieldName,
@@ -162,6 +194,23 @@ QWidget *CustomWizardFieldPage::registerPathChooser(const QString &fieldName,
     return pathChooser;
 } // Utils::PathChooser
 
+QWidget *CustomWizardFieldPage::registerCheckBox(const QString &fieldName,
+                                                 const QString &fieldDescription,
+                                                 const CustomWizardField &field)
+{
+    typedef CustomWizardField::ControlAttributeMap::const_iterator AttributeMapConstIt;
+
+    TextFieldCheckBox *checkBox = new TextFieldCheckBox(fieldDescription);
+    const AttributeMapConstIt trueTextIt = field.controlAttributes.constFind(QLatin1String("truevalue"));
+    if (trueTextIt != field.controlAttributes.constEnd()) // Also set empty texts
+        checkBox->setTrueText(trueTextIt.value());
+    const AttributeMapConstIt falseTextIt = field.controlAttributes.constFind(QLatin1String("falsevalue"));
+    if (falseTextIt != field.controlAttributes.constEnd()) // Also set empty texts
+        checkBox->setFalseText(falseTextIt.value());
+    registerField(fieldName, checkBox, "text", SIGNAL(textChanged(QString)));
+    return checkBox;
+}
+
 QWidget *CustomWizardFieldPage::registerLineEdit(const QString &fieldName,
                                                  const CustomWizardField &field)
 {
diff --git a/src/plugins/projectexplorer/customwizard/customwizardpage.h b/src/plugins/projectexplorer/customwizard/customwizardpage.h
index 936c033f3f6..ccf27ae198c 100644
--- a/src/plugins/projectexplorer/customwizard/customwizardpage.h
+++ b/src/plugins/projectexplorer/customwizard/customwizardpage.h
@@ -31,6 +31,7 @@
 #define CUSTOMPROJECTWIZARDDIALOG_H
 
 #include <QtGui/QComboBox>
+#include <QtGui/QCheckBox>
 #include <QtGui/QWizardPage>
 #include <QtCore/QSharedPointer>
 
@@ -52,7 +53,7 @@ struct CustomWizardParameters;
 struct CustomWizardContext;
 
 // A non-editable combo for text editing purposes that plays
-// with QWizard::registerField (providing a settable text property).
+// with QWizard::registerField (providing a settable 'text' property).
 class TextFieldComboBox : public QComboBox {
     Q_PROPERTY(QString text READ text WRITE setText)
     Q_OBJECT
@@ -66,6 +67,35 @@ signals:
     void text4Changed(const QString &); // Do not conflict with Qt 3 compat signal.
 };
 
+// A Checkbox that plays with QWizard::registerField (providing a settable
+// 'text' property containing predefined strings for 'true'/'false').
+class TextFieldCheckBox : public QCheckBox {
+    Q_PROPERTY(QString text READ text WRITE setText)
+    Q_PROPERTY(QString trueText READ trueText WRITE setTrueText)
+    Q_PROPERTY(QString falseText READ falseText WRITE setFalseText)
+    Q_OBJECT
+public:
+    explicit TextFieldCheckBox(const QString &text, QWidget *parent = 0);
+
+    QString text() const;
+    void setText(const QString &s);
+
+    void setTrueText(const QString &t)  { m_trueText = t;    }
+    QString trueText() const            { return m_trueText; }
+    void setFalseText(const QString &t) { m_falseText = t; }
+    QString falseText() const              { return m_falseText; }
+
+signals:
+    void textChanged(const QString &);
+
+private slots:
+    void slotStateChanged(int);
+
+private:
+    QString m_trueText;
+    QString m_falseText;
+};
+
 // A simple custom wizard page presenting the fields to be used
 // as page 2 of a BaseProjectWizardDialog if there are any fields.
 // Uses the 'field' functionality of QWizard.
@@ -105,6 +135,9 @@ private:
     QWidget *registerComboBox(const QString &fieldName, const CustomWizardField &field);
     QWidget *registerTextEdit(const QString &fieldName, const CustomWizardField &field);
     QWidget *registerPathChooser(const QString &fieldName, const CustomWizardField &field);
+    QWidget *registerCheckBox(const QString &fieldName,
+                              const QString &fieldDescription,
+                              const CustomWizardField &field);
     void addField(const CustomWizardField &f);
 
     const QSharedPointer<CustomWizardContext> m_context;
-- 
GitLab