welcomemode.cpp 14.8 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/QDateTime>
42
#include <QtCore/QDir>
43
#include <QtCore/QFileInfo>
con's avatar
con committed
44
45
#include <QtCore/QDebug>
#include <QtCore/QUrl>
46
#include <QtCore/QSettings>
con's avatar
con committed
47

48
49
#include <cstdlib>

50
51
#include "ui_welcomemode.h"

con's avatar
con committed
52
53
54
namespace Core {
namespace Internal {

hjk's avatar
hjk committed
55
56
struct WelcomeModePrivate
{
con's avatar
con committed
57
58
59
    WelcomeModePrivate();

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

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

99
100
101
102
103
104
105
106
107
108
109
110
111
// Format a title + ruler for projects/session labels
static inline QString devTitleLabel(const QString &text)
{
    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
112
113
114
115
// ---  WelcomeMode
WelcomeMode::WelcomeMode() :
    m_d(new WelcomeModePrivate)
{
116
    m_d->m_widget = new QWidget;
con's avatar
con committed
117
118
119
120
    QVBoxLayout *l = new QVBoxLayout(m_d->m_widget);
    l->setMargin(0);
    l->setSpacing(0);
    l->addWidget(new QToolBar(m_d->m_widget));
121
    m_d->rssFetcher = new RSSFetcher(8, this);
122
123
    m_d->m_welcomePage = new QWidget(m_d->m_widget);
    m_d->ui.setupUi(m_d->m_welcomePage);
124
125
    m_d->ui.projTitleLabel->setText(devTitleLabel(tr("Projects")));
    m_d->ui.titleLabel->setText(devTitleLabel(tr("Sessions")));
126
127
    m_d->ui.sessTreeWidget->viewport()->setAutoFillBackground(false);
    m_d->ui.projTreeWidget->viewport()->setAutoFillBackground(false);
128
129
130
    m_d->ui.newsTreeWidget->viewport()->setAutoFillBackground(false);
    m_d->ui.sitesTreeWidget->viewport()->setAutoFillBackground(false);
    m_d->ui.tutorialTreeWidget->viewport()->setAutoFillBackground(false);
131
    m_d->ui.didYouKnowTextBrowser->viewport()->setAutoFillBackground(false);
132
133
134
135
    l->addWidget(m_d->m_welcomePage);

    updateWelcomePage(WelcomePageData());

136
137
138
139
    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);
140

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

143
144
145
146
    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)));
147
148
    connect(m_d->ui.newsTreeWidget, SIGNAL(activated(QString)), SLOT(slotUrlClicked(QString)));
    connect(m_d->ui.sitesTreeWidget, SIGNAL(activated(QString)), SLOT(slotUrlClicked(QString)));
149
    connect(m_d->ui.tutorialTreeWidget, SIGNAL(activated(QString)), SIGNAL(openHelpPage(const QString&)));
150

151
152
    connect(m_d->rssFetcher, SIGNAL(newsItemReady(QString, QString)),
        m_d->ui.newsTreeWidget, SLOT(slotAddItem(QString, QString)));
153
154

    //: Add localized feed here only if one exists
155
    m_d->rssFetcher->fetch(QUrl(tr("http://labs.trolltech.com/blogs/feed")));
156
157
158
159
160
161
162

    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"));

163
    m_d->ui.tutorialTreeWidget->addItem(tr("Qt Creator - A quick tour"),
164
                                        QString("qthelp://com.nokia.qtcreator.%1%2/doc/index.html").arg(IDE_VERSION_MAJOR).arg(IDE_VERSION_MINOR));
165
    m_d->ui.tutorialTreeWidget->addItem(tr("Understanding widgets"),
166
167
168
169
170
171
172
173
                                        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"));

174
175
176
177
178
179
180
181
182
183
184
185
    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()));

186
187
    QSettings *settings = ICore::instance()->settings();
    int id = settings->value("General/WelcomeTab", 0).toInt();
188
189
    m_d->btnGrp->button(id)->setChecked(true);
    m_d->ui.stackedWidget->setCurrentIndex(id);
con's avatar
con committed
190
191
192
193
}

