File indexing completed on 2024-04-21 14:56:00

0001 /*  This file is part of the KDE Libraries
0002  *  Copyright (C) 1999-2000 Espen Sand (espen@kde.org)
0003  *  Copyright (C) 2006 Urs Wolfer <uwolfer at fwo.ch>
0004  *
0005  *  This library is free software; you can redistribute it and/or
0006  *  modify it under the terms of the GNU Library General Public
0007  *  License as published by the Free Software Foundation; either
0008  *  version 2 of the License, or (at your option) any later version.
0009  *
0010  *  This library is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  *  Library General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU Library General Public License
0016  *  along with this library; see the file COPYING.LIB.  If not, write to
0017  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  *  Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "ktextbrowser.h"
0022 
0023 #include <QAction>
0024 #include <QMenu>
0025 #include <QKeyEvent>
0026 #include <QWhatsThis>
0027 
0028 #include <kicontheme.h>
0029 #include <kglobalsettings.h>
0030 #include <qdesktopservices.h>
0031 
0032 class Q_DECL_HIDDEN KTextBrowser::Private
0033 {
0034 public:
0035     Private()
0036         : notifyClick(false)
0037     {
0038     }
0039 
0040     ~Private()
0041     {
0042     }
0043 
0044     bool notifyClick;
0045 };
0046 
0047 KTextBrowser::KTextBrowser(QWidget *parent, bool notifyClick)
0048     : QTextBrowser(parent), d(new Private)
0049 {
0050     d->notifyClick = notifyClick;
0051 }
0052 
0053 KTextBrowser::~KTextBrowser()
0054 {
0055     delete d;
0056 }
0057 
0058 void KTextBrowser::setNotifyClick(bool notifyClick)
0059 {
0060     d->notifyClick = notifyClick;
0061 }
0062 
0063 bool KTextBrowser::isNotifyClick() const
0064 {
0065     return d->notifyClick;
0066 }
0067 
0068 void KTextBrowser::setSource(const QUrl &name)
0069 {
0070     QString strName = name.toString();
0071     if (strName.isNull()) {
0072         return;
0073     }
0074 
0075     QRegExp whatsthis("whatsthis:/*([^/].*)");
0076     if (!d->notifyClick && whatsthis.exactMatch(strName)) {
0077         QWhatsThis::showText(QCursor::pos(), whatsthis.cap(1));
0078     } else if (strName.indexOf('@') > -1) {
0079         if (!d->notifyClick) {
0080             QDesktopServices::openUrl(name);
0081         } else {
0082             emit mailClick(QString(), strName);
0083         }
0084     } else {
0085         if (!d->notifyClick) {
0086             QDesktopServices::openUrl(name);
0087         } else {
0088             emit urlClick(strName);
0089         }
0090     }
0091 }
0092 
0093 void KTextBrowser::keyPressEvent(QKeyEvent *event)
0094 {
0095     if (event->key() == Qt::Key_Escape) {
0096         event->ignore();
0097     } else if (event->key() == Qt::Key_F1) {
0098         event->ignore();
0099     } else {
0100         QTextBrowser::keyPressEvent(event);
0101     }
0102 }
0103 
0104 void KTextBrowser::wheelEvent(QWheelEvent *event)
0105 {
0106     if (KGlobalSettings::wheelMouseZooms()) {
0107         QTextBrowser::wheelEvent(event);
0108     } else { // thanks, we don't want to zoom, so skip QTextEdit's impl.
0109         QAbstractScrollArea::wheelEvent(event);
0110     }
0111 }
0112 
0113 void KTextBrowser::contextMenuEvent(QContextMenuEvent *event)
0114 {
0115     QMenu *popup = createStandardContextMenu(event->pos());
0116     if (popup) {
0117         KIconTheme::assignIconsToContextMenu(isReadOnly() ? KIconTheme::ReadOnlyText
0118                                              : KIconTheme::TextEditor,
0119                                              popup->actions());
0120 
0121         popup->exec(event->globalPos());
0122         delete popup;
0123     }
0124 }
0125 
0126 #include "moc_ktextbrowser.cpp"