Chris@69: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
Chris@69: 
Chris@69: /*
Chris@69:     EasyMercurial
Chris@69: 
Chris@69:     Based on HgExplorer by Jari Korhonen
Chris@69:     Copyright (c) 2010 Jari Korhonen
Chris@560:     Copyright (c) 2012 Chris Cannam
Chris@560:     Copyright (c) 2012 Queen Mary, University of London
Chris@69:     
Chris@69:     This program is free software; you can redistribute it and/or
Chris@69:     modify it under the terms of the GNU General Public License as
Chris@69:     published by the Free Software Foundation; either version 2 of the
Chris@69:     License, or (at your option) any later version.  See the file
Chris@69:     COPYING included with this distribution for more information.
Chris@69: */
Chris@69: 
Chris@69: #include "selectablelabel.h"
Chris@69: 
Chris@69: #include "debug.h"
Chris@69: 
Chris@69: #include <iostream>
Chris@69: #include <QApplication>
Chris@69: 
Chris@69: SelectableLabel::SelectableLabel(QWidget *p) :
Chris@69:     QLabel(p),
Chris@69:     m_selected(false)
Chris@69: {
Chris@69:     setTextFormat(Qt::RichText);
Chris@69:     setupStyle();
Chris@69:     setOpenExternalLinks(true);
Chris@69: }
Chris@69: 
Chris@69: SelectableLabel::~SelectableLabel()
Chris@69: {
Chris@69: }
Chris@69: 
Chris@69: void
Chris@69: SelectableLabel::setUnselectedText(QString text)
Chris@69: {
Chris@69:     if (m_unselectedText == text) return;
Chris@69:     m_unselectedText = text;
Chris@69:     if (!m_selected) {
Chris@69:         setText(m_unselectedText);
Chris@69:         resize(sizeHint());
Chris@69:     }
Chris@69: }
Chris@69: 
Chris@69: void
Chris@69: SelectableLabel::setSelectedText(QString text)
Chris@69: {
Chris@69:     if (m_selectedText == text) return;
Chris@69:     m_selectedText = text;
Chris@69:     if (m_selected) {
Chris@69:         setText(m_selectedText);
Chris@69:         resize(sizeHint());
Chris@69:     }
Chris@69: }
Chris@69: 
Chris@69: void
Chris@69: SelectableLabel::setupStyle()
Chris@69: {
Chris@69:     QPalette palette = QApplication::palette();
Chris@69: 
Chris@69:     setTextInteractionFlags(Qt::LinksAccessibleByKeyboard |
Chris@69:                             Qt::LinksAccessibleByMouse |
Chris@69:                             Qt::TextSelectableByMouse);
Chris@69: 
Chris@69:     if (m_selected) {
Chris@69:         setStyleSheet
Chris@72:             (QString("QLabel { background: %1; border: 1px solid %2; padding: 7px } ")
Chris@72:              .arg(palette.light().color().name())
Chris@72:              .arg(palette.dark().color().name()));
Chris@69:     } else {
Chris@69:         setStyleSheet
Chris@72:             (QString("QLabel { border: 0; padding: 7px } "));
Chris@69:     }
Chris@69: }    
Chris@69: 
Chris@69: void
Chris@69: SelectableLabel::setSelected(bool s)
Chris@69: {
Chris@69:     if (m_selected == s) return;
Chris@69:     m_selected = s;
Chris@69:     if (m_selected) {
Chris@69:         setText(m_selectedText);
Chris@69:     } else {
Chris@69:         setText(m_unselectedText);
Chris@69:     }
Chris@69:     setupStyle();
Chris@69:     parentWidget()->resize(parentWidget()->sizeHint());
Chris@69: }
Chris@69: 
Chris@69: void
Chris@69: SelectableLabel::toggle()
Chris@69: {
Chris@69:     setSelected(!m_selected);
Chris@69: }
Chris@69: 
Chris@69: void
Chris@69: SelectableLabel::mousePressEvent(QMouseEvent *e)
Chris@69: {
Chris@69:     m_swallowRelease = !m_selected;
Chris@69:     setSelected(true);
Chris@69:     QLabel::mousePressEvent(e);
Chris@69:     emit selectionChanged();
Chris@69: }
Chris@69: 
Chris@69: void
Chris@69: SelectableLabel::mouseDoubleClickEvent(QMouseEvent *e)
Chris@69: {
Chris@69:     QLabel::mouseDoubleClickEvent(e);
Chris@69:     emit doubleClicked();
Chris@69: }
Chris@69: 
Chris@69: void
Chris@69: SelectableLabel::mouseReleaseEvent(QMouseEvent *e)
Chris@69: {
Chris@69:     if (!m_swallowRelease) QLabel::mouseReleaseEvent(e);
Chris@69:     m_swallowRelease = false;
Chris@69: }
Chris@69: 
Chris@69: void
Chris@69: SelectableLabel::enterEvent(QEvent *)
Chris@69: {
Chris@69: //    std::cerr << "enterEvent" << std::endl;
Chris@69: //    QPalette palette = QApplication::palette();
Chris@69: //    palette.setColor(QPalette::Window, Qt::gray);
Chris@69: //    setStyleSheet("background: gray");
Chris@69: //    setPalette(palette);
Chris@69: }
Chris@69: 
Chris@69: void
Chris@69: SelectableLabel::leaveEvent(QEvent *)
Chris@69: {
Chris@69: //    std::cerr << "leaveEvent" << std::endl;
Chris@69: //    setStyleSheet("background: white");
Chris@69: //    QPalette palette = QApplication::palette();
Chris@69: //    palette.setColor(QPalette::Window, Qt::gray);
Chris@69: //    setPalette(palette);
Chris@69: }