2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-09-15 13:39:28 +02:00
|
|
|
**
|
2013-01-28 17:12:19 +01:00
|
|
|
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2010-09-15 13:39:28 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-09-15 13:39:28 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2010-09-15 13:39:28 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-09-15 13:39:28 +02:00
|
|
|
|
|
|
|
|
#include "historycompleter.h"
|
2012-08-23 14:26:24 +02:00
|
|
|
|
2012-08-22 15:08:38 +02:00
|
|
|
#include "qtcassert.h"
|
2011-01-24 12:29:48 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QAbstractListModel>
|
|
|
|
|
#include <QSettings>
|
2011-01-24 12:29:48 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QItemDelegate>
|
2012-08-22 15:08:38 +02:00
|
|
|
#include <QKeyEvent>
|
|
|
|
|
#include <QLineEdit>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QListView>
|
|
|
|
|
#include <QPainter>
|
|
|
|
|
#include <QStyle>
|
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
|
|
|
|
2012-08-23 13:47:15 +02:00
|
|
|
static QSettings *theSettings = 0;
|
|
|
|
|
|
2012-08-22 13:53:21 +02:00
|
|
|
class HistoryCompleterPrivate : public QAbstractListModel
|
2010-09-15 13:39:28 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2012-08-27 16:23:49 +02:00
|
|
|
HistoryCompleterPrivate() : maxLines(30), lineEdit(0) {}
|
2012-08-23 14:26:24 +02:00
|
|
|
|
2012-08-22 13:53:21 +02:00
|
|
|
int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
|
|
|
|
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
|
|
|
|
|
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-15 13:39:28 +02:00
|
|
|
|
|
|
|
|
QStringList list;
|
2012-08-22 15:08:38 +02:00
|
|
|
QString historyKey;
|
2010-09-15 13:39:28 +02:00
|
|
|
int maxLines;
|
2012-08-27 16:23:49 +02:00
|
|
|
QLineEdit *lineEdit;
|
2010-09-15 13:39:28 +02:00
|
|
|
};
|
|
|
|
|
|
2010-09-17 12:49:08 +02:00
|
|
|
class HistoryLineDelegate : public QItemDelegate
|
|
|
|
|
{
|
|
|
|
|
public:
|
2012-08-22 13:53:21 +02:00
|
|
|
HistoryLineDelegate()
|
|
|
|
|
: pixmap(QLatin1String(":/core/images/editclear.png"))
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
void 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);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-17 12:49:08 +02:00
|
|
|
QPixmap pixmap;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class HistoryLineView : public QListView
|
|
|
|
|
{
|
|
|
|
|
public:
|
2012-08-23 14:26:24 +02:00
|
|
|
HistoryLineView(HistoryCompleterPrivate *model_)
|
|
|
|
|
: model(model_)
|
|
|
|
|
{
|
|
|
|
|
HistoryLineDelegate *delegate = new HistoryLineDelegate;
|
|
|
|
|
pixmapWidth = delegate->pixmap.width();
|
|
|
|
|
setItemDelegate(delegate);
|
|
|
|
|
}
|
2012-08-22 13:53:21 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void mousePressEvent(QMouseEvent *event)
|
|
|
|
|
{
|
|
|
|
|
int rr= event->x();
|
|
|
|
|
if (layoutDirection() == Qt::LeftToRight)
|
|
|
|
|
rr = viewport()->width() - event->x();
|
|
|
|
|
if (rr < pixmapWidth) {
|
|
|
|
|
model->removeRow(indexAt(event->pos()).row());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
QListView::mousePressEvent(event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HistoryCompleterPrivate *model;
|
2010-09-17 12:49:08 +02:00
|
|
|
int pixmapWidth;
|
|
|
|
|
};
|
|
|
|
|
|
2011-11-23 14:14:57 +01:00
|
|
|
} // namespace Internal
|
|
|
|
|
|
2012-08-22 13:53:21 +02:00
|
|
|
using namespace Internal;
|
2010-09-17 12:49:08 +02:00
|
|
|
|
2012-08-22 13:53:21 +02:00
|
|
|
int HistoryCompleterPrivate::rowCount(const QModelIndex &parent) const
|
2010-09-15 13:39:28 +02:00
|
|
|
{
|
2012-08-23 14:26:24 +02:00
|
|
|
return parent.isValid() ? 0 : list.count();
|
2010-09-15 13:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
2012-08-22 13:53:21 +02:00
|
|
|
QVariant HistoryCompleterPrivate::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
|
|
|
}
|
|
|
|
|
|
2012-08-22 13:53:21 +02:00
|
|
|
bool HistoryCompleterPrivate::removeRows(int row, int count, const QModelIndex &parent)
|
2010-09-17 12:49:08 +02:00
|
|
|
{
|
2012-10-28 12:12:04 +08:00
|
|
|
QTC_ASSERT(theSettings, return false);
|
|
|
|
|
if (row + count > list.count())
|
|
|
|
|
return false;
|
|
|
|
|
beginRemoveRows(parent, row, row + count -1);
|
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
|
list.removeAt(row);
|
2012-08-23 13:47:15 +02:00
|
|
|
theSettings->setValue(historyKey, list);
|
2010-09-17 12:49:08 +02:00
|
|
|
endRemoveRows();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-22 13:53:21 +02:00
|
|
|
void HistoryCompleterPrivate::clearHistory()
|
2010-09-15 13:39:28 +02:00
|
|
|
{
|
2012-09-20 10:31:34 +02:00
|
|
|
beginResetModel();
|
2010-09-15 13:39:28 +02:00
|
|
|
list.clear();
|
2012-09-20 10:31:34 +02:00
|
|
|
endResetModel();
|
2010-09-15 13:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
2012-08-22 13:53:21 +02:00
|
|
|
void HistoryCompleterPrivate::saveEntry(const QString &str)
|
2010-09-15 13:39:28 +02:00
|
|
|
{
|
2012-08-23 13:47:15 +02:00
|
|
|
QTC_ASSERT(theSettings, return);
|
2012-10-19 23:37:54 +08:00
|
|
|
const QString &entry = str.trimmed();
|
|
|
|
|
int removeIndex = list.indexOf(entry);
|
2012-10-16 19:19:26 +02:00
|
|
|
if (removeIndex != -1) {
|
|
|
|
|
beginRemoveRows(QModelIndex(), removeIndex, removeIndex);
|
|
|
|
|
list.removeAt(removeIndex);
|
|
|
|
|
endRemoveRows();
|
|
|
|
|
}
|
2010-09-15 13:39:28 +02:00
|
|
|
beginInsertRows (QModelIndex(), list.count(), list.count());
|
2012-10-19 23:37:54 +08:00
|
|
|
list.prepend(entry);
|
2010-09-15 13:39:28 +02:00
|
|
|
list = list.mid(0, maxLines);
|
|
|
|
|
endInsertRows();
|
2012-08-23 13:47:15 +02:00
|
|
|
theSettings->setValue(historyKey, list);
|
2010-09-15 13:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
2012-08-27 15:49:08 +03:00
|
|
|
HistoryCompleter::HistoryCompleter(QLineEdit *lineEdit, const QString &historyKey, QObject *parent)
|
|
|
|
|
: QCompleter(parent),
|
|
|
|
|
d(new HistoryCompleterPrivate)
|
2010-09-15 13:39:28 +02:00
|
|
|
{
|
2012-08-23 14:26:24 +02:00
|
|
|
QTC_ASSERT(lineEdit, return);
|
2012-08-23 13:47:15 +02:00
|
|
|
QTC_ASSERT(!historyKey.isEmpty(), return);
|
2012-08-23 14:26:24 +02:00
|
|
|
QTC_ASSERT(theSettings, return);
|
2012-08-22 13:53:21 +02:00
|
|
|
|
2012-08-23 13:47:15 +02:00
|
|
|
d->historyKey = QLatin1String("CompleterHistory/") + historyKey;
|
|
|
|
|
d->list = theSettings->value(d->historyKey).toStringList();
|
2012-08-27 16:23:49 +02:00
|
|
|
d->lineEdit = lineEdit;
|
2012-08-23 14:26:24 +02:00
|
|
|
if (d->list.count())
|
|
|
|
|
lineEdit->setText(d->list.at(0));
|
2010-12-01 15:41:08 +01:00
|
|
|
|
2012-08-22 13:53:21 +02:00
|
|
|
setModel(d);
|
2012-08-23 14:26:24 +02:00
|
|
|
setPopup(new HistoryLineView(d));
|
|
|
|
|
lineEdit->installEventFilter(this);
|
2012-08-27 16:23:49 +02:00
|
|
|
|
|
|
|
|
connect(lineEdit, SIGNAL(editingFinished()), this, SLOT(saveHistory()));
|
2010-09-15 13:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
2012-07-19 22:26:45 +03:00
|
|
|
HistoryCompleter::~HistoryCompleter()
|
|
|
|
|
{
|
|
|
|
|
delete d;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-23 14:26:24 +02:00
|
|
|
bool HistoryCompleter::eventFilter(QObject *obj, QEvent *event)
|
|
|
|
|
{
|
2012-08-27 16:23:49 +02:00
|
|
|
if (event->type() == QEvent::KeyPress
|
|
|
|
|
&& static_cast<QKeyEvent *>(event)->key() == Qt::Key_Down
|
|
|
|
|
&& !popup()->isVisible()) {
|
2012-08-23 14:26:24 +02:00
|
|
|
setCompletionPrefix(QString());
|
|
|
|
|
complete();
|
|
|
|
|
}
|
|
|
|
|
return QCompleter::eventFilter(obj, event);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-15 13:39:28 +02:00
|
|
|
int HistoryCompleter::historySize() const
|
|
|
|
|
{
|
2012-08-22 13:53:21 +02:00
|
|
|
return d->rowCount();
|
2010-09-15 13:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int HistoryCompleter::maximalHistorySize() const
|
|
|
|
|
{
|
2012-08-22 13:53:21 +02:00
|
|
|
return d->maxLines;
|
2010-09-15 13:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HistoryCompleter::setMaximalHistorySize(int numberOfEntries)
|
|
|
|
|
{
|
2012-08-22 13:53:21 +02:00
|
|
|
d->maxLines = numberOfEntries;
|
2010-09-15 13:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HistoryCompleter::clearHistory()
|
|
|
|
|
{
|
2012-08-22 13:53:21 +02:00
|
|
|
d->clearHistory();
|
2010-09-15 13:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HistoryCompleter::saveHistory()
|
|
|
|
|
{
|
2012-08-27 16:23:49 +02:00
|
|
|
d->saveEntry(d->lineEdit->text());
|
2010-09-17 12:49:08 +02:00
|
|
|
}
|
|
|
|
|
|
2012-08-23 13:47:15 +02:00
|
|
|
void HistoryCompleter::setSettings(QSettings *settings)
|
|
|
|
|
{
|
|
|
|
|
Internal::theSettings = settings;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-22 13:53:21 +02:00
|
|
|
} // namespace Utils
|