]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCompleter.cpp
BoxUi.ui: revert unintended commit [a6e42e50/lyxgit]
[lyx.git] / src / frontends / qt4 / GuiCompleter.cpp
1 /**
2  * \file GuiCompleter.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Stefan Schimanski
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GuiCompleter.h"
14
15 #include "Buffer.h"
16 #include "BufferView.h"
17 #include "CompletionList.h"
18 #include "Cursor.h"
19 #include "Dimension.h"
20 #include "GuiWorkArea.h"
21 #include "GuiView.h"
22 #include "LyX.h"
23 #include "LyXRC.h"
24 #include "Paragraph.h"
25 #include "version.h"
26
27 #include "support/lassert.h"
28
29 #include <QApplication>
30 #include <QHeaderView>
31 #include <QKeyEvent>
32 #include <QPainter>
33 #include <QPixmapCache>
34 #include <QScrollBar>
35 #include <QItemDelegate>
36 #include <QTreeView>
37 #include <QTimer>
38
39 using namespace std;
40 using namespace lyx::support;
41
42 namespace lyx {
43 namespace frontend {
44
45 class CompleterItemDelegate : public QItemDelegate
46 {
47 public:
48         explicit CompleterItemDelegate(QObject * parent)
49                 : QItemDelegate(parent)
50         {}
51
52         ~CompleterItemDelegate()
53         {}
54
55 protected:
56         void paint(QPainter *painter, const QStyleOptionViewItem &option,
57                    const QModelIndex &index) const
58         {
59                 if (index.column() == 0) {
60                         QItemDelegate::paint(painter, option, index);
61                         return;
62                 }
63                 QStyleOptionViewItem opt = setOptions(index, option);
64                 QVariant value = index.data(Qt::DisplayRole);
65                 QPixmap pixmap = qvariant_cast<QPixmap>(value);
66                 
67                 // draw
68                 painter->save();
69                 drawBackground(painter, opt, index);
70                 if (!pixmap.isNull()) {
71                         const QSize size = pixmap.size();
72                         painter->drawPixmap(option.rect.left() + (16 - size.width()) / 2,
73                                 option.rect.top() + (option.rect.height() - size.height()) / 2,
74                                 pixmap);
75                 }
76                 drawFocus(painter, opt, option.rect);
77                 painter->restore();
78         }
79 };
80
81 class GuiCompletionModel : public QAbstractListModel
82 {
83 public:
84         ///
85         GuiCompletionModel(QObject * parent, CompletionList const * l)
86                 : QAbstractListModel(parent), list_(l)
87         {}
88         ///
89         ~GuiCompletionModel() { delete list_; }
90         ///
91         void setList(CompletionList const * l) {
92                 delete list_;
93                 list_ = l;
94                 reset();
95         }
96         ///
97         bool sorted() const
98         {
99                 if (list_)
100                         return list_->sorted();
101                 return false;
102         }
103         ///
104         int columnCount(const QModelIndex & /*parent*/ = QModelIndex()) const
105         {
106                 return 2;
107         }
108         ///
109         int rowCount(const QModelIndex & /*parent*/ = QModelIndex()) const
110         {
111                 if (list_ == 0)
112                         return 0;
113                 return list_->size();
114         }
115
116         ///
117         QVariant data(const QModelIndex & index, int role) const
118         {
119                 if (list_ == 0)
120                         return QVariant();
121
122                 if (index.row() < 0 || index.row() >= rowCount())
123                         return QVariant();
124
125                 if (role != Qt::DisplayRole && role != Qt::EditRole)
126                     return QVariant();
127                     
128                 if (index.column() == 0)
129                         return toqstr(list_->data(index.row()));
130
131                 if (index.column() != 1)
132                         return QVariant();
133         
134                 // get icon from cache
135                 QPixmap scaled;
136                 QString const name = ":" + toqstr(list_->icon(index.row()));
137                 if (!QPixmapCache::find("completion" + name, scaled)) {
138                         // load icon from disk
139                         QPixmap p = QPixmap(name);
140                         if (!p.isNull()) {
141                                 // scale it to 16x16 or smaller
142                                 scaled = p.scaled(min(16, p.width()), min(16, p.height()), 
143                                         Qt::KeepAspectRatio, Qt::SmoothTransformation);
144                         }
145                         QPixmapCache::insert("completion" + name, scaled);
146                 }
147                 return scaled;
148         }
149
150 private:
151         /// owned by us
152         CompletionList const * list_;
153 };
154
155
156 GuiCompleter::GuiCompleter(GuiWorkArea * gui, QObject * parent)
157         : QCompleter(parent), gui_(gui), old_cursor_(0), updateLock_(0),
158           inlineVisible_(false), popupVisible_(false),
159           modelActive_(false)
160 {
161         // Setup the completion popup
162         model_ = new GuiCompletionModel(this, 0);
163         setModel(model_);
164         setCompletionMode(QCompleter::PopupCompletion);
165         setCaseSensitivity(Qt::CaseSensitive);
166         setWidget(gui_);
167         
168         // create the popup
169         QTreeView *listView = new QTreeView;
170         listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
171         listView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
172         listView->setSelectionBehavior(QAbstractItemView::SelectRows);
173         listView->setSelectionMode(QAbstractItemView::SingleSelection);
174         listView->header()->hide();
175         listView->setIndentation(0);
176         listView->setUniformRowHeights(true);
177         setPopup(listView);
178         
179         itemDelegate_ = new CompleterItemDelegate(this);
180         popup()->setItemDelegate(itemDelegate_);
181         
182         // create timeout timers
183         popup_timer_.setSingleShot(true);
184         inline_timer_.setSingleShot(true);
185         connect(this, SIGNAL(highlighted(const QString &)),
186                 this, SLOT(popupHighlighted(const QString &)));
187         connect(this, SIGNAL(activated(const QString &)),
188                 this, SLOT(popupActivated(const QString &)));
189         connect(&popup_timer_, SIGNAL(timeout()),
190                 this, SLOT(showPopup()));
191         connect(&inline_timer_, SIGNAL(timeout()),
192                 this, SLOT(showInline()));
193 }
194
195
196 GuiCompleter::~GuiCompleter()
197 {
198         popup()->hide();
199 }
200
201
202 bool GuiCompleter::eventFilter(QObject * watched, QEvent * e)
203 {
204         // hijack back the tab key from the popup
205         // (which stole it from the workspace before)
206         if (e->type() == QEvent::KeyPress && popupVisible()) {
207                 QKeyEvent *ke = static_cast<QKeyEvent *>(e);
208                 switch (ke->key()) {
209                 case Qt::Key_Tab:
210                         tab();
211                         ke->accept();
212                         return true;
213                 default: break;
214                 }
215         }
216         
217         return QCompleter::eventFilter(watched, e);
218 }
219
220
221 bool GuiCompleter::popupPossible(Cursor const & cur) const
222 {
223         return QApplication::activeWindow()
224                 && gui_->hasFocus()
225                 && cur.inset().completionSupported(cur);
226 }
227
228
229 bool GuiCompleter::inlinePossible(Cursor const & cur) const
230 {
231         return cur.inset().inlineCompletionSupported(cur);
232 }
233
234
235 bool GuiCompleter::uniqueCompletionAvailable() const
236 {
237         if (!modelActive_)
238                 return false;
239
240         size_t n = popup()->model()->rowCount();
241         if (n > 1 || n == 0)
242                 return false;
243
244         // if there is exactly one, we have to check whether it is a 
245         // real completion, i.e. longer than the current prefix.
246         if (completionPrefix() == currentCompletion())
247                 return false;
248
249         return true;
250 }
251
252
253 bool GuiCompleter::completionAvailable() const
254 {
255         if (!modelActive_)
256                 return false;
257
258         size_t n = popup()->model()->rowCount();
259
260         // if there is exactly one, we have to check whether it is a 
261         // real completion, i.e. longer than the current prefix.
262         if (n == 1 && completionPrefix() == currentCompletion())
263             return false;
264
265         return n > 0;
266 }
267
268
269 bool GuiCompleter::popupVisible() const
270 {
271         return popupVisible_;
272 }
273
274
275 bool GuiCompleter::inlineVisible() const
276 {
277         // In fact using BufferView::inlineCompletionPos.empty() should be
278         // here. But unfortunately this information is not good enough
279         // because destructive operations like backspace might invalidate
280         // inlineCompletionPos. But then the completion should stay visible
281         // (i.e. reshown on the next update). Hence be keep this information
282         // in the inlineVisible_ variable.
283         return inlineVisible_;
284 }
285
286
287 void GuiCompleter::updateVisibility(Cursor & cur, bool start, bool keep)
288 {
289         // parameters which affect the completion
290         bool moved = cur != old_cursor_;
291         if (moved)
292                 old_cursor_ = cur;
293
294         bool const possiblePopupState = popupPossible(cur);
295         bool const possibleInlineState = inlinePossible(cur);
296
297         // we moved or popup state is not ok for popup?
298         if ((moved && !keep) || !possiblePopupState)
299                 hidePopup();
300
301         // we moved or inline state is not ok for inline completion?
302         if ((moved && !keep) || !possibleInlineState)
303                 hideInline(cur);
304
305         // we inserted something and are in a possible popup state?
306         if (!popupVisible() && possiblePopupState && start
307                 && cur.inset().automaticPopupCompletion())
308                 popup_timer_.start(int(lyxrc.completion_popup_delay * 1000));
309
310         // we inserted something and are in a possible inline completion state?
311         if (!inlineVisible() && possibleInlineState && start
312                 && cur.inset().automaticInlineCompletion())
313                 inline_timer_.start(int(lyxrc.completion_inline_delay * 1000));
314         else if (cur.inMathed() && !lyxrc.completion_inline_math) {
315                 // no inline completion, hence a metrics update is needed
316                 if (!(cur.result().screenUpdate() & Update::Force))
317                         cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
318         }
319
320         // update prefix if any completion is possible
321         bool modelActive = modelActive_ && model()->rowCount() > 0;
322         if (possiblePopupState || possibleInlineState) {
323                 if (modelActive)
324                         updatePrefix(cur);
325                 else
326                         updateAvailability();
327         }
328 }
329
330
331 void GuiCompleter::updateVisibility(bool start, bool keep)
332 {
333         Cursor cur = gui_->bufferView().cursor();
334         cur.screenUpdateFlags(Update::None);
335         
336         updateVisibility(cur, start, keep);
337         
338         if (cur.result().screenUpdate())
339                 gui_->bufferView().processUpdateFlags(cur.result().screenUpdate());
340 }
341
342
343 void GuiCompleter::updatePrefix(Cursor const & cur)
344 {
345         // get new prefix. Do nothing if unchanged
346         QString newPrefix = toqstr(cur.inset().completionPrefix(cur));
347         if (newPrefix == completionPrefix())
348                 return;
349         
350         // value which should be kept selected
351         QString old = currentCompletion();
352         if (old.length() == 0)
353                 old = last_selection_;
354         
355         // update completer to new prefix
356         setCompletionPrefix(newPrefix);
357
358         // update popup because its size might have changed
359         if (popupVisible())
360                 updatePopup(cur);
361
362         // restore old selection
363         setCurrentCompletion(old);
364         
365         // if popup is not empty, the new selection will
366         // be our last valid one
367         QString const & s = currentCompletion();
368         if (popupVisible() || inlineVisible()) {
369                 if (s.length() > 0)
370                         last_selection_ = s;
371                 else
372                         last_selection_ = old;
373         }
374
375         // update inline completion because the default
376         // completion string might have changed
377         if (inlineVisible())
378                 updateInline(cur, s);
379 }
380
381
382 void GuiCompleter::updateInline(Cursor const & cur, QString const & completion)
383 {
384         if (!cur.inset().inlineCompletionSupported(cur))
385                 return;
386         
387         // compute postfix
388         docstring prefix = cur.inset().completionPrefix(cur);
389         docstring postfix = qstring_to_ucs4(completion.mid(prefix.length()));
390         
391         // shorten it if necessary
392         if (lyxrc.completion_inline_dots != -1
393             && postfix.size() > unsigned(lyxrc.completion_inline_dots))
394                 postfix = postfix.substr(0, lyxrc.completion_inline_dots - 1) + "...";
395
396         // set inline completion at cursor position
397         size_t uniqueTo = max(longestUniqueCompletion().size(), prefix.size());
398         gui_->bufferView().setInlineCompletion(cur, cur, postfix, uniqueTo - prefix.size());
399         inlineVisible_ = true;
400 }
401
402
403 void GuiCompleter::updatePopup(Cursor const & cur)
404 {
405         if (!cur.inset().completionSupported(cur))
406                 return;
407         
408         popupVisible_ = true;
409
410         if (completionCount() == 0) {
411                 QTimer::singleShot(0, popup(), SLOT(hide()));
412                 return;
413         }
414
415         QTimer::singleShot(0, this, SLOT(asyncUpdatePopup()));
416 }
417
418
419 void GuiCompleter::asyncUpdatePopup()
420 {
421         Cursor cur = gui_->bufferView().cursor();
422         if (!cur.inset().completionSupported(cur)
423                   || !cur.bv().paragraphVisible(cur)) {
424                 popupVisible_ = false;
425                 return;
426         }
427
428         // get dimensions of completion prefix
429         Dimension dim;
430         int x;
431         int y;
432         cur.inset().completionPosAndDim(cur, x, y, dim);
433         
434         // and calculate the rect of the popup
435         QRect rect;
436         if (popup()->layoutDirection() == Qt::RightToLeft)
437                 rect = QRect(x + dim.width() - 200, y - dim.ascent() - 3, 200, dim.height() + 6);
438         else
439                 rect = QRect(x, y - dim.ascent() - 3, 200, dim.height() + 6);
440         
441         // Resize the columns in the popup.
442         // This should really be in the constructor. But somehow the treeview
443         // has a bad memory about it and we have to tell him again and again.
444         QTreeView * listView = static_cast<QTreeView *>(popup());
445         listView->header()->setStretchLastSection(false);
446         listView->header()->setResizeMode(0, QHeaderView::Stretch);
447         listView->header()->setResizeMode(1, QHeaderView::Fixed);
448         listView->header()->resizeSection(1, 22);
449         
450         // show/update popup
451         complete(rect);
452 }
453
454
455 void GuiCompleter::updateAvailability()
456 {
457         // this should really only be of interest if no completion is
458         // visible yet, i.e. especially if automatic completion is disabled.
459         if (inlineVisible() || popupVisible())
460                 return;
461         Cursor const & cur = gui_->bufferView().cursor();
462         if (!popupPossible(cur) && !inlinePossible(cur))
463                 return;
464         
465         updateModel(cur, false, false);
466 }
467         
468
469 void GuiCompleter::updateModel(Cursor const & cur, bool popupUpdate, bool inlineUpdate)
470 {
471         // value which should be kept selected
472         QString old = currentCompletion();
473         if (old.length() == 0)
474                 old = last_selection_;
475
476         // set whether rtl
477         bool rtl = false;
478         if (cur.inTexted()) {
479                 Paragraph const & par = cur.paragraph();
480                 Font const & font =
481                         par.getFontSettings(cur.bv().buffer().params(), cur.pos());
482                 rtl = font.isVisibleRightToLeft();
483         }
484         popup()->setLayoutDirection(rtl ? Qt::RightToLeft : Qt::LeftToRight);
485
486         // set new model
487         CompletionList const * list = cur.inset().createCompletionList(cur);
488         model_->setList(list);
489         modelActive_ = true;
490         if (list->sorted())
491                 setModelSorting(QCompleter::CaseSensitivelySortedModel);
492         else
493                 setModelSorting(QCompleter::UnsortedModel);
494
495         // set prefix
496         QString newPrefix = toqstr(cur.inset().completionPrefix(cur));
497         if (newPrefix != completionPrefix())
498                 setCompletionPrefix(newPrefix);
499
500         // show popup
501         if (popupUpdate)
502                 updatePopup(cur);
503
504         // restore old selection
505         setCurrentCompletion(old);
506         
507         // if popup is not empty, the new selection will
508         // be our last valid one
509         if (popupVisible() || inlineVisible()) {
510                 QString const & s = currentCompletion();
511                 if (s.length() > 0)
512                         last_selection_ = s;
513                 else
514                         last_selection_ = old;
515         }
516
517         // show inline completion
518         if (inlineUpdate)
519                 updateInline(cur, currentCompletion());
520 }
521
522
523 void GuiCompleter::showPopup(Cursor const & cur)
524 {
525         if (!popupPossible(cur))
526                 return;
527         
528         updateModel(cur, true, inlineVisible());
529 }
530
531
532 void GuiCompleter::asyncHidePopup()
533 {
534         popup()->hide();
535         if (!inlineVisible())
536                 model_->setList(0);
537 }
538
539
540 void GuiCompleter::showInline(Cursor const & cur)
541 {
542         if (!inlinePossible(cur))
543                 return;
544         
545         updateModel(cur, popupVisible(), true);
546 }
547
548
549 void GuiCompleter::hideInline(Cursor const & cur)
550 {
551         gui_->bufferView().setInlineCompletion(cur, DocIterator(cur.buffer()), docstring());
552         inlineVisible_ = false;
553         
554         if (inline_timer_.isActive())
555                 inline_timer_.stop();
556         
557         // Trigger asynchronous part of hideInline. We might be
558         // in a dispatcher here and the setModel call might
559         // trigger focus events which is are not healthy here.
560         QTimer::singleShot(0, this, SLOT(asyncHideInline()));
561
562         // mark that the asynchronous part will reset the model
563         if (!popupVisible())
564                 modelActive_ = false;
565 }
566
567
568 void GuiCompleter::asyncHideInline()
569 {
570         if (!popupVisible())
571                 model_->setList(0);
572 }
573
574
575 void GuiCompleter::showPopup()
576 {
577         Cursor cur = gui_->bufferView().cursor();
578         cur.screenUpdateFlags(Update::None);
579         
580         showPopup(cur);
581
582         // redraw if needed
583         if (cur.result().screenUpdate())
584                 gui_->bufferView().processUpdateFlags(cur.result().screenUpdate());
585 }
586
587
588 void GuiCompleter::showInline()
589 {
590         Cursor cur = gui_->bufferView().cursor();
591         cur.screenUpdateFlags(Update::None);
592         
593         showInline(cur);
594
595         // redraw if needed
596         if (cur.result().screenUpdate())
597                 gui_->bufferView().processUpdateFlags(cur.result().screenUpdate());
598 }
599
600
601 void GuiCompleter::hidePopup()
602 {
603         popupVisible_ = false;
604
605         if (popup_timer_.isActive())
606                 popup_timer_.stop();
607
608         // hide popup asynchronously because we might be here inside of
609         // LFUN dispatchers. Hiding a popup can trigger a focus event on the
610         // workarea which then redisplays the cursor. But the metrics are not
611         // yet up to date such that the coord cache has not all insets yet. The
612         // cursorPos methods would triggers asserts in the coord cache then.
613         QTimer::singleShot(0, this, SLOT(asyncHidePopup()));
614         
615         // mark that the asynchronous part will reset the model
616         if (!inlineVisible())
617                 modelActive_ = false;
618 }
619
620
621 void GuiCompleter::hideInline()
622 {
623         Cursor cur = gui_->bufferView().cursor();
624         cur.screenUpdateFlags(Update::None);
625         
626         hideInline(cur);
627         
628         // redraw if needed
629         if (cur.result().screenUpdate())
630                 gui_->bufferView().processUpdateFlags(cur.result().screenUpdate());
631 }
632
633
634 void GuiCompleter::activate()
635 {
636         if (!popupVisible() && !inlineVisible())
637                 tab();
638         else
639                 popupActivated(currentCompletion());
640 }
641
642
643 void GuiCompleter::tab()
644 {
645         BufferView * bv = &gui_->bufferView();
646         Cursor cur = bv->cursor();
647         cur.screenUpdateFlags(Update::None);
648         
649         // check that inline completion is active
650         if (!inlineVisible() && !uniqueCompletionAvailable()) {
651                 // try to activate the inline completion
652                 if (cur.inset().inlineCompletionSupported(cur)) {
653                         showInline();
654                         
655                         // show popup without delay because the completion was not unique
656                         if (lyxrc.completion_popup_after_complete
657                             && !popupVisible()
658                             && popup()->model()->rowCount() > 1)
659                                 popup_timer_.start(0);
660
661                         return;
662                 }
663                 // or try popup
664                 if (!popupVisible() && cur.inset().completionSupported(cur)) {
665                         showPopup();
666                         return;
667                 }
668                 
669                 return;
670         }
671         
672         // Make undo possible
673         cur.beginUndoGroup();
674         cur.recordUndo();
675
676         // If completion is active, at least complete by one character
677         docstring prefix = cur.inset().completionPrefix(cur);
678         docstring completion = qstring_to_ucs4(currentCompletion());
679         if (completion.size() <= prefix.size()) {
680                 // finalize completion
681                 cur.inset().insertCompletion(cur, docstring(), true);
682                 
683                 // hide popup and inline completion
684                 hidePopup();
685                 hideInline(cur);
686                 updateVisibility(false, false);
687                 cur.endUndoGroup();
688                 return;
689         }
690         docstring nextchar = completion.substr(prefix.size(), 1);
691         if (!cur.inset().insertCompletion(cur, nextchar, false)) {
692                 cur.endUndoGroup();
693                 return;
694         }
695         updatePrefix(cur);
696
697         // try to complete as far as it is unique
698         docstring longestCompletion = longestUniqueCompletion();
699         prefix = cur.inset().completionPrefix(cur);
700         docstring postfix = longestCompletion.substr(min(longestCompletion.size(), prefix.size()));
701         cur.inset().insertCompletion(cur, postfix, false);
702         old_cursor_ = bv->cursor();
703         updatePrefix(cur);
704
705         // show popup without delay because the completion was not unique
706         if (lyxrc.completion_popup_after_complete
707             && !popupVisible()
708             && popup()->model()->rowCount() > 1)
709                 popup_timer_.start(0);
710
711         // redraw if needed
712         if (cur.result().screenUpdate())
713                 gui_->bufferView().processUpdateFlags(cur.result().screenUpdate());
714         cur.endUndoGroup();
715 }
716
717
718 QString GuiCompleter::currentCompletion() const
719 {
720         if (!popup()->selectionModel()->hasSelection())
721                 return QString();
722
723         // Not sure if this is bug in Qt: currentIndex() always 
724         // return the first element in the list.
725         QModelIndex idx = popup()->currentIndex();
726         return popup()->model()->data(idx, Qt::EditRole).toString();
727 }
728
729
730 void GuiCompleter::setCurrentCompletion(QString const & s)
731 {       
732         QAbstractItemModel const & model = *popup()->model();
733         size_t n = model.rowCount();
734         if (n == 0)
735                 return;
736
737         // select the first if s is empty
738         if (s.length() == 0) {
739                 updateLock_++;
740                 popup()->setCurrentIndex(model.index(0, 0));
741                 updateLock_--;
742                 return;
743         }
744
745         // find old selection in model
746         size_t i;
747         if (modelSorting() == QCompleter::UnsortedModel) {
748                 // In unsorted models, iterate through list until the s is found
749                 for (i = 0; i < n; ++i) {
750                         QString const & is
751                         = model.data(model.index(i, 0), Qt::EditRole).toString();
752                         if (is == s)
753                                 break;
754                 }
755         } else {
756                 // In sorted models, do binary search for s.
757                 int l = 0;
758                 int r = n - 1;
759                 while (r >= l && l < int(n)) {
760                         size_t mid = (r + l) / 2;
761                         QString const & mids
762                         = model.data(model.index(mid, 0),
763                                      Qt::EditRole).toString();
764
765                         // left or right?
766                         // FIXME: is this really the same order that the docstring
767                         // from the CompletionList has?
768                         int c = s.compare(mids, Qt::CaseSensitive);
769                         if (c == 0) {
770                                 l = mid;
771                                 break;
772                         } else if (l == r) {
773                                 l = n;
774                                 break;
775                         } else if (c > 0)
776                                 // middle is not far enough
777                                 l = mid + 1;
778                         else
779                                 // middle is too far
780                                 r = mid - 1;
781                 }
782
783                 // loop was left without finding anything
784                 if (r < l)
785                         i = n;
786                 else
787                         i = l;
788                 LASSERT(i <= n, /**/);
789         }
790
791         // select the first if none was found
792         if (i == n)
793                 i = 0;
794
795         updateLock_++;
796         popup()->setCurrentIndex(model.index(i, 0));
797         updateLock_--;
798 }
799
800
801 size_t commonPrefix(QString const & s1, QString const & s2)
802 {
803         // find common prefix
804         size_t j;
805         size_t n1 = s1.length();
806         size_t n2 = s2.length();
807         for (j = 0; j < n1 && j < n2; ++j) {
808                 if (s1.at(j) != s2.at(j))
809                         break;
810         }
811         return j;
812 }
813
814
815 docstring GuiCompleter::longestUniqueCompletion() const
816 {
817         QAbstractItemModel const & model = *popup()->model();
818         size_t n = model.rowCount();
819         if (n == 0)
820                 return docstring();
821         QString s = model.data(model.index(0, 0), Qt::EditRole).toString();
822
823         if (modelSorting() == QCompleter::UnsortedModel) {
824                 // For unsorted model we cannot do more than iteration.
825                 // Iterate through the completions and cut off where s differs
826                 for (size_t i = 0; i < n && s.length() > 0; ++i) {
827                         QString const & is
828                         = model.data(model.index(i, 0), Qt::EditRole).toString();
829
830                         s = s.left(commonPrefix(is, s));
831                 }
832         } else {
833                 // For sorted models we can do binary search multiple times,
834                 // each time to find the first string which has s not as prefix.
835                 size_t i = 0;
836                 while (i < n && s.length() > 0) {
837                         // find first string that does not have s as prefix
838                         // via binary search in [i,n-1]
839                         size_t r = n - 1;
840                         do {
841                                 // get common prefix with the middle string
842                                 size_t mid = (r + i) / 2;
843                                 QString const & mids
844                                 = model.data(model.index(mid, 0), 
845                                         Qt::EditRole).toString();
846                                 size_t oldLen = s.length();
847                                 size_t len = commonPrefix(mids, s);
848                                 s = s.left(len);
849
850                                 // left or right?
851                                 if (oldLen == len) {
852                                         // middle is not far enough
853                                         i = mid + 1;
854                                 } else {
855                                         // middle is maybe too far
856                                         r = mid;
857                                 }
858                         } while (r - i > 0 && i < n);
859                 }
860         }
861
862         return qstring_to_ucs4(s);
863 }
864
865
866 void GuiCompleter::popupActivated(const QString & completion)
867 {
868         Cursor cur = gui_->bufferView().cursor();
869         cur.screenUpdateFlags(Update::None);
870
871         cur.beginUndoGroup();
872         cur.recordUndo();
873
874         docstring prefix = cur.inset().completionPrefix(cur);
875         docstring postfix = qstring_to_ucs4(completion.mid(prefix.length()));
876         cur.inset().insertCompletion(cur, postfix, true);
877         hidePopup();
878         hideInline(cur);
879         
880         if (cur.result().screenUpdate())
881                 gui_->bufferView().processUpdateFlags(cur.result().screenUpdate());
882         cur.endUndoGroup();
883 }
884
885
886 void GuiCompleter::popupHighlighted(const QString & completion)
887 {
888         if (updateLock_ > 0)
889                 return;
890
891         Cursor cur = gui_->bufferView().cursor();
892         cur.screenUpdateFlags(Update::None);
893         
894         if (inlineVisible())
895                 updateInline(cur, completion);
896         
897         if (cur.result().screenUpdate())
898                 gui_->bufferView().processUpdateFlags(cur.result().screenUpdate());
899 }
900
901 } // namespace frontend
902 } // namespace lyx
903
904 #include "moc_GuiCompleter.cpp"