scide: cancel ScRequests when recompiling the class library
[supercollider.git] / editors / sc-ide / widgets / cmd_line.cpp
blob05265f9871a7a9fe516e1849d29c26d7990af542
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 "sc_symbol_references.hpp"
25 #include "../core/main.hpp"
26 #include "../core/settings/manager.hpp"
28 #include <QHBoxLayout>
29 #include <QLabel>
30 #include <QKeyEvent>
32 namespace ScIDE {
34 QString CmdLineEdit::symbolUnderCursor()
36 // TODO: select word under cursor
37 return selectedText();
40 bool CmdLineEdit::openDocumentation()
42 return Main::openDocumentation(symbolUnderCursor());
45 void CmdLineEdit::openDefinition()
47 return Main::openDefinition(symbolUnderCursor(), MainWindow::instance());
50 void CmdLineEdit::lookupReferences()
52 return Main::openReferences(symbolUnderCursor(), MainWindow::instance());
56 CmdLine::CmdLine( const QString &text, int maxHist ) :
57 curHistory( -1 ),
58 maxHistory( qMax(1,maxHist) )
60 QLabel *lbl = new QLabel(text);
62 expr = new CmdLineEdit;
64 QHBoxLayout *l = new QHBoxLayout;
65 l->setContentsMargins(0,0,0,0);
66 l->addWidget(lbl);
67 l->addWidget(expr);
68 setLayout( l );
70 expr->installEventFilter( this );
71 setFocusProxy(expr);
73 applySettings( Main::settings() );
76 void CmdLine::applySettings( Settings::Manager *settings )
78 QFont codeFont = settings->codeFont();
79 expr->setFont( codeFont );
82 bool CmdLine::eventFilter( QObject *, QEvent *e )
84 int type = e->type();
85 if( type != QEvent::KeyPress ) return false;
87 QKeyEvent *ke = static_cast<QKeyEvent*>(e);
89 switch( ke->key() )
91 case Qt::Key_Return:
92 case Qt::Key_Enter:
94 if( expr->text().isEmpty() ) return true;
96 emit invoked( expr->text(), false );
97 if( history.count() == 0 || history[0] != expr->text() )
99 if( history.count() >= maxHistory ) history.removeAt( history.count() - 1 );
100 history.prepend( expr->text() );
102 curHistory = -1;
103 expr->clear();
104 return true;
106 case Qt::Key_Up:
107 if( curHistory < history.count() - 1 ) {
108 expr->blockSignals(true);
109 expr->setText( history[++curHistory] );
110 expr->blockSignals(false);
112 return true;
114 case Qt::Key_Down:
115 if( curHistory > -1 ) {
116 --curHistory;
117 expr->blockSignals(true);
118 if( curHistory == -1 ) expr->clear();
119 else expr->setText( history[curHistory] );
120 expr->blockSignals(false);
122 return true;
124 default: return false;
128 } // namespace ScIDE