Welcome: Reimplement session and project shortcuts

Got disabled in 16944277d2 during
refactoring.

Delegates and models are not the right places for registering commands,
since there can be multiple instances. Do registration in project
welcome page, and let delegates retrieve shortcut string from the
models.

Task-number: QTCREATORBUG-17881
Change-Id: I06e3c29e57ee893865bf108a2f13bdb385c26d70
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Eike Ziller
2017-03-21 09:47:55 +01:00
parent 551efd9199
commit 7da34ce33a
4 changed files with 71 additions and 47 deletions

View File

@@ -56,6 +56,7 @@ using namespace Core;
using namespace Utils; using namespace Utils;
const int LINK_HEIGHT = 35; const int LINK_HEIGHT = 35;
const char PROJECT_BASE_ID[] = "Welcome.OpenRecentProject";
namespace ProjectExplorer { namespace ProjectExplorer {
namespace Internal { namespace Internal {
@@ -84,6 +85,12 @@ QVariant ProjectModel::data(const QModelIndex &index, int role) const
return data.first; return data.first;
case PrettyFilePathRole: case PrettyFilePathRole:
return Utils::withTildeHomePath(data.first); return Utils::withTildeHomePath(data.first);
case ShortcutRole: {
const Id projectBase = PROJECT_BASE_ID;
if (Command *cmd = ActionManager::command(projectBase.withSuffix(index.row() + 1)))
return cmd->keySequence().toString(QKeySequence::NativeText);
return QVariant();
}
default: default:
return QVariant(); return QVariant();
} }
@@ -108,6 +115,26 @@ void ProjectModel::resetProjects()
/////////////////// ///////////////////
ProjectWelcomePage::ProjectWelcomePage()
{
const int actionsCount = 9;
Context welcomeContext(Core::Constants::C_WELCOME_MODE);
const Id projectBase = PROJECT_BASE_ID;
const Id sessionBase = SESSION_BASE_ID;
for (int i = 1; i <= actionsCount; ++i) {
auto act = new QAction(tr("Open Session #%1").arg(i), this);
Command *cmd = ActionManager::registerAction(act, sessionBase.withSuffix(i), welcomeContext);
cmd->setDefaultKeySequence(QKeySequence((UseMacShortcuts ? tr("Ctrl+Meta+%1") : tr("Ctrl+Alt+%1")).arg(i)));
connect(act, &QAction::triggered, this, [this, i] { openSessionAt(i - 1); });
act = new QAction(tr("Open Recent Project #%1").arg(i), this);
cmd = ActionManager::registerAction(act, projectBase.withSuffix(i), welcomeContext);
cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+%1").arg(i)));
connect(act, &QAction::triggered, this, [this, i] { openProjectAt(i - 1); });
}
}
Core::Id ProjectWelcomePage::id() const Core::Id ProjectWelcomePage::id() const
{ {
return "Develop"; return "Develop";
@@ -128,7 +155,21 @@ void ProjectWelcomePage::newProject()
void ProjectWelcomePage::openProject() void ProjectWelcomePage::openProject()
{ {
ProjectExplorerPlugin::openOpenProjectDialog(); ProjectExplorerPlugin::openOpenProjectDialog();
}
void ProjectWelcomePage::openSessionAt(int index)
{
QTC_ASSERT(m_sessionModel, return);
m_sessionModel->switchToSession(m_sessionModel->sessionAt(index));
}
void ProjectWelcomePage::openProjectAt(int index)
{
QTC_ASSERT(m_projectModel, return);
const QString projectFile = m_projectModel->data(m_projectModel->index(index, 0),
ProjectModel::FilePathRole).toString();
ProjectExplorerPlugin::openProjectWelcomePage(projectFile);
} }
/////////////////// ///////////////////
@@ -160,6 +201,7 @@ protected:
{ {
return itemRect; return itemRect;
} }
virtual int shortcutRole() const = 0;
bool helpEvent(QHelpEvent *ev, QAbstractItemView *view, bool helpEvent(QHelpEvent *ev, QAbstractItemView *view,
const QStyleOptionViewItem &option, const QModelIndex &idx) final const QStyleOptionViewItem &option, const QModelIndex &idx) final
@@ -169,9 +211,7 @@ protected:
return false; return false;
} }
QString shortcut; QString shortcut = idx.data(shortcutRole()).toString();
if (idx.row() < m_shortcuts.size())
shortcut = m_shortcuts.at(idx.row());
QString name = idx.data(Qt::DisplayRole).toString(); QString name = idx.data(Qt::DisplayRole).toString();
QString tooltipText; QString tooltipText;
@@ -187,8 +227,6 @@ protected:
QToolTip::showText(ev->globalPos(), tooltipText, view); QToolTip::showText(ev->globalPos(), tooltipText, view);
return true; return true;
} }
QStringList m_shortcuts;
}; };
class SessionDelegate : public BaseDelegate class SessionDelegate : public BaseDelegate
@@ -202,27 +240,9 @@ protected:
const bool expanded = m_expandedSessions.contains(idx.data(Qt::DisplayRole).toString()); const bool expanded = m_expandedSessions.contains(idx.data(Qt::DisplayRole).toString());
return expanded ? itemRect.adjusted(0, 0, 0, -LINK_HEIGHT) : itemRect; return expanded ? itemRect.adjusted(0, 0, 0, -LINK_HEIGHT) : itemRect;
} }
int shortcutRole() const override { return SessionModel::ShortcutRole; }
public: public:
SessionDelegate() {
const int actionsCount = 9;
Context welcomeContext(Core::Constants::C_WELCOME_MODE);
const Id sessionBase = "Welcome.OpenSession";
for (int i = 1; i <= actionsCount; ++i) {
auto act = new QAction(tr("Open Session #%1").arg(i), this);
Command *cmd = ActionManager::registerAction(act, sessionBase.withSuffix(i), welcomeContext);
cmd->setDefaultKeySequence(QKeySequence((UseMacShortcuts ? tr("Ctrl+Meta+%1") : tr("Ctrl+Alt+%1")).arg(i)));
m_shortcuts.append(cmd->keySequence().toString(QKeySequence::NativeText));
// connect(act, &QAction::triggered, this, [this, i] { openSessionTriggered(i-1); });
connect(cmd, &Command::keySequenceChanged, this, [this, i, cmd] {
m_shortcuts[i-1] = cmd->keySequence().toString(QKeySequence::NativeText);
// emit sessionsShortcutsChanged(m_sessionShortcuts);
});
}
}
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &idx) const final void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &idx) const final
{ {
static const QPixmap sessionIcon = pixmap("session", Theme::Welcome_ForegroundSecondaryColor); static const QPixmap sessionIcon = pixmap("session", Theme::Welcome_ForegroundSecondaryColor);
@@ -393,27 +413,9 @@ private:
class ProjectDelegate : public BaseDelegate class ProjectDelegate : public BaseDelegate
{ {
QString entryType() override { return tr("project", "Appears in \"Open project <name>\""); } QString entryType() override { return tr("project", "Appears in \"Open project <name>\""); }
int shortcutRole() const override { return ProjectModel::ShortcutRole; }
public: public:
ProjectDelegate()
{
const int actionsCount = 9;
Context welcomeContext(Core::Constants::C_WELCOME_MODE);
const Id projectBase = "Welcome.OpenRecentProject";
for (int i = 1; i <= actionsCount; ++i) {
auto act = new QAction(tr("Open Recent Project #%1").arg(i), this);
Command *cmd = ActionManager::registerAction(act, projectBase.withSuffix(i), welcomeContext);
cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+%1").arg(i)));
m_shortcuts.append(cmd->keySequence().toString(QKeySequence::NativeText));
// connect(act, &QAction::triggered, this, [this, i] { openRecentProjectTriggered(i-1); });
connect(cmd, &Command::keySequenceChanged, this, [this, i, cmd] {
m_shortcuts[i - 1] = cmd->keySequence().toString(QKeySequence::NativeText);
});
}
}
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &idx) const final void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &idx) const final
{ {
QRect rc = option.rect; QRect rc = option.rect;

View File

@@ -40,7 +40,7 @@ class ProjectModel : public QAbstractListModel
Q_OBJECT Q_OBJECT
public: public:
enum { FilePathRole = Qt::UserRole+1, PrettyFilePathRole }; enum { FilePathRole = Qt::UserRole+1, PrettyFilePathRole, ShortcutRole };
ProjectModel(QObject *parent = nullptr); ProjectModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent) const override; int rowCount(const QModelIndex &parent) const override;
@@ -55,7 +55,7 @@ class ProjectWelcomePage : public Core::IWelcomePage
{ {
Q_OBJECT Q_OBJECT
public: public:
ProjectWelcomePage() = default; ProjectWelcomePage();
QString title() const override { return tr("Projects"); } QString title() const override { return tr("Projects"); }
int priority() const override { return 20; } int priority() const override { return 20; }
@@ -73,6 +73,9 @@ signals:
void manageSessions(); void manageSessions();
private: private:
void openSessionAt(int index);
void openProjectAt(int index);
friend class SessionsPage; friend class SessionsPage;
SessionModel *m_sessionModel = nullptr; SessionModel *m_sessionModel = nullptr;
ProjectModel *m_projectModel = nullptr; ProjectModel *m_projectModel = nullptr;

View File

@@ -28,6 +28,9 @@
#include "sessiondialog.h" #include "sessiondialog.h"
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/id.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <utils/stringutils.h> #include <utils/stringutils.h>
@@ -35,6 +38,8 @@
#include <QFileInfo> #include <QFileInfo>
#include <QDir> #include <QDir>
using namespace Core;
namespace ProjectExplorer { namespace ProjectExplorer {
namespace Internal { namespace Internal {
@@ -146,6 +151,11 @@ QVariant SessionModel::data(const QModelIndex &index, int role) const
case ProjectsDisplayRole: case ProjectsDisplayRole:
result = pathsToBaseNames(SessionManager::projectsForSessionName(sessionName)); result = pathsToBaseNames(SessionManager::projectsForSessionName(sessionName));
break; break;
case ShortcutRole: {
const Id sessionBase = SESSION_BASE_ID;
if (Command *cmd = ActionManager::command(sessionBase.withSuffix(index.row() + 1)))
result = cmd->keySequence().toString(QKeySequence::NativeText);
} break;
} // switch (role) } // switch (role)
} }

View File

@@ -32,12 +32,21 @@
namespace ProjectExplorer { namespace ProjectExplorer {
namespace Internal { namespace Internal {
const char SESSION_BASE_ID[] = "Welcome.OpenSession";
class SessionModel : public QAbstractTableModel class SessionModel : public QAbstractTableModel
{ {
Q_OBJECT Q_OBJECT
public: public:
enum { DefaultSessionRole = Qt::UserRole+1, LastSessionRole, ActiveSessionRole, ProjectsPathRole, ProjectsDisplayRole }; enum {
DefaultSessionRole = Qt::UserRole+1,
LastSessionRole,
ActiveSessionRole,
ProjectsPathRole,
ProjectsDisplayRole,
ShortcutRole
};
explicit SessionModel(QObject *parent = nullptr); explicit SessionModel(QObject *parent = nullptr);