• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • KDevelop Generic Shell
 

KDevelop Generic Shell

  • src
editorproxy.cpp
1 #include <tqwidget.h>
2 #include <tqpopupmenu.h>
3 #include <tqtimer.h>
4 
5 #include <tdeversion.h>
6 #include <kdebug.h>
7 #include <tdeconfig.h>
8 #include <tdeapplication.h>
9 
10 #include <tdeparts/part.h>
11 
12 #include <tdetexteditor/document.h>
13 #include <tdetexteditor/view.h>
14 #include <tdetexteditor/viewcursorinterface.h>
15 #include <tdetexteditor/popupmenuinterface.h>
16 #include <tdetexteditor/editinterface.h>
17 #include <tdetexteditor/selectioninterface.h>
18 #include <tdetexteditor/view.h>
19 #include <kxmlguiclient.h>
20 #include <kxmlguifactory.h>
21 #include <tdemainwindow.h>
22 #include <tdeactioncollection.h>
23 #include <tdelocale.h>
24 #include <tdestdaccel.h>
25 
26 #include "toplevel.h"
27 #include "partcontroller.h"
28 #include "core.h"
29 #include "multibuffer.h"
30 #include "kdeveditorutil.h"
31 
32 #include "editorproxy.h"
33 
34 using namespace KTextEditor;
35 
36 EditorProxy *EditorProxy::s_instance = 0;
37 
38 
39 EditorProxy::EditorProxy()
40  : TQObject()
41 {
42  m_delayedLineTimer = new TQTimer( this );
43  connect( m_delayedLineTimer, TQT_SIGNAL( timeout() ), this, TQT_SLOT(setLineNumberDelayed()) );
44  TDEConfig *config = kapp->config();
45 
46  m_delayedViewCreationCompatibleUI = true;
47 
48  TDEAction *ac = new TDEAction( i18n("Show Context Menu"), 0, this,
49  TQT_SLOT(showPopup()), TopLevel::getInstance()->main()->actionCollection(), "show_popup" );
50  TDEShortcut cut ;/*= TDEStdAccel::shortcut(TDEStdAccel::PopupMenuContext);*/
51  cut.append(KKey(CTRL+Key_Return));
52  ac->setShortcut(cut);
53 }
54 
55 
56 EditorProxy *EditorProxy::getInstance()
57 {
58  if (!s_instance)
59  s_instance = new EditorProxy;
60 
61  return s_instance;
62 }
63 
64 void EditorProxy::setLineNumberDelayed()
65 {
66  if( !this ) return;
67  setLineNumber(m_delayedPart, m_delayedLine, m_delayedCol);
68 }
69 
70 
71 void EditorProxy::setLineNumber(KParts::Part *part, int lineNum, int col)
72 {
73  if (!part || !part->inherits("KTextEditor::Document"))
74  return;
75 
76  if ( lineNum < 0 )
77  return;
78 
79  KURL url = dynamic_cast<KParts::ReadOnlyPart*>( part )->url();
80  ViewCursorInterface *iface = dynamic_cast<ViewCursorInterface*>(part->widget());
81  if (iface)
82  {
83 #if KDE_IS_VERSION(3,5,5)
84 #else
85  if (!part->widget()->hasFocus()) //workaround for TQXIMInputContext crashes. Keep for KDE <=3.5.4!
86  {
87  m_delayedPart = part;
88  m_delayedLine = lineNum;
89  m_delayedCol = col;
90  m_delayedLineTimer->start( 1, true );
91  } else
92 #endif
93  iface->setCursorPositionReal(lineNum, col == -1 ? 0 : col);
94  }
95  else {
96  // Save the position for a rainy day (or when the view gets activated and wants its position)
97  for (TQValueList<MultiBuffer*>::ConstIterator it = m_editorParts.begin(); it != m_editorParts.end(); ++it)
98  if ((*it)->hasURL( url )) {
99  (*it)->registerDelayedActivation( part, lineNum, col );
100  return;
101  }
102 
103  // Shouldn't hit this?
104  Q_ASSERT(false);
105  }
106 }
107 
108 void EditorProxy::installPopup( KParts::Part * part )
109 {
110 
111  if ( part->inherits("KTextEditor::Document") && part->widget())
112  {
113  PopupMenuInterface *iface = dynamic_cast<PopupMenuInterface*>(part->widget());
114  if (iface)
115  {
116  KTextEditor::View * view = static_cast<KTextEditor::View*>( part->widget() );
117 
118  TQPopupMenu * popup = static_cast<TQPopupMenu*>( part->factory()->container("tdetexteditor_popup", view ) );
119 
120  if (!popup)
121  {
122  kdWarning() << k_funcinfo << "Popup not found!" << endl;
123  return;
124  }
125 
126  TDEAction * action = NULL;
127  //If there is a tab for this file, we don't need to plug the closing menu entries here
128  TDEConfig *config = TDEGlobal::config();
129  config->setGroup("UI");
130  bool m_tabBarShown = ! config->readNumEntry("TabWidgetVisibility", 0);
131  if (!m_tabBarShown)
132  {
133  action = TopLevel::getInstance()->main()->actionCollection()->action( "file_close" );
134  if ( action && !action->isPlugged( popup ) )
135  {
136  popup->insertSeparator( 0 );
137  action->plug( popup, 0 );
138  }
139  action = TopLevel::getInstance()->main()->actionCollection()->action( "file_closeother" );
140  if ( action && !action->isPlugged( popup ) )
141  action->plug( popup, 1 );
142  }
143 
144  iface->installPopup( popup );
145 
146  connect(popup, TQT_SIGNAL(aboutToShow()), this, TQT_SLOT(popupAboutToShow()));
147 
148  // ugly hack: mark the "original" items
149  m_popupIds.resize(popup->count());
150  for (uint index=0; index < popup->count(); ++index)
151  m_popupIds[index] = popup->idAt(index);
152  }
153  }
154 }
155 
156 void EditorProxy::popupAboutToShow()
157 {
158  TQPopupMenu *popup = (TQPopupMenu*)sender();
159  if (!popup)
160  return;
161 
162  // ugly hack: remove all but the "original" items
163  for (int index=popup->count()-1; index >= 0; --index)
164  {
165  int id = popup->idAt(index);
166  if (m_popupIds.contains(id) == 0)
167  {
168  TQMenuItem *item = popup->findItem(id);
169  if ( item && item->popup() )
170  delete item->popup();
171  else
172  popup->removeItemAt(index);
173  }
174  }
175 
176  KTextEditor::Document * doc = dynamic_cast<KTextEditor::Document*>( PartController::getInstance()->activePart() );
177  if (!doc ) return;
178 
179  unsigned int line;
180  unsigned int col;
181  if ( !KDevEditorUtil::currentPositionReal( &line, &col, doc ) ) return;
182 
183  TQString wordstr;
184  TQString selection = KDevEditorUtil::currentSelection( doc );
185  if ( !selection.isEmpty() && selection.contains('\n') != 0 )
186  {
187  wordstr = selection;
188  }
189  else
190  {
191  wordstr = KDevEditorUtil::currentWord( doc );
192  }
193 
194  TQString linestr = KDevEditorUtil::currentLine( doc );
195 
196  EditorContext context( doc->url(), line, col, linestr, wordstr );
197  Core::getInstance()->fillContextMenu( popup, &context );
198 
199  // Remove redundant separators (any that are first, last, or doubled)
200  bool lastWasSeparator = true;
201  for( uint i = 0; i < popup->count(); )
202  {
203  int id = popup->idAt( i );
204  if( lastWasSeparator && popup->findItem( id )->isSeparator() )
205  {
206  popup->removeItem( id );
207  // Since we removed an item, don't increment i
208  } else
209  {
210  lastWasSeparator = false;
211  i++;
212  }
213  }
214  if( lastWasSeparator && popup->count() > 0 )
215  popup->removeItem( popup->idAt( popup->count() - 1 ) );
216 }
217 
218 void EditorProxy::showPopup( )
219 {
220  kdDebug(9000) << k_funcinfo << endl;
221 
222  if ( KParts::Part * part = PartController::getInstance()->activePart() )
223  {
224  ViewCursorInterface *iface = dynamic_cast<ViewCursorInterface*>( part->widget() );
225  if ( iface )
226  {
227  KTextEditor::View * view = static_cast<KTextEditor::View*>( part->widget() );
228  TQPopupMenu * popup = static_cast<TQPopupMenu*>( view->factory()->container("tdetexteditor_popup", view ) );
229 
230  if ( popup )
231  {
232  popup->exec( view->mapToGlobal( iface->cursorCoordinates() ) );
233  }
234  }
235  }
236 }
237 
238 void EditorProxy::registerEditor(MultiBuffer* wrapper)
239 {
240  m_editorParts.append(wrapper);
241 }
242 
243 void EditorProxy::deregisterEditor(MultiBuffer* wrapper)
244 {
245  m_editorParts.remove(wrapper);
246 }
247 
248 TQWidget * EditorProxy::widgetForPart( KParts::Part * part )
249 {
250  if ( !part ) return 0;
251 
252  if (part->widget())
253  return part->widget();
254 
255  KURL url = dynamic_cast<KParts::ReadOnlyPart*>( part )->url();
256 
257  for (TQValueList<MultiBuffer*>::ConstIterator it = m_editorParts.begin(); it != m_editorParts.end(); ++it)
258  if ((*it)->hasURL( url ))
259  return *it;
260 
261  return 0L;
262 }
263 
264 TQWidget * EditorProxy::topWidgetForPart( KParts::Part * part )
265 {
266  if ( !part ) return 0;
267 
268  KURL url = dynamic_cast<KParts::ReadOnlyPart*>( part )->url();
269 
270  for (TQValueList<MultiBuffer*>::ConstIterator it = m_editorParts.begin(); it != m_editorParts.end(); ++it)
271  if ((*it)->hasURL( url ))
272  return *it;
273 
274  if (part->widget())
275  return part->widget();
276 
277  return 0L;
278 }
279 
280 bool EditorProxy::isDelayedViewCapable( )
281 {
282  return m_delayedViewCreationCompatibleUI;
283 }
284 
285 TQWidget *EditorProxy::findPartWidget(KParts::Part *part)
286 {
287  for (TQValueList<MultiBuffer*>::ConstIterator it = m_editorParts.begin(); it != m_editorParts.end(); ++it)
288  if ((*it)->hasPart(part))
289  return *it;
290 
291  if (part->widget())
292  return part->widget();
293 
294  return 0L;
295 }
296 
297 #include "editorproxy.moc"
KTextEditor
Definition: partcontroller.h:23
TopLevel::getInstance
static KDevMainWindow * getInstance()
Get a pointer to the single KDevTopLevel object.
Definition: toplevel.cpp:18

KDevelop Generic Shell

Skip menu "KDevelop Generic Shell"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

KDevelop Generic Shell

Skip menu "KDevelop Generic Shell"
  • buildtools
  •   lib
  •     base
  •     parsers
  •       autotools
  •       qmake
  •     widgets
  •   api
  • languages
  •   lib
  •     debugger
  •     designer_integration
  •     interfaces
  • lib
  •   catalog
  •   interfaces
  •     extensions
  •     external
  •     extras
  •   util
  •   widgets
  •     propeditor
  • parts
  •   documentation
  •     interfaces
  • src
  •   profileengine
  •     lib
Generated for KDevelop Generic Shell by doxygen 1.8.13
This website is maintained by Timothy Pearson.