class library: PriorityQueue - implement removeValue, hide array
[supercollider.git] / QtCollider / widgets / QcTextEdit.cpp
blobd2630542453dc648f4ef54a95ce075dc5e8b1f4d
1 /************************************************************************
3 * Copyright 2010 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"
25 #include <QFile>
26 #include <QKeyEvent>
27 #include <QApplication>
29 class QcTextEditFactory : public QcWidgetFactory<QcTextEdit>
31 void initialize( QWidgetProxy *p, QcTextEdit *w, QList<QVariant> & ) {
32 p->setMouseEventWidget( w->viewport() );
36 static QcTextEditFactory factory;
38 QcTextEdit::QcTextEdit() : _interpretSelection(false)
40 connect( this, SIGNAL(interpret(QString)),
41 qApp, SLOT(interpret(QString)),
42 Qt::QueuedConnection );
46 QString QcTextEdit::documentFilename() const
48 return _document;
51 void QcTextEdit::setDocument( const QString &filename )
53 QFile file( filename );
54 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
55 return;
56 QByteArray data = file.readAll();
57 setText( data );
58 _document = filename;
61 int QcTextEdit::selectionStart() const
63 return textCursor().selectionStart();
66 int QcTextEdit::selectionSize() const
68 QTextCursor cursor = textCursor();
69 return cursor.selectionEnd() - cursor.selectionStart();
72 void QcTextEdit::select( int start, int size )
74 if( start < 0 ) start = 0;
76 QTextCursor cursor( document() );
78 cursor.movePosition( QTextCursor::Right, QTextCursor::MoveAnchor, start );
79 cursor.movePosition( size > 0 ? QTextCursor::Right : QTextCursor::Left,
80 QTextCursor::KeepAnchor, qAbs(size) );
82 setTextCursor( cursor );
85 QString QcTextEdit::selectedString() const
87 // NOTE QTextCuror.selectedText() contains unicode paragraph separators U+2029
88 // instead of newline \n characters
89 return textCursor().selectedText().replace( QChar( 0x2029 ), QChar( '\n' ) );
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;
104 format.setFont( f );
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.setPosition( start );
135 cursor.setPosition( start + size, QTextCursor::KeepAnchor );
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;
147 format.setFont( f );
149 QTextCursor cursor( document() );
150 cursor.setPosition( start );
151 cursor.setPosition( start + size, QTextCursor::KeepAnchor );
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.setPosition( start );
164 cursor.setPosition( start + size, QTextCursor::KeepAnchor );
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 QString selection = selectedString();
174 if( !selection.isEmpty() ) {
175 Q_EMIT( interpret( selection ) );
176 return;
180 QTextEdit::keyPressEvent( e );