SelectableLabel.cpp
Go to the documentation of this file.
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 
22  QLabel(p),
23  m_selected(false)
24 {
25  setTextFormat(Qt::RichText);
26 // setLineWidth(2);
27 // setFixedWidth(480);
28  setupStyle();
29  setOpenExternalLinks(true);
30 }
31 
33 {
34 }
35 
36 void
38 {
39  if (m_unselectedText == text) return;
40  m_unselectedText = text;
41  if (!m_selected) {
42  setText(m_unselectedText);
43  resize(sizeHint());
44  }
45 }
46 
47 void
49 {
50  if (m_selectedText == text) return;
51  m_selectedText = text;
52  if (m_selected) {
53  setText(m_selectedText);
54  resize(sizeHint());
55  }
56 }
57 
58 void
60 {
61  QPalette palette = QApplication::palette();
62 
63  setTextInteractionFlags(Qt::LinksAccessibleByKeyboard |
64  Qt::LinksAccessibleByMouse |
65  Qt::TextSelectableByMouse);
66 
67  if (m_selected) {
68  setWordWrap(true);
69  setStyleSheet
70  (QString("QLabel:hover { background: %1; color: %3; } "
71  "QLabel:!hover { background: %2; color: %3 } "
72  "QLabel { padding: 7px }")
73  .arg(palette.mid().color().lighter(120).name())
74  .arg(palette.mid().color().lighter(140).name())
75  .arg(palette.text().color().name()));
76  } else {
77  setWordWrap(false);
78  setStyleSheet
79  (QString("QLabel:hover { background: %1; color: %3; } "
80  "QLabel:!hover { background: %2; color: %3 } "
81  "QLabel { padding: 7px }")
82  .arg(palette.button().color().name())
83  .arg(palette.light().color().name())
84  .arg(palette.text().color().name()));
85  }
86 }
87 
88 void
90 {
91  if (m_selected == s) return;
92  m_selected = s;
93  if (m_selected) {
94  setText(m_selectedText);
95  } else {
96  setText(m_unselectedText);
97  }
98  setupStyle();
99  parentWidget()->resize(parentWidget()->sizeHint());
100 }
101 
102 void
104 {
106 }
107 
108 void
110 {
112  setSelected(true);
113  QLabel::mousePressEvent(e);
114  emit selectionChanged();
115 }
116 
117 void
119 {
120  QLabel::mouseDoubleClickEvent(e);
121  emit doubleClicked();
122 }
123 
124 void
126 {
127  if (!m_swallowRelease) QLabel::mouseReleaseEvent(e);
128  m_swallowRelease = false;
129 }
130 
131 void
133 {
134 // cerr << "enterEvent" << endl;
135 // QPalette palette = QApplication::palette();
136 // palette.setColor(QPalette::Window, Qt::gray);
137 // setStyleSheet("background: gray");
138 // setPalette(palette);
139 }
140 
141 void
143 {
144 // cerr << "leaveEvent" << endl;
145 // setStyleSheet("background: white");
146 // QPalette palette = QApplication::palette();
147 // palette.setColor(QPalette::Window, Qt::gray);
148 // setPalette(palette);
149 }
void setSelected(bool)
SelectableLabel(QWidget *parent=0)
void enterEvent(QEvent *) override
void mouseDoubleClickEvent(QMouseEvent *e) override
void doubleClicked()
void mouseReleaseEvent(QMouseEvent *e) override
virtual ~SelectableLabel()
QString m_selectedText
void setUnselectedText(QString)
QString m_unselectedText
void setSelectedText(QString)
void mousePressEvent(QMouseEvent *e) override
void selectionChanged()
void leaveEvent(QEvent *) override