From 99530f8faee4bbfac83f7d32640a8c8ba0d1cb0b Mon Sep 17 00:00:00 2001 From: "Nox P@sNox" Date: Sat, 15 Sep 2012 17:40:21 +0200 Subject: [PATCH] Make the api for logging thread safe. Small change due to threading support and api change --- example/UIMain.cpp | 230 +++++++++++++++++++++++++-------------------- example/UIMain.h | 14 ++- 2 files changed, 140 insertions(+), 104 deletions(-) diff --git a/example/UIMain.cpp b/example/UIMain.cpp index fdeaa74..928a575 100644 --- a/example/UIMain.cpp +++ b/example/UIMain.cpp @@ -19,11 +19,16 @@ #include "editor/TextDocument.h" #include "margin/MarginStacker.h" #include "syntax/Highlighter.h" +#include "syntax/Model.h" +#include "syntax/Document.h" #include "QodeEdit.h" +#include "Manager.h" +#include "Tools.h" #include -UIMain* UIMain::qMain = 0; +static QMutex qMutex; +static UIMain* qMain = 0; // QodeEditor @@ -83,135 +88,73 @@ class SpacerWidget : public QWidget { // UIMain UIMain::UIMain( QWidget* parent ) - : QMainWindow( parent ), ui( new Ui_UIMain ) + : QMainWindow( parent ), + ui( new Ui_UIMain ), + mManager( new QodeEdit::Manager( this ) ) { - if ( !UIMain::qMain ) { - UIMain::qMain = this; + { + QMutexLocker locker( &qMutex ); + + if ( !qMain ) { + qMain = this; + } } ui->setupUi( this ); + ui->cbSyntax->setModel( mManager->model() ); ui->toolBar->addWidget( new SpacerWidget( this ) ); ui->toolBar->addWidget( ui->cbSyntax ); qInstallMsgHandler( UIMain::messageHandler ); + connect( mManager, SIGNAL( updated() ), this, SLOT( manager_updated() ) ); - return; - - QString error; - - /*if ( Syntax::Factory::load( &error ) ) { - qWarning() << Q_FUNC_INFO << "Syntaxes loaded correctly"; - }*/ - - //qWarning() << Syntax::Factory::availableSyntaxes(); - - /*".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", - "AWK", "B-Method", "Bash", "BibTeX", "Boo", "C", "C#", "C++", "Cg", "CGiS", "ChangeLog", - "Cisco", "Clipper", "Clojure", "CMake", "CoffeeScript", "ColdFusion", "Common Lisp", - "Component-Pascal", "Crack", "CSS", "CUE Sheet", "D", "Debian Changelog", "Debian Control", - "Diff", "Django HTML Template", "dot", "Doxygen", "DoxygenLua", "DTD", "E Language", "Eiffel", - "Email", "Erlang", "Euphoria", "ferite", "Fortran", "FreeBASIC", "FSharp", "fstab", "GAP", - "GDB Backtrace", "GDL", "GlossTex", "GLSL", "GNU Assembler", "GNU Gettext", "GNU Linker Script", - "GNU M4", "Go", "Haskell", "Haxe", "HTML", "IDL", "ILERPG", "Inform", "INI Files", "Intel x86 (NASM)", - "Jam", "Java", "Javadoc", "JavaScript", "JSON", "JSP", "KBasic", "KDev-PG[-Qt] Grammar", "LaTeX", - "LDIF", "Lex/Flex", "LilyPond", "Literate Haskell", "Logtalk", "LPC", "LSL", "Lua", "M3U", "MAB-DB", - "Makefile", "Mason", "Matlab", "Maxima", "MediaWiki", "MEL", "mergetag text", "Metapost/Metafont", - "MIPS Assembler", "Modelica", "Modelines", "Modula-2", "MonoBasic", "Motorola 68k (VASM/Devpac)", - "Motorola DSP56k", "MS-DOS Batch", "Music Publisher", "Nemerle", "noweb", "Objective Caml", - "Objective-C", "Objective-C++", "Octave", "OORS", "OPAL", "Pango", "Pascal", "Perl", "PGN", - "PHP/PHP", "PicAsm", "Pig", "Pike", "PostScript", "POV-Ray", "progress", "Prolog", "PureBasic", - "Python", "QMake", "QML", "Quake Script", "R Script", "RapidQ", "RELAX NG", "RelaxNG-Compact", - "RenderMan RIB", "reStructuredText", "REXX", "Roff", "RPM Spec", "RSI IDL", "Ruby", "Ruby/Rails/RHTML", - "Sather", "Scala", "Scheme", "scilab", "SCSS", "sed", "SGML", "Sieve", "SiSU", "SML", "Spice", "SQL", - "SQL (MySQL)", "SQL (PostgreSQL)", "Stata", "SystemC", "SystemVerilog", "TADS 3", "Tcl/Tk", "Tcsh", - "Texinfo", "TI Basic", "Troff Mandoc", "txt2tags", "UnrealScript", "Valgrind Suppression", "Velocity", - "Vera", "Verilog", "VHDL", "VRML", "Wesnoth Markup Language", "WINE Config", "x.org Configuration", - "xHarbour", "XML", "XML (Debug)", "xslt", "XUL", "yacas", "Yacc/Bison", "YAML", "Zonnon", "Zsh"*/ - - /*QDir dir( QodeEdit::Manager::sharedDataFilePath( "/samples" ) ); - const QFileInfoList files = dir.entryInfoList( QDir::Files | QDir::NoDotAndDotDot ); - - foreach ( const QFileInfo& file, files ) { - const QString filePath = file.absoluteFilePath(); - Syntax::Highlighter* highlighter = Syntax::Factory::highlighterForFilePath( filePath ); - - if ( highlighter ) { - QodeEditor* editor = new QodeEditor( this ); - editor->setInitialText( QodeEditor::fileContent( filePath ) ); - editor->textDocument()->setSyntaxHighlighter( highlighter ); - - QListWidgetItem* item = new QListWidgetItem( ui->lwEditors ); - item->setText( QString( "%1 (%2)" ).arg( file.fileName() ).arg( highlighter->syntaxDocument().name() ) ); - item->setData( Qt::UserRole, QVariant::fromValue( editor ) ); - ui->swEditors->addWidget( editor ); - } - else { - qWarning( "%s: Can't create highlighter for '%s'", Q_FUNC_INFO, qPrintable( filePath ) ); - } - }*/ - - statusBar()->showMessage( error ); - - /*qWarning() << Syntax::Factory::mimeTypeForFile( "toto.h" ); - qWarning() << Syntax::Factory::mimeTypeForFile( "toto.c" ); - qWarning() << Syntax::Factory::mimeTypeForFile( "toto.cpp" ); - qWarning() << Syntax::Factory::mimeTypeForFile( "toto.adb" ); - - qWarning() << Syntax::Factory::mimeTypeForData( "#include \n" ); - qWarning() << Syntax::Factory::mimeTypeForData( "#include \n" ); - qWarning() << Syntax::Factory::mimeTypeForData( "#import \n" ); - - qWarning() << Syntax::Factory::mimeTypeForUrl( QUrl( "http://toto.com/test.html" ) ); - qWarning() << Syntax::Factory::mimeTypeForUrl( QUrl( "http://toto.com/test.pdf" ) ); - qWarning() << Syntax::Factory::mimeTypeForUrl( QUrl( "http://toto.com/test.jpg" ) ); - - qWarning() << Syntax::Factory::mimeTypesForFileName( "toto.h" ); - qWarning() << Syntax::Factory::mimeTypesForFileName( "toto.c" ); - qWarning() << Syntax::Factory::mimeTypesForFileName( "toto.cpp" ); - qWarning() << Syntax::Factory::mimeTypesForFileName( "toto.adb" ); - - qWarning() << QodeEdit::rulerToString( QodeEdit::NoRuler ); - qWarning() << QodeEdit::rulerToString( QodeEdit::BackgroundRuler ); - qWarning() << QodeEdit::stringToRuler( "background" ); - qWarning() << QodeEdit::stringToRuler( "backgroundRuler" ); - qWarning() << QodeEdit::stringToRuler( "Background" );*/ + mManager->initialize(); } UIMain::~UIMain() { - if ( UIMain::qMain == this ) { - UIMain::qMain = 0; + { + QMutexLocker locker( &qMutex ); + + if ( qMain == this ) { + qMain = 0; + } } delete ui; - //Syntax::Factory::free(); -} - -void UIMain::appendDebugMessage( const QString& message ) -{ - ui->pteDebug->appendPlainText( message ); - printf( "%s\n", qPrintable( message ) ); } void UIMain::messageHandler( QtMsgType type, const char* msg ) { + QMutexLocker locker( &qMutex ); + QString string; + switch ( type ) { case QtDebugMsg: - UIMain::qMain->appendDebugMessage( QString( "Debug: %1" ).arg( msg ) ); + string = QString( "Debug: %1" ).arg( msg ); break; case QtWarningMsg: - UIMain::qMain->appendDebugMessage( QString( "Warning: %1" ).arg( msg ) ); + string = QString( "Warning: %1" ).arg( msg ); break; case QtCriticalMsg: - UIMain::qMain->appendDebugMessage( QString( "Critical: %1" ).arg( msg ) ); + string = QString( "Critical: %1" ).arg( msg ); break; case QtFatalMsg: - UIMain::qMain->appendDebugMessage( QString( "Fatal: %1" ).arg( msg ) ); - //abort(); + string = QString( "Fatal: %1" ).arg( msg ); break; } + + QMetaObject::invokeMethod( qMain, "appendDebugMessage", Qt::QueuedConnection, Q_ARG( const QString&, string ) ); + + if ( type == QtFatalMsg ) { + abort(); + } +} + +void UIMain::appendDebugMessage( const QString& message ) +{ + ui->pteDebug->appendPlainText( message ); + printf( "%s\n", qPrintable( message ) ); } QodeEditor* UIMain::editor( int row ) const @@ -226,6 +169,33 @@ QodeEditor* UIMain::editor( int row ) const return item->data( Qt::UserRole ).value(); } +void UIMain::debug() +{ + qWarning() << mManager->mimeTypeForFile( "toto.h" ); + qWarning() << mManager->mimeTypeForFile( "toto.c" ); + qWarning() << mManager->mimeTypeForFile( "toto.cpp" ); + qWarning() << mManager->mimeTypeForFile( "toto.adb" ); + + qWarning() << mManager->mimeTypeForData( "#include \n" ); + qWarning() << mManager->mimeTypeForData( "#include \n" ); + qWarning() << mManager->mimeTypeForData( "#import \n" ); + + qWarning() << mManager->mimeTypeForUrl( QUrl( "http://toto.com/test.html" ) ); + qWarning() << mManager->mimeTypeForUrl( QUrl( "http://toto.com/test.pdf" ) ); + qWarning() << mManager->mimeTypeForUrl( QUrl( "http://toto.com/test.jpg" ) ); + + qWarning() << mManager->mimeTypesForFileName( "toto.h" ); + qWarning() << mManager->mimeTypesForFileName( "toto.c" ); + qWarning() << mManager->mimeTypesForFileName( "toto.cpp" ); + qWarning() << mManager->mimeTypesForFileName( "toto.adb" ); + + qWarning() << QodeEdit::Tools::rulerToString( QodeEdit::NoRuler ); + qWarning() << QodeEdit::Tools::rulerToString( QodeEdit::BackgroundRuler ); + qWarning() << QodeEdit::Tools::stringToRuler( "background" ); + qWarning() << QodeEdit::Tools::stringToRuler( "backgroundRuler" ); + qWarning() << QodeEdit::Tools::stringToRuler( "Background" ); +} + void UIMain::on_lwEditors_currentRowChanged( int row ) { if ( ui->swEditors->currentIndex() != row ) { @@ -239,5 +209,63 @@ void UIMain::on_swEditors_currentChanged( int row ) ui->lwEditors->setCurrentRow( row ); } - //ui->cbSyntax->setCurrentSyntax( editor( row )->textDocument()->syntaxHighlighter()->syntaxDocument().name() ); + ui->cbSyntax->setCurrentSyntax( editor( row )->textDocument()->syntaxHighlighter()->syntaxDocument().name() ); +} + +void UIMain::manager_updated() +{ +#if !defined( QT_NO_DEBUG ) + //debug(); +#endif + + //qWarning() << Syntax::Factory::availableSyntaxes(); + + /*".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", + "AWK", "B-Method", "Bash", "BibTeX", "Boo", "C", "C#", "C++", "Cg", "CGiS", "ChangeLog", + "Cisco", "Clipper", "Clojure", "CMake", "CoffeeScript", "ColdFusion", "Common Lisp", + "Component-Pascal", "Crack", "CSS", "CUE Sheet", "D", "Debian Changelog", "Debian Control", + "Diff", "Django HTML Template", "dot", "Doxygen", "DoxygenLua", "DTD", "E Language", "Eiffel", + "Email", "Erlang", "Euphoria", "ferite", "Fortran", "FreeBASIC", "FSharp", "fstab", "GAP", + "GDB Backtrace", "GDL", "GlossTex", "GLSL", "GNU Assembler", "GNU Gettext", "GNU Linker Script", + "GNU M4", "Go", "Haskell", "Haxe", "HTML", "IDL", "ILERPG", "Inform", "INI Files", "Intel x86 (NASM)", + "Jam", "Java", "Javadoc", "JavaScript", "JSON", "JSP", "KBasic", "KDev-PG[-Qt] Grammar", "LaTeX", + "LDIF", "Lex/Flex", "LilyPond", "Literate Haskell", "Logtalk", "LPC", "LSL", "Lua", "M3U", "MAB-DB", + "Makefile", "Mason", "Matlab", "Maxima", "MediaWiki", "MEL", "mergetag text", "Metapost/Metafont", + "MIPS Assembler", "Modelica", "Modelines", "Modula-2", "MonoBasic", "Motorola 68k (VASM/Devpac)", + "Motorola DSP56k", "MS-DOS Batch", "Music Publisher", "Nemerle", "noweb", "Objective Caml", + "Objective-C", "Objective-C++", "Octave", "OORS", "OPAL", "Pango", "Pascal", "Perl", "PGN", + "PHP/PHP", "PicAsm", "Pig", "Pike", "PostScript", "POV-Ray", "progress", "Prolog", "PureBasic", + "Python", "QMake", "QML", "Quake Script", "R Script", "RapidQ", "RELAX NG", "RelaxNG-Compact", + "RenderMan RIB", "reStructuredText", "REXX", "Roff", "RPM Spec", "RSI IDL", "Ruby", "Ruby/Rails/RHTML", + "Sather", "Scala", "Scheme", "scilab", "SCSS", "sed", "SGML", "Sieve", "SiSU", "SML", "Spice", "SQL", + "SQL (MySQL)", "SQL (PostgreSQL)", "Stata", "SystemC", "SystemVerilog", "TADS 3", "Tcl/Tk", "Tcsh", + "Texinfo", "TI Basic", "Troff Mandoc", "txt2tags", "UnrealScript", "Valgrind Suppression", "Velocity", + "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 ); + + foreach ( const QString& filePath, filePaths ) { + Syntax::Highlighter* highlighter = mManager->highlighterForFilePath( filePath ); + + if ( highlighter ) { + QodeEditor* editor = new QodeEditor( this ); + #warning Create a QodeEdit threaded/futured api for file loading + editor->setInitialText( QodeEditor::fileContent( filePath ) ); + 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 ); + } + else { + qWarning( "%s: Can't create highlighter for '%s'", Q_FUNC_INFO, qPrintable( filePath ) ); + } + } + + statusBar()->showMessage( tr( "QodeEdit ready." ) ); } diff --git a/example/UIMain.h b/example/UIMain.h index f966353..742b1f3 100644 --- a/example/UIMain.h +++ b/example/UIMain.h @@ -22,6 +22,10 @@ class Ui_UIMain; +namespace QodeEdit { + class Manager; +}; + class QodeEditor : public CodeEditor { Q_OBJECT @@ -45,20 +49,24 @@ class UIMain : public QMainWindow UIMain( QWidget* parent = 0 ); virtual ~UIMain(); - void appendDebugMessage( const QString& message ); - static void messageHandler( QtMsgType type, const char* msg ); - static UIMain* qMain; + +public slots: + void appendDebugMessage( const QString& message ); protected: Ui_UIMain* ui; + QodeEdit::Manager* mManager; QHash mEditors; QodeEditor* editor( int row ) const; + void debug(); + protected slots: void on_lwEditors_currentRowChanged( int row ); void on_swEditors_currentChanged( int row ); + void manager_updated(); }; #endif // UIMAIN_H