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