]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiCompleter.cpp
* completion cursor
[lyx.git] / src / frontends / qt4 / GuiCompleter.cpp
index cd4cae74e24844461193d612dfb95bea02436ba3..1eafa25fa4b6688e72f24837fd7d0fbf45ca2b5a 100644 (file)
@@ -130,9 +130,10 @@ public:
                if (role != Qt::DisplayRole && role != Qt::EditRole)
                    return QVariant();
                    
-               if (index.column() == 0)
-                       return toqstr(list_->data(index.row()));
-               else if (index.column() == 1) {
+               if (index.column() == 0) {
+                       docstring const word = list_->data(index.row());
+                       return toqstr(word);
+               } else if (index.column() == 1) {
                        // get icon from cache
                        QPixmap scaled;
                        QString const name = ":" + toqstr(list_->icon(index.row()));
@@ -233,6 +234,12 @@ bool GuiCompleter::inlinePossible(Cursor const & cur) const
 }
 
 
+bool GuiCompleter::completionAvailable() const
+{
+       return popup()->model()->rowCount() > 0;
+}
+
+
 bool GuiCompleter::popupVisible() const
 {
        return popup()->isVisible();
@@ -378,9 +385,10 @@ void GuiCompleter::updatePopup(Cursor & cur)
                rect = QRect(x, y - dim.ascent() - 3, 200, dim.height() + 6);
        
        // show/update popup
-       complete(rect);
        QTreeView * p = static_cast<QTreeView *>(popup());
        p->setColumnWidth(0, popup()->width() - 22 - p->verticalScrollBar()->width());
+
+       complete(rect);
 }
 
 
@@ -453,6 +461,9 @@ void GuiCompleter::hidePopup(Cursor & cur)
        popup()->hide();
        if (popup_timer_.isActive())
                popup_timer_.stop();
+       
+       if (!inlineVisible())
+               setModel(new GuiCompletionModel(this, 0));
 }
 
 
@@ -469,6 +480,9 @@ void GuiCompleter::hideInline(Cursor & cur)
 {
        gui_->bufferView().setInlineCompletion(cur, DocIterator(), docstring());
        inlineVisible_ = false;
+       
+       if (!popupVisible())
+               setModel(new GuiCompletionModel(this, 0));
 }
 
 
@@ -498,6 +512,32 @@ void GuiCompleter::showInline()
 }
 
 
+void GuiCompleter::hidePopup()
+{
+       Cursor cur = gui_->bufferView().cursor();
+       cur.updateFlags(Update::None);
+       
+       hidePopup(cur);
+       
+       // redraw if needed
+       if (cur.disp_.update())
+               gui_->bufferView().processUpdateFlags(cur.disp_.update());
+}
+
+
+void GuiCompleter::hideInline()
+{
+       Cursor cur = gui_->bufferView().cursor();
+       cur.updateFlags(Update::None);
+       
+       hideInline(cur);
+       
+       // redraw if needed
+       if (cur.disp_.update())
+               gui_->bufferView().processUpdateFlags(cur.disp_.update());
+}
+
+
 void GuiCompleter::activate()
 {
        if (!popupVisible() && !inlineVisible())
@@ -613,10 +653,10 @@ void GuiCompleter::setCurrentCompletion(QString const & s)
                }
        } else {
                // In sorted models, do binary search for s.
-               i = 0;
-               size_t r = n - 1;
-               while (r >= i && i < n) {
-                       size_t mid = (r + i) / 2;
+               int l = 0;
+               int r = n - 1;
+               while (r >= l && l < int(n)) {
+                       size_t mid = (r + l) / 2;
                        QString const & mids
                        = model.data(model.index(mid, 0),
                                     Qt::EditRole).toString();
@@ -626,22 +666,25 @@ void GuiCompleter::setCurrentCompletion(QString const & s)
                        // from the CompletionList has?
                        int c = s.compare(mids, Qt::CaseSensitive);
                        if (c == 0) {
-                               i = mid;
+                               l = mid;
                                break;
-                       } else if (i == r) {
-                               i = n;
+                       } else if (l == r) {
+                               l = n;
                                break;
                        } else if (c > 0)
                                // middle is not far enough
-                               i = mid + 1;
+                               l = mid + 1;
                        else
                                // middle is too far
                                r = mid - 1;
                }
 
                // loop was left without finding anything
-               if (r < i)
+               if (r < l)
                        i = n;
+               else
+                       i = l;
+               BOOST_ASSERT(0 <= i && i <= n);
        }
 
        // select the first if none was found