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@560
|
8 Copyright (c) 2012 Chris Cannam
|
Chris@560
|
9 Copyright (c) 2012 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 setupStyle();
|
Chris@69
|
31 setOpenExternalLinks(true);
|
Chris@69
|
32 }
|
Chris@69
|
33
|
Chris@69
|
34 SelectableLabel::~SelectableLabel()
|
Chris@69
|
35 {
|
Chris@69
|
36 }
|
Chris@69
|
37
|
Chris@69
|
38 void
|
Chris@69
|
39 SelectableLabel::setUnselectedText(QString text)
|
Chris@69
|
40 {
|
Chris@69
|
41 if (m_unselectedText == text) return;
|
Chris@69
|
42 m_unselectedText = text;
|
Chris@69
|
43 if (!m_selected) {
|
Chris@69
|
44 setText(m_unselectedText);
|
Chris@69
|
45 resize(sizeHint());
|
Chris@69
|
46 }
|
Chris@69
|
47 }
|
Chris@69
|
48
|
Chris@69
|
49 void
|
Chris@69
|
50 SelectableLabel::setSelectedText(QString text)
|
Chris@69
|
51 {
|
Chris@69
|
52 if (m_selectedText == text) return;
|
Chris@69
|
53 m_selectedText = text;
|
Chris@69
|
54 if (m_selected) {
|
Chris@69
|
55 setText(m_selectedText);
|
Chris@69
|
56 resize(sizeHint());
|
Chris@69
|
57 }
|
Chris@69
|
58 }
|
Chris@69
|
59
|
Chris@69
|
60 void
|
Chris@69
|
61 SelectableLabel::setupStyle()
|
Chris@69
|
62 {
|
Chris@69
|
63 QPalette palette = QApplication::palette();
|
Chris@69
|
64
|
Chris@69
|
65 setTextInteractionFlags(Qt::LinksAccessibleByKeyboard |
|
Chris@69
|
66 Qt::LinksAccessibleByMouse |
|
Chris@69
|
67 Qt::TextSelectableByMouse);
|
Chris@69
|
68
|
Chris@69
|
69 if (m_selected) {
|
Chris@69
|
70 setStyleSheet
|
Chris@72
|
71 (QString("QLabel { background: %1; border: 1px solid %2; padding: 7px } ")
|
Chris@72
|
72 .arg(palette.light().color().name())
|
Chris@72
|
73 .arg(palette.dark().color().name()));
|
Chris@69
|
74 } else {
|
Chris@69
|
75 setStyleSheet
|
Chris@72
|
76 (QString("QLabel { border: 0; padding: 7px } "));
|
Chris@69
|
77 }
|
Chris@69
|
78 }
|
Chris@69
|
79
|
Chris@69
|
80 void
|
Chris@69
|
81 SelectableLabel::setSelected(bool s)
|
Chris@69
|
82 {
|
Chris@69
|
83 if (m_selected == s) return;
|
Chris@69
|
84 m_selected = s;
|
Chris@69
|
85 if (m_selected) {
|
Chris@69
|
86 setText(m_selectedText);
|
Chris@69
|
87 } else {
|
Chris@69
|
88 setText(m_unselectedText);
|
Chris@69
|
89 }
|
Chris@69
|
90 setupStyle();
|
Chris@69
|
91 parentWidget()->resize(parentWidget()->sizeHint());
|
Chris@69
|
92 }
|
Chris@69
|
93
|
Chris@69
|
94 void
|
Chris@69
|
95 SelectableLabel::toggle()
|
Chris@69
|
96 {
|
Chris@69
|
97 setSelected(!m_selected);
|
Chris@69
|
98 }
|
Chris@69
|
99
|
Chris@69
|
100 void
|
Chris@69
|
101 SelectableLabel::mousePressEvent(QMouseEvent *e)
|
Chris@69
|
102 {
|
Chris@69
|
103 m_swallowRelease = !m_selected;
|
Chris@69
|
104 setSelected(true);
|
Chris@69
|
105 QLabel::mousePressEvent(e);
|
Chris@69
|
106 emit selectionChanged();
|
Chris@69
|
107 }
|
Chris@69
|
108
|
Chris@69
|
109 void
|
Chris@69
|
110 SelectableLabel::mouseDoubleClickEvent(QMouseEvent *e)
|
Chris@69
|
111 {
|
Chris@69
|
112 QLabel::mouseDoubleClickEvent(e);
|
Chris@69
|
113 emit doubleClicked();
|
Chris@69
|
114 }
|
Chris@69
|
115
|
Chris@69
|
116 void
|
Chris@69
|
117 SelectableLabel::mouseReleaseEvent(QMouseEvent *e)
|
Chris@69
|
118 {
|
Chris@69
|
119 if (!m_swallowRelease) QLabel::mouseReleaseEvent(e);
|
Chris@69
|
120 m_swallowRelease = false;
|
Chris@69
|
121 }
|
Chris@69
|
122
|
Chris@69
|
123 void
|
Chris@69
|
124 SelectableLabel::enterEvent(QEvent *)
|
Chris@69
|
125 {
|
Chris@69
|
126 // std::cerr << "enterEvent" << std::endl;
|
Chris@69
|
127 // QPalette palette = QApplication::palette();
|
Chris@69
|
128 // palette.setColor(QPalette::Window, Qt::gray);
|
Chris@69
|
129 // setStyleSheet("background: gray");
|
Chris@69
|
130 // setPalette(palette);
|
Chris@69
|
131 }
|
Chris@69
|
132
|
Chris@69
|
133 void
|
Chris@69
|
134 SelectableLabel::leaveEvent(QEvent *)
|
Chris@69
|
135 {
|
Chris@69
|
136 // std::cerr << "leaveEvent" << std::endl;
|
Chris@69
|
137 // setStyleSheet("background: white");
|
Chris@69
|
138 // QPalette palette = QApplication::palette();
|
Chris@69
|
139 // palette.setColor(QPalette::Window, Qt::gray);
|
Chris@69
|
140 // setPalette(palette);
|
Chris@69
|
141 }
|