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