annotate widgets/TransformFinder.cpp @ 422:ea9e41027b93

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