Commit eaee72c4 authored by hjk's avatar hjk

historycompleter: even less use of object names.

This allows history to be shared between similar but different
line edits.

Change-Id: I6ddaa686f99cf0d89e9be024ccb006d179a6bd1c
Reviewed-by: default avatarhjk <qthjk@ovi.com>
parent c685abeb
...@@ -29,19 +29,18 @@ ...@@ -29,19 +29,18 @@
**************************************************************************/ **************************************************************************/
#include "historycompleter.h" #include "historycompleter.h"
#include "qtcassert.h"
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QSettings> #include <QSettings>
#include <QLineEdit>
#include <QKeyEvent>
#include <QItemDelegate> #include <QItemDelegate>
#include <QKeyEvent>
#include <QLineEdit>
#include <QListView> #include <QListView>
#include <QPainter> #include <QPainter>
#include <QStyle> #include <QStyle>
static const char SETTINGS_PREFIX[] = "CompleterHistory/";
namespace Utils { namespace Utils {
namespace Internal { namespace Internal {
...@@ -59,7 +58,7 @@ public: ...@@ -59,7 +58,7 @@ public:
bool eventFilter(QObject *obj, QEvent *event); bool eventFilter(QObject *obj, QEvent *event);
QStringList list; QStringList list;
QByteArray historyKey; QString historyKey;
HistoryCompleter *completer; HistoryCompleter *completer;
QWidget *lastSeenWidget; QWidget *lastSeenWidget;
QSettings *settings; QSettings *settings;
...@@ -122,15 +121,13 @@ HistoryCompleterPrivate::HistoryCompleterPrivate(HistoryCompleter *parent) ...@@ -122,15 +121,13 @@ HistoryCompleterPrivate::HistoryCompleterPrivate(HistoryCompleter *parent)
void HistoryCompleterPrivate::fetchHistory() void HistoryCompleterPrivate::fetchHistory()
{ {
if (!completer->widget() || !settings) { QTC_ASSERT(settings, return);
if (!completer->widget()) {
list.clear(); list.clear();
reset(); reset();
return; return;
} }
QString objectName = completer->widget()->objectName(); list = settings->value(historyKey).toStringList();
if (objectName.isEmpty())
return;
list = settings->value(QLatin1String(SETTINGS_PREFIX) + objectName).toStringList();
reset(); reset();
} }
...@@ -168,11 +165,7 @@ bool HistoryCompleterPrivate::removeRows(int row, int count, const QModelIndex & ...@@ -168,11 +165,7 @@ bool HistoryCompleterPrivate::removeRows(int row, int count, const QModelIndex &
{ {
beginRemoveRows (parent, row, row + count); beginRemoveRows (parent, row, row + count);
list.removeAt(row); list.removeAt(row);
if (settings) { settings->setValue(historyKey, list);
QString objectName = completer->widget()->objectName();
settings->setValue(QLatin1String(SETTINGS_PREFIX) + objectName, list);
}
endRemoveRows(); endRemoveRows();
return true; return true;
} }
...@@ -198,15 +191,11 @@ void HistoryCompleterPrivate::saveEntry(const QString &str) ...@@ -198,15 +191,11 @@ void HistoryCompleterPrivate::saveEntry(const QString &str)
fetchHistory(); fetchHistory();
lastSeenWidget = completer->widget(); lastSeenWidget = completer->widget();
} }
QString objectName = completer->widget()->objectName();
if (objectName.isEmpty())
return;
beginInsertRows (QModelIndex(), list.count(), list.count()); beginInsertRows (QModelIndex(), list.count(), list.count());
list.prepend(str); list.prepend(str);
list = list.mid(0, maxLines); list = list.mid(0, maxLines);
endInsertRows(); endInsertRows();
if (settings) settings->setValue(historyKey, list);
settings->setValue(QLatin1String(SETTINGS_PREFIX) + objectName, list);
} }
bool HistoryCompleterPrivate::eventFilter(QObject *obj, QEvent *event) bool HistoryCompleterPrivate::eventFilter(QObject *obj, QEvent *event)
...@@ -218,28 +207,26 @@ bool HistoryCompleterPrivate::eventFilter(QObject *obj, QEvent *event) ...@@ -218,28 +207,26 @@ bool HistoryCompleterPrivate::eventFilter(QObject *obj, QEvent *event)
return QAbstractListModel::eventFilter(obj,event); return QAbstractListModel::eventFilter(obj,event);
} }
HistoryCompleter::HistoryCompleter(QSettings *settings, QObject *parent, const QByteArray &historyKey) HistoryCompleter::HistoryCompleter(QSettings *settings, QObject *parent, const QString &historyKey)
: QCompleter(parent) : QCompleter(parent)
, d(new HistoryCompleterPrivate(this)) , d(new HistoryCompleterPrivate(this))
{ {
QTC_ASSERT(settings, return);
d->settings = settings; d->settings = settings;
// make an assumption to allow pressing of the down // make an assumption to allow pressing of the down
// key, before the first model run: // key, before the first model run:
// parent is likely the lineedit // parent is likely the lineedit
if (historyKey.isEmpty()) if (historyKey.isEmpty())
d->historyKey = parent->objectName().toLatin1(); d->historyKey = parent->objectName();
else else
d->historyKey = historyKey; d->historyKey = historyKey;
if (d->historyKey.isEmpty()) if (d->historyKey.isEmpty())
return; return;
d->historyKey = QLatin1String("CompleterHistory/") + d->historyKey;
QWidget *p = qobject_cast<QWidget *>(parent); parent->installEventFilter(d);
if (p) { d->list = d->settings->value(d->historyKey).toStringList();
p->installEventFilter(d);
if (d->settings)
d->list = d->settings->value(QLatin1String(SETTINGS_PREFIX) + d->historyKey).toStringList();
}
QLineEdit *l = qobject_cast<QLineEdit *>(parent); QLineEdit *l = qobject_cast<QLineEdit *>(parent);
if (l && d->list.count()) if (l && d->list.count())
......
...@@ -35,13 +35,13 @@ ...@@ -35,13 +35,13 @@
#include <QCompleter> #include <QCompleter>
QT_FORWARD_DECLARE_CLASS(QSettings) QT_BEGIN_NAMESPACE
class QSettings;
QT_END_NAMESPACE
namespace Utils { namespace Utils {
namespace Internal { namespace Internal { class HistoryCompleterPrivate; }
class HistoryCompleterPrivate;
}
class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter
{ {
...@@ -49,7 +49,7 @@ class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter ...@@ -49,7 +49,7 @@ class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter
public: public:
HistoryCompleter(QSettings *settings, QObject *parent, HistoryCompleter(QSettings *settings, QObject *parent,
const QByteArray &historyKey = QByteArray()); const QString &historyKey = QString());
~HistoryCompleter(); ~HistoryCompleter();
int historySize() const; int historySize() const;
int maximalHistorySize() const; int maximalHistorySize() const;
...@@ -61,7 +61,6 @@ public Q_SLOTS: ...@@ -61,7 +61,6 @@ public Q_SLOTS:
private: private:
Internal::HistoryCompleterPrivate *d; Internal::HistoryCompleterPrivate *d;
}; };
} // namespace Utils } // namespace Utils
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment