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 <iostream>
Chris@424: #include <QApplication>
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@441:     setOpenExternalLinks(true);
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@425:     if (m_unselectedText == text) return;
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@425:     if (m_selectedText == text) return;
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@441:     setTextInteractionFlags(Qt::LinksAccessibleByKeyboard |
Chris@441:                             Qt::LinksAccessibleByMouse |
Chris@441:                             Qt::TextSelectableByMouse);
Chris@441: 
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@425:              .arg(palette.mid().color().lighter(120).name())
Chris@425:              .arg(palette.mid().color().lighter(140).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: }    
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@441:     m_swallowRelease = !m_selected;
Chris@424:     setSelected(true);
Chris@441:     QLabel::mousePressEvent(e);
Chris@424:     emit selectionChanged();
Chris@424: }
Chris@424: 
Chris@424: void
Chris@424: SelectableLabel::mouseDoubleClickEvent(QMouseEvent *e)
Chris@424: {
Chris@441:     QLabel::mouseDoubleClickEvent(e);
Chris@424:     emit doubleClicked();
Chris@424: }
Chris@424: 
Chris@424: void
Chris@441: SelectableLabel::mouseReleaseEvent(QMouseEvent *e)
Chris@441: {
Chris@441:     if (!m_swallowRelease) QLabel::mouseReleaseEvent(e);
Chris@441:     m_swallowRelease = false;
Chris@441: }
Chris@441: 
Chris@441: void
Chris@424: SelectableLabel::enterEvent(QEvent *)
Chris@424: {
Chris@682: //    cerr << "enterEvent" << 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@682: //    cerr << "leaveEvent" << 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: }