Chris@416: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@416: Chris@416: /* Chris@416: Sonic Visualiser Chris@416: An audio file viewer and annotation editor. Chris@416: Centre for Digital Music, Queen Mary, University of London. Chris@416: This file copyright 2008 QMUL. Chris@416: Chris@416: This program is free software; you can redistribute it and/or Chris@416: modify it under the terms of the GNU General Public License as Chris@416: published by the Free Software Foundation; either version 2 of the Chris@416: License, or (at your option) any later version. See the file Chris@416: COPYING included with this distribution for more information. Chris@416: */ Chris@416: Chris@416: #include "TransformFinder.h" Chris@416: Chris@416: #include "transform/TransformFactory.h" Chris@416: Chris@419: #include Chris@416: #include Chris@416: #include Chris@416: #include Chris@419: //#include Chris@416: #include Chris@416: #include Chris@416: Chris@420: SelectableLabel::SelectableLabel(QWidget *parent) : Chris@420: QLabel(parent), Chris@420: m_selected(false) Chris@420: { Chris@420: setTextFormat(Qt::RichText); Chris@420: setLineWidth(2); Chris@420: setFixedWidth(420); Chris@420: setWordWrap(true); Chris@420: } Chris@420: Chris@419: void Chris@419: SelectableLabel::setUnselectedText(QString text) Chris@419: { Chris@420: m_unselectedText = text; Chris@420: if (!m_selected) { Chris@420: setText(m_unselectedText); Chris@420: resize(sizeHint()); Chris@420: } Chris@419: } Chris@419: Chris@419: void Chris@419: SelectableLabel::setSelectedText(QString text) Chris@419: { Chris@420: m_selectedText = text; Chris@420: if (m_selected) { Chris@420: setText(m_selectedText); Chris@420: resize(sizeHint()); Chris@420: } Chris@420: } Chris@420: Chris@420: void Chris@420: SelectableLabel::setSelected(bool s) Chris@420: { Chris@420: if (m_selected == s) return; Chris@420: m_selected = s; Chris@420: if (m_selected) { Chris@420: setText(m_selectedText); Chris@420: setFrameStyle(QFrame::Box | QFrame::Plain); Chris@420: } else { Chris@420: setText(m_unselectedText); Chris@420: setFrameStyle(QFrame::NoFrame); Chris@420: } Chris@420: resize(sizeHint()); Chris@420: parentWidget()->resize(parentWidget()->sizeHint()); Chris@420: } Chris@420: Chris@420: void Chris@420: SelectableLabel::toggle() Chris@420: { Chris@420: setSelected(!m_selected); Chris@419: } Chris@419: Chris@419: void Chris@419: SelectableLabel::mousePressEvent(QMouseEvent *e) Chris@419: { Chris@420: toggle(); Chris@420: emit selectionChanged(); Chris@419: } Chris@419: Chris@416: TransformFinder::TransformFinder(QWidget *parent) : Chris@417: QDialog(parent), Chris@417: m_resultsFrame(0), Chris@417: m_resultsLayout(0) Chris@416: { Chris@416: setWindowTitle(tr("Find a Transform")); Chris@416: Chris@416: QGridLayout *mainGrid = new QGridLayout; Chris@416: setLayout(mainGrid); Chris@416: Chris@416: mainGrid->addWidget(new QLabel(tr("Find:")), 0, 0); Chris@416: Chris@416: QLineEdit *searchField = new QLineEdit; Chris@416: mainGrid->addWidget(searchField, 0, 1); Chris@416: connect(searchField, SIGNAL(textChanged(const QString &)), Chris@416: this, SLOT(searchTextChanged(const QString &))); Chris@416: Chris@416: m_resultsScroll = new QScrollArea; Chris@420: // m_resultsScroll->setWidgetResizable(true); Chris@416: mainGrid->addWidget(m_resultsScroll, 1, 0, 1, 2); Chris@416: mainGrid->setRowStretch(1, 10); Chris@416: Chris@416: QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok | Chris@416: QDialogButtonBox::Cancel); Chris@416: mainGrid->addWidget(bb, 2, 0, 1, 2); Chris@416: connect(bb, SIGNAL(accepted()), this, SLOT(accept())); Chris@416: connect(bb, SIGNAL(rejected()), this, SLOT(reject())); Chris@416: Chris@416: resize(500, 400); //!!! Chris@416: } Chris@416: Chris@416: TransformFinder::~TransformFinder() Chris@416: { Chris@416: } Chris@416: Chris@416: void Chris@416: TransformFinder::searchTextChanged(const QString &text) Chris@416: { Chris@416: std::cerr << "text is " << text.toStdString() << std::endl; Chris@416: Chris@416: QStringList keywords = text.split(' ', QString::SkipEmptyParts); Chris@416: TransformFactory::SearchResults results = Chris@416: TransformFactory::getInstance()->search(keywords); Chris@416: Chris@416: std::cerr << results.size() << " result(s)..." << std::endl; Chris@416: Chris@416: std::set sorted; Chris@416: for (TransformFactory::SearchResults::const_iterator j = results.begin(); Chris@416: j != results.end(); ++j) { Chris@416: sorted.insert(j->second); Chris@416: } Chris@416: Chris@416: int i = 0; Chris@416: /* Chris@416: for (std::set::const_iterator j = sorted.begin(); Chris@416: j != sorted.end(); ++j) { Chris@416: std::cerr << i++ << ": " << j->transform.toStdString() << ": "; Chris@416: for (TransformFactory::Match::FragmentMap::const_iterator k = Chris@416: j->fragments.begin(); Chris@416: k != j->fragments.end(); ++k) { Chris@416: std::cerr << k->first.toStdString() << ": " Chris@416: << k->second.toStdString() << " "; Chris@416: } Chris@416: std::cerr << "(" << j->score << ")" << std::endl; Chris@416: } Chris@416: */ Chris@417: Chris@417: if (!m_resultsLayout) { Chris@417: std::cerr << "creating frame & layout" << std::endl; Chris@417: m_resultsFrame = new QWidget; Chris@417: // resultsFrame->setFrameStyle(QFrame::Sunken | QFrame::Box); Chris@419: m_resultsLayout = new QGridLayout; Chris@417: m_resultsFrame->setLayout(m_resultsLayout); Chris@417: m_resultsScroll->setWidget(m_resultsFrame); Chris@417: m_resultsFrame->show(); Chris@417: } Chris@416: Chris@416: i = 0; Chris@416: int maxResults = 40; Chris@417: int height = 0; Chris@417: int width = 0; Chris@416: Chris@420: if (sorted.empty()) m_selectedTransform = ""; Chris@420: Chris@416: for (std::set::const_iterator j = sorted.end(); Chris@416: j != sorted.begin(); ) { Chris@416: --j; Chris@417: Chris@416: TransformDescription desc = Chris@416: TransformFactory::getInstance()->getTransformDescription(j->transform); Chris@419: Chris@419: QString labelText; Chris@416: labelText += tr("%2
").arg(desc.name); Chris@416: labelText += "..."; Chris@416: for (TransformFactory::Match::FragmentMap::const_iterator k = Chris@416: j->fragments.begin(); Chris@416: k != j->fragments.end(); ++k) { Chris@416: labelText += k->second; Chris@416: labelText += "... "; Chris@416: } Chris@416: labelText += tr(""); Chris@417: Chris@419: QString selectedText; Chris@419: selectedText += tr("%1
").arg(desc.name); Chris@420: selectedText += tr("%1").arg(desc.longDescription); Chris@419: /* Chris@419: for (TransformFactory::Match::FragmentMap::const_iterator k = Chris@419: j->fragments.begin(); Chris@419: k != j->fragments.end(); ++k) { Chris@419: selectedText += tr("
%1: %2").arg(k->first).arg(k->second); Chris@419: } Chris@419: */ Chris@419: Chris@420: selectedText += tr("
    "); Chris@420: selectedText += tr("
  • Plugin type: %1
  • ").arg(desc.type); Chris@420: selectedText += tr("
  • Category: %1
  • ").arg(desc.category); Chris@420: selectedText += tr("
  • System identifier: %1
  • ").arg(desc.identifier); Chris@420: selectedText += tr("
