OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / help / openpagesmanager.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "openpagesmanager.h"
35
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"
43
44 #include <QtGui/QApplication>
45 #include <QtGui/QClipboard>
46 #include <QtGui/QComboBox>
47 #include <QtGui/QMenu>
48 #include <QtGui/QTreeView>
49
50 #include <QtHelp/QHelpEngine>
51
52 #include <coreplugin/coreconstants.h>
53 #include <coreplugin/helpmanager.h>
54 #include <coreplugin/modemanager.h>
55
56 using namespace Help::Internal;
57
58 OpenPagesManager *OpenPagesManager::m_instance = 0;
59
60 // -- OpenPagesManager
61
62 OpenPagesManager::OpenPagesManager(QObject *parent)
63     : QObject(parent)
64     , m_comboBox(0)
65     , m_model(0)
66     , m_openPagesWidget(0)
67     , m_openPagesSwitcher(0)
68 {
69     Q_ASSERT(!m_instance);
70
71     m_instance = this;
72     m_model = new OpenPagesModel(this);
73
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)));
80
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)));
86 }
87
88 OpenPagesManager ::~OpenPagesManager()
89 {
90     m_instance = 0;
91     delete m_openPagesSwitcher;
92 }
93
94 OpenPagesManager &OpenPagesManager::instance()
95 {
96     Q_ASSERT(m_instance);
97     return *m_instance;
98 }
99
100 QWidget *OpenPagesManager::openPagesWidget() const
101 {
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)));
111     }
112     return m_openPagesWidget;
113 }
114
115 QComboBox *OpenPagesManager::openPagesComboBox() const
116 {
117     return m_comboBox;
118 }
119
120 int OpenPagesManager::pageCount() const
121 {
122     return m_model->rowCount();
123 }
124
125 QStringList splitString(const QVariant &value)
126 {
127     using namespace Help::Constants;
128     return value.toString().split(ListSeparator, QString::SkipEmptyParts);
129 }
130
131 void OpenPagesManager::setupInitialPages()
132 {
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();
138
139     int initialPage = 0;
140     switch (option) {
141         case Help::Constants::ShowHomePage: {
142             m_model->addPage(engine.customValue(QLatin1String("HomePage"),
143                 homePage).toString());
144         }   break;
145
146         case Help::Constants::ShowBlankPage: {
147             m_model->addPage(QUrl(Help::Constants::AboutBlank));
148         }   break;
149
150         case Help::Constants::ShowLastPages: {
151             const QStringList &lastShownPageList = splitString(engine
152                 .customValue(QLatin1String("LastShownPages")));
153             const int pageCount = lastShownPageList.count();
154
155             if (pageCount > 0) {
156                 QStringList zoomFactors = splitString(engine
157                     .customValue(QLatin1String("LastShownPagesZoom")));
158                 while (zoomFactors.count() < pageCount)
159                     zoomFactors.append(Help::Constants::DefaultZoomFactor);
160
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) {
168                         --initialPage;
169                     }
170                 }
171             }
172         }   break;
173
174         default: break;
175     }
176
177     if (m_model->rowCount() == 0)
178         m_model->addPage(homePage);
179
180     for (int i = 0; i < m_model->rowCount(); ++i)
181         CentralWidget::instance()->addPage(m_model->pageAt(i));
182
183     emit pagesChanged();
184     setCurrentPage((initialPage >= m_model->rowCount())
185         ? m_model->rowCount() - 1 : initialPage);
186     m_openPagesSwitcher->selectCurrentPage();
187 }
188
189 // -- public slots
190
191 HelpViewer *OpenPagesManager::createPage()
192 {
193     return createPage(QUrl(Help::Constants::AboutBlank));
194 }
195
196 HelpViewer *OpenPagesManager::createPageFromSearch(const QUrl &url)
197 {
198     return createPage(url, true);
199 }
200
201 HelpViewer *OpenPagesManager::createPage(const QUrl &url, bool fromSearch)
202 {
203     if (HelpViewer::launchWithExternalApp(url))
204         return 0;
205
206     m_model->addPage(url);
207
208     const int index = m_model->rowCount() - 1;
209     HelpViewer * const page = m_model->pageAt(index);
210     CentralWidget::instance()->addPage(page, fromSearch);
211
212     emit pagesChanged();
213     setCurrentPage(index);
214
215     return page;
216 }
217
218 void OpenPagesManager::setCurrentPage(int index)
219 {
220     CentralWidget::instance()->setCurrentPage(m_model->pageAt(index));
221
222     m_comboBox->setCurrentIndex(index);
223     if (m_openPagesWidget)
224         m_openPagesWidget->selectCurrentPage();
225 }
226
227 void OpenPagesManager::setCurrentPage(const QModelIndex &index)
228 {
229     if (index.isValid())
230         setCurrentPage(index.row());
231 }
232
233 void OpenPagesManager::closeCurrentPage()
234 {
235     if (!m_openPagesWidget)
236         return;
237
238     QModelIndexList indexes = m_openPagesWidget->selectionModel()->selectedRows();
239     if (indexes.isEmpty())
240         return;
241
242     Core::HelpManager *manager = Core::HelpManager::instance();
243     const bool closeOnReturn = manager->customValue(QLatin1String("ReturnOnClose"),
244         false).toBool();
245
246     if (m_model->rowCount() == 1 && closeOnReturn) {
247         Core::ModeManager *modeManager = Core::ModeManager::instance();
248         modeManager->activateMode(Core::Constants::MODE_EDIT);
249     } else {
250         Q_ASSERT(indexes.count() == 1);
251         removePage(indexes.first().row());
252     }
253 }
254
255 void OpenPagesManager::closePage(const QModelIndex &index)
256 {
257     if (index.isValid())
258         removePage(index.row());
259 }
260
261 void OpenPagesManager::closePagesExcept(const QModelIndex &index)
262 {
263     if (index.isValid()) {
264         int i = 0;
265         HelpViewer *viewer = m_model->pageAt(index.row());
266         while (m_model->rowCount() > 1) {
267             if (m_model->pageAt(i) != viewer) {
268                 removePage(i);
269             } else {
270                 i++;
271             }
272         }
273     }
274 }
275
276 void OpenPagesManager::gotoNextPage()
277 {
278     if (!m_openPagesSwitcher->isVisible()) {
279         m_openPagesSwitcher->selectCurrentPage();
280         m_openPagesSwitcher->gotoNextPage();
281         showTwicherOrSelectPage();
282     } else {
283         m_openPagesSwitcher->gotoNextPage();
284     }
285 }
286
287 void OpenPagesManager::gotoPreviousPage()
288 {
289     if (!m_openPagesSwitcher->isVisible()) {
290         m_openPagesSwitcher->selectCurrentPage();
291         m_openPagesSwitcher->gotoPreviousPage();
292         showTwicherOrSelectPage();
293     } else {
294         m_openPagesSwitcher->gotoPreviousPage();
295     }
296 }
297
298 // -- private
299
300 void OpenPagesManager::removePage(int index)
301 {
302     Q_ASSERT(m_model->rowCount() > 1);
303
304     m_model->removePage(index);
305     CentralWidget::instance()->removePage(index);
306
307     emit pagesChanged();
308     if (m_openPagesWidget)
309         m_openPagesWidget->selectCurrentPage();
310 }
311
312 void OpenPagesManager::showTwicherOrSelectPage() const
313 {
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);
321     } else {
322         m_openPagesSwitcher->selectAndHide();
323     }
324 }
325
326 // -- private slots
327
328 void OpenPagesManager::openPagesContextMenu(const QPoint &point)
329 {
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())
333         return;
334
335     QMenu menu;
336     menu.addAction(tr("Copy Full Path to Clipboard"));
337     if (menu.exec(m_comboBox->mapToGlobal(point)))
338         QApplication::clipboard()->setText(fileName);
339 }