Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/syntax'
Browse files Browse the repository at this point in the history
  • Loading branch information
pasnox committed Sep 16, 2012
2 parents 800ce16 + 217e31e commit 08cd303
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 50 deletions.
84 changes: 46 additions & 38 deletions example/UIMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
#include "QodeEdit.h"
#include "Manager.h"
#include "Tools.h"

#include <QUrl>
#include "Threading.h"

static QMutex qMutex;
static UIMain* qMain = 0;
Expand All @@ -48,27 +47,6 @@ QodeEditor::QodeEditor( QWidget* parent )
new QShortcut( QKeySequence::Save, this, SLOT( save() ) );
}

QString QodeEditor::fileContent( const QString& filePath, const QByteArray& textCodec )
{
QFile file( filePath );

if ( !file.exists() ) {
return QString::null;
}

if ( !file.open( QIODevice::ReadOnly ) ) {
return QString::null;
}

QTextCodec* codec = QTextCodec::codecForName( textCodec );

if ( !codec ) {
codec = QTextCodec::codecForLocale();
}

return codec->toUnicode( file.readAll() );
}

void QodeEditor::save()
{
document()->setModified( false );
Expand Down Expand Up @@ -109,6 +87,10 @@ UIMain::UIMain( QWidget* parent )
connect( mManager, SIGNAL( updated() ), this, SLOT( manager_updated() ) );

mManager->initialize();

#if !defined( QT_NO_DEBUG )
//debug();
#endif
}

UIMain::~UIMain()
Expand Down Expand Up @@ -171,6 +153,8 @@ QodeEditor* UIMain::editor( int row ) const

void UIMain::debug()
{
qWarning() << mManager->availableSyntaxes();

qWarning() << mManager->mimeTypeForFile( "toto.h" );
qWarning() << mManager->mimeTypeForFile( "toto.c" );
qWarning() << mManager->mimeTypeForFile( "toto.cpp" );
Expand Down Expand Up @@ -212,14 +196,20 @@ void UIMain::on_swEditors_currentChanged( int row )
ui->cbSyntax->setCurrentSyntax( editor( row )->textDocument()->syntaxHighlighter()->syntaxDocument().name() );
}

void UIMain::manager_updated()
void UIMain::listFilesFinished()
{
#if !defined( QT_NO_DEBUG )
//debug();
#endif

//qWarning() << Syntax::Factory::availableSyntaxes();
const QStringList files = mListFilesWatcher->result();
mListFilesWatcher->deleteLater();

if ( !mOpenFilesWatcher ) {
mOpenFilesWatcher = new QFutureWatcher<QHash<QString, QPair<QString, QString> > >( this );
connect( mOpenFilesWatcher, SIGNAL( finished() ), this, SLOT( openFilesFinished() ) );
mOpenFilesWatcher->setFuture( QodeEdit::Threading::getFilesContentWithTextCodec( files, "UTF-8" ) );
}
}

