diff --git a/src/libs/utils/historycompleter.cpp b/src/libs/utils/historycompleter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..165bd99a0069fa9447bc47ae9f8dcd2529e0e696 --- /dev/null +++ b/src/libs/utils/historycompleter.cpp @@ -0,0 +1,190 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://qt.nokia.com/contact. +** +**************************************************************************/ + +#include "historycompleter.h" +#include <QLineEdit> +#include <QCompleter> +#include <QAbstractListModel> +#include <QSettings> + +namespace Utils { + +class HistoryListModel : public QAbstractListModel +{ +public: + HistoryListModel(HistoryCompleter *parent); + void fetchHistory(); + int rowCount(const QModelIndex & parent = QModelIndex()) const; + QVariant data( const QModelIndex & index, int role = Qt::DisplayRole) const; + void clearHistory(); + void saveEntry(const QString & str); + + QStringList list; + HistoryCompleter *q; + QWidget *lastSeenWidget; + QSettings *settings; + int maxLines; +}; + +HistoryListModel::HistoryListModel(HistoryCompleter *parent) + : QAbstractListModel(parent) + , q(parent) + , lastSeenWidget(0) + , settings(new QSettings(parent)) + , maxLines(30) +{ + settings->beginGroup(QLatin1String("completerHistory")); +} + +void HistoryListModel::fetchHistory() +{ + if (!q->widget()) { + list.clear(); + reset(); + return; + } + QString objectName = q->widget()->objectName(); + if (objectName.isEmpty()) + return; + list = settings->value(objectName).toStringList(); + reset(); +} + +int HistoryListModel::rowCount(const QModelIndex & parent) const +{ + if (lastSeenWidget != q->widget()) { + if (qobject_cast<QLineEdit *>(lastSeenWidget)) + // this will result in spamming the history with garbage in some corner cases. + // not my idea. + disconnect(lastSeenWidget, SIGNAL(editingFinished ()), q, SLOT(saveHistory())); + HistoryListModel *that = const_cast<HistoryListModel *>(this); + that->lastSeenWidget = q->widget(); + that->fetchHistory(); + if (qobject_cast<QLineEdit *>(lastSeenWidget)) + connect(lastSeenWidget, SIGNAL(editingFinished ()), q, SLOT(saveHistory())); + } + if (parent.isValid()) + return 0; + return list.count(); +} + +QVariant HistoryListModel::data(const QModelIndex & index, int role) const +{ + if (index.row() >= list.count() || index.column() != 0 || (role != Qt::DisplayRole && role != Qt::EditRole)) + return QVariant(); + return list.at(index.row()); +} + +void HistoryListModel::clearHistory() +{ + list.clear(); + reset(); +} + +void HistoryListModel::saveEntry(const QString & str) +{ + if (list.contains(str)) + return; + if (!q->widget()) + return; + if (lastSeenWidget != q->widget()) { + fetchHistory(); + lastSeenWidget = q->widget(); + } + QString objectName = q->widget()->objectName(); + if (objectName.isEmpty()) + return; + beginInsertRows (QModelIndex(), list.count(), list.count()); + list.prepend(str); + list = list.mid(0, maxLines); + endInsertRows(); + settings->setValue(objectName, list); +} + + +class HistoryCompleterPrivate +{ +public: + HistoryCompleterPrivate(HistoryCompleter *parent) + : q_ptr(parent) + , model(new HistoryListModel(parent)) + { + Q_Q(HistoryCompleter); + q->setModel(model); + } + HistoryCompleter *q_ptr; + HistoryListModel *model; + Q_DECLARE_PUBLIC(HistoryCompleter); +}; + + +HistoryCompleter::HistoryCompleter(QObject *parent) + : QCompleter(parent) + , d_ptr(new HistoryCompleterPrivate(this)) +{ + +} + +QSettings *HistoryCompleter::settings() const +{ + Q_D(const HistoryCompleter); + return d->model->settings; +} + +int HistoryCompleter::historySize() const +{ + Q_D(const HistoryCompleter); + return d->model->rowCount(); +} + +int HistoryCompleter::maximalHistorySize() const +{ + Q_D(const HistoryCompleter); + return d->model->maxLines; +} + +void HistoryCompleter::setMaximalHistorySize(int numberOfEntries) +{ + Q_D(const HistoryCompleter); + d->model->maxLines = numberOfEntries; +} + +void HistoryCompleter::clearHistory() +{ + Q_D(const HistoryCompleter); + d->model->clearHistory(); +} + +void HistoryCompleter::saveHistory() +{ + Q_D(HistoryCompleter); + d->model->saveEntry(completionPrefix()); +} + +} // namespace Utils diff --git a/src/libs/utils/historycompleter.h b/src/libs/utils/historycompleter.h new file mode 100644 index 0000000000000000000000000000000000000000..b61d2310448fedff1447a396b8859769b6ee466f --- /dev/null +++ b/src/libs/utils/historycompleter.h @@ -0,0 +1,62 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://qt.nokia.com/contact. +** +**************************************************************************/ + +#ifndef HISTORYCOMPLETER_H +#define HISTORYCOMPLETER_H + +#include "utils_global.h" + +#include <QtGui/QCompleter> + +QT_FORWARD_DECLARE_CLASS(QSettings) + +namespace Utils { + +class HistoryCompleterPrivate; +class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter +{ +Q_OBJECT +public: + HistoryCompleter(QObject *parent = 0); + QSettings *settings() const; + int historySize() const; + int maximalHistorySize() const; + void setMaximalHistorySize(int numberOfEntries); +public Q_SLOTS: + void clearHistory(); + void saveHistory(); +protected: + HistoryCompleterPrivate *d_ptr; +private: + Q_DECLARE_PRIVATE(HistoryCompleter); +}; + +} // namespace Utils + +#endif // HISTORYCOMPLETER_H diff --git a/src/libs/utils/utils-lib.pri b/src/libs/utils/utils-lib.pri index f159d4d313ccd76c550a3f9fb1432661e6c3f1f8..d338913edf36aef1691bb22e0b36b0c7b47732f1 100644 --- a/src/libs/utils/utils-lib.pri +++ b/src/libs/utils/utils-lib.pri @@ -46,7 +46,8 @@ SOURCES += $$PWD/reloadpromptutils.cpp \ $$PWD/htmldocextractor.cpp \ $$PWD/navigationtreeview.cpp \ $$PWD/crumblepath.cpp \ - $$PWD/debuggerlanguagechooser.cpp + $$PWD/debuggerlanguagechooser.cpp \ + $$PWD/historycompleter.cpp win32 { SOURCES += $$PWD/abstractprocess_win.cpp \ @@ -104,7 +105,8 @@ HEADERS += $$PWD/utils_global.h \ $$PWD/htmldocextractor.h \ $$PWD/navigationtreeview.h \ $$PWD/crumblepath.h \ - $$PWD/debuggerlanguagechooser.h + $$PWD/debuggerlanguagechooser.h \ + $$PWD/historycompleter.h FORMS += $$PWD/filewizardpage.ui \ $$PWD/projectintropage.ui \