tabbedbrowser.cpp

来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 526 行 · 第 1/2 页

CPP
526
字号
void TabbedBrowser::transferFocus(){    if(currentBrowser()) {        currentBrowser()->setFocus();    }    mainWindow()->setWindowTitle(Config::configuration()->title()                             + QLatin1String(" - ")                             + currentBrowser()->documentTitle());}void TabbedBrowser::initHelpWindow(HelpWindow * /*win*/){}void TabbedBrowser::setup(){    newTab(QString());}void TabbedBrowser::copy(){    currentBrowser()->copy();}void TabbedBrowser::closeTab(){    if(ui.tab->count()==1)        return;    HelpWindow *win = currentBrowser();    mainWindow()->removePendingBrowser(win);    ui.tab->removeTab(ui.tab->indexOf(win));    QTimer::singleShot(0, win, SLOT(deleteLater()));    ui.tab->cornerWidget(Qt::TopRightCorner)->setEnabled(ui.tab->count() > 1);    emit tabCountChanged(ui.tab->count());}QStringList TabbedBrowser::sources() const{    QStringList lst;    int cnt = ui.tab->count();    for(int i=0; i<cnt; i++) {        lst.append(((QTextBrowser*) ui.tab->widget(i))->source().toString());    }    return lst;}QList<HelpWindow*> TabbedBrowser::browsers() const{    QList<HelpWindow*> list;    for (int i=0; i<ui.tab->count(); ++i) {        Q_ASSERT(::qobject_cast<HelpWindow*>(ui.tab->widget(i)));        list.append(static_cast<HelpWindow*>(ui.tab->widget(i)));    }    return list;}void TabbedBrowser::sourceChanged(){    HelpWindow *win = ::qobject_cast<HelpWindow *>(QObject::sender());    Q_ASSERT(win);    QString docTitle(win->documentTitle());    if (docTitle.isEmpty())        docTitle = QLatin1String("...");    // Make the classname in the title a bit more visible (otherwise    // we just see the "Qt 4.0 : Q..." which isn't really helpful ;-)    QString qtTitle = QLatin1String("Qt ") + QString::number( (QT_VERSION >> 16) & 0xff )        + QLatin1String(".") + QString::number( (QT_VERSION >> 8) & 0xff )        + QLatin1String(": ");    if (docTitle.startsWith(qtTitle))        docTitle = docTitle.mid(qtTitle.length());    setTitle(win, docTitle);	ui.frameFind->hide();    ui.labelWrapped->hide();	win->setTextCursor(win->cursorForPosition(QPoint(0, 0)));}void TabbedBrowser::setTitle(HelpWindow *win, const QString &title){    const QString tt = title.trimmed();    ui.tab->setTabText(ui.tab->indexOf(win), tt);    if (win == currentBrowser())        mainWindow()->setWindowTitle(Config::configuration()->title() + QLatin1String(" - ") + tt);}void TabbedBrowser::keyPressEvent(QKeyEvent *e){	int key = e->key();	QString ttf = ui.editFind->text();	QString text = e->text();	if (ui.frameFind->isVisible()) {		switch (key) {		case Qt::Key_Escape:			ui.frameFind->hide();            ui.labelWrapped->hide();			return;		case Qt::Key_Backspace:			ttf.chop(1);			break;		case Qt::Key_Return:        case Qt::Key_Enter:			// Return/Enter key events are not accepted by QLineEdit			return;		default:			if (text.isEmpty()) {				QWidget::keyPressEvent(e);                                return;                        }			ttf += text;		}	} else {		if (text.isEmpty() || text[0].isSpace() || !text[0].isPrint()) {			QWidget::keyPressEvent(e);                        return;                }        if (text.startsWith(QLatin1Char('/'))) {            ui.editFind->clear();            find();            return;        }		ttf = text;		ui.frameFind->show();	}	ui.editFind->setText(ttf);	find(ttf, false, false);}void TabbedBrowser::findNext(){	find(ui.editFind->text(), true, false);}void TabbedBrowser::findPrevious(){	find(ui.editFind->text(), false, true);}void TabbedBrowser::find(){	ui.frameFind->show();	ui.editFind->setFocus(Qt::ShortcutFocusReason);	ui.editFind->selectAll();	autoHideTimer->stop();}void TabbedBrowser::find(QString ttf, bool forward, bool backward){	HelpWindow *browser = currentBrowser();	QTextDocument *doc = browser->document();	QString oldText = ui.editFind->text();	QTextCursor c = browser->textCursor();	QTextDocument::FindFlags options;	QPalette p = ui.editFind->palette();	p.setColor(QPalette::Active, QPalette::Base, Qt::white);	if (c.hasSelection())		c.setPosition(forward ? c.position() : c.anchor(), QTextCursor::MoveAnchor);	QTextCursor newCursor = c;	if (!ttf.isEmpty()) {		if (backward)			options |= QTextDocument::FindBackward;		if (ui.checkCase->isChecked())			options |= QTextDocument::FindCaseSensitively;		if (ui.checkWholeWords->isChecked())			options |= QTextDocument::FindWholeWords;		newCursor = doc->find(ttf, c, options);		ui.labelWrapped->hide();		if (newCursor.isNull()) {			QTextCursor ac(doc);			ac.movePosition(options & QTextDocument::FindBackward							? QTextCursor::End : QTextCursor::Start);			newCursor = doc->find(ttf, ac, options);			if (newCursor.isNull()) {				p.setColor(QPalette::Active, QPalette::Base, QColor(255, 102, 102));				newCursor = c;			} else				ui.labelWrapped->show();		}	}	if (!ui.frameFind->isVisible())		ui.frameFind->show();	browser->setTextCursor(newCursor);	ui.editFind->setPalette(p);	if (!ui.editFind->hasFocus())		autoHideTimer->start();}bool TabbedBrowser::eventFilter(QObject *o, QEvent *e){    if (o == ui.editFind) {        if (e->type() == QEvent::FocusIn && autoHideTimer->isActive())            autoHideTimer->stop();    } else if (e->type() == QEvent::KeyPress && ui.frameFind->isVisible()) { // assume textbrowser		QKeyEvent *ke = static_cast<QKeyEvent *>(e);		if (ke->key() == Qt::Key_Space) {			keyPressEvent(ke);			return true;		}	}	return QWidget::eventFilter(o, e);}void TabbedBrowser::openTabMenu(const QPoint& pos){    QTabBar *tabBar = qFindChild<QTabBar*>(ui.tab);    QMenu *m = new QMenu(tabBar);    QAction *new_action = m->addAction(tr("New Tab"));    QAction *close_action = m->addAction(tr("Close Tab"));    QAction *close_others_action = m->addAction(tr("Close Other Tabs"));    if (tabBar->count() < 2) {        close_action->setEnabled(false);        close_others_action->setEnabled(false);    }    QAction *action_picked = m->exec(tabBar->mapToGlobal(pos));    if (action_picked) {        if (action_picked == new_action) {            newTab();        } else if (action_picked == close_action) {            for (int i=0; i< tabBar->count(); ++i) {                if (tabBar->tabRect(i).contains(pos)) {                    HelpWindow *win = static_cast<HelpWindow*>(ui.tab->widget(i));                    mainWindow()->removePendingBrowser(win);                    QTimer::singleShot(0, win, SLOT(deleteLater()));                    ui.tab->cornerWidget(Qt::TopRightCorner)->setEnabled(ui.tab->count() > 1);                    emit tabCountChanged(ui.tab->count());                    break;                }            }        } else if (action_picked == close_others_action) {            int current_tab_index = -1;            for (int i=0; i< tabBar->count(); ++i) {                if (tabBar->tabRect(i).contains(pos)) {                    current_tab_index = i;                    break;                }            }            for (int i=tabBar->count()-1; i>=0; --i) {                if (i == current_tab_index) {                    continue;                } else {                    HelpWindow *win = static_cast<HelpWindow*>(ui.tab->widget(i));                    mainWindow()->removePendingBrowser(win);                    QTimer::singleShot(0, win, SLOT(deleteLater()));                    if (i < current_tab_index)                        --current_tab_index;                }            }            ui.tab->cornerWidget(Qt::TopRightCorner)->setEnabled(ui.tab->count() > 1);            emit tabCountChanged(ui.tab->count());        }    }    delete m;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?