void UIMain::openFilesFinished()
{
/*".desktop", "4GL", "4GL-PER", "ABAP", "ABC", "ActionScript 2.0", "Ada", --
"AHDL", "Alerts", "Alerts_indent", "AMPLE", "ANS-Forth94", "ANSI C89", "Ansys",
"Apache Configuration", "Asm6502", "ASN.1", "ASP", "Asterisk", "AVR Assembler",
Expand All @@ -245,27 +235,45 @@ void UIMain::manager_updated()
"Vera", "Verilog", "VHDL", "VRML", "Wesnoth Markup Language", "WINE Config", "x.org Configuration",
"xHarbour", "XML", "XML (Debug)", "xslt", "XUL", "yacas", "Yacc/Bison", "YAML", "Zonnon", "Zsh"*/

const QString path = mManager->sharedDataFilePath( "/samples" );
const QStringList filePaths = QodeEdit::Tools::listFilesInPath( path, QStringList(), true );
const QHash<QString, QPair<QString, QString> > contents = mOpenFilesWatcher->result();
mOpenFilesWatcher->deleteLater();

foreach ( const QString& filePath, filePaths ) {
Syntax::Highlighter* highlighter = mManager->highlighterForFilePath( filePath );
foreach ( const QString& filePath, contents.keys() ) {
const QPair<QString, QString>& pair = contents[ filePath ];

if ( highlighter ) {
// File opened
if ( pair.second.isEmpty() ) {
QodeEditor* editor = new QodeEditor( this );
#warning Create a QodeEdit threaded/futured api for file loading
editor->setInitialText( QodeEditor::fileContent( filePath ) );
Syntax::Highlighter* highlighter = mManager->highlighterForFilePath( filePath );

editor->setInitialText( pair.first );
editor->textDocument()->setSyntaxHighlighter( highlighter );

QListWidgetItem* item = new QListWidgetItem( ui->lwEditors );
item->setText( QString( "%1 (%2)" ).arg( QFileInfo( filePath ).fileName() ).arg( highlighter->syntaxDocument().name() ) );
item->setData( Qt::UserRole, QVariant::fromValue( editor ) );
ui->swEditors->addWidget( editor );
}
// An error occurs
else {
qWarning( "%s: Can't create highlighter for '%s'", Q_FUNC_INFO, qPrintable( filePath ) );
qWarning() << pair.second;
}
}

}

void UIMain::manager_updated()
{
statusBar()->showMessage( tr( "QodeEdit ready." ) );

if ( ui->swEditors->count() > 0 ) {
return;
}

if ( !mListFilesWatcher ) {
const QString path = mManager->sharedDataFilePath( "/samples" );

mListFilesWatcher = new QFutureWatcher<QStringList>( this );
connect( mListFilesWatcher, SIGNAL( finished() ), this, SLOT( listFilesFinished() ) );
mListFilesWatcher->setFuture( QodeEdit::Threading::listFilesInPaths( QStringList( path ), QStringList(), true ) );
}
}
4 changes: 4 additions & 0 deletions example/UIMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ class UIMain : public QMainWindow

public slots:
void appendDebugMessage( const QString& message );
void listFilesFinished();
void openFilesFinished();

protected:
Ui_UIMain* ui;
QodeEdit::Manager* mManager;
QHash<QString, QodeEditor*> mEditors;
QPointer<QFutureWatcher<QStringList> > mListFilesWatcher;
QPointer<QFutureWatcher<QHash<QString, QPair<QString, QString> > > > mOpenFilesWatcher;

QodeEditor* editor( int row ) const;

Expand Down
9 changes: 3 additions & 6 deletions src/Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,12 @@ class QodeEdit::ManagerData : public QObject {
// update syntaxes
syntaxesFilesWatcher = new QFutureWatcher<QHash<QString, Syntax::Document> >( this );
connect( syntaxesFilesWatcher, SIGNAL( finished() ), this, SLOT( syntaxesFilesParsed() ) );

syntaxesFilesWatcher->setFuture( QodeEdit::Threading::parseSyntaxesFiles( manager->syntaxDefinitionFilePaths() ) );

// update schemas
/*schemasFilesWatcher = new QFutureWatcher<QStringList>( this );
connect( syntaxesFilesWatcher, SIGNAL( finished() ), this, SLOT( syntaxesFilesParsed() ) );
const QFuture<QStringList> future = QodeEdit::Threading::listFilesInPaths( manager->syntaxDefinitionFilePaths(), QStringList( "*.xml" ), true );
syntaxesFilesWatcher->setFuture( future );*/
schemasFilesWatcher->setFuture( QodeEdit::Threading::listFilesInPaths( manager->syntaxDefinitionFilePaths(), QStringList( "*.xml" ), true ) );
}
public slots:
Expand Down Expand Up @@ -226,7 +223,7 @@ Syntax::Highlighter* QodeEdit::Manager::highlighterForFilePath( const QString& f
}

if ( documents.isEmpty() ) {
return 0;
return QodeEdit::Manager::highlighter( QString::null, QString::null, textDocument );
}

return QodeEdit::Manager::highlighter( ( documents.end() -1 ).value()->name(), QString::null, textDocument );
Expand All @@ -245,7 +242,7 @@ Syntax::Highlighter* QodeEdit::Manager::highlighterForMimeType( const QString& m
}

if ( documents.isEmpty() ) {
return 0;
return QodeEdit::Manager::highlighter( QString::null, QString::null, textDocument );
}

return QodeEdit::Manager::highlighter( ( documents.end() -1 ).value()->name(), QString::null, textDocument );
Expand Down
5 changes: 5 additions & 0 deletions src/Threading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ QFuture<QHash<QString, QString> > QodeEdit::Threading::bestMatchingMimeTypesIcon
{
return QtConcurrent::run( QodeEdit::Tools::bestMatchingMimeTypesIcons, mimeTypes, defaultMimeType, false );
}

QFuture<QHash<QString, QPair<QString, QString> > > QodeEdit::Threading::getFilesContentWithTextCodec( const QStringList& filePaths, const QByteArray& textCodec )
{
return QtConcurrent::run( QodeEdit::Tools::getFilesContentWithTextCodec, filePaths, textCodec, false );
}
1 change: 1 addition & 0 deletions src/Threading.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Threading
QFuture<QStringList> listFilesInPaths( const QStringList& paths, const QStringList& filters, bool recursive, bool sort = true );
QFuture<QHash<QString, Syntax::Document> > parseSyntaxesFiles( const QStringList& paths );
QFuture<QHash<QString, QString> > bestMatchingMimeTypesIcons( const QHash<QString, QStringList>& mimeTypes, const QString& defaultMimeType );
QFuture<QHash<QString, QPair<QString, QString> > > getFilesContentWithTextCodec( const QStringList& filePaths, const QByteArray& textCodec );
}; // Threading

}; // QodeEdit
Expand Down
39 changes: 39 additions & 0 deletions src/Tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <QTime>
#include <QIcon>
#include <QApplication>
#include <QTextCodec>
#include <QDebug>

namespace QodeEdit {
Expand Down Expand Up @@ -175,6 +176,21 @@ bool QodeEdit::Tools::versionStringLessThan( const QString& left, const QString&
return leftParts.value( 4 ) < rightParts.value( 4 ); // not the best but afaik ;)
}

QTextCodec* QodeEdit::Tools::textCodec( const QByteArray& name, const QByteArray& data )
{
QTextCodec* codec = QTextCodec::codecForName( name );

if ( !codec ) {
codec = QTextCodec::codecForUtfText( data );
}

if ( !codec ) {
codec = QTextCodec::codecForLocale();
}

return codec;
}

QStringList QodeEdit::Tools::listFilesInPath( const QString& path, const QStringList& filters, bool recursive, bool sort )
{
QDir dir( path );
Expand Down Expand Up @@ -283,3 +299,26 @@ QHash<QString, QString> QodeEdit::Tools::bestMatchingMimeTypesIcons( const QHash

return hash;
}

QHash<QString, QPair<QString, QString> > QodeEdit::Tools::getFilesContentWithTextCodec( const QStringList& filePaths, const QByteArray& textCodec, bool processEvents )
{
const QTextCodec* codec = QodeEdit::Tools::textCodec( textCodec );
QHash<QString, QPair<QString, QString> > contents;

foreach ( const QString& filePath, filePaths ) {
QFile file( filePath );

if ( file.open( QIODevice::ReadOnly ) ) {
contents[ filePath ] = qMakePair( codec->toUnicode( file.readAll() ), QString() );
}
else {
contents[ filePath ] = qMakePair( QString(), file.errorString() );
}

if ( processEvents ) {
QApplication::processEvents();
}
}

return contents;
}
8 changes: 4 additions & 4 deletions src/Tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
#include "QodeEdit.h"

#include <Qt>

class QString;
template <class Key, class T> class QHash;
class QStringList;
#include <QHash>
#include <QPair>

namespace Syntax {
class Document;
Expand All @@ -48,11 +46,13 @@ namespace Tools {
bool localeAwareStringEquals( const QString& left, const QString& right );
bool localeAwareStringLessThan( const QString& left, const QString& right );
bool versionStringLessThan( const QString& left, const QString& right );
QTextCodec* textCodec( const QByteArray& name, const QByteArray& data = QByteArray() );

QStringList listFilesInPath( const QString& path, const QStringList& filters, bool recursive, bool sort = false );
QStringList listFilesInPaths( const QStringList& paths, const QStringList& filters, bool recursive, bool sort = false );
QHash<QString, Syntax::Document> parseSyntaxesFiles( const QStringList& paths );
QHash<QString, QString> bestMatchingMimeTypesIcons( const QHash<QString, QStringList>& mimeTypes, const QString& defaultMimeType, bool processEvents );
QHash<QString, QPair<QString, QString> > getFilesContentWithTextCodec( const QStringList& filePaths, const QByteArray& textCodec, bool processEvents );
}; // Tools

}; // QodeEdit
Expand Down
8 changes: 6 additions & 2 deletions src/syntax/Highlighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@ class Syntax::HighlighterPrivate {
: highlighter( _highlighter )
{
Q_ASSERT( highlighter );

if ( highlighter->textDocument() ) {
highlighter->textDocument()->setSyntaxHighlighter( highlighter );
}
}

TextBlockUserData* testBlockUserData( const QTextBlock& block ) const {
/*TextBlockUserData* testBlockUserData( const QTextBlock& block ) const {
return highlighter->textDocument()->testUserData( block );
}
TextBlockUserData* blockUserData( QTextBlock& block ) const {
return highlighter->textDocument()->userData( block );
}
}*/

private:
Syntax::Highlighter* highlighter;
Expand Down

0 comments on commit 08cd303

Please sign in to comment.