📄 evachatview.cpp
字号:
/*************************************************************************** * Copyright (C) 2005 by yunfan * * yunfan_zg@163.com * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/#include "evachatview.h"#include "evahtmlparser.h"#include "evaresource.h"#include "../evamain.h"#include <qstringlist.h>#include <qtimer.h>#include <qclipboard.h>#include <qfileinfo.h>#include <qdragobject.h>#include <qapplication.h>#include <kaction.h>#include <kconfig.h>#include <kservice.h>#include <kurifilter.h>#include <kmimetype.h>#include <kstandarddirs.h> // function "locate"#include <kiconloader.h> // function "SmallIcon"#include <kdesktopfile.h>#include <kurldrag.h>#include <kmultipledrag.h>#include <kmessagebox.h>#include <kfiledialog.h>#include <kio/job.h>#include <kpopupmenu.h>#include <kapplication.h>#include <klocale.h>/// defines come from khtml/misc/htmltags.h//#define ATTR_HREF 54//#define ATTR_TARGET 133#define ID_IMG 48#include <dom/html_element.h>#include <dom/html_image.h>#define SCROLL_DELAY 200#define MIN_FONT_SIZE 8#define MAX_FONT_SIZE 20class MenuPrivateData{public: KURL m_url; KURL m_imageURL; QPixmap m_pixmap; QString m_suggestedFilename;};const QString EvaChatView::protocolAccept = "cmd://accept";const QString EvaChatView::protocolSaveAs = "cmd://saveas";const QString EvaChatView::protocolCancel = "cmd://cancel";const QString EvaChatView::protocolResume = "cmd://resume";const QString EvaChatView::protocolNewOne = "cmd://newone";EvaChatView::EvaChatView( QWidget * parent, const char * name ) : KHTMLPart(parent, name), menu(NULL), d(0), buffer(""){ //setOnlyLocalReferences(true); menu = new KPopupMenu(0, "popup"); copyAction = KStdAction::copy( this, SLOT(copy()), actionCollection()); copyAction->setText(i18n("&Copy Text")); setDNDEnabled(true); setAutoloadImages(true); view()->setHScrollBarMode(QScrollView::AlwaysOff); //setStandardFont("Helvetica"); //buffer += "<body style=\"font-size:9pt;font-family:Helvetica\">"; QObject::connect(this, SIGNAL(selectionChanged()), SLOT(slotSelectionChanged())); QObject::connect(this, SIGNAL(popupMenu(const QString &, const QPoint &)), SLOT(slotPopupMenu(const QString &, const QPoint &))); QObject::connect(browserExtension(), SIGNAL(openURLRequest(const KURL &, const KParts::URLArgs &)), SLOT(slotLinkClicked( const KURL &, const KParts::URLArgs &)));}EvaChatView::~EvaChatView(){ if(menu) delete menu; if(d) delete d;}QString EvaChatView::wrapFontAttributes(QColor color, Q_UINT8 size, bool underline, bool italic, bool bold, QString contents){ QString fontHead = "<span style=\""; QString fontSize = "font-size: "+QString::number((size<=MIN_FONT_SIZE)?MIN_FONT_SIZE:((size>MAX_FONT_SIZE)?MAX_FONT_SIZE:size))+"pt; "; QString fontColor = QString("color: ") + color.name() + QString("\" >"); QString fontStye = "", fontStyeEnd=""; if(bold){ fontStye+="<b>"; fontStyeEnd+="</b>"; } if(italic){ fontStye+="<i>"; fontStyeEnd="</i>" + fontStyeEnd; } if(underline){ fontStye+="<u>"; fontStyeEnd="</u>" + fontStyeEnd; } QString fontEnd = "</span>"; QString ret = fontHead + fontSize + fontColor + fontStye + contents + fontStyeEnd + fontEnd; return ret;}QString EvaChatView::wrapNickName(QString &nick, QDateTime time, QColor color, bool isNormal){ QString htmlName = nick; EvaHtmlParser parser; parser.setAbsImagePath(EvaMain::images->getSmileyPath()); parser.convertToHtml(htmlName, false, true); QString msg = "<span style=\"font-size: 9pt; color: " + color.name() +"\">" + htmlName + " "; if(!isNormal) msg+=i18n("(Auto-Reply)"); msg+=" "+time.toString("yyyy-MM-dd hh:mm:ss") + "</span><br>"; return msg;}void EvaChatView::append( QString & nick, QDateTime time, QColor nameColor, bool isNormal, QColor msgColor, Q_UINT8 size, bool underline, bool italic, bool bold, QString contents ){ QString msg = wrapNickName(nick, time, nameColor, isNormal) + wrapFontAttributes(msgColor, size, underline, italic, bold, contents); updateContents(msg);}void EvaChatView::updatePicture( const QString filename , const QString tmpFileName){ bool needScroll = ( view()->contentsHeight() == (view()->contentsY() +view()->visibleHeight()) ); buffer.replace(tmpFileName, filename); begin(); write(buffer); write("</body>"); end(); if(needScroll){ QTimer::singleShot(SCROLL_DELAY, this, SLOT(slotScrollToBottom())); }}void EvaChatView::slotScrollToBottom(){ view()->setContentsPos(view()->contentsX(), view()->contentsHeight() - view()->visibleHeight() );}void EvaChatView::slotLinkClicked( const KURL & url, const KParts::URLArgs &/*args*/){ QString cmd = url.url(); QString strSession = cmd.mid(13,cmd.length() - 13); bool ok; unsigned int session = strSession.toUInt(&ok); if(ok){ if(cmd.startsWith(protocolAccept) ){ emit fileTransferAcceptRequest(session); return; } if(cmd.startsWith(protocolSaveAs) ){ emit fileTransferSaveAsRequest(session); return; } if(cmd.startsWith(protocolCancel) ){ emit fileTransferCancelRequest(session); return; } if(cmd.startsWith(protocolResume) ){ emit fileTransferResume(session, true); return; } if(cmd.startsWith(protocolNewOne) ){ emit fileTransferResume(session, false); return; } } QStringList args; if(url.isLocalFile()){ args<<"exec"<< QString::fromLocal8Bit(url.path().ascii()); }else{ args<<"exec" <<cmd; } kapp->kdeinitExec("kfmclient",args);}/* we just simplify the process. if we use KParts::BrowserExtension, we have to do lots extra work, adding so much classes. so just hack like following. grab useful code from KHTMLPopupGUIClient(khtml_ext.cpp), and change a little bit to fit our needs*/void EvaChatView::slotPopupMenu( const QString & _url, const QPoint & point ){ menu->clear(); bool isImage = false; bool hasSelection = KHTMLPart::hasSelection(); KURL url = KURL(_url); if(d) delete d; d = new MenuPrivateData; d->m_url = url; DOM::Element e = nodeUnderMouse(); if ( !e.isNull() && (e.elementId() == ID_IMG) ){ DOM::HTMLImageElement ie = static_cast<DOM::HTMLImageElement>(e); QString src = ie.src().string(); d->m_imageURL = KURL(src); d->m_suggestedFilename = src.right(src.length() - src.findRev("/") -1); isImage=true; } KAction *action = 0L; if(hasSelection){ //action = new KAction( i18n( "&Copy Text" ), KShortcut("Ctrl+C"), this, SLOT( copy() ), // actionCollection(), "copy" ); //action = KStdAction::copy( browserExtension(), SLOT(copy()), actionCollection(), "copy"); //action->setText(i18n("&Copy Text")); //action->setEnabled(true); copyAction->plug(menu); // Fill search provider entries KConfig config("kuriikwsfilterrc"); config.setGroup("General"); const QString defaultEngine = config.readEntry("DefaultSearchEngine", "google"); const char keywordDelimiter = config.readNumEntry("KeywordDelimiter", ':'); // search text QString selectedText = KHTMLPart::selectedText(); if ( selectedText.length()>18 ) { selectedText.truncate(15); selectedText+="..."; } // default search provider KService::Ptr service = KService::serviceByDesktopPath(QString("searchproviders/%1.desktop").arg(defaultEngine)); // search provider icon QPixmap icon; KURIFilterData data; QStringList list; const QString defaultSearchProviderPrefix = *(service->property("Keys").toStringList().begin()) + keywordDelimiter; data.setData( defaultSearchProviderPrefix + QString("some keyword") ); list << "kurisearchfilter" << "kuriikwsfilter"; QString name; if ( KURIFilter::self()->filterURI(data, list) ){ QString iconPath = locate("cache", KMimeType::favIconForURL(data.uri()) + ".png");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -