annotate widgets/TransformFinder.cpp @ 429:427e5c58658e

* Fix a nasty and long-standing race condition in MatrixFile's use of FileReadThread that was causing crashes sometimes
author Chris Cannam
date Thu, 09 Oct 2008 20:10:28 +0000
parents 6bf3c906b7a5
children 4711b400f976
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@424 134 std::set<TransformFactory::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@424 142 for (std::set<TransformFactory::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@424 150 else m_selectedTransform = m_sortedResults.begin()->transform;
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@424 167 std::cerr << "sorted size = " << m_sortedResults.size() << std::endl;
Chris@417 168
Chris@416 169 TransformDescription desc =
Chris@424 170 TransformFactory::getInstance()->getTransformDescription
Chris@424 171 (m_sortedResults[i].transform);
Chris@419 172
Chris@419 173 QString labelText;
Chris@425 174 labelText += tr("%1<br><small>")
Chris@424 175 .arg(XmlExportable::encodeEntities(desc.name));
Chris@424 176
Chris@416 177 labelText += "...";
Chris@416 178 for (TransformFactory::Match::FragmentMap::const_iterator k =
Chris@424 179 m_sortedResults[i].fragments.begin();
Chris@424 180 k != m_sortedResults[i].fragments.end(); ++k) {
Chris@416 181 labelText += k->second;
Chris@416 182 labelText += "... ";
Chris@416 183 }
Chris@416 184 labelText += tr("</small>");
Chris@417 185
Chris@419 186 QString selectedText;
Chris@424 187 selectedText += tr("<b>%1</b><br>")
Chris@424 188 .arg(XmlExportable::encodeEntities(desc.name));
Chris@424 189 selectedText += tr("<small>%1</small>")
Chris@424 190 .arg(XmlExportable::encodeEntities(desc.longDescription));
Chris@419 191
Chris@426 192 selectedText += tr("<br><small>");
Chris@426 193 selectedText += tr("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&mdash; Plugin type: %1<br>")
Chris@424 194 .arg(XmlExportable::encodeEntities(desc.type));
Chris@426 195 selectedText += tr("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&mdash; Category: %1<br>")
Chris@424 196 .arg(XmlExportable::encodeEntities(desc.category));
Chris@426 197 selectedText += tr("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&mdash; System identifier: %1")
Chris@424 198 .arg(XmlExportable::encodeEntities(desc.identifier));
Chris@426 199 selectedText += tr("</small>");
Chris@419 200
Chris@417 201 if (i >= m_labels.size()) {
Chris@419 202 SelectableLabel *label = new SelectableLabel(m_resultsFrame);
Chris@421 203 m_resultsLayout->addWidget(label);
Chris@420 204 connect(label, SIGNAL(selectionChanged()), this,
Chris@420 205 SLOT(selectedLabelChanged()));
Chris@424 206 connect(label, SIGNAL(doubleClicked()), this,
Chris@424 207 SLOT(accept()));
Chris@421 208 QPalette palette = label->palette();
Chris@421 209 label->setPalette(palette);
Chris@417 210 m_labels.push_back(label);
Chris@417 211 }
Chris@421 212
Chris@420 213 m_labels[i]->setObjectName(desc.identifier);
Chris@423 214 m_labels[i]->setFixedWidth(this->width() - 40);
Chris@419 215 m_labels[i]->setUnselectedText(labelText);
Chris@425 216
Chris@425 217 // std::cerr << "selected text: " << selectedText.toStdString() << std::endl;
Chris@419 218 m_labels[i]->setSelectedText(selectedText);
Chris@423 219
Chris@424 220 m_labels[i]->setSelected(m_selectedTransform == desc.identifier);
Chris@425 221
Chris@425 222 if (!m_labels[i]->isVisible()) m_labels[i]->show();
Chris@417 223
Chris@424 224 ++m_upToDateCount;
Chris@424 225
Chris@425 226 if (i == 0) break;
Chris@416 227 }
Chris@425 228
Chris@425 229 m_resultsFrame->resize(m_resultsFrame->sizeHint());
Chris@416 230 }
Chris@416 231
Chris@420 232 void
Chris@420 233 TransformFinder::selectedLabelChanged()
Chris@420 234 {
Chris@420 235 QObject *s = sender();
Chris@420 236 m_selectedTransform = "";
Chris@420 237 for (int i = 0; i < m_labels.size(); ++i) {
Chris@420 238 if (!m_labels[i]->isVisible()) continue;
Chris@420 239 if (m_labels[i] == s) {
Chris@420 240 if (m_labels[i]->isSelected()) {
Chris@420 241 m_selectedTransform = m_labels[i]->objectName();
Chris@420 242 }
Chris@420 243 } else {
Chris@420 244 if (m_labels[i]->isSelected()) {
Chris@420 245 m_labels[i]->setSelected(false);
Chris@420 246 }
Chris@420 247 }
Chris@420 248 }
Chris@420 249 std::cerr << "selectedLabelChanged: selected transform is now \""
Chris@420 250 << m_selectedTransform.toStdString() << "\"" << std::endl;
Chris@420 251 }
Chris@420 252
Chris@416 253 TransformId
Chris@416 254 TransformFinder::getTransform() const
Chris@416 255 {
Chris@424 256 return m_selectedTransform;
Chris@416 257 }
Chris@416 258
Chris@426 259 void
Chris@426 260 TransformFinder::up()
Chris@426 261 {
Chris@426 262 for (int i = 0; i < m_labels.size(); ++i) {
Chris@426 263 if (!m_labels[i]->isVisible()) continue;
Chris@426 264 if (m_labels[i]->objectName() == m_selectedTransform) {
Chris@426 265 if (i > 0) {
Chris@426 266 m_labels[i]->setSelected(false);
Chris@426 267 m_labels[i-1]->setSelected(true);
Chris@426 268 m_selectedTransform = m_labels[i-1]->objectName();
Chris@426 269 }
Chris@426 270 return;
Chris@426 271 }
Chris@426 272 }
Chris@426 273 }
Chris@426 274
Chris@426 275 void
Chris@426 276 TransformFinder::down()
Chris@426 277 {
Chris@426 278 for (int i = 0; i < m_labels.size(); ++i) {
Chris@426 279 if (!m_labels[i]->isVisible()) continue;
Chris@426 280 if (m_labels[i]->objectName() == m_selectedTransform) {
Chris@426 281 if (i+1 < m_labels.size() &&
Chris@426 282 m_labels[i+1]->isVisible()) {
Chris@426 283 m_labels[i]->setSelected(false);
Chris@426 284 m_labels[i+1]->setSelected(true);
Chris@426 285 m_selectedTransform = m_labels[i+1]->objectName();
Chris@426 286 }
Chris@426 287 return;
Chris@426 288 }
Chris@426 289 }
Chris@426 290 }
Chris@426 291