📄 qwhatsthis.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qwhatsthis.h"#ifndef QT_NO_WHATSTHIS#include "qpointer.h"#include "qapplication.h"#include "qdesktopwidget.h"#include "qevent.h"#include "qpixmap.h"#include "qpainter.h"#include "qtimer.h"#include "qhash.h"#include "qaction.h"#include "qcursor.h"#include "qbitmap.h"#include "qtextdocument.h"#include "../text/qtextdocumentlayout_p.h"#include "qtoolbutton.h"#include "qdebug.h"#ifndef QT_NO_ACCESSIBILITY#include "qaccessible.h"#endif#if defined(Q_WS_WIN)#include "qt_windows.h"#ifndef SPI_GETDROPSHADOW#define SPI_GETDROPSHADOW 0x1024#endif#endif#if defined(Q_WS_X11)#include "qx11info_x11.h"#include <qwidget.h>#endif/*! \class QWhatsThis \brief The QWhatsThis class provides a simple description of any widget, i.e. answering the question "What's This?". \ingroup helpsystem \mainclass "What's This?" help is part of an application's online help system, and provides users with information about the functionality and usage of a particular widget. "What's This?" help texts are typically longer and more detailed than \link QToolTip tooltips\endlink, but generally provide less information than that supplied by separate help windows. QWhatsThis provides a single window with an explanatory text that pops up when the user asks "What's This?". The default way for users to ask the question is to move the focus to the relevant widget and press Shift+F1. The help text appears immediately; it goes away as soon as the user does something else. (Note that if there is a shortcut for Shift+F1, this mechanism will not work.) Some dialogs provide a "?" button that users can click to enter "What's This?" mode; they then click the relevant widget to pop up the "What's This?" window. It is also possible to provide a a menu option or toolbar button to switch into "What's This?" mode. To add "What's This?" text to a widget or an action, you simply call QWidget::setWhatsThis() or QAction::setWhatsThis(). The text can be either rich text or plain text. If you specify a rich text formatted string, it will be rendered using the default stylesheet, making it possible to embed images in the displayed text. To be as fast as possible, the default stylesheet uses a simple method to determine whether the text can be rendered as plain text. See Qt::mightBeRichText() for details. \quotefile snippets/whatsthis/whatsthis.cpp \skipto newAct = \printuntil setWhatsThis An alternative way to enter "What's This?" mode is to call createAction(), and add the returned QAction to either a menu or a tool bar. By invoking this context help action (in the picture below, the button with the arrow and question mark icon) the user switches into "What's This?" mode. If they now click on a widget the appropriate help text is shown. The mode is left when help is given or when the user presses Esc. \img whatsthis.png You can enter "What's This?" mode programmatically with enterWhatsThisMode(), check the mode with inWhatsThisMode(), and return to normal mode with leaveWhatsThisMode(). If you want to control the "What's This?" behavior of a widget manually see Qt::WA_CustomWhatsThis. It is also possible to show different help texts for different regions of a widget, by using a QHelpEvent of type QEvent::WhatsThis. Intercept the help event in your widget's QWidget::event() function and call QWhatsThis::showText() with the text you want to display for the position specified in QHelpEvent::pos(). If the text is rich text and the user clicks on a link, the widget also receives a QWhatsThisClickedEvent with the link's reference as QWhatsThisClickedEvent::href(). If a QWhatsThisClickedEvent is handled (i.e. QWidget::event() returns true), the help window remains visible. Call QWhatsThis::hideText() to hide it explicitly. \sa QToolTip*/class QWhatsThat : public QWidget{ Q_OBJECTpublic: QWhatsThat(const QString& txt, QWidget* parent, QWidget *showTextFor); ~QWhatsThat() ; static QWhatsThat *instance;protected: void showEvent(QShowEvent *e); void mousePressEvent(QMouseEvent*); void mouseReleaseEvent(QMouseEvent*); void mouseMoveEvent(QMouseEvent*); void keyPressEvent(QKeyEvent*); void paintEvent(QPaintEvent*);private: QPointer<QWidget>widget; bool pressed; QString text; QTextDocument* doc; QString anchor; QPixmap background;};QWhatsThat *QWhatsThat::instance = 0;// shadowWidth not const, for XP drop-shadow-fu turns it to 0static int shadowWidth = 6; // also used as '5' and '6' and even '8' belowstatic const int vMargin = 8;static const int hMargin = 12;QWhatsThat::QWhatsThat(const QString& txt, QWidget* parent, QWidget *showTextFor) : QWidget(parent, Qt::Popup), widget(showTextFor), pressed(false), text(txt){ delete instance; instance = this; setAttribute(Qt::WA_DeleteOnClose, true); setAttribute(Qt::WA_NoSystemBackground, true); QPalette pal(Qt::black, QColor(255,255,238), QColor(96,96,96), QColor(192,192,192), Qt::black, Qt::black, QColor(255,255,238)); setPalette(pal); setMouseTracking(true); setFocusPolicy(Qt::StrongFocus);#ifndef QT_NO_CURSOR setCursor(Qt::ArrowCursor);#endif QRect r; doc = 0; if (Qt::mightBeRichText(text)) { doc = new QTextDocument(); doc->setUndoRedoEnabled(false); doc->setDefaultFont(QApplication::font(this)); doc->setHtml(text); doc->setUndoRedoEnabled(false); doc->adjustSize(); r.setTop(0); r.setLeft(0); r.setSize(doc->size().toSize()); } else { int sw = QApplication::desktop()->width() / 3; if (sw < 200) sw = 200; else if (sw > 300) sw = 300; r = fontMetrics().boundingRect(0, 0, sw, 1000, Qt::AlignLeft + Qt::AlignTop + Qt::TextWordWrap + Qt::TextExpandTabs, text); }#if defined(Q_WS_WIN) if ((QSysInfo::WindowsVersion&QSysInfo::WV_NT_based) > QSysInfo::WV_2000) { BOOL shadow; SystemParametersInfo(SPI_GETDROPSHADOW, 0, &shadow, 0); shadowWidth = shadow ? 0 : 6; }#endif resize(r.width() + 2*hMargin + shadowWidth, r.height() + 2*vMargin + shadowWidth);}QWhatsThat::~QWhatsThat(){ instance = 0; if (doc) delete doc;}void QWhatsThat::showEvent(QShowEvent *){ background = QPixmap::grabWindow(QApplication::desktop()->internalWinId(), x(), y(), width(), height());}void QWhatsThat::mousePressEvent(QMouseEvent* e){ pressed = true; if (e->button() == Qt::LeftButton && rect().contains(e->pos())) { if (doc) anchor = doc->documentLayout()->anchorAt(e->pos() - QPoint(hMargin, vMargin)); return; } close();}void QWhatsThat::mouseReleaseEvent(QMouseEvent* e){ if (!pressed) return; if (widget && e->button() == Qt::LeftButton && doc && rect().contains(e->pos())) { QString a = doc->documentLayout()->anchorAt(e->pos() - QPoint(hMargin, vMargin)); QString href; if (anchor == a) href = a; anchor.clear(); if (!href.isEmpty()) { QWhatsThisClickedEvent e(href); if (QApplication::sendEvent(widget, &e)) return; } } close();}void QWhatsThat::mouseMoveEvent(QMouseEvent* e){#ifdef QT_NO_CURSOR Q_UNUSED(e);#else if (!doc) return; QString a = doc->documentLayout()->anchorAt(e->pos() - QPoint(hMargin, vMargin)); if (!a.isEmpty()) setCursor(Qt::PointingHandCursor); else setCursor(Qt::ArrowCursor);#endif}void QWhatsThat::keyPressEvent(QKeyEvent*){ close();}void QWhatsThat::paintEvent(QPaintEvent*){ bool drawShadow = true;#if defined(Q_WS_WIN) if ((QSysInfo::WindowsVersion&QSysInfo::WV_NT_based) > QSysInfo::WV_2000) { BOOL shadow; SystemParametersInfo(SPI_GETDROPSHADOW, 0, &shadow, 0); drawShadow = !shadow; }#elif defined(Q_WS_MAC) || defined(Q_WS_QWS) drawShadow = false; // never draw it on OS X or QWS, as we get it for free#endif QRect r = rect(); r.adjust(0, 0, -1, -1); if (drawShadow) r.adjust(0, 0, -shadowWidth, -shadowWidth); QPainter p(this); p.drawPixmap(0, 0, background); p.setPen(palette().foreground().color()); p.setBrush(palette().brush(QPalette::Window)); p.drawRect(r); int w = r.width(); int h = r.height(); p.setPen(palette().brush(QPalette::Dark).color()); p.drawRect(1, 1, w-2, h-2); if (drawShadow) { p.setPen(palette().shadow().color()); p.drawPoint(w + 5, 6); p.drawLine(w + 3, 6, w + 5, 8); p.drawLine(w + 1, 6, w + 5, 10); int i; for(i=7; i < h; i += 2) p.drawLine(w, i, w + 5, i + 5); for(i = w - i + h; i > 6; i -= 2) p.drawLine(i, h, i + 5, h + 5); for(; i > 0 ; i -= 2) p.drawLine(6, h + 6 - i, i + 5, h + 5); } r.adjust(0, 0, 1, 1); p.setPen(palette().foreground().color()); r.adjust(hMargin, vMargin, -hMargin, -vMargin); if (doc) { p.translate(r.x(), r.y()); QRect rect = r; rect.translate(-r.x(), -r.y()); p.setClipRect(rect); QAbstractTextDocumentLayout::PaintContext context; doc->documentLayout()->draw(&p, context); } else { p.drawText(r, Qt::AlignLeft + Qt::AlignTop + Qt::TextWordWrap + Qt::TextExpandTabs, text); }}static const char * const button_image[] = {"16 16 3 1"," c None","o c #000000","a c #000080","o aaaaa ","oo aaa aaa ","ooo aaa aaa","oooo aa aa","ooooo aa aa","oooooo a aaa","ooooooo aaa ","oooooooo aaa ","ooooooooo aaa ","ooooo aaa ","oo ooo ","o ooo aaa "," ooo aaa "," ooo "," ooo "," ooo "};class QWhatsThisPrivate : public QObject{ public: QWhatsThisPrivate(); ~QWhatsThisPrivate(); static QWhatsThisPrivate *instance; bool eventFilter(QObject *, QEvent *); QPointer<QAction> action;#ifdef QT3_SUPPORT
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -