annotate widgets/TransformFinder.cpp @ 417:fbf9f53cf979

* Annoyingly not-really-working twist on things
author Chris Cannam
date Tue, 23 Sep 2008 16:42:43 +0000
parents f4487328320d
children bd1e4ff5091d
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@416 20 #include <QVBoxLayout>
Chris@416 21 #include <QGridLayout>
Chris@416 22 #include <QLineEdit>
Chris@416 23 #include <QLabel>
Chris@416 24 #include <QDialogButtonBox>
Chris@416 25 #include <QScrollArea>
Chris@416 26
Chris@416 27 TransformFinder::TransformFinder(QWidget *parent) :
Chris@417 28 QDialog(parent),
Chris@417 29 m_resultsFrame(0),
Chris@417 30 m_resultsLayout(0)
Chris@416 31 {
Chris@416 32 setWindowTitle(tr("Find a Transform"));
Chris@416 33
Chris@416 34 QGridLayout *mainGrid = new QGridLayout;
Chris@416 35 setLayout(mainGrid);
Chris@416 36
Chris@416 37 mainGrid->addWidget(new QLabel(tr("Find:")), 0, 0);
Chris@416 38
Chris@416 39 QLineEdit *searchField = new QLineEdit;
Chris@416 40 mainGrid->addWidget(searchField, 0, 1);
Chris@416 41 connect(searchField, SIGNAL(textChanged(const QString &)),
Chris@416 42 this, SLOT(searchTextChanged(const QString &)));
Chris@416 43
Chris@416 44 m_resultsScroll = new QScrollArea;
Chris@416 45 mainGrid->addWidget(m_resultsScroll, 1, 0, 1, 2);
Chris@416 46 mainGrid->setRowStretch(1, 10);
Chris@416 47
Chris@416 48 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok |
Chris@416 49 QDialogButtonBox::Cancel);
Chris@416 50 mainGrid->addWidget(bb, 2, 0, 1, 2);
Chris@416 51 connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
Chris@416 52 connect(bb, SIGNAL(rejected()), this, SLOT(reject()));
Chris@416 53
Chris@416 54 resize(500, 400); //!!!
Chris@416 55 }
Chris@416 56
Chris@416 57 TransformFinder::~TransformFinder()
Chris@416 58 {
Chris@416 59 }
Chris@416 60
Chris@416 61 void
Chris@416 62 TransformFinder::searchTextChanged(const QString &text)
Chris@416 63 {
Chris@416 64 std::cerr << "text is " << text.toStdString() << std::endl;
Chris@416 65
Chris@416 66 QStringList keywords = text.split(' ', QString::SkipEmptyParts);
Chris@416 67 TransformFactory::SearchResults results =
Chris@416 68 TransformFactory::getInstance()->search(keywords);
Chris@416 69
Chris@416 70 std::cerr << results.size() << " result(s)..." << std::endl;
Chris@416 71
Chris@416 72 std::set<TransformFactory::Match> sorted;
Chris@416 73 for (TransformFactory::SearchResults::const_iterator j = results.begin();
Chris@416 74 j != results.end(); ++j) {
Chris@416 75 sorted.insert(j->second);
Chris@416 76 }
Chris@416 77
Chris@416 78 int i = 0;
Chris@416 79 /*
Chris@416 80 for (std::set<TransformFactory::Match>::const_iterator j = sorted.begin();
Chris@416 81 j != sorted.end(); ++j) {
Chris@416 82 std::cerr << i++ << ": " << j->transform.toStdString() << ": ";
Chris@416 83 for (TransformFactory::Match::FragmentMap::const_iterator k =
Chris@416 84 j->fragments.begin();
Chris@416 85 k != j->fragments.end(); ++k) {
Chris@416 86 std::cerr << k->first.toStdString() << ": "
Chris@416 87 << k->second.toStdString() << " ";
Chris@416 88 }
Chris@416 89 std::cerr << "(" << j->score << ")" << std::endl;
Chris@416 90 }
Chris@416 91 */
Chris@417 92
Chris@417 93 if (!m_resultsLayout) {
Chris@417 94 std::cerr << "creating frame & layout" << std::endl;
Chris@417 95 m_resultsFrame = new QWidget;
Chris@417 96 // resultsFrame->setFrameStyle(QFrame::Sunken | QFrame::Box);
Chris@417 97 m_resultsLayout = new QVBoxLayout;
Chris@417 98 m_resultsFrame->setLayout(m_resultsLayout);
Chris@417 99 m_resultsScroll->setWidget(m_resultsFrame);
Chris@417 100 m_resultsFrame->show();
Chris@417 101 }
Chris@416 102
Chris@416 103 i = 0;
Chris@416 104 int maxResults = 40;
Chris@417 105 int height = 0;
Chris@417 106 int width = 0;
Chris@416 107
Chris@416 108 for (std::set<TransformFactory::Match>::const_iterator j = sorted.end();
Chris@416 109 j != sorted.begin(); ) {
Chris@416 110 --j;
Chris@417 111
Chris@416 112 QString labelText;
Chris@416 113 TransformDescription desc =
Chris@416 114 TransformFactory::getInstance()->getTransformDescription(j->transform);
Chris@416 115 labelText += tr("%2<br><small>").arg(desc.name);
Chris@416 116 labelText += "...";
Chris@416 117 for (TransformFactory::Match::FragmentMap::const_iterator k =
Chris@416 118 j->fragments.begin();
Chris@416 119 k != j->fragments.end(); ++k) {
Chris@416 120 labelText += k->second;
Chris@416 121 labelText += "... ";
Chris@416 122 }
Chris@416 123 labelText += tr("</small>");
Chris@417 124
Chris@417 125 if (i >= m_labels.size()) {
Chris@417 126 QLabel *label = new QLabel(m_resultsFrame);
Chris@417 127 m_resultsLayout->addWidget(label);
Chris@417 128 m_labels.push_back(label);
Chris@417 129 }
Chris@417 130 m_labels[i]->setText(labelText);
Chris@417 131 QSize sh = m_labels[i]->sizeHint();
Chris@417 132 std::cerr << "size hint for text \"" << labelText.toStdString() << "\" has height " << sh.height() << std::endl;
Chris@417 133 height += sh.height();
Chris@417 134 if (sh.width() > width) width = sh.width();
Chris@417 135 m_labels[i]->show();
Chris@417 136
Chris@416 137 if (++i == maxResults) break;
Chris@416 138 }
Chris@416 139
Chris@417 140 std::cerr << "m_labels.size() = " << m_labels.size() << ", i = " << i << ", height = " << height << std::endl;
Chris@417 141
Chris@417 142 while (i < m_labels.size()) m_labels[i++]->hide();
Chris@417 143
Chris@417 144 m_resultsFrame->resize(height, width);
Chris@416 145 }
Chris@416 146
Chris@416 147 TransformId
Chris@416 148 TransformFinder::getTransform() const
Chris@416 149 {
Chris@416 150 return "";
Chris@416 151 }
Chris@416 152