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

Commit

Permalink
Project layout change WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pasnox committed Sep 14, 2012
1 parent 2016456 commit 629e219
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 72 deletions.
44 changes: 17 additions & 27 deletions src/QodeEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "QodeEditConfig.h"
#include "Tools.h"
#include "syntax/Document.h"
#include "syntax/Model.h"
#include "syntax/Highlighter.h"
#include "theme/Theme.h"

Expand All @@ -29,7 +30,7 @@

/*#include "syntax/DocumentBuilder.h"
#include "syntax/Highlighter.h"
#include "syntax/Model.h"
#include "theme/Theme.h"
#include "editor/TextDocument.h"
#include "Tools.h"
Expand Down Expand Up @@ -176,6 +177,21 @@ QStringList QodeEdit::Manager::availableSyntaxes() const
return syntaxes;
}

QHash<QString, Syntax::Document> QodeEdit::Manager::availableDocuments() const
{
return d->mDocuments;
}

Syntax::Document QodeEdit::Manager::document( const QString& name )
{
return d->mDocuments.value( name );
}

Syntax::Model* QodeEdit::Manager::model( QObject* parent )
{
return new Syntax::Model( this, parent );
}

Syntax::Highlighter* QodeEdit::Manager::highlighter( const QString& syntaxName, const QString& syntaxThemeName, TextDocument* textDocument ) const
{
Q_UNUSED( syntaxThemeName );
Expand Down Expand Up @@ -221,15 +237,6 @@ Syntax::Highlighter* QodeEdit::Manager::highlighterForMimeType( const QString& m
return QodeEdit::Manager::highlighter( ( documents.end() -1 ).value()->name(), QString::null, textDocument );
}










/*bool QodeEdit::Manager::load( QString* _error )
{
#if !defined( QT_NO_DEBUG )
Expand Down Expand Up @@ -290,23 +297,6 @@ Syntax::Highlighter* QodeEdit::Manager::highlighterForMimeType( const QString& m
void QodeEdit::Manager::free()
{
d->mDocuments.clear();
}
Syntax::Document QodeEdit::Manager::document( const QString& name )
{
return d->mDocuments.value( name );
}
Syntax::Model* QodeEdit::Manager::model( QObject* parent )
{
d->mModels << new Syntax::Model( &d->mDocuments, parent );
return d->mModels.last();
}*/

const char* QodeEdit::Manager::version()
Expand Down
24 changes: 10 additions & 14 deletions src/QodeEdit.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define QODEEDIT_H

#include <QObject>
#include <QHash>

class QStringList;
class QFileInfo;
Expand Down Expand Up @@ -124,35 +125,30 @@ class Manager : public QObject

QStringList availableSyntaxes() const;

QHash<QString, Syntax::Document> availableDocuments() const;
Syntax::Document document( const QString& name );

Syntax::Model* model( QObject* parent = 0 );

Syntax::Highlighter* highlighter( const QString& syntaxName, const QString& syntaxThemeName = QString::null, TextDocument* textDocument = 0 ) const;
Syntax::Highlighter* highlighterForFilePath( const QString& filePath, TextDocument* textDocument = 0 ) const;
Syntax::Highlighter* highlighterForMimeType( const QString& mimeType, TextDocument* textDocument = 0 ) const;

////
bool load( QString* error = 0 );
void free();





Syntax::Document document( const QString& syntaxName );



Syntax::Model* model( QObject* parent = 0 );
////





static QString sharedDataFilePath( const QString& extended = QString::null );
static QString schemaDefinitionFilePath();
static QString syntaxDefinitionFilePath();

static const char* version();
static QString versionString();

signals:
void syntaxesChanged();
void themesChanged();
}; // Manager

}; // QodeEdit
Expand Down
67 changes: 42 additions & 25 deletions src/syntax/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,48 @@
****************************************************************************/
#include "Model.h"
#include "Document.h"
#include "QodeEdit.h"
#include "Tools.h"

#include <QStringList>
#include <QIcon>
#include <QApplication>
#include <QDebug>

// ModelPrivate

