comparison selectablelabel.cpp @ 69:6d5a5571caec

* Basic working "Open Repository" dialog
author Chris Cannam
date Thu, 18 Nov 2010 15:14:39 +0000
parents
children 121cb1032717
comparison
equal deleted inserted replaced
68:b918e57c7bea 69:6d5a5571caec
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) 2010 Chris Cannam
9 Copyright (c) 2010 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 // setLineWidth(2);
31 // setFixedWidth(480);
32 setupStyle();
33 setOpenExternalLinks(true);
34 }
35
36 SelectableLabel::~SelectableLabel()
37 {
38 }
39
40 void
41 SelectableLabel::setUnselectedText(QString text)
42 {
43 if (m_unselectedText == text) return;
44 m_unselectedText = text;
45 if (!m_selected) {
46 setText(m_unselectedText);
47 resize(sizeHint());
48 }
49 }
50
51 void
52 SelectableLabel::setSelectedText(QString text)
53 {
54 if (m_selectedText == text) return;
55 m_selectedText = text;
56 if (m_selected) {
57 setText(m_selectedText);
58 resize(sizeHint());
59 }
60 }
61
62 void
63 SelectableLabel::setupStyle()
64 {
65 QPalette palette = QApplication::palette();
66
67 setTextInteractionFlags(Qt::LinksAccessibleByKeyboard |
68 Qt::LinksAccessibleByMouse |
69 Qt::TextSelectableByMouse);
70
71 if (m_selected) {
72 setStyleSheet
73 (QString("QLabel:hover { background: %1; color: %3; } "
74 "QLabel:!hover { background: %2; color: %3 } "
75 "QLabel { padding: 7px }")
76 .arg(palette.highlight().color().name())
77 .arg(palette.highlight().color().name())
78 .arg(palette.text().color().name()));
79 } else {
80 setStyleSheet
81 (QString("QLabel:hover { background: %1; color: %3; } "
82 "QLabel:!hover { background: %2; color: %3 } "
83 "QLabel { padding: 7px }")
84 .arg(palette.button().color().name())
85 .arg(palette.light().color().name())
86 .arg(palette.text().color().name()));
87 }
88 }
89
90 void
91 SelectableLabel::setSelected(bool s)
92 {
93 if (m_selected == s) return;
94 m_selected = s;
95 if (m_selected) {
96 setText(m_selectedText);
97 } else {
98 setText(m_unselectedText);
99 }
100 setupStyle();
101 parentWidget()->resize(parentWidget()->sizeHint());
102 }
103
104 void
105 SelectableLabel::toggle()
106 {
107 setSelected(!m_selected);
108 }
109
110 void
111 SelectableLabel::mousePressEvent(QMouseEvent *e)
112 {
113 m_swallowRelease = !m_selected;
114 setSelected(true);
115 QLabel::mousePressEvent(e);
116 emit selectionChanged();
117 }
118
119 void
120 SelectableLabel::mouseDoubleClickEvent(QMouseEvent *e)
121 {
122 QLabel::mouseDoubleClickEvent(e);
123 emit doubleClicked();
124 }
125
126 void
127 SelectableLabel::mouseReleaseEvent(QMouseEvent *e)
128 {
129 if (!m_swallowRelease) QLabel::mouseReleaseEvent(e);
130 m_swallowRelease = false;
131 }
132
133 void
134 SelectableLabel::enterEvent(QEvent *)
135 {
136 // std::cerr << "enterEvent" << std::endl;
137 // QPalette palette = QApplication::palette();
138 // palette.setColor(QPalette::Window, Qt::gray);
139 // setStyleSheet("background: gray");
140 // setPalette(palette);
141 }
142
143 void
144 SelectableLabel::leaveEvent(QEvent *)
145 {
146 // std::cerr << "leaveEvent" << std::endl;
147 // setStyleSheet("background: white");
148 // QPalette palette = QApplication::palette();
149 // palette.setColor(QPalette::Window, Qt::gray);
150 // setPalette(palette);
151 }