2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-09-15 13:39:28 +02:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
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
|
2016-01-15 14:58:39 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2010-09-15 13:39:28 +02:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-09-15 13:39:28 +02:00
|
|
|
|
|
|
|
|
#include "historycompleter.h"
|
2020-12-22 16:21:45 +01:00
|
|
|
|
|
|
|
|
#include "qtcassert.h"
|
|
|
|
|
#include "qtcsettings.h"
|
2015-11-23 16:41:54 +01:00
|
|
|
#include "utilsicons.h"
|
2012-08-23 14:26:24 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QItemDelegate>
|
|
|
|
|
#include <QListView>
|
|
|
|
|
#include <QPainter>
|
2018-06-12 08:13:33 +02:00
|
|
|
#include <QWindow>
|
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
|
|
|
|
2020-12-22 16:21:45 +01:00
|
|
|
static QtcSettings *theSettings = nullptr;
|
|
|
|
|
const bool isLastItemEmptyDefault = false;
|
2012-08-23 13:47:15 +02:00
|
|
|
|
2012-08-22 13:53:21 +02:00
|
|
|
class HistoryCompleterPrivate : public QAbstractListModel
|
2010-09-15 13:39:28 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2018-07-19 16:39:41 +02:00
|
|
|
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
|
|
|
|
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
|
|
|
|
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
|
2012-08-22 13:53:21 +02:00
|
|
|
|
2010-09-15 13:39:28 +02:00
|
|
|
void clearHistory();
|
2014-07-07 15:04:52 +02:00
|
|
|
void addEntry(const QString &str);
|
2010-09-15 13:39:28 +02:00
|
|
|
|
|
|
|
|
QStringList list;
|
2012-08-22 15:08:38 +02:00
|
|
|
QString historyKey;
|
2015-02-08 22:28:49 +03:00
|
|
|
QString historyKeyIsLastItemEmpty;
|
2018-10-22 12:42:41 +02:00
|
|
|
int maxLines = 6;
|
2020-12-22 16:21:45 +01:00
|
|
|
bool isLastItemEmpty = isLastItemEmptyDefault;
|
2010-09-15 13:39:28 +02:00
|
|
|
};
|
|
|
|
|
|
2010-09-17 12:49:08 +02:00
|
|
|
class HistoryLineDelegate : public QItemDelegate
|
|
|
|
|
{
|
|
|
|
|
public:
|
2018-06-12 08:13:33 +02:00
|
|
|
HistoryLineDelegate(QAbstractItemView *parent)
|
2013-08-14 11:03:24 +02:00
|
|
|
: QItemDelegate(parent)
|
2018-06-12 08:13:33 +02:00
|
|
|
, view(parent)
|
|
|
|
|
, icon(Icons::EDIT_CLEAR.icon())
|
2012-08-22 13:53:21 +02:00
|
|
|
{}
|
|
|
|
|
|
2018-07-19 16:39:41 +02:00
|
|
|
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
|
2012-08-22 13:53:21 +02:00
|
|
|
{
|
2018-06-12 08:13:33 +02:00
|
|
|
// from QHistoryCompleter
|
|
|
|
|
QStyleOptionViewItem optCopy = option;
|
|
|
|
|
optCopy.showDecorationSelected = true;
|
|
|
|
|
if (view->currentIndex() == index)
|
|
|
|
|
optCopy.state |= QStyle::State_HasFocus;
|
2012-08-22 13:53:21 +02:00
|
|
|
QItemDelegate::paint(painter,option,index);
|
2018-06-12 08:13:33 +02:00
|
|
|
// add remove button
|
|
|
|
|
QWindow *window = view->window()->windowHandle();
|
|
|
|
|
const QPixmap iconPixmap = icon.pixmap(window, option.rect.size());
|
|
|
|
|
QRect pixmapRect = QStyle::alignedRect(option.direction,
|
|
|
|
|
Qt::AlignRight | Qt::AlignVCenter,
|
|
|
|
|
iconPixmap.size() / window->devicePixelRatio(),
|
|
|
|
|
option.rect);
|
|
|
|
|
if (!clearIconSize.isValid())
|
|
|
|
|
clearIconSize = pixmapRect.size();
|
|
|
|
|
painter->drawPixmap(pixmapRect, iconPixmap);
|
2012-08-22 13:53:21 +02:00
|
|
|
}
|
|
|
|
|
|
2018-06-12 08:13:33 +02:00
|
|
|
QAbstractItemView *view;
|
|
|
|
|
QIcon icon;
|
|
|
|
|
mutable QSize clearIconSize;
|
2010-09-17 12:49:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class HistoryLineView : public QListView
|
|
|
|
|
{
|
|
|
|
|
public:
|
2012-08-23 14:26:24 +02:00
|
|
|
HistoryLineView(HistoryCompleterPrivate *model_)
|
|
|
|
|
: model(model_)
|
|
|
|
|
{
|
2018-06-12 08:13:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void installDelegate()
|
|
|
|
|
{
|
|
|
|
|
delegate = new HistoryLineDelegate(this);
|
2012-08-23 14:26:24 +02:00
|
|
|
setItemDelegate(delegate);
|
|
|
|
|
}
|
2012-08-22 13:53:21 +02:00
|
|
|
|
|
|
|
|
private:
|
2018-07-19 16:39:41 +02:00
|
|
|
void mousePressEvent(QMouseEvent *event) override
|
2012-08-22 13:53:21 +02:00
|
|
|
{
|
2018-06-12 08:13:33 +02:00
|
|
|
const QSize clearButtonSize = delegate->clearIconSize;
|
|
|
|
|
if (clearButtonSize.isValid()) {
|
|
|
|
|
int rr = event->x();
|
|
|
|
|
if (layoutDirection() == Qt::LeftToRight)
|
|
|
|
|
rr = viewport()->width() - event->x();
|
|
|
|
|
if (rr < clearButtonSize.width()) {
|
2018-12-03 09:35:44 +01:00
|
|
|
const QModelIndex index = indexAt(event->pos());
|
|
|
|
|
if (index.isValid()) {
|
|
|
|
|
model->removeRow(indexAt(event->pos()).row());
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-06-12 08:13:33 +02:00
|
|
|
}
|
2012-08-22 13:53:21 +02:00
|
|
|
}
|
|
|
|
|
QListView::mousePressEvent(event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HistoryCompleterPrivate *model;
|
2018-06-12 08:13:33 +02:00
|
|
|
HistoryLineDelegate *delegate;
|
2010-09-17 12:49:08 +02:00
|
|
|
};
|
|
|
|
|
|
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);
|
2020-12-22 16:21:45 +01:00
|
|
|
theSettings->setValueWithDefault(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
|
|
|
}
|
|
|
|
|
|
2014-07-07 15:04:52 +02:00
|
|
|
void HistoryCompleterPrivate::addEntry(const QString &str)
|
2010-09-15 13:39:28 +02:00
|
|
|
{
|
2016-01-18 23:05:31 +02:00
|
|
|
QTC_ASSERT(theSettings, return);
|
2014-07-07 15:04:52 +02:00
|
|
|
const QString entry = str.trimmed();
|
2015-02-08 22:28:49 +03:00
|
|
|
if (entry.isEmpty()) {
|
|
|
|
|
isLastItemEmpty = true;
|
2020-12-22 16:21:45 +01:00
|
|
|
theSettings->setValueWithDefault(historyKeyIsLastItemEmpty,
|
|
|
|
|
isLastItemEmpty,
|
|
|
|
|
isLastItemEmptyDefault);
|
2014-07-07 15:04:52 +02:00
|
|
|
return;
|
2015-02-08 22:28:49 +03:00
|
|
|
}
|
2012-10-19 23:37:54 +08:00
|
|
|
int removeIndex = list.indexOf(entry);
|
2014-07-07 15:04:52 +02:00
|
|
|
beginResetModel();
|
2012-10-28 12:13:09 +08:00
|
|
|
if (removeIndex != -1)
|
2014-07-07 15:04:52 +02:00
|
|
|
list.removeAt(removeIndex);
|
2012-10-19 23:37:54 +08:00
|
|
|
list.prepend(entry);
|
2014-07-07 15:04:52 +02:00
|
|
|
list = list.mid(0, maxLines - 1);
|
|
|
|
|
endResetModel();
|
2020-12-22 16:21:45 +01:00
|
|
|
theSettings->setValueWithDefault(historyKey, list);
|
2015-02-08 22:28:49 +03:00
|
|
|
isLastItemEmpty = false;
|
2020-12-22 16:21:45 +01:00
|
|
|
theSettings->setValueWithDefault(historyKeyIsLastItemEmpty,
|
|
|
|
|
isLastItemEmpty,
|
|
|
|
|
isLastItemEmptyDefault);
|
2010-09-15 13:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-07 15:04:52 +02:00
|
|
|
HistoryCompleter::HistoryCompleter(const QString &historyKey, QObject *parent)
|
2012-08-27 15:49:08 +03:00
|
|
|
: QCompleter(parent),
|
|
|
|
|
d(new HistoryCompleterPrivate)
|
2010-09-15 13:39:28 +02:00
|
|
|
{
|
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();
|
2015-02-08 22:28:49 +03:00
|
|
|
d->historyKeyIsLastItemEmpty = QLatin1String("CompleterHistory/")
|
|
|
|
|
+ historyKey + QLatin1String(".IsLastItemEmpty");
|
2020-12-22 16:21:45 +01:00
|
|
|
d->isLastItemEmpty = theSettings->value(d->historyKeyIsLastItemEmpty, isLastItemEmptyDefault)
|
|
|
|
|
.toBool();
|
2010-12-01 15:41:08 +01:00
|
|
|
|
2012-08-22 13:53:21 +02:00
|
|
|
setModel(d);
|
2018-06-12 08:13:33 +02:00
|
|
|
auto popup = new HistoryLineView(d);
|
|
|
|
|
setPopup(popup);
|
|
|
|
|
// setPopup unconditionally sets a delegate on the popup,
|
|
|
|
|
// so we need to set our delegate afterwards
|
|
|
|
|
popup->installDelegate();
|
2010-09-15 13:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
2013-02-04 19:37:45 +08:00
|
|
|
bool HistoryCompleter::removeHistoryItem(int index)
|
|
|
|
|
{
|
|
|
|
|
return d->removeRow(index);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-08 22:28:49 +03:00
|
|
|
QString HistoryCompleter::historyItem() const
|
|
|
|
|
{
|
|
|
|
|
if (historySize() == 0 || d->isLastItemEmpty)
|
|
|
|
|
return QString();
|
|
|
|
|
return d->list.at(0);
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-26 21:47:16 +02:00
|
|
|
bool HistoryCompleter::historyExistsFor(const QString &historyKey)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(theSettings, return false);
|
|
|
|
|
const QString fullKey = QLatin1String("CompleterHistory/") + historyKey;
|
|
|
|
|
return theSettings->value(fullKey).isValid();
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-19 22:26:45 +03:00
|
|
|
HistoryCompleter::~HistoryCompleter()
|
|
|
|
|
{
|
|
|
|
|
delete d;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2014-07-07 15:04:52 +02:00
|
|
|
void HistoryCompleter::addEntry(const QString &str)
|
2010-09-15 13:39:28 +02:00
|
|
|
{
|
2014-07-07 15:04:52 +02:00
|
|
|
d->addEntry(str);
|
2010-09-17 12:49:08 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-22 16:21:45 +01:00
|
|
|
void HistoryCompleter::setSettings(QtcSettings *settings)
|
2012-08-23 13:47:15 +02:00
|
|
|
{
|
|
|
|
|
Internal::theSettings = settings;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-22 13:53:21 +02:00
|
|
|
} // namespace Utils
|