]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiCompleter.cpp
* fix spelling in comments to please John.
[lyx.git] / src / frontends / qt4 / GuiCompleter.cpp
index 386f019575fb869a3dd3102473509880dae4e09c..33bc42465137194ed0c4460ffdd00a262064c9d4 100644 (file)
@@ -47,33 +47,13 @@ class CompleterItemDelegate : public QItemDelegate
 {
 public:
        explicit CompleterItemDelegate(QObject * parent)
-               : QItemDelegate(parent), enabled_(false)
+               : QItemDelegate(parent)
        {}
 
        ~CompleterItemDelegate()
        {}
 
-       void setEnabled(bool enabled = true)
-       {
-               enabled_ = enabled;
-       }
-       
 protected:
-       void drawDisplay(QPainter * painter,
-               QStyleOptionViewItem const & option,
-               QRect const & rect, QString const & text) const
-       {
-               if (!enabled_) {
-                       QItemDelegate::drawDisplay(painter, option, rect, text);
-                       return;
-               }
-
-               // FIXME: do this more elegantly
-               docstring stltext = qstring_to_ucs4(text);
-               reverse(stltext.begin(), stltext.end());
-               QItemDelegate::drawDisplay(painter, option, rect, toqstr(stltext));
-       }
-
        void paint(QPainter *painter, const QStyleOptionViewItem &option,
                   const QModelIndex &index) const
        {
@@ -97,9 +77,6 @@ protected:
                drawFocus(painter, opt, option.rect);
                painter->restore();
        }
-
-private:
-       bool enabled_;
 };
 
 class GuiCompletionModel : public QAbstractListModel
@@ -180,7 +157,7 @@ private:
 
 
 GuiCompleter::GuiCompleter(GuiWorkArea * gui, QObject * parent)
-       : QCompleter(parent), gui_(gui), updateLock_(0),
+       : QCompleter(parent), gui_(gui), old_cursor_(0), updateLock_(0),
          inlineVisible_(false), popupVisible_(false),
          modelActive_(false)
 {
@@ -188,6 +165,7 @@ GuiCompleter::GuiCompleter(GuiWorkArea * gui, QObject * parent)
        model_ = new GuiCompletionModel(this, 0);
        setModel(model_);
        setCompletionMode(QCompleter::PopupCompletion);
+       setCaseSensitivity(Qt::CaseInsensitive);
        setWidget(gui_);
        
        // create the popup
@@ -257,6 +235,24 @@ bool GuiCompleter::inlinePossible(Cursor const & cur) const
 }
 
 
+bool GuiCompleter::uniqueCompletionAvailable() const
+{
+       if (!modelActive_)
+               return false;
+
+       size_t n = popup()->model()->rowCount();
+       if (n > 1 || n == 0)
+               return false;
+
+       // if there is exactly one, we have to check whether it is a 
+       // real completion, i.e. longer than the current prefix.
+       if (completionPrefix() == currentCompletion())
+               return false;
+
+       return true;
+}
+
+
 bool GuiCompleter::completionAvailable() const
 {
        if (!modelActive_)
@@ -318,6 +314,11 @@ void GuiCompleter::updateVisibility(Cursor & cur, bool start, bool keep, bool cu
        if (!inlineVisible() && possibleInlineState && start
                && cur.inset().automaticInlineCompletion())
                inline_timer_.start(int(lyxrc.completion_inline_delay * 1000));
+       else {
+               // no inline completion, hence a metrics update is needed
+               if (!(cur.disp_.update() & Update::Force))
+                       cur.updateFlags(cur.disp_.update() | Update::SinglePar);
+       }
 
        // update prefix if any completion is possible
        bool modelActive = modelActive_ && model()->rowCount() > 0;
@@ -478,16 +479,12 @@ void GuiCompleter::updateModel(Cursor & cur, bool popupUpdate, bool inlineUpdate
        bool rtl = false;
        if (cur.inTexted()) {
                Paragraph const & par = cur.paragraph();
-               Font const font =
-               par.getFontSettings(cur.bv().buffer().params(), cur.pos());
+               Font const font =
+                       par.getFontSettings(cur.bv().buffer().params(), cur.pos());
                rtl = font.isVisibleRightToLeft();
        }
        popup()->setLayoutDirection(rtl ? Qt::RightToLeft : Qt::LeftToRight);
 
-       // turn the direction of the strings in the popup.
-       // Qt does not do that itself.
-       itemDelegate_->setEnabled(rtl);
-
        // set new model
        CompletionList const * list = cur.inset().createCompletionList(cur);
        model_->setList(list);
@@ -573,7 +570,7 @@ void GuiCompleter::showInline(Cursor & cur)
 
 void GuiCompleter::hideInline(Cursor & cur)
 {
-       gui_->bufferView().setInlineCompletion(cur, DocIterator(), docstring());
+       gui_->bufferView().setInlineCompletion(cur, DocIterator(cur.buffer()), docstring());
        inlineVisible_ = false;
        
        if (inline_timer_.isActive())
@@ -652,9 +649,9 @@ void GuiCompleter::hideInline()
 void GuiCompleter::activate()
 {
        if (!popupVisible() && !inlineVisible())
-               return;
-
-       popupActivated(currentCompletion());
+               tab();
+       else
+               popupActivated(currentCompletion());
 }
 
 
@@ -665,7 +662,7 @@ void GuiCompleter::tab()
        cur.updateFlags(Update::None);
        
        // check that inline completion is active
-       if (!inlineVisible()) {
+       if (!inlineVisible() && !uniqueCompletionAvailable()) {
                // try to activate the inline completion
                if (cur.inset().inlineCompletionSupported(cur)) {
                        showInline();
@@ -687,6 +684,9 @@ void GuiCompleter::tab()
                return;
        }
        
+       // Make undo possible
+       cur.recordUndo();
+
        // If completion is active, at least complete by one character
        docstring prefix = cur.inset().completionPrefix(cur);
        docstring completion = qstring_to_ucs4(currentCompletion());
@@ -829,7 +829,7 @@ docstring GuiCompleter::longestUniqueCompletion() const
        if (n == 0)
                return docstring();
        QString s = model.data(model.index(0, 0), Qt::EditRole).toString();
-       
+
        if (modelSorting() == QCompleter::UnsortedModel) {
                // For unsorted model we cannot do more than iteration.
                // Iterate through the completions and cut off where s differs
@@ -878,6 +878,8 @@ void GuiCompleter::popupActivated(const QString & completion)
        Cursor cur = gui_->bufferView().cursor();
        cur.updateFlags(Update::None);
        
+       cur.recordUndo();
+
        docstring prefix = cur.inset().completionPrefix(cur);
        docstring postfix = qstring_to_ucs4(completion.mid(prefix.length()));
        cur.inset().insertCompletion(cur, postfix, true);
@@ -907,4 +909,4 @@ void GuiCompleter::popupHighlighted(const QString & completion)
 } // namespace frontend
 } // namespace lyx
 
-#include "GuiCompleter_moc.cpp"
+#include "moc_GuiCompleter.cpp"