2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2011-12-15 18:34:48 +01:00
|
|
|
* Copyright (C) 2010-2011 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"
|
|
|
|
#include "webview.h"
|
2011-03-15 22:14:15 +01:00
|
|
|
#include "htmlhighlighter.h"
|
2011-03-19 00:28:37 +01:00
|
|
|
#include "sourceviewersearch.h"
|
2011-09-21 14:20:49 +02:00
|
|
|
#include "globalfunctions.h"
|
2011-09-30 21:44:18 +02:00
|
|
|
#include "iconprovider.h"
|
2011-12-14 22:30:05 +01:00
|
|
|
#include "enhancedmenu.h"
|
2011-03-15 22:14:15 +01:00
|
|
|
|
2011-12-12 21:14:43 +01:00
|
|
|
SourceViewer::SourceViewer(QWebPage* page, const QString &selectedHtml)
|
|
|
|
: QWidget(0)
|
2011-11-06 17:01:23 +01:00
|
|
|
, m_page(page)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-03-15 22:14:15 +01:00
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
2011-11-06 17:01:23 +01:00
|
|
|
setWindowTitle(tr("Source of ") + page->mainFrame()->url().toString());
|
2011-03-02 16:57:41 +01:00
|
|
|
m_layout = new QBoxLayout(QBoxLayout::TopToBottom, this);
|
|
|
|
m_sourceEdit = new QTextEdit(this);
|
2011-09-11 19:15:06 +02:00
|
|
|
m_sourceEdit->setObjectName("sourceviewer-textedit");
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-03-15 22:14:15 +01:00
|
|
|
m_statusBar = new QStatusBar(this);
|
|
|
|
m_statusBar->showMessage(page->mainFrame()->url().toString());
|
|
|
|
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
|
|
|
|
|
|
|
this->resize(650, 600);
|
|
|
|
m_sourceEdit->setReadOnly(true);
|
|
|
|
m_sourceEdit->moveCursor(QTextCursor::Start);
|
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());
|
|
|
|
|
|
|
|
QMenu* menuFile = new QMenu(tr("File"));
|
|
|
|
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"));
|
|
|
|
menuEdit->addAction(QIcon::fromTheme("edit-undo"), tr("Undo"), m_sourceEdit, SLOT(undo()))->setShortcut(QKeySequence("Ctrl+Z"));
|
|
|
|
menuEdit->addAction(QIcon::fromTheme("edit-redo"), tr("Redo"), m_sourceEdit, SLOT(redo()))->setShortcut(QKeySequence("Ctrl+Shift+Z"));
|
|
|
|
menuEdit->addSeparator();
|
|
|
|
menuEdit->addAction(QIcon::fromTheme("edit-cut"), tr("Cut"), m_sourceEdit, SLOT(cut()))->setShortcut(QKeySequence("Ctrl+X"));
|
|
|
|
menuEdit->addAction(QIcon::fromTheme("edit-copy"), tr("Copy"), m_sourceEdit, SLOT(copy()))->setShortcut(QKeySequence("Ctrl+C"));
|
|
|
|
menuEdit->addAction(QIcon::fromTheme("edit-paste"), tr("Paste"), m_sourceEdit, SLOT(paste()))->setShortcut(QKeySequence("Ctrl+V"));
|
|
|
|
menuEdit->addAction(QIcon::fromTheme("edit-delete"), tr("Delete"))->setShortcut(QKeySequence("Del"));
|
|
|
|
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);
|
|
|
|
|
|
|
|
QMenu* menuView = new QMenu(tr("View"));
|
2011-09-30 21:44:18 +02:00
|
|
|
menuView->addAction(IconProvider::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);
|
|
|
|
|
2011-12-04 16:54:57 +01:00
|
|
|
qz_centerWidgetToParent(this, page->view());
|
|
|
|
|
|
|
|
m_sourceEdit->setUndoRedoEnabled(false);
|
|
|
|
m_sourceEdit->insertPlainText(m_page->mainFrame()->toHtml());
|
|
|
|
m_sourceEdit->setUndoRedoEnabled(true);
|
2011-07-19 22:04:08 +02:00
|
|
|
|
|
|
|
//Highlight selectedHtml
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!selectedHtml.isEmpty()) {
|
2011-07-19 22:04:08 +02:00
|
|
|
m_sourceEdit->find(selectedHtml, QTextDocument::FindWholeWords);
|
2011-12-08 21:52:03 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-12-06 19:18:06 +01:00
|
|
|
QTextCursor cursor = m_sourceEdit->textCursor();
|
|
|
|
cursor.setPosition(0);
|
|
|
|
m_sourceEdit->setTextCursor(cursor);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
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()
|
|
|
|
{
|
|
|
|
m_sourceEdit->clear();
|
|
|
|
m_sourceEdit->insertPlainText(m_page->mainFrame()->toHtml());
|
|
|
|
|
|
|
|
m_statusBar->showMessage(tr("Source reloaded"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourceViewer::setTextEditable()
|
|
|
|
{
|
|
|
|
m_sourceEdit->setReadOnly(!m_sourceEdit->isReadOnly());
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
m_sourceEdit->setUpdatesEnabled(false);
|
|
|
|
m_sourceEdit->moveCursor(QTextCursor::Start);
|
2011-11-06 17:01:23 +01:00
|
|
|
for (int i = 0; i < line; i++) {
|
2011-03-15 22:14:15 +01:00
|
|
|
m_sourceEdit->moveCursor(QTextCursor::Down);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-15 22:14:15 +01:00
|
|
|
m_sourceEdit->setUpdatesEnabled(true);
|
|
|
|
}
|