WpsEditor: add linenumbrs WpsEditor: Add linenumbers to the WPS Code, and highlight the error line, if parsing fails. (FS#9362)

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@18483 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Dominik Wenger 2008-09-10 19:41:18 +00:00
parent 1ff9ce202d
commit f414c6e19f
6 changed files with 339 additions and 100 deletions

View file

@ -1,7 +1,7 @@
TEMPLATE = app
TARGET =
DEPENDPATH += . build src ui
INCLUDEPATH += . src/QPropertyEditor ../libwps/src
INCLUDEPATH += . src src/QPropertyEditor ../libwps/src
DESTDIR = bin
OBJECTS_DIR = build
MOC_DIR = build
@ -17,7 +17,9 @@ HEADERS += ../libwps/src/api.h \
src/qwpseditorwindow.h \
src/utils.h \
src/qwpsdrawer.h \
src/qsyntaxer.h
src/qsyntaxer.h \
src/numberedtextview.h
FORMS += ui/mainwindow.ui ui/slider.ui
SOURCES += src/main.cpp \
src/slider.cpp \
@ -27,8 +29,10 @@ SOURCES += src/main.cpp \
src/utils.cpp \
src/qwpsdrawer.cpp \
src/qwpsdrawer_static.cpp \
src/qsyntaxer.cpp
LIBS += -Lbin
src/qsyntaxer.cpp \
src/numberedtextview.cpp
LIBS += -Lbin
CONFIG(debug, debug|release) {
LIBS += -lQPropertyEditord
TARGET = wpseditord

View file

@ -0,0 +1,181 @@
/* This file is part of the KDE libraries
Copyright (C) 2005, 2006 KJSEmbed Authors
See included AUTHORS file.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
--------------------------------------------------------------------------------------
Imported into the WPS editor and simplified by Dominik Wenger
*/
#include <QTextDocument>
#include <QTextBlock>
#include <QTextEdit>
#include <QHBoxLayout>
#include <QScrollBar>
#include <QPainter>
#include <QAbstractTextDocumentLayout>
#include <QDebug>
#include "numberedtextview.h"
NumberBar::NumberBar( QWidget *parent )
: QWidget( parent ), edit(0), markedLine(-1)
{
// Make room for 4 digits and the breakpoint icon
setFixedWidth( fontMetrics().width( QString("0000") + 10 + 32 ) );
markerIcon = QPixmap( "images/marker.png" );
}
NumberBar::~NumberBar()
{
}
void NumberBar::markLine( int lineno )
{
markedLine = lineno;
}
void NumberBar::setTextEdit( QTextEdit *edit )
{
this->edit = edit;
connect( edit->document()->documentLayout(), SIGNAL( update(const QRectF &) ),
this, SLOT( update() ) );
connect( edit->verticalScrollBar(), SIGNAL(valueChanged(int) ),
this, SLOT( update() ) );
}
void NumberBar::paintEvent( QPaintEvent * )
{
QAbstractTextDocumentLayout *layout = edit->document()->documentLayout();
int contentsY = edit->verticalScrollBar()->value();
qreal pageBottom = contentsY + edit->viewport()->height();
const QFontMetrics fm = fontMetrics();
const int ascent = fontMetrics().ascent() + 1; // height = ascent + descent + 1
int lineCount = 1;
QPainter p(this);
markedRect = QRect();
for ( QTextBlock block = edit->document()->begin();
block.isValid(); block = block.next(), ++lineCount )
{
const QRectF boundingRect = layout->blockBoundingRect( block );
QPointF position = boundingRect.topLeft();
if ( position.y() + boundingRect.height() < contentsY )
continue;
if ( position.y() > pageBottom )
break;
const QString txt = QString::number( lineCount );
p.drawText( width() - fm.width(txt), qRound( position.y() ) - contentsY + ascent, txt );
// marker
if ( markedLine == lineCount )
{
p.drawPixmap( 1, qRound( position.y() ) - contentsY, markerIcon );
markedRect = QRect( 1, qRound( position.y() ) - contentsY, markerIcon.width(), markerIcon.height() );
}
}
}
NumberedTextView::NumberedTextView( QWidget *parent )
: QFrame( parent )
{
setFrameStyle( QFrame::StyledPanel | QFrame::Sunken );
setLineWidth( 2 );
// Setup the main view
view = new QTextEdit( this );
//view->setFontFamily( "Courier" );
view->setLineWrapMode( QTextEdit::NoWrap );
view->setFrameStyle( QFrame::NoFrame );
connect( view->document(), SIGNAL(contentsChange(int,int,int)), this, SLOT(textChanged(int,int,int)) );
// Setup the line number pane
numbers = new NumberBar( this );
numbers->setTextEdit( view );
// Test
markLine(2);
//setup layout
box = new QHBoxLayout( this );
box->setSpacing( 0 );
box->setMargin( 0 );
box->addWidget( numbers );
box->addWidget( view );
}
NumberedTextView::~NumberedTextView()
{
}
void NumberedTextView::markLine( int lineno )
{
markedLine = lineno;
numbers->markLine( lineno );
textChanged(1,1,1);
}
void NumberedTextView::scrolltoLine( int lineno )
{
int max = view->verticalScrollBar()->maximum();
int min = view->verticalScrollBar()->minimum();
int lines = view->document()->blockCount();
view->verticalScrollBar()->setValue( (max*lineno)/lines+min );
}
void NumberedTextView::textChanged( int pos, int removed, int added )
{
Q_UNUSED( pos );
if ( removed == 0 && added == 0 )
return;
QTextBlock block = highlight.block();
QTextBlockFormat fmt = block.blockFormat();
QColor bg = view->palette().base().color();
fmt.setBackground( bg );
highlight.setBlockFormat( fmt );
int lineCount = 1;
for ( QTextBlock block = view->document()->begin();
block.isValid(); block = block.next(), ++lineCount )
{
if ( lineCount == markedLine )
{
fmt = block.blockFormat();
QColor bg = Qt::red;
fmt.setBackground( bg.light(150) );
highlight = QTextCursor( block );
highlight.movePosition( QTextCursor::EndOfBlock, QTextCursor::KeepAnchor );
highlight.setBlockFormat( fmt );
break;
}
}
}

View file

@ -0,0 +1,87 @@
/* This file is part of the KDE libraries
Copyright (C) 2005, 2006 KJSEmbed Authors
See included AUTHORS file.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
--------------------------------------------------------------------------------------
Imported into the WPS editor and simplified by Dominik Wenger
*/
#ifndef NUMBERED_TEXT_VIEW_H
#define NUMBERED_TEXT_VIEW_H
#include <QFrame>
#include <QPixmap>
#include <QTextCursor>
class QTextEdit;
class QHBoxLayout;
// Shows the Line numbers
class NumberBar : public QWidget
{
Q_OBJECT
public:
NumberBar( QWidget *parent );
~NumberBar();
void markLine( int lineno );
void setTextEdit( QTextEdit *edit );
void paintEvent( QPaintEvent *ev );
private:
QTextEdit *edit;
QPixmap markerIcon;
int markedLine;
QRect markedRect;
};
// Shows a QTextEdit with Line numbers
class NumberedTextView : public QFrame
{
Q_OBJECT
public:
NumberedTextView( QWidget *parent = 0 );
~NumberedTextView();
/** Returns the QTextEdit of the main view. */
QTextEdit *textEdit() const { return view; }
/* marks the line with a icon */
void markLine( int lineno );
void scrolltoLine( int lineno );
private slots:
void textChanged( int pos, int removed, int added );
private:
QTextEdit *view;
NumberBar *numbers;
QHBoxLayout *box;
QTextCursor highlight;
int markedLine;
};
#endif // NUMBERED_TEXT_VIEW_H

View file

@ -47,6 +47,7 @@ const char *playmodeNames[] = {
QWpsEditorWindow::QWpsEditorWindow( QWidget * parent, Qt::WFlags f)
: QMainWindow(parent, f) {
logEdit = 0;
scrollingLine = -1;
setupUi(this);
drawer = new QWpsDrawer(&wpsState,&trackState, this);
QWpsDrawer::api.verbose = 1;
@ -54,7 +55,8 @@ QWpsEditorWindow::QWpsEditorWindow( QWidget * parent, Qt::WFlags f)
connectActions();
m_propertyEditor->addObject(&trackState);
m_propertyEditor->addObject(&wpsState);
new QSyntaxer(plainWpsEdit->document());
new QSyntaxer(plainWpsEdit->textEdit()->document());
plainWpsEdit->markLine(-1);
}
void QWpsEditorWindow::connectActions() {
@ -65,7 +67,7 @@ void QWpsEditorWindow::connectActions() {
connect(actShowGrid, SIGNAL(triggered(bool)), drawer, SLOT(slotShowGrid(bool)));
connect(actUpdatePlainWps, SIGNAL(triggered()), SLOT(slotUpdatePlainWps()));
connect(plainWpsEdit->document(),SIGNAL(modificationChanged(bool)),SLOT(slotPlainDocModChanged(bool)));
connect(plainWpsEdit->textEdit()->document(),SIGNAL(modificationChanged(bool)),SLOT(slotPlainDocModChanged(bool)));
connect(&wpsState, SIGNAL(stateChanged(wpsstate)), drawer, SLOT(slotWpsStateChanged(wpsstate)));
connect(&trackState, SIGNAL(stateChanged(trackstate)), drawer, SLOT(slotTrackStateChanged(trackstate)));
@ -124,17 +126,23 @@ void QWpsEditorWindow::slotOpenWps() {
DEBUGF1(tr("File wasn't chosen"));
return;
}
m_propertyEditor->setEnabled(true);
scrollingLine = -1;
drawer->WpsInit(wpsfile);
plainWpsEdit->clear();
plainWpsEdit->append(drawer->wpsString());
trackState.setAlbum(trackState.album()); ////updating property editor
actGroupAudios->setEnabled(true);
plainWpsEdit->textEdit()->clear();
plainWpsEdit->textEdit()->append(drawer->wpsString());
postWpsUpdate();
}
void QWpsEditorWindow::logMsg(QString s) {
logEdit->append(s);
// check for error line:
if (s.contains("ERR: Failed parsing on line ")) {
QRegExp error("\\d+");
if (error.indexIn(s) != -1) {
scrollingLine = error.cap(0).toInt();
plainWpsEdit->markLine(scrollingLine);
}
}
}
void QWpsEditorWindow::slotVerboseLevel() {
@ -146,18 +154,18 @@ void QWpsEditorWindow::slotVerboseLevel() {
void QWpsEditorWindow::slotUpdatePlainWps() {
DEBUGF1(tr("Updating WPS"));
plainWpsEdit->document()->setModified(false);
drawer->WpsInit(plainWpsEdit->toPlainText(),false);
m_propertyEditor->setEnabled(true);
actGroupAudios->setEnabled(true);
trackState.setAlbum(trackState.album()); //updating property editor
scrollingLine = -1;
drawer->WpsInit(plainWpsEdit->textEdit()->toPlainText(),false);
postWpsUpdate();
}
void QWpsEditorWindow::slotPlainDocModChanged(bool changed) {
if (changed)
if (changed) {
dockPlainWps->setWindowTitle(tr("PlainWps*"));
else
plainWpsEdit->markLine(-1);
} else {
dockPlainWps->setWindowTitle(tr("PlainWps"));
}
}
void QWpsEditorWindow::slotSetTarget(const QString & target) {
if (drawer->setTarget(target)) {
@ -169,5 +177,15 @@ void QWpsEditorWindow::slotSetTarget(const QString & target) {
slotUpdatePlainWps();
}
void QWpsEditorWindow::postWpsUpdate() {
m_propertyEditor->setEnabled(true);
actGroupAudios->setEnabled(true);
trackState.setAlbum(trackState.album()); ////updating property editor
plainWpsEdit->markLine(scrollingLine);
plainWpsEdit->textEdit()->document()->setModified(false);
plainWpsEdit->scrolltoLine(scrollingLine);
scrollingLine = -1;
}

View file

@ -35,7 +35,7 @@
class QWpsEditorWindow : public QMainWindow, public Ui::MainWindow {
Q_OBJECT
QWpsState wpsState;
QTrackState trackState;
QPointer<QWpsDrawer> drawer;
@ -47,9 +47,11 @@ class QWpsEditorWindow : public QMainWindow, public Ui::MainWindow {
QHash<QString,QAction *> actTargets;
QActionGroup *actGroupTargets;
QSignalMapper *targetsSignalMapper;
int scrollingLine;
protected:
void connectActions();
void postWpsUpdate();
public:
QWpsEditorWindow( QWidget * parent = 0, Qt::WFlags f = 0 );
void logMsg(QString s);
@ -74,3 +76,4 @@ signals:

View file

@ -12,23 +12,14 @@
<property name="windowTitle" >
<string>WPS Editor</string>
</property>
<widget class="QWidget" name="centralwidget" >
<property name="geometry" >
<rect>
<x>262</x>
<y>19</y>
<width>340</width>
<height>346</height>
</rect>
</property>
</widget>
<widget class="QWidget" name="centralwidget" />
<widget class="QMenuBar" name="menubar" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>882</width>
<height>19</height>
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="menuFile" >
@ -57,25 +48,8 @@
<addaction name="menuPlay" />
<addaction name="menuTarget" />
</widget>
<widget class="QStatusBar" name="statusbar" >
<property name="geometry" >
<rect>
<x>0</x>
<y>650</y>
<width>882</width>
<height>19</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar" />
<widget class="QDockWidget" name="dockPlainWps" >
<property name="geometry" >
<rect>
<x>0</x>
<y>371</y>
<width>882</width>
<height>279</height>
</rect>
</property>
<property name="minimumSize" >
<size>
<width>0</width>
@ -89,25 +63,7 @@
<number>8</number>
</attribute>
<widget class="QWidget" name="dockWidgetContents_3" >
<property name="geometry" >
<rect>
<x>0</x>
<y>20</y>
<width>882</width>
<height>259</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_2" >
<item rowspan="2" row="0" column="0" >
<widget class="QTextEdit" name="plainWpsEdit" >
<property name="autoFillBackground" >
<bool>false</bool>
</property>
<property name="readOnly" >
<bool>false</bool>
</property>
</widget>
</item>
<item row="0" column="1" >
<widget class="QPushButton" name="btnUpdatePlainWps" >
<property name="text" >
@ -128,18 +84,26 @@
</property>
</spacer>
</item>
<item rowspan="2" row="0" column="0" >
<widget class="NumberedTextView" name="plainWpsEdit" >
<property name="sizePolicy" >
<sizepolicy vsizetype="Preferred" hsizetype="Preferred" >
<horstretch>2</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape" >
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow" >
<enum>QFrame::Raised</enum>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QDockWidget" name="m_dockWidget" >
<property name="geometry" >
<rect>
<x>0</x>
<y>19</y>
<width>256</width>
<height>346</height>
</rect>
</property>
<property name="windowTitle" >
<string>Property Editor</string>
</property>
@ -150,25 +114,9 @@
<property name="enabled" >
<bool>false</bool>
</property>
<property name="geometry" >
<rect>
<x>0</x>
<y>20</y>
<width>256</width>
<height>326</height>
</rect>
</property>
</widget>
</widget>
<widget class="QDockWidget" name="dockWidget" >
<property name="geometry" >
<rect>
<x>608</x>
<y>19</y>
<width>274</width>
<height>346</height>
</rect>
</property>
<property name="minimumSize" >
<size>
<width>0</width>
@ -182,14 +130,6 @@
<number>2</number>
</attribute>
<widget class="QWidget" name="dockWidgetContents" >
<property name="geometry" >
<rect>
<x>0</x>
<y>20</y>
<width>274</width>
<height>326</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout" >
<item row="0" column="0" >
<widget class="QTextEdit" name="logEdit" >
@ -257,6 +197,12 @@
<extends>QTreeView</extends>
<header>QPropertyEditorWidget.h</header>
</customwidget>
<customwidget>
<class>NumberedTextView</class>
<extends>QFrame</extends>
<header>numberedtextview.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections>