OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / shared / help / contentwindow.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 "contentwindow.h"
35
36 #include "centralwidget.h"
37 #include "helpviewer.h"
38 #include "localhelpmanager.h"
39 #include "openpagesmanager.h"
40
41 #include <QtGui/QLayout>
42 #include <QtGui/QFocusEvent>
43 #include <QtGui/QMenu>
44
45 #include <QtHelp/QHelpEngine>
46 #include <QtHelp/QHelpContentWidget>
47
48 using namespace Help::Internal;
49
50 ContentWindow::ContentWindow()
51     : m_contentWidget(0)
52     , m_expandDepth(-2)
53 {
54     m_contentWidget = (&LocalHelpManager::helpEngine())->contentWidget();
55     m_contentWidget->installEventFilter(this);
56     m_contentWidget->viewport()->installEventFilter(this);
57     m_contentWidget->setContextMenuPolicy(Qt::CustomContextMenu);
58     setFocusProxy(m_contentWidget);
59
60     QVBoxLayout *layout = new QVBoxLayout(this);
61     layout->setMargin(0);
62     layout->addWidget(m_contentWidget);
63
64     connect(m_contentWidget, SIGNAL(customContextMenuRequested(QPoint)), this,
65         SLOT(showContextMenu(QPoint)));
66     connect(m_contentWidget, SIGNAL(linkActivated(QUrl)), this,
67         SIGNAL(linkActivated(QUrl)));
68
69     QHelpContentModel *contentModel =
70         qobject_cast<QHelpContentModel*>(m_contentWidget->model());
71     connect(contentModel, SIGNAL(contentsCreated()), this, SLOT(expandTOC()));
72
73     m_contentWidget->setFrameStyle(QFrame::NoFrame);
74 }
75
76 ContentWindow::~ContentWindow()
77 {
78 }
79
80 bool ContentWindow::syncToContent(const QUrl& url)
81 {
82     QModelIndex idx = m_contentWidget->indexOf(url);
83     if (!idx.isValid())
84         return false;
85     m_contentWidget->setCurrentIndex(idx);
86     return true;
87 }
88
89 void ContentWindow::expandTOC()
90 {
91     if (m_expandDepth > -2) {
92         expandToDepth(m_expandDepth);
93         m_expandDepth = -2;
94     }
95 }
96
97 void ContentWindow::expandToDepth(int depth)
98 {
99     m_expandDepth = depth;
100     if (depth == -1)
101         m_contentWidget->expandAll();
102     else
103         m_contentWidget->expandToDepth(depth);
104 }
105
106 bool ContentWindow::eventFilter(QObject *o, QEvent *e)
107 {
108     if (m_contentWidget && o == m_contentWidget->viewport()
109         && e->type() == QEvent::MouseButtonRelease) {
110         QMouseEvent *me = static_cast<QMouseEvent*>(e);
111         QItemSelectionModel *sm = m_contentWidget->selectionModel();
112         if (!me || !sm)
113             return QWidget::eventFilter(o, e);
114
115         Qt::MouseButtons button = me->button();
116         const QModelIndex &index = m_contentWidget->indexAt(me->pos());
117
118         if (index.isValid() && sm->isSelected(index)) {
119             if ((button == Qt::LeftButton && (me->modifiers() & Qt::ControlModifier))
120                 || (button == Qt::MidButton)) {
121                 QHelpContentModel *contentModel =
122                     qobject_cast<QHelpContentModel*>(m_contentWidget->model());
123                 if (contentModel) {
124                     QHelpContentItem *itm = contentModel->contentItemAt(index);
125                     if (itm && HelpViewer::canOpenPage(itm->url().path()))
126                         OpenPagesManager::instance().createPage(itm->url());
127                 }
128             } else if (button == Qt::LeftButton) {
129                 itemClicked(index);
130             }
131         }
132     }
133     return QWidget::eventFilter(o, e);
134 }
135
136 void ContentWindow::showContextMenu(const QPoint &pos)
137 {
138     if (!m_contentWidget->indexAt(pos).isValid())
139         return;
140
141     QHelpContentModel *contentModel =
142         qobject_cast<QHelpContentModel*>(m_contentWidget->model());
143     QHelpContentItem *itm =
144         contentModel->contentItemAt(m_contentWidget->currentIndex());
145
146     QMenu menu;
147     QAction *curTab = menu.addAction(tr("Open Link"));
148     QAction *newTab = menu.addAction(tr("Open Link as New Page"));
149     if (!HelpViewer::canOpenPage(itm->url().path()))
150         newTab->setEnabled(false);
151
152     menu.move(m_contentWidget->mapToGlobal(pos));
153
154     QAction *action = menu.exec();
155     if (curTab == action)
156         emit linkActivated(itm->url());
157     else if (newTab == action)
158         OpenPagesManager::instance().createPage(itm->url());
159 }
160
161 void ContentWindow::itemClicked(const QModelIndex &index)
162 {
163     QHelpContentModel *contentModel =
164         qobject_cast<QHelpContentModel*>(m_contentWidget->model());
165
166     if (contentModel) {
167         if (QHelpContentItem *itm = contentModel->contentItemAt(index)) {
168             const QUrl &url = itm->url();
169             if (url != CentralWidget::instance()->currentHelpViewer()->source())
170                 emit linkActivated(itm->url());
171         }
172     }
173 }