source: branches/vendor/trolltech/qt/current/tools/assistant/tools/qhelpconverter/conversionwizard.cpp @ 2

Last change on this file since 2 was 2, checked in by Dmitry A. Kuminov, 13 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 9.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information (qt-info@nokia.com)
5**
6** This file is part of the Qt Assistant of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file.  Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file.  Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at qt-sales@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtCore/QFileInfo>
43#include <QtCore/QDir>
44#include <QtCore/QVariant>
45#include <QtCore/QTimer>
46#include <QtGui/QApplication>
47#include <QtGui/QMouseEvent>
48
49#include "conversionwizard.h"
50#include "inputpage.h"
51#include "generalpage.h"
52#include "filterpage.h"
53#include "identifierpage.h"
54#include "pathpage.h"
55#include "filespage.h"
56#include "outputpage.h"
57#include "finishpage.h"
58#include "qhpwriter.h"
59#include "qhcpwriter.h"
60#include "helpwindow.h"
61
62QT_BEGIN_NAMESPACE
63
64ConversionWizard::ConversionWizard()
65{
66    setWindowIcon(QIcon(QLatin1String(":/trolltech/qhelpconverter/assistant.png")));
67    setWindowTitle(tr("Help Conversion Wizard"));
68    setPixmap(QWizard::WatermarkPixmap,
69        QPixmap(QLatin1String(":/trolltech/qhelpconverter/assistant-128.png"))) ;
70    setOptions(QWizard::IndependentPages|QWizard::NoBackButtonOnLastPage
71        |QWizard::HaveHelpButton);
72
73    m_inputPage = new InputPage(&m_adpReader);
74    setPage(Input_Page, m_inputPage);
75
76    m_generalPage = new GeneralPage();
77    setPage(General_Page, m_generalPage);
78
79    m_filterPage = new FilterPage();
80    setPage(Filter_Page, m_filterPage);
81    m_filterPage->setMaximumHeight(240);
82
83    m_identifierPage = new IdentifierPage();
84    setPage(Identifier_Page, m_identifierPage);
85
86    m_pathPage = new PathPage();
87    setPage(Path_Page, m_pathPage);
88    m_pathPage->setMaximumHeight(240);
89
90    m_filesPage = new FilesPage();
91    setPage(Files_Page, m_filesPage);
92    m_filesPage->setMaximumHeight(240);
93
94    m_outputPage = new OutputPage();
95    setPage(Output_Page, m_outputPage);
96    m_outputPage->setMaximumHeight(240);
97
98    m_finishPage = new FinishPage();
99    setPage(Finish_Page, m_finishPage);
100    m_finishPage->setMaximumHeight(240);
101
102    connect(this, SIGNAL(currentIdChanged(int)),
103        this, SLOT(pageChanged(int)));
104
105    m_helpWindow = 0;
106    qApp->installEventFilter(this);
107
108    QAbstractButton *btn = button(QWizard::HelpButton);
109    btn->setCheckable(true);
110    connect(btn, SIGNAL(toggled(bool)), this, SLOT(showHelp(bool)));
111}
112
113void ConversionWizard::setAdpFileName(const QString &fileName)
114{
115    setField(QLatin1String("adpFileName"), fileName);
116}
117
118void ConversionWizard::initializePage(int id)
119{
120    switch (id) {
121    case Path_Page: {
122        QFileInfo fi(field(QLatin1String("adpFileName")).toString());
123        m_pathPage->setPath(fi.absolutePath());
124        break;
125        }
126    case Output_Page: {
127        QFileInfo fi(field(QLatin1String("adpFileName")).toString());
128        m_outputPage->setPath(fi.absolutePath());
129        setField(QLatin1String("ProjectFileName"), fi.baseName()
130            + QLatin1String(".qhp"));
131        setField(QLatin1String("CollectionFileName"), fi.baseName()
132            + QLatin1String(".qhcp"));
133        break;
134        }
135    }
136}
137
138void ConversionWizard::pageChanged(int id)
139{
140    if (id == Files_Page) {
141        QApplication::setOverrideCursor(Qt::WaitCursor);
142        m_files.clear();
143        QFileInfo fi(field(QLatin1String("adpFileName")).toString());
144        QDir rootDir = fi.absolutePath();
145        foreach (const QString &p, m_pathPage->paths()) {
146            QDir dir(p);
147            QString rel = rootDir.relativeFilePath(dir.absolutePath());
148            if (!rel.isEmpty())
149                rel.append(QLatin1Char('/'));
150            foreach (const QString &f, dir.entryList(m_pathPage->filters()))
151                m_files.append(rel + f);
152        }
153        m_filesPage->setFilesToRemove(getUnreferencedFiles(m_files));
154        QApplication::restoreOverrideCursor();
155    } else if (id == Output_Page) {
156        m_outputPage->setCollectionComponentEnabled(
157            !m_adpReader.properties().isEmpty());
158    } else if (id == Finish_Page) {
159        QTimer::singleShot(300, this, SLOT(convert()));
160    }
161}
162
163void ConversionWizard::showHelp(bool toggle)
164{
165    int w = 180;
166    int h = 180;
167    if (!m_helpWindow) {
168        m_helpWindow = new HelpWindow(this);
169        m_helpWindow->setMaximumWidth(w);
170        m_helpWindow->setMaximumHeight(h);
171        m_helpWindow->setMinimumHeight(h);
172    }
173
174    if (toggle) {
175        m_helpWindow->setHelp(currentPage()->objectName());
176        QAbstractButton *btn = button(QWizard::HelpButton);
177        QPoint p = btn->pos();
178        int x = p.x();
179        if (btn->pos().x() > w)
180            x = p.x() + btn->width() - w;
181        m_helpWindow->move(x, p.y()-h);
182        m_helpWindow->show();
183    } else {
184        m_helpWindow->hide();
185    }
186}
187
188bool ConversionWizard::eventFilter(QObject *obj, QEvent *e)
189{
190    if (m_helpWindow && m_helpWindow->isVisible()) {
191        if (obj != button(QWizard::HelpButton)
192            && e->type() == QEvent::MouseButtonRelease) {
193            QMouseEvent *me = static_cast<QMouseEvent*>(e);
194            if (!m_helpWindow->geometry().contains(mapFromParent(me->globalPos()))) {
195                m_helpWindow->hide();
196                button(QWizard::HelpButton)->setChecked(false);
197            }
198        } else if (e->type() == QEvent::KeyPress) {
199            m_helpWindow->hide();
200            button(QWizard::HelpButton)->setChecked(false);
201        }
202    }
203    return QWizard::eventFilter(obj, e);
204}
205
206QStringList ConversionWizard::getUnreferencedFiles(const QStringList &files)
207{
208    QStringList lst;
209    QSet<QString> adpFiles = m_adpReader.files();
210    foreach (const QString &s, files) {
211        if (!adpFiles.contains(s))
212            lst.append(s);
213    }
214    return lst;
215}
216
217void ConversionWizard::convert()
218{
219    QFileInfo fi(field(QLatin1String("adpFileName")).toString());
220    m_finishPage->appendMessage(tr("Converting %1...")
221        .arg(fi.fileName()));
222    QApplication::setOverrideCursor(Qt::WaitCursor);
223    QString qhpFileName = field(QLatin1String("ProjectFileName")).toString();
224    QhpWriter qhpWriter(field(QLatin1String("namespaceName")).toString(),
225        field(QLatin1String("virtualFolder")).toString());
226    qhpWriter.setAdpReader(&m_adpReader);
227    qhpWriter.setFilterAttributes(m_filterPage->filterAttributes());
228    qhpWriter.setCustomFilters(m_filterPage->customFilters());
229
230    foreach (const QString &f, m_filesPage->filesToRemove())
231        m_files.removeAll(f);
232    qhpWriter.setFiles(m_files);
233
234    if (field(QLatin1String("createIdentifier")).toBool()) {
235        if (field(QLatin1String("fileNamePrefix")).toBool())
236            qhpWriter.generateIdentifiers(QhpWriter::FilePrefix);
237        else
238            qhpWriter.generateIdentifiers(QhpWriter::GlobalPrefix,
239                field(QLatin1String("globalPrefix")).toString());
240    } else {
241        qhpWriter.generateIdentifiers(QhpWriter::SkipAll);
242    }
243
244    qhpWriter.writeFile(fi.absolutePath() + QDir::separator()
245        + qhpFileName);
246
247    m_finishPage->appendMessage(tr("Writing help collection file..."));
248
249    if (!m_adpReader.properties().isEmpty()) {
250        QhcpWriter qhcpWriter;
251        qhcpWriter.setHelpProjectFile(qhpFileName);
252        qhcpWriter.setProperties(m_adpReader.properties());
253        qhcpWriter.setTitlePath(QLatin1String("qthelp://")
254            + field(QLatin1String("namespaceName")).toString()
255            + QLatin1String("/")
256            +field(QLatin1String("virtualFolder")).toString());
257        qhcpWriter.writeFile(fi.absolutePath() + QDir::separator()
258            + field(QLatin1String("CollectionFileName")).toString());
259    }
260
261    m_finishPage->appendMessage(tr("Done."));
262    QApplication::restoreOverrideCursor();
263}
264
265QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.