00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
#include "qeditor_arghint.h"
00020
#include "qeditor_view.h"
00021
#include "qeditor_part.h"
00022
00023
#include <qlabel.h>
00024
#include <qintdict.h>
00025
#include <qlayout.h>
00026
#include <qregexp.h>
00027
#include <qapplication.h>
00028
00029
#include <kdebug.h>
00030
00031 struct QEditorArgHintData
00032 {
00033 QEditorView*
editorView;
00034 QIntDict<QLabel> labelDict;
00035 QLayout*
layout;
00036 };
00037
00038
00039
using namespace std;
00040
00041 QEditorArgHint::QEditorArgHint(
QEditorView* parent,
const char* name )
00042 :
QFrame( parent, name, WType_Popup )
00043 {
00044 setBackgroundColor( black );
00045
00046
d =
new QEditorArgHintData();
00047
d->
labelDict.setAutoDelete( TRUE );
00048
d->
layout =
new QVBoxLayout(
this, 1, 2 );
00049
d->
layout->setAutoAdd( TRUE );
00050
d->
editorView = parent;
00051
00052
m_markCurrentFunction =
true;
00053
00054 setFocusPolicy( StrongFocus );
00055 setFocusProxy( parent );
00056
00057
reset( -1, -1 );
00058 }
00059
00060 QEditorArgHint::~QEditorArgHint()
00061 {
00062
delete(
d );
00063
d = 0;
00064 }
00065
00066 void QEditorArgHint::setArgMarkInfos(
const QString& wrapping,
const QString& delimiter )
00067 {
00068
m_wrapping = wrapping;
00069
m_delimiter = delimiter;
00070
m_markCurrentFunction =
true;
00071 }
00072
00073 void QEditorArgHint::reset(
int line,
int col )
00074 {
00075
m_functionMap.clear();
00076
m_currentFunction = -1;
00077
d->
labelDict.clear();
00078
00079
m_currentLine = line;
00080
m_currentCol = col - 1;
00081 }
00082
00083 void QEditorArgHint::slotDone()
00084 {
00085 hide();
00086
00087
m_currentLine =
m_currentCol = -1;
00088
00089 emit
argHintHidden();
00090 }
00091
00092 void QEditorArgHint::cursorPositionChanged(
QEditorView* view,
int line,
int col )
00093 {
00094
if(
m_currentCol == -1 ||
m_currentLine == -1 ){
00095
slotDone();
00096
return;
00097 }
00098
00099
int nCountDelimiter = 0;
00100
int count = 0;
00101
00102
QString currentTextLine = view->
doc()->
textLine( line );
00103
QString text = currentTextLine.mid(
m_currentCol, col -
m_currentCol );
00104
QRegExp strconst_rx(
"\"[^\"]*\"" );
00105
QRegExp chrconst_rx(
"'[^']*'" );
00106
00107
text =
text
00108 .replace( strconst_rx,
"\"\"" )
00109 .replace( chrconst_rx,
"''" );
00110
00111
int index = 0;
00112
while( index < (
int)
text.length() ){
00113
if(
text[index] ==
m_wrapping[0] ){
00114 ++count;
00115 }
else if(
text[index] == m_wrapping[1] ){
00116 --count;
00117 }
else if( count > 0 &&
text[index] ==
m_delimiter[0] ){
00118 ++nCountDelimiter;
00119 }
00120 ++index;
00121 }
00122
00123
if( (
m_currentLine > 0 &&
m_currentLine != line) || (
m_currentLine < col) || (count == 0) ){
00124
slotDone();
00125
return;
00126 }
00127
00128
00129
00130 }
00131
00132 void QEditorArgHint::addFunction(
int id,
const QString& prot )
00133 {
00134
m_functionMap[
id ] = prot;
00135
QLabel* label =
new QLabel( prot.stripWhiteSpace().simplifyWhiteSpace(),
this );
00136 label->setBackgroundColor(
QColor(255, 255, 238) );
00137 label->show();
00138
d->
labelDict.insert(
id, label );
00139
00140
if(
m_currentFunction < 0 )
00141
setCurrentFunction(
id );
00142 }
00143
00144 void QEditorArgHint::setCurrentFunction(
int currentFunction )
00145 {
00146
if(
m_currentFunction != currentFunction ){
00147
00148
if( currentFunction < 0 )
00149 currentFunction = (
int)
m_functionMap.size() - 1;
00150
00151
if( currentFunction > (
int)
m_functionMap.size()-1 )
00152 currentFunction = 0;
00153
00154
if(
m_markCurrentFunction &&
m_currentFunction >= 0 ){
00155
QLabel* label =
d->
labelDict[ m_currentFunction ];
00156 label->setFont( font() );
00157 }
00158
00159 m_currentFunction = currentFunction;
00160
00161
if(
m_markCurrentFunction ){
00162
QLabel* label =
d->
labelDict[ currentFunction ];
00163
QFont fnt( font() );
00164 fnt.setBold( TRUE );
00165 label->setFont( fnt );
00166 }
00167
00168
adjustSize();
00169 }
00170 }
00171
00172 void QEditorArgHint::show()
00173 {
00174 QFrame::show();
00175
adjustSize();
00176 }
00177
00178 bool QEditorArgHint::eventFilter(
QObject*,
QEvent* e )
00179 {
00180
if( isVisible() && e->type() == QEvent::KeyPress ){
00181
QKeyEvent* ke = static_cast<QKeyEvent*>( e );
00182
if( (ke->state() & ControlButton) && ke->key() == Key_Left ){
00183
setCurrentFunction(
currentFunction() - 1 );
00184 ke->accept();
00185
return TRUE;
00186 }
else if( ke->key() == Key_Escape ){
00187
slotDone();
00188
return FALSE;
00189 }
else if( (ke->state() & ControlButton) && ke->key() == Key_Right ){
00190
setCurrentFunction(
currentFunction() + 1 );
00191 ke->accept();
00192
return TRUE;
00193 }
00194 }
00195
00196
return FALSE;
00197 }
00198
00199 void QEditorArgHint::adjustSize( )
00200 {
00201
QRect screen = QApplication::desktop()->screenGeometry(
00202 #
if QT_VERSION >= 0x030100
00203 pos()
00204 #endif
00205 );
00206
00207 QFrame::adjustSize();
00208
if( width() > screen.width() )
00209 resize( screen.width(), height() );
00210
00211
if( x() + width() > screen.width() )
00212 move( screen.width() - width(), y() );
00213 }
00214
00215
#include "qeditor_arghint.moc"