"); Chris@419: Chris@417: if (i >= m_labels.size()) { Chris@419: SelectableLabel *label = new SelectableLabel(m_resultsFrame); Chris@419: m_resultsLayout->addWidget(label, i, 0); Chris@420: connect(label, SIGNAL(selectionChanged()), this, Chris@420: SLOT(selectedLabelChanged())); Chris@417: m_labels.push_back(label); Chris@417: } Chris@420: Chris@420: m_labels[i]->setObjectName(desc.identifier); Chris@420: m_selectedTransform = desc.identifier; Chris@419: m_labels[i]->setUnselectedText(labelText); Chris@419: m_labels[i]->setSelectedText(selectedText); Chris@420: m_labels[i]->setSelected(i == 0); Chris@419: Chris@419: /* Chris@417: QSize sh = m_labels[i]->sizeHint(); Chris@417: std::cerr << "size hint for text \"" << labelText.toStdString() << "\" has height " << sh.height() << std::endl; Chris@417: height += sh.height(); Chris@417: if (sh.width() > width) width = sh.width(); Chris@419: */ Chris@419: // m_labels[i]->resize(m_labels[i]->sizeHint()); Chris@419: // m_labels[i]->updateGeometry(); Chris@417: m_labels[i]->show(); Chris@417: Chris@416: if (++i == maxResults) break; Chris@416: } Chris@416: Chris@417: std::cerr << "m_labels.size() = " << m_labels.size() << ", i = " << i << ", height = " << height << std::endl; Chris@417: Chris@419: for (int j = m_labels.size(); j > i; ) m_labels[--j]->hide(); Chris@417: Chris@419: m_resultsFrame->resize(m_resultsFrame->sizeHint()); Chris@419: // m_resultsFrame->resize(height, width); Chris@416: } Chris@416: Chris@420: void Chris@420: TransformFinder::selectedLabelChanged() Chris@420: { Chris@420: QObject *s = sender(); Chris@420: m_selectedTransform = ""; Chris@420: for (int i = 0; i < m_labels.size(); ++i) { Chris@420: if (!m_labels[i]->isVisible()) continue; Chris@420: if (m_labels[i] == s) { Chris@420: if (m_labels[i]->isSelected()) { Chris@420: m_selectedTransform = m_labels[i]->objectName(); Chris@420: } Chris@420: } else { Chris@420: if (m_labels[i]->isSelected()) { Chris@420: m_labels[i]->setSelected(false); Chris@420: } Chris@420: } Chris@420: } Chris@420: std::cerr << "selectedLabelChanged: selected transform is now \"" Chris@420: << m_selectedTransform.toStdString() << "\"" << std::endl; Chris@420: } Chris@420: Chris@416: TransformId Chris@416: TransformFinder::getTransform() const Chris@416: { Chris@416: return ""; Chris@416: } Chris@416: