1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "globalactions.h"
5
6#include "centralwidget.h"
7#include "helpviewer.h"
8#include "tracer.h"
9
10#include <QtWidgets/QMenu>
11
12#include <QtGui/QAction>
13
14#if defined(BROWSER_QTWEBKIT)
15# include <QWebHistory>
16#endif
17
18GlobalActions *GlobalActions::instance(QObject *parent)
19{
20 Q_ASSERT(!m_instance != !parent);
21 if (!m_instance)
22 m_instance = new GlobalActions(parent);
23 return m_instance;
24}
25
26GlobalActions::GlobalActions(QObject *parent) : QObject(parent)
27{
28 TRACE_OBJ
29
30 // TODO: Put resource path in misc class
31 QString resourcePath = QLatin1String(":/qt-project.org/assistant/images/");
32#ifdef Q_OS_MAC
33 resourcePath.append(QLatin1String("mac"));
34#else
35 resourcePath.append(s: QLatin1String("win"));
36#endif
37 CentralWidget *centralWidget = CentralWidget::instance();
38
39 m_backAction = new QAction(tr(s: "&Back"), parent);
40 m_backAction->setEnabled(false);
41 m_backAction->setShortcuts(QKeySequence::Back);
42 m_backAction->setIcon(QIcon(resourcePath + QLatin1String("/previous.png")));
43 connect(sender: m_backAction, signal: &QAction::triggered, context: centralWidget, slot: &CentralWidget::backward);
44 m_actionList << m_backAction;
45
46 m_nextAction = new QAction(tr(s: "&Forward"), parent);
47 m_nextAction->setPriority(QAction::LowPriority);
48 m_nextAction->setEnabled(false);
49 m_nextAction->setShortcuts(QKeySequence::Forward);
50 m_nextAction->setIcon(QIcon(resourcePath + QLatin1String("/next.png")));
51 connect(sender: m_nextAction, signal: &QAction::triggered, context: centralWidget, slot: &CentralWidget::forward);
52 m_actionList << m_nextAction;
53
54 setupNavigationMenus(back: m_backAction, next: m_nextAction, parent: centralWidget);
55
56 m_homeAction = new QAction(tr(s: "&Home"), parent);
57 m_homeAction->setShortcut(tr(s: "ALT+Home"));
58 m_homeAction->setIcon(QIcon(resourcePath + QLatin1String("/home.png")));
59 connect(sender: m_homeAction, signal: &QAction::triggered, context: centralWidget, slot: &CentralWidget::home);
60 m_actionList << m_homeAction;
61
62 QAction *separator = new QAction(parent);
63 separator->setSeparator(true);
64 m_actionList << separator;
65
66 m_zoomInAction = new QAction(tr(s: "Zoom &in"), parent);
67 m_zoomInAction->setPriority(QAction::LowPriority);
68 m_zoomInAction->setIcon(QIcon(resourcePath + QLatin1String("/zoomin.png")));
69 m_zoomInAction->setShortcut(QKeySequence::ZoomIn);
70 connect(sender: m_zoomInAction, signal: &QAction::triggered, context: centralWidget, slot: &CentralWidget::zoomIn);
71 m_actionList << m_zoomInAction;
72
73 m_zoomOutAction = new QAction(tr(s: "Zoom &out"), parent);
74 m_zoomOutAction->setPriority(QAction::LowPriority);
75 m_zoomOutAction->setIcon(QIcon(resourcePath + QLatin1String("/zoomout.png")));
76 m_zoomOutAction->setShortcut(QKeySequence::ZoomOut);
77 connect(sender: m_zoomOutAction, signal: &QAction::triggered, context: centralWidget, slot: &CentralWidget::zoomOut);
78 m_actionList << m_zoomOutAction;
79
80 separator = new QAction(parent);
81 separator->setSeparator(true);
82 m_actionList << separator;
83
84#if QT_CONFIG(clipboard)
85 m_copyAction = new QAction(tr(s: "&Copy selected Text"), parent);
86 m_copyAction->setPriority(QAction::LowPriority);
87 m_copyAction->setIconText("&Copy");
88 m_copyAction->setIcon(QIcon(resourcePath + QLatin1String("/editcopy.png")));
89 m_copyAction->setShortcuts(QKeySequence::Copy);
90 m_copyAction->setEnabled(false);
91 connect(sender: m_copyAction, signal: &QAction::triggered, context: centralWidget, slot: &CentralWidget::copy);
92 m_actionList << m_copyAction;
93#endif
94
95 m_printAction = new QAction(tr(s: "&Print..."), parent);
96 m_printAction->setPriority(QAction::LowPriority);
97 m_printAction->setIcon(QIcon(resourcePath + QLatin1String("/print.png")));
98 m_printAction->setShortcut(QKeySequence::Print);
99 connect(sender: m_printAction, signal: &QAction::triggered, context: centralWidget, slot: &CentralWidget::print);
100 m_actionList << m_printAction;
101
102 m_findAction = new QAction(tr(s: "&Find in Text..."), parent);
103 m_findAction->setIconText(tr(s: "&Find"));
104 m_findAction->setIcon(QIcon(resourcePath + QLatin1String("/find.png")));
105 m_findAction->setShortcuts(QKeySequence::Find);
106 connect(sender: m_findAction, signal: &QAction::triggered, context: centralWidget, slot: &CentralWidget::showTextSearch);
107 m_actionList << m_findAction;
108
109#if defined (Q_OS_UNIX) && !defined(Q_OS_MAC)
110 m_backAction->setIcon(QIcon::fromTheme(QStringLiteral("go-previous") , fallback: m_backAction->icon()));
111 m_nextAction->setIcon(QIcon::fromTheme(QStringLiteral("go-next") , fallback: m_nextAction->icon()));
112 m_zoomInAction->setIcon(QIcon::fromTheme(QStringLiteral("zoom-in") , fallback: m_zoomInAction->icon()));
113 m_zoomOutAction->setIcon(QIcon::fromTheme(QStringLiteral("zoom-out") , fallback: m_zoomOutAction->icon()));
114#if QT_CONFIG(clipboard)
115 m_copyAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy") , fallback: m_copyAction->icon()));
116#endif
117 m_findAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-find") , fallback: m_findAction->icon()));
118 m_homeAction->setIcon(QIcon::fromTheme(QStringLiteral("go-home") , fallback: m_homeAction->icon()));
119 m_printAction->setIcon(QIcon::fromTheme(QStringLiteral("document-print") , fallback: m_printAction->icon()));
120#endif
121}
122
123void GlobalActions::updateActions()
124{
125 TRACE_OBJ
126 CentralWidget *centralWidget = CentralWidget::instance();
127#if QT_CONFIG(clipboard)
128 m_copyAction->setEnabled(centralWidget->hasSelection());
129#endif
130 m_nextAction->setEnabled(centralWidget->isForwardAvailable());
131 m_backAction->setEnabled(centralWidget->isBackwardAvailable());
132}
133
134#if QT_CONFIG(clipboard)
135void GlobalActions::setCopyAvailable(bool available)
136{
137 TRACE_OBJ
138 m_copyAction->setEnabled(available);
139}
140#endif
141
142#if defined(BROWSER_QTWEBKIT)
143
144void GlobalActions::slotAboutToShowBackMenu()
145{
146 TRACE_OBJ
147 m_backMenu->clear();
148 if (QWebHistory *history = CentralWidget::instance()->currentHelpViewer()->history()) {
149 const int currentItemIndex = history->currentItemIndex();
150 QList<QWebHistoryItem> items = history->backItems(history->count());
151 for (int i = items.count() - 1; i >= 0; --i) {
152 QAction *action = new QAction(this);
153 action->setText(items.at(i).title());
154 action->setData(-1 * (currentItemIndex - i));
155 m_backMenu->addAction(action);
156 }
157 }
158}
159
160void GlobalActions::slotAboutToShowNextMenu()
161{
162 TRACE_OBJ
163 m_nextMenu->clear();
164 if (QWebHistory *history = CentralWidget::instance()->currentHelpViewer()->history()) {
165 const int count = history->count();
166 QList<QWebHistoryItem> items = history->forwardItems(count);
167 for (int i = 0; i < items.count(); ++i) {
168 QAction *action = new QAction(this);
169 action->setData(count - i);
170 action->setText(items.at(i).title());
171 m_nextMenu->addAction(action);
172 }
173 }
174}
175
176void GlobalActions::slotOpenActionUrl(QAction *action)
177{
178 TRACE_OBJ
179 if (HelpViewer* viewer = CentralWidget::instance()->currentHelpViewer()) {
180 const int offset = action->data().toInt();
181 QWebHistory *history = viewer->history();
182 if (offset > 0) {
183 history->goToItem(history->forwardItems(history->count()
184 - offset + 1).back()); // forward
185 } else if (offset < 0) {
186 history->goToItem(history->backItems(-1 * offset).first()); // back
187 }
188 }
189}
190
191#endif // BROWSER_QTWEBKIT
192
193void GlobalActions::setupNavigationMenus(QAction *back, QAction *next,
194 QWidget *parent)
195{
196#if defined(BROWSER_QTWEBKIT)
197 m_backMenu = new QMenu(parent);
198 connect(m_backMenu, &QMenu::aboutToShow,
199 this, &GlobalActions::slotAboutToShowBackMenu);
200 connect(m_backMenu, &QMenu::triggered,
201 this, &GlobalActions::slotOpenActionUrl);
202 back->setMenu(m_backMenu);
203
204 m_nextMenu = new QMenu(parent);
205 connect(m_nextMenu, &QMenu::aboutToShow,
206 this, &GlobalActions::slotAboutToShowNextMenu);
207 connect(m_nextMenu, &QMenu::triggered,
208 this, &GlobalActions::slotOpenActionUrl);
209 next->setMenu(m_nextMenu);
210#else
211 Q_UNUSED(back);
212 Q_UNUSED(next);
213 Q_UNUSED(parent);
214#endif
215}
216
217GlobalActions *GlobalActions::m_instance = nullptr;
218

source code of qttools/src/assistant/assistant/globalactions.cpp