~ubuntu-branches/ubuntu/vivid/qpdfview/vivid

« back to all changes in this revision

Viewing changes to sources/shortcutstablemodel.cpp

  • Committer: Package Import Robot
  • Author(s): Benjamin Eltzner
  • Date: 2013-04-04 14:51:58 UTC
  • mfrom: (1.2.6)
  • Revision ID: package-import@ubuntu.com-20130404145158-cis99qi9kcaiwvet
Tags: 0.4.1-1
* New upstream release.
* Source package now builds three binary packages.
* Included SyncTeX copyright and license into debian/copyright.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
Copyright 2013 Adam Reichold
 
4
 
 
5
This file is part of qpdfview.
 
6
 
 
7
qpdfview is free software: you can redistribute it and/or modify
 
8
it under the terms of the GNU General Public License as published by
 
9
the Free Software Foundation, either version 2 of the License, or
 
10
(at your option) any later version.
 
11
 
 
12
qpdfview is distributed in the hope that it will be useful,
 
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
GNU General Public License for more details.
 
16
 
 
17
You should have received a copy of the GNU General Public License
 
18
along with qpdfview.  If not, see <http://www.gnu.org/licenses/>.
 
19
 
 
20
*/
 
21
 
 
22
#include "shortcutstablemodel.h"
 
23
 
 
24
#include <QAction>
 
25
#include <QSet>
 
26
 
 
27
ShortcutsTableModel::ShortcutsTableModel(const QList< QAction* >& actions, const QMap< QAction*, QKeySequence >& defaultShortcuts, QObject* parent) : QAbstractTableModel(parent),
 
28
    m_actions(actions),
 
29
    m_defaultShortcuts(defaultShortcuts),
 
30
    m_shortcuts()
 
31
{
 
32
    foreach(QAction* action, m_actions)
 
33
    {
 
34
        m_shortcuts.insert(action, action->shortcut());
 
35
    }
 
36
}
 
37
 
 
38
int ShortcutsTableModel::columnCount(const QModelIndex& parent) const
 
39
{
 
40
    Q_UNUSED(parent);
 
41
 
 
42
    return 2;
 
43
}
 
44
 
 
45
int ShortcutsTableModel::rowCount(const QModelIndex& parent) const
 
46
{
 
47
    Q_UNUSED(parent);
 
48
 
 
49
    return m_actions.count();
 
50
}
 
51
 
 
52
Qt::ItemFlags ShortcutsTableModel::flags(const QModelIndex& index) const
 
53
{
 
54
    switch(index.column())
 
55
    {
 
56
    case 0:
 
57
        return Qt::ItemIsEnabled;
 
58
        break;
 
59
    case 1:
 
60
        return Qt::ItemIsEnabled | Qt::ItemIsEditable;
 
61
        break;
 
62
    }
 
63
 
 
64
    return Qt::NoItemFlags;
 
65
}
 
66
 
 
67
QVariant ShortcutsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
 
68
{
 
69
    Q_UNUSED(orientation);
 
70
 
 
71
    if(role == Qt::DisplayRole)
 
72
    {
 
73
        switch(section)
 
74
        {
 
75
        case 0:
 
76
            return tr("Action");
 
77
            break;
 
78
        case 1:
 
79
            return tr("Key sequence");
 
80
            break;
 
81
        }
 
82
    }
 
83
 
 
84
    return QVariant();
 
85
}
 
86
 
 
87
QVariant ShortcutsTableModel::data(const QModelIndex& index, int role) const
 
88
{
 
89
    if((role == Qt::DisplayRole || role == Qt::EditRole) && index.row() >= 0 && index.row() < m_actions.count())
 
90
    {
 
91
        switch(index.column())
 
92
        {
 
93
        case 0:
 
94
            return m_actions.at(index.row())->text().remove(QLatin1Char('&'));
 
95
            break;
 
96
        case 1:
 
97
            return m_shortcuts.value(m_actions.at(index.row()));
 
98
            break;
 
99
        }
 
100
 
 
101
        return QString::number(index.row()) + ":" + QString::number(index.column());
 
102
    }
 
103
 
 
104
    return QVariant();
 
105
}
 
106
 
 
107
bool ShortcutsTableModel::setData(const QModelIndex& index, const QVariant& value, int role)
 
108
{
 
109
    if(role == Qt::EditRole && index.column() == 1 && index.row() >= 0 && index.row() < m_actions.count())
 
110
    {
 
111
        QKeySequence shortcut(value.toString());
 
112
 
 
113
        if(!shortcut.isEmpty() || value.toString().isEmpty())
 
114
        {
 
115
            m_shortcuts.insert(m_actions.at(index.row()), shortcut);
 
116
 
 
117
            emit dataChanged(index, index);
 
118
 
 
119
            return true;
 
120
        }
 
121
    }
 
122
 
 
123
    return false;
 
124
}
 
125
 
 
126
void ShortcutsTableModel::accept()
 
127
{
 
128
    for(QMap< QAction*, QKeySequence >::iterator iterator = m_shortcuts.begin(); iterator != m_shortcuts.end(); ++iterator)
 
129
    {
 
130
        iterator.key()->setShortcut(iterator.value());
 
131
    }
 
132
}
 
133
 
 
134
void ShortcutsTableModel::reset()
 
135
{
 
136
    m_shortcuts = m_defaultShortcuts;
 
137
 
 
138
    emit dataChanged(createIndex(0, 1), createIndex(m_actions.count(), 1));
 
139
}