Skip to content

Commit

Permalink
CustomShortcutModel code cleanups
Browse files Browse the repository at this point in the history
Use QVector instead of QList
Remove some unused code
  • Loading branch information
callaa committed May 3, 2019
1 parent 7d81c59 commit 0418a82
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 33 deletions.
33 changes: 11 additions & 22 deletions src/client/utils/customshortcutmodel.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Drawpile - a collaborative drawing program.
Copyright (C) 2015-2018 Calle Laakkonen
Copyright (C) 2015-2019 Calle Laakkonen
Drawpile is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -98,7 +98,7 @@ QVariant CustomShortcutModel::headerData(int section, Qt::Orientation orientatio

void CustomShortcutModel::loadShortcuts()
{
QList<CustomShortcut> actions;
QVector<CustomShortcut> actions;
actions.reserve(m_customizableActions.size());

QSettings cfg;
Expand All @@ -114,9 +114,7 @@ void CustomShortcutModel::loadShortcuts()
actions.append(a);
}

std::sort(actions.begin(), actions.end(),
[](const CustomShortcut &a1, const CustomShortcut &a2) { return a1.title.compare(a2.title) < 0; }
);
std::sort(actions.begin(), actions.end());

beginResetModel();
m_shortcuts = actions;
Expand All @@ -128,7 +126,7 @@ void CustomShortcutModel::saveShortcuts()
QSettings cfg;

cfg.beginGroup("settings/shortcuts");
cfg.remove("");
cfg.remove(QString());

for(const CustomShortcut &cs : m_shortcuts) {
if(cs.currentShortcut != cs.defaultShortcut)
Expand All @@ -141,26 +139,17 @@ void CustomShortcutModel::registerCustomizableAction(const QString &name, const
if(m_customizableActions.contains(name))
return;

m_customizableActions[name] = CustomShortcut(name, title, defaultShortcut);
}

bool CustomShortcutModel::hasDefaultShortcut(const QString &name)
{
return m_customizableActions.contains(name);
m_customizableActions[name] = CustomShortcut {
name,
title,
defaultShortcut,
QKeySequence()
};
}

QKeySequence CustomShortcutModel::getDefaultShortcut(const QString &name)
{
return m_customizableActions[name].defaultShortcut;
}

QKeySequence CustomShortcutModel::getShorcut(const QString &name)
{
QSettings cfg;
cfg.beginGroup("settings/shortcuts");
if(cfg.contains(name))
return cfg.value(name).value<QKeySequence>();
else if(m_customizableActions.contains(name))
if(m_customizableActions.contains(name))
return m_customizableActions[name].defaultShortcut;
else
return QKeySequence();
Expand Down
14 changes: 4 additions & 10 deletions src/client/utils/customshortcutmodel.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Drawpile - a collaborative drawing program.
Copyright (C) 2015-2018 Calle Laakkonen
Copyright (C) 2015-2019 Calle Laakkonen
Drawpile is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -30,10 +30,7 @@ struct CustomShortcut {
QKeySequence defaultShortcut;
QKeySequence currentShortcut;

CustomShortcut() { }
CustomShortcut(const QString &n, const QString &t, const QKeySequence &s)
: name(n), title(t), defaultShortcut(s), currentShortcut(QKeySequence())
{ }
bool operator<(const CustomShortcut &other) const { return title.compare(other.title) < 0; }
};

class CustomShortcutModel : public QAbstractTableModel
Expand All @@ -52,14 +49,11 @@ class CustomShortcutModel : public QAbstractTableModel
void loadShortcuts();
void saveShortcuts();

static bool hasDefaultShortcut(const QString &name);
static QKeySequence getDefaultShortcut(const QString &name);
static QKeySequence getShorcut(const QString &name);

static void registerCustomizableAction(const QString &name, const QString &title, const QKeySequence &defaultShortcut);
private:

QList<CustomShortcut> m_shortcuts;
private:
QVector<CustomShortcut> m_shortcuts;

static QMap<QString, CustomShortcut> m_customizableActions;
};
Expand Down
2 changes: 1 addition & 1 deletion src/desktop/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ void MainWindow::loadShortcuts()
if(!name.isEmpty()) {
if(cfg.contains(name))
a->setShortcut(cfg.value(name).value<QKeySequence>());
else if(CustomShortcutModel::hasDefaultShortcut(name))
else
a->setShortcut(CustomShortcutModel::getDefaultShortcut(name));

// If an action has a shortcut, show it in the tooltip
Expand Down

0 comments on commit 0418a82

Please sign in to comment.