Example #1
0
MainWindow::MainWindow(QWidget *parent)
    : QWebView(parent)
{
    //! The object we will expose to JavaScript engine:
    m_magicbox = new CMagicBox(this);

    // Signal is emitted before frame loads any web content:
    QObject::connect(page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
                     this, SLOT(addJSObject()));

    // QWebInspector
    QWebInspector *inspector = new QWebInspector;
    inspector->setPage(page());
    QWebSettings::globalSettings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
    // context menu
    this->setContextMenuPolicy(Qt::NoContextMenu); //No context menu is allowed if you don't need it

    // Try to handle cicks by self
    page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);//Handle link clicks by yourself
    QObject::connect(page(), SIGNAL( linkClicked( QUrl ) ),
                  this, SLOT( linkClickedSlot( QUrl ) ) );

    // qrc:// URLs refer to resources. See views.qrc
    QUrl startURL = QUrl("qrc:/index.html");
    // Load web content now!
    this->setUrl(startURL);
}
void tst_QWebInspector::attachAndDestroy()
{
    {   // External inspector + manual destruction of page first
        QWebPage* page = new QWebPage();
        page->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
        QWebInspector* inspector = new QWebInspector();
        inspector->setPage(page);
        page->updatePositionDependentActions(QPoint(0, 0));
        page->triggerAction(QWebPage::InspectElement);

        delete page;
        delete inspector;
    }
    {   // External inspector + manual destruction of inspector first
        QWebPage* page = new QWebPage();
        page->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
        QWebInspector* inspector = new QWebInspector();
        inspector->setPage(page);
        page->updatePositionDependentActions(QPoint(0, 0));
        page->triggerAction(QWebPage::InspectElement);

        delete inspector;
        delete page;
    }
    {   // Internal inspector
        QWebPage page;
        page.settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
        page.updatePositionDependentActions(QPoint(0, 0));
        page.triggerAction(QWebPage::InspectElement);
    }
}
void wrapInFunction()
{

//! [0]
    // ...
    QWebPage *page = new QWebPage;
    // ...

    QWebInspector *inspector = new QWebInspector;
    inspector->setPage(page);

    connect(page, SIGNAL(webInspectorTriggered(QWebElement)), inspector, SLOT(show()));
//! [0]

}
Example #4
0
void UtmpWebView::SetFrameWindow(int flag)
{
    switch(flag)
    {
        case 0:
            this->close();
            break;
        case 1:
            this->showMinimized();
            break;
        case 2:
        {
            //全局也就只能有一个
            QDialog* d = new QDialog(this,(Qt::WindowMinimizeButtonHint|Qt::WindowMaximizeButtonHint|Qt::WindowCloseButtonHint));
            d->setAttribute(Qt::WA_DeleteOnClose, true);
            QWebInspector* wi = new QWebInspector(d);
            wi->setPage(this->page());
            d->setLayout(new QVBoxLayout());
            d->layout()->setMargin(0);
            d->layout()->addWidget(wi);
            d->show();
            d->resize(600,350);
            break;
        }
        case 3:
        {
            QMessageBox::StandardButton rb = QMessageBox::information(NULL, "from QT", "这是网页让QT弹出的对话框", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
            if(rb == QMessageBox::Yes)
            {
                //这样直接调用,执行速度异常慢
                //this->page()->mainFrame()->evaluateJavaScript("Ext.Msg.alert('from ext','这是QT让网页弹出的对话框');");
                this->page()->mainFrame()->evaluateJavaScript("testFun();");
            }
            break;
        }
        case 4:
        {
            this->reload();
        }
    }
}
Example #5
0
int QtMainWindow::tabbarAddTab(const QString& label, const char* icon, bool disabled, const QColor* web_bkg_color, QTabBarRuntimeParams& tbrp)
{
    QWebView* wv = main_webView;
    QWebInspector* wI = main_webInspector;
    if (!tbrp["use_current_view_for_tab"].toBool()) {
        // creating web view
        wv = new QWebView();
        wv->setMaximumSize(0,0);
        wv->setParent(ui->centralWidget);
        wv->setAttribute(Qt::WA_AcceptTouchEvents, false);
        ui->verticalLayout->addWidget(wv);
		wv->setPage(new QtWebPage());
        setUpWebPage(wv->page());
        if (web_bkg_color && (web_bkg_color->name().length()>0))
            wv->setHtml( QString("<!DOCTYPE html><html><body style=\"background:") + web_bkg_color->name() + QString("\"></body></html>") );
        // creating and attaching web inspector
        wI = new QWebInspector();
        wI->setWindowTitle("Web Inspector");
        wI->setPage(wv->page());
    }

    tabViews.push_back(wv);
    tabInspect.push_back(wI);
    webInspectorWindow->addWebInspector(wI);

    cur_tbrp = &tbrp;
    if (icon && (strlen(icon) > 0))
        ui->tabBar->addTab(QIcon(QString(icon)), label);
    else
        ui->tabBar->addTab(label);
    ui->tabBar->setTabToolTip(ui->tabBar->count()-1, label);
    if (disabled)
        ui->tabBar->setTabEnabled(ui->tabBar->count()-1, false);
    cur_tbrp = 0;
	ui->tabBar->setTabData(ui->tabBar->count()-1, tbrp);

    return ui->tabBar->count() - 1;
}
    WebView::WebView(QWidget * parent)
        : QWebView(parent), _page(new WebPage(this)), useInspector(false)
    {
        setPage(_page);
        connect(_page, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
#if defined(Q_OS_MACX)
        //settings()->setFontFamily(QWebSettings::SansSerifFont, "Arial");
#endif

#ifdef Q_OS_WIN32
        char env[1024] = { 0 };
        int status = GetEnvironmentVariable("UTOPIA_WEBKIT_INSPECTOR", env, sizeof(env));
        if (status != 0) { env[0] = 0; }
#else
        char * env = ::getenv("UTOPIA_WEBKIT_INSPECTOR");
#endif
        useInspector = (env && strcmp(env, "0") != 0);

        if (useInspector) {
            page()->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
            QWebInspector * inspector = new QWebInspector(0);
            inspector->setPage(page());
        }
    }
Example #7
0
void MainWindow::initializeApp()
{
    // inform us when a link in the table of contents or preview view is clicked
    ui->webView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
    ui->tocWebView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);

    // show custom context menu for HTML preview
    // most actions don't work and can even lead to crashes (like reload)
    ui->webView->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(ui->webView, SIGNAL(customContextMenuRequested(QPoint)),
            this, SLOT(webViewContextMenu(QPoint)));

    // set default style
    styleDefault();

    ui->plainTextEdit->tabWidthChanged(options->tabWidth());

    // init extension flags
    ui->actionAutolink->setChecked(options->isAutolinkEnabled());
    ui->actionStrikethroughOption->setChecked(options->isStrikethroughEnabled());
    ui->actionAlphabeticLists->setChecked(options->isAlphabeticListsEnabled());
    ui->actionDefinitionLists->setChecked(options->isDefinitionListsEnabled());
    ui->actionSmartyPants->setChecked(options->isSmartyPantsEnabled());
    ui->actionFootnotes->setChecked(options->isFootnotesEnabled());
    ui->actionSuperscript->setChecked(options->isSuperscriptEnabled());

    // init option flags
    ui->actionMathSupport->setChecked(options->isMathSupportEnabled());
    ui->actionCodeHighlighting->setChecked(options->isCodeHighlightingEnabled());
    ui->actionShowSpecialCharacters->setChecked(options->isShowSpecialCharactersEnabled());
    ui->actionWordWrap->setChecked(options->isWordWrapEnabled());
    ui->actionCheckSpelling->setChecked(options->isSpellingCheckEnabled());
    ui->plainTextEdit->setSpellingCheckEnabled(options->isSpellingCheckEnabled());

    // set url to markdown syntax help
    ui->webView_2->setUrl(tr("qrc:/syntax.html"));

    // allow loading of remote javascript
    QWebSettings::globalSettings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, true);

    // setup disk cache for network access
    QString cacheDir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
    diskCache->setCacheDirectory(cacheDir);

    ui->webView->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
    QWebInspector *inspector = new QWebInspector();
    inspector->setPage(ui->webView->page());

    loadCustomStyles();
    ui->menuLanguages->loadDictionaries(options->dictionaryLanguage());

    //: path to built-in snippets resource.
    JsonSnippetFile::load(tr(":/markdown-snippets.json"), snippetCollection);
    QString path = DataLocation::writableLocation();
    JsonSnippetFile::load(path + "/user-snippets.json", snippetCollection);

    // setup file explorer
    connect(ui->fileExplorerDockContents, SIGNAL(fileSelected(QString)),
            this, SLOT(openRecentFile(QString)));

    // setup jump list on windows
#ifdef Q_OS_WIN
    QWinJumpList jumplist;
    jumplist.recent()->setVisible(true);
#endif

    // load file passed to application on start
    if (!fileName.isEmpty()) {
        load(fileName);
    }
}
Example #8
0
void MainWindow::init(bool is_development) {
    //qApp->installEventFilter(this);
  
    setWindowTitle(settings->value("window_title").toString());

    if (settings->value("minified_state").toString() == "true") {
        setWindowState(Qt::WindowMinimized);
    }

    this->setMinimumWidth(430);
    this->setMinimumHeight(430);

    webView = new QWebView(this);
    if (settings->value("context_menu").toString() == "false") {
        webView->setContextMenuPolicy(Qt::NoContextMenu);
    }
    this->setCentralWidget(webView);

    m_network_manager = new QNetworkAccessManager(this);

    QWebPage *webPage = webView->page();
    webPage->setNetworkAccessManager(m_network_manager);
    webPage->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
    connect(webPage, &QWebPage::linkClicked, this, &MainWindow::onLinkClicked);
    connect(webPage->mainFrame(), &QWebFrame::javaScriptWindowObjectCleared, this, &MainWindow::attachJsApi);

    webView->show();

    bootstrap(is_development);

    // restore saved window geometry from .ini file
    QVariant size = settings->value("main_window_geometry");
    if ( size.isNull() ) {
        this->setGeometry(QRect(500, 100, 700, 500));
    } else {
        this->setGeometry(size.toRect());
    }

    // restore saved zoom factor from .ini file
    qreal z = settings->value("zoom_factor").toReal();
    if ( z ) webView->page()->mainFrame()->setZoomFactor(z);

    m_jsApi = new JsApi(this);

    optionsDialog = new OptionsDialog(this);
    optionsDialog->m_version = m_version;
    connect(optionsDialog, SIGNAL(accepting()), m_jsApi, SLOT(onOptionsDialogAccepted()));

    QShortcut *zoomin = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Plus), this);
    connect(zoomin, &QShortcut::activated, this, &MainWindow::onZoomIn);

    QShortcut *zoomout = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Minus), this);
    connect(zoomout, &QShortcut::activated, this, &MainWindow::onZoomOut);

    QShortcut *shutdown = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this);
    connect(shutdown, &QShortcut::activated, m_jsApi, &JsApi::shutdown);

    QWebSettings::setObjectCacheCapacities(0, 0, 0);

    if (settings->value("webinspector").toString() == "true") {
        QWebSettings::globalSettings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);

        QWebInspector *inspector;
        inspector = new QWebInspector();
        inspector->setPage(webView->page());
        inspector->setGeometry(QRect(500, 10, 1000, 700));
        inspector->show();
    }

    QIcon icon = QIcon(application_path + "/" + APPNAME + ".png");
    m_trayIcon = new QSystemTrayIcon(this);
    m_trayIcon->setIcon(icon);
    m_trayIcon->show();
    connect(m_trayIcon, &QSystemTrayIcon::messageClicked, m_jsApi, &JsApi::onTrayMessageClicked);
    connect(m_trayIcon, &QSystemTrayIcon::activated, m_jsApi, &JsApi::onTrayIconActivated);
}