WelcomeMode::~WelcomeMode()
{
194
195
    QSettings *settings = ICore::instance()->settings();
    settings->setValue("General/WelcomeTab", m_d->btnGrp->checkedId());
con's avatar
con committed
196
197
198
199
200
    delete m_d;
}

QString WelcomeMode::name() const
{
201
    return tr("Welcome");
con's avatar
con committed
202
203
204
205
}

QIcon WelcomeMode::icon() const
{
206
    return QIcon(QLatin1String(":/core/images/qtcreator_logo_32.png"));
con's avatar
con committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
}

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>()
227
                                 << UniqueIDManager::instance()->uniqueIdentifier(Constants::C_WELCOME_MODE);
con's avatar
con committed
228
229
230
231
232
233
234
235
236
237
    return contexts;
}

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

238
    m_d->m_widget->setUpdatesEnabled(false);
con's avatar
con committed
239
    if (!welcomePageData.previousSession.isEmpty() || !welcomePageData.projectList.isEmpty()) {
240
241
        m_d->ui.sessTreeWidget->clear();
        m_d->ui.projTreeWidget->clear();
con's avatar
con committed
242
243

        if (welcomePageData.sessionList.count() > 1) {
244
            foreach (const QString &s, welcomePageData.sessionList) {
245
                QString str = s;
con's avatar
con committed
246
                if (s == welcomePageData.previousSession)
247
248
                    str = tr("%1 (last session)").arg(s);
                m_d->ui.sessTreeWidget->addItem(str, s);
con's avatar
con committed
249
            }
250
251
252
253
            m_d->ui.sessTreeWidget->updateGeometry();
            m_d->ui.sessTreeWidget->show();
        } else {
            m_d->ui.sessTreeWidget->hide();
con's avatar
con committed
254
        }
255
256

        typedef QPair<QString, QString> QStringPair;
257
        foreach (const QStringPair &it, welcomePageData.projectList) {
258
259
260
            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
261
        }
262
        m_d->ui.projTreeWidget->updateGeometry();
con's avatar
con committed
263

264
265
266
267
268
269
270
        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
271
272
}

273
void WelcomeMode::activateEditMode()
con's avatar
con committed
274
{
275
    Core::ModeManager *modeManager = ModeManager::instance();
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
    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();
}

292
293
294
295
296
void WelcomeMode::slotUrlClicked(const QString &data)
{
    QDesktopServices::openUrl(QUrl(data));
}

297
298
299
300
301
302
303
304
305
306
307
308
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")));
}

309
310
311
312
313
314
315
316
317
318
319
320
321
void WelcomeMode::slotNextTip()
{
    QStringList tips = tipsOfTheDay();
    m_d->ui.didYouKnowTextBrowser->setText(tips.at((++m_d->currentTip)%tips.count()));
}

void WelcomeMode::slotPrevTip()
{
    QStringList tips = tipsOfTheDay();
    m_d->ui.didYouKnowTextBrowser->setText(tips.at((--m_d->currentTip)%tips.count()));
}

QStringList WelcomeMode::tipsOfTheDay()
322
323
324
325
326
327
328
329
330
{
    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."));                       
331
        tips.append(tr("If you add <a href=\"qthelp://com.nokia.qtcreator/doc/creator-external-library-handling.html\""
332
333
334
                       ">external libraries</a>, Qt Creator will automatically offer syntax highlighting "
                        "and code completion."));
    }
335
    return tips;
336
337
}

338
339
340
341
// ---  WelcomeModeButton

WelcomeModeButton::WelcomeModeButton(QWidget *parent) :
        QLabel(parent),
342
343
        m_isPressed(false),
        m_isInited(false)
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
{
    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
360
361
362
    }
}

363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
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);
}

385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// ---  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;
}

418
419
420
421
422
void WelcomeModeTreeWidget::slotAddItem(const QString &label, const QString &data)
{
    addTopLevelItem(addItem(label,data));
}

423
424
425
426
427
void WelcomeModeTreeWidget::slotItemClicked(QTreeWidgetItem *item)
{
    emit activated(item->data(0, Qt::UserRole).toString());
}

con's avatar
con committed
428
429
} // namespace Internal
} // namespace Core