welcomemode.cpp 16 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"
32
#include "iwizard.h"
con's avatar
con committed
33
34
35
#include "coreconstants.h"
#include "uniqueidmanager.h"
#include "modemanager.h"
36
#include "newdialog.h"
37
#include "rssfetcher.h"
con's avatar
con committed
38
39
40

#include <QtGui/QToolBar>
#include <QtGui/QDesktopServices>
41
#include <QtGui/QMouseEvent>
con's avatar
con committed
42

43
#include <QtCore/QDateTime>
44
#include <QtCore/QDir>
45
#include <QtCore/QFileInfo>
con's avatar
con committed
46
47
#include <QtCore/QDebug>
#include <QtCore/QUrl>
48
#include <QtCore/QSettings>
con's avatar
con committed
49

50
51
#include <cstdlib>

52
53
#include "ui_welcomemode.h"

con's avatar
con committed
54
55
56
namespace Core {
namespace Internal {

hjk's avatar
hjk committed
57
58
struct WelcomeModePrivate
{
con's avatar
con committed
59
60
61
    WelcomeModePrivate();

    QWidget *m_widget;
62
    QWidget *m_welcomePage;
63
    QButtonGroup *btnGrp;
64
    Ui::WelcomePage ui;
65
    RSSFetcher *rssFetcher;
con's avatar
con committed
66
    WelcomeMode::WelcomePageData lastData;
67
    int currentTip;
con's avatar
con committed
68
69
};

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

// ---  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;
}

101
102
// Format a title + ruler for title labels
static inline QString titleLabel(const QString &text)
103
104
105
106
107
108
109
110
111
112
113
{
    QString  rc = QLatin1String(
    "<html><head><style type=\"text/css\">p, li { white-space: pre-wrap; }</style></head>"
    "<body style=\" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;\">"
    "<p style=\" margin-top:16px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">"
    "<span style=\" font-size:x-large; color:#555555;\">");
    rc += text;
    rc += QLatin1String("</span></p><hr/></body></html>");
    return rc;
}

con's avatar
con committed
114
115
116
117
// ---  WelcomeMode
WelcomeMode::WelcomeMode() :
    m_d(new WelcomeModePrivate)
{
118
    m_d->m_widget = new QWidget;
con's avatar
con committed
119
120
121
122
    QVBoxLayout *l = new QVBoxLayout(m_d->m_widget);
    l->setMargin(0);
    l->setSpacing(0);
    l->addWidget(new QToolBar(m_d->m_widget));
123
    m_d->rssFetcher = new RSSFetcher(7, this);
124
125
    m_d->m_welcomePage = new QWidget(m_d->m_widget);
    m_d->ui.setupUi(m_d->m_welcomePage);
126
127
128
129
130
131
132
    m_d->ui.projTitleLabel->setText(titleLabel(tr("Projects")));
    m_d->ui.recentSessionsTitleLabel->setText(titleLabel(tr("Sessions")));
    m_d->ui.tutorialsTitleLabel->setText(titleLabel(tr("Tutorials")));
    m_d->ui.demoTitleLabel->setText(titleLabel(tr("Qt Demos and Examples")));
    m_d->ui.didYouKnowTitleLabel->setText(titleLabel(tr("Did you know?")));
    m_d->ui.labsTitleLabel->setText(titleLabel(tr("News from the Qt Labs")));
    m_d->ui.sitesTitleLabel->setText(titleLabel(tr("Qt Websites")));
133
134
    m_d->ui.sessTreeWidget->viewport()->setAutoFillBackground(false);
    m_d->ui.projTreeWidget->viewport()->setAutoFillBackground(false);
135
136
137
    m_d->ui.newsTreeWidget->viewport()->setAutoFillBackground(false);
    m_d->ui.sitesTreeWidget->viewport()->setAutoFillBackground(false);
    m_d->ui.tutorialTreeWidget->viewport()->setAutoFillBackground(false);
138
    m_d->ui.didYouKnowTextBrowser->viewport()->setAutoFillBackground(false);
139
140
141
142
    l->addWidget(m_d->m_welcomePage);

    updateWelcomePage(WelcomePageData());

143
144
145
146
    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);
147

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

150
    connect(m_d->ui.feedbackButton, SIGNAL(clicked()), SLOT(slotFeedback()));
151
152
    connect(m_d->ui.manageSessionsButton, SIGNAL(clicked()), SIGNAL(manageSessions()));
    connect(m_d->ui.createNewProjectButton, SIGNAL(clicked()), SLOT(slotCreateNewProject()));
153
154
    connect(m_d->ui.sessTreeWidget, SIGNAL(activated(QString)), SLOT(slotSessionClicked(QString)));
    connect(m_d->ui.projTreeWidget, SIGNAL(activated(QString)), SLOT(slotProjectClicked(QString)));
155
156
    connect(m_d->ui.newsTreeWidget, SIGNAL(activated(QString)), SLOT(slotUrlClicked(QString)));
    connect(m_d->ui.sitesTreeWidget, SIGNAL(activated(QString)), SLOT(slotUrlClicked(QString)));
157
    connect(m_d->ui.tutorialTreeWidget, SIGNAL(activated(QString)), SIGNAL(openHelpPage(const QString&)));
158

159
160
    connect(m_d->rssFetcher, SIGNAL(newsItemReady(QString, QString, QString)),
        m_d->ui.newsTreeWidget, SLOT(slotAddNewsItem(QString, QString, QString)));
161
162

