1
/****************************************************************************
2
* Copyright (C) 2013-2014 by Savoir-Faire Linux *
3
* Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com> *
5
* This library is free software; you can redistribute it and/or *
6
* modify it under the terms of the GNU Lesser General Public *
7
* License as published by the Free Software Foundation; either *
8
* version 2.1 of the License, or (at your option) any later version. *
10
* This library is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13
* Lesser General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
17
***************************************************************************/
18
#include "autocompletiondelegate.h"
20
#include <QtGui/QPainter>
21
#include <QtGui/QFontMetrics>
22
#include <QtGui/QApplication>
24
#include <lib/numbercompletionmodel.h>
26
static const int RIGHT_MARGIN = 2;
28
AutoCompletionDelegate::AutoCompletionDelegate() : QStyledItemDelegate()
33
void AutoCompletionDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
35
QStyledItemDelegate::paint(painter,option,index);
36
const QString tag = index.data(Qt::UserRole).toString();
39
painter->setRenderHint(QPainter::Antialiasing, true);
40
painter->setClipRect(option.rect);
41
static bool init = false;
43
static QLinearGradient g;
47
f.setPointSize(f.pointSize()-1);
48
g = QLinearGradient(QPointF(0, 0), QPointF(0, option.rect.height()-4));
49
QColor col = QApplication::palette().color(QPalette::Highlight);
56
static QFontMetrics m(f);
57
static int height = m.height();
58
static int topMargin = 2;
60
const int width = m.width(tag);
61
const int x = option.rect.x()+option.rect.width()-width-RIGHT_MARGIN-option.rect.height()/2;
63
painter->setPen(Qt::NoPen);
64
painter->drawRect(QRect(x-option.rect.height()/2+option.rect.height()/2,option.rect.y()+2,width,option.rect.height()-4));
65
painter->setClipRect(x-option.rect.height()/2,option.rect.y()+2,option.rect.height()/2,option.rect.height()-4);
66
painter->drawEllipse(QRect(x-option.rect.height()/2,option.rect.y()+2,option.rect.height()-4,option.rect.height()-4));
67
painter->setClipRect(x+width,option.rect.y()+2,option.rect.height()-4,option.rect.height()-4);
68
painter->drawEllipse(QRect(x+width-option.rect.height()/2,option.rect.y()+2,option.rect.height()-4,option.rect.height()-4));
69
painter->setPen(QApplication::palette().base().color());
70
painter->setClipping(false);
71
painter->drawText(x,option.rect.y()+height-topMargin,tag);