annotate widgets/TransformFinder.cpp @ 421:7466402a32c5

* moar pointless twiddlin
author Chris Cannam
date Fri, 26 Sep 2008 16:18:36 +0000
parents f8e04ad8e7b1
children ea9e41027b93
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@421 92 SelectableLabel::enterEvent(QEvent *)
Chris@421 93 {
Chris@421 94 // std::cerr << "enterEvent" << std::endl;
Chris@421 95 // QPalette palette = QApplication::palette();
Chris@421 96 // palette.setColor(QPalette::Window, Qt::gray);
Chris@421 97 // setStyleSheet("background: gray");
Chris@421 98 // setPalette(palette);
Chris@421 99 }
Chris@421 100
Chris@421 101 void
Chris@421 102 SelectableLabel::leaveEvent(QEvent *)
Chris@421 103 {
Chris@421 104 // std::cerr << "leaveEvent" << std::endl;
Chris@421 105 // setStyleSheet("background: white");
Chris@421 106 // QPalette palette = QApplication::palette();
Chris@421 107 // palette.setColor(QPalette::Window, Qt::gray);
Chris@421 108 // setPalette(palette);
Chris@421 109 }
Chris@421 110
Chris@416 111 TransformFinder::TransformFinder(QWidget *parent) :
Chris@417 112 QDialog(parent),
Chris@417 113 m_resultsFrame(0),
Chris@417 114 m_resultsLayout(0)
Chris@416 115 {
Chris@416 116 setWindowTitle(tr("Find a Transform"));
Chris@416 117
Chris@416 118 QGridLayout *mainGrid = new QGridLayout;
Chris@421 119 mainGrid->setVerticalSpacing(0);
Chris@416 120 setLayout(mainGrid);
Chris@416 121
Chris@416 122 mainGrid->addWidget(new QLabel(tr("Find:")), 0, 0);
Chris@416 123
Chris@416 124 QLineEdit *searchField = new QLineEdit;
Chris@416 125 mainGrid->addWidget(searchField, 0, 1);
Chris@416 126 connect(searchField, SIGNAL(textChanged(const QString &)),
Chris@416 127 this, SLOT(searchTextChanged(const QString &)));
Chris@416 128
Chris@416 129 m_resultsScroll = new QScrollArea;
Chris@420 130 // m_resultsScroll->setWidgetResizable(true);
Chris@416 131 mainGrid->addWidget(m_resultsScroll, 1, 0, 1, 2);
Chris@416 132 mainGrid->setRowStretch(1, 10);
Chris@416 133
Chris@416 134 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok |
Chris@416 135 QDialogButtonBox::Cancel);
Chris@416 136 mainGrid->addWidget(bb, 2, 0, 1, 2);
Chris@416 137 connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
Chris@416 138 connect(bb, SIGNAL(rejected()), this, SLOT(reject()));
Chris@421 139 if (!m_resultsLayout) {
Chris@421 140 std::cerr << "creating frame & layout" << std::endl;
Chris@421 141 m_resultsFrame = new QWidget;
Chris@421 142 QPalette palette = m_resultsFrame->palette();
Chris@421 143 palette.setColor(QPalette::Window, palette.color(QPalette::Base));
Chris@421 144 m_resultsFrame->setPalette(palette);
Chris@421 145 m_resultsScroll->setPalette(palette);
Chris@421 146 // resultsFrame->setFrameStyle(QFrame::Sunken | QFrame::Box);
Chris@421 147 m_resultsLayout = new QVBoxLayout;
Chris@421 148 m_resultsFrame->setLayout(m_resultsLayout);
Chris@421 149 m_resultsScroll->setWidget(m_resultsFrame);
Chris@421 150 m_resultsFrame->show();
Chris@421 151 }
Chris@416 152
Chris@416 153 resize(500, 400); //!!!
Chris@421 154 m_resultsFrame->resize(480, 380);
Chris@416 155 }
Chris@416 156
Chris@416 157 TransformFinder::~TransformFinder()
Chris@416 158 {
Chris@416 159 }
Chris@416 160
Chris@416 161 void
Chris@416 162 TransformFinder::searchTextChanged(const QString &text)
Chris@416 163 {
Chris@416 164 std::cerr << "text is " << text.toStdString() << std::endl;
Chris@416 165
Chris@416 166 QStringList keywords = text.split(' ', QString::SkipEmptyParts);
Chris@416 167 TransformFactory::SearchResults results =
Chris@416 168 TransformFactory::getInstance()->search(keywords);
Chris@416 169
Chris@416 170 std::cerr << results.size() << " result(s)..." << std::endl;
Chris@416 171
Chris@416 172 std::set<TransformFactory::Match> sorted;
Chris@416 173 for (TransformFactory::SearchResults::const_iterator j = results.begin();
Chris@416 174 j != results.end(); ++j) {
Chris@416 175 sorted.insert(j->second);
Chris@416 176 }
Chris@416 177
Chris@416 178 int i = 0;
Chris@416 179 /*
Chris@416 180 for (std::set<TransformFactory::Match>::const_iterator j = sorted.begin();
Chris@416 181 j != sorted.end(); ++j) {
Chris@416 182 std::cerr << i++ << ": " << j->transform.toStdString() << ": ";
Chris@416 183 for (TransformFactory::Match::FragmentMap::const_iterator k =
Chris@416 184 j->fragments.begin();
Chris@416 185 k != j->fragments.end(); ++k) {
Chris@416 186 std::cerr << k->first.toStdString() << ": "
Chris@416 187 << k->second.toStdString() << " ";
Chris@416 188 }
Chris@416 189 std::cerr << "(" << j->score << ")" << std::endl;
Chris@416 190 }
Chris@416 191 */
Chris@417 192
Chris@416 193 i = 0;
Chris@416 194 int maxResults = 40;
Chris@417 195 int height = 0;
Chris@417 196 int width = 0;
Chris@416 197
Chris@420 198 if (sorted.empty()) m_selectedTransform = "";
Chris@420 199
Chris@416 200 for (std::set<TransformFactory::Match>::const_iterator j = sorted.end();
Chris@416 201 j != sorted.begin(); ) {
Chris@416 202 --j;
Chris@417 203
Chris@416 204 TransformDescription desc =
Chris@416 205 TransformFactory::getInstance()->getTransformDescription(j->transform);
Chris@419 206
Chris@419 207 QString labelText;
Chris@416 208 labelText += tr("%2<br><small>").arg(desc.name);
Chris@416 209 labelText += "...";
Chris@416 210 for (TransformFactory::Match::FragmentMap::const_iterator k =
Chris@416 211 j->fragments.begin();
Chris@416 212 k != j->fragments.end(); ++k) {
Chris@416 213 labelText += k->second;
Chris@416 214 labelText += "... ";
Chris@416 215 }
Chris@416 216 labelText += tr("</small>");
Chris@417 217
Chris@419 218 QString selectedText;
Chris@419 219 selectedText += tr("<b>%1</b><br>").arg(desc.name);
Chris@420 220 selectedText += tr("<small>%1</small>").arg(desc.longDescription);
Chris@419 221 /*
Chris@419 222 for (TransformFactory::Match::FragmentMap::const_iterator k =
Chris@419 223 j->fragments.begin();
Chris@419 224 k != j->fragments.end(); ++k) {
Chris@419 225 selectedText += tr("<br><small>%1: %2</small>").arg(k->first).arg(k->second);
Chris@419 226 }
Chris@419 227 */
Chris@419 228
Chris@420 229 selectedText += tr("<ul><small>");
Chris@420 230 selectedText += tr("<li>Plugin type: %1</li>").arg(desc.type);
Chris@420 231 selectedText += tr("<li>Category: %1</li>").arg(desc.category);
Chris@420 232 selectedText += tr("<li>System identifier: %1</li>").arg(desc.identifier);
Chris@420 233 selectedText += tr("</small></ul>");
Chris@419 234
Chris@417 235 if (i >= m_labels.size()) {
Chris@419 236 SelectableLabel *label = new SelectableLabel(m_resultsFrame);
Chris@421 237 // m_resultsLayout->addWidget(label, i, 0);
Chris@421 238 m_resultsLayout->addWidget(label);
Chris@420 239 connect(label, SIGNAL(selectionChanged()), this,
Chris@420 240 SLOT(selectedLabelChanged()));
Chris@421 241 QPalette palette = label->palette();
Chris@421 242 label->setPalette(palette);
Chris@417 243 m_labels.push_back(label);
Chris@417 244 }
Chris@421 245
Chris@420 246 m_labels[i]->setObjectName(desc.identifier);
Chris@420 247 m_selectedTransform = desc.identifier;
Chris@419 248 m_labels[i]->setUnselectedText(labelText);
Chris@419 249 m_labels[i]->setSelectedText(selectedText);
Chris@420 250 m_labels[i]->setSelected(i == 0);
Chris@419 251
Chris@419 252 /*
Chris@417 253 QSize sh = m_labels[i]->sizeHint();
Chris@417 254 std::cerr << "size hint for text \"" << labelText.toStdString() << "\" has height " << sh.height() << std::endl;
Chris@417 255 height += sh.height();
Chris@417 256 if (sh.width() > width) width = sh.width();
Chris@419 257 */
Chris@419 258 // m_labels[i]->resize(m_labels[i]->sizeHint());
Chris@419 259 // m_labels[i]->updateGeometry();
Chris@417 260 m_labels[i]->show();
Chris@417 261
Chris@416 262 if (++i == maxResults) break;
Chris@416 263 }
Chris@416 264
Chris@417 265 std::cerr << "m_labels.size() = " << m_labels.size() << ", i = " << i << ", height = " << height << std::endl;
Chris@417 266
Chris@419 267 for (int j = m_labels.size(); j > i; ) m_labels[--j]->hide();
Chris@417 268
Chris@419 269 m_resultsFrame->resize(m_resultsFrame->sizeHint());
Chris@419 270 // m_resultsFrame->resize(height, width);
Chris@416 271 }
Chris@416 272
Chris@420 273 void
Chris@420 274 TransformFinder::selectedLabelChanged()
Chris@420 275 {
Chris@420 276 QObject *s = sender();
Chris@420 277 m_selectedTransform = "";
Chris@420 278 for (int i = 0; i < m_labels.size(); ++i) {
Chris@420 279 if (!m_labels[i]->isVisible()) continue;
Chris@420 280 if (m_labels[i] == s) {
Chris@420 281 if (m_labels[i]->isSelected()) {
Chris@420 282 m_selectedTransform = m_labels[i]->objectName();
Chris@420 283 }
Chris@420 284 } else {
Chris@420 285 if (m_labels[i]->isSelected()) {
Chris@420 286 m_labels[i]->setSelected(false);
Chris@420 287 }
Chris@420 288 }
Chris@420 289 }
Chris@420 290 std::cerr << "selectedLabelChanged: selected transform is now \""
Chris@420 291 << m_selectedTransform.toStdString() << "\"" << std::endl;
Chris@420 292 }
Chris@420 293
Chris@416 294 TransformId
Chris@416 295 TransformFinder::getTransform() const
Chris@416 296 {
Chris@416 297 return "";
Chris@416 298 }
Chris@416 299