00001
#include <qwidget.h>
00002
#include <qpopupmenu.h>
00003
#include <qtimer.h>
00004
00005
#include <kdeversion.h>
00006
#include <kdebug.h>
00007
00008
#include <ktexteditor/viewcursorinterface.h>
00009
#include <ktexteditor/popupmenuinterface.h>
00010
#include <ktexteditor/editinterface.h>
00011
#include <ktexteditor/selectioninterface.h>
00012
00013
#include "partcontroller.h"
00014
#include "core.h"
00015
#include "debugger.h"
00016
00017
00018
#include "editorproxy.h"
00019
00020
00021
using namespace KTextEditor;
00022
00023 EditorProxy *
EditorProxy::s_instance = 0;
00024
00025
00026 EditorProxy::EditorProxy()
00027 :
QObject()
00028 {
00029 }
00030
00031
00032 EditorProxy *
EditorProxy::getInstance()
00033 {
00034
if (!
s_instance)
00035
s_instance =
new EditorProxy;
00036
00037
return s_instance;
00038 }
00039
00040
00041 void EditorProxy::setLineNumber(
KParts::Part *part,
int lineNum,
int col)
00042 {
00043
if (!part || !part->inherits(
"KTextEditor::Document"))
00044
return;
00045
00046
if ( lineNum < 0 )
00047
return;
00048
00049
ViewCursorInterface *iface = dynamic_cast<ViewCursorInterface*>(part->
widget());
00050
if (iface)
00051 iface->
setCursorPositionReal(lineNum, col == -1 ? 0 : col);
00052 }
00053
00054
00055 void EditorProxy::installPopup(
KParts::Part *part,
QPopupMenu *popup,
bool revalidate )
00056 {
00057
kdDebug( 9000 ) <<
"EditorProxy::installPopup called with popup = " << popup <<
endl;
00058
00059
if (part->inherits(
"KTextEditor::Document") && part->
widget())
00060 {
00061
PopupMenuInterface *iface = dynamic_cast<PopupMenuInterface*>(part->
widget());
00062
if (iface)
00063 {
00064 iface->
installPopup(popup);
00065
00066
00067
00068
00069
00070
00071
00072 disconnect(popup, SIGNAL(aboutToShow()),
this, 0);
00073
00074 connect(popup, SIGNAL(aboutToShow()),
this, SLOT(
popupAboutToShow()));
00075 }
00076 }
00077
00078
static bool forcerevalidation =
true;
00079
00080
if( forcerevalidation || revalidate ){
00081
00082 forcerevalidation =
false;
00083
00084
00085
m_popupIds.resize(popup->count());
00086
for (uint index=0; index < popup->count(); ++index)
00087
m_popupIds[index] = popup->idAt(index);
00088
00089 }
00090
00091 }
00092
00093
00094 void EditorProxy::popupAboutToShow()
00095 {
00096
QPopupMenu *popup = (
QPopupMenu*)sender();
00097
if (!popup)
00098
return;
00099
00100
00101
for (
int index=popup->count()-1; index >= 0; --index)
00102 {
00103
int id = popup->idAt(index);
00104
if (
m_popupIds.contains(
id) == 0)
00105 {
00106 QMenuItem *item = popup->findItem(
id);
00107
if (item->popup())
00108
delete item->popup();
00109
else
00110 popup->removeItemAt(index);
00111
00112 }
else {
00113
00114 }
00115 }
00116
00117
00118
m_popupIds.resize(popup->count());
00119
for (uint index=0; index < popup->count(); ++index)
00120
m_popupIds[index] = popup->idAt(index);
00121
00122
00123
KParts::ReadOnlyPart *ro_part = dynamic_cast<KParts::ReadOnlyPart*>(
PartController::getInstance()->
activePart());
00124
if (!ro_part)
00125
return;
00126
00127
00128
FileContext context(ro_part->
url().
path(),
false);
00129
Core::getInstance()->
fillContextMenu(popup, &context);
00130
00131
00132
if (!ro_part->
widget())
00133
return;
00134
00135
SelectionInterface *selectIface = dynamic_cast<SelectionInterface*>(ro_part);
00136
ViewCursorInterface *cursorIface = dynamic_cast<ViewCursorInterface*>(ro_part->
widget());
00137
EditInterface *editIface = dynamic_cast<EditInterface*>(ro_part);
00138
00139
QString wordstr, linestr;
00140
bool hasMultilineSelection =
false;
00141
if( selectIface && selectIface->
hasSelection() )
00142 {
00143 hasMultilineSelection = ( selectIface->
selection().contains(
'\n') != 0 );
00144
if ( !hasMultilineSelection )
00145 {
00146 wordstr = selectIface->
selection();
00147 }
00148 }
00149
if( cursorIface && editIface )
00150 {
00151 uint line, col;
00152 line = col = 0;
00153 cursorIface->
cursorPositionReal(&line, &col);
00154 linestr = editIface->
textLine(line);
00155
if( wordstr.isEmpty() && !hasMultilineSelection ) {
00156
int startPos = QMAX(QMIN((
int)col, (
int)linestr.length()-1), 0);
00157
int endPos = startPos;
00158
while (startPos >= 0 && ( linestr[startPos].isLetterOrNumber() || linestr[startPos] ==
'_' ) )
00159 startPos--;
00160
while (endPos < (
int)linestr.length() && ( linestr[endPos].isLetterOrNumber() || linestr[endPos] ==
'_' ) )
00161 endPos++;
00162 wordstr = (startPos==endPos)?
00163
QString() : linestr.mid(startPos+1, endPos-startPos-1);
00164 }
00165
kdDebug(9000) <<
"Word:" << wordstr <<
":" <<
endl;
00166
EditorContext context(ro_part->
url(), line, col, linestr, wordstr);
00167
Core::getInstance()->
fillContextMenu(popup, &context);
00168 }
else {
00169
Core::getInstance()->
fillContextMenu(popup, 0);
00170 }
00171
00172
00173
bool lastWasSeparator =
true;
00174
for( uint i = 0; i < popup->count(); ) {
00175
int id = popup->idAt( i );
00176
if( lastWasSeparator && popup->findItem(
id )->isSeparator() ) {
00177 popup->removeItem(
id );
00178
00179 }
else {
00180 lastWasSeparator =
false;
00181 i++;
00182 }
00183 }
00184
if( lastWasSeparator && popup->count() > 0 )
00185 popup->removeItem( popup->idAt( popup->count() - 1 ) );
00186 }
00187
00188
#include "editorproxy.moc"