sc ide: MultiSplitter - reimplement findChild() to respect split order
[supercollider.git] / editors / sc-ide / widgets / cmd_line.cpp
blob1eda8337df4805b32ed9e420a82296de73e5c3a6
1 /*
2 SuperCollider Qt IDE
3 Copyright (c) 2010-2012 Jakob Leben
4 Copyright (c) 2012 Tim Blechmann
5 http://www.audiosynth.com
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 2 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, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "cmd_line.hpp"
24 #include <QHBoxLayout>
25 #include <QLabel>
26 #include <QKeyEvent>
28 namespace ScIDE {
30 CmdLine::CmdLine( const QString &text, int maxHist ) :
31 curHistory( -1 ),
32 maxHistory( qMax(1,maxHist) )
34 QLabel *lbl = new QLabel(text);
36 expr = new QLineEdit;
37 QFont f( expr->font() );
38 f.setFamily("monospace");
39 f.setStyleHint(QFont::TypeWriter);
40 expr->setFont(f);
42 QHBoxLayout *l = new QHBoxLayout;
43 l->setContentsMargins(0,0,0,0);
44 l->addWidget(lbl);
45 l->addWidget(expr);
46 setLayout( l );
48 expr->installEventFilter( this );
49 setFocusProxy(expr);
52 bool CmdLine::eventFilter( QObject *, QEvent *e )
54 int type = e->type();
55 if( type != QEvent::KeyPress ) return false;
57 QKeyEvent *ke = static_cast<QKeyEvent*>(e);
59 switch( ke->key() )
61 case Qt::Key_Return:
62 case Qt::Key_Enter:
64 if( expr->text().isEmpty() ) return true;
66 emit invoked( expr->text(), false );
67 if( history.count() == 0 || history[0] != expr->text() )
69 if( history.count() >= maxHistory ) history.removeAt( history.count() - 1 );
70 history.prepend( expr->text() );
72 curHistory = -1;
73 expr->clear();
74 return true;
76 case Qt::Key_Up:
77 if( curHistory < history.count() - 1 ) {
78 expr->blockSignals(true);
79 expr->setText( history[++curHistory] );
80 expr->blockSignals(false);
82 return true;
84 case Qt::Key_Down:
85 if( curHistory > -1 ) {
86 --curHistory;
87 expr->blockSignals(true);
88 if( curHistory == -1 ) expr->clear();
89 else expr->setText( history[curHistory] );
90 expr->blockSignals(false);
92 return true;
94 default: return false;
98 } // namespace ScIDE