webpagewizard.cpp 3.54 KB
Newer Older
1 2 3 4 5 6
//! [0]
#include "webpagewizard.h"

#include <utils/filewizarddialog.h>
#include <utils/qtcassert.h>

7
#include <QXmlStreamWriter>
8

9 10 11 12
#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPlainTextEdit>
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

namespace MyPlugin {
namespace Internal {

//! [1]
WebContentPageWizardPage::WebContentPageWizardPage(QWidget *parent) :
    QWizardPage(parent),
    m_titleLineEdit(new QLineEdit),
    m_textEdit(new QPlainTextEdit),
    m_complete(false)
{
    QGridLayout *layout = new QGridLayout(this);
    QLabel *titleLabel = new QLabel(tr("&Title"));
    layout->addWidget(titleLabel, 0, 0);
    layout->addWidget(m_titleLineEdit, 0, 1);
    QLabel *contentLabel = new QLabel(tr("&Content"));
    layout->addWidget(contentLabel, 1, 0, 1, 1, Qt::AlignTop);
    layout->addWidget(m_textEdit, 1, 1);
    titleLabel->setBuddy(m_titleLineEdit);
    contentLabel->setBuddy(m_textEdit);
    setTitle(tr("Web Page Contents"));

    connect(m_titleLineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotChanged()));
    connect(m_textEdit, SIGNAL(textChanged()), this, SLOT(slotChanged()));
}

QString WebContentPageWizardPage::title() const
{
    return m_titleLineEdit->text();
}

QString WebContentPageWizardPage::contents() const
{
    return m_textEdit->toPlainText();
}

void WebContentPageWizardPage::slotChanged()
{
    const bool completeNow = !m_titleLineEdit->text().isEmpty()
            && m_textEdit->blockCount() > 0;
    if (completeNow != m_complete) {
        m_complete = completeNow;
        emit completeChanged();
    }
}

//! [1] //! [2]

WebContentWizardDialog::WebContentWizardDialog(QWidget *parent) :
    Utils::FileWizardDialog(parent), m_contentPage(new WebContentPageWizardPage)
{
    addPage(m_contentPage);
}

//! [2] //! [3]

WebPageWizard::WebPageWizard(const Core::BaseFileWizardParameters &parameters,
                             QObject *parent) :
    Core::BaseFileWizard(parameters, parent)
{
}

QWizard *WebPageWizard::createWizardDialog(QWidget *parent,
                                    const QString &defaultPath,
                                    const WizardPageList &extensionPages) const
{
    WebContentWizardDialog *dialog = new WebContentWizardDialog(parent);
    dialog->setPath(defaultPath);
    foreach (QWizardPage *p, extensionPages)
        dialog->addPage(p);
    return dialog;
}

Core::GeneratedFiles
    WebPageWizard::generateFiles(const QWizard *w,
                                     QString *) const
{
    Core::GeneratedFiles files;
    const WebContentWizardDialog *dialog = qobject_cast<const WebContentWizardDialog*>(w);
    QTC_ASSERT(dialog, return files; )

    const QString fileName = Core::BaseFileWizard::buildFileName(dialog->path(), dialog->fileName(), QLatin1String("html"));

    Core::GeneratedFile file(fileName);

    QString contents;
    QXmlStreamWriter writer(&contents);
    writer.setAutoFormatting(true);
    writer.writeStartDocument();
    writer.writeStartElement(QLatin1String("html"));
    writer.writeStartElement(QLatin1String("head"));
    writer.writeTextElement(QLatin1String("title"), dialog->title());
    writer.writeEndElement(); // HEAD
    writer.writeStartElement(QLatin1String("body"));
    writer.writeTextElement(QLatin1String("h1"), dialog->title());
    writer.writeTextElement(QLatin1String("p"), dialog->contents());
    writer.writeEndElement(); // BODY
    writer.writeEndElement(); // HTML
    file.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
    file.setContents(contents);
    files.push_back(file);
    return files;
}

//! [3]

} // namespace Internal
} // namespace MyPlugin

//! [0]