comparison widgets/KeyReference.cpp @ 281:ac58acbd7482

* Add key reference window to Help menu (also needs mouse reference)
author Chris Cannam
date Wed, 04 Jul 2007 17:22:12 +0000
parents
children 4edaff85875d
comparison
equal deleted inserted replaced
280:3c402c6052f6 281:ac58acbd7482
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2007 QMUL.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #include "KeyReference.h"
17
18 #include <QAction>
19 #include <QTextEdit>
20
21 KeyReference::KeyReference() :
22 m_text(0)
23 {
24 }
25
26 KeyReference::~KeyReference()
27 {
28 delete m_text;
29 }
30
31 void
32 KeyReference::setCategory(QString category)
33 {
34 if (m_map.find(category) == m_map.end()) {
35 m_categoryOrder.push_back(category);
36 }
37 m_currentCategory = category;
38 m_map[category] = KeyList();
39 }
40
41 void
42 KeyReference::registerShortcut(QAction *action, QString overrideName)
43 {
44 QString name = action->text();
45 if (overrideName != "") name = overrideName;
46
47 QString shortcut = action->shortcut();
48 QString tip = action->statusTip();
49
50 registerShortcut(name, shortcut, tip);
51 }
52
53 void
54 KeyReference::registerShortcut(QString name, QString shortcut, QString tip)
55 {
56 KeyList &list = m_map[m_currentCategory];
57
58 for (KeyList::iterator i = list.begin(); i != list.end(); ++i) {
59 if (i->actionName == name) {
60 i->shortcut = shortcut;
61 i->tip = tip;
62 i->alternatives.clear();
63 return;
64 }
65 }
66
67 KeyDetails details;
68 details.actionName = name;
69 details.shortcut = shortcut;
70 details.tip = tip;
71
72 list.push_back(details);
73 }
74
75 void
76 KeyReference::registerAlternativeShortcut(QAction *action, QString alternative)
77 {
78 QString name = action->text();
79 registerAlternativeShortcut(name, alternative);
80 }
81
82 void
83 KeyReference::registerAlternativeShortcut(QString name, QString alternative)
84 {
85 KeyList &list = m_map[m_currentCategory];
86
87 for (KeyList::iterator i = list.begin(); i != list.end(); ++i) {
88 if (i->actionName == name) {
89 i->alternatives.push_back(alternative);
90 return;
91 }
92 }
93 }
94
95 void
96 KeyReference::show()
97 {
98 if (m_text) {
99 m_text->show();
100 m_text->raise();
101 return;
102 }
103
104 QString text;
105
106 text += "<center><table bgcolor=\"#e8e8e8\">";
107
108 for (CategoryList::iterator i = m_categoryOrder.begin();
109 i != m_categoryOrder.end(); ++i) {
110
111 QString category = *i;
112 KeyList &list = m_map[category];
113
114 text += QString("<tr><td bgcolor=\"#d0d0d0\" colspan=3><br>&nbsp;<b>%1</b><br></td></tr>\n").arg(category);
115
116 for (KeyList::iterator j = list.begin(); j != list.end(); ++j) {
117
118 QString actionName = j->actionName;
119 actionName.replace(tr("&"), "");
120
121 QString tip = j->tip;
122 if (tip != "") tip = QString("<i>%1</i>").arg(tip);
123
124 QString altdesc;
125 if (!j->alternatives.empty()) {
126 for (std::vector<QString>::iterator k = j->alternatives.begin();
127 k != j->alternatives.end(); ++k) {
128 altdesc += tr("<i>or</i>&nbsp;<b>%1</b>").arg(*k);
129 }
130 altdesc = tr("</b>&nbsp;(%1)<b>").arg(altdesc);
131 }
132
133 text += QString("<tr><td>&nbsp;<b>%1%2</b></td><td>&nbsp;%3</td><td>%4</td></tr>\n")
134 .arg(j->shortcut).arg(altdesc).arg(actionName).arg(tip);
135 }
136 }
137
138 text += "</table></center>\n";
139
140 m_text = new QTextEdit;
141 m_text->setHtml(text);
142 m_text->setReadOnly(true);
143 m_text->setObjectName(tr("Key Reference"));
144 m_text->show();
145 m_text->resize(600, 450);
146 m_text->raise();
147 }
148