diff --git a/src/kcompletion_p.h b/src/kcompletion_p.h index 639b026..04b01eb 100644 --- a/src/kcompletion_p.h +++ b/src/kcompletion_p.h @@ -1,337 +1,337 @@ /* This file is part of the KDE libraries Copyright (C) 1999 Carsten Pfeiffer This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KCOMPLETION_PRIVATE_H #define KCOMPLETION_PRIVATE_H #include "kcompletion.h" #include #include class KCompTreeNode; /** * @internal */ class KCOMPLETION_EXPORT KCompTreeNodeList { public: KCompTreeNodeList() : m_first(nullptr), m_last(nullptr), m_count(0) {} KCompTreeNode *begin() const { return m_first; } KCompTreeNode *end() const { return m_last; } KCompTreeNode *at(uint index) const; void append(KCompTreeNode *item); void prepend(KCompTreeNode *item); void insert(KCompTreeNode *after, KCompTreeNode *item); KCompTreeNode *remove(KCompTreeNode *item); uint count() const { return m_count; } private: KCompTreeNode *m_first; KCompTreeNode *m_last; uint m_count; }; typedef KCompTreeNodeList KCompTreeChildren; /** * A helper class for KCompletion. Implements a tree of QChar. * * The tree looks like this (containing the items "kde", "kde-ui", * "kde-core" and "pfeiffer". Every item is delimited with QChar( 0x0 ) * * some_root_node * / \ * k p * | | * d f * | | * e e * /| | * 0x0 - i * / \ | * u c f * | | | * i o f * | | | * 0x0 r e * | | * e r * | | * 0x0 0x0 * * @author Carsten Pfeiffer * @internal */ class KCOMPLETION_EXPORT KCompTreeNode : public QChar { public: KCompTreeNode() : QChar(), m_next(nullptr), m_weight(0) {} explicit KCompTreeNode(const QChar &ch, uint weight = 0) : QChar(ch), m_next(nullptr), m_weight(weight) {} ~KCompTreeNode(); void *operator new(size_t s) { Q_ASSERT(m_alloc); return m_alloc->allocate(s); } void operator delete(void *s) { Q_ASSERT(m_alloc); m_alloc->deallocate(s); } // Returns a child of this node matching ch, if available. // Otherwise, returns 0L inline KCompTreeNode *find(const QChar &ch) const { KCompTreeNode *cur = m_children.begin(); while (cur && (*cur != ch)) { cur = cur->m_next; } return cur; } KCompTreeNode *insert(const QChar &, bool sorted); void remove(const QString &); inline int childrenCount() const { return m_children.count(); } inline void confirm() { m_weight++; } inline void confirm(uint w) { m_weight += w; } inline void decline() { m_weight--; } inline uint weight() const { return m_weight; } inline const KCompTreeChildren *children() const { return &m_children; } inline const KCompTreeNode *childAt(int index) const { return m_children.at(index); } inline const KCompTreeNode *firstChild() const { return m_children.begin(); } inline const KCompTreeNode *lastChild() const { return m_children.end(); } /* We want to handle a list of KCompTreeNodes on our own, to not need to use QValueList<>. And to make it even faster we don't use an accessor, but just a public member. */ KCompTreeNode *m_next; /** * Custom allocator used for all KCompTreeNode instances */ static QSharedPointer allocator() { return m_alloc; } private: uint m_weight; KCompTreeNodeList m_children; static QSharedPointer m_alloc; }; /** * @internal */ class KCOMPLETION_EXPORT KCompletionMatchesWrapper { public: KCompletionMatchesWrapper(KCompletion::CompOrder compOrder = KCompletion::Insertion) : m_sortedList(compOrder == KCompletion::Weighted ? new KCompletionMatchesList : nullptr), m_dirty(false), m_compOrder(compOrder) {} ~KCompletionMatchesWrapper() { delete m_sortedList; } void setSorting(KCompletion::CompOrder compOrder) { if (compOrder == KCompletion::Weighted && !m_sortedList) { m_sortedList = new KCompletionMatchesList; } else if (compOrder != KCompletion::Weighted) { delete m_sortedList; m_sortedList = nullptr; } m_compOrder = compOrder; m_stringList.clear(); m_dirty = false; } KCompletion::CompOrder sorting() const { return m_compOrder; } void append(int i, const QString &string) { if (m_sortedList) { m_sortedList->insert(i, string); } else { m_stringList.append(string); } m_dirty = true; } void clear() { if (m_sortedList) { m_sortedList->clear(); } m_stringList.clear(); m_dirty = false; } uint count() const { if (m_sortedList) { return m_sortedList->count(); } return m_stringList.count(); } bool isEmpty() const { return count() == 0; } QString first() const { - return list().first(); + return list().constFirst(); } QString last() const { - return list().last(); + return list().constLast(); } QStringList list() const; void findAllCompletions(const KCompTreeNode *, const QString &, bool ignoreCase, bool &hasMultipleMatches); void extractStringsFromNode(const KCompTreeNode *, const QString &beginning, bool addWeight = false); void extractStringsFromNodeCI(const KCompTreeNode *, const QString &beginning, const QString &restString); mutable QStringList m_stringList; KCompletionMatchesList *m_sortedList; mutable bool m_dirty; KCompletion::CompOrder m_compOrder; }; class KCompletionPrivate { public: KCompletionPrivate(KCompletion *parent) : q_ptr(parent) {} void init(); ~KCompletionPrivate() { delete treeRoot; } void addWeightedItem(const QString &); QString findCompletion(const QString &string); // list used for nextMatch() and previousMatch() KCompletionMatchesWrapper matches; KCompletion::CompletionMode completionMode; QSharedPointer treeNodeAllocator; QString lastString; QString lastMatch; QString currentMatch; KCompTreeNode *treeRoot; KCompletion * const q_ptr; int rotationIndex; // TODO: Change hasMultipleMatches to bitfield after moving findAllCompletions() // to KCompletionMatchesPrivate KCompletion::CompOrder order : 3; bool hasMultipleMatches; bool beep : 1; bool ignoreCase : 1; Q_DECLARE_PUBLIC(KCompletion) }; #endif // KCOMPLETION_PRIVATE_H diff --git a/src/khistorycombobox.cpp b/src/khistorycombobox.cpp index 2d52272..91efa36 100644 --- a/src/khistorycombobox.cpp +++ b/src/khistorycombobox.cpp @@ -1,488 +1,489 @@ /* This file is part of the KDE libraries Copyright (c) 2000,2001 Dawit Alemayehu Copyright (c) 2000,2001 Carsten Pfeiffer Copyright (c) 2000 Stefan Schimanski <1Stein@gmx.de> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License (LGPL) as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "khistorycombobox.h" #include #include #include #include #include #include #include class KHistoryComboBoxPrivate { public: KHistoryComboBoxPrivate(KHistoryComboBox *parent): q_ptr(parent) {} void init(bool useCompletion); void rotateUp(); void rotateDown(); /** * Called from the popupmenu, * calls clearHistory() and emits cleared() */ void _k_clear(); /** * Appends our own context menu entry. */ void _k_addContextMenuItems(QMenu *); /** * Used to emit the activated(QString) signal when enter is pressed */ void _k_simulateActivated(const QString &); /** * The text typed before Up or Down was pressed. */ QString typedText; KPixmapProvider *pixmapProvider = nullptr; KHistoryComboBox * const q_ptr; /** * The current index in the combobox, used for Up and Down */ int currentIndex; /** * Indicates that the user at least once rotated Up through the entire list * Needed to allow going back after rotation. */ bool rotated = false; Q_DECLARE_PUBLIC(KHistoryComboBox) }; void KHistoryComboBoxPrivate::init(bool useCompletion) { Q_Q(KHistoryComboBox); // Set a default history size to something reasonable, Qt sets it to INT_MAX by default q->setMaxCount(50); if (useCompletion) { q->completionObject()->setOrder(KCompletion::Weighted); } q->setInsertPolicy(KHistoryComboBox::NoInsert); currentIndex = -1; rotated = false; pixmapProvider = nullptr; // obey HISTCONTROL setting QByteArray histControl = qgetenv("HISTCONTROL"); if (histControl == "ignoredups" || histControl == "ignoreboth") { q->setDuplicatesEnabled(false); } q->connect(q, SIGNAL(aboutToShowContextMenu(QMenu*)), SLOT(_k_addContextMenuItems(QMenu*))); q->connect(q, SIGNAL(activated(int)), SLOT(reset())); q->connect(q, SIGNAL(returnPressed(QString)), SLOT(reset())); // We want _k_simulateActivated to be called _after_ QComboBoxPrivate::_q_returnPressed // otherwise there's a risk of emitting activated twice (_k_simulateActivated will find // the item, after some app's slotActivated inserted the item into the combo). q->connect(q, SIGNAL(returnPressed(QString)), SLOT(_k_simulateActivated(QString)), Qt::QueuedConnection); } // we are always read-write KHistoryComboBox::KHistoryComboBox(QWidget *parent) : KComboBox(true, parent), d_ptr(new KHistoryComboBoxPrivate(this)) { Q_D(KHistoryComboBox); d->init(true); // using completion } // we are always read-write KHistoryComboBox::KHistoryComboBox(bool useCompletion, QWidget *parent) : KComboBox(true, parent), d_ptr(new KHistoryComboBoxPrivate(this)) { Q_D(KHistoryComboBox); d->init(useCompletion); } KHistoryComboBox::~KHistoryComboBox() { Q_D(KHistoryComboBox); delete d->pixmapProvider; } void KHistoryComboBox::setHistoryItems(const QStringList &items) { setHistoryItems(items, false); } void KHistoryComboBox::setHistoryItems(const QStringList &items, bool setCompletionList) { QStringList insertingItems = items; KComboBox::clear(); // limit to maxCount() const int itemCount = insertingItems.count(); const int toRemove = itemCount - maxCount(); if (toRemove >= itemCount) { insertingItems.clear(); } else { for (int i = 0; i < toRemove; ++i) { insertingItems.pop_front(); } } insertItems(insertingItems); if (setCompletionList && useCompletion()) { // we don't have any weighting information here ;( KCompletion *comp = completionObject(); comp->setOrder(KCompletion::Insertion); comp->setItems(insertingItems); comp->setOrder(KCompletion::Weighted); } clearEditText(); } QStringList KHistoryComboBox::historyItems() const { QStringList list; const int itemCount = count(); + list.reserve(itemCount); for (int i = 0; i < itemCount; ++i) { list.append(itemText(i)); } return list; } bool KHistoryComboBox::useCompletion() const { return compObj(); } void KHistoryComboBox::clearHistory() { const QString temp = currentText(); KComboBox::clear(); if (useCompletion()) { completionObject()->clear(); } setEditText(temp); } void KHistoryComboBoxPrivate::_k_addContextMenuItems(QMenu *menu) { Q_Q(KHistoryComboBox); if (menu) { menu->addSeparator(); QAction *clearHistory = menu->addAction(QIcon::fromTheme(QStringLiteral("edit-clear-history")), KHistoryComboBox::tr("Clear &History"), q, SLOT(_k_clear())); if (!q->count()) { clearHistory->setEnabled(false); } } } void KHistoryComboBox::addToHistory(const QString &item) { Q_D(KHistoryComboBox); if (item.isEmpty() || (count() > 0 && item == itemText(0))) { return; } bool wasCurrent = false; // remove all existing items before adding if (!duplicatesEnabled()) { int i = 0; int itemCount = count(); while (i < itemCount) { if (itemText(i) == item) { if (!wasCurrent) { wasCurrent = (i == currentIndex()); } removeItem(i); --itemCount; } else { ++i; } } } // now add the item if (d->pixmapProvider) { insertItem(0, d->pixmapProvider->pixmapFor(item, iconSize().height()), item); } else { insertItem(0, item); } if (wasCurrent) { setCurrentIndex(0); } const bool useComp = useCompletion(); const int last = count() - 1; // last valid index const int mc = maxCount(); const int stopAt = qMax(mc, 0); for (int rmIndex = last; rmIndex >= stopAt; --rmIndex) { // remove the last item, as long as we are longer than maxCount() // remove the removed item from the completionObject if it isn't // anymore available at all in the combobox. const QString rmItem = itemText(rmIndex); removeItem(rmIndex); if (useComp && !contains(rmItem)) { completionObject()->removeItem(rmItem); } } if (useComp) { completionObject()->addItem(item); } } bool KHistoryComboBox::removeFromHistory(const QString &item) { if (item.isEmpty()) { return false; } bool removed = false; const QString temp = currentText(); int i = 0; int itemCount = count(); while (i < itemCount) { if (item == itemText(i)) { removed = true; removeItem(i); --itemCount; } else { ++i; } } if (removed && useCompletion()) { completionObject()->removeItem(item); } setEditText(temp); return removed; } // going up in the history, rotating when reaching QListBox::count() // // Note: this differs from QComboBox because "up" means ++index here, // to simulate the way shell history works (up goes to the most // recent item). In QComboBox "down" means ++index, to match the popup... // void KHistoryComboBoxPrivate::rotateUp() { Q_Q(KHistoryComboBox); // save the current text in the lineedit // (This is also where this differs from standard up/down in QComboBox, // where a single keypress can make you lose your typed text) if (currentIndex == -1) { typedText = q->currentText(); } ++currentIndex; // skip duplicates/empty items const int last = q->count() - 1; // last valid index const QString currText = q->currentText(); while (currentIndex < last && (currText == q->itemText(currentIndex) || q->itemText(currentIndex).isEmpty())) { ++currentIndex; } if (currentIndex >= q->count()) { rotated = true; currentIndex = -1; // if the typed text is the same as the first item, skip the first if (q->count() > 0 && typedText == q->itemText(0)) { currentIndex = 0; } q->setEditText(typedText); } else { q->setCurrentIndex(currentIndex); } } // going down in the history, no rotation possible. Last item will be // the text that was in the lineedit before Up was called. void KHistoryComboBoxPrivate::rotateDown() { Q_Q(KHistoryComboBox); // save the current text in the lineedit if (currentIndex == -1) { typedText = q->currentText(); } --currentIndex; const QString currText = q->currentText(); // skip duplicates/empty items while (currentIndex >= 0 && (currText == q->itemText(currentIndex) || q->itemText(currentIndex).isEmpty())) { --currentIndex; } if (currentIndex < 0) { if (rotated && currentIndex == -2) { rotated = false; currentIndex = q->count() - 1; q->setEditText(q->itemText(currentIndex)); } else { // bottom of history currentIndex = -1; if (q->currentText() != typedText) { q->setEditText(typedText); } } } else { q->setCurrentIndex(currentIndex); } } void KHistoryComboBox::keyPressEvent(QKeyEvent *e) { Q_D(KHistoryComboBox); int event_key = e->key() | e->modifiers(); if (KStandardShortcut::rotateUp().contains(event_key)) { d->rotateUp(); } else if (KStandardShortcut::rotateDown().contains(event_key)) { d->rotateDown(); } else { KComboBox::keyPressEvent(e); } } void KHistoryComboBox::wheelEvent(QWheelEvent *ev) { Q_D(KHistoryComboBox); // Pass to poppable listbox if it's up QAbstractItemView *const iv = view(); if (iv && iv->isVisible()) { QApplication::sendEvent(iv, ev); return; } // Otherwise make it change the text without emitting activated if (ev->delta() > 0) { d->rotateUp(); } else { d->rotateDown(); } ev->accept(); } void KHistoryComboBox::setPixmapProvider(KPixmapProvider *provider) { Q_D(KHistoryComboBox); if (d->pixmapProvider == provider) { return; } delete d->pixmapProvider; d->pixmapProvider = provider; // re-insert all the items with/without pixmap // I would prefer to use changeItem(), but that doesn't honor the pixmap // when using an editable combobox (what we do) if (count() > 0) { QStringList items(historyItems()); clear(); insertItems(items); } } void KHistoryComboBox::insertItems(const QStringList &items) { Q_D(KHistoryComboBox); QStringList::ConstIterator it = items.constBegin(); const QStringList::ConstIterator itEnd = items.constEnd(); while (it != itEnd) { const QString item = *it; if (!item.isEmpty()) { // only insert non-empty items if (d->pixmapProvider) addItem(d->pixmapProvider->pixmapFor(item, iconSize().height()), item); else { addItem(item); } } ++it; } } void KHistoryComboBoxPrivate::_k_clear() { Q_Q(KHistoryComboBox); q->clearHistory(); emit q->cleared(); } void KHistoryComboBoxPrivate::_k_simulateActivated(const QString &text) { Q_Q(KHistoryComboBox); /* With the insertion policy NoInsert, which we use by default, Qt doesn't emit activated on typed text if the item is not already there, which is perhaps reasonable. Generate the signal ourselves if that's the case. */ if ((q->insertPolicy() == q->NoInsert && q->findText(text, Qt::MatchFixedString | Qt::MatchCaseSensitive) == -1)) { emit q->activated(text); } /* Qt also doesn't emit it if the box is full, and policy is not InsertAtCurrent */ else if (q->insertPolicy() != q->InsertAtCurrent && q->count() >= q->maxCount()) { emit q->activated(text); } } KPixmapProvider *KHistoryComboBox::pixmapProvider() const { Q_D(const KHistoryComboBox); return d->pixmapProvider; } void KHistoryComboBox::reset() { Q_D(KHistoryComboBox); d->currentIndex = -1; d->rotated = false; } #include "moc_khistorycombobox.cpp"