projectwelcomepage.cpp 8.33 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29 30

#include "projectwelcomepage.h"
31

32 33
#include <QQmlContext>
#include <QQmlEngine>
34 35
#include <QFileInfo>
#include <QDir>
36

37
#include <coreplugin/icore.h>
Tobias Hunger's avatar
Tobias Hunger committed
38
#include <coreplugin/iwizardfactory.h>
39 40
#include <projectexplorer/session.h>
#include <projectexplorer/projectexplorer.h>
41
#include <projectexplorer/sessiondialog.h>
42

Orgad Shaneh's avatar
Orgad Shaneh committed
43 44
#include <utils/fileutils.h>
#include <utils/stringutils.h>
45
#include <utils/algorithm.h>
46

47 48 49
namespace ProjectExplorer {
namespace Internal {

hjk's avatar
hjk committed
50 51
SessionModel::SessionModel(QObject *parent)
    : QAbstractListModel(parent)
52
{
hjk's avatar
hjk committed
53
    connect(SessionManager::instance(), SIGNAL(sessionLoaded(QString)), SLOT(resetSessions()));
54 55 56 57
}

int SessionModel::rowCount(const QModelIndex &) const
{
hjk's avatar
hjk committed
58
    return SessionManager::sessions().count();
59 60
}

61 62
QStringList pathsToBaseNames(const QStringList &paths)
{
63 64 65
    return Utils::transform(paths, [](const QString &path) {
        return QFileInfo(path).completeBaseName();
    });
66 67 68 69 70 71
}



QStringList pathsWithTildeHomePath(const QStringList &paths)
{
72 73 74
    return Utils::transform(paths, [](const QString &path) {
        return Utils::withTildeHomePath(QDir::toNativeSeparators(path));
    });
75 76
}

77 78
QVariant SessionModel::data(const QModelIndex &index, int role) const
{
79
    if (role == Qt::DisplayRole || role == DefaultSessionRole ||
80
            role == LastSessionRole || role == ActiveSessionRole || role == ProjectsPathRole  || role == ProjectsDisplayRole) {
hjk's avatar
hjk committed
81
        QString sessionName = SessionManager::sessions().at(index.row());
82 83 84
        if (role == Qt::DisplayRole)
            return sessionName;
        else if (role == DefaultSessionRole)
hjk's avatar
hjk committed
85
            return SessionManager::isDefaultSession(sessionName);
86
        else if (role == LastSessionRole)
hjk's avatar
hjk committed
87
            return SessionManager::lastSession() == sessionName;
88
        else if (role == ActiveSessionRole)
hjk's avatar
hjk committed
89
            return SessionManager::activeSession() == sessionName;
90
        else if (role == ProjectsPathRole)
hjk's avatar
hjk committed
91
            return pathsWithTildeHomePath(SessionManager::projectsForSessionName(sessionName));
92
        else if (role == ProjectsDisplayRole)
hjk's avatar
hjk committed
93
            return pathsToBaseNames(SessionManager::projectsForSessionName(sessionName));
Friedemann Kleint's avatar
Friedemann Kleint committed
94 95
    }
    return QVariant();
96 97
}

98 99 100 101 102 103 104 105 106 107 108 109
QHash<int, QByteArray> SessionModel::roleNames() const
{
    QHash<int, QByteArray> roleNames;
    roleNames[Qt::DisplayRole] = "sessionName";
    roleNames[DefaultSessionRole] = "defaultSession";
    roleNames[ActiveSessionRole] = "activeSession";
    roleNames[LastSessionRole] = "lastSession";
    roleNames[ProjectsPathRole] = "projectsPath";
    roleNames[ProjectsDisplayRole] = "projectsName";
    return roleNames;
}

110 111
bool SessionModel::isDefaultVirgin() const
{
hjk's avatar
hjk committed
112
    return SessionManager::isDefaultVirgin();
113
}
114 115 116

void SessionModel::resetSessions()
{
117 118
    beginResetModel();
    endResetModel();
119 120
}

121 122
void SessionModel::cloneSession(const QString &session)
{
hjk's avatar
hjk committed
123
    SessionNameInputDialog newSessionInputDialog(SessionManager::sessions(), 0);
124 125 126 127 128
    newSessionInputDialog.setWindowTitle(tr("New session name"));
    newSessionInputDialog.setValue(session + QLatin1String(" (2)"));

    if (newSessionInputDialog.exec() == QDialog::Accepted) {
        QString newSession = newSessionInputDialog.value();
hjk's avatar
hjk committed
129
        if (newSession.isEmpty() || SessionManager::sessions().contains(newSession))
130
            return;
131
        beginResetModel();
hjk's avatar
hjk committed
132
        SessionManager::cloneSession(session, newSession);
133
        endResetModel();
134

135
        if (newSessionInputDialog.isSwitchToRequested())
hjk's avatar
hjk committed
136
            SessionManager::loadSession(newSession);
137 138 139 140 141
    }
}

void SessionModel::deleteSession(const QString &session)
{
hjk's avatar
hjk committed
142
    if (!SessionManager::confirmSessionDelete(session))
143
        return;
144
    beginResetModel();
hjk's avatar
hjk committed
145
    SessionManager::deleteSession(session);
146
    endResetModel();
147 148 149 150
}

void SessionModel::renameSession(const QString &session)
{
hjk's avatar
hjk committed
151
    SessionNameInputDialog newSessionInputDialog(SessionManager::sessions(), 0);
152 153 154 155 156
    newSessionInputDialog.setWindowTitle(tr("New session name"));
    newSessionInputDialog.setValue(session);

    if (newSessionInputDialog.exec() == QDialog::Accepted) {
        QString newSession = newSessionInputDialog.value();
hjk's avatar
hjk committed
157
        if (newSession.isEmpty() || SessionManager::sessions().contains(newSession))
158
            return;
159
        beginResetModel();
hjk's avatar
hjk committed
160
        SessionManager::renameSession(session, newSession);
161
        endResetModel();
162

163
        if (newSessionInputDialog.isSwitchToRequested())
hjk's avatar
hjk committed
164
            SessionManager::loadSession(newSession);
165 166
    }
}
167

168 169
ProjectModel::ProjectModel(QObject *parent)
    : QAbstractListModel(parent)
170
{
171 172
    connect(ProjectExplorerPlugin::instance(), &ProjectExplorerPlugin::recentProjectsChanged,
            this, &ProjectModel::resetProjects);
173 174 175 176
}

int ProjectModel::rowCount(const QModelIndex &) const
{
177
    return ProjectExplorerPlugin::recentProjects().count();
178 179 180 181
}

QVariant ProjectModel::data(const QModelIndex &index, int role) const
{
182
    QPair<QString,QString> data = ProjectExplorerPlugin::recentProjects().at(index.row());
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    switch (role) {
    case Qt::DisplayRole:
        return data.second;
        break;
    case FilePathRole:
        return data.first;
    case PrettyFilePathRole:
        return Utils::withTildeHomePath(data.first);
    default:
        return QVariant();
    }

    return QVariant();
}

198 199 200 201 202 203 204 205 206
QHash<int, QByteArray> ProjectModel::roleNames() const
{
    QHash<int, QByteArray> roleNames;
    roleNames[Qt::DisplayRole] = "displayName";
    roleNames[FilePathRole] = "filePath";
    roleNames[PrettyFilePathRole] = "prettyFilePath";
    return roleNames;
}

207
void ProjectModel::resetProjects()
208
{
209 210
    beginResetModel();
    endResetModel();
211 212
}

213 214
///////////////////

215 216
ProjectWelcomePage::ProjectWelcomePage() :
    m_sessionModel(0), m_projectModel(0)
217
{
218
}
219

220
void ProjectWelcomePage::facilitateQml(QQmlEngine *engine)
221
{
hjk's avatar
hjk committed
222
    m_sessionModel = new SessionModel(this);
223
    m_projectModel = new ProjectModel(this);
224

225
    QQmlContext *ctx = engine->rootContext();
226 227 228
    ctx->setContextProperty(QLatin1String("sessionList"), m_sessionModel);
    ctx->setContextProperty(QLatin1String("projectList"), m_projectModel);
    ctx->setContextProperty(QLatin1String("projectWelcomePage"), this);
229 230
}

231 232 233
QUrl ProjectWelcomePage::pageLocation() const
{
    // normalize paths so QML doesn't freak out if it's wrongly capitalized on Windows
Orgad Shaneh's avatar
Orgad Shaneh committed
234 235
    const QString resourcePath = Utils::FileUtils::normalizePathName(Core::ICore::resourcePath());
    return QUrl::fromLocalFile(resourcePath + QLatin1String("/welcomescreen/develop.qml"));
236 237
}

238 239 240 241 242
ProjectWelcomePage::Id ProjectWelcomePage::id() const
{
    return Develop;
}

243
void ProjectWelcomePage::reloadWelcomeScreenData()
244
{
245 246 247 248
    if (m_sessionModel)
        m_sessionModel->resetSessions();
    if (m_projectModel)
        m_projectModel->resetProjects();
249 250
}

251 252 253
void ProjectWelcomePage::newProject()
{
    Core::ICore::showNewItemDialog(tr("New Project"),
Tobias Hunger's avatar
Tobias Hunger committed
254
                                   Core::IWizardFactory::wizardFactoriesOfKind(Core::IWizardFactory::ProjectWizard));
255 256 257 258
}

void ProjectWelcomePage::openProject()
{
259
     ProjectExplorerPlugin::openOpenProjectDialog();
260 261
}

262 263
} // namespace Internal
} // namespace ProjectExplorer