    //: Add localized feed here only if one exists
163
    m_d->rssFetcher->fetch(QUrl(tr("http://labs.trolltech.com/blogs/feed")));
164

165
166
    m_d->ui.sitesTreeWidget->addItem(tr("Qt Software"), QLatin1String("http://www.trolltech.com"));
    m_d->ui.sitesTreeWidget->addItem(tr("Qt Labs"), QLatin1String("http://labs.trolltech.com"));
167
168
    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"));
169
    m_d->ui.sitesTreeWidget->addItem(tr("Qt for S60 at Forum Nokia"), QLatin1String("http://discussion.forum.nokia.com/forum/forumdisplay.php?f=196"));
170

171
    m_d->ui.tutorialTreeWidget->addItem(tr("<b>Qt Creator - A quick tour</b>"),
172
                                        QString("qthelp://com.nokia.qtcreator.%1%2/doc/index.html").arg(IDE_VERSION_MAJOR).arg(IDE_VERSION_MINOR));
173
    m_d->ui.tutorialTreeWidget->addItem(tr("Understanding widgets"),
174
175
176
177
178
179
180
181
                                        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"));

182
183
184
185
186
187
188
189
190
191
192
193
    srand(QDateTime::currentDateTime().toTime_t());
    QStringList tips = tipsOfTheDay();
    m_d->currentTip = rand()%tips.count();

    QTextDocument *doc = m_d->ui.didYouKnowTextBrowser->document();
    doc->setDefaultStyleSheet("a:link {color:black;}");
    m_d->ui.didYouKnowTextBrowser->setDocument(doc);
    m_d->ui.didYouKnowTextBrowser->setText(tips.at(m_d->currentTip));

    connect(m_d->ui.nextTipBtn, SIGNAL(clicked()), this, SLOT(slotNextTip()));
    connect(m_d->ui.prevTipBtn, SIGNAL(clicked()), this, SLOT(slotPrevTip()));

194
195
    QSettings *settings = ICore::instance()->settings();
    int id = settings->value("General/WelcomeTab", 0).toInt();
196
197
    m_d->btnGrp->button(id)->setChecked(true);
    m_d->ui.stackedWidget->setCurrentIndex(id);
con's avatar
con committed
198
199
200
201
}

WelcomeMode::~WelcomeMode()
{
202
203
    QSettings *settings = ICore::instance()->settings();
    settings->setValue("General/WelcomeTab", m_d->btnGrp->checkedId());
con's avatar
con committed
204
205
206
207
208
    delete m_d;
}

QString WelcomeMode::name() const
{
209
    return tr("Welcome");
con's avatar
con committed
210
211
212
213
}

QIcon WelcomeMode::icon() const
{
214
    return QIcon(QLatin1String(":/core/images/qtcreator_logo_32.png"));
con's avatar
con committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
}

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>()
235
                                 << UniqueIDManager::instance()->uniqueIdentifier(Constants::C_WELCOME_MODE);
con's avatar
con committed
236
237
238
239
240
241
242
243
244
245
    return contexts;
}

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

246
    m_d->m_widget->setUpdatesEnabled(false);
con's avatar
con committed
247
    if (!welcomePageData.previousSession.isEmpty() || !welcomePageData.projectList.isEmpty()) {
248
249
        m_d->ui.sessTreeWidget->clear();
        m_d->ui.projTreeWidget->clear();
con's avatar
con committed
250
251

        if (welcomePageData.sessionList.count() > 1) {
252
            foreach (const QString &s, welcomePageData.sessionList) {
253
                QString str = s;
con's avatar
con committed
254
                if (s == welcomePageData.previousSession)
255
256
                    str = tr("%1 (last session)").arg(s);
                m_d->ui.sessTreeWidget->addItem(str, s);
con's avatar
con committed
257
            }
258
259
260
261
            m_d->ui.sessTreeWidget->updateGeometry();
            m_d->ui.sessTreeWidget->show();
        } else {
            m_d->ui.sessTreeWidget->hide();
con's avatar
con committed
262
        }
263
264

        typedef QPair<QString, QString> QStringPair;
265
        foreach (const QStringPair &it, welcomePageData.projectList) {
266
267
268
            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
269
        }
270
        m_d->ui.projTreeWidget->updateGeometry();
con's avatar
con committed
271

272
273
274
275
276
277
278
        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
279
280
}

