BrowserView.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2009 Jürgen Riegel <juergen.riegel@web.de>              *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 
00026 #ifndef _PreComp_
00027 # include <QAbstractTextDocumentLayout>
00028 # include <QApplication>
00029 # include <QClipboard>
00030 # include <QDateTime>
00031 # include <QHBoxLayout>
00032 # include <QMessageBox>
00033 # include <QPainter>
00034 # include <QPrinter>
00035 # include <QPrintDialog>
00036 # include <QScrollBar>
00037 # if QT_VERSION >= 0x040400
00038 # include <QWebFrame>
00039 # include <QWebView>
00040 # include <QWebSettings>
00041 # endif
00042 # include <QStatusBar>
00043 # include <QTextBlock>
00044 # include <QTextCodec>
00045 # include <QTextStream>
00046 # include <QTimer>
00047 # include <QFileInfo>
00048 #endif
00049 
00050 #include "BrowserView.h"
00051 #include <Gui/Application.h>
00052 #include <Gui/MainWindow.h>
00053 #include <Gui/ProgressBar.h>
00054 #include <Gui/DownloadDialog.h>
00055 #include <Gui/Command.h>
00056 #include <Gui/OnlineDocumentation.h>
00057 
00058 #include <Base/Parameter.h>
00059 #include <Base/Exception.h>
00060 
00061 using namespace WebGui;
00062 using namespace Gui;
00063 
00064 
00065 /* TRANSLATOR Gui::BrowserView */
00066 
00071 BrowserView::BrowserView(QWidget* parent)
00072     : MDIView(0,parent,0),
00073       WindowParameter( "Browser" ),
00074       isLoading(false),
00075       textSizeMultiplier(1.0)
00076 {
00077     WebView = new QWebView(this);
00078     setCentralWidget(WebView);
00079 
00080     WebView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
00081 
00082     connect(WebView, SIGNAL(loadStarted()),
00083             this, SLOT(onLoadStarted()));
00084     connect(WebView, SIGNAL(loadProgress(int)),
00085             this, SLOT(onLoadProgress(int)));
00086     connect(WebView, SIGNAL(loadFinished(bool)),
00087             this, SLOT(onLoadFinished()));
00088     connect(WebView, SIGNAL(linkClicked(const QUrl &)),
00089             this, SLOT(onLinkClicked(const QUrl &)));
00090 }
00091 
00093 BrowserView::~BrowserView()
00094 {
00095     delete WebView;
00096 }
00097 
00098 void BrowserView::onLinkClicked (const QUrl & url) 
00099 {
00100     QString scheme   = url.scheme();
00101     QString host     = url.host();
00102 
00103     // path handling 
00104     QString path     = url.path();
00105     QFileInfo fi(path);
00106     QString ext = fi.completeSuffix();
00107 
00108     //QString fragment = url.   fragment();
00109 
00110     if(scheme==QString::fromLatin1("http")){
00111 /*         Dialog::DownloadDialog Dlg (url,QString::fromLatin1("c:/temp/test.fcstd"));
00112         int result = Dlg.exec();
00113        if(ext ==QString::fromLatin1("fcstd") )
00114              Gui::Command::doCommand(Gui::Command::Gui,"Gui.open('%s')",);
00115 
00116         load(url);*/
00117         OpenURLInBrowser(url.toString().toLatin1());
00118     }
00119     // run scripts if not from somewhere else!
00120     if((scheme.size() < 2 || scheme==QString::fromLatin1("file"))&& host.isEmpty()){
00121         QFileInfo fi(path);
00122         if(fi.exists()){
00123             QString ext = fi.completeSuffix();
00124             if (ext == QString::fromLatin1("py")) {
00125                 try {
00126                     Gui::Command::doCommand(Gui::Command::Gui,"execfile('%s')",(const char*) fi.absoluteFilePath().     toLocal8Bit());
00127                 }
00128                 catch (const Base::Exception& e) {
00129                     QMessageBox::critical(this, tr("Error"), QString::fromUtf8(e.what()));
00130                 }
00131             }
00132         }
00133         else {
00134             QMessageBox::warning(Gui::getMainWindow(), QObject::tr("File does not exist!"),
00135             fi.absoluteFilePath ());
00136         }
00137     }
00138 }
00139 
00140 bool BrowserView::chckHostAllowed(const QString& host)
00141 {
00142     // only check if a local file, later we can do here a dialog to ask the user if 
00143     return host.isEmpty();
00144 }
00145 
00146 
00147 void BrowserView::load(const char* URL)
00148 {
00149     QUrl url = QUrl(QString::fromUtf8(URL));
00150     load(url);
00151 }
00152 
00153 void BrowserView::load(const QUrl & url)
00154 {
00155     if(isLoading)
00156         stop();
00157 
00158     WebView->load(url);
00159     WebView->setUrl(url);
00160     if(url.scheme().size() < 2){
00161         QString path     = url.path();
00162         QFileInfo fi(path);
00163         QString name = fi.baseName();
00164 
00165         setWindowTitle(name);
00166     }else 
00167         setWindowTitle(url.host());
00168 
00169     setWindowIcon(QWebSettings::iconForUrl(url));
00170 }
00171 
00172 void BrowserView::setHtml(const QString& HtmlCode,const QUrl & BaseUrl,const QString& TabName)
00173 {
00174     if(isLoading)
00175         stop();
00176 
00177     WebView->setHtml(HtmlCode,BaseUrl);
00178     setWindowTitle(TabName);
00179     setWindowIcon(QWebSettings::iconForUrl(BaseUrl));
00180 }
00181 
00182 void BrowserView::stop(void)
00183 {
00184     WebView->stop();
00185 }
00186 
00187 void BrowserView::onLoadStarted()
00188 {
00189     QProgressBar* bar = Gui::Sequencer::instance()->getProgressBar();
00190     bar->setRange(0, 100);
00191     bar->show();
00192     Gui::getMainWindow()->statusBar()->showMessage(tr("Loading %1...").arg(WebView->url().toString()));
00193     isLoading = true;
00194 }
00195 
00196 void BrowserView::onLoadProgress(int step)
00197 {
00198     QProgressBar* bar = Gui::Sequencer::instance()->getProgressBar();
00199     bar->setValue(step);
00200 }
00201 
00202 void BrowserView::onLoadFinished()
00203 {
00204     QProgressBar* bar = Sequencer::instance()->getProgressBar();
00205     bar->setValue(100);
00206     bar->hide();
00207     getMainWindow()->statusBar()->showMessage(QString());
00208     isLoading = false;
00209 }
00210 
00211 void BrowserView::OnChange(Base::Subject<const char*> &rCaller,const char* rcReason)
00212 {
00213 }
00214 
00218 bool BrowserView::onMsg(const char* pMsg,const char** ppReturn)
00219 {
00220     if (strcmp(pMsg,"Back")==0){
00221         WebView->back();
00222         return true;
00223     } else if (strcmp(pMsg,"Next")==0){
00224         WebView->forward();
00225         return true;
00226     } else if (strcmp(pMsg,"Refresh")==0){
00227         WebView->reload();
00228         return true;
00229     } else if (strcmp(pMsg,"Stop")==0){
00230         stop();
00231         return true;
00232     } else if (strcmp(pMsg,"ZoomIn")==0){
00233         textSizeMultiplier += 0.2f;
00234         WebView->setTextSizeMultiplier(textSizeMultiplier);
00235         return true;
00236     } else if (strcmp(pMsg,"ZoomOut")==0){
00237         textSizeMultiplier -= 0.2f;
00238         WebView->setTextSizeMultiplier(textSizeMultiplier);
00239         return true;
00240     }
00241 
00242     return false;
00243 }
00244 
00249 bool BrowserView::onHasMsg(const char* pMsg) const
00250 {
00251     if (strcmp(pMsg,"Back")==0)  return true;
00252     if (strcmp(pMsg,"Next")==0)  return true;
00253     if (strcmp(pMsg,"Refresh")==0) return !isLoading;
00254     if (strcmp(pMsg,"Stop")==0) return isLoading;
00255     if (strcmp(pMsg,"ZoomIn")==0) return true;
00256     if (strcmp(pMsg,"ZoomOut")==0) return true;
00257 
00258     return false;
00259 }
00260 
00262 bool BrowserView::canClose(void)
00263 {
00264    return true;
00265 }
00266 
00267 #include "moc_BrowserView.cpp"
00268 

Generated on Wed Nov 23 18:59:59 2011 for FreeCAD by  doxygen 1.6.1