annotate widgets/KeyReference.cpp @ 609:5f3f5c1e23e8

Add hide method
author Chris Cannam
date Mon, 30 Jan 2012 17:22:48 +0000
parents 3801289761a1
children d632a1e87018
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@282 20 #include <QDialog>
Chris@282 21 #include <QVBoxLayout>
Chris@282 22 #include <QDialogButtonBox>
Chris@282 23 #include <QApplication>
Chris@282 24 #include <QDesktopWidget>
Chris@281 25
Chris@281 26 KeyReference::KeyReference() :
Chris@282 27 m_dialog(0)
Chris@281 28 {
Chris@281 29 }
Chris@281 30
Chris@281 31 KeyReference::~KeyReference()
Chris@281 32 {
Chris@282 33 delete m_dialog;
Chris@281 34 }
Chris@281 35
Chris@281 36 void
Chris@281 37 KeyReference::setCategory(QString category)
Chris@281 38 {
Chris@281 39 if (m_map.find(category) == m_map.end()) {
Chris@281 40 m_categoryOrder.push_back(category);
Chris@282 41 m_map[category] = KeyList();
Chris@281 42 }
Chris@281 43 m_currentCategory = category;
Chris@281 44 }
Chris@281 45
Chris@281 46 void
Chris@281 47 KeyReference::registerShortcut(QAction *action, QString overrideName)
Chris@281 48 {
Chris@281 49 QString name = action->text();
Chris@281 50 if (overrideName != "") name = overrideName;
Chris@281 51
Chris@281 52 QString shortcut = action->shortcut();
Chris@281 53 QString tip = action->statusTip();
Chris@281 54
Chris@281 55 registerShortcut(name, shortcut, tip);
Chris@281 56 }
Chris@281 57
Chris@281 58 void
Chris@281 59 KeyReference::registerShortcut(QString name, QString shortcut, QString tip)
Chris@281 60 {
Chris@282 61 name.replace(tr("&"), "");
Chris@282 62
Chris@281 63 KeyList &list = m_map[m_currentCategory];
Chris@281 64
Chris@281 65 for (KeyList::iterator i = list.begin(); i != list.end(); ++i) {
Chris@281 66 if (i->actionName == name) {
Chris@281 67 i->shortcut = shortcut;
Chris@281 68 i->tip = tip;
Chris@281 69 i->alternatives.clear();
Chris@281 70 return;
Chris@281 71 }
Chris@281 72 }
Chris@281 73
Chris@281 74 KeyDetails details;
Chris@281 75 details.actionName = name;
Chris@281 76 details.shortcut = shortcut;
Chris@281 77 details.tip = tip;
Chris@281 78
Chris@281 79 list.push_back(details);
Chris@281 80 }
Chris@281 81
Chris@281 82 void
Chris@281 83 KeyReference::registerAlternativeShortcut(QAction *action, QString alternative)
Chris@281 84 {
Chris@281 85 QString name = action->text();
Chris@281 86 registerAlternativeShortcut(name, alternative);
Chris@281 87 }
Chris@281 88
Chris@281 89 void
Chris@281 90 KeyReference::registerAlternativeShortcut(QString name, QString alternative)
Chris@281 91 {
Chris@282 92 name.replace(tr("&"), "");
Chris@282 93
Chris@281 94 KeyList &list = m_map[m_currentCategory];
Chris@281 95
Chris@281 96 for (KeyList::iterator i = list.begin(); i != list.end(); ++i) {
Chris@281 97 if (i->actionName == name) {
Chris@281 98 i->alternatives.push_back(alternative);
Chris@281 99 return;
Chris@281 100 }
Chris@281 101 }
Chris@281 102 }
Chris@281 103
Chris@281 104 void
Chris@281 105 KeyReference::show()
Chris@281 106 {
Chris@282 107 if (m_dialog) {
Chris@282 108 m_dialog->show();
Chris@282 109 m_dialog->raise();
Chris@281 110 return;
Chris@281 111 }
Chris@281 112
Chris@281 113 QString text;
Chris@291 114
Chris@291 115 QColor bgcolor = QApplication::palette().window().color();
Chris@291 116 bool darkbg = (bgcolor.red() + bgcolor.green() + bgcolor.blue() < 384);
Chris@281 117
Chris@291 118 text += QString("<center><table bgcolor=\"%1\">")
Chris@291 119 .arg(darkbg ? "#121212" : "#e8e8e8");
Chris@281 120
Chris@281 121 for (CategoryList::iterator i = m_categoryOrder.begin();
Chris@281 122 i != m_categoryOrder.end(); ++i) {
Chris@281 123
Chris@281 124 QString category = *i;
Chris@281 125 KeyList &list = m_map[category];
Chris@281 126
Chris@291 127 text += QString("<tr><td bgcolor=\"%1\" colspan=3 align=\"center\"><br><b>%2</b><br></td></tr>\n").arg(darkbg ? "#303030" : "#d0d0d0").arg(category);
Chris@281 128
Chris@281 129 for (KeyList::iterator j = list.begin(); j != list.end(); ++j) {
Chris@281 130
Chris@281 131 QString actionName = j->actionName;
Chris@282 132
Chris@282 133 QString shortcut = j->shortcut;
Chris@282 134 shortcut.replace(" ", "&nbsp;");
Chris@281 135
Chris@281 136 QString tip = j->tip;
Chris@281 137 if (tip != "") tip = QString("<i>%1</i>").arg(tip);
Chris@281 138
Chris@281 139 QString altdesc;
Chris@281 140 if (!j->alternatives.empty()) {
Chris@281 141 for (std::vector<QString>::iterator k = j->alternatives.begin();
Chris@281 142 k != j->alternatives.end(); ++k) {
Chris@282 143 QString alt = *k;
Chris@282 144 alt.replace(" ", "&nbsp;");
Chris@282 145 altdesc += tr("<i>or</i>&nbsp;<b>%1</b>").arg(alt);
Chris@281 146 }
Chris@281 147 altdesc = tr("</b>&nbsp;(%1)<b>").arg(altdesc);
Chris@281 148 }
Chris@281 149
Chris@281 150 text += QString("<tr><td>&nbsp;<b>%1%2</b></td><td>&nbsp;%3</td><td>%4</td></tr>\n")
Chris@282 151 .arg(shortcut).arg(altdesc).arg(actionName).arg(tip);
Chris@281 152 }
Chris@281 153 }
Chris@281 154
Chris@281 155 text += "</table></center>\n";
Chris@281 156
Chris@281 157 m_text = new QTextEdit;
Chris@281 158 m_text->setHtml(text);
Chris@281 159 m_text->setReadOnly(true);
Chris@282 160
Chris@282 161 m_dialog = new QDialog;
Chris@282 162 m_dialog->setWindowTitle(tr("Sonic Visualiser: Key and Mouse Reference"));
Chris@282 163
Chris@282 164 QVBoxLayout *layout = new QVBoxLayout;
Chris@282 165 m_dialog->setLayout(layout);
Chris@282 166 layout->addWidget(m_text);
Chris@282 167
Chris@282 168 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Close);
Chris@282 169 connect(bb, SIGNAL(clicked(QAbstractButton *)), this, SLOT(dialogButtonClicked(QAbstractButton *)));
Chris@282 170 layout->addWidget(bb);
Chris@282 171
Chris@282 172 m_dialog->show();
Chris@282 173
Chris@282 174 QDesktopWidget *desktop = QApplication::desktop();
Chris@282 175 QRect available = desktop->availableGeometry();
Chris@282 176
Chris@282 177 int width = available.width() * 3 / 5;
Chris@282 178 int height = available.height() * 2 / 3;
Chris@282 179 if (height < 450) {
Chris@282 180 if (available.height() > 500) height = 450;
Chris@282 181 }
Chris@282 182 if (width < 600) {
Chris@282 183 if (available.width() > 650) width = 600;
Chris@282 184 }
Chris@282 185
Chris@282 186 m_dialog->resize(width, height);
Chris@282 187 m_dialog->raise();
Chris@281 188 }
Chris@281 189
Chris@282 190 void
Chris@282 191 KeyReference::dialogButtonClicked(QAbstractButton *)
Chris@282 192 {
Chris@282 193 // only button is Close
Chris@282 194 m_dialog->hide();
Chris@282 195 }
Chris@282 196
Chris@609 197 void
Chris@609 198 KeyReference::hide()
Chris@609 199 {
Chris@609 200 if (m_dialog) {
Chris@609 201 m_dialog->hide();
Chris@609 202 }
Chris@609 203 }