annotate widgets/KeyReference.cpp @ 851:cf23656a8ca6

Clarify some sneaky logic
author Chris Cannam
date Wed, 03 Sep 2014 11:49:31 +0100
parents 394caa98d21a
children 44675ab217a3
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@739 52 QString shortcut = action->shortcut().toString(QKeySequence::NativeText);
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@740 90 KeyReference::registerAlternativeShortcut(QAction *action, QKeySequence shortcut)
Chris@740 91 {
Chris@740 92 QString name = action->text();
Chris@740 93 registerAlternativeShortcut(name, shortcut.toString(QKeySequence::NativeText));
Chris@740 94 }
Chris@740 95
Chris@740 96 void
Chris@281 97 KeyReference::registerAlternativeShortcut(QString name, QString alternative)
Chris@281 98 {
Chris@282 99 name.replace(tr("&"), "");
Chris@282 100
Chris@281 101 KeyList &list = m_map[m_currentCategory];
Chris@281 102
Chris@281 103 for (KeyList::iterator i = list.begin(); i != list.end(); ++i) {
Chris@281 104 if (i->actionName == name) {
Chris@281 105 i->alternatives.push_back(alternative);
Chris@281 106 return;
Chris@281 107 }
Chris@281 108 }
Chris@281 109 }
Chris@281 110
Chris@281 111 void
Chris@740 112 KeyReference::registerAlternativeShortcut(QString name, QKeySequence shortcut)
Chris@740 113 {
Chris@740 114 registerAlternativeShortcut(name, shortcut.toString(QKeySequence::NativeText));
Chris@740 115 }
Chris@740 116
Chris@740 117 void
Chris@281 118 KeyReference::show()
Chris@281 119 {
Chris@282 120 if (m_dialog) {
Chris@282 121 m_dialog->show();
Chris@282 122 m_dialog->raise();
Chris@281 123 return;
Chris@281 124 }
Chris@281 125
Chris@281 126 QString text;
Chris@291 127
Chris@291 128 QColor bgcolor = QApplication::palette().window().color();
Chris@291 129 bool darkbg = (bgcolor.red() + bgcolor.green() + bgcolor.blue() < 384);
Chris@281 130
Chris@291 131 text += QString("<center><table bgcolor=\"%1\">")
Chris@291 132 .arg(darkbg ? "#121212" : "#e8e8e8");
Chris@281 133
Chris@281 134 for (CategoryList::iterator i = m_categoryOrder.begin();
Chris@281 135 i != m_categoryOrder.end(); ++i) {
Chris@281 136
Chris@281 137 QString category = *i;
Chris@281 138 KeyList &list = m_map[category];
Chris@281 139
Chris@291 140 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 141
Chris@281 142 for (KeyList::iterator j = list.begin(); j != list.end(); ++j) {
Chris@281 143
Chris@281 144 QString actionName = j->actionName;
Chris@282 145
Chris@282 146 QString shortcut = j->shortcut;
Chris@282 147 shortcut.replace(" ", "&nbsp;");
Chris@281 148
Chris@281 149 QString tip = j->tip;
Chris@281 150 if (tip != "") tip = QString("<i>%1</i>").arg(tip);
Chris@281 151
Chris@281 152 QString altdesc;
Chris@281 153 if (!j->alternatives.empty()) {
Chris@281 154 for (std::vector<QString>::iterator k = j->alternatives.begin();
Chris@281 155 k != j->alternatives.end(); ++k) {
Chris@282 156 QString alt = *k;
Chris@282 157 alt.replace(" ", "&nbsp;");
Chris@282 158 altdesc += tr("<i>or</i>&nbsp;<b>%1</b>").arg(alt);
Chris@281 159 }
Chris@281 160 altdesc = tr("</b>&nbsp;(%1)<b>").arg(altdesc);
Chris@281 161 }
Chris@281 162
Chris@740 163 text += QString("<tr><td width=\"12%\">&nbsp;<b>%1%2</b></td><td>&nbsp;%3</td><td>%4</td></tr>\n")
Chris@282 164 .arg(shortcut).arg(altdesc).arg(actionName).arg(tip);
Chris@281 165 }
Chris@281 166 }
Chris@281 167
Chris@281 168 text += "</table></center>\n";
Chris@281 169
Chris@281 170 m_text = new QTextEdit;
Chris@281 171 m_text->setHtml(text);
Chris@281 172 m_text->setReadOnly(true);
Chris@282 173
Chris@282 174 m_dialog = new QDialog;
Chris@750 175 m_dialog->setWindowTitle(tr("%1: Key and Mouse Reference")
Chris@750 176 .arg(QApplication::applicationName()));
Chris@282 177
Chris@282 178 QVBoxLayout *layout = new QVBoxLayout;
Chris@282 179 m_dialog->setLayout(layout);
Chris@282 180 layout->addWidget(m_text);
Chris@282 181
Chris@282 182 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Close);
Chris@282 183 connect(bb, SIGNAL(clicked(QAbstractButton *)), this, SLOT(dialogButtonClicked(QAbstractButton *)));
Chris@282 184 layout->addWidget(bb);
Chris@282 185
Chris@282 186 m_dialog->show();
Chris@282 187
Chris@282 188 QDesktopWidget *desktop = QApplication::desktop();
Chris@282 189 QRect available = desktop->availableGeometry();
Chris@282 190
Chris@282 191 int width = available.width() * 3 / 5;
Chris@282 192 int height = available.height() * 2 / 3;
Chris@282 193 if (height < 450) {
Chris@282 194 if (available.height() > 500) height = 450;
Chris@282 195 }
Chris@282 196 if (width < 600) {
Chris@282 197 if (available.width() > 650) width = 600;
Chris@282 198 }
Chris@282 199
Chris@282 200 m_dialog->resize(width, height);
Chris@282 201 m_dialog->raise();
Chris@281 202 }
Chris@281 203
Chris@282 204 void
Chris@282 205 KeyReference::dialogButtonClicked(QAbstractButton *)
Chris@282 206 {
Chris@282 207 // only button is Close
Chris@282 208 m_dialog->hide();
Chris@282 209 }
Chris@282 210
Chris@609 211 void
Chris@609 212 KeyReference::hide()
Chris@609 213 {
Chris@609 214 if (m_dialog) {
Chris@609 215 m_dialog->hide();
Chris@609 216 }
Chris@609 217 }