OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / projectexplorer / projectwelcomepagewidget.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "projectwelcomepagewidget.h"
35 #include "projectexplorer.h"
36 #include "ui_projectwelcomepagewidget.h"
37
38 #include <coreplugin/coreconstants.h>
39 #include <coreplugin/uniqueidmanager.h>
40 #include <coreplugin/modemanager.h>
41 #include <coreplugin/icore.h>
42 #include <coreplugin/dialogs/iwizard.h>
43 #include <coreplugin/filemanager.h>
44
45 #include <utils/stringutils.h>
46
47 #include <QtCore/QFileInfo>
48 #include <QtCore/QDir>
49 #include <QtCore/QPair>
50 #include <QtGui/QLabel>
51
52 #include <QtCore/QDebug>
53
54 #define MAX_RECENT_PROJECT_ITEMS 6
55
56 #ifdef Q_OS_MAC
57 #  define MAX_RECENT_SESSION_ITEMS 8
58 #else
59 #  define MAX_RECENT_SESSION_ITEMS 9
60 #endif
61
62 using namespace ProjectExplorer::Internal;
63
64 bool ProjectWelcomePageWidget::WelcomePageData::operator==(const WelcomePageData &rhs) const
65 {
66     return previousSession == rhs.previousSession
67         && activeSession   == rhs.activeSession
68         && sessionList     == rhs.sessionList
69         && projectList     == rhs.projectList;
70 }
71
72 bool ProjectWelcomePageWidget::WelcomePageData::operator!=(const WelcomePageData &rhs) const
73 {
74     return previousSession != rhs.previousSession
75         || activeSession   != rhs.activeSession
76         || sessionList     != rhs.sessionList
77         || projectList     != rhs.projectList;
78 }
79
80 QDebug operator<<(QDebug dgb, const ProjectWelcomePageWidget::WelcomePageData &d)
81 {
82     dgb.nospace() << "PreviousSession=" << d.previousSession
83         << " activeSession=" << d.activeSession
84         << " sessionList=" << d.sessionList
85         << " projectList=" << d.projectList;
86     return dgb;
87 }
88
89 ProjectWelcomePageWidget::ProjectWelcomePageWidget(QWidget *parent) :
90     QWidget(parent),
91     ui(new Ui::ProjectWelcomePageWidget)
92 {
93     ui->setupUi(this);
94     updateWelcomePage(WelcomePageData());
95
96     connect(ui->sessTreeWidget, SIGNAL(activated(QString)), SLOT(slotSessionClicked(QString)));
97     connect(ui->projTreeWidget, SIGNAL(activated(QString)), SLOT(slotProjectClicked(QString)));
98     connect(ui->createNewProjectButton, SIGNAL(clicked()), SLOT(slotCreateNewProject()));
99     connect(ui->openProjectButton, SIGNAL(clicked()),
100             ProjectExplorer::ProjectExplorerPlugin::instance(),
101             SLOT(openOpenProjectDialog()));
102     connect(ui->manageSessionsButton, SIGNAL(clicked()), SIGNAL(manageSessions()));
103
104     ui->createNewProjectButton->setIcon(
105             QIcon::fromTheme(QLatin1String("document-new"), ui->createNewProjectButton->icon()));
106     ui->openProjectButton->setIcon(
107             QIcon::fromTheme(QLatin1String("document-open"), ui->openProjectButton->icon()));
108 }
109
110 ProjectWelcomePageWidget::~ProjectWelcomePageWidget()
111 {
112     delete ui;
113 }
114
115 void ProjectWelcomePageWidget::updateWelcomePage(const WelcomePageData &welcomePageData)
116 {
117     // Update only if data are modified
118     if (welcomePageData == lastData)
119         return;
120     lastData = welcomePageData;
121
122     setUpdatesEnabled(false);
123     ui->sessTreeWidget->clear();
124     ui->projTreeWidget->clear();
125
126     if (welcomePageData.sessionList.count() > 0) {
127         int items = 0;
128         foreach (const QString &s, welcomePageData.sessionList) {
129             if (++items > MAX_RECENT_SESSION_ITEMS)
130                 break;
131             QString str = s;
132             if (welcomePageData.activeSession.isEmpty()) {
133                 if (s == welcomePageData.previousSession)
134                     str = tr("%1 (last session)").arg(s);
135             } else {
136                 if (s == welcomePageData.activeSession)
137                     str = tr("%1 (current session)").arg(s);
138             }
139             ui->sessTreeWidget->addItem(str, s);
140         }
141         ui->sessTreeWidget->updateGeometry();
142     }
143
144     typedef QPair<QString, QString> QStringPair;
145     if (welcomePageData.projectList.count() > 0) {
146         int items = 0;
147         QFontMetrics fm = fontMetrics();
148         foreach (const QStringPair &it, welcomePageData.projectList) {
149             if (++items > MAX_RECENT_PROJECT_ITEMS)
150                 break;
151             const QFileInfo fi(it.first);
152             QString label = "<b>" + it.second +
153                     "</b><br><font color=gray>" +
154                     fm.elidedText(QDir::toNativeSeparators(Utils::withTildeHomePath(it.first)),
155                                   Qt::ElideMiddle, 250);
156             ui->projTreeWidget->addItem(label, it.first,
157                                         QDir::toNativeSeparators(fi.absolutePath()));
158         }
159         ui->projTreeWidget->updateGeometry();
160     }
161     setUpdatesEnabled(true);
162 }
163
164 void ProjectWelcomePageWidget::activateEditMode()
165 {
166     Core::ModeManager *modeManager = Core::ModeManager::instance();
167     if (modeManager->currentMode() == modeManager->mode(Core::Constants::MODE_WELCOME))
168         modeManager->activateMode(Core::Constants::MODE_EDIT);
169 }
170
171 void ProjectWelcomePageWidget::slotSessionClicked(const QString &data)
172 {
173     emit requestSession(data);
174     activateEditMode();
175 }
176
177 void ProjectWelcomePageWidget::slotProjectClicked(const QString &data)
178 {
179     emit requestProject(data);
180     activateEditMode();
181 }
182
183 void ProjectWelcomePageWidget::slotCreateNewProject()
184 {
185     Core::ICore::instance()->showNewItemDialog(tr("New Project"),
186                                                Core::IWizard::wizardsOfKind(Core::IWizard::ProjectWizard));
187 }