Repository navigation
Expand file tree
/
Copy pathnavigationmanager.cpp
More file actions
143 lines (117 loc) · 3.05 KB
/
Copy pathnavigationmanager.cpp
File metadata and controls
143 lines (117 loc) · 3.05 KB
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
#include "navigationmanager.h"
NavigationManager::NavigationManager(QObject *parent)
: QObject(parent)
{
}
bool NavigationManager::canGoBack() const
{
return m_historyIndex > 0;
}
bool NavigationManager::canGoForward() const
{
return m_historyIndex >= 0 && m_historyIndex < m_history.size() - 1;
}
QUrl NavigationManager::currentUrl() const
{
if (m_historyIndex >= 0 && m_historyIndex < m_history.size())
return m_history[m_historyIndex];
return {};
}
int NavigationManager::historyCount() const
{
return m_history.size();
}
const QList<QUrl> &NavigationManager::history() const
{
return m_history;
}
int NavigationManager::currentIndex() const
{
return m_historyIndex;
}
void NavigationManager::navigateTo(const QUrl &url)
{
if (!url.isValid() || url.isEmpty())
return;
if (m_historyIndex >= 0 && m_historyIndex < m_history.size()
&& m_history[m_historyIndex] == url) {
emit navigationRequested(url);
return;
}
pushUrl(url);
emit navigationRequested(url);
emitStateChanges();
}
void NavigationManager::goBack()
{
if (!canGoBack())
return;
--m_historyIndex;
emit currentUrlChanged(m_history[m_historyIndex]);
emit navigationRequested(m_history[m_historyIndex]);
emitStateChanges();
}
void NavigationManager::goForward()
{
if (!canGoForward())
return;
++m_historyIndex;
emit currentUrlChanged(m_history[m_historyIndex]);
emit navigationRequested(m_history[m_historyIndex]);
emitStateChanges();
}
void NavigationManager::recordExternalNavigation(const QUrl &url)
{
if (!url.isValid() || url.isEmpty())
return;
if (m_historyIndex >= 0 && m_historyIndex < m_history.size()
&& m_history[m_historyIndex] == url) {
return;
}
pushUrl(url);
emitStateChanges();
}
void NavigationManager::pushUrl(const QUrl &url)
{
if (m_historyIndex < m_history.size() - 1) {
m_history.erase(m_history.begin() + m_historyIndex + 1, m_history.end());
}
m_history.append(url);
m_historyIndex = m_history.size() - 1;
trimHistory();
emit currentUrlChanged(url);
}
void NavigationManager::trimHistory()
{
while (m_history.size() > m_maxHistorySize && m_historyIndex > 0) {
m_history.removeFirst();
--m_historyIndex;
}
while (m_history.size() > m_maxHistorySize)
m_history.removeLast();
}
int NavigationManager::maxHistorySize() const
{
return m_maxHistorySize;
}
void NavigationManager::setMaxHistorySize(int size)
{
m_maxHistorySize = qMax(1, size);
const bool hadBack = m_lastCanGoBack;
trimHistory();
if (hadBack != canGoBack() || m_lastCanGoForward != canGoForward())
emitStateChanges();
}
void NavigationManager::emitStateChanges()
{
const bool back = canGoBack();
const bool fwd = canGoForward();
if (back != m_lastCanGoBack) {
m_lastCanGoBack = back;
emit canGoBackChanged(back);
}
if (fwd != m_lastCanGoForward) {
m_lastCanGoForward = fwd;
emit canGoForwardChanged(fwd);
}
}