Salome HOME
DCQ : Merge with Ecole_ete_a6.
[modules/kernel.git] / src / SALOMEGUI / QAD_Message.cxx
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : QAD_Message.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29 using namespace std;
30 #include "QAD_Message.h"
31 #include "QAD_Application.h"
32 #include "QAD_Config.h"
33 #include "QAD_Tools.h"
34 #include <stdio.h>
35 #include <qstring.h>
36 #include <qpopupmenu.h>
37 #include <qmap.h>
38
39 enum { IdCopy, IdClear, IdSelectAll };
40
41 /*!
42   \class QAD_Message QAD_Message.h
43   \brief Messages window for Study. 
44 */
45
46 /*!
47   Constructor - creates Message Log window
48 */
49 QAD_Message::QAD_Message(QWidget *parent, const char* name)
50   : QMultiLineEdit(parent, name)
51 {
52   // setting default title
53   myTitle = tr("MESSAGE_LOG_NAME");
54   // setting default prompt
55   myPrompt = ">>> ";
56   // setting font ( from preferences )
57   QString fntSet = QAD_CONFIG->getSetting("Viewer:ConsoleFont");
58   QFont myFont = QAD_Tools::stringToFont( fntSet );
59 //  QFont myFont("Courier", 11);
60   setFont(myFont);
61   setReadOnly(TRUE);
62   // appending title
63   setMessage(myTitle);
64   // setting palette (special)
65   setPalette( QAD_Application::getPalette(true) );
66   setFocusPolicy( NoFocus );
67   // currently no text wrap
68   setWordWrap(NoWrap);
69 }
70
71 /*!
72     Destructor
73 */
74 QAD_Message::~QAD_Message()
75 {
76 }
77
78 /*!
79   Adds message
80 */
81 void QAD_Message::setMessage(const QString& s)
82 {
83   int line = numLines() > 0 ? numLines()-1 : 0;
84   int col  = lineLength(line);
85   if (!s.isNull() && s.length() > 0) {
86     QString ss = s;
87     insertAt(ss, line, col, false);
88   }
89   append(myPrompt);
90 }
91
92 /*
93    Processes own popup manu
94 */
95 void QAD_Message::mousePressEvent(QMouseEvent* event)
96 {
97   if ( event->button() == RightButton ) {
98     QPopupMenu *popup = new QPopupMenu( this );
99     QMap<int, int> idMap;
100
101     int line1, col1, line2, col2;
102     getMarkedRegion(&line1, &col1, &line2, &col2);
103     bool allSelected = getMarkedRegion(&line1, &col1, &line2, &col2) &&
104       line1 == 0 && line2 == numLines()-1 && col1 == 0 && col2 == lineLength(line2);
105     int id;
106     id = popup->insertItem( tr( "EDIT_COPY_CMD" ) );
107     idMap.insert(IdCopy, id);
108     id = popup->insertItem( tr( "EDIT_CLEAR_CMD" ) );
109     idMap.insert(IdClear, id);
110     popup->insertSeparator();
111     id = popup->insertItem( tr( "EDIT_SELECTALL_CMD" ) );
112     idMap.insert(IdSelectAll, id);
113     popup->setItemEnabled( idMap[ IdCopy ], hasMarkedText() );
114     popup->setItemEnabled( idMap[ IdSelectAll ], !allSelected );
115     popup->setItemEnabled( idMap[ IdClear ], textLine(1)!=myPrompt );
116         
117     int r = popup->exec( event->globalPos() );
118     delete popup;
119     
120     if ( r == idMap[ IdCopy ] ) {
121       copy();
122     }
123     else if ( r == idMap [ IdClear ] ) {
124       clear();
125       setMessage(myTitle);
126     }
127     else if ( r == idMap[ IdSelectAll ] ) {
128       selectAll();
129     }
130     return;
131   }
132   else {
133     QMultiLineEdit::mousePressEvent(event);
134   }
135 }
136
137 /*
138    Sets prompt ( default is '>>> ' )
139 */
140 void QAD_Message::setPrompt(const QString& prompt) 
141 {
142   myPrompt = prompt;
143   clear();
144   setMessage(myTitle);
145 }