Skip to content

Commit

Permalink
Merge pull request #4 from pasnox/master
Browse files Browse the repository at this point in the history
Enhancements
  • Loading branch information
sschober committed Jul 25, 2013
2 parents ae2b6f2 + ef975ab commit 6bb4586
Show file tree
Hide file tree
Showing 11 changed files with 637 additions and 71 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
/*.pro.user
3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "sundown"]
path = sundown
url = https://github.com/pasnox/sundown.git
161 changes: 161 additions & 0 deletions PlainTextEditor.cpp
@@ -0,0 +1,161 @@
/*
* Indent / Unindent code taken from project at: https://code.google.com/p/kodepad
**/

#include "PlainTextEditor.h"

#include <QTextCursor>
#include <QTextBlock>

#define IndentSize 4
#define TabSize 4

PlainTextEditor::PlainTextEditor( QWidget* parent )
: QPlainTextEdit( parent )
{
setTabStopWidth( 40 );
setTabChangesFocus( false );
}

#include <QDebug>

bool PlainTextEditor::event( QEvent* event )
{
if ( event->type() == QEvent::KeyPress ) {
QKeyEvent* ke = static_cast<QKeyEvent*>( event );
const bool isBackTab =
( ke->key() == Qt::Key_Backtab && ( ke->modifiers() == Qt::NoModifier || ke->modifiers() == Qt::ShiftModifier ) ) ||
( ke->key() == Qt::Key_Tab && ke->modifiers() == Qt::ShiftModifier )
;
const bool isTab = ke->key() == Qt::Key_Tab && ke->modifiers() == Qt::NoModifier;

if ( isTab ) {
onTabKey( ke );
return true;
}
else if ( isBackTab ) {
onShiftTabKey( ke );
return true;
}
}

return QPlainTextEdit::event( event );
}

void PlainTextEditor::onTabKey( QKeyEvent* event )
{
Q_UNUSED( event );

if ( !textCursor().hasSelection() ) {
textCursor().insertText( QString( IndentSize, QLatin1Char( ' ' ) ) );
}
else {
indentOrUnindent( true );
}
}

void PlainTextEditor::onShiftTabKey( QKeyEvent* event )
{
Q_UNUSED( event );

if ( !textCursor().hasSelection() ) {
QTextCursor cursor = textCursor();
int newPos = cursor.position() -IndentSize;

if ( newPos < 0 ) {
newPos = 0;
}

cursor.setPosition( newPos );
setTextCursor( cursor );
}
else {
indentOrUnindent( false );
}
}

void PlainTextEditor::indentOrUnindent( bool doIndent )
{
QTextCursor cursor = textCursor();
cursor.beginEditBlock();

// Indent or unindent the selected lines
int pos = cursor.position();
int anchor = cursor.anchor();
int start = qMin( anchor, pos );
int end = qMax( anchor, pos );

QTextDocument* doc = document();
QTextBlock startBlock = doc->findBlock(start);
QTextBlock endBlock = doc->findBlock( end -1 ).next();

for ( QTextBlock block = startBlock; block != endBlock; block = block.next() ) {
QString text = block.text();

if ( doIndent ) {
const int indentPosition = firstNonSpace( text );
cursor.setPosition( block.position() +indentPosition );
cursor.insertText( QString( IndentSize, QLatin1Char( ' ' ) ) );
} else {
const int indentPosition = firstNonSpace( text );
const int targetColumn = indentedColumn( columnAt( text, indentPosition ), false );
cursor.setPosition( block.position() +indentPosition );
cursor.setPosition( block.position() +targetColumn, QTextCursor::KeepAnchor );
cursor.removeSelectedText();
}
}

// Reselect the selected lines
cursor.setPosition( startBlock.position() );
cursor.setPosition( endBlock.previous().position(), QTextCursor::KeepAnchor );
cursor.movePosition( QTextCursor::EndOfBlock, QTextCursor::KeepAnchor );

cursor.endEditBlock();
setTextCursor( cursor );
}

int PlainTextEditor::firstNonSpace( const QString& text ) const
{
int i = 0;

while ( i < text.size() ) {
if ( !text.at( i ).isSpace() ) {
return i;
}

++i;
}

return i;
}

int PlainTextEditor::columnAt( const QString& text, int position ) const
{
int column = 0;

for ( int i = 0; i < position; ++i ) {
if ( text.at( i ) == QLatin1Char( '\t' ) ) {
column = column -( column %TabSize ) +TabSize;
}
else {
++column;
}
}

return column;
}

int PlainTextEditor::indentedColumn( int column, bool doIndent ) const
{
const int aligned = ( column /IndentSize ) *IndentSize;

if ( doIndent ) {
return aligned +IndentSize;
}

if ( aligned < column ) {
return aligned;
}

return qMax( 0, aligned -IndentSize );
}
26 changes: 26 additions & 0 deletions PlainTextEditor.h
@@ -0,0 +1,26 @@
#ifndef PLAINTEXTEDITOR_H
#define PLAINTEXTEDITOR_H

#include <QPlainTextEdit>

class PlainTextEditor : public QPlainTextEdit
{
Q_OBJECT

public:
PlainTextEditor( QWidget* parent = 0 );

virtual bool event( QEvent* event );

public slots:
void indentOrUnindent( bool doIndent );

private:
void onTabKey( QKeyEvent* event );
void onShiftTabKey(QKeyEvent* event );
int firstNonSpace( const QString& text ) const;
int columnAt( const QString& text, int position ) const;
int indentedColumn( int column, bool doIndent ) const;
};

#endif // PLAINTEXTEDITOR_H
13 changes: 10 additions & 3 deletions main.cpp
@@ -1,11 +1,18 @@
#include <QtGui/QApplication>
#include <QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setApplicationName( "QarkDown" );
a.setApplicationVersion( "1.0.0" );
a.setOrganizationDomain( "com.github/pasnox/qarkdown" );
a.setOrganizationName( a.applicationName() );
a.setWindowIcon( QIcon( ":/icons/application.png" ) );

MainWindow w;
w.show();

w.showMaximized();

QObject::connect(&a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}

0 comments on commit 6bb4586

Please sign in to comment.