aboutsummaryrefslogtreecommitdiff
path: root/src/completion/CompleterDelegate.cpp
blob: ce446b2584db6e67d81bab38feaed7621bed512f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#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::setCurrentRow(uint32_t r) { row = r; }

uint32_t CompleterDelegate::currentRow() { return row; }

void CompleterDelegate::paint(QPainter *painter,
                              const QStyleOptionViewItem &option,
                              const QModelIndex &index) const {
  painter->save();

  bool selected = index.row() == row;

  // Style
  if (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();
}