welcomemode.cpp 12.2 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
7
8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
9
** Commercial Usage
10
**
11
12
13
14
** 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.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** 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.
24
**
25
26
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "welcomemode.h"
31
#include "icore.h"
con's avatar
con committed
32
33
34
#include "coreconstants.h"
#include "uniqueidmanager.h"
#include "modemanager.h"
35
#include "rssfetcher.h"
con's avatar
con committed
36
37
38

#include <QtGui/QToolBar>
#include <QtGui/QDesktopServices>
39
#include <QtGui/QMouseEvent>
con's avatar
con committed
40

41
#include <QtCore/QDir>
42
#include <QtCore/QFileInfo>
con's avatar
con committed
43
44
#include <QtCore/QDebug>
#include <QtCore/QUrl>
45
#include <QtCore/QSettings>
con's avatar
con committed
46

47
48
#include "ui_welcomemode.h"

con's avatar
con committed
49
50
51
namespace Core {
namespace Internal {

hjk's avatar
hjk committed
52
53
struct WelcomeModePrivate
{
con's avatar
con committed
54
55
56
    WelcomeModePrivate();

    QWidget *m_widget;
57
    QWidget *m_welcomePage;
58
    QButtonGroup *btnGrp;
59
    Ui::WelcomePage ui;
60
    RSSFetcher *rssFetcher;
con's avatar
con committed
61
62
63
    WelcomeMode::WelcomePageData lastData;
};

64
WelcomeModePrivate::WelcomeModePrivate()
con's avatar
con committed
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
{
}

// ---  WelcomePageData

bool WelcomeMode::WelcomePageData::operator==(const WelcomePageData &rhs) const
{
    return previousSession == rhs.previousSession
        && activeSession   == rhs.activeSession
        && sessionList     == rhs.sessionList
        && projectList     == rhs.projectList;
}

bool WelcomeMode::WelcomePageData::operator!=(const WelcomePageData &rhs) const
{
    return previousSession != rhs.previousSession
        || activeSession   != rhs.activeSession
        || sessionList     != rhs.sessionList
        || projectList     != rhs.projectList;
}

QDebug operator<<(QDebug dgb, const WelcomeMode::WelcomePageData &d)
{
    dgb.nospace() << "PreviousSession=" << d.previousSession
        << " activeSession=" << d.activeSession
        << " sessionList=" << d.sessionList
        << " projectList=" << d.projectList;
    return dgb;
}

// ---  WelcomeMode
WelcomeMode::WelcomeMode() :
    m_d(new WelcomeModePrivate)
{
99
    m_d->m_widget = new QWidget;
con's avatar
con committed
100
101
102
103
    QVBoxLayout *l = new QVBoxLayout(m_d->m_widget);
    l->setMargin(0);
    l->setSpacing(0);
    l->addWidget(new QToolBar(m_d->m_widget));
104
    m_d->rssFetcher = new RSSFetcher(6, this);
105
106
107
108
    m_d->m_welcomePage = new QWidget(m_d->m_widget);
    m_d->ui.setupUi(m_d->m_welcomePage);
    m_d->ui.sessTreeWidget->viewport()->setAutoFillBackground(false);
    m_d->ui.projTreeWidget->viewport()->setAutoFillBackground(false);
109
110
111
    m_d->ui.newsTreeWidget->viewport()->setAutoFillBackground(false);
    m_d->ui.sitesTreeWidget->viewport()->setAutoFillBackground(false);
    m_d->ui.tutorialTreeWidget->viewport()->setAutoFillBackground(false);
112
113
114
115
    l->addWidget(m_d->m_welcomePage);

    updateWelcomePage(WelcomePageData());

116
117
118
119
    m_d->btnGrp = new QButtonGroup(this);
    m_d->btnGrp->addButton(m_d->ui.gettingStartedSectButton, 0);
    m_d->btnGrp->addButton(m_d->ui.developSectButton, 1);
    m_d->btnGrp->addButton(m_d->ui.communitySectButton, 2);
120

121
    connect(m_d->btnGrp, SIGNAL(buttonClicked(int)), m_d->ui.stackedWidget, SLOT(setCurrentIndex(int)));
122

123
124
125
126
127
    connect(m_d->ui.gettingStartedButton, SIGNAL(clicked()), SIGNAL(requestHelp()));
    connect(m_d->ui.feedbackButton, SIGNAL(clicked()), SLOT(slotFeedback()));
    connect(m_d->ui.restoreSessionButton, SIGNAL(clicked()), SLOT(slotRestoreLastSession()));
    connect(m_d->ui.sessTreeWidget, SIGNAL(activated(QString)), SLOT(slotSessionClicked(QString)));
    connect(m_d->ui.projTreeWidget, SIGNAL(activated(QString)), SLOT(slotProjectClicked(QString)));
128
129
    connect(m_d->ui.newsTreeWidget, SIGNAL(activated(QString)), SLOT(slotUrlClicked(QString)));
    connect(m_d->ui.sitesTreeWidget, SIGNAL(activated(QString)), SLOT(slotUrlClicked(QString)));
130
    connect(m_d->ui.tutorialTreeWidget, SIGNAL(activated(QString)), SIGNAL(openHelpPage(const QString&)));
131

132
133
    connect(m_d->rssFetcher, SIGNAL(newsItemReady(QString, QString)),
        m_d->ui.newsTreeWidget, SLOT(slotAddItem(QString, QString)));
134
135

    //: Add localized feed here only if one exists
136
    m_d->rssFetcher->fetch(QUrl(tr("http://labs.trolltech.com/blogs/feed")));
137
138
139
140
141
142
143

    m_d->ui.sitesTreeWidget->addItem(tr("Qt Software"), QLatin1String("http://www.qtsoftware.com"));
    m_d->ui.sitesTreeWidget->addItem(tr("Qt Labs"), QLatin1String("http://labs.qtsoftware.com"));
    m_d->ui.sitesTreeWidget->addItem(tr("Qt Git Hosting"), QLatin1String("http://qt.gitorious.org"));
    m_d->ui.sitesTreeWidget->addItem(tr("Qt Centre"), QLatin1String("http://www.qtcentre.org"));
    m_d->ui.sitesTreeWidget->addItem(tr("Qt/S60 at Forum Nokia"), QLatin1String("http://discussion.forum.nokia.com/forum/forumdisplay.php?f=196"));

144
145
146
147
148
149
150
151
152
153
154
155
156
    m_d->ui.tutorialTreeWidget->addItem(tr("Using Qt Creator"),
                                        QString("qthelp://com.nokia.qtcreator.%1%2/doc/index.html").arg(IDE_VERSION_MAJOR).arg(IDE_VERSION_MINOR));
    m_d->ui.tutorialTreeWidget->addItem(tr("Understanding Widgets"),
                                        QLatin1String("qthelp://com.trolltech.qt/qdoc/widgets-tutorial.html"));
    m_d->ui.tutorialTreeWidget->addItem(tr("Creating an address book"),
                                        QLatin1String("qthelp://com.trolltech.qt/qdoc/tutorials-addressbook.html"));
    m_d->ui.tutorialTreeWidget->addItem(tr("Building with qmake"),
                                        QLatin1String("qthelp://com.trolltech.qmake/qdoc/qmake-tutorial.html"));
    m_d->ui.tutorialTreeWidget->addItem(tr("Writing test cases"),
                                        QLatin1String("qthelp://com.trolltech.qt/qdoc/qtestlib-tutorial.html"));

    QSettings *settings = ICore::instance()->settings();
    int id = settings->value("General/WelcomeTab", 0).toInt();
157
158
    m_d->btnGrp->button(id)->setChecked(true);
    m_d->ui.stackedWidget->setCurrentIndex(id);
con's avatar
con committed
159
160
161
162
}

WelcomeMode::~WelcomeMode()
{
163
164
    QSettings *settings = ICore::instance()->settings();
    settings->setValue("General/WelcomeTab", m_d->btnGrp->checkedId());
con's avatar
con committed
165
166
167
168
169
    delete m_d;
}

QString WelcomeMode::name() const
{
170
    return tr("Welcome");
con's avatar
con committed
171
172
173
174
}

QIcon WelcomeMode::icon() const
{
175
    return QIcon(QLatin1String(":/core/images/qtcreator_logo_32.png"));
con's avatar
con committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
}

int WelcomeMode::priority() const
{
    return Constants::P_MODE_WELCOME;
}

QWidget* WelcomeMode::widget()
{
    return m_d->m_widget;
}

const char* WelcomeMode::uniqueModeName() const
{
    return Constants::MODE_WELCOME;
}

QList<int> WelcomeMode::context() const
{
    static QList<int> contexts = QList<int>()
196
                                 << UniqueIDManager::instance()->uniqueIdentifier(Constants::C_WELCOME_MODE);
con's avatar
con committed
197
198
199
200
201
202
203
204
205
206
    return contexts;
}

void WelcomeMode::updateWelcomePage(const WelcomePageData &welcomePageData)
{
    // Update only if data are modified
    if (welcomePageData == m_d->lastData)
        return;
    m_d->lastData = welcomePageData;

207
    m_d->m_widget->setUpdatesEnabled(false);
con's avatar
con committed
208
    if (!welcomePageData.previousSession.isEmpty() || !welcomePageData.projectList.isEmpty()) {
209
210
        m_d->ui.sessTreeWidget->clear();
        m_d->ui.projTreeWidget->clear();
con's avatar
con committed
211
212

        if (welcomePageData.sessionList.count() > 1) {
213
214
            foreach (const QString &s, welcomePageData.sessionList) {
                QString str = s;
con's avatar
con committed
215
                if (s == welcomePageData.previousSession)
216
217
                    str = tr("%1 (last session)").arg(s);
                m_d->ui.sessTreeWidget->addItem(str, s);
con's avatar
con committed
218
            }
219
220
221
222
            m_d->ui.sessTreeWidget->updateGeometry();
            m_d->ui.sessTreeWidget->show();
        } else {
            m_d->ui.sessTreeWidget->hide();
con's avatar
con committed
223
        }
224
225
226
227
228
229

        typedef QPair<QString, QString> QStringPair;
        foreach (const QStringPair &it, welcomePageData.projectList) {
            QTreeWidgetItem *item = m_d->ui.projTreeWidget->addItem(it.second, it.first);
            const QFileInfo fi(it.first);
            item->setToolTip(1, QDir::toNativeSeparators(fi.absolutePath()));
con's avatar
con committed
230
        }
231
        m_d->ui.projTreeWidget->updateGeometry();
con's avatar
con committed
232

233
234
235
236
237
238
239
        m_d->ui.recentSessionsFrame->show();
        m_d->ui.recentProjectsFrame->show();
    } else {
        m_d->ui.recentSessionsFrame->hide();
        m_d->ui.recentProjectsFrame->hide();
    }
    m_d->m_widget->setUpdatesEnabled(true);
con's avatar
con committed
240
241
}

242
void WelcomeMode::activateEditMode()
con's avatar
con committed
243
{
244
    Core::ModeManager *modeManager = ModeManager::instance();
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
    if (modeManager->currentMode() == this)
        modeManager->activateMode(Core::Constants::MODE_EDIT);
}

void WelcomeMode::slotSessionClicked(const QString &data)
{
    emit requestSession(data);
    activateEditMode();
}

void WelcomeMode::slotProjectClicked(const QString &data)
{
    emit requestProject(data);
    activateEditMode();
}

261
262
263
264
265
void WelcomeMode::slotUrlClicked(const QString &data)
{
    QDesktopServices::openUrl(QUrl(data));
}

266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
void WelcomeMode::slotRestoreLastSession()
{
    emit requestSession(m_d->lastData.previousSession);
    activateEditMode();
}

void WelcomeMode::slotFeedback()
{
    QDesktopServices::openUrl(QUrl(QLatin1String(
            "http://www.qtsoftware.com/forms/feedback-forms/qt-creator-user-feedback/view")));
}

// ---  WelcomeModeButton

WelcomeModeButton::WelcomeModeButton(QWidget *parent) :
        QLabel(parent),
282
283
        m_isPressed(false),
        m_isInited(false)
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
{
    setCursor(QCursor(Qt::PointingHandCursor));
}

void WelcomeModeButton::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
        m_isPressed = true;
}

void WelcomeModeButton::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton && m_isPressed) {
        m_isPressed = false;
        if (rect().contains(event->pos()))
            emit clicked();
con's avatar
con committed
300
301
302
    }
}

