📄 qwhatsthis.cpp
字号:
QPointer<QToolButton> button;#endif static void say(QWidget *, const QString &, int x = 0, int y = 0); static void notifyToplevels(QEvent *e);};void QWhatsThisPrivate::notifyToplevels(QEvent *e){ QWidgetList toplevels = QApplication::topLevelWidgets(); for (int i = 0; i < toplevels.count(); ++i) { register QWidget *w = toplevels.at(i); QApplication::sendEvent(w, e); }}QWhatsThisPrivate *QWhatsThisPrivate::instance = 0;QWhatsThisPrivate::QWhatsThisPrivate(){ instance = this; qApp->installEventFilter(this); QPoint pos = QCursor::pos(); if (QWidget *w = QApplication::widgetAt(pos)) { QHelpEvent e(QEvent::QueryWhatsThis, w->mapFromGlobal(pos), pos); bool sentEvent = QApplication::sendEvent(w, &e);#ifdef QT_NO_CURSOR Q_UNUSED(sentEvent);#else QApplication::setOverrideCursor((!sentEvent || !e.isAccepted())? Qt::ForbiddenCursor:Qt::WhatsThisCursor); } else { QApplication::setOverrideCursor(Qt::WhatsThisCursor);#endif }#ifndef QT_NO_ACCESSIBILITY QAccessible::updateAccessibility(this, 0, QAccessible::ContextHelpStart);#endif}QWhatsThisPrivate::~QWhatsThisPrivate(){ if (action) action->setChecked(false);#ifdef QT3_SUPPORT if (button) button->setChecked(false);#endif#ifndef QT_NO_CURSOR QApplication::restoreOverrideCursor();#endif#ifndef QT_NO_ACCESSIBILITY QAccessible::updateAccessibility(this, 0, QAccessible::ContextHelpEnd);#endif instance = 0;}bool QWhatsThisPrivate::eventFilter(QObject *o, QEvent *e){ if (!o->isWidgetType()) return false; QWidget * w = static_cast<QWidget *>(o); bool customWhatsThis = w->testAttribute(Qt::WA_CustomWhatsThis); switch (e->type()) { case QEvent::MouseButtonPress: { QMouseEvent *me = static_cast<QMouseEvent*>(e); if (me->button() == Qt::RightButton || customWhatsThis) return false; QHelpEvent e(QEvent::WhatsThis, me->pos(), me->globalPos()); if (!QApplication::sendEvent(w, &e) || !e.isAccepted()) QWhatsThis::leaveWhatsThisMode(); } break; case QEvent::MouseMove: { QMouseEvent *me = static_cast<QMouseEvent*>(e); QHelpEvent e(QEvent::QueryWhatsThis, me->pos(), me->globalPos()); bool sentEvent = QApplication::sendEvent(w, &e);#ifdef QT_NO_CURSOR Q_UNUSED(sentEvent);#else QApplication::changeOverrideCursor((!sentEvent || !e.isAccepted())? Qt::ForbiddenCursor:Qt::WhatsThisCursor);#endif } // fall thorugh case QEvent::MouseButtonRelease: case QEvent::MouseButtonDblClick: if (static_cast<QMouseEvent*>(e)->button() == Qt::RightButton || customWhatsThis) return false; // ignore RMB release break; case QEvent::KeyPress: { QKeyEvent* kev = (QKeyEvent*)e; if (kev->key() == Qt::Key_Escape) { QWhatsThis::leaveWhatsThisMode(); return true; } else if (customWhatsThis) { return false; } else if (kev->key() == Qt::Key_Menu || (kev->key() == Qt::Key_F10 && kev->modifiers() == Qt::ShiftModifier)) { // we don't react to these keys, they are used for context menus return false; } else if (kev->key() != Qt::Key_Shift && kev->key() != Qt::Key_Alt // not a modifier key && kev->key() != Qt::Key_Control && kev->key() != Qt::Key_Meta) { QWhatsThis::leaveWhatsThisMode(); } } break; default: return false; } return true;}class QWhatsThisAction: public QAction{ Q_OBJECTpublic: explicit QWhatsThisAction(QObject* parent = 0);private slots: void actionTriggered();};QWhatsThisAction::QWhatsThisAction(QObject *parent) : QAction(tr("What's This?"), parent){#ifndef QT_NO_IMAGEFORMAT_XPM QPixmap p((const char**)button_image); setIcon(p);#endif setCheckable(true); connect(this, SIGNAL(triggered()), this, SLOT(actionTriggered()));#ifndef QT_NO_SHORTCUT setShortcut(Qt::ShiftModifier + Qt::Key_F1);#endif}void QWhatsThisAction::actionTriggered(){ if (isChecked()) { QWhatsThis::enterWhatsThisMode(); QWhatsThisPrivate::instance->action = this; }}QWhatsThis::QWhatsThis(){}#ifdef QT3_SUPPORT/*! \obsolete Sets the What's This text \a s for the widget \a w. Use QWidget::setWhatsThis() or QAction::setWhatsThis() instead.*/void QWhatsThis::add(QWidget *w, const QString &s){ w->setWhatsThis(s);}/*! \obsolete Remove's the What's This text for the widget \a w. Use QWidget::setWhatsThis() or QAction::setWhatsThis() instead.*/void QWhatsThis::remove(QWidget *w){ w->setWhatsThis(QString());}class QWhatsThisButton : public QToolButton{ Q_OBJECTpublic: QWhatsThisButton(QWidget *p) : QToolButton(p) { setCheckable(true); QPixmap pix( const_cast<const char**>(button_image) ); setIcon( pix ); QObject::connect(this, SIGNAL(toggled(bool)), this, SLOT(whatToggled(bool))); setAutoRaise(true); setFocusPolicy(Qt::NoFocus); }public slots: void whatToggled(bool b) { if (b) { QWhatsThis::enterWhatsThisMode(); QWhatsThisPrivate::instance->button = this; } }};/*! Returns a new "What's This?" QToolButton with the given \a parent. To do this now, create your own QToolButton and a QWhatsThis object and call the QWhatsThis object's showText() function when the QToolButton is invoked. Use createAction() instead.*/QToolButton * QWhatsThis::whatsThisButton(QWidget * parent){ return new QWhatsThisButton(parent);}#endif/*! This function switches the user interface into "What's This?" mode. The user interface can be switched back into normal mode by the user (e.g. by them clicking or pressing Esc), or programmatically by calling leaveWhatsThisMode(). When entering "What's This?" mode, a QEvent of type Qt::EnterWhatsThisMode is sent to all toplevel widgets. \sa inWhatsThisMode() leaveWhatsThisMode()*/void QWhatsThis::enterWhatsThisMode(){ if (QWhatsThisPrivate::instance) return; (void) new QWhatsThisPrivate; QEvent e(QEvent::EnterWhatsThisMode); QWhatsThisPrivate::notifyToplevels(&e); }/*! Returns true if the user interface is in "What's This?" mode; otherwise returns false. \sa enterWhatsThisMode()*/bool QWhatsThis::inWhatsThisMode(){ return (QWhatsThisPrivate::instance != 0);}/*! If the user interface is in "What's This?" mode, this function switches back to normal mode; otherwise it does nothing. When leaving "What's This?" mode, a QEvent of type Qt::LeaveWhatsThisMode is sent to all toplevel widgets. \sa enterWhatsThisMode() inWhatsThisMode()*/void QWhatsThis::leaveWhatsThisMode(){ delete QWhatsThisPrivate::instance; QEvent e(QEvent::LeaveWhatsThisMode); QWhatsThisPrivate::notifyToplevels(&e);}void QWhatsThisPrivate::say(QWidget * widget, const QString &text, int x, int y){ if (text.size() == 0) return; // make a fresh widget, and set it up QWhatsThat *whatsThat = new QWhatsThat( text,#if defined(Q_WS_X11) QApplication::desktop()->screen(widget ? widget->x11Info().screen() : QCursor::x11Screen()),#else 0,#endif widget ); // okay, now to find a suitable location int scr = (widget ? QApplication::desktop()->screenNumber(widget) :#if defined(Q_WS_X11) QCursor::x11Screen()#else QApplication::desktop()->screenNumber(QPoint(x,y))#endif // Q_WS_X11 ); QRect screen = QApplication::desktop()->screenGeometry(scr); int w = whatsThat->width(); int h = whatsThat->height(); int sx = screen.x(); int sy = screen.y(); // first try locating the widget immediately above/below, // with nice alignment if possible. QPoint pos; if (widget) pos = widget->mapToGlobal(QPoint(0,0)); if (widget && w > widget->width() + 16) x = pos.x() + widget->width()/2 - w/2; else x = x - w/2; // squeeze it in if that would result in part of what's this // being only partially visible if (x + w + shadowWidth > sx+screen.width()) x = (widget? (qMin(screen.width(), pos.x() + widget->width()) ) : screen.width()) - w; if (x < sx) x = sx; if (widget && h > widget->height() + 16) { y = pos.y() + widget->height() + 2; // below, two pixels spacing // what's this is above or below, wherever there's most space if (y + h + 10 > sy+screen.height()) y = pos.y() + 2 - shadowWidth - h; // above, overlap } y = y + 2; // squeeze it in if that would result in part of what's this // being only partially visible if (y + h + shadowWidth > sy+screen.height()) y = (widget ? (qMin(screen.height(), pos.y() + widget->height()) ) : screen.height()) - h; if (y < sy) y = sy; whatsThat->move(x, y); whatsThat->show(); whatsThat->grabKeyboard();}/*! Shows \a text as a "What's This?" window, at global position \a pos. The optional widget argument, \a w, is used to determine the appropriate screen on multi-head systems. \sa hideText()*/void QWhatsThis::showText(const QPoint &pos, const QString &text, QWidget *w){ leaveWhatsThisMode(); QWhatsThisPrivate::say(w, text, pos.x(), pos.y());}/*! If a "What's This?" window is showing, this destroys it. \sa showText()*/void QWhatsThis::hideText(){ delete QWhatsThat::instance;}/*! Returns a ready-made QAction, used to invoke "What's This?" context help, with the given \a parent. The returned QAction provides a convenient way to let users enter "What's This?" mode.*/QAction *QWhatsThis::createAction(QObject *parent){ return new QWhatsThisAction(parent);}#include "qwhatsthis.moc"#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -