1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Assistant module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include "globalactions.h" |
30 | |
31 | #include "centralwidget.h" |
32 | #include "helpviewer.h" |
33 | #include "tracer.h" |
34 | |
35 | #include <QtWidgets/QAction> |
36 | #include <QtWidgets/QMenu> |
37 | |
38 | #if defined(BROWSER_QTWEBKIT) |
39 | # include <QWebHistory> |
40 | #endif |
41 | |
42 | GlobalActions *GlobalActions::instance(QObject *parent) |
43 | { |
44 | Q_ASSERT(!m_instance != !parent); |
45 | if (!m_instance) |
46 | m_instance = new GlobalActions(parent); |
47 | return m_instance; |
48 | } |
49 | |
50 | GlobalActions::GlobalActions(QObject *parent) : QObject(parent) |
51 | { |
52 | TRACE_OBJ |
53 | |
54 | // TODO: Put resource path in misc class |
55 | QString resourcePath = QLatin1String(":/qt-project.org/assistant/images/" ); |
56 | #ifdef Q_OS_MAC |
57 | resourcePath.append(QLatin1String("mac" )); |
58 | #else |
59 | resourcePath.append(s: QLatin1String("win" )); |
60 | #endif |
61 | CentralWidget *centralWidget = CentralWidget::instance(); |
62 | |
63 | m_backAction = new QAction(tr(s: "&Back" ), parent); |
64 | m_backAction->setEnabled(false); |
65 | m_backAction->setShortcuts(QKeySequence::Back); |
66 | m_backAction->setIcon(QIcon(resourcePath + QLatin1String("/previous.png" ))); |
67 | connect(sender: m_backAction, signal: &QAction::triggered, receiver: centralWidget, slot: &CentralWidget::backward); |
68 | m_actionList << m_backAction; |
69 | |
70 | m_nextAction = new QAction(tr(s: "&Forward" ), parent); |
71 | m_nextAction->setPriority(QAction::LowPriority); |
72 | m_nextAction->setEnabled(false); |
73 | m_nextAction->setShortcuts(QKeySequence::Forward); |
74 | m_nextAction->setIcon(QIcon(resourcePath + QLatin1String("/next.png" ))); |
75 | connect(sender: m_nextAction, signal: &QAction::triggered, receiver: centralWidget, slot: &CentralWidget::forward); |
76 | m_actionList << m_nextAction; |
77 | |
78 | setupNavigationMenus(back: m_backAction, next: m_nextAction, parent: centralWidget); |
79 | |
80 | m_homeAction = new QAction(tr(s: "&Home" ), parent); |
81 | m_homeAction->setShortcut(tr(s: "ALT+Home" )); |
82 | m_homeAction->setIcon(QIcon(resourcePath + QLatin1String("/home.png" ))); |
83 | connect(sender: m_homeAction, signal: &QAction::triggered, receiver: centralWidget, slot: &CentralWidget::home); |
84 | m_actionList << m_homeAction; |
85 | |
86 | QAction *separator = new QAction(parent); |
87 | separator->setSeparator(true); |
88 | m_actionList << separator; |
89 | |
90 | m_zoomInAction = new QAction(tr(s: "Zoom &in" ), parent); |
91 | m_zoomInAction->setPriority(QAction::LowPriority); |
92 | m_zoomInAction->setIcon(QIcon(resourcePath + QLatin1String("/zoomin.png" ))); |
93 | m_zoomInAction->setShortcut(QKeySequence::ZoomIn); |
94 | connect(sender: m_zoomInAction, signal: &QAction::triggered, receiver: centralWidget, slot: &CentralWidget::zoomIn); |
95 | m_actionList << m_zoomInAction; |
96 | |
97 | m_zoomOutAction = new QAction(tr(s: "Zoom &out" ), parent); |
98 | m_zoomOutAction->setPriority(QAction::LowPriority); |
99 | m_zoomOutAction->setIcon(QIcon(resourcePath + QLatin1String("/zoomout.png" ))); |
100 | m_zoomOutAction->setShortcut(QKeySequence::ZoomOut); |
101 | connect(sender: m_zoomOutAction, signal: &QAction::triggered, receiver: centralWidget, slot: &CentralWidget::zoomOut); |
102 | m_actionList << m_zoomOutAction; |
103 | |
104 | separator = new QAction(parent); |
105 | separator->setSeparator(true); |
106 | m_actionList << separator; |
107 | |
108 | #if QT_CONFIG(clipboard) |
109 | m_copyAction = new QAction(tr(s: "&Copy selected Text" ), parent); |
110 | m_copyAction->setPriority(QAction::LowPriority); |
111 | m_copyAction->setIconText("&Copy" ); |
112 | m_copyAction->setIcon(QIcon(resourcePath + QLatin1String("/editcopy.png" ))); |
113 | m_copyAction->setShortcuts(QKeySequence::Copy); |
114 | m_copyAction->setEnabled(false); |
115 | connect(sender: m_copyAction, signal: &QAction::triggered, receiver: centralWidget, slot: &CentralWidget::copy); |
116 | m_actionList << m_copyAction; |
117 | #endif |
118 | |
119 | m_printAction = new QAction(tr(s: "&Print..." ), parent); |
120 | m_printAction->setPriority(QAction::LowPriority); |
121 | m_printAction->setIcon(QIcon(resourcePath + QLatin1String("/print.png" ))); |
122 | m_printAction->setShortcut(QKeySequence::Print); |
123 | connect(sender: m_printAction, signal: &QAction::triggered, receiver: centralWidget, slot: &CentralWidget::print); |
124 | m_actionList << m_printAction; |
125 | |
126 | m_findAction = new QAction(tr(s: "&Find in Text..." ), parent); |
127 | m_findAction->setIconText(tr(s: "&Find" )); |
128 | m_findAction->setIcon(QIcon(resourcePath + QLatin1String("/find.png" ))); |
129 | m_findAction->setShortcuts(QKeySequence::Find); |
130 | connect(sender: m_findAction, signal: &QAction::triggered, receiver: centralWidget, slot: &CentralWidget::showTextSearch); |
131 | m_actionList << m_findAction; |
132 | |
133 | #if defined (Q_OS_UNIX) && !defined(Q_OS_MAC) |
134 | m_backAction->setIcon(QIcon::fromTheme(QStringLiteral("go-previous" ) , fallback: m_backAction->icon())); |
135 | m_nextAction->setIcon(QIcon::fromTheme(QStringLiteral("go-next" ) , fallback: m_nextAction->icon())); |
136 | m_zoomInAction->setIcon(QIcon::fromTheme(QStringLiteral("zoom-in" ) , fallback: m_zoomInAction->icon())); |
137 | m_zoomOutAction->setIcon(QIcon::fromTheme(QStringLiteral("zoom-out" ) , fallback: m_zoomOutAction->icon())); |
138 | #if QT_CONFIG(clipboard) |
139 | m_copyAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy" ) , fallback: m_copyAction->icon())); |
140 | #endif |
141 | m_findAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-find" ) , fallback: m_findAction->icon())); |
142 | m_homeAction->setIcon(QIcon::fromTheme(QStringLiteral("go-home" ) , fallback: m_homeAction->icon())); |
143 | m_printAction->setIcon(QIcon::fromTheme(QStringLiteral("document-print" ) , fallback: m_printAction->icon())); |
144 | #endif |
145 | } |
146 | |
147 | void GlobalActions::updateActions() |
148 | { |
149 | TRACE_OBJ |
150 | CentralWidget *centralWidget = CentralWidget::instance(); |
151 | #if QT_CONFIG(clipboard) |
152 | m_copyAction->setEnabled(centralWidget->hasSelection()); |
153 | #endif |
154 | m_nextAction->setEnabled(centralWidget->isForwardAvailable()); |
155 | m_backAction->setEnabled(centralWidget->isBackwardAvailable()); |
156 | } |
157 | |
158 | #if QT_CONFIG(clipboard) |
159 | void GlobalActions::setCopyAvailable(bool available) |
160 | { |
161 | TRACE_OBJ |
162 | m_copyAction->setEnabled(available); |
163 | } |
164 | #endif |
165 | |
166 | #if defined(BROWSER_QTWEBKIT) |
167 | |
168 | void GlobalActions::slotAboutToShowBackMenu() |
169 | { |
170 | TRACE_OBJ |
171 | m_backMenu->clear(); |
172 | if (QWebHistory *history = CentralWidget::instance()->currentHelpViewer()->history()) { |
173 | const int currentItemIndex = history->currentItemIndex(); |
174 | QList<QWebHistoryItem> items = history->backItems(history->count()); |
175 | for (int i = items.count() - 1; i >= 0; --i) { |
176 | QAction *action = new QAction(this); |
177 | action->setText(items.at(i).title()); |
178 | action->setData(-1 * (currentItemIndex - i)); |
179 | m_backMenu->addAction(action); |
180 | } |
181 | } |
182 | } |
183 | |
184 | void GlobalActions::slotAboutToShowNextMenu() |
185 | { |
186 | TRACE_OBJ |
187 | m_nextMenu->clear(); |
188 | if (QWebHistory *history = CentralWidget::instance()->currentHelpViewer()->history()) { |
189 | const int count = history->count(); |
190 | QList<QWebHistoryItem> items = history->forwardItems(count); |
191 | for (int i = 0; i < items.count(); ++i) { |
192 | QAction *action = new QAction(this); |
193 | action->setData(count - i); |
194 | action->setText(items.at(i).title()); |
195 | m_nextMenu->addAction(action); |
196 | } |
197 | } |
198 | } |
199 | |
200 | void GlobalActions::slotOpenActionUrl(QAction *action) |
201 | { |
202 | TRACE_OBJ |
203 | if (HelpViewer* viewer = CentralWidget::instance()->currentHelpViewer()) { |
204 | const int offset = action->data().toInt(); |
205 | QWebHistory *history = viewer->history(); |
206 | if (offset > 0) { |
207 | history->goToItem(history->forwardItems(history->count() |
208 | - offset + 1).back()); // forward |
209 | } else if (offset < 0) { |
210 | history->goToItem(history->backItems(-1 * offset).first()); // back |
211 | } |
212 | } |
213 | } |
214 | |
215 | #endif // BROWSER_QTWEBKIT |
216 | |
217 | void GlobalActions::(QAction *back, QAction *next, |
218 | QWidget *parent) |
219 | { |
220 | #if defined(BROWSER_QTWEBKIT) |
221 | m_backMenu = new QMenu(parent); |
222 | connect(m_backMenu, &QMenu::aboutToShow, |
223 | this, &GlobalActions::slotAboutToShowBackMenu); |
224 | connect(m_backMenu, &QMenu::triggered, |
225 | this, &GlobalActions::slotOpenActionUrl); |
226 | back->setMenu(m_backMenu); |
227 | |
228 | m_nextMenu = new QMenu(parent); |
229 | connect(m_nextMenu, &QMenu::aboutToShow, |
230 | this, &GlobalActions::slotAboutToShowNextMenu); |
231 | connect(m_nextMenu, &QMenu::triggered, |
232 | this, &GlobalActions::slotOpenActionUrl); |
233 | next->setMenu(m_nextMenu); |
234 | #else |
235 | Q_UNUSED(back); |
236 | Q_UNUSED(next); |
237 | Q_UNUSED(parent); |
238 | #endif |
239 | } |
240 | |
241 | GlobalActions *GlobalActions::m_instance = 0; |
242 | |