historycompleter.cpp 6.63 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11
** 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
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
30 31

#include "historycompleter.h"
32
#include "fancylineedit.h"
33 34
#include "theme/theme.h"
#include "utilsicons.h"
hjk's avatar
hjk committed
35

36
#include "qtcassert.h"
37

38
#include <QSettings>
39

40
#include <QItemDelegate>
41
#include <QKeyEvent>
42 43
#include <QListView>
#include <QPainter>
44 45

namespace Utils {
Eike Ziller's avatar
Eike Ziller committed
46
namespace Internal {
47

hjk's avatar
hjk committed
48 49
static QSettings *theSettings = 0;

hjk's avatar
hjk committed
50
class HistoryCompleterPrivate : public QAbstractListModel
51 52
{
public:
53
    HistoryCompleterPrivate() : maxLines(30) {}
hjk's avatar
hjk committed
54

hjk's avatar
hjk committed
55 56 57 58
    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());

59
    void clearHistory();
60
    void addEntry(const QString &str);
61 62

    QStringList list;
63
    QString historyKey;
64 65
    bool isLastItemEmpty;
    QString historyKeyIsLastItemEmpty;
66 67 68
    int maxLines;
};

69 70 71
class HistoryLineDelegate : public QItemDelegate
{
public:
72 73
    HistoryLineDelegate(QObject *parent)
        : QItemDelegate(parent)
74
        , pixmap(Icons::EDIT_CLEAR.pixmap())
hjk's avatar
hjk committed
75 76 77 78 79 80 81 82 83
    {}

    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);
    }

84 85 86 87 88 89
    QPixmap pixmap;
};

class HistoryLineView : public QListView
{
public:
hjk's avatar
hjk committed
90 91 92
    HistoryLineView(HistoryCompleterPrivate *model_)
        : model(model_)
    {
93
        HistoryLineDelegate *delegate = new HistoryLineDelegate(this);
hjk's avatar
hjk committed
94 95 96
        pixmapWidth = delegate->pixmap.width();
        setItemDelegate(delegate);
    }
hjk's avatar
hjk committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

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;
112 113 114
    int pixmapWidth;
};

Eike Ziller's avatar
Eike Ziller committed
115 116
} // namespace Internal

hjk's avatar
hjk committed
117
using namespace Internal;
118

hjk's avatar
hjk committed
119
int HistoryCompleterPrivate::rowCount(const QModelIndex &parent) const
120
{
hjk's avatar
hjk committed
121
    return parent.isValid() ? 0 : list.count();
122 123
}

hjk's avatar
hjk committed
124
QVariant HistoryCompleterPrivate::data(const QModelIndex &index, int role) const
125
{
hjk's avatar
hjk committed
126
    if (index.row() >= list.count() || index.column() != 0)
127
        return QVariant();
hjk's avatar
hjk committed
128 129 130
    if (role == Qt::DisplayRole || role == Qt::EditRole)
        return list.at(index.row());
    return QVariant();
131 132
}

hjk's avatar
hjk committed
133
bool HistoryCompleterPrivate::removeRows(int row, int count, const QModelIndex &parent)
134
{
135 136 137 138 139 140
    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);
hjk's avatar
hjk committed
141
    theSettings->setValue(historyKey, list);
142 143 144 145
    endRemoveRows();
    return true;
}

hjk's avatar
hjk committed
146
void HistoryCompleterPrivate::clearHistory()
147
{
148
    beginResetModel();
149
    list.clear();
150
    endResetModel();
151 152
}

153
void HistoryCompleterPrivate::addEntry(const QString &str)
154
{
155
    QTC_ASSERT(theSettings, return);
156
    const QString entry = str.trimmed();
157 158 159
    if (entry.isEmpty()) {
        isLastItemEmpty = true;
        theSettings->setValue(historyKeyIsLastItemEmpty, isLastItemEmpty);
160
        return;
161
    }
Yuchen Deng's avatar
Yuchen Deng committed
162
    int removeIndex = list.indexOf(entry);
163
    beginResetModel();
164
    if (removeIndex != -1)
165
        list.removeAt(removeIndex);
Yuchen Deng's avatar
Yuchen Deng committed
166
    list.prepend(entry);
167 168
    list = list.mid(0, maxLines - 1);
    endResetModel();
hjk's avatar
hjk committed
169
    theSettings->setValue(historyKey, list);
170 171
    isLastItemEmpty = false;
    theSettings->setValue(historyKeyIsLastItemEmpty, isLastItemEmpty);
172 173
}

174
HistoryCompleter::HistoryCompleter(const QString &historyKey, QObject *parent)
Orgad Shaneh's avatar
Orgad Shaneh committed
175 176
    : QCompleter(parent),
      d(new HistoryCompleterPrivate)
177
{
hjk's avatar
hjk committed
178
    QTC_ASSERT(!historyKey.isEmpty(), return);
hjk's avatar
hjk committed
179
    QTC_ASSERT(theSettings, return);
180
    setCompletionMode(QCompleter::UnfilteredPopupCompletion);
hjk's avatar
hjk committed
181

hjk's avatar
hjk committed
182 183
    d->historyKey = QLatin1String("CompleterHistory/") + historyKey;
    d->list = theSettings->value(d->historyKey).toStringList();
184 185 186
    d->historyKeyIsLastItemEmpty = QLatin1String("CompleterHistory/")
        + historyKey + QLatin1String(".IsLastItemEmpty");
    d->isLastItemEmpty = theSettings->value(d->historyKeyIsLastItemEmpty, false).toBool();
187

hjk's avatar
hjk committed
188
    setModel(d);
hjk's avatar
hjk committed
189
    setPopup(new HistoryLineView(d));
190 191
}

192 193 194 195 196
bool HistoryCompleter::removeHistoryItem(int index)
{
    return d->removeRow(index);
}

197 198 199 200 201 202 203
QString HistoryCompleter::historyItem() const
{
    if (historySize() == 0 || d->isLastItemEmpty)
        return QString();
    return d->list.at(0);
}

Orgad Shaneh's avatar
Orgad Shaneh committed
204 205 206 207 208
HistoryCompleter::~HistoryCompleter()
{
    delete d;
}

209 210
int HistoryCompleter::historySize() const
{
hjk's avatar
hjk committed
211
    return d->rowCount();
212 213 214 215
}

int HistoryCompleter::maximalHistorySize() const
{
hjk's avatar
hjk committed
216
    return d->maxLines;
217 218 219 220
}

void HistoryCompleter::setMaximalHistorySize(int numberOfEntries)
{
hjk's avatar
hjk committed
221
    d->maxLines = numberOfEntries;
222 223 224 225
}

void HistoryCompleter::clearHistory()
{
hjk's avatar
hjk committed
226
    d->clearHistory();
227 228
}

229
void HistoryCompleter::addEntry(const QString &str)
230
{
231
    d->addEntry(str);
232 233
}

hjk's avatar
hjk committed
234 235 236 237 238
void HistoryCompleter::setSettings(QSettings *settings)
{
    Internal::theSettings = settings;
}

hjk's avatar
hjk committed
239
} // namespace Utils