1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** 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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "openpagesmanager.h"
36 #include "centralwidget.h"
37 #include "helpconstants.h"
38 #include "helpviewer.h"
39 #include "localhelpmanager.h"
40 #include "openpagesmodel.h"
41 #include "openpagesswitcher.h"
42 #include "openpageswidget.h"
44 #include <QtGui/QApplication>
45 #include <QtGui/QClipboard>
46 #include <QtGui/QComboBox>
47 #include <QtGui/QMenu>
48 #include <QtGui/QTreeView>
50 #include <QtHelp/QHelpEngine>
52 #include <coreplugin/coreconstants.h>
53 #include <coreplugin/helpmanager.h>
54 #include <coreplugin/modemanager.h>
56 using namespace Help::Internal;
58 OpenPagesManager *OpenPagesManager::m_instance = 0;
60 // -- OpenPagesManager
62 OpenPagesManager::OpenPagesManager(QObject *parent)
66 , m_openPagesWidget(0)
67 , m_openPagesSwitcher(0)
69 Q_ASSERT(!m_instance);
72 m_model = new OpenPagesModel(this);
74 m_comboBox = new QComboBox;
75 m_comboBox->setModel(m_model);
76 m_comboBox->setContextMenuPolicy(Qt::CustomContextMenu);
77 connect(m_comboBox, SIGNAL(activated(int)), this, SLOT(setCurrentPage(int)));
78 connect(m_comboBox, SIGNAL(customContextMenuRequested(QPoint)), this,
79 SLOT(openPagesContextMenu(QPoint)));
81 m_openPagesSwitcher = new OpenPagesSwitcher(m_model);
82 connect(m_openPagesSwitcher, SIGNAL(closePage(QModelIndex)), this,
83 SLOT(closePage(QModelIndex)));
84 connect(m_openPagesSwitcher, SIGNAL(setCurrentPage(QModelIndex)), this,
85 SLOT(setCurrentPage(QModelIndex)));
88 OpenPagesManager ::~OpenPagesManager()
91 delete m_openPagesSwitcher;
94 OpenPagesManager &OpenPagesManager::instance()
100 QWidget *OpenPagesManager::openPagesWidget() const
102 if (!m_openPagesWidget) {
103 m_openPagesWidget = new OpenPagesWidget(m_model);
104 m_openPagesWidget->setFrameStyle(QFrame::NoFrame);
105 connect(m_openPagesWidget, SIGNAL(setCurrentPage(QModelIndex)), this,
106 SLOT(setCurrentPage(QModelIndex)));
107 connect(m_openPagesWidget, SIGNAL(closePage(QModelIndex)), this,
108 SLOT(closePage(QModelIndex)));
109 connect(m_openPagesWidget, SIGNAL(closePagesExcept(QModelIndex)), this,
110 SLOT(closePagesExcept(QModelIndex)));
112 return m_openPagesWidget;
115 QComboBox *OpenPagesManager::openPagesComboBox() const
120 int OpenPagesManager::pageCount() const
122 return m_model->rowCount();
125 QStringList splitString(const QVariant &value)
127 using namespace Help::Constants;
128 return value.toString().split(ListSeparator, QString::SkipEmptyParts);
131 void OpenPagesManager::setupInitialPages()
133 const QHelpEngineCore &engine = LocalHelpManager::helpEngine();
134 const int option = engine.customValue(QLatin1String("StartOption"),
135 Help::Constants::ShowLastPages).toInt();
136 QString homePage = engine.customValue(QLatin1String("DefaultHomePage"),
137 Help::Constants::AboutBlank).toString();
141 case Help::Constants::ShowHomePage: {
142 m_model->addPage(engine.customValue(QLatin1String("HomePage"),
143 homePage).toString());
146 case Help::Constants::ShowBlankPage: {
147 m_model->addPage(QUrl(Help::Constants::AboutBlank));
150 case Help::Constants::ShowLastPages: {
151 const QStringList &lastShownPageList = splitString(engine
152 .customValue(QLatin1String("LastShownPages")));
153 const int pageCount = lastShownPageList.count();
156 QStringList zoomFactors = splitString(engine
157 .customValue(QLatin1String("LastShownPagesZoom")));
158 while (zoomFactors.count() < pageCount)
159 zoomFactors.append(Help::Constants::DefaultZoomFactor);
161 initialPage = engine.customValue(QLatin1String("LastTabPage"), 0).toInt();
162 for (int curPage = 0; curPage < pageCount; ++curPage) {
163 const QString &curFile = lastShownPageList.at(curPage);
164 if (engine.findFile(curFile).isValid()
165 || curFile == Help::Constants::AboutBlank) {
166 m_model->addPage(curFile, zoomFactors.at(curPage).toFloat());
167 } else if (curPage <= initialPage && initialPage > 0) {
177 if (m_model->rowCount() == 0)
178 m_model->addPage(homePage);
180 for (int i = 0; i < m_model->rowCount(); ++i)
181 CentralWidget::instance()->addPage(m_model->pageAt(i));
184 setCurrentPage((initialPage >= m_model->rowCount())
185 ? m_model->rowCount() - 1 : initialPage);
186 m_openPagesSwitcher->selectCurrentPage();
191 HelpViewer *OpenPagesManager::createPage()
193 return createPage(QUrl(Help::Constants::AboutBlank));
196 HelpViewer *OpenPagesManager::createPageFromSearch(const QUrl &url)
198 return createPage(url, true);
201 HelpViewer *OpenPagesManager::createPage(const QUrl &url, bool fromSearch)
203 if (HelpViewer::launchWithExternalApp(url))
206 m_model->addPage(url);
208 const int index = m_model->rowCount() - 1;
209 HelpViewer * const page = m_model->pageAt(index);
210 CentralWidget::instance()->addPage(page, fromSearch);
213 setCurrentPage(index);
218 void OpenPagesManager::setCurrentPage(int index)
220 CentralWidget::instance()->setCurrentPage(m_model->pageAt(index));
222 m_comboBox->setCurrentIndex(index);
223 if (m_openPagesWidget)
224 m_openPagesWidget->selectCurrentPage();
227 void OpenPagesManager::setCurrentPage(const QModelIndex &index)
230 setCurrentPage(index.row());
233 void OpenPagesManager::closeCurrentPage()
235 if (!m_openPagesWidget)
238 QModelIndexList indexes = m_openPagesWidget->selectionModel()->selectedRows();
239 if (indexes.isEmpty())
242 Core::HelpManager *manager = Core::HelpManager::instance();
243 const bool closeOnReturn = manager->customValue(QLatin1String("ReturnOnClose"),
246 if (m_model->rowCount() == 1 && closeOnReturn) {
247 Core::ModeManager *modeManager = Core::ModeManager::instance();
248 modeManager->activateMode(Core::Constants::MODE_EDIT);
250 Q_ASSERT(indexes.count() == 1);
251 removePage(indexes.first().row());
255 void OpenPagesManager::closePage(const QModelIndex &index)
258 removePage(index.row());
261 void OpenPagesManager::closePagesExcept(const QModelIndex &index)
263 if (index.isValid()) {
265 HelpViewer *viewer = m_model->pageAt(index.row());
266 while (m_model->rowCount() > 1) {
267 if (m_model->pageAt(i) != viewer) {
276 void OpenPagesManager::gotoNextPage()
278 if (!m_openPagesSwitcher->isVisible()) {
279 m_openPagesSwitcher->selectCurrentPage();
280 m_openPagesSwitcher->gotoNextPage();
281 showTwicherOrSelectPage();
283 m_openPagesSwitcher->gotoNextPage();
287 void OpenPagesManager::gotoPreviousPage()
289 if (!m_openPagesSwitcher->isVisible()) {
290 m_openPagesSwitcher->selectCurrentPage();
291 m_openPagesSwitcher->gotoPreviousPage();
292 showTwicherOrSelectPage();
294 m_openPagesSwitcher->gotoPreviousPage();
300 void OpenPagesManager::removePage(int index)
302 Q_ASSERT(m_model->rowCount() > 1);
304 m_model->removePage(index);
305 CentralWidget::instance()->removePage(index);
308 if (m_openPagesWidget)
309 m_openPagesWidget->selectCurrentPage();
312 void OpenPagesManager::showTwicherOrSelectPage() const
314 if (QApplication::keyboardModifiers() != Qt::NoModifier) {
315 const int width = CentralWidget::instance()->width();
316 const int height = CentralWidget::instance()->height();
317 const QPoint p(CentralWidget::instance()->mapToGlobal(QPoint(0, 0)));
318 m_openPagesSwitcher->move((width - m_openPagesSwitcher->width()) / 2 + p.x(),
319 (height - m_openPagesSwitcher->height()) / 2 + p.y());
320 m_openPagesSwitcher->setVisible(true);
322 m_openPagesSwitcher->selectAndHide();
328 void OpenPagesManager::openPagesContextMenu(const QPoint &point)
330 const QModelIndex &index = m_model->index(m_comboBox->currentIndex(), 0);
331 const QString &fileName = m_model->data(index, Qt::ToolTipRole).toString();
332 if (fileName.isEmpty())
336 menu.addAction(tr("Copy Full Path to Clipboard"));
337 if (menu.exec(m_comboBox->mapToGlobal(point)))
338 QApplication::clipboard()->setText(fileName);