annotate widgets/KeyReference.cpp @ 857:44675ab217a3

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