2010-09-15 13:39:28 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2010-09-15 13:39:28 +02:00
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2010-09-15 13:39:28 +02:00
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
2010-09-15 13:39:28 +02:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
2010-09-15 13:39:28 +02:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "historycompleter.h"
|
2011-01-24 12:29:48 +01:00
|
|
|
|
|
|
|
|
#include <QtCore/QAbstractListModel>
|
|
|
|
|
#include <QtCore/QSettings>
|
|
|
|
|
|
|
|
|
|
#include <QtGui/QLineEdit>
|
|
|
|
|
#include <QtGui/QKeyEvent>
|
|
|
|
|
#include <QtGui/QItemDelegate>
|
|
|
|
|
#include <QtGui/QListView>
|
|
|
|
|
#include <QtGui/QPainter>
|
|
|
|
|
#include <QtGui/QStyle>
|
2010-09-15 13:39:28 +02:00
|
|
|
|
2011-11-23 12:31:04 +01:00
|
|
|
static const char SETTINGS_PREFIX[] = "CompleterHistory/";
|
|
|
|
|
|
2010-09-15 13:39:28 +02:00
|
|
|
namespace Utils {
|
2011-11-23 14:14:57 +01:00
|
|
|
namespace Internal {
|
2010-09-15 13:39:28 +02:00
|
|
|
|
|
|
|
|
class HistoryListModel : public QAbstractListModel
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
HistoryListModel(HistoryCompleter *parent);
|
|
|
|
|
void fetchHistory();
|
2010-09-17 12:49:08 +02:00
|
|
|
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
|
|
|
|
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
|
|
|
|
|
virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
|
2010-09-15 13:39:28 +02:00
|
|
|
void clearHistory();
|
2010-09-16 11:57:37 +02:00
|
|
|
void saveEntry(const QString &str);
|
2010-09-17 10:54:12 +02:00
|
|
|
bool eventFilter(QObject *obj, QEvent *event);
|
2010-09-15 13:39:28 +02:00
|
|
|
|
|
|
|
|
QStringList list;
|
2011-11-23 14:14:57 +01:00
|
|
|
HistoryCompleter *completer;
|
2010-09-15 13:39:28 +02:00
|
|
|
QWidget *lastSeenWidget;
|
|
|
|
|
QSettings *settings;
|
|
|
|
|
int maxLines;
|
|
|
|
|
};
|
|
|
|
|
|
2010-09-17 12:49:08 +02:00
|
|
|
class HistoryCompleterPrivate
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
HistoryCompleterPrivate(HistoryCompleter *parent);
|
2011-11-23 14:14:57 +01:00
|
|
|
HistoryCompleter *q;
|
2010-09-17 12:49:08 +02:00
|
|
|
HistoryListModel *model;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class HistoryLineDelegate : public QItemDelegate
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
HistoryLineDelegate();
|
|
|
|
|
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
|
|
|
|
|
QPixmap pixmap;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class HistoryLineView : public QListView
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
HistoryCompleterPrivate *d;
|
|
|
|
|
int pixmapWidth;
|
|
|
|
|
HistoryLineView(HistoryCompleterPrivate *d_, int pixmapWith_);
|
|
|
|
|
virtual void mousePressEvent(QMouseEvent *event);
|
|
|
|
|
};
|
|
|
|
|
|
2011-11-23 14:14:57 +01:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Utils
|
|
|
|
|
|
|
|
|
|
using namespace Utils;
|
|
|
|
|
using namespace Utils::Internal;
|
2010-09-17 12:49:08 +02:00
|
|
|
|
2010-09-15 13:39:28 +02:00
|
|
|
HistoryListModel::HistoryListModel(HistoryCompleter *parent)
|
|
|
|
|
: QAbstractListModel(parent)
|
2011-11-23 14:14:57 +01:00
|
|
|
, completer(parent)
|
2010-09-15 13:39:28 +02:00
|
|
|
, lastSeenWidget(0)
|
2011-11-23 12:31:04 +01:00
|
|
|
, settings(0)
|
2010-09-15 13:39:28 +02:00
|
|
|
, maxLines(30)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HistoryListModel::fetchHistory()
|
|
|
|
|
{
|
2011-11-23 14:14:57 +01:00
|
|
|
if (!completer->widget() || !settings) {
|
2010-09-15 13:39:28 +02:00
|
|
|
list.clear();
|
|
|
|
|
reset();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-11-23 14:14:57 +01:00
|
|
|
QString objectName = completer->widget()->objectName();
|
2010-09-15 13:39:28 +02:00
|
|
|
if (objectName.isEmpty())
|
|
|
|
|
return;
|
2011-11-23 12:31:04 +01:00
|
|
|
list = settings->value(QLatin1String(SETTINGS_PREFIX) + objectName).toStringList();
|
2010-09-15 13:39:28 +02:00
|
|
|
reset();
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 11:57:37 +02:00
|
|
|
int HistoryListModel::rowCount(const QModelIndex &parent) const
|
2010-09-15 13:39:28 +02:00
|
|
|
{
|
2011-11-23 14:14:57 +01:00
|
|
|
if (lastSeenWidget != completer->widget()) {
|
2010-09-17 10:54:12 +02:00
|
|
|
if (lastSeenWidget)
|
|
|
|
|
const_cast<QWidget*>(lastSeenWidget)->removeEventFilter(const_cast<HistoryListModel *>(this));
|
2011-11-23 14:14:57 +01:00
|
|
|
const_cast<QWidget*>(completer->widget())->installEventFilter(const_cast<HistoryListModel *>(this));
|
2010-09-15 13:39:28 +02:00
|
|
|
if (qobject_cast<QLineEdit *>(lastSeenWidget))
|
|
|
|
|
// this will result in spamming the history with garbage in some corner cases.
|
|
|
|
|
// not my idea.
|
2011-11-23 14:14:57 +01:00
|
|
|
disconnect(lastSeenWidget, SIGNAL(editingFinished ()), completer, SLOT(saveHistory()));
|
2010-09-15 13:39:28 +02:00
|
|
|
HistoryListModel *that = const_cast<HistoryListModel *>(this);
|
2011-11-23 14:14:57 +01:00
|
|
|
that->lastSeenWidget = completer->widget();
|
2010-09-15 13:39:28 +02:00
|
|
|
that->fetchHistory();
|
|
|
|
|
if (qobject_cast<QLineEdit *>(lastSeenWidget))
|
2011-11-23 14:14:57 +01:00
|
|
|
connect(lastSeenWidget, SIGNAL(editingFinished ()), completer, SLOT(saveHistory()));
|
2010-09-15 13:39:28 +02:00
|
|
|
}
|
|
|
|
|
if (parent.isValid())
|
|
|
|
|
return 0;
|
|
|
|
|
return list.count();
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 11:57:37 +02:00
|
|
|
QVariant HistoryListModel::data(const QModelIndex &index, int role) const
|
2010-09-15 13:39:28 +02:00
|
|
|
{
|
2010-09-16 11:57:37 +02:00
|
|
|
if (index.row() >= list.count() || index.column() != 0)
|
2010-09-15 13:39:28 +02:00
|
|
|
return QVariant();
|
2010-09-16 11:57:37 +02:00
|
|
|
if (role == Qt::DisplayRole || role == Qt::EditRole)
|
|
|
|
|
return list.at(index.row());
|
|
|
|
|
return QVariant();
|
2010-09-15 13:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
2010-09-17 12:49:08 +02:00
|
|
|
bool HistoryListModel::removeRows(int row, int count, const QModelIndex &parent)
|
|
|
|
|
{
|
|
|
|
|
beginRemoveRows (parent, row, row + count);
|
|
|
|
|
list.removeAt(row);
|
2011-11-23 12:31:04 +01:00
|
|
|
if (settings) {
|
2011-11-23 14:14:57 +01:00
|
|
|
QString objectName = completer->widget()->objectName();
|
2011-11-23 12:31:04 +01:00
|
|
|
settings->setValue(QLatin1String(SETTINGS_PREFIX) + objectName, list);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-17 12:49:08 +02:00
|
|
|
endRemoveRows();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-15 13:39:28 +02:00
|
|
|
void HistoryListModel::clearHistory()
|
|
|
|
|
{
|
|
|
|
|
list.clear();
|
|
|
|
|
reset();
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 11:57:37 +02:00
|
|
|
void HistoryListModel::saveEntry(const QString &str)
|
2010-09-15 13:39:28 +02:00
|
|
|
{
|
2010-09-17 13:45:41 +02:00
|
|
|
if (str.isEmpty())
|
|
|
|
|
return;
|
2010-09-15 13:39:28 +02:00
|
|
|
if (list.contains(str))
|
|
|
|
|
return;
|
2011-11-23 14:14:57 +01:00
|
|
|
if (!completer->widget())
|
2010-09-15 13:39:28 +02:00
|
|
|
return;
|
2011-11-23 14:14:57 +01:00
|
|
|
if (lastSeenWidget != completer->widget()) {
|
2010-09-17 10:54:12 +02:00
|
|
|
if (lastSeenWidget)
|
|
|
|
|
lastSeenWidget->removeEventFilter(this);
|
2011-11-23 14:14:57 +01:00
|
|
|
completer->widget()->installEventFilter(this);
|
2010-09-15 13:39:28 +02:00
|
|
|
fetchHistory();
|
2011-11-23 14:14:57 +01:00
|
|
|
lastSeenWidget = completer->widget();
|
2010-09-15 13:39:28 +02:00
|
|
|
}
|
2011-11-23 14:14:57 +01:00
|
|
|
QString objectName = completer->widget()->objectName();
|
2010-09-15 13:39:28 +02:00
|
|
|
if (objectName.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
beginInsertRows (QModelIndex(), list.count(), list.count());
|
|
|
|
|
list.prepend(str);
|
|
|
|
|
list = list.mid(0, maxLines);
|
|
|
|
|
endInsertRows();
|
2011-11-23 12:31:04 +01:00
|
|
|
if (settings)
|
|
|
|
|
settings->setValue(QLatin1String(SETTINGS_PREFIX) + objectName, list);
|
2010-09-15 13:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
2010-09-17 10:54:12 +02:00
|
|
|
bool HistoryListModel::eventFilter(QObject *obj, QEvent *event)
|
|
|
|
|
{
|
|
|
|
|
if (event->type() == QEvent::KeyPress && static_cast<QKeyEvent *>(event)->key() == Qt::Key_Down) {
|
2011-11-23 14:14:57 +01:00
|
|
|
completer->setCompletionPrefix(QString());
|
|
|
|
|
completer->complete();
|
2010-09-17 10:54:12 +02:00
|
|
|
}
|
|
|
|
|
return QAbstractListModel::eventFilter(obj,event);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-23 12:31:04 +01:00
|
|
|
HistoryCompleter::HistoryCompleter(QSettings *settings, QObject *parent)
|
2010-09-15 13:39:28 +02:00
|
|
|
: QCompleter(parent)
|
2011-11-23 14:14:57 +01:00
|
|
|
, d(new HistoryCompleterPrivate(this))
|
2010-09-15 13:39:28 +02:00
|
|
|
{
|
2011-11-23 14:14:57 +01:00
|
|
|
d->model->settings = settings;
|
2010-09-17 10:54:12 +02:00
|
|
|
// make an assumption to allow pressing of the down
|
|
|
|
|
// key, before the first model run:
|
|
|
|
|
// parent is likely the lineedit
|
2010-12-01 15:41:08 +01:00
|
|
|
QWidget *p = qobject_cast<QWidget *>(parent);
|
2010-09-17 10:54:12 +02:00
|
|
|
if (p) {
|
2011-11-23 14:14:57 +01:00
|
|
|
p->installEventFilter(d->model);
|
2010-09-17 10:54:12 +02:00
|
|
|
QString objectName = p->objectName();
|
|
|
|
|
if (objectName.isEmpty())
|
|
|
|
|
return;
|
2011-11-23 14:14:57 +01:00
|
|
|
if (d->model->settings) {
|
|
|
|
|
d->model->list = d->model->settings->value(
|
2011-11-23 12:31:04 +01:00
|
|
|
QLatin1String(SETTINGS_PREFIX) + objectName).toStringList();
|
|
|
|
|
}
|
2010-09-17 10:54:12 +02:00
|
|
|
}
|
2010-12-01 15:41:08 +01:00
|
|
|
|
|
|
|
|
QLineEdit *l = qobject_cast<QLineEdit *>(parent);
|
2011-11-23 14:14:57 +01:00
|
|
|
if (l && d->model->list.count())
|
|
|
|
|
l->setText(d->model->list.at(0));
|
2010-12-01 15:41:08 +01:00
|
|
|
|
2011-11-23 14:14:57 +01:00
|
|
|
setModel(d->model);
|
2010-09-17 12:49:08 +02:00
|
|
|
HistoryLineDelegate *delegate = new HistoryLineDelegate;
|
2011-11-23 14:14:57 +01:00
|
|
|
HistoryLineView *view = new HistoryLineView(d, delegate->pixmap.width());
|
2010-09-17 12:49:08 +02:00
|
|
|
setPopup(view);
|
|
|
|
|
view->setItemDelegate(delegate);
|
2010-09-15 13:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int HistoryCompleter::historySize() const
|
|
|
|
|
{
|
|
|
|
|
return d->model->rowCount();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int HistoryCompleter::maximalHistorySize() const
|
|
|
|
|
{
|
|
|
|
|
return d->model->maxLines;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HistoryCompleter::setMaximalHistorySize(int numberOfEntries)
|
|
|
|
|
{
|
|
|
|
|
d->model->maxLines = numberOfEntries;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HistoryCompleter::clearHistory()
|
|
|
|
|
{
|
|
|
|
|
d->model->clearHistory();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HistoryCompleter::saveHistory()
|
|
|
|
|
{
|
|
|
|
|
d->model->saveEntry(completionPrefix());
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-17 12:49:08 +02:00
|
|
|
HistoryCompleterPrivate::HistoryCompleterPrivate(HistoryCompleter *parent)
|
2011-11-23 14:14:57 +01:00
|
|
|
: q(parent)
|
2010-09-17 12:49:08 +02:00
|
|
|
, model(new HistoryListModel(parent))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HistoryLineDelegate::HistoryLineDelegate()
|
|
|
|
|
{
|
|
|
|
|
pixmap = QPixmap(":/core/images/editclear.png");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HistoryLineDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
|
|
|
{
|
|
|
|
|
QItemDelegate::paint(painter,option,index);
|
|
|
|
|
QRect r = QStyle::alignedRect(option.direction, Qt::AlignRight | Qt::AlignVCenter , pixmap.size(), option.rect);
|
|
|
|
|
painter->drawPixmap(r, pixmap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HistoryLineView::HistoryLineView(HistoryCompleterPrivate *d_, int pixmapWith_)
|
|
|
|
|
: d(d_)
|
|
|
|
|
, pixmapWidth(pixmapWith_)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HistoryLineView::mousePressEvent(QMouseEvent *event)
|
|
|
|
|
{
|
|
|
|
|
int rr= event->x();
|
|
|
|
|
if (layoutDirection() == Qt::LeftToRight)
|
|
|
|
|
rr = viewport()->width() - event->x();
|
|
|
|
|
if (rr < pixmapWidth) {
|
|
|
|
|
d->model->removeRow(indexAt(event->pos()).row());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
QListView::mousePressEvent(event);
|
|
|
|
|
}
|
|
|
|
|
|