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@424
|
38 m_unselectedText = text;
|
Chris@424
|
39 if (!m_selected) {
|
Chris@424
|
40 setText(m_unselectedText);
|
Chris@424
|
41 resize(sizeHint());
|
Chris@424
|
42 }
|
Chris@424
|
43 }
|
Chris@424
|
44
|
Chris@424
|
45 void
|
Chris@424
|
46 SelectableLabel::setSelectedText(QString text)
|
Chris@424
|
47 {
|
Chris@424
|
48 m_selectedText = text;
|
Chris@424
|
49 if (m_selected) {
|
Chris@424
|
50 setText(m_selectedText);
|
Chris@424
|
51 resize(sizeHint());
|
Chris@424
|
52 }
|
Chris@424
|
53 }
|
Chris@424
|
54
|
Chris@424
|
55 void
|
Chris@424
|
56 SelectableLabel::setupStyle()
|
Chris@424
|
57 {
|
Chris@424
|
58 QPalette palette = QApplication::palette();
|
Chris@424
|
59
|
Chris@424
|
60 if (m_selected) {
|
Chris@424
|
61 setWordWrap(true);
|
Chris@424
|
62 setStyleSheet
|
Chris@424
|
63 (QString("QLabel:hover { background: %1; color: %3; } "
|
Chris@424
|
64 "QLabel:!hover { background: %2; color: %3 } "
|
Chris@424
|
65 "QLabel { padding: 7px }")
|
Chris@424
|
66 .arg(palette.button().color().name())
|
Chris@424
|
67 .arg(palette.mid().color().light().name())
|
Chris@424
|
68 .arg(palette.text().color().name()));
|
Chris@424
|
69 } else {
|
Chris@424
|
70 setWordWrap(false);
|
Chris@424
|
71 setStyleSheet
|
Chris@424
|
72 (QString("QLabel:hover { background: %1; color: %3; } "
|
Chris@424
|
73 "QLabel:!hover { background: %2; color: %3 } "
|
Chris@424
|
74 "QLabel { padding: 7px }")
|
Chris@424
|
75 .arg(palette.button().color().name())
|
Chris@424
|
76 .arg(palette.light().color().name())
|
Chris@424
|
77 .arg(palette.text().color().name()));
|
Chris@424
|
78
|
Chris@424
|
79 // setStyleSheet("QLabel:hover { background: #e0e0e0; color: black; } QLabel:!hover { background: white; color: black } QLabel { padding: 7px }");
|
Chris@424
|
80 }
|
Chris@424
|
81 }
|
Chris@424
|
82
|
Chris@424
|
83 void
|
Chris@424
|
84 SelectableLabel::setSelected(bool s)
|
Chris@424
|
85 {
|
Chris@424
|
86 if (m_selected == s) return;
|
Chris@424
|
87 m_selected = s;
|
Chris@424
|
88 if (m_selected) {
|
Chris@424
|
89 setText(m_selectedText);
|
Chris@424
|
90 } else {
|
Chris@424
|
91 setText(m_unselectedText);
|
Chris@424
|
92 }
|
Chris@424
|
93 setupStyle();
|
Chris@424
|
94 parentWidget()->resize(parentWidget()->sizeHint());
|
Chris@424
|
95 }
|
Chris@424
|
96
|
Chris@424
|
97 void
|
Chris@424
|
98 SelectableLabel::toggle()
|
Chris@424
|
99 {
|
Chris@424
|
100 setSelected(!m_selected);
|
Chris@424
|
101 }
|
Chris@424
|
102
|
Chris@424
|
103 void
|
Chris@424
|
104 SelectableLabel::mousePressEvent(QMouseEvent *e)
|
Chris@424
|
105 {
|
Chris@424
|
106 setSelected(true);
|
Chris@424
|
107 emit selectionChanged();
|
Chris@424
|
108 }
|
Chris@424
|
109
|
Chris@424
|
110 void
|
Chris@424
|
111 SelectableLabel::mouseDoubleClickEvent(QMouseEvent *e)
|
Chris@424
|
112 {
|
Chris@424
|
113 std::cerr << "mouseDoubleClickEvent" << std::endl;
|
Chris@424
|
114 emit doubleClicked();
|
Chris@424
|
115 }
|
Chris@424
|
116
|
Chris@424
|
117 void
|
Chris@424
|
118 SelectableLabel::enterEvent(QEvent *)
|
Chris@424
|
119 {
|
Chris@424
|
120 // std::cerr << "enterEvent" << std::endl;
|
Chris@424
|
121 // QPalette palette = QApplication::palette();
|
Chris@424
|
122 // palette.setColor(QPalette::Window, Qt::gray);
|
Chris@424
|
123 // setStyleSheet("background: gray");
|
Chris@424
|
124 // setPalette(palette);
|
Chris@424
|
125 }
|
Chris@424
|
126
|
Chris@424
|
127 void
|
Chris@424
|
128 SelectableLabel::leaveEvent(QEvent *)
|
Chris@424
|
129 {
|
Chris@424
|
130 // std::cerr << "leaveEvent" << std::endl;
|
Chris@424
|
131 // setStyleSheet("background: white");
|
Chris@424
|
132 // QPalette palette = QApplication::palette();
|
Chris@424
|
133 // palette.setColor(QPalette::Window, Qt::gray);
|
Chris@424
|
134 // setPalette(palette);
|
Chris@424
|
135 }
|