-
Notifications
You must be signed in to change notification settings - Fork 105
/
symbol_tooltip.h
149 lines (127 loc) · 4.44 KB
/
symbol_tooltip.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright 2012, 2013 Thomas Schöps
* Copyright 2017 Kai Pastor
*
* This file is part of OpenOrienteering.
*
* OpenOrienteering is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenOrienteering is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenOrienteering. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPENORIENTEERING_SYMBOL_TOOLTIP_H
#define OPENORIENTEERING_SYMBOL_TOOLTIP_H
#include <QObject>
#include <QRect>
#include <QTimer>
#include <QWidget>
class QEvent;
class QHideEvent;
class QLabel;
class QPaintEvent;
class QShortcut;
class QShowEvent;
// IWYU pragma: no_forward_declare QWidget
// IWYU pragma: no_include <QString>
namespace OpenOrienteering {
class Map;
class Symbol;
/**
* @brief A SymbolToolTip displays the name and description of a symbol.
*
* The tooltip is normally not displayed immediately but scheduled to be
* displayed after a short delay, by a call to scheduleShow(). Initially, the
* tooltip displays the symbol's number and name only. The description can be
* made visible by a call to showDescription().
*
* A QShortcut may be set which will trigger the display of the description
* while the tooltip is already visible. The shortcut does not need to be
* unique: SymbolToolTip enables the shortcut only when the tooltip is visible
* and the description is not yet shown. SymbolToolTip does not take ownership
* of the shortcut.
*
* To permanently hide a SymbolToolTip it is not enough to call hide() because
* the widget may be hidden but scheduled to show up. Call reset() to hide the
* widget and stop the timer if scheduled.
*
* SymbolToolTip does not use the standard tooltip colors but the text editor
* colors.
*/
class SymbolToolTip : public QWidget
{
Q_OBJECT
public:
/**
* Constructs a new SymbolToolTip.
* The optional shortcut will trigger the description to be shown.
*/
SymbolToolTip(QWidget* parent = nullptr, QShortcut* shortcut = nullptr);
/**
* Schedules the tooltip for the symbol to be shown close to
* but not covering the region given by rect.
*
* In mobile mode, tooltip placement relative to the symbol icon is tried
* in the order: above, left, right, below.
* Otherwise, placement is tried in the order: below, above, right, left.
* (desktop mode).
*/
void scheduleShow(const Symbol* symbol, const Map* map, QRect rect, bool mobile_mode);
/**
* Resets the tooltip.
* It hides the widget, stops the timer and disables the shortcut.
*/
void reset();
/**
* Returns the symbol for which the tooltip is currently shown or
* scheduled to be shown, or nullptr.
*/
const Symbol* getSymbol() const;
public slots:
/**
* Expands the symbol's description in the tooltip.
* Disables the shortcut.
*/
void showDescription();
protected:
/**
* Hides the tooltip when the mouse enters it.
* This is necessary to let the user select another symbol.
*/
void enterEvent(QEvent* event) override;
/**
* Enables the shortcut when the tooltip is shown.
*/
void showEvent(QShowEvent* event) override;
/**
* Resets the tooltip's state on hiding the tooltip.
* Disables the shortcut.
*/
void hideEvent(QHideEvent* event) override;
/**
* Draws the tooltip's background.
*/
void paintEvent(QPaintEvent* event) override;
private:
/**
* Moves the tooltip so that it is nicely placed close the region given
* to scheduleShow().
*/
void adjustPosition(bool mobile_mode);
QTimer tooltip_timer; /// The timer which triggers the delayed showing.
QShortcut* shortcut; /// An optional shortcut for showing the description.
const Symbol* symbol; /// The current symbol, or nullptr.
QLabel* name_label; /// The label displaying the symbol's name.
QLabel* description_label; /// The label displaying the symbol's description.
QRect icon_rect; /// The region to be considered when determining position.
bool description_shown; /// If true, the full description is visible.
};
} // namespace OpenOrienteering
#endif