Chris@424: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@424: Chris@424: /* Chris@424: Sonic Visualiser Chris@424: An audio file viewer and annotation editor. Chris@424: Centre for Digital Music, Queen Mary, University of London. Chris@424: This file copyright 2008 QMUL. Chris@424: Chris@424: This program is free software; you can redistribute it and/or Chris@424: modify it under the terms of the GNU General Public License as Chris@424: published by the Free Software Foundation; either version 2 of the Chris@424: License, or (at your option) any later version. See the file Chris@424: COPYING included with this distribution for more information. Chris@424: */ Chris@424: Chris@424: #include "SelectableLabel.h" Chris@424: Chris@424: #include Chris@424: #include Chris@424: Chris@424: SelectableLabel::SelectableLabel(QWidget *p) : Chris@424: QLabel(p), Chris@424: m_selected(false) Chris@424: { Chris@424: setTextFormat(Qt::RichText); Chris@424: // setLineWidth(2); Chris@424: // setFixedWidth(480); Chris@424: setupStyle(); Chris@424: } Chris@424: Chris@424: SelectableLabel::~SelectableLabel() Chris@424: { Chris@424: } Chris@424: Chris@424: void Chris@424: SelectableLabel::setUnselectedText(QString text) Chris@424: { Chris@424: m_unselectedText = text; Chris@424: if (!m_selected) { Chris@424: setText(m_unselectedText); Chris@424: resize(sizeHint()); Chris@424: } Chris@424: } Chris@424: Chris@424: void Chris@424: SelectableLabel::setSelectedText(QString text) Chris@424: { Chris@424: m_selectedText = text; Chris@424: if (m_selected) { Chris@424: setText(m_selectedText); Chris@424: resize(sizeHint()); Chris@424: } Chris@424: } Chris@424: Chris@424: void Chris@424: SelectableLabel::setupStyle() Chris@424: { Chris@424: QPalette palette = QApplication::palette(); Chris@424: Chris@424: if (m_selected) { Chris@424: setWordWrap(true); Chris@424: setStyleSheet Chris@424: (QString("QLabel:hover { background: %1; color: %3; } " Chris@424: "QLabel:!hover { background: %2; color: %3 } " Chris@424: "QLabel { padding: 7px }") Chris@424: .arg(palette.button().color().name()) Chris@424: .arg(palette.mid().color().light().name()) Chris@424: .arg(palette.text().color().name())); Chris@424: } else { Chris@424: setWordWrap(false); Chris@424: setStyleSheet Chris@424: (QString("QLabel:hover { background: %1; color: %3; } " Chris@424: "QLabel:!hover { background: %2; color: %3 } " Chris@424: "QLabel { padding: 7px }") Chris@424: .arg(palette.button().color().name()) Chris@424: .arg(palette.light().color().name()) Chris@424: .arg(palette.text().color().name())); Chris@424: Chris@424: // setStyleSheet("QLabel:hover { background: #e0e0e0; color: black; } QLabel:!hover { background: white; color: black } QLabel { padding: 7px }"); Chris@424: } Chris@424: } Chris@424: Chris@424: void Chris@424: SelectableLabel::setSelected(bool s) Chris@424: { Chris@424: if (m_selected == s) return; Chris@424: m_selected = s; Chris@424: if (m_selected) { Chris@424: setText(m_selectedText); Chris@424: } else { Chris@424: setText(m_unselectedText); Chris@424: } Chris@424: setupStyle(); Chris@424: parentWidget()->resize(parentWidget()->sizeHint()); Chris@424: } Chris@424: Chris@424: void Chris@424: SelectableLabel::toggle() Chris@424: { Chris@424: setSelected(!m_selected); Chris@424: } Chris@424: Chris@424: void Chris@424: SelectableLabel::mousePressEvent(QMouseEvent *e) Chris@424: { Chris@424: setSelected(true); Chris@424: emit selectionChanged(); Chris@424: } Chris@424: Chris@424: void Chris@424: SelectableLabel::mouseDoubleClickEvent(QMouseEvent *e) Chris@424: { Chris@424: std::cerr << "mouseDoubleClickEvent" << std::endl; Chris@424: emit doubleClicked(); Chris@424: } Chris@424: Chris@424: void Chris@424: SelectableLabel::enterEvent(QEvent *) Chris@424: { Chris@424: // std::cerr << "enterEvent" << std::endl; Chris@424: // QPalette palette = QApplication::palette(); Chris@424: // palette.setColor(QPalette::Window, Qt::gray); Chris@424: // setStyleSheet("background: gray"); Chris@424: // setPalette(palette); Chris@424: } Chris@424: Chris@424: void Chris@424: SelectableLabel::leaveEvent(QEvent *) Chris@424: { Chris@424: // std::cerr << "leaveEvent" << std::endl; Chris@424: // setStyleSheet("background: white"); Chris@424: // QPalette palette = QApplication::palette(); Chris@424: // palette.setColor(QPalette::Window, Qt::gray); Chris@424: // setPalette(palette); Chris@424: }