helpdialog.cpp
来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 1,320 行 · 第 1/3 页
CPP
1,320 行
} else{ if ((item.depth > depth) && root) { depth = item.depth; stack.push(contentEntry); } if (item.depth == depth) { contentEntry = new QTreeWidgetItem(stack.top(), lastItem[ depth ]); lastItem[ depth ] = contentEntry; contentEntry->setText(0, item.title); contentEntry->setData(0, LinkRole, item.reference); } else if (item.depth < depth) { stack.pop(); depth--; item = *(--it); } } } processEvents(); } setCursor(Qt::ArrowCursor); showInitDoneMessage();}void HelpDialog::showContentsTopic(){ QTreeWidgetItem *i = (QTreeWidgetItem*)ui.listContents->currentItem(); if (!i) return; emit showLink(i->data(0, LinkRole).toString());}QTreeWidgetItem * HelpDialog::locateLink(QTreeWidgetItem *item, const QString &link){ QTreeWidgetItem *child = 0;#ifdef Q_OS_WIN Qt::CaseSensitivity checkCase = Qt::CaseInsensitive;#else Qt::CaseSensitivity checkCase = Qt::CaseSensitive;#endif for (int i = 0, childCount = item->childCount(); i<childCount; i++) { child = item->child(i); ///check whether it is this item if (link.startsWith(child->data(0, LinkRole).toString(), checkCase)) break; //check if the link is a child of this item else if (child->childCount()) { child = locateLink(child, link); if (child) break; } child = 0; } return child;}void HelpDialog::locateContents(const QString &link){ //ensure the TOC is filled if (!contentsInserted) insertContents();#ifdef Q_OS_WIN Qt::CaseSensitivity checkCase = Qt::CaseInsensitive;#else Qt::CaseSensitivity checkCase = Qt::CaseSensitive;#endif QString findLink(link); //Installations on a windows local drive will give the 'link' as <file:///C:/xxx> //and the contents in the TOC will be <file:C:/xxx>. //But on others the 'link' of format <file:///root/xxx> //and the contents in the TOC will be <file:/root/xxx>. if (findLink.contains(QLatin1String("file:///"))) { if (findLink[9] == QLatin1Char(':')) //on windows drives findLink.replace(0, 8, QLatin1String("file:")); else findLink.replace(0, 8, QLatin1String("file:/")); } bool topLevel = false; QTreeWidgetItem *item = 0; int totalItems = ui.listContents->topLevelItemCount(); for (int i = 0; i < totalItems; i++ ) { // first see if we are one of the top level items item = (QTreeWidgetItem*)ui.listContents->topLevelItem(i); if (findLink.startsWith(item->data(0, LinkRole).toString(), checkCase)) { topLevel = true; break; } } if (!topLevel) { // now try to find it in the sublevel items for (int n = 0; n < totalItems; ++n) { item = (QTreeWidgetItem*)ui.listContents->topLevelItem(n); item = locateLink(item, findLink); if (item) break; } } //remove the old selection QList<QTreeWidgetItem *> selected = ui.listContents->selectedItems(); foreach(QTreeWidgetItem *sel, selected) ui.listContents->setItemSelected(sel, false); //set the TOC item and show ui.listContents->setCurrentItem(item); ui.listContents->setItemSelected(item, true); ui.listContents->scrollToItem(item);}void HelpDialog::toggleContents(){ if (!isVisible() || ui.tabWidget->currentIndex() != 0) { ui.tabWidget->setCurrentIndex(0); parentWidget()->show(); } else parentWidget()->hide();}void HelpDialog::toggleIndex(){ if (!isVisible() || ui.tabWidget->currentIndex() != 1 || !ui.editIndex->hasFocus()) { ui.tabWidget->setCurrentIndex(1); parentWidget()->show(); ui.editIndex->setFocus(); } else parentWidget()->hide();}void HelpDialog::toggleBookmarks(){ if (!isVisible() || ui.tabWidget->currentIndex() != 2) { ui.tabWidget->setCurrentIndex(2); parentWidget()->show(); } else parentWidget()->hide();}void HelpDialog::toggleSearch(){ if (!isVisible() || ui.tabWidget->currentIndex() != 3) { ui.tabWidget->setCurrentIndex(3); parentWidget()->show(); } else parentWidget()->hide();}void HelpDialog::setupFullTextIndex(){ if (fullTextIndex) return; QString pname = Config::configuration()->profileName(); fullTextIndex = new Index(QStringList(), QDir::homePath()); // ### Is this correct ? if (!verifyDirectory(cacheFilesPath)) { QMessageBox::warning(help, tr("Qt Assistant"), tr("Failed to save fulltext search index\n" "Assistant will not work!")); return; } fullTextIndex->setDictionaryFile(cacheFilesPath + QDir::separator() + QLatin1String("indexdb40.dict.") + pname); fullTextIndex->setDocListFile(cacheFilesPath + QDir::separator() + QLatin1String("indexdb40.doc.") + pname); processEvents(); connect(fullTextIndex, SIGNAL(indexingProgress(int)), this, SLOT(setIndexingProgress(int))); QFile f(cacheFilesPath + QDir::separator() + QLatin1String("indexdb40.dict.") + pname); if (!f.exists()) { QString doc; QSet<QString> documentSet; QMap<QString, QString>::ConstIterator it = titleMap.constBegin(); for (; it != titleMap.constEnd(); ++it) { doc = HelpDialog::removeAnchorFromLink(it.key()); if (!doc.isEmpty()) documentSet.insert(doc); } loadIndexFile(); for ( QStringList::Iterator it = keywordDocuments.begin(); it != keywordDocuments.end(); ++it ) { if (!(*it).isEmpty()) documentSet.insert(*it); } fullTextIndex->setDocList( documentSet.toList() ); help->statusBar()->clearMessage(); setCursor(Qt::WaitCursor); ui.labelPrepare->setText(tr("Indexing files...")); ui.progressPrepare->setMaximum(100); ui.progressPrepare->reset(); ui.progressPrepare->show(); ui.framePrepare->show(); processEvents(); if (fullTextIndex->makeIndex() == -1) return; fullTextIndex->writeDict(); ui.progressPrepare->setValue(100); ui.framePrepare->hide(); setCursor(Qt::ArrowCursor); showInitDoneMessage(); } else { setCursor(Qt::WaitCursor); help->statusBar()->showMessage(tr("Reading dictionary...")); processEvents(); fullTextIndex->readDict(); help->statusBar()->showMessage(tr("Done"), 3000); setCursor(Qt::ArrowCursor); } keywordDocuments.clear();}void HelpDialog::setIndexingProgress(int prog){ ui.progressPrepare->setValue(prog); processEvents();}void HelpDialog::startSearch(){ QString str = ui.termsEdit->text(); str = str.simplified(); str = str.replace(QLatin1String("\'"), QLatin1String("\"")); str = str.replace(QLatin1String("`"), QLatin1String("\"")); QString buf = str; str = str.replace(QLatin1String("-"), QLatin1String(" ")); str = str.replace(QRegExp(QLatin1String("\\s[\\S]?\\s")), QLatin1String(" ")); terms = str.split(QLatin1Char(' ')); QStringList termSeq; QStringList seqWords; QStringList::iterator it = terms.begin(); for (; it != terms.end(); ++it) { (*it) = (*it).simplified(); (*it) = (*it).toLower(); (*it) = (*it).replace(QLatin1String("\""), QLatin1String("")); } if (str.contains(QLatin1Char('\"'))) { if ((str.count(QLatin1Char('\"')))%2 == 0) { int beg = 0; int end = 0; QString s; beg = str.indexOf(QLatin1Char('\"'), beg); while (beg != -1) { beg++; end = str.indexOf(QLatin1Char('\"'), beg); s = str.mid(beg, end - beg); s = s.toLower(); s = s.simplified(); if (s.contains(QLatin1Char('*'))) { QMessageBox::warning(this, tr("Full Text Search"), tr("Using a wildcard within phrases is not allowed.")); return; } seqWords += s.split(QLatin1Char(' ')); termSeq << s; beg = str.indexOf(QLatin1Char('\"'), end + 1); } } else { QMessageBox::warning(this, tr("Full Text Search"), tr("The closing quotation mark is missing.")); return; } } setCursor(Qt::WaitCursor); foundDocs.clear(); foundDocs = fullTextIndex->query(terms, termSeq, seqWords); QString msg = QString::fromLatin1("%1 documents found.").arg(foundDocs.count()); help->statusBar()->showMessage(tr(msg.toUtf8()), 3000); ui.resultBox->clear(); for (it = foundDocs.begin(); it != foundDocs.end(); ++it) ui.resultBox->addItem(fullTextIndex->getDocumentTitle(*it)); terms.clear(); bool isPhrase = false; QString s; for (int i = 0; i < (int)buf.length(); ++i) { if (buf[i] == QLatin1Char('\"')) { isPhrase = !isPhrase; s = s.simplified(); if (!s.isEmpty()) terms << s; s = QLatin1String(""); } else if (buf[i] == QLatin1Char(' ') && !isPhrase) { s = s.simplified(); if (!s.isEmpty()) terms << s; s = QLatin1String(""); } else s += buf[i]; } if (!s.isEmpty()) terms << s; setCursor(Qt::ArrowCursor);}void HelpDialog::on_helpButton_clicked(){ emit showLink(MainWindow::urlifyFileName( Config::configuration()->assistantDocPath() + QLatin1String("/assistant-manual.html#full-text-searching")));}void HelpDialog::on_resultBox_itemActivated(QListWidgetItem *item){ showResultPage(item);}void HelpDialog::showResultPage(QListWidgetItem *item){ if (item) emit showSearchLink(foundDocs[ui.resultBox->row(item)], terms);}void HelpDialog::showIndexItemMenu(const QPoint &pos){ QListView *listView = qobject_cast<QListView*>(sender()); if (!listView) return; QModelIndex idx = listView->indexAt(pos); if (!idx.isValid()) return; QAction *action = itemPopup->exec(listView->viewport()->mapToGlobal(pos)); if (action == actionOpenCurrentTab) { showTopic(); } else if (action) { HelpWindow *hw = help->browsers()->currentBrowser(); QString itemName = idx.data().toString(); ui.editIndex->setText(itemName); QStringList links = indexModel->links(idx.row()); if (links.count() == 1) { if (action == actionOpenLinkInNewWindow) hw->openLinkInNewWindow(links.first()); else hw->openLinkInNewPage(links.first()); } else { QStringList::Iterator it = links.begin(); QStringList linkList; QStringList linkNames; for (; it != links.end(); ++it) { linkList << *it; linkNames << titleOfLink(*it); } QString link = TopicChooser::getLink(this, linkNames, linkList, itemName); if (!link.isEmpty()) { if (action == actionOpenLinkInNewWindow) hw->openLinkInNewWindow(link); else hw->openLinkInNewPage(link); } } }}void HelpDialog::showListItemMenu(const QPoint &pos){ QListWidget *listWidget = qobject_cast<QListWidget*>(sender()); if (!listWidget) return; QListWidgetItem *item = listWidget->itemAt(pos); if (!item) return; QAction *action = itemPopup->exec(listWidget->viewport()->mapToGlobal(pos)); if (action == actionOpenCurrentTab) { showResultPage(item); } else if (action) { HelpWindow *hw = help->browsers()->currentBrowser(); QString link = foundDocs[ui.resultBox->row(item)]; if (action == actionOpenLinkInNewWindow) hw->openLinkInNewWindow(link); else hw->openLinkInNewPage(link); }}void HelpDialog::showTreeItemMenu(const QPoint &pos){ QTreeWidget *treeWidget = qobject_cast<QTreeWidget*>(sender()); if (!treeWidget) return; QTreeWidgetItem *item = treeWidget->itemAt(pos); if (!item) return; QAction *action = itemPopup->exec(treeWidget->viewport()->mapToGlobal(pos)); if (action == actionOpenCurrentTab) { if (ui.tabWidget->currentWidget()->objectName() == QLatin1String("contentPage")) showContentsTopic(); else showBookmarkTopic(); } else if (action) { QTreeWidgetItem *i = (QTreeWidgetItem*)item; if (action == actionOpenLinkInNewWindow) help->browsers()->currentBrowser()->openLinkInNewWindow(i->data(0, LinkRole).toString()); else help->browsers()->currentBrowser()->openLinkInNewPage(i->data(0, LinkRole).toString()); }}void HelpDialog::on_termsEdit_returnPressed(){ startSearch();}void HelpDialog::updateSearchButton(const QString &txt){ ui.searchButton->setDisabled(txt.isEmpty());}void HelpDialog::on_searchButton_clicked(){ startSearch();}QString HelpDialog::removeAnchorFromLink(const QString &link){ int i = link.length(); int j = link.lastIndexOf(QLatin1Char('/')); int l = link.lastIndexOf(QDir::separator()); if (l > j) j = l; if (j > -1) { QString fileName = link.mid(j+1); int k = fileName.lastIndexOf(QLatin1Char('#')); if (k > -1) i = j + k + 1; } return link.left(i);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?