281
void WelcomeMode::activateEditMode()
con's avatar
con committed
282
{
283
    Core::ModeManager *modeManager = ModeManager::instance();
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
    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();
}

300
301
302
303
304
void WelcomeMode::slotUrlClicked(const QString &data)
{
    QDesktopServices::openUrl(QUrl(data));
}

305
void WelcomeMode::slotFeedback()
306
{
307
308
    QDesktopServices::openUrl(QUrl(QLatin1String(
            "http://www.trolltech.com/forms/feedback-forms/qt-creator-user-feedback/view")));
309
310
}

311
void WelcomeMode::slotCreateNewProject()
312
{
313
314
    Core::ICore::instance()->showNewItemDialog(tr("New Project..."),
                                               Core::IWizard::wizardsOfKind(Core::IWizard::ProjectWizard));
315
316
}

317
318
319
void WelcomeMode::slotNextTip()
{
    QStringList tips = tipsOfTheDay();
320
321
    m_d->currentTip = ((m_d->currentTip+1)%tips.count());
    m_d->ui.didYouKnowTextBrowser->setText(tips.at(m_d->currentTip));
322
323
324
325
326
}

void WelcomeMode::slotPrevTip()
{
    QStringList tips = tipsOfTheDay();
327
    m_d->currentTip = ((m_d->currentTip-1)+tips.count())%tips.count();
328
    m_d->ui.didYouKnowTextBrowser->setText(tips.at(m_d->currentTip));
329
330
331
}

QStringList WelcomeMode::tipsOfTheDay()
332
333
334
335
336
337
338
339
340
{
    static QStringList tips;
    if (tips.isEmpty()) {
        tips.append(tr("You can switch between Qt Creator's modes using <tt>Ctrl+number</tt>:"
                       "<ol><li> - Welcome</li><li> - Edit</li><li>- Debug</li><li>- Projects</li><li>- Help</li>"
                       "<li></li><li>- Output</li></ol>"));
        tips.append(tr("You can show and hide the side bar using <tt>Alt+0<tt>."));
        tips.append(tr("You can fine tune the <tt>Find</tt> function by selecting &quot;Whole Words&quot; "
                       "or &quot;Case Sensitive&quot;. Simply click on the icons on the right end of the line edit."));                       
341
        tips.append(tr("If you add <a href=\"qthelp://com.nokia.qtcreator/doc/creator-external-library-handling.html\""
342
343
344
                       ">external libraries</a>, Qt Creator will automatically offer syntax highlighting "
                        "and code completion."));
    }
345
    return tips;
346
347
}

348
349
350
351
// ---  WelcomeModeButton

WelcomeModeButton::WelcomeModeButton(QWidget *parent) :
        QLabel(parent),
352
353
        m_isPressed(false),
        m_isInited(false)
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
{
    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
370
371
372
    }
}

373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
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);
}

395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// ---  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));
415
416
    QLabel *lbl = new QLabel(label);
    lbl->setTextInteractionFlags(Qt::NoTextInteraction);
417
418
419
420
421
422
423
424
425
426
427
428
    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;
}

429
void WelcomeModeTreeWidget::slotAddNewsItem(const QString &title, const QString &description, const QString &link)
430
{
431
432
433
434
435
436
437
    int itemWidth = width()-header()->sectionSize(0);
    QFont f = font();
    QString elidedText = QFontMetrics(f).elidedText(description, Qt::ElideRight, itemWidth);
    f.setBold(true);
    QString elidedTitle = QFontMetrics(f).elidedText(title, Qt::ElideRight, itemWidth);
    QString data = QString::fromLatin1("<b>%1</b><br />%2").arg(elidedTitle).arg(elidedText);
    addTopLevelItem(addItem(data,link));
438
439
}

440
441
442
443
444
void WelcomeModeTreeWidget::slotItemClicked(QTreeWidgetItem *item)
{
    emit activated(item->data(0, Qt::UserRole).toString());
}

con's avatar
con committed
445
446
} // namespace Internal
} // namespace Core