303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
void WelcomeModeButton::enterEvent(QEvent *)
{
    if (!m_isInited) {
        m_isInited = true;
        m_text = text();
        m_hoverText = m_text;
        m_hoverText.replace(QLatin1String(".png"), QLatin1String("_hover.png"));
        if (m_text == m_hoverText) {
            m_text.clear();
            m_hoverText.clear();
        }
    }
    if (!m_hoverText.isEmpty())
        setText(m_hoverText);
}

void WelcomeModeButton::leaveEvent(QEvent *)
{
    if (!m_text.isEmpty())
        setText(m_text);
}

325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// ---  WelcomeModeTreeWidget

WelcomeModeTreeWidget::WelcomeModeTreeWidget(QWidget *parent) :
        QTreeWidget(parent),
        m_bullet(QLatin1String(":/core/images/welcomemode/list_bullet_arrow.png"))
{
    connect(this, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
            SLOT(slotItemClicked(QTreeWidgetItem *)));
}

QSize WelcomeModeTreeWidget::sizeHint() const
{
    return QSize(QTreeWidget::sizeHint().width(), 30 * topLevelItemCount());
}

QTreeWidgetItem *WelcomeModeTreeWidget::addItem(const QString &label, const QString &data)
{
    QTreeWidgetItem *item = new QTreeWidgetItem(this);
    item->setIcon(0, m_bullet);
    item->setSizeHint(0, QSize(24, 30));
    QWidget *lbl = new QLabel(label);
    lbl->setCursor(QCursor(Qt::PointingHandCursor));
    lbl->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    QBoxLayout *lay = new QVBoxLayout;
    lay->setContentsMargins(3, 2, 0, 0);
    lay->addWidget(lbl);
    QWidget *wdg = new QWidget;
    wdg->setLayout(lay);
    setItemWidget(item, 1, wdg);
    item->setData(0, Qt::UserRole, data);
    return item;
}

358
359
360
361
362
void WelcomeModeTreeWidget::slotAddItem(const QString &label, const QString &data)
{
    addTopLevelItem(addItem(label,data));
}

363
364
365
366
367
void WelcomeModeTreeWidget::slotItemClicked(QTreeWidgetItem *item)
{
    emit activated(item->data(0, Qt::UserRole).toString());
}

con's avatar
con committed
368
369
} // namespace Internal
} // namespace Core