2 // File : QAD_PyEditor.cxx
3 // Created : Thu Jun 14 16:04:59 2001
4 // Author : Nicolas REJNERI
7 // Copyright : Open CASCADE
10 #include "QAD_PyEditor.h"
11 #include "QAD_PyInterp.h"
12 #include "QAD_Application.h"
13 #include "QAD_Desktop.h"
14 #include "QAD_Config.h"
15 #include "QAD_Tools.h"
16 #include "QAD_MessageBox.h"
17 //#include "QAD_RightFrame.h"
19 #include <qapplication.h>
21 #include <qclipboard.h>
23 // NRI : Temporary added
25 #include <SALOMEconfig.h>
26 #include CORBA_SERVER_HEADER(SALOMEDS)
27 #include CORBA_SERVER_HEADER(SALOMEDS_Attributes)
31 enum { IdCopy, IdPaste, IdClear, IdSelectAll };
36 QAD_PyEditor::QAD_PyEditor(QAD_PyInterp* interp,
37 QWidget *parent, const char *name)
38 : QMultiLineEdit(parent,name)
40 QString fntSet = QAD_CONFIG->getSetting("Viewer:ConsoleFont");
41 QFont myFont = QAD_Tools::stringToFont( fntSet );
42 // QFont myFont("Courier",11);
45 string banner = _interp->getbanner();
46 setText(banner.c_str());
48 _currentPrompt = ">>> ";
49 // put error messages of interpreter if they exist.
51 setText(_interp->getverr());
52 setText(_currentPrompt);
53 setPalette( QAD_Application::getPalette(true) );
55 connect(this,SIGNAL(returnPressed()),this,SLOT(handleReturn()) );
61 QAD_PyEditor::~QAD_PyEditor()
66 Called to insert a string s
68 void QAD_PyEditor::setText(QString s)
70 // MESSAGE("setText");
71 int line=numLines()-1;
72 int col=lineLength(line);
75 setCursorPosition( n, textLine(n).length());
79 Called when an handleReturn
81 void QAD_PyEditor::handleReturn()
83 QApplication::setOverrideCursor( Qt::waitCursor );
85 int line=numLines()-2;
87 // NRI : Temporary added
88 SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
90 if ( aStudy->GetProperties()->IsLocked() ) {
91 QApplication::restoreOverrideCursor();
92 QAD_MessageBox::warn1 ( (QWidget*)QAD_Application::getDesktop(),
93 QObject::tr("WARNING"),
94 QObject::tr("WRN_STUDY_LOCKED"),
95 QObject::tr("BUT_OK") );
97 _currentPrompt = ">>> ";
98 setText(_currentPrompt);
104 _buf.append(textLine(line).remove(0,SIZEPR));
105 ret = _interp->run(_buf);
109 setText(_interp->getvout());
110 setText(_interp->getverr());
111 _currentPrompt = ">>> ";
112 setText(_currentPrompt);
117 _currentPrompt = "... ";
118 setText(_currentPrompt);
120 _isInHistory = false;
121 QApplication::restoreOverrideCursor();
125 Processes own popup menu
127 void QAD_PyEditor::mousePressEvent (QMouseEvent * event)
129 if ( event->button() == RightButton ) {
130 QPopupMenu *popup = new QPopupMenu( this );
131 QMap<int, int> idMap;
133 int line1, col1, line2, col2;
134 getMarkedRegion(&line1, &col1, &line2, &col2);
135 bool allSelected = getMarkedRegion(&line1, &col1, &line2, &col2) &&
136 line1 == 0 && line2 == numLines()-1 && col1 == 0 && col2 == lineLength(line2);
138 id = popup->insertItem( tr( "EDIT_COPY_CMD" ) );
139 idMap.insert(IdCopy, id);
140 id = popup->insertItem( tr( "EDIT_PASTE_CMD" ) );
141 idMap.insert(IdPaste, id);
142 id = popup->insertItem( tr( "EDIT_CLEAR_CMD" ) );
143 idMap.insert(IdClear, id);
144 popup->insertSeparator();
145 id = popup->insertItem( tr( "EDIT_SELECTALL_CMD" ) );
146 idMap.insert(IdSelectAll, id);
147 popup->setItemEnabled( idMap[ IdCopy ], hasMarkedText() );
148 popup->setItemEnabled( idMap[ IdPaste ],
149 !isReadOnly() && (bool)QApplication::clipboard()->text().length() );
150 popup->setItemEnabled( idMap[ IdSelectAll ],
151 (bool)text().length() && !allSelected );
153 int r = popup->exec( event->globalPos() );
156 if ( r == idMap[ IdCopy ] ) {
159 else if ( r == idMap[ IdPaste ] ) {
162 else if ( r == idMap[ IdClear ] ) {
164 string banner = _interp->getbanner();
165 setText(banner.c_str());
166 setText(_currentPrompt);
168 else if ( r == idMap[ IdSelectAll ] ) {
174 QMultiLineEdit::mousePressEvent(event);
179 Called when a Mouse release event
181 void QAD_PyEditor::mouseReleaseEvent ( QMouseEvent * e )
183 // MESSAGE("mouseReleaseEvent");
184 int curLine, curCol; // for cursor position
185 int endLine, endCol; // for last edited line
186 getCursorPosition(&curLine, &curCol);
187 endLine = numLines() -1;
188 if (e->button() != MidButton)
189 QMultiLineEdit::mouseReleaseEvent(e);
190 else if ((curLine == endLine) && (curCol >= SIZEPR))
191 QMultiLineEdit::mouseReleaseEvent(e);
195 Called when a drop event (Drag & Drop)
197 void QAD_PyEditor::dropEvent (QDropEvent *e)
199 INFOS("dropEvent : not handled");
203 Checks, is the string a command line or not.
206 bool QAD_PyEditor::isCommand( const QString& str) const
208 if (str.find(_currentPrompt)==0)
215 Called when a keyPress event
217 void QAD_PyEditor::keyPressEvent( QKeyEvent *e )
219 int curLine, curCol; // for cursor position
220 int endLine, endCol; // for last edited line
221 getCursorPosition(&curLine, &curCol);
222 endLine = numLines() -1;
223 //MESSAGE("current position " << curLine << ", " << curCol);
224 //MESSAGE("last line " << endLine);
228 if ((aKey >= Key_Space) && (aKey <= Key_ydiaeresis))
233 bool ctrlPressed = ( (e->state() & ControlButton) == ControlButton );
234 bool shftPressed = ( (e->state() & ShiftButton) == ShiftButton );
240 if (curLine <endLine)
242 setCursorPosition(endLine, SIZEPR);
245 QMultiLineEdit::keyPressEvent( e );
251 if (curLine <endLine)
253 setCursorPosition(endLine, SIZEPR);
256 QMultiLineEdit::keyPressEvent( e );
261 // if Cntr+Key_Up event then scroll the commands stack up
263 QString histLine = _currentPrompt;
267 _currentCommand = textLine(endLine).remove(0,SIZEPR);
268 SCRUTE(_currentCommand);
270 QString previousCommand = _interp->getPrevious();
271 if (previousCommand.compare(BEGIN_HISTORY_PY) != 0)
274 histLine.append(previousCommand);
275 insertLine(histLine);
277 endLine = numLines() -1;
278 setCursorPosition(endLine, lineLength(endLine));
280 // if Shift+Key_Up event then move cursor up and select the text
281 else if ( shftPressed && curLine > 0 ){
282 setCursorPosition(curLine-1, curCol, true);
285 else { QMultiLineEdit::keyPressEvent( e ); }
290 // if Cntr+Key_Down event then scroll the commands stack down
292 QString histLine = _currentPrompt;
293 QString nextCommand = _interp->getNext();
294 if (nextCommand.compare(TOP_HISTORY_PY) != 0)
297 histLine.append(nextCommand);
298 insertLine(histLine);
303 _isInHistory = false;
305 histLine.append(_currentCommand);
306 insertLine(histLine);
308 endLine = numLines() -1;
309 setCursorPosition(endLine, lineLength(endLine));
311 // if Shift+Key_Down event then move cursor down and select the text
312 else if ( shftPressed && curLine < endLine ) {
313 setCursorPosition(curLine+1, curCol, true);
316 else { QMultiLineEdit::keyPressEvent( e ); }
322 if (!shftPressed && isCommand(textLine(curLine)) && curCol <= SIZEPR )
324 setCursorPosition((curLine -1), SIZEPR);
327 else QMultiLineEdit::keyPressEvent( e );
332 if (!shftPressed && isCommand(textLine(curLine))
333 && curCol < SIZEPR) setCursorPosition(curLine, SIZEPR-1);
334 QMultiLineEdit::keyPressEvent( e );
339 if (isCommand(textLine(curLine)) && curCol <= SIZEPR)
340 setCursorPosition(curLine, SIZEPR, shftPressed);
341 else setCursorPosition(curLine, 0, shftPressed);
346 setCursorPosition(curLine, textLine(curLine).length(), shftPressed);
351 if ((curLine == endLine) && (curCol > SIZEPR))
352 QMultiLineEdit::keyPressEvent( e );
357 if ((curLine == endLine) && (curCol > SIZEPR-1))
358 QMultiLineEdit::keyPressEvent( e );
362 if ( e->key() == Key_C && ( e->state() & ControlButton ) )
366 _currentPrompt = ">>> ";
367 setText(_currentPrompt);
370 // NRI : DEBUG PAS TERRIBLE //
371 if (( e->key() == Key_F3) ||
372 ( e->key() == Key_F4) ||
373 ( e->key() == Key_Return) ||
374 ( e->key() == Key_Escape))
375 QAD_Application::getDesktop()->onKeyPress( e );