comparison widgets/TransformFinder.cpp @ 424:374b72f5b4bb

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