1 /***************************************************************************
2 * This file is part of the Lime Report project *
3 * Copyright (C) 2015 by Alexander Arin *
4 * arin_a@bk.ru *
5 * *
6 ** GNU General Public License Usage **
7 * *
8 * This library is free software: you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation, either version 3 of the License, or *
11 * (at your option) any later version. *
12 * You should have received a copy of the GNU General Public License *
13 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
14 * *
15 ** GNU Lesser General Public License **
16 * *
17 * This library is free software: you can redistribute it and/or modify *
18 * it under the terms of the GNU Lesser General Public License as *
19 * published by the Free Software Foundation, either version 3 of the *
20 * License, or (at your option) any later version. *
21 * You should have received a copy of the GNU Lesser General Public *
22 * License along with this library. *
23 * If not, see <http://www.gnu.org/licenses/>. *
24 * *
25 * This library is distributed in the hope that it will be useful, *
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
28 * GNU General Public License for more details. *
29 ****************************************************************************/
30 #include "lrcoloreditor.h"
31 #include "lrglobal.h"
32
33 #include <QHBoxLayout>
34 #include <QColorDialog>
35 #include <QPaintEvent>
36 #include <QPainter>
37 #include <QApplication>
38 #include <QStyle>
39
40 namespace LimeReport{
41
ColorEditor(QWidget * parent)42 ColorEditor::ColorEditor(QWidget *parent) :
43 QWidget(parent), m_buttonPressed(false)
44 {
45 m_colorIndicator = new ColorIndicator(this);
46 m_colorIndicator->setColor(m_color);
47 m_button = new QToolButton(this);
48 m_button->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
49 m_button->setText("...");
50 m_button->installEventFilter(this);
51 QHBoxLayout* layout = new QHBoxLayout(this);
52 layout->addWidget(m_colorIndicator);
53 layout->addWidget(m_button);
54 layout->setSpacing(0);
55 layout->setContentsMargins(1,1,1,1);
56 setFocusProxy(m_button);
57 setAutoFillBackground(true);
58 setLayout(layout);
59 setAutoFillBackground(true);
60 connect(m_button,SIGNAL(clicked()),this,SLOT(slotClicked()));
61 }
62
setColor(const QColor & value)63 void ColorEditor::setColor(const QColor &value)
64 {
65 m_color=value;
66 m_colorIndicator->setColor(m_color);
67 }
68
eventFilter(QObject * obj,QEvent * event)69 bool ColorEditor::eventFilter(QObject *obj, QEvent *event)
70 {
71 if (obj == m_button){
72 if (event->type() == QEvent::FocusOut && !m_buttonPressed){
73 QFocusEvent* focusEvent = dynamic_cast<QFocusEvent*>(event);
74 if (focusEvent && focusEvent->reason()!=Qt::MouseFocusReason){
75 setFocusToParent();
76 emit(editingFinished());
77 }
78 return false;
79 }
80 }
81 return false;
82 }
83
setFocusToParent()84 void ColorEditor::setFocusToParent(){
85 if (parentWidget())
86 parentWidget()->setFocus();
87 }
88
slotClicked()89 void ColorEditor::slotClicked()
90 {
91 m_buttonPressed = true;
92 QColorDialog* dialog = new QColorDialog(this);
93 dialog->setCurrentColor(m_color);
94 if (dialog->exec()) m_color=dialog->currentColor();
95 delete dialog;
96 setFocusToParent();
97 emit(editingFinished());
98 }
99
paintEvent(QPaintEvent * event)100 void ColorIndicator::paintEvent(QPaintEvent* event)
101 {
102 QPainter painter(this);
103
104 QStyle* style = QApplication::style();
105 painter.save();
106 painter.setBrush(m_color);
107 QColor penColor = isColorDark(m_color) ? Qt::transparent : Qt::darkGray;
108
109 painter.setPen(penColor);
110 int border = (event->rect().height() - style->pixelMetric(QStyle::PM_IndicatorWidth))/2;
111
112 QRect rect(event->rect().x()+border, event->rect().y()+border,
113 style->pixelMetric(QStyle::PM_IndicatorWidth),
114 style->pixelMetric(QStyle::PM_IndicatorWidth));// = option.rect.adjusted(4,4,-4,-6);
115
116 painter.drawRect(rect);
117 painter.restore();
118 }
119
ColorIndicator(QWidget * parent)120 ColorIndicator::ColorIndicator(QWidget *parent)
121 :QWidget(parent), m_color(Qt::white){
122 setAttribute(Qt::WA_StaticContents);
123 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
124 setFocusPolicy(Qt::NoFocus);
125 }
126
color() const127 QColor ColorIndicator::color() const
128 {
129 return m_color;
130 }
131
setColor(const QColor & color)132 void ColorIndicator::setColor(const QColor &color)
133 {
134 m_color = color;
135 }
136
sizeHint() const137 QSize ColorIndicator::sizeHint() const
138 {
139 return QSize(20,20);
140 }
141
142 } // namespace LimeReport
143