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