diff options
Diffstat (limited to '')
| -rw-r--r-- | src/completion/CommandsModel.cpp | 42 | ||||
| -rw-r--r-- | src/completion/Completer.cpp | 46 | ||||
| -rw-r--r-- | src/completion/CompleterDelegate.cpp | 40 |
3 files changed, 128 insertions, 0 deletions
diff --git a/src/completion/CommandsModel.cpp b/src/completion/CommandsModel.cpp new file mode 100644 index 0000000..0f66233 --- /dev/null +++ b/src/completion/CommandsModel.cpp @@ -0,0 +1,42 @@ +#include <QAbstractListModel> +#include <QWidget> +#include <QtCore> + +#include "completion/CommandsModel.hpp" + +CommandsModel::CommandsModel(QObject *parent) : QAbstractListModel(parent) { + items = commands; +} + +QVariant CommandsModel::data(const QModelIndex &index, int role) const { + if (!index.isValid() || index.row() >= items.size()) + return QVariant(); + + const Command &item = items.at(index.row()); + + if (role == Qt::DisplayRole) + return item.name; + else if (role == Qt::ToolTipRole) + return item.description; + else if (role == Qt::UserRole) + return item.description; + + return QVariant(); +} + +QHash<int, QByteArray> CommandsModel::roleNames() const { + QHash<int, QByteArray> roles; + roles[Qt::DisplayRole] = "name"; + roles[Qt::UserRole] = "description"; + return roles; +} + +int CommandsModel::rowCount(const QModelIndex &parent) const { + Q_UNUSED(parent); + return items.size(); +} + +int CommandsModel::columnCount(const QModelIndex &parent) const { + Q_UNUSED(parent); + return 2; // name + description +} diff --git a/src/completion/Completer.cpp b/src/completion/Completer.cpp new file mode 100644 index 0000000..96a363e --- /dev/null +++ b/src/completion/Completer.cpp @@ -0,0 +1,46 @@ +#include <QAbstractItemView> +#include <QHeaderView> +#include <QLineEdit> +#include <QTreeView> +#include <QWidget> +#include <QtCore> + +#include "completion/Completer.hpp" +#include "completion/CompleterDelegate.hpp" + +const char *completerStyles = R"( + background-color: #111; + color: #fff; + border-radius: 0; +)"; + +Completer::Completer(QObject *parent) : QCompleter(parent) { + setFilterMode(Qt::MatchContains); + setCaseSensitivity(Qt::CaseInsensitive); + setCompletionRole(Qt::DisplayRole); + setCompletionMode(QCompleter::PopupCompletion); + + auto treeView = new QTreeView(); + setPopup(treeView); + treeView->setStyleSheet(completerStyles); + treeView->setItemDelegate(new CompleterDelegate(treeView)); + treeView->setRootIsDecorated(false); + treeView->setUniformRowHeights(true); + treeView->header()->hide(); + treeView->setColumnWidth(1, 160); + + complete(); +} + +// bool Completer::eventFilter(QObject *watched, QEvent *event) { +// auto isFocusIn = event->type() == QEvent::FocusIn; +// auto isKeyRelease = event->type() == QEvent::KeyRelease; +// +// QLineEdit *lineEdit = qobject_cast<QLineEdit *>(watched); +// bool isInputEmpty = lineEdit && lineEdit->text().isEmpty(); +// +// if ((isFocusIn || isKeyRelease) && isInputEmpty) +// complete(); +// +// return QCompleter::eventFilter(watched, event); +// } diff --git a/src/completion/CompleterDelegate.cpp b/src/completion/CompleterDelegate.cpp new file mode 100644 index 0000000..a35305f --- /dev/null +++ b/src/completion/CompleterDelegate.cpp @@ -0,0 +1,40 @@ +#include <QPainter> +#include <QWidget> +#include <QtCore/qlogging.h> +#include <QtCore/qnamespace.h> +#include <QtCore> + +#include "completion/CompleterDelegate.hpp" + +CompleterDelegate::CompleterDelegate(QObject *parent) + : QStyledItemDelegate(parent) {} + +void CompleterDelegate::paint(QPainter *painter, + const QStyleOptionViewItem &option, + const QModelIndex &index) const { + painter->save(); + + // Style + if (option.state & QStyle::State_Selected) { + painter->fillRect(option.rect, QColor("#aaa")); + painter->setPen(Qt::black); + } else { + painter->fillRect(option.rect, QColor("#111")); + painter->setPen(Qt::white); + } + + // Draw text + QString text; + QRect rect; + if (index.column() == 0) { + text = index.data(Qt::DisplayRole).toString(); + rect = option.rect.adjusted(5, 0, 0, 0); + painter->drawText(rect, Qt::AlignLeft | Qt::AlignVCenter, text); + } else if (index.column() == 1) { + text = index.data(Qt::UserRole).toString(); + rect = option.rect.adjusted(0, 0, -5, 0); + painter->drawText(rect, Qt::AlignLeft | Qt::AlignVCenter, text); + } + + painter->restore(); +} |
