2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2013-02-05 15:17:36 +01:00
|
|
|
* Copyright (C) 2010-2013 David Rosca <nowrep@gmail.com>
|
2011-03-03 18:29:20 +01:00
|
|
|
*
|
|
|
|
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2011-03-02 16:57:41 +01:00
|
|
|
#include "sourceviewer.h"
|
2012-01-21 20:27:45 +01:00
|
|
|
#include "tabbedwebview.h"
|
2011-03-15 22:14:15 +01:00
|
|
|
#include "htmlhighlighter.h"
|
2011-03-19 00:28:37 +01:00
|
|
|
#include "sourceviewersearch.h"
|
2013-01-22 19:04:22 +01:00
|
|
|
#include "qztools.h"
|
2011-09-30 21:44:18 +02:00
|
|
|
#include "iconprovider.h"
|
2011-12-14 22:30:05 +01:00
|
|
|
#include "enhancedmenu.h"
|
2012-03-15 19:35:37 +01:00
|
|
|
#include "plaineditwithlines.h"
|
2011-03-15 22:14:15 +01:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QBoxLayout>
|
|
|
|
#include <QMenuBar>
|
|
|
|
#include <QStatusBar>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QInputDialog>
|
|
|
|
#include <QWebFrame>
|
2012-03-16 15:55:34 +01:00
|
|
|
#include <QTimer>
|
2012-03-16 13:53:49 +01:00
|
|
|
|
2012-01-02 13:56:52 +01:00
|
|
|
SourceViewer::SourceViewer(QWebFrame* frame, const QString &selectedHtml)
|
2011-12-12 21:14:43 +01:00
|
|
|
: QWidget(0)
|
2012-01-02 13:56:52 +01:00
|
|
|
, m_frame(frame)
|
2012-03-16 15:55:34 +01:00
|
|
|
, m_selectedHtml(selectedHtml)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-03-15 22:14:15 +01:00
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
2012-01-02 13:56:52 +01:00
|
|
|
setWindowTitle(tr("Source of ") + frame->url().toString());
|
2011-03-02 16:57:41 +01:00
|
|
|
m_layout = new QBoxLayout(QBoxLayout::TopToBottom, this);
|
2012-03-15 19:35:37 +01:00
|
|
|
m_sourceEdit = new PlainEditWithLines(this);
|
2011-09-11 19:15:06 +02:00
|
|
|
m_sourceEdit->setObjectName("sourceviewer-textedit");
|
2012-03-16 13:53:49 +01:00
|
|
|
m_sourceEdit->setReadOnly(true);
|
|
|
|
m_sourceEdit->setUndoRedoEnabled(false);
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-03-15 22:14:15 +01:00
|
|
|
m_statusBar = new QStatusBar(this);
|
2012-01-02 13:56:52 +01:00
|
|
|
m_statusBar->showMessage(frame->url().toString());
|
2012-03-16 23:28:23 +01:00
|
|
|
|
2011-03-15 22:14:15 +01:00
|
|
|
QMenuBar* menuBar = new QMenuBar(this);
|
2011-03-02 16:57:41 +01:00
|
|
|
m_layout->addWidget(m_sourceEdit);
|
2011-03-15 22:14:15 +01:00
|
|
|
m_layout->addWidget(m_statusBar);
|
|
|
|
m_layout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
m_layout->setSpacing(0);
|
|
|
|
m_layout->setMenuBar(menuBar);
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-03-15 22:14:15 +01:00
|
|
|
QFont font;
|
|
|
|
font.setFamily("Tahoma");
|
|
|
|
font.setFixedPitch(true);
|
|
|
|
font.setPointSize(10);
|
|
|
|
|
|
|
|
m_sourceEdit->setFont(font);
|
|
|
|
new HtmlHighlighter(m_sourceEdit->document());
|
|
|
|
|
2012-03-16 23:28:23 +01:00
|
|
|
resize(650, 600);
|
2013-01-22 19:04:22 +01:00
|
|
|
QzTools::centerWidgetToParent(this, frame->page()->view());
|
2012-03-16 23:28:23 +01:00
|
|
|
|
2011-03-15 22:14:15 +01:00
|
|
|
QMenu* menuFile = new QMenu(tr("File"));
|
2013-02-05 15:17:36 +01:00
|
|
|
menuFile->addAction(tr("Load in page"), this, SLOT(loadInPage()));
|
2011-03-15 22:14:15 +01:00
|
|
|
menuFile->addAction(QIcon::fromTheme("document-save"), tr("Save as..."), this, SLOT(save()))->setShortcut(QKeySequence("Ctrl+S"));
|
|
|
|
menuFile->addSeparator();
|
|
|
|
menuFile->addAction(QIcon::fromTheme("window-close"), tr("Close"), this, SLOT(close()))->setShortcut(QKeySequence("Ctrl+W"));
|
|
|
|
menuBar->addMenu(menuFile);
|
|
|
|
|
|
|
|
QMenu* menuEdit = new QMenu(tr("Edit"));
|
2012-03-16 15:55:34 +01:00
|
|
|
m_actionUndo = menuEdit->addAction(QIcon::fromTheme("edit-undo"), tr("Undo"), m_sourceEdit, SLOT(undo()));
|
|
|
|
m_actionRedo = menuEdit->addAction(QIcon::fromTheme("edit-redo"), tr("Redo"), m_sourceEdit, SLOT(redo()));
|
2011-03-15 22:14:15 +01:00
|
|
|
menuEdit->addSeparator();
|
2012-03-16 15:55:34 +01:00
|
|
|
m_actionCut = menuEdit->addAction(QIcon::fromTheme("edit-cut"), tr("Cut"), m_sourceEdit, SLOT(cut()));
|
|
|
|
m_actionCopy = menuEdit->addAction(QIcon::fromTheme("edit-copy"), tr("Copy"), m_sourceEdit, SLOT(copy()));
|
|
|
|
m_actionPaste = menuEdit->addAction(QIcon::fromTheme("edit-paste"), tr("Paste"), m_sourceEdit, SLOT(paste()));
|
2011-03-15 22:14:15 +01:00
|
|
|
menuEdit->addSeparator();
|
|
|
|
menuEdit->addAction(QIcon::fromTheme("edit-select-all"), tr("Select All"), m_sourceEdit, SLOT(selectAll()))->setShortcut(QKeySequence("Ctrl+A"));
|
|
|
|
menuEdit->addAction(QIcon::fromTheme("edit-find"), tr("Find"), this, SLOT(findText()))->setShortcut(QKeySequence("Ctrl+F"));
|
|
|
|
menuEdit->addSeparator();
|
|
|
|
menuEdit->addAction(QIcon::fromTheme("go-jump"), tr("Go to Line..."), this, SLOT(goToLine()))->setShortcut(QKeySequence("Ctrl+L"));
|
|
|
|
menuBar->addMenu(menuEdit);
|
|
|
|
|
2012-03-16 15:55:34 +01:00
|
|
|
m_actionUndo->setShortcut(QKeySequence("Ctrl+Z"));
|
|
|
|
m_actionRedo->setShortcut(QKeySequence("Ctrl+Shift+Z"));
|
|
|
|
m_actionCut->setShortcut(QKeySequence("Ctrl+X"));
|
|
|
|
m_actionCopy->setShortcut(QKeySequence("Ctrl+C"));
|
|
|
|
m_actionPaste->setShortcut(QKeySequence("Ctrl+V"));
|
|
|
|
|
2011-03-15 22:14:15 +01:00
|
|
|
QMenu* menuView = new QMenu(tr("View"));
|
2012-04-22 20:51:28 +02:00
|
|
|
menuView->addAction(qIconProvider->standardIcon(QStyle::SP_BrowserReload), tr("Reload"), this, SLOT(reload()))->setShortcut(QKeySequence("F5"));
|
2011-03-15 22:14:15 +01:00
|
|
|
menuView->addSeparator();
|
|
|
|
menuView->addAction(tr("Editable"), this, SLOT(setTextEditable()))->setCheckable(true);
|
|
|
|
menuView->addAction(tr("Word Wrap"), this, SLOT(setTextWordWrap()))->setCheckable(true);
|
|
|
|
menuView->actions().at(3)->setChecked(true);
|
|
|
|
menuBar->addMenu(menuView);
|
|
|
|
|
2012-03-16 15:55:34 +01:00
|
|
|
connect(m_sourceEdit, SIGNAL(copyAvailable(bool)), this, SLOT(copyAvailable(bool)));
|
|
|
|
connect(m_sourceEdit, SIGNAL(redoAvailable(bool)), this, SLOT(redoAvailable(bool)));
|
|
|
|
connect(m_sourceEdit, SIGNAL(undoAvailable(bool)), this, SLOT(undoAvailable(bool)));
|
|
|
|
connect(menuEdit, SIGNAL(aboutToShow()), this, SLOT(pasteAvailable()));
|
|
|
|
|
|
|
|
QTimer::singleShot(0, this, SLOT(loadSource()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourceViewer::copyAvailable(bool yes)
|
|
|
|
{
|
|
|
|
m_actionCopy->setEnabled(yes);
|
|
|
|
m_actionCut->setEnabled(yes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourceViewer::redoAvailable(bool available)
|
|
|
|
{
|
|
|
|
m_actionRedo->setEnabled(available);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourceViewer::undoAvailable(bool available)
|
|
|
|
{
|
|
|
|
m_actionUndo->setEnabled(available);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourceViewer::pasteAvailable()
|
|
|
|
{
|
|
|
|
m_actionPaste->setEnabled(m_sourceEdit->canPaste());
|
|
|
|
}
|
|
|
|
|
2013-02-05 15:17:36 +01:00
|
|
|
void SourceViewer::loadInPage()
|
|
|
|
{
|
|
|
|
if (m_frame) {
|
|
|
|
m_frame.data()->setHtml(m_sourceEdit->toPlainText(), m_frame.data()->baseUrl());
|
|
|
|
m_statusBar->showMessage(tr("Source loaded in page"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_statusBar->showMessage(tr("Cannot load in page. Page has been closed."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-16 15:55:34 +01:00
|
|
|
void SourceViewer::loadSource()
|
|
|
|
{
|
|
|
|
m_actionUndo->setEnabled(false);
|
|
|
|
m_actionRedo->setEnabled(false);
|
|
|
|
m_actionCut->setEnabled(false);
|
|
|
|
m_actionCopy->setEnabled(false);
|
|
|
|
m_actionPaste->setEnabled(false);
|
|
|
|
|
2012-06-28 12:03:08 +02:00
|
|
|
QString html = m_frame.data()->toHtml();
|
|
|
|
// Remove AdBlock element hiding rules
|
2013-05-09 19:49:13 +02:00
|
|
|
html.remove(QzRegExp("<style type=\"text/css\">\n/\\* AdBlock for QupZilla \\*/\n.*\\{display:none !important;\\}[\n]*</style>"));
|
2012-06-28 12:03:08 +02:00
|
|
|
m_sourceEdit->setPlainText(html);
|
2011-07-19 22:04:08 +02:00
|
|
|
|
2013-02-05 15:17:36 +01:00
|
|
|
// Highlight selectedHtml
|
2012-03-16 15:55:34 +01:00
|
|
|
if (!m_selectedHtml.isEmpty()) {
|
|
|
|
m_sourceEdit->find(m_selectedHtml, QTextDocument::FindWholeWords);
|
2011-12-08 21:52:03 +01:00
|
|
|
}
|
2012-03-16 23:28:23 +01:00
|
|
|
|
|
|
|
m_sourceEdit->setShowingCursor(true);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
2011-03-15 22:14:15 +01:00
|
|
|
|
|
|
|
void SourceViewer::save()
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
QString filePath = QFileDialog::getSaveFileName(this, tr("Save file..."), QDir::homePath() + "/source_code.html");
|
|
|
|
if (filePath.isEmpty()) {
|
2011-03-15 22:14:15 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-15 22:14:15 +01:00
|
|
|
|
|
|
|
QFile file(filePath);
|
|
|
|
if (!file.open(QFile::WriteOnly)) {
|
|
|
|
QMessageBox::critical(this, tr("Error!"), tr("Cannot write to file!"));
|
2011-03-17 17:03:04 +01:00
|
|
|
m_statusBar->showMessage(tr("Error writing to file"));
|
2011-03-15 22:14:15 +01:00
|
|
|
return;
|
|
|
|
}
|
2011-12-04 16:54:57 +01:00
|
|
|
file.write(m_sourceEdit->toPlainText().toUtf8());
|
2011-03-15 22:14:15 +01:00
|
|
|
file.close();
|
|
|
|
|
2011-11-01 12:30:48 +01:00
|
|
|
m_statusBar->showMessage(tr("Source successfully saved"));
|
2011-03-15 22:14:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SourceViewer::findText()
|
|
|
|
{
|
2011-03-19 00:28:37 +01:00
|
|
|
if (m_layout->count() > 2) {
|
2011-11-06 17:01:23 +01:00
|
|
|
SourceViewerSearch* search = qobject_cast<SourceViewerSearch*>(m_layout->itemAt(1)->widget());
|
2011-03-19 00:28:37 +01:00
|
|
|
search->activateLineEdit();
|
|
|
|
return;
|
|
|
|
}
|
2011-03-15 22:14:15 +01:00
|
|
|
|
2011-10-09 14:51:25 +02:00
|
|
|
SourceViewerSearch* s = new SourceViewerSearch(this);
|
|
|
|
m_layout->insertWidget(1, s);
|
|
|
|
s->activateLineEdit();
|
2011-03-15 22:14:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SourceViewer::reload()
|
|
|
|
{
|
2012-01-21 20:27:45 +01:00
|
|
|
if (m_frame) {
|
|
|
|
m_sourceEdit->clear();
|
2013-02-05 15:17:36 +01:00
|
|
|
loadSource();
|
2011-03-15 22:14:15 +01:00
|
|
|
|
2012-01-21 20:27:45 +01:00
|
|
|
m_statusBar->showMessage(tr("Source reloaded"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_statusBar->showMessage(tr("Cannot reload source. Page has been closed."));
|
|
|
|
}
|
2011-03-15 22:14:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SourceViewer::setTextEditable()
|
|
|
|
{
|
|
|
|
m_sourceEdit->setReadOnly(!m_sourceEdit->isReadOnly());
|
2012-03-16 13:53:49 +01:00
|
|
|
m_sourceEdit->setUndoRedoEnabled(true);
|
2011-03-15 22:14:15 +01:00
|
|
|
|
|
|
|
m_statusBar->showMessage(tr("Editable changed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourceViewer::setTextWordWrap()
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
m_sourceEdit->setWordWrapMode((m_sourceEdit->wordWrapMode() == QTextOption::NoWrap) ? QTextOption::WordWrap : QTextOption::NoWrap);
|
2011-03-15 22:14:15 +01:00
|
|
|
|
|
|
|
m_statusBar->showMessage(tr("Word Wrap changed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourceViewer::goToLine()
|
|
|
|
{
|
2011-03-19 00:28:37 +01:00
|
|
|
int line = QInputDialog::getInt(this, tr("Go to Line..."), tr("Enter line number"), 0, 1, 5000);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (line == 0) {
|
2011-03-15 22:14:15 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-15 22:14:15 +01:00
|
|
|
|
2012-03-15 19:35:37 +01:00
|
|
|
m_sourceEdit->goToLine(line);
|
2011-03-15 22:14:15 +01:00
|
|
|
}
|