class Syntax::ModelPrivate {
class Syntax::ModelPrivate : public QObject {
Q_OBJECT

public:
const QHash<QString, Syntax::Document>* constSyntaxes;
Syntax::Model* model;
QodeEdit::Manager* manager;
QStringList syntaxes;
QHash<QString, QIcon> icons; // name, icon
bool updatingIcons;

ModelPrivate( Syntax::Model* _model, const QHash<QString, Syntax::Document>* _syntaxes )
: constSyntaxes( _syntaxes ),
model( _model )
ModelPrivate( Syntax::Model* _model, QodeEdit::Manager* _manager )
: model( _model ),
manager( _manager ),
updatingIcons( false )
{
Q_ASSERT( model );
Q_ASSERT( constSyntaxes );
Q_ASSERT( manager );
updateSyntaxes();
connect( manager, SIGNAL( syntaxesChanged() ), this, SLOT( updateSyntaxes() ) );
}

public slots:
void updateIcons() {
if ( updatingIcons ) {
return;
}

updatingIcons = true;
const QHash<QString, Syntax::Document> availableDocuments = manager->availableDocuments();

foreach ( const QString& syntax, syntaxes ) {
const Syntax::Document& document = (*constSyntaxes)[ syntax ];
const Syntax::Document& document = availableDocuments[ syntax ];

if ( icons.contains( document.name() ) ) {
continue;
Expand Down Expand Up @@ -68,14 +85,19 @@ class Syntax::ModelPrivate {
icon = QIcon::fromTheme( iconName );
icons[ document.name() ] = icon;
}

//QApplication::processEvents(); // avoid possible freeze/lag
}

updatingIcons = false;
}

void update() {
const QStringList oldSyntaxes;
void updateSyntaxes() {
const QHash<QString, Syntax::Document> availableDocuments = manager->availableDocuments();
const QStringList oldSyntaxes = syntaxes;
QStringList newSyntaxes;

foreach ( const Syntax::Document& document, constSyntaxes->values() ) {
foreach ( const Syntax::Document& document, availableDocuments.values() ) {
newSyntaxes << ( document.localizedName().isEmpty() ? document.name() : document.localizedName() );
}

Expand All @@ -92,15 +114,14 @@ class Syntax::ModelPrivate {
emit model->layoutAboutToBeChanged();

syntaxes = newSyntaxes;
updateIcons();

for ( int i = 0; i < newSyntaxes.count(); i++ ) {
const Syntax::Document& document = (*constSyntaxes)[ newSyntaxes[ i ] ];
const Syntax::Document& document = availableDocuments[ newSyntaxes[ i ] ];
newPositions[ document.name() ] = i;
}

for ( int i = 0; i < oldSyntaxes.count(); i++ ) {
const Syntax::Document& document = (*constSyntaxes)[ oldSyntaxes[ i ] ];
const Syntax::Document& document = availableDocuments[ oldSyntaxes[ i ] ];
const int row = newPositions.value( document.name(), -1 );

if ( row == -1 ) {
Expand All @@ -113,24 +134,22 @@ class Syntax::ModelPrivate {

model->changePersistentIndexList( oldIndexes, newIndexes );

updateIcons();

emit model->layoutChanged();
}

private:
Syntax::Model* model;
};

// Model

Syntax::Model::Model( const QHash<QString, Syntax::Document>* syntaxes, QObject* parent )
Syntax::Model::Model( QodeEdit::Manager* manager, QObject* parent )
: QAbstractListModel( parent ),
d( new Syntax::ModelPrivate( this, syntaxes ) )
d( new Syntax::ModelPrivate( this, manager ) )
{
}

Syntax::Model::~Model()
{
delete d;
}

int Syntax::Model::rowCount( const QModelIndex& parent ) const
Expand All @@ -144,22 +163,20 @@ QVariant Syntax::Model::data( const QModelIndex& index, int role ) const
return QVariant();
}

const Syntax::Document& document = (*d->constSyntaxes)[ d->syntaxes[ index.row() ] ];
const Syntax::Document document = d->manager->document( d->syntaxes.at( index.row() ) );

switch ( role ) {
case Qt::DecorationRole:
return d->icons.value( document.name() );
case Qt::DisplayRole:
case Qt::ToolTipRole:
return document.localizedName().isEmpty() ? document.name() : document.localizedName();
case Syntax::Model::DisplayName:
return document.localizedName().isEmpty() ? document.name() : document.localizedName();
case Syntax::Model::InternalName:
return document.name();
}

return QVariant();
}

void Syntax::Model::update()
{
d->update();
}
#include "Model.moc"
13 changes: 7 additions & 6 deletions src/syntax/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

#include <QAbstractListModel>

namespace QodeEdit {
class Manager;
};

namespace Syntax {

class ModelPrivate;
class Document;

class Model : public QAbstractListModel
{
Expand All @@ -30,18 +33,16 @@ class Model : public QAbstractListModel

public:
enum ExtendedRole {
InternalName = Qt::UserRole
InternalName = Qt::UserRole,
DisplayName
};

Model( const QHash<QString, Syntax::Document>* syntaxes, QObject* parent = 0 );
Model( QodeEdit::Manager* manager, QObject* parent = 0 );
virtual ~Model();

virtual int rowCount( const QModelIndex& parent = QModelIndex() ) const;
virtual QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const;

public slots:
void update();

private:
Syntax::ModelPrivate* d;
};
Expand Down

0 comments on commit 629e219

Please sign in to comment.