svgui
1.9
|
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ 00002 00003 /* 00004 Sonic Visualiser 00005 An audio file viewer and annotation editor. 00006 Centre for Digital Music, Queen Mary, University of London. 00007 This file copyright 2007 QMUL. 00008 00009 This program is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU General Public License as 00011 published by the Free Software Foundation; either version 2 of the 00012 License, or (at your option) any later version. See the file 00013 COPYING included with this distribution for more information. 00014 */ 00015 00016 #include "KeyReference.h" 00017 00018 #include <QAction> 00019 #include <QTextEdit> 00020 #include <QDialog> 00021 #include <QVBoxLayout> 00022 #include <QDialogButtonBox> 00023 #include <QApplication> 00024 #include <QDesktopWidget> 00025 00026 KeyReference::KeyReference() : 00027 m_text(0), 00028 m_dialog(0) 00029 { 00030 } 00031 00032 KeyReference::~KeyReference() 00033 { 00034 delete m_dialog; 00035 } 00036 00037 void 00038 KeyReference::setCategory(QString category) 00039 { 00040 if (m_map.find(category) == m_map.end()) { 00041 m_categoryOrder.push_back(category); 00042 m_map[category] = KeyList(); 00043 } 00044 m_currentCategory = category; 00045 } 00046 00047 void 00048 KeyReference::registerShortcut(QAction *action, QString overrideName) 00049 { 00050 QString name = action->text(); 00051 if (overrideName != "") name = overrideName; 00052 00053 QString shortcut = action->shortcut().toString(QKeySequence::NativeText); 00054 QString tip = action->statusTip(); 00055 00056 registerShortcut(name, shortcut, tip); 00057 } 00058 00059 void 00060 KeyReference::registerShortcut(QString name, QString shortcut, QString tip) 00061 { 00062 name.replace(tr("&"), ""); 00063 00064 KeyList &list = m_map[m_currentCategory]; 00065 00066 for (KeyList::iterator i = list.begin(); i != list.end(); ++i) { 00067 if (i->actionName == name) { 00068 i->shortcut = shortcut; 00069 i->tip = tip; 00070 i->alternatives.clear(); 00071 return; 00072 } 00073 } 00074 00075 KeyDetails details; 00076 details.actionName = name; 00077 details.shortcut = shortcut; 00078 details.tip = tip; 00079 00080 list.push_back(details); 00081 } 00082 00083 void 00084 KeyReference::registerAlternativeShortcut(QAction *action, QString alternative) 00085 { 00086 QString name = action->text(); 00087 registerAlternativeShortcut(name, alternative); 00088 } 00089 00090 void 00091 KeyReference::registerAlternativeShortcut(QAction *action, QKeySequence shortcut) 00092 { 00093 QString name = action->text(); 00094 registerAlternativeShortcut(name, shortcut.toString(QKeySequence::NativeText)); 00095 } 00096 00097 void 00098 KeyReference::registerAlternativeShortcut(QString name, QString alternative) 00099 { 00100 name.replace(tr("&"), ""); 00101 00102 KeyList &list = m_map[m_currentCategory]; 00103 00104 for (KeyList::iterator i = list.begin(); i != list.end(); ++i) { 00105 if (i->actionName == name) { 00106 i->alternatives.push_back(alternative); 00107 return; 00108 } 00109 } 00110 } 00111 00112 void 00113 KeyReference::registerAlternativeShortcut(QString name, QKeySequence shortcut) 00114 { 00115 registerAlternativeShortcut(name, shortcut.toString(QKeySequence::NativeText)); 00116 } 00117 00118 void 00119 KeyReference::show() 00120 { 00121 if (m_dialog) { 00122 m_dialog->show(); 00123 m_dialog->raise(); 00124 return; 00125 } 00126 00127 QString text; 00128 00129 QColor bgcolor = QApplication::palette().window().color(); 00130 bool darkbg = (bgcolor.red() + bgcolor.green() + bgcolor.blue() < 384); 00131 00132 text += QString("<center><table bgcolor=\"%1\">") 00133 .arg(darkbg ? "#121212" : "#e8e8e8"); 00134 00135 for (CategoryList::iterator i = m_categoryOrder.begin(); 00136 i != m_categoryOrder.end(); ++i) { 00137 00138 QString category = *i; 00139 KeyList &list = m_map[category]; 00140 00141 text += QString("<tr><td bgcolor=\"%1\" colspan=3 align=\"center\"><br><b>%2</b><br></td></tr>\n").arg(darkbg ? "#303030" : "#d0d0d0").arg(category); 00142 00143 for (KeyList::iterator j = list.begin(); j != list.end(); ++j) { 00144 00145 QString actionName = j->actionName; 00146 00147 QString shortcut = j->shortcut; 00148 shortcut.replace(" ", " "); 00149 00150 QString tip = j->tip; 00151 if (tip != "") tip = QString("<i>%1</i>").arg(tip); 00152 00153 QString altdesc; 00154 if (!j->alternatives.empty()) { 00155 for (std::vector<QString>::iterator k = j->alternatives.begin(); 00156 k != j->alternatives.end(); ++k) { 00157 QString alt = *k; 00158 alt.replace(" ", " "); 00159 altdesc += tr("<i>or</i> <b>%1</b>").arg(alt); 00160 } 00161 altdesc = tr("</b> (%1)<b>").arg(altdesc); 00162 } 00163 00164 text += QString("<tr><td width=\"12%\"> <b>%1%2</b></td><td> %3</td><td>%4</td></tr>\n") 00165 .arg(shortcut).arg(altdesc).arg(actionName).arg(tip); 00166 } 00167 } 00168 00169 text += "</table></center>\n"; 00170 00171 m_text = new QTextEdit; 00172 m_text->setHtml(text); 00173 m_text->setReadOnly(true); 00174 00175 m_dialog = new QDialog; 00176 m_dialog->setWindowTitle(tr("%1: Key and Mouse Reference") 00177 .arg(QApplication::applicationName())); 00178 00179 QVBoxLayout *layout = new QVBoxLayout; 00180 m_dialog->setLayout(layout); 00181 layout->addWidget(m_text); 00182 00183 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Close); 00184 connect(bb, SIGNAL(clicked(QAbstractButton *)), this, SLOT(dialogButtonClicked(QAbstractButton *))); 00185 layout->addWidget(bb); 00186 00187 m_dialog->show(); 00188 00189 QDesktopWidget *desktop = QApplication::desktop(); 00190 QRect available = desktop->availableGeometry(); 00191 00192 int width = available.width() * 3 / 5; 00193 int height = available.height() * 2 / 3; 00194 if (height < 450) { 00195 if (available.height() > 500) height = 450; 00196 } 00197 if (width < 600) { 00198 if (available.width() > 650) width = 600; 00199 } 00200 00201 m_dialog->resize(width, height); 00202 m_dialog->raise(); 00203 } 00204 00205 void 00206 KeyReference::dialogButtonClicked(QAbstractButton *) 00207 { 00208 // only button is Close 00209 m_dialog->hide(); 00210 } 00211 00212 void 00213 KeyReference::hide() 00214 { 00215 if (m_dialog) { 00216 m_dialog->hide(); 00217 } 00218 }