1 /************************************************************************
3 * Copyright 2010-2012 Jakob Leben (jakob.leben@gmail.com)
5 * This file is part of SuperCollider Qt GUI.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ************************************************************************/
22 #include "QcTextEdit.h"
23 #include "../QcWidgetFactory.h"
28 #include <QApplication>
30 class QcTextEditFactory
: public QcWidgetFactory
<QcTextEdit
>
32 void initialize( QWidgetProxy
*p
, QcTextEdit
*w
) {
33 p
->setMouseEventWidget( w
->viewport() );
37 QC_DECLARE_FACTORY( QcTextEdit
, QcTextEditFactory
);
39 QcTextEdit::QcTextEdit() : _interpretSelection(false)
41 connect( this, SIGNAL(interpret(QString
)),
42 qApp
, SLOT(interpret(QString
)),
43 Qt::QueuedConnection
);
47 QString
QcTextEdit::documentFilename() const
52 void QcTextEdit::setDocument( const QString
&filename
)
54 QFile
file( filename
);
55 if (!file
.open(QIODevice::ReadOnly
| QIODevice::Text
))
57 QByteArray data
= file
.readAll();
62 int QcTextEdit::selectionStart() const
64 return textCursor().selectionStart();
67 int QcTextEdit::selectionSize() const
69 QTextCursor cursor
= textCursor();
70 return cursor
.selectionEnd() - cursor
.selectionStart();
73 void QcTextEdit::select( int start
, int size
)
75 if( start
< 0 ) start
= 0;
77 QTextCursor
cursor( document() );
79 cursor
.movePosition( QTextCursor::Right
, QTextCursor::MoveAnchor
, start
);
80 cursor
.movePosition( size
> 0 ? QTextCursor::Right
: QTextCursor::Left
,
81 QTextCursor::KeepAnchor
, qAbs(size
) );
83 setTextCursor( cursor
);
86 QString
QcTextEdit::selectedString() const
88 QString
selection( textCursor().selectedText() );
89 return prepareText(selection
);
92 void QcTextEdit::replaceSelectedText( const QString
&string
)
94 QTextCursor
cursor( textCursor() );
95 if( cursor
.hasSelection() ) {
96 cursor
.removeSelectedText();
97 cursor
.insertText( string
);
101 void QcTextEdit::setTextFont( const QFont
&f
)
103 QTextCharFormat format
;
106 QTextCursor
cursor( document() );
107 cursor
.select( QTextCursor::Document
);
108 cursor
.setCharFormat( format
);
110 QTextEdit::setFont(f
);
113 void QcTextEdit::setTextColor( const QColor
&color
)
115 QTextCharFormat format
;
116 format
.setForeground( color
);
118 QTextCursor
cursor( document() );
119 cursor
.select( QTextCursor::Document
);
120 cursor
.setCharFormat( format
);
123 void QcTextEdit::setRangeColor( const VariantList
&list
)
125 if( list
.data
.count() < 3 ) return;
126 QColor c
= list
.data
[0].value
<QColor
>();
127 int start
= list
.data
[1].toInt();
128 int size
= list
.data
[2].toInt();
130 QTextCharFormat format
;
131 format
.setForeground( c
);
133 QTextCursor
cursor( document() );
134 cursor
.movePosition( QTextCursor::NextCharacter
, QTextCursor::MoveAnchor
, start
);
135 cursor
.movePosition( QTextCursor::NextCharacter
, QTextCursor::KeepAnchor
, size
);
136 cursor
.setCharFormat( format
);
139 void QcTextEdit::setRangeFont( const VariantList
& list
)
141 if( list
.data
.count() < 3 ) return;
142 QFont f
= list
.data
[0].value
<QFont
>();
143 int start
= list
.data
[1].toInt();
144 int size
= list
.data
[2].toInt();
146 QTextCharFormat format
;
149 QTextCursor
cursor( document() );
150 cursor
.movePosition( QTextCursor::NextCharacter
, QTextCursor::MoveAnchor
, start
);
151 cursor
.movePosition( QTextCursor::NextCharacter
, QTextCursor::KeepAnchor
, size
);
152 cursor
.setCharFormat( format
);
155 void QcTextEdit::setRangeText( const VariantList
& list
)
157 if( list
.data
.count() < 3 ) return;
158 QString text
= list
.data
[0].value
<QString
>();
159 int start
= list
.data
[1].toInt();
160 int size
= list
.data
[2].toInt();
162 QTextCursor
cursor( document() );
163 cursor
.movePosition( QTextCursor::NextCharacter
, QTextCursor::MoveAnchor
, start
);
164 cursor
.movePosition( QTextCursor::NextCharacter
, QTextCursor::KeepAnchor
, size
);
165 cursor
.insertText( text
);
168 void QcTextEdit::keyPressEvent( QKeyEvent
*e
)
170 if( _interpretSelection
&& e
->modifiers() & (Qt::ControlModifier
|Qt::ShiftModifier
)
171 && ( e
->key() == Qt::Key_Return
|| e
->key() == Qt::Key_Enter
) )
173 QTextCursor
c(textCursor());
175 if ( !c
.hasSelection() ) {
176 c
.movePosition( QTextCursor::StartOfLine
);
177 c
.movePosition( QTextCursor::EndOfLine
, QTextCursor::KeepAnchor
);
180 if ( c
.hasSelection() ) {
181 QString
selection( c
.selectedText() );
182 Q_EMIT( interpret( prepareText(selection
) ) );
188 QTextEdit::keyPressEvent( e
);
191 void QcTextEdit::insertFromMimeData ( const QMimeData
* data
)
193 if(data
->hasUrls()) {
194 QTextCursor
c( textCursor() );
195 QList
<QUrl
> urls
= data
->urls();
196 int n
= urls
.count();
197 for(int i
= 0; i
< n
; ++i
)
200 QString text
= url
.scheme() == "file" ? url
.toLocalFile() : url
.toString();
202 if(n
> 1) c
.insertText("\n");
206 QTextEdit::insertFromMimeData(data
);
209 QString
& QcTextEdit::prepareText( QString
& text
) const
211 // NOTE: QTextDocument contains unicode paragraph separators U+2029
212 // instead of newline \n characters
213 return text
.replace( QChar( 0x2029 ), QChar( '\n' ) );