Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / editors / sc-ide / widgets / cmd_line.cpp
blobe8ea466bf5220dad2e81ef8fdb97eaf342ddd329
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"
23 #include "main_window.hpp"
24 #include "../core/main.hpp"
25 #include "../core/settings/manager.hpp"
27 #include <QHBoxLayout>
28 #include <QLabel>
29 #include <QKeyEvent>
31 namespace ScIDE {
33 QString CmdLineEdit::symbolUnderCursor()
35 // TODO: select word under cursor
36 return selectedText();
39 bool CmdLineEdit::openDocumentation()
41 return Main::openDocumentation(symbolUnderCursor());
44 void CmdLineEdit::openDefinition()
46 return Main::openDefinition(symbolUnderCursor(), MainWindow::instance());
49 void CmdLineEdit::findReferences()
51 return Main::findReferences(symbolUnderCursor(), MainWindow::instance());
55 CmdLine::CmdLine( const QString &text, int maxHist ) :
56 curHistory( -1 ),
57 maxHistory( qMax(1,maxHist) )
59 QLabel *lbl = new QLabel(text);
61 expr = new CmdLineEdit;
63 QHBoxLayout *l = new QHBoxLayout;
64 l->setContentsMargins(0,0,0,0);
65 l->addWidget(lbl);
66 l->addWidget(expr);
67 setLayout( l );
69 expr->installEventFilter( this );
70 setFocusProxy(expr);
72 applySettings( Main::settings() );
75 void CmdLine::applySettings( Settings::Manager *settings )
77 QFont codeFont = settings->codeFont();
78 expr->setFont( codeFont );
81 bool CmdLine::eventFilter( QObject *, QEvent *e )
83 int type = e->type();
84 if( type != QEvent::KeyPress ) return false;
86 QKeyEvent *ke = static_cast<QKeyEvent*>(e);
88 switch( ke->key() )
90 case Qt::Key_Return:
91 case Qt::Key_Enter:
93 if( expr->text().isEmpty() ) return true;
95 emit invoked( expr->text(), false );
96 if( history.count() == 0 || history[0] != expr->text() )
98 if( history.count() >= maxHistory ) history.removeAt( history.count() - 1 );
99 history.prepend( expr->text() );
101 curHistory = -1;
102 expr->clear();
103 return true;
105 case Qt::Key_Up:
106 if( curHistory < history.count() - 1 ) {
107 expr->blockSignals(true);
108 expr->setText( history[++curHistory] );
109 expr->blockSignals(false);
111 return true;
113 case Qt::Key_Down:
114 if( curHistory > -1 ) {
115 --curHistory;
116 expr->blockSignals(true);
117 if( curHistory == -1 ) expr->clear();
118 else expr->setText( history[curHistory] );
119 expr->blockSignals(false);
121 return true;
123 default: return false;
127 } // namespace ScIDE