comparison widgets/SelectableLabel.cpp @ 424:374b72f5b4bb

* More SelectableLabel and TransformFinder tweaking
author Chris Cannam
date Mon, 29 Sep 2008 15:06:43 +0000
parents
children 4a5fe5471282
comparison
equal deleted inserted replaced
423:5a55d0683ee5 424:374b72f5b4bb
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2008 QMUL.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #include "SelectableLabel.h"
17
18 #include <iostream>
19 #include <QApplication>
20
21 SelectableLabel::SelectableLabel(QWidget *p) :
22 QLabel(p),
23 m_selected(false)
24 {
25 setTextFormat(Qt::RichText);
26 // setLineWidth(2);
27 // setFixedWidth(480);
28 setupStyle();
29 }
30
31 SelectableLabel::~SelectableLabel()
32 {
33 }
34
35 void
36 SelectableLabel::setUnselectedText(QString text)
37 {
38 m_unselectedText = text;
39 if (!m_selected) {
40 setText(m_unselectedText);
41 resize(sizeHint());
42 }
43 }
44
45 void
46 SelectableLabel::setSelectedText(QString text)
47 {
48 m_selectedText = text;
49 if (m_selected) {
50 setText(m_selectedText);
51 resize(sizeHint());
52 }
53 }
54
55 void
56 SelectableLabel::setupStyle()
57 {
58 QPalette palette = QApplication::palette();
59
60 if (m_selected) {
61 setWordWrap(true);
62 setStyleSheet
63 (QString("QLabel:hover { background: %1; color: %3; } "
64 "QLabel:!hover { background: %2; color: %3 } "
65 "QLabel { padding: 7px }")
66 .arg(palette.button().color().name())
67 .arg(palette.mid().color().light().name())
68 .arg(palette.text().color().name()));
69 } else {
70 setWordWrap(false);
71 setStyleSheet
72 (QString("QLabel:hover { background: %1; color: %3; } "
73 "QLabel:!hover { background: %2; color: %3 } "
74 "QLabel { padding: 7px }")
75 .arg(palette.button().color().name())
76 .arg(palette.light().color().name())
77 .arg(palette.text().color().name()));
78
79 // setStyleSheet("QLabel:hover { background: #e0e0e0; color: black; } QLabel:!hover { background: white; color: black } QLabel { padding: 7px }");
80 }
81 }
82
83 void
84 SelectableLabel::setSelected(bool s)
85 {
86 if (m_selected == s) return;
87 m_selected = s;
88 if (m_selected) {
89 setText(m_selectedText);
90 } else {
91 setText(m_unselectedText);
92 }
93 setupStyle();
94 parentWidget()->resize(parentWidget()->sizeHint());
95 }
96
97 void
98 SelectableLabel::toggle()
99 {
100 setSelected(!m_selected);
101 }
102
103 void
104 SelectableLabel::mousePressEvent(QMouseEvent *e)
105 {
106 setSelected(true);
107 emit selectionChanged();
108 }
109
110 void
111 SelectableLabel::mouseDoubleClickEvent(QMouseEvent *e)
112 {
113 std::cerr << "mouseDoubleClickEvent" << std::endl;
114 emit doubleClicked();
115 }
116
117 void
118 SelectableLabel::enterEvent(QEvent *)
119 {
120 // std::cerr << "enterEvent" << std::endl;
121 // QPalette palette = QApplication::palette();
122 // palette.setColor(QPalette::Window, Qt::gray);
123 // setStyleSheet("background: gray");
124 // setPalette(palette);
125 }
126
127 void
128 SelectableLabel::leaveEvent(QEvent *)
129 {
130 // std::cerr << "leaveEvent" << std::endl;
131 // setStyleSheet("background: white");
132 // QPalette palette = QApplication::palette();
133 // palette.setColor(QPalette::Window, Qt::gray);
134 // setPalette(palette);
135 }