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