source: trunk/tools/assistant/compat/mainwindow.cpp@ 507

Last change on this file since 507 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 27.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the Qt Assistant of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "mainwindow.h"
43#include "tabbedbrowser.h"
44#include "helpdialog.h"
45#include "config.h"
46#include "fontsettingsdialog.h"
47
48#include <QDockWidget>
49#include <QDir>
50#include <QTimer>
51#include <QStatusBar>
52#include <QShortcut>
53#include <QMessageBox>
54#include <QPainter>
55#include <QEventLoop>
56#include <QtEvents>
57#include <QFontDatabase>
58#include <QWhatsThis>
59#include <QTextDocumentFragment>
60#include <QLibraryInfo>
61#include <QPrinter>
62#include <QPrintDialog>
63#include <QAbstractTextDocumentLayout>
64#include <QTextDocument>
65#include <QTextObject>
66#include <QFileDialog>
67#include <QThread>
68
69QT_BEGIN_NAMESPACE
70
71QList<MainWindow*> MainWindow::windows;
72
73#if defined(Q_WS_WIN)
74extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;
75#endif
76
77MainWindow::MainWindow()
78{
79 setUnifiedTitleAndToolBarOnMac(true);
80 ui.setupUi(this);
81
82#if defined(Q_WS_WIN)
83 // Workaround for QMimeSourceFactory failing in QFileInfo::isReadable() for
84 // certain user configs. See task: 34372
85 qt_ntfs_permission_lookup = 0;
86#endif
87 setupCompleted = false;
88
89 goActions = QList<QAction*>();
90 goActionDocFiles = new QMap<QAction*,QString>;
91
92 windows.append(this);
93 tabs = new TabbedBrowser(this);
94 connect(tabs, SIGNAL(tabCountChanged(int)), this, SLOT(updateTabActions(int)));
95 setCentralWidget(tabs);
96
97 Config *config = Config::configuration();
98
99 updateProfileSettings();
100
101 dw = new QDockWidget(this);
102 dw->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
103 dw->setWindowTitle(tr("Sidebar"));
104 dw->setObjectName(QLatin1String("sidebar"));
105 helpDock = new HelpDialog(dw, this);
106 dw->setWidget(helpDock);
107
108 addDockWidget(Qt::LeftDockWidgetArea, dw);
109
110 // read geometry configuration
111 setupGoActions();
112
113 restoreGeometry(config->windowGeometry());
114 restoreState(config->mainWindowState());
115 if (config->sideBarHidden())
116 dw->hide();
117
118 tabs->setup();
119 QTimer::singleShot(0, this, SLOT(setup()));
120#if defined(Q_WS_MAC)
121 QMenu *windowMenu = new QMenu(tr("&Window"), this);
122 menuBar()->insertMenu(ui.helpMenu->menuAction(), windowMenu);
123 windowMenu->addAction(tr("Minimize"), this,
124 SLOT(showMinimized()), QKeySequence(tr("Ctrl+M")));
125 // Use the same forward and backward browser shortcuts as Safari and Internet Explorer do
126 // on the Mac. This means that if you have access to one of those cool Intellimice, the thing
127 // works just fine, since that's how Microsoft hacked it.
128 ui.actionGoPrevious->setShortcut(QKeySequence(Qt::CTRL|Qt::Key_Left));
129 ui.actionGoNext->setShortcut(QKeySequence(Qt::CTRL|Qt::Key_Right));
130
131 static const QLatin1String MacIconPath(":/trolltech/assistant/images/mac");
132 ui.actionGoNext->setIcon(QIcon(MacIconPath + QLatin1String("/next.png")));
133 ui.actionGoPrevious->setIcon(QIcon(MacIconPath + QLatin1String("/prev.png")));
134 ui.actionGoHome->setIcon(QIcon(MacIconPath + QLatin1String("/home.png")));
135 ui.actionEditCopy->setIcon(QIcon(MacIconPath + QLatin1String("/editcopy.png")));
136 ui.actionEditCopy->setIcon(QIcon(MacIconPath + QLatin1String("/editcopy.png")));
137 ui.actionEditFind->setIcon(QIcon(MacIconPath + QLatin1String("/find.png")));
138 ui.actionFilePrint->setIcon(QIcon(MacIconPath + QLatin1String("/print.png")));
139 ui.actionZoomOut->setIcon(QIcon(MacIconPath + QLatin1String("/zoomout.png")));
140 ui.actionZoomIn->setIcon(QIcon(MacIconPath + QLatin1String("/zoomin.png")));
141 ui.actionSyncToc->setIcon(QIcon(MacIconPath + QLatin1String("/synctoc.png")));
142 ui.actionHelpWhatsThis->setIcon(QIcon(MacIconPath + QLatin1String("/whatsthis.png")));
143#endif
144}
145
146MainWindow::~MainWindow()
147{
148 windows.removeAll(this);
149 delete goActionDocFiles;
150}
151
152void MainWindow::setup()
153{
154 if(setupCompleted)
155 return;
156
157 qApp->setOverrideCursor(QCursor(Qt::WaitCursor));
158 statusBar()->showMessage(tr("Initializing Qt Assistant..."));
159 setupCompleted = true;
160 helpDock->initialize();
161 connect(ui.actionGoPrevious, SIGNAL(triggered()), tabs, SLOT(backward()));
162 connect(ui.actionGoNext, SIGNAL(triggered()), tabs, SLOT(forward()));
163 connect(ui.actionEditCopy, SIGNAL(triggered()), tabs, SLOT(copy()));
164 connect(ui.actionFileExit, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
165 connect(ui.actionAddBookmark, SIGNAL(triggered()),
166 helpDock, SLOT(addBookmark()));
167 connect(helpDock, SIGNAL(showLink(QString)),
168 this, SLOT(showLink(QString)));
169 connect(helpDock, SIGNAL(showSearchLink(QString,QStringList)),
170 this, SLOT(showSearchLink(QString,QStringList)));
171
172 connect(ui.bookmarkMenu, SIGNAL(triggered(QAction*)),
173 this, SLOT(showBookmark(QAction*)));
174 connect(ui.actionZoomIn, SIGNAL(triggered()), tabs, SLOT(zoomIn()));
175 connect(ui.actionZoomOut, SIGNAL(triggered()), tabs, SLOT(zoomOut()));
176
177 connect(ui.actionOpenPage, SIGNAL(triggered()), tabs, SLOT(newTab()));
178 connect(ui.actionClosePage, SIGNAL(triggered()), tabs, SLOT(closeTab()));
179 connect(ui.actionNextPage, SIGNAL(triggered()), tabs, SLOT(nextTab()));
180 connect(ui.actionPrevPage, SIGNAL(triggered()), tabs, SLOT(previousTab()));
181
182
183#if defined(Q_OS_WIN32) || defined(Q_OS_WIN64)
184 QShortcut *acc = new QShortcut(tr("SHIFT+CTRL+="), this);
185 connect(acc, SIGNAL(activated()), ui.actionZoomIn, SIGNAL(triggered()));
186#endif
187
188 connect(new QShortcut(tr("Ctrl+T"), this), SIGNAL(activated()), helpDock, SLOT(toggleContents()));
189 connect(new QShortcut(tr("Ctrl+I"), this), SIGNAL(activated()), helpDock, SLOT(toggleIndex()));
190 connect(new QShortcut(tr("Ctrl+B"), this), SIGNAL(activated()), helpDock, SLOT(toggleBookmarks()));
191 connect(new QShortcut(tr("Ctrl+S"), this), SIGNAL(activated()), helpDock, SLOT(toggleSearch()));
192 connect(new QShortcut(tr("Ctrl+]"), this), SIGNAL(activated()), tabs, SLOT(nextTab()));
193 connect(new QShortcut(tr("Ctrl+["), this), SIGNAL(activated()), tabs, SLOT(previousTab()));
194
195 Config *config = Config::configuration();
196
197 setupBookmarkMenu();
198
199 QAction *viewsAction = createPopupMenu()->menuAction();
200 viewsAction->setText(tr("Views"));
201 ui.viewMenu->addAction(viewsAction);
202
203 const int tabIndex = config->sideBarPage();
204 helpDock->tabWidget()->setCurrentIndex(tabIndex);
205 // The tab index is 0 by default, so we need to force an upate
206 // to poulate the contents in this case.
207 if (tabIndex == 0)
208 helpDock->currentTabChanged(tabIndex);
209
210 ui.actionEditFind->setShortcut(QKeySequence::Find);
211 ui.actionEditFindNext->setShortcut(QKeySequence::FindNext);
212 ui.actionEditFindPrev->setShortcut(QKeySequence::FindPrevious);
213
214 QObject::connect(ui.actionEditFind, SIGNAL(triggered()), tabs, SLOT(find()));
215 QObject::connect(ui.actionEditFindNext, SIGNAL(triggered()), tabs, SLOT(findNext()));
216 QObject::connect(ui.actionEditFindPrev, SIGNAL(triggered()), tabs, SLOT(findPrevious()));
217 connect(ui.actionEditFont_Settings, SIGNAL(triggered()), this, SLOT(showFontSettingsDialog()));
218
219 qApp->restoreOverrideCursor();
220 ui.actionGoPrevious->setEnabled(false);
221 ui.actionGoNext->setEnabled(false);
222 ui.actionEditCopy->setEnabled(false);
223
224 // set the current selected item in the treeview
225 helpDialog()->locateContents(tabs->currentBrowser()->source().toString());
226 connect(tabs, SIGNAL(browserUrlChanged(QString)), helpDock, SLOT(locateContents(QString)));
227}
228
229void MainWindow::browserTabChanged()
230{
231 HelpWindow *win = tabs->currentBrowser();
232 if (win) {
233 QTextCursor cursor(win->textCursor());
234 ui.actionEditCopy->setEnabled(cursor.hasSelection());
235 ui.actionGoPrevious->setEnabled(win->isBackwardAvailable());
236 ui.actionGoNext->setEnabled(win->isForwardAvailable());
237 }
238}
239
240void MainWindow::copyAvailable(bool yes)
241{
242 ui.actionEditCopy->setEnabled(yes);
243}
244
245void MainWindow::updateTabActions(int index)
246{
247 bool enabled = (index > 1) ? true : false;
248 ui.actionPrevPage->setEnabled(enabled);
249 ui.actionNextPage->setEnabled(enabled);
250 ui.actionClosePage->setEnabled(enabled);
251}
252
253void MainWindow::setupGoActions()
254{
255 Config *config = Config::configuration();
256 QStringList titles = config->docTitles();
257 QAction *action = 0;
258
259 static bool separatorInserted = false;
260
261 foreach (QAction *a, goActions) {
262 ui.goMenu->removeAction(a);
263 ui.goActionToolbar->removeAction(a);
264 }
265 qDeleteAll(goActions);
266 goActions.clear();
267 goActionDocFiles->clear();
268
269 int addCount = 0;
270
271 foreach (QString title, titles) {
272 QPixmap pix = config->docIcon(title);
273 if(!pix.isNull()) {
274 if(!separatorInserted) {
275 ui.goMenu->addSeparator();
276 separatorInserted = true;
277 }
278 action = new QAction(this);
279 action->setText(title);
280 action->setWhatsThis(tr("Displays the main page of a specific documentation set."));
281 action->setIcon(QIcon(pix));
282 ui.goMenu->addAction(action);
283 ui.goActionToolbar->addAction(action);
284 goActions.append(action);
285 goActionDocFiles->insert(action, config->indexPage(title));
286 connect(action, SIGNAL(triggered()),
287 this, SLOT(showGoActionLink()));
288 ++addCount;
289 }
290 }
291 if(!addCount)
292 ui.goActionToolbar->hide();
293 else
294 ui.goActionToolbar->show();
295
296}
297
298bool MainWindow::insertActionSeparator()
299{
300 ui.goMenu->addSeparator();
301 ui.Toolbar->addSeparator();
302 return true;
303}
304
305void MainWindow::closeEvent(QCloseEvent *e)
306{
307 saveSettings();
308 e->accept();
309}
310
311void MainWindow::about()
312{
313 QMessageBox box(this);
314
315 // TODO: Remove these variables for 4.6.0. Must keep this way for 4.5.x due to string freeze.
316 QString edition;
317 QString info;
318 QString moreInfo;
319
320 box.setText(QString::fromLatin1("<center><img src=\":/trolltech/assistant/images/assistant-128.png\">"
321 "<h3>%1</h3>"
322 "<p>Version %2 %3</p></center>"
323 "<p>%4</p>"
324 "<p>%5</p>"
325 "<p>Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).</p>"
326 "<p>The program is provided AS IS with NO WARRANTY OF ANY KIND,"
327 " INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A"
328 " PARTICULAR PURPOSE.<p/>")
329 .arg(tr("Qt Assistant")).arg(QLatin1String(QT_VERSION_STR)).arg(edition).arg(info).arg(moreInfo));
330 box.setWindowTitle(tr("Qt Assistant"));
331 box.setIcon(QMessageBox::NoIcon);
332 box.exec();
333}
334
335void MainWindow::on_actionAboutApplication_triggered()