Sessions: introduce a sessionDateTime

This can show the last modified time of a session and
will be used in a next commit to make it more clear
when session was saved.

Change-Id: I9f73f62652efc9287563f833f25c49f79c39d936
Task-number: QTCREATORBUG-15790
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Tim Jenssen
2016-09-23 19:14:50 +02:00
parent b8f198a985
commit 29f6f4f253
5 changed files with 60 additions and 10 deletions

View File

@@ -99,6 +99,7 @@ public:
bool m_casadeSetActive = false;
mutable QStringList m_sessions;
mutable QHash<QString, QDateTime> m_sessionDateTimes;
mutable QHash<Project *, QStringList> m_projectFileCache;
@@ -466,7 +467,9 @@ bool SessionManager::save()
data.insert(QLatin1String("valueKeys"), keys);
bool result = d->m_writer->save(data, ICore::mainWindow());
if (!result) {
if (result) {
d->m_sessionDateTimes.insert(activeSession(), QDateTime::currentDateTime());
} else {
QMessageBox::warning(ICore::dialogParent(), tr("Error while saving session"),
tr("Could not save session to file %1").arg(d->m_writer->fileName().toUserOutput()));
}
@@ -750,14 +753,21 @@ QStringList SessionManager::sessions()
QDir sessionDir(ICore::userResourcePath());
QList<QFileInfo> sessionFiles = sessionDir.entryInfoList(QStringList() << QLatin1String("*.qws"), QDir::NoFilter, QDir::Time);
foreach (const QFileInfo &fileInfo, sessionFiles) {
if (fileInfo.completeBaseName() != QLatin1String("default"))
d->m_sessions << fileInfo.completeBaseName();
const QString &name = fileInfo.completeBaseName();
d->m_sessionDateTimes.insert(name, fileInfo.lastModified());
if (name != QLatin1String("default"))
d->m_sessions << name;
}
d->m_sessions.prepend(QLatin1String("default"));
}
return d->m_sessions;
}
QDateTime SessionManager::sessionDateTime(const QString &session)
{
return d->m_sessionDateTimes.value(session);
}
FileName SessionManager::sessionNameToFileName(const QString &session)
{
return FileName::fromString(ICore::userResourcePath() + QLatin1Char('/') + session + QLatin1String(".qws"));
@@ -820,6 +830,7 @@ bool SessionManager::cloneSession(const QString &original, const QString &clone)
// If the file does not exist, we can still clone
if (!fi.exists() || fi.copy(sessionNameToFileName(clone).toString())) {
d->m_sessions.insert(1, clone);
d->m_sessionDateTimes.insert(clone, QFileInfo(sessionNameToFileName(clone).toString()).lastModified());
return true;
}
return false;

View File

@@ -33,6 +33,7 @@
#include <QString>
#include <QStringList>
#include <QDateTime>
namespace Core { class IEditor; }
@@ -61,6 +62,7 @@ public:
static QString activeSession();
static QString lastSession();
static QStringList sessions();
static QDateTime sessionDateTime(const QString &session);
static bool createSession(const QString &session);

View File

@@ -39,7 +39,7 @@ namespace ProjectExplorer {
namespace Internal {
SessionModel::SessionModel(QObject *parent)
: QAbstractListModel(parent)
: QAbstractTableModel(parent)
{
connect(SessionManager::instance(), &SessionManager::sessionLoaded,
this, &SessionModel::resetSessions);
@@ -55,6 +55,36 @@ QString SessionModel::sessionAt(int row)
return SessionManager::sessions().value(row, QString());
}
QVariant SessionModel::headerData(int section, Qt::Orientation orientation, int role) const
{
QVariant result;
if (orientation == Qt::Horizontal) {
switch (role) {
case Qt::DisplayRole:
switch (section) {
case 0: result = tr("Session");
break;
case 1: result = tr("Last Modified");
break;
} // switch (section)
break;
} // switch (role) {
}
return result;
}
int SessionModel::columnCount(const QModelIndex &) const
{
static int sectionCount = 0;
if (sectionCount == 0) {
// headers sections defining possible columns
while (!headerData(sectionCount, Qt::Horizontal, Qt::DisplayRole).isNull())
sectionCount++;
}
return sectionCount;
}
int SessionModel::rowCount(const QModelIndex &) const
{
return SessionManager::sessions().count();
@@ -85,6 +115,8 @@ QVariant SessionModel::data(const QModelIndex &index, int role) const
switch (index.column()) {
case 0: result = sessionName;
break;
case 1: result = SessionManager::sessionDateTime(sessionName);
break;
} // switch (section)
break;
case DefaultSessionRole:
@@ -118,7 +150,7 @@ QHash<int, QByteArray> SessionModel::roleNames() const
{ProjectsPathRole, "projectsPath"},
{ProjectsDisplayRole, "projectsName"}
};
return QAbstractListModel::roleNames().unite(extraRoles);
return QAbstractTableModel::roleNames().unite(extraRoles);
}
bool SessionModel::isDefaultVirgin() const
@@ -186,5 +218,6 @@ void SessionModel::runNewSessionDialog(const QString &suggestedName, std::functi
switchToSession(newSession);
}
}
} // namespace Internal
} // namespace ProjectExplorer

View File

@@ -25,14 +25,14 @@
#pragma once
#include <QAbstractListModel>
#include <QAbstractTableModel>
#include <functional>
namespace ProjectExplorer {
namespace Internal {
class SessionModel : public QAbstractListModel
class SessionModel : public QAbstractTableModel
{
Q_OBJECT
@@ -45,6 +45,9 @@ public:
QString sessionAt(int row);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
QVariant data(const QModelIndex &index, int role) const override;
QHash<int, QByteArray> roleNames() const override;

View File

@@ -63,9 +63,10 @@ SessionView::SessionView(QWidget *parent)
setModel(&m_sessionModel);
QItemSelection firstRow(m_sessionModel.index(0,0), m_sessionModel.index(0, 0));
selectionModel()->select(firstRow,
QItemSelectionModel::QItemSelectionModel::SelectCurrent);
QItemSelection firstRow(m_sessionModel.index(0,0), m_sessionModel.index(
0, m_sessionModel.columnCount() - 1));
selectionModel()->select(firstRow, QItemSelectionModel::QItemSelectionModel::
SelectCurrent);
connect(this, &QTreeView::activated, [this](const QModelIndex &index){
emit activated(m_sessionModel.sessionAt(index.row()));