comparison src/selectablelabel.cpp @ 370:b9c153e00e84

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