annotate selectablelabel.cpp @ 69:6d5a5571caec

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