svgui
1.9
|
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ 00002 00003 /* 00004 Sonic Visualiser 00005 An audio file viewer and annotation editor. 00006 Centre for Digital Music, Queen Mary, University of London. 00007 This file copyright 2008 QMUL. 00008 00009 This program is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU General Public License as 00011 published by the Free Software Foundation; either version 2 of the 00012 License, or (at your option) any later version. See the file 00013 COPYING included with this distribution for more information. 00014 */ 00015 00016 #include "SelectableLabel.h" 00017 00018 #include <iostream> 00019 #include <QApplication> 00020 00021 SelectableLabel::SelectableLabel(QWidget *p) : 00022 QLabel(p), 00023 m_selected(false) 00024 { 00025 setTextFormat(Qt::RichText); 00026 // setLineWidth(2); 00027 // setFixedWidth(480); 00028 setupStyle(); 00029 setOpenExternalLinks(true); 00030 } 00031 00032 SelectableLabel::~SelectableLabel() 00033 { 00034 } 00035 00036 void 00037 SelectableLabel::setUnselectedText(QString text) 00038 { 00039 if (m_unselectedText == text) return; 00040 m_unselectedText = text; 00041 if (!m_selected) { 00042 setText(m_unselectedText); 00043 resize(sizeHint()); 00044 } 00045 } 00046 00047 void 00048 SelectableLabel::setSelectedText(QString text) 00049 { 00050 if (m_selectedText == text) return; 00051 m_selectedText = text; 00052 if (m_selected) { 00053 setText(m_selectedText); 00054 resize(sizeHint()); 00055 } 00056 } 00057 00058 void 00059 SelectableLabel::setupStyle() 00060 { 00061 QPalette palette = QApplication::palette(); 00062 00063 setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | 00064 Qt::LinksAccessibleByMouse | 00065 Qt::TextSelectableByMouse); 00066 00067 if (m_selected) { 00068 setWordWrap(true); 00069 setStyleSheet 00070 (QString("QLabel:hover { background: %1; color: %3; } " 00071 "QLabel:!hover { background: %2; color: %3 } " 00072 "QLabel { padding: 7px }") 00073 .arg(palette.mid().color().lighter(120).name()) 00074 .arg(palette.mid().color().lighter(140).name()) 00075 .arg(palette.text().color().name())); 00076 } else { 00077 setWordWrap(false); 00078 setStyleSheet 00079 (QString("QLabel:hover { background: %1; color: %3; } " 00080 "QLabel:!hover { background: %2; color: %3 } " 00081 "QLabel { padding: 7px }") 00082 .arg(palette.button().color().name()) 00083 .arg(palette.light().color().name()) 00084 .arg(palette.text().color().name())); 00085 } 00086 } 00087 00088 void 00089 SelectableLabel::setSelected(bool s) 00090 { 00091 if (m_selected == s) return; 00092 m_selected = s; 00093 if (m_selected) { 00094 setText(m_selectedText); 00095 } else { 00096 setText(m_unselectedText); 00097 } 00098 setupStyle(); 00099 parentWidget()->resize(parentWidget()->sizeHint()); 00100 } 00101 00102 void 00103 SelectableLabel::toggle() 00104 { 00105 setSelected(!m_selected); 00106 } 00107 00108 void 00109 SelectableLabel::mousePressEvent(QMouseEvent *e) 00110 { 00111 m_swallowRelease = !m_selected; 00112 setSelected(true); 00113 QLabel::mousePressEvent(e); 00114 emit selectionChanged(); 00115 } 00116 00117 void 00118 SelectableLabel::mouseDoubleClickEvent(QMouseEvent *e) 00119 { 00120 QLabel::mouseDoubleClickEvent(e); 00121 emit doubleClicked(); 00122 } 00123 00124 void 00125 SelectableLabel::mouseReleaseEvent(QMouseEvent *e) 00126 { 00127 if (!m_swallowRelease) QLabel::mouseReleaseEvent(e); 00128 m_swallowRelease = false; 00129 } 00130 00131 void 00132 SelectableLabel::enterEvent(QEvent *) 00133 { 00134 // cerr << "enterEvent" << endl; 00135 // QPalette palette = QApplication::palette(); 00136 // palette.setColor(QPalette::Window, Qt::gray); 00137 // setStyleSheet("background: gray"); 00138 // setPalette(palette); 00139 } 00140 00141 void 00142 SelectableLabel::leaveEvent(QEvent *) 00143 { 00144 // cerr << "leaveEvent" << endl; 00145 // setStyleSheet("background: white"); 00146 // QPalette palette = QApplication::palette(); 00147 // palette.setColor(QPalette::Window, Qt::gray); 00148 // setPalette(palette); 00149 }