annotate widgets/SelectableLabel.cpp @ 1041:fcedcc7f0d87 3.0-integration

Merge from branch "tony-2.0-integration"
author Chris Cannam
date Thu, 04 Feb 2016 11:17:23 +0000
parents 1a0dfcbffaf1
children
rev   line source
Chris@424 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@424 2
Chris@424 3 /*
Chris@424 4 Sonic Visualiser
Chris@424 5 An audio file viewer and annotation editor.
Chris@424 6 Centre for Digital Music, Queen Mary, University of London.
Chris@424 7 This file copyright 2008 QMUL.
Chris@424 8
Chris@424 9 This program is free software; you can redistribute it and/or
Chris@424 10 modify it under the terms of the GNU General Public License as
Chris@424 11 published by the Free Software Foundation; either version 2 of the
Chris@424 12 License, or (at your option) any later version. See the file
Chris@424 13 COPYING included with this distribution for more information.
Chris@424 14 */
Chris@424 15
Chris@424 16 #include "SelectableLabel.h"
Chris@424 17
Chris@424 18 #include <iostream>
Chris@424 19 #include <QApplication>
Chris@424 20
Chris@424 21 SelectableLabel::SelectableLabel(QWidget *p) :
Chris@424 22 QLabel(p),
Chris@424 23 m_selected(false)
Chris@424 24 {
Chris@424 25 setTextFormat(Qt::RichText);
Chris@424 26 // setLineWidth(2);
Chris@424 27 // setFixedWidth(480);
Chris@424 28 setupStyle();
Chris@441 29 setOpenExternalLinks(true);
Chris@424 30 }
Chris@424 31
Chris@424 32 SelectableLabel::~SelectableLabel()
Chris@424 33 {
Chris@424 34 }
Chris@424 35
Chris@424 36 void
Chris@424 37 SelectableLabel::setUnselectedText(QString text)
Chris@424 38 {
Chris@425 39 if (m_unselectedText == text) return;
Chris@424 40 m_unselectedText = text;
Chris@424 41 if (!m_selected) {
Chris@424 42 setText(m_unselectedText);
Chris@424 43 resize(sizeHint());
Chris@424 44 }
Chris@424 45 }
Chris@424 46
Chris@424 47 void
Chris@424 48 SelectableLabel::setSelectedText(QString text)
Chris@424 49 {
Chris@425 50 if (m_selectedText == text) return;
Chris@424 51 m_selectedText = text;
Chris@424 52 if (m_selected) {
Chris@424 53 setText(m_selectedText);
Chris@424 54 resize(sizeHint());
Chris@424 55 }
Chris@424 56 }
Chris@424 57
Chris@424 58 void
Chris@424 59 SelectableLabel::setupStyle()
Chris@424 60 {
Chris@424 61 QPalette palette = QApplication::palette();
Chris@424 62
Chris@441 63 setTextInteractionFlags(Qt::LinksAccessibleByKeyboard |
Chris@441 64 Qt::LinksAccessibleByMouse |
Chris@441 65 Qt::TextSelectableByMouse);
Chris@441 66
Chris@424 67 if (m_selected) {
Chris@424 68 setWordWrap(true);
Chris@424 69 setStyleSheet
Chris@424 70 (QString("QLabel:hover { background: %1; color: %3; } "
Chris@424 71 "QLabel:!hover { background: %2; color: %3 } "
Chris@424 72 "QLabel { padding: 7px }")
Chris@425 73 .arg(palette.mid().color().lighter(120).name())
Chris@425 74 .arg(palette.mid().color().lighter(140).name())
Chris@424 75 .arg(palette.text().color().name()));
Chris@424 76 } else {
Chris@424 77 setWordWrap(false);
Chris@424 78 setStyleSheet
Chris@424 79 (QString("QLabel:hover { background: %1; color: %3; } "
Chris@424 80 "QLabel:!hover { background: %2; color: %3 } "
Chris@424 81 "QLabel { padding: 7px }")
Chris@424 82 .arg(palette.button().color().name())
Chris@424 83 .arg(palette.light().color().name())
Chris@424 84 .arg(palette.text().color().name()));
Chris@424 85 }
Chris@424 86 }
Chris@424 87
Chris@424 88 void
Chris@424 89 SelectableLabel::setSelected(bool s)
Chris@424 90 {
Chris@424 91 if (m_selected == s) return;
Chris@424 92 m_selected = s;
Chris@424 93 if (m_selected) {
Chris@424 94 setText(m_selectedText);
Chris@424 95 } else {
Chris@424 96 setText(m_unselectedText);
Chris@424 97 }
Chris@424 98 setupStyle();
Chris@424 99 parentWidget()->resize(parentWidget()->sizeHint());
Chris@424 100 }
Chris@424 101
Chris@424 102 void
Chris@424 103 SelectableLabel::toggle()
Chris@424 104 {
Chris@424 105 setSelected(!m_selected);
Chris@424 106 }
Chris@424 107
Chris@424 108 void
Chris@424 109 SelectableLabel::mousePressEvent(QMouseEvent *e)
Chris@424 110 {
Chris@441 111 m_swallowRelease = !m_selected;
Chris@424 112 setSelected(true);
Chris@441 113 QLabel::mousePressEvent(e);
Chris@424 114 emit selectionChanged();
Chris@424 115 }
Chris@424 116
Chris@424 117 void
Chris@424 118 SelectableLabel::mouseDoubleClickEvent(QMouseEvent *e)
Chris@424 119 {
Chris@441 120 QLabel::mouseDoubleClickEvent(e);
Chris@424 121 emit doubleClicked();
Chris@424 122 }
Chris@424 123
Chris@424 124 void
Chris@441 125 SelectableLabel::mouseReleaseEvent(QMouseEvent *e)
Chris@441 126 {
Chris@441 127 if (!m_swallowRelease) QLabel::mouseReleaseEvent(e);
Chris@441 128 m_swallowRelease = false;
Chris@441 129 }
Chris@441 130
Chris@441 131 void
Chris@424 132 SelectableLabel::enterEvent(QEvent *)
Chris@424 133 {
Chris@682 134 // cerr << "enterEvent" << endl;
Chris@424 135 // QPalette palette = QApplication::palette();
Chris@424 136 // palette.setColor(QPalette::Window, Qt::gray);
Chris@424 137 // setStyleSheet("background: gray");
Chris@424 138 // setPalette(palette);
Chris@424 139 }
Chris@424 140
Chris@424 141 void
Chris@424 142 SelectableLabel::leaveEvent(QEvent *)
Chris@424 143 {
Chris@682 144 // cerr << "leaveEvent" << endl;
Chris@424 145 // setStyleSheet("background: white");
Chris@424 146 // QPalette palette = QApplication::palette();
Chris@424 147 // palette.setColor(QPalette::Window, Qt::gray);
Chris@424 148 // setPalette(palette);
Chris@424 149 }