annotate widgets/TransformFinder.cpp @ 437:755243c67f59

* Add vertical zoom and pan to time-value layer. Still some defects, particularly in logarithmic mode. Now need to get this in note layer as well! * Some fixes to log colouring in segmentation mode of time-value layer.
author Chris Cannam
date Fri, 24 Oct 2008 14:52:40 +0000
parents 1e0744b2cebe
children f8fcfbe13c8f
rev   line source
Chris@416 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@416 2
Chris@416 3 /*
Chris@416 4 Sonic Visualiser
Chris@416 5 An audio file viewer and annotation editor.
Chris@416 6 Centre for Digital Music, Queen Mary, University of London.
Chris@416 7 This file copyright 2008 QMUL.
Chris@416 8
Chris@416 9 This program is free software; you can redistribute it and/or
Chris@416 10 modify it under the terms of the GNU General Public License as
Chris@416 11 published by the Free Software Foundation; either version 2 of the
Chris@416 12 License, or (at your option) any later version. See the file
Chris@416 13 COPYING included with this distribution for more information.
Chris@416 14 */
Chris@416 15
Chris@416 16 #include "TransformFinder.h"
Chris@416 17
Chris@424 18 #include "base/XmlExportable.h"
Chris@416 19 #include "transform/TransformFactory.h"
Chris@424 20 #include "SelectableLabel.h"
Chris@416 21
Chris@421 22 #include <QVBoxLayout>
Chris@416 23 #include <QGridLayout>
Chris@416 24 #include <QLineEdit>
Chris@416 25 #include <QLabel>
Chris@416 26 #include <QDialogButtonBox>
Chris@416 27 #include <QScrollArea>
Chris@421 28 #include <QApplication>
Chris@423 29 #include <QDesktopWidget>
Chris@424 30 #include <QTimer>
Chris@426 31 #include <QAction>
Chris@421 32
Chris@416 33 TransformFinder::TransformFinder(QWidget *parent) :
Chris@417 34 QDialog(parent),
Chris@417 35 m_resultsFrame(0),
Chris@417 36 m_resultsLayout(0)
Chris@416 37 {
Chris@416 38 setWindowTitle(tr("Find a Transform"));
Chris@416 39
Chris@416 40 QGridLayout *mainGrid = new QGridLayout;
Chris@421 41 mainGrid->setVerticalSpacing(0);
Chris@416 42 setLayout(mainGrid);
Chris@416 43
Chris@416 44 mainGrid->addWidget(new QLabel(tr("Find:")), 0, 0);
Chris@416 45
Chris@416 46 QLineEdit *searchField = new QLineEdit;
Chris@416 47 mainGrid->addWidget(searchField, 0, 1);
Chris@416 48 connect(searchField, SIGNAL(textChanged(const QString &)),
Chris@416 49 this, SLOT(searchTextChanged(const QString &)));
Chris@416 50
Chris@416 51 m_resultsScroll = new QScrollArea;
Chris@420 52 // m_resultsScroll->setWidgetResizable(true);
Chris@416 53 mainGrid->addWidget(m_resultsScroll, 1, 0, 1, 2);
Chris@416 54 mainGrid->setRowStretch(1, 10);
Chris@416 55
Chris@416 56 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok |
Chris@416 57 QDialogButtonBox::Cancel);
Chris@416 58 mainGrid->addWidget(bb, 2, 0, 1, 2);
Chris@416 59 connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
Chris@416 60 connect(bb, SIGNAL(rejected()), this, SLOT(reject()));
Chris@421 61 if (!m_resultsLayout) {
Chris@421 62 std::cerr << "creating frame & layout" << std::endl;
Chris@421 63 m_resultsFrame = new QWidget;
Chris@421 64 QPalette palette = m_resultsFrame->palette();
Chris@421 65 palette.setColor(QPalette::Window, palette.color(QPalette::Base));
Chris@421 66 m_resultsFrame->setPalette(palette);
Chris@421 67 m_resultsScroll->setPalette(palette);
Chris@421 68 m_resultsLayout = new QVBoxLayout;
Chris@423 69 m_resultsLayout->setSpacing(0);
Chris@423 70 m_resultsLayout->setContentsMargins(0, 0, 0, 0);
Chris@421 71 m_resultsFrame->setLayout(m_resultsLayout);
Chris@421 72 m_resultsScroll->setWidget(m_resultsFrame);
Chris@421 73 m_resultsFrame->show();
Chris@421 74 }
Chris@416 75
Chris@426 76 QAction *up = new QAction(tr("Up"), this);
Chris@426 77 up->setShortcut(tr("Up"));
Chris@426 78 connect(up, SIGNAL(triggered()), this, SLOT(up()));
Chris@426 79 addAction(up);
Chris@426 80
Chris@426 81 QAction *down = new QAction(tr("Down"), this);
Chris@426 82 down->setShortcut(tr("Down"));
Chris@426 83 connect(down, SIGNAL(triggered()), this, SLOT(down()));
Chris@426 84 addAction(down);
Chris@426 85
Chris@423 86 QDesktopWidget *desktop = QApplication::desktop();
Chris@423 87 QRect available = desktop->availableGeometry();
Chris@423 88
Chris@423 89 int width = available.width() / 2;
Chris@423 90 int height = available.height() / 2;
Chris@423 91 if (height < 450) {
Chris@423 92 if (available.height() > 500) height = 450;
Chris@423 93 }
Chris@423 94 if (width < 600) {
Chris@423 95 if (available.width() > 650) width = 600;
Chris@423 96 }
Chris@423 97
Chris@423 98 resize(width, height);
Chris@423 99 raise();
Chris@424 100
Chris@424 101 m_upToDateCount = 0;
Chris@424 102 m_timer = new QTimer(this);
Chris@424 103 connect(m_timer, SIGNAL(timeout()), this, SLOT(timeout()));
Chris@425 104 m_timer->start(30);
Chris@416 105 }
Chris@416 106
Chris@416 107 TransformFinder::~TransformFinder()
Chris@416 108 {
Chris@416 109 }
Chris@416 110
Chris@416 111 void
Chris@416 112 TransformFinder::searchTextChanged(const QString &text)
Chris@416 113 {
Chris@416 114 std::cerr << "text is " << text.toStdString() << std::endl;
Chris@424 115 m_newSearchText = text;
Chris@424 116 }
Chris@416 117
Chris@424 118 void
Chris@424 119 TransformFinder::timeout()
Chris@424 120 {
Chris@424 121 int maxResults = 40;
Chris@416 122
Chris@424 123 if (m_newSearchText != "") {
Chris@416 124
Chris@424 125 QString text = m_newSearchText;
Chris@424 126 m_newSearchText = "";
Chris@424 127
Chris@424 128 QStringList keywords = text.split(' ', QString::SkipEmptyParts);
Chris@424 129 TransformFactory::SearchResults results =
Chris@424 130 TransformFactory::getInstance()->search(keywords);
Chris@424 131
Chris@424 132 std::cerr << results.size() << " result(s)..." << std::endl;
Chris@424 133
Chris@431 134 std::set<TextMatcher::Match> sorted;
Chris@424 135 sorted.clear();
Chris@424 136 for (TransformFactory::SearchResults::const_iterator j = results.begin();
Chris@424 137 j != results.end(); ++j) {
Chris@424 138 sorted.insert(j->second);
Chris@424 139 }
Chris@424 140
Chris@424 141 m_sortedResults.clear();
Chris@431 142 for (std::set<TextMatcher::Match>::const_iterator j = sorted.end();
Chris@424 143 j != sorted.begin(); ) {
Chris@424 144 --j;
Chris@424 145 m_sortedResults.push_back(*j);
Chris@424 146 if (m_sortedResults.size() == maxResults) break;
Chris@424 147 }
Chris@424 148
Chris@424 149 if (m_sortedResults.empty()) m_selectedTransform = "";
Chris@431 150 else m_selectedTransform = m_sortedResults.begin()->key;
Chris@424 151
Chris@424 152 m_upToDateCount = 0;
Chris@424 153
Chris@424 154 for (int j = m_labels.size(); j > m_sortedResults.size(); ) {
Chris@424 155 m_labels[--j]->hide();
Chris@424 156 }
Chris@424 157
Chris@424 158 return;
Chris@416 159 }
Chris@416 160
Chris@425 161 if (m_upToDateCount >= m_sortedResults.size()) return;
Chris@425 162
Chris@425 163 while (m_upToDateCount < m_sortedResults.size()) {
Chris@417 164
Chris@424 165 int i = m_upToDateCount;
Chris@416 166
Chris@431 167 // std::cerr << "sorted size = " << m_sortedResults.size() << std::endl;
Chris@417 168
Chris@431 169 TransformDescription desc;
Chris@431 170 TransformId tid = m_sortedResults[i].key;
Chris@431 171 TransformFactory *factory = TransformFactory::getInstance();
Chris@431 172 TransformFactory::TransformInstallStatus status =
Chris@431 173 factory->getTransformInstallStatus(tid);
Chris@431 174 QString suffix;
Chris@431 175
Chris@431 176 if (status == TransformFactory::TransformInstalled) {
Chris@431 177 desc = factory->getTransformDescription(tid);
Chris@431 178 } else {
Chris@431 179 desc = factory->getUninstalledTransformDescription(tid);
Chris@431 180 suffix = tr("<i> (not installed)</i>");
Chris@431 181 }
Chris@419 182
Chris@419 183 QString labelText;
Chris@431 184 labelText += tr("%1%2<br><small>")
Chris@431 185 .arg(XmlExportable::encodeEntities(desc.name))
Chris@431 186 .arg(suffix);
Chris@424 187
Chris@416 188 labelText += "...";
Chris@431 189 for (TextMatcher::Match::FragmentMap::const_iterator k =
Chris@424 190 m_sortedResults[i].fragments.begin();
Chris@424 191 k != m_sortedResults[i].fragments.end(); ++k) {
Chris@416 192 labelText += k->second;
Chris@416 193 labelText += "... ";
Chris@416 194 }
Chris@416 195 labelText += tr("</small>");
Chris@417 196
Chris@419 197 QString selectedText;
Chris@431 198 selectedText += tr("<b>%1</b>%2<br>")
Chris@431 199 .arg(XmlExportable::encodeEntities
Chris@431 200 (desc.name == "" ? desc.identifier : desc.name))
Chris@431 201 .arg(suffix);
Chris@431 202
Chris@431 203 if (desc.longDescription != "") {
Chris@431 204 selectedText += tr("<small>%1</small>")
Chris@431 205 .arg(XmlExportable::encodeEntities(desc.longDescription));
Chris@431 206 } else if (desc.description != "") {
Chris@431 207 selectedText += tr("<small>%1</small>")
Chris@431 208 .arg(XmlExportable::encodeEntities(desc.description));
Chris@431 209 }
Chris@419 210
Chris@436 211 selectedText += tr("<small>");
Chris@431 212 if (desc.type != "") {
Chris@436 213 selectedText += tr("<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&mdash; Plugin type: %1")
Chris@431 214 .arg(XmlExportable::encodeEntities(desc.type));
Chris@431 215 }
Chris@431 216 if (desc.category != "") {
Chris@436 217 selectedText += tr("<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&mdash; Category: %1")
Chris@431 218 .arg(XmlExportable::encodeEntities(desc.category));
Chris@431 219 }
Chris@436 220 selectedText += tr("<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&mdash; System identifier: %1")
Chris@424 221 .arg(XmlExportable::encodeEntities(desc.identifier));
Chris@436 222 if (desc.infoUrl != "") {
Chris@436 223 selectedText += tr("<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&mdash; More information: <a href=\"%1\">%1</a>")
Chris@436 224 .arg(desc.infoUrl);
Chris@436 225 }
Chris@426 226 selectedText += tr("</small>");
Chris@419 227
Chris@417 228 if (i >= m_labels.size()) {
Chris@419 229 SelectableLabel *label = new SelectableLabel(m_resultsFrame);
Chris@421 230 m_resultsLayout->addWidget(label);
Chris@420 231 connect(label, SIGNAL(selectionChanged()), this,
Chris@420 232 SLOT(selectedLabelChanged()));
Chris@424 233 connect(label, SIGNAL(doubleClicked()), this,
Chris@424 234 SLOT(accept()));
Chris@421 235 QPalette palette = label->palette();
Chris@421 236 label->setPalette(palette);
Chris@436 237 label->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard |
Chris@436 238 Qt::LinksAccessibleByMouse |
Chris@436 239 Qt::TextSelectableByMouse);
Chris@436 240 label->setOpenExternalLinks(true);
Chris@417 241 m_labels.push_back(label);
Chris@417 242 }
Chris@421 243
Chris@420 244 m_labels[i]->setObjectName(desc.identifier);
Chris@423 245 m_labels[i]->setFixedWidth(this->width() - 40);
Chris@419 246 m_labels[i]->setUnselectedText(labelText);
Chris@425 247
Chris@425 248 // std::cerr << "selected text: " << selectedText.toStdString() << std::endl;
Chris@419 249 m_labels[i]->setSelectedText(selectedText);
Chris@423 250
Chris@424 251 m_labels[i]->setSelected(m_selectedTransform == desc.identifier);
Chris@425 252
Chris@425 253 if (!m_labels[i]->isVisible()) m_labels[i]->show();
Chris@417 254
Chris@424 255 ++m_upToDateCount;
Chris@424 256
Chris@425 257 if (i == 0) break;
Chris@416 258 }
Chris@425 259
Chris@425 260 m_resultsFrame->resize(m_resultsFrame->sizeHint());
Chris@416 261 }
Chris@416 262
Chris@420 263 void
Chris@420 264 TransformFinder::selectedLabelChanged()
Chris@420 265 {
Chris@420 266 QObject *s = sender();
Chris@420 267 m_selectedTransform = "";
Chris@420 268 for (int i = 0; i < m_labels.size(); ++i) {
Chris@420 269 if (!m_labels[i]->isVisible()) continue;
Chris@420 270 if (m_labels[i] == s) {
Chris@420 271 if (m_labels[i]->isSelected()) {
Chris@420 272 m_selectedTransform = m_labels[i]->objectName();
Chris@420 273 }
Chris@420 274 } else {
Chris@420 275 if (m_labels[i]->isSelected()) {
Chris@420 276 m_labels[i]->setSelected(false);
Chris@420 277 }
Chris@420 278 }
Chris@420 279 }
Chris@420 280 std::cerr << "selectedLabelChanged: selected transform is now \""
Chris@420 281 << m_selectedTransform.toStdString() << "\"" << std::endl;
Chris@420 282 }
Chris@420 283
Chris@416 284 TransformId
Chris@416 285 TransformFinder::getTransform() const
Chris@416 286 {
Chris@424 287 return m_selectedTransform;
Chris@416 288 }
Chris@416 289
Chris@426 290 void
Chris@426 291 TransformFinder::up()
Chris@426 292 {
Chris@426 293 for (int i = 0; i < m_labels.size(); ++i) {
Chris@426 294 if (!m_labels[i]->isVisible()) continue;
Chris@426 295 if (m_labels[i]->objectName() == m_selectedTransform) {
Chris@426 296 if (i > 0) {
Chris@426 297 m_labels[i]->setSelected(false);
Chris@426 298 m_labels[i-1]->setSelected(true);
Chris@426 299 m_selectedTransform = m_labels[i-1]->objectName();
Chris@426 300 }
Chris@426 301 return;
Chris@426 302 }
Chris@426 303 }
Chris@426 304 }
Chris@426 305
Chris@426 306 void
Chris@426 307 TransformFinder::down()
Chris@426 308 {
Chris@426 309 for (int i = 0; i < m_labels.size(); ++i) {
Chris@426 310 if (!m_labels[i]->isVisible()) continue;
Chris@426 311 if (m_labels[i]->objectName() == m_selectedTransform) {
Chris@426 312 if (i+1 < m_labels.size() &&
Chris@426 313 m_labels[i+1]->isVisible()) {
Chris@426 314 m_labels[i]->setSelected(false);
Chris@426 315 m_labels[i+1]->setSelected(true);
Chris@426 316 m_selectedTransform = m_labels[i+1]->objectName();
Chris@426 317 }
Chris@426 318 return;
Chris@426 319 }
Chris@426 320 }
Chris@426 321 }
Chris@426 322