KeyReference.cpp

Go to the documentation of this file.
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_dialog(0)
00028 {
00029 }
00030 
00031 KeyReference::~KeyReference()
00032 {
00033     delete m_dialog;
00034 }
00035 
00036 void
00037 KeyReference::setCategory(QString category)
00038 {
00039     if (m_map.find(category) == m_map.end()) {
00040         m_categoryOrder.push_back(category);
00041         m_map[category] = KeyList();
00042     }
00043     m_currentCategory = category;
00044 }
00045 
00046 void
00047 KeyReference::registerShortcut(QAction *action, QString overrideName)
00048 {
00049     QString name = action->text();
00050     if (overrideName != "") name = overrideName;
00051 
00052     QString shortcut = action->shortcut();
00053     QString tip = action->statusTip();
00054 
00055     registerShortcut(name, shortcut, tip);
00056 }
00057 
00058 void
00059 KeyReference::registerShortcut(QString name, QString shortcut, QString tip)
00060 {
00061     name.replace(tr("&"), "");
00062             
00063     KeyList &list = m_map[m_currentCategory];
00064 
00065     for (KeyList::iterator i = list.begin(); i != list.end(); ++i) {
00066         if (i->actionName == name) {
00067             i->shortcut = shortcut;
00068             i->tip = tip;
00069             i->alternatives.clear();
00070             return;
00071         }
00072     }
00073 
00074     KeyDetails details;
00075     details.actionName = name;
00076     details.shortcut = shortcut;
00077     details.tip = tip;
00078 
00079     list.push_back(details);
00080 }
00081 
00082 void
00083 KeyReference::registerAlternativeShortcut(QAction *action, QString alternative)
00084 {
00085     QString name = action->text();
00086     registerAlternativeShortcut(name, alternative);
00087 }
00088 
00089 void
00090 KeyReference::registerAlternativeShortcut(QString name, QString alternative)
00091 {
00092     name.replace(tr("&"), "");
00093 
00094     KeyList &list = m_map[m_currentCategory];
00095 
00096     for (KeyList::iterator i = list.begin(); i != list.end(); ++i) {
00097         if (i->actionName == name) {
00098             i->alternatives.push_back(alternative);
00099             return;
00100         }
00101     }
00102 }
00103 
00104 void
00105 KeyReference::show()
00106 {
00107     if (m_dialog) {
00108         m_dialog->show();
00109         m_dialog->raise();
00110         return;
00111     }
00112 
00113     QString text;
00114     
00115     QColor bgcolor = QApplication::palette().window().color();
00116     bool darkbg = (bgcolor.red() + bgcolor.green() + bgcolor.blue() < 384);
00117 
00118     text += QString("<center><table bgcolor=\"%1\">")
00119         .arg(darkbg ? "#121212" : "#e8e8e8");
00120         
00121     for (CategoryList::iterator i = m_categoryOrder.begin();
00122          i != m_categoryOrder.end(); ++i) {
00123 
00124         QString category = *i;
00125         KeyList &list = m_map[category];
00126 
00127         text += QString("<tr><td bgcolor=\"%1\" colspan=3 align=\"center\"><br><b>%2</b><br></td></tr>\n").arg(darkbg ? "#303030" : "#d0d0d0").arg(category);
00128 
00129         for (KeyList::iterator j = list.begin(); j != list.end(); ++j) {
00130 
00131             QString actionName = j->actionName;
00132 
00133             QString shortcut = j->shortcut;
00134             shortcut.replace(" ", "&nbsp;");
00135 
00136             QString tip = j->tip;
00137             if (tip != "") tip = QString("<i>%1</i>").arg(tip);
00138 
00139             QString altdesc;
00140             if (!j->alternatives.empty()) {
00141                 for (std::vector<QString>::iterator k = j->alternatives.begin();
00142                      k != j->alternatives.end(); ++k) {
00143                     QString alt = *k;
00144                     alt.replace(" ", "&nbsp;");
00145                     altdesc += tr("<i>or</i>&nbsp;<b>%1</b>").arg(alt);
00146                 }
00147                 altdesc = tr("</b>&nbsp;(%1)<b>").arg(altdesc);
00148             }
00149 
00150             text += QString("<tr><td>&nbsp;<b>%1%2</b></td><td>&nbsp;%3</td><td>%4</td></tr>\n")
00151                 .arg(shortcut).arg(altdesc).arg(actionName).arg(tip);
00152         }
00153     }
00154 
00155     text += "</table></center>\n";
00156 
00157     m_text = new QTextEdit;
00158     m_text->setHtml(text);
00159     m_text->setReadOnly(true);
00160 
00161     m_dialog = new QDialog;
00162     m_dialog->setWindowTitle(tr("Sonic Visualiser: Key and Mouse Reference"));
00163 
00164     QVBoxLayout *layout = new QVBoxLayout;
00165     m_dialog->setLayout(layout);
00166     layout->addWidget(m_text);
00167 
00168     QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Close);
00169     connect(bb, SIGNAL(clicked(QAbstractButton *)), this, SLOT(dialogButtonClicked(QAbstractButton *)));
00170     layout->addWidget(bb);
00171 
00172     m_dialog->show();
00173     
00174     QDesktopWidget *desktop = QApplication::desktop();
00175     QRect available = desktop->availableGeometry();
00176 
00177     int width = available.width() * 3 / 5;
00178     int height = available.height() * 2 / 3;
00179     if (height < 450) {
00180         if (available.height() > 500) height = 450;
00181     }
00182     if (width < 600) {
00183         if (available.width() > 650) width = 600;
00184     }
00185 
00186     m_dialog->resize(width, height);
00187     m_dialog->raise();
00188 }
00189 
00190 void
00191 KeyReference::dialogButtonClicked(QAbstractButton *)
00192 {
00193     // only button is Close
00194     m_dialog->hide();
00195 }
00196 

Generated on Wed Feb 20 15:45:26 2008 for SonicVisualiser by  doxygen 1.5.1