annotate widgets/TransformFinder.cpp @ 420:f8e04ad8e7b1

* More transform finder fiddling
author Chris Cannam
date Thu, 25 Sep 2008 13:47:40 +0000
parents b0c8e0def62e
children 7466402a32c5
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@416 18 #include "transform/TransformFactory.h"
Chris@416 19
Chris@419 20 #include <QGridLayout>
Chris@416 21 #include <QGridLayout>
Chris@416 22 #include <QLineEdit>
Chris@416 23 #include <QLabel>
Chris@419 24 //#include <SelectableLabel>
Chris@416 25 #include <QDialogButtonBox>
Chris@416 26 #include <QScrollArea>
Chris@416 27
Chris@420 28 SelectableLabel::SelectableLabel(QWidget *parent) :
Chris@420 29 QLabel(parent),
Chris@420 30 m_selected(false)
Chris@420 31 {
Chris@420 32 setTextFormat(Qt::RichText);
Chris@420 33 setLineWidth(2);
Chris@420 34 setFixedWidth(420);
Chris@420 35 setWordWrap(true);
Chris@420 36 }
Chris@420 37
Chris@419 38 void
Chris@419 39 SelectableLabel::setUnselectedText(QString text)
Chris@419 40 {
Chris@420 41 m_unselectedText = text;
Chris@420 42 if (!m_selected) {
Chris@420 43 setText(m_unselectedText);
Chris@420 44 resize(sizeHint());
Chris@420 45 }
Chris@419 46 }
Chris@419 47
Chris@419 48 void
Chris@419 49 SelectableLabel::setSelectedText(QString text)
Chris@419 50 {
Chris@420 51 m_selectedText = text;
Chris@420 52 if (m_selected) {
Chris@420 53 setText(m_selectedText);
Chris@420 54 resize(sizeHint());
Chris@420 55 }
Chris@420 56 }
Chris@420 57
Chris@420 58 void
Chris@420 59 SelectableLabel::setSelected(bool s)
Chris@420 60 {
Chris@420 61 if (m_selected == s) return;
Chris@420 62 m_selected = s;
Chris@420 63 if (m_selected) {
Chris@420 64 setText(m_selectedText);
Chris@420 65 setFrameStyle(QFrame::Box | QFrame::Plain);
Chris@420 66 } else {
Chris@420 67 setText(m_unselectedText);
Chris@420 68 setFrameStyle(QFrame::NoFrame);
Chris@420 69 }
Chris@420 70 resize(sizeHint());
Chris@420 71 parentWidget()->resize(parentWidget()->sizeHint());
Chris@420 72 }
Chris@420 73
Chris@420 74 void
Chris@420 75 SelectableLabel::toggle()
Chris@420 76 {
Chris@420 77 setSelected(!m_selected);
Chris@419 78 }
Chris@419 79
Chris@419 80 void
Chris@419 81 SelectableLabel::mousePressEvent(QMouseEvent *e)
Chris@419 82 {
Chris@420 83 toggle();
Chris@420 84 emit selectionChanged();
Chris@419 85 }
Chris@419 86
Chris@416 87 TransformFinder::TransformFinder(QWidget *parent) :
Chris@417 88 QDialog(parent),
Chris@417 89 m_resultsFrame(0),
Chris@417 90 m_resultsLayout(0)
Chris@416 91 {
Chris@416 92 setWindowTitle(tr("Find a Transform"));
Chris@416 93
Chris@416 94 QGridLayout *mainGrid = new QGridLayout;
Chris@416 95 setLayout(mainGrid);
Chris@416 96
Chris@416 97 mainGrid->addWidget(new QLabel(tr("Find:")), 0, 0);
Chris@416 98
Chris@416 99 QLineEdit *searchField = new QLineEdit;
Chris@416 100 mainGrid->addWidget(searchField, 0, 1);
Chris@416 101 connect(searchField, SIGNAL(textChanged(const QString &)),
Chris@416 102 this, SLOT(searchTextChanged(const QString &)));
Chris@416 103
Chris@416 104 m_resultsScroll = new QScrollArea;
Chris@420 105 // m_resultsScroll->setWidgetResizable(true);
Chris@416 106 mainGrid->addWidget(m_resultsScroll, 1, 0, 1, 2);
Chris@416 107 mainGrid->setRowStretch(1, 10);
Chris@416 108
Chris@416 109 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok |
Chris@416 110 QDialogButtonBox::Cancel);
Chris@416 111 mainGrid->addWidget(bb, 2, 0, 1, 2);
Chris@416 112 connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
Chris@416 113 connect(bb, SIGNAL(rejected()), this, SLOT(reject()));
Chris@416 114
Chris@416 115 resize(500, 400); //!!!
Chris@416 116 }
Chris@416 117
Chris@416 118 TransformFinder::~TransformFinder()
Chris@416 119 {
Chris@416 120 }
Chris@416 121
Chris@416 122 void
Chris@416 123 TransformFinder::searchTextChanged(const QString &text)
Chris@416 124 {
Chris@416 125 std::cerr << "text is " << text.toStdString() << std::endl;
Chris@416 126
Chris@416 127 QStringList keywords = text.split(' ', QString::SkipEmptyParts);
Chris@416 128 TransformFactory::SearchResults results =
Chris@416 129 TransformFactory::getInstance()->search(keywords);
Chris@416 130
Chris@416 131 std::cerr << results.size() << " result(s)..." << std::endl;
Chris@416 132
Chris@416 133 std::set<TransformFactory::Match> sorted;
Chris@416 134 for (TransformFactory::SearchResults::const_iterator j = results.begin();
Chris@416 135 j != results.end(); ++j) {
Chris@416 136 sorted.insert(j->second);
Chris@416 137 }
Chris@416 138
Chris@416 139 int i = 0;
Chris@416 140 /*
Chris@416 141 for (std::set<TransformFactory::Match>::const_iterator j = sorted.begin();
Chris@416 142 j != sorted.end(); ++j) {
Chris@416 143 std::cerr << i++ << ": " << j->transform.toStdString() << ": ";
Chris@416 144 for (TransformFactory::Match::FragmentMap::const_iterator k =
Chris@416 145 j->fragments.begin();
Chris@416 146 k != j->fragments.end(); ++k) {
Chris@416 147 std::cerr << k->first.toStdString() << ": "
Chris@416 148 << k->second.toStdString() << " ";
Chris@416 149 }
Chris@416 150 std::cerr << "(" << j->score << ")" << std::endl;
Chris@416 151 }
Chris@416 152 */
Chris@417 153
Chris@417 154 if (!m_resultsLayout) {
Chris@417 155 std::cerr << "creating frame & layout" << std::endl;
Chris@417 156 m_resultsFrame = new QWidget;
Chris@417 157 // resultsFrame->setFrameStyle(QFrame::Sunken | QFrame::Box);
Chris@419 158 m_resultsLayout = new QGridLayout;
Chris@417 159 m_resultsFrame->setLayout(m_resultsLayout);
Chris@417 160 m_resultsScroll->setWidget(m_resultsFrame);
Chris@417 161 m_resultsFrame->show();
Chris@417 162 }
Chris@416 163
Chris@416 164 i = 0;
Chris@416 165 int maxResults = 40;
Chris@417 166 int height = 0;
Chris@417 167 int width = 0;
Chris@416 168
Chris@420 169 if (sorted.empty()) m_selectedTransform = "";
Chris@420 170
Chris@416 171 for (std::set<TransformFactory::Match>::const_iterator j = sorted.end();
Chris@416 172 j != sorted.begin(); ) {
Chris@416 173 --j;
Chris@417 174
Chris@416 175 TransformDescription desc =
Chris@416 176 TransformFactory::getInstance()->getTransformDescription(j->transform);
Chris@419 177
Chris@419 178 QString labelText;
Chris@416 179 labelText += tr("%2<br><small>").arg(desc.name);
Chris@416 180 labelText += "...";
Chris@416 181 for (TransformFactory::Match::FragmentMap::const_iterator k =
Chris@416 182 j->fragments.begin();
Chris@416 183 k != j->fragments.end(); ++k) {
Chris@416 184 labelText += k->second;
Chris@416 185 labelText += "... ";
Chris@416 186 }
Chris@416 187 labelText += tr("</small>");
Chris@417 188
Chris@419 189 QString selectedText;
Chris@419 190 selectedText += tr("<b>%1</b><br>").arg(desc.name);
Chris@420 191 selectedText += tr("<small>%1</small>").arg(desc.longDescription);
Chris@419 192 /*
Chris@419 193 for (TransformFactory::Match::FragmentMap::const_iterator k =
Chris@419 194 j->fragments.begin();
Chris@419 195 k != j->fragments.end(); ++k) {
Chris@419 196 selectedText += tr("<br><small>%1: %2</small>").arg(k->first).arg(k->second);
Chris@419 197 }
Chris@419 198 */
Chris@419 199
Chris@420 200 selectedText += tr("<ul><small>");
Chris@420 201 selectedText += tr("<li>Plugin type: %1</li>").arg(desc.type);
Chris@420 202 selectedText += tr("<li>Category: %1</li>").arg(desc.category);
Chris@420 203 selectedText += tr("<li>System identifier: %1</li>").arg(desc.identifier);
Chris@420 204 selectedText += tr("</small></ul>");
Chris@419 205
Chris@417 206 if (i >= m_labels.size()) {
Chris@419 207 SelectableLabel *label = new SelectableLabel(m_resultsFrame);
Chris@419 208 m_resultsLayout->addWidget(label, i, 0);
Chris@420 209 connect(label, SIGNAL(selectionChanged()), this,
Chris@420 210 SLOT(selectedLabelChanged()));
Chris@417 211 m_labels.push_back(label);
Chris@417 212 }
Chris@420 213
Chris@420 214 m_labels[i]->setObjectName(desc.identifier);
Chris@420 215 m_selectedTransform = desc.identifier;
Chris@419 216 m_labels[i]->setUnselectedText(labelText);
Chris@419 217 m_labels[i]->setSelectedText(selectedText);
Chris@420 218 m_labels[i]->setSelected(i == 0);
Chris@419 219
Chris@419 220 /*
Chris@417 221 QSize sh = m_labels[i]->sizeHint();
Chris@417 222 std::cerr << "size hint for text \"" << labelText.toStdString() << "\" has height " << sh.height() << std::endl;
Chris@417 223 height += sh.height();
Chris@417 224 if (sh.width() > width) width = sh.width();
Chris@419 225 */
Chris@419 226 // m_labels[i]->resize(m_labels[i]->sizeHint());
Chris@419 227 // m_labels[i]->updateGeometry();
Chris@417 228 m_labels[i]->show();
Chris@417 229
Chris@416 230 if (++i == maxResults) break;
Chris@416 231 }
Chris@416 232
Chris@417 233 std::cerr << "m_labels.size() = " << m_labels.size() << ", i = " << i << ", height = " << height << std::endl;
Chris@417 234
Chris@419 235 for (int j = m_labels.size(); j > i; ) m_labels[--j]->hide();
Chris@417 236
Chris@419 237 m_resultsFrame->resize(m_resultsFrame->sizeHint());
Chris@419 238 // m_resultsFrame->resize(height, width);
Chris@416 239 }
Chris@416 240
Chris@420 241 void
Chris@420 242 TransformFinder::selectedLabelChanged()
Chris@420 243 {
Chris@420 244 QObject *s = sender();
Chris@420 245 m_selectedTransform = "";
Chris@420 246 for (int i = 0; i < m_labels.size(); ++i) {
Chris@420 247 if (!m_labels[i]->isVisible()) continue;
Chris@420 248 if (m_labels[i] == s) {
Chris@420 249 if (m_labels[i]->isSelected()) {
Chris@420 250 m_selectedTransform = m_labels[i]->objectName();
Chris@420 251 }
Chris@420 252 } else {
Chris@420 253 if (m_labels[i]->isSelected()) {
Chris@420 254 m_labels[i]->setSelected(false);
Chris@420 255 }
Chris@420 256 }
Chris@420 257 }
Chris@420 258 std::cerr << "selectedLabelChanged: selected transform is now \""
Chris@420 259 << m_selectedTransform.toStdString() << "\"" << std::endl;
Chris@420 260 }
Chris@420 261
Chris@416 262 TransformId
Chris@416 263 TransformFinder::getTransform() const
Chris@416 264 {
Chris@416 265 return "";
Chris@416 266 }
Chris@416 267