annotate widgets/TransformFinder.cpp @ 423:5a55d0683ee5

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