| 1 | /* |
| 2 | Copyright (C) 2014 Mario Stephan <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Lesser General Public License as published |
| 6 | by the Free Software Foundation; either version 2.1 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public License |
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include "stackdisplay.h" |
| 19 | |
| 20 | #include <QColor> |
| 21 | #include <QPainter> |
| 22 | #include <QStyleOption> |
| 23 | |
| 24 | class StackDisplayPrivate { |
| 25 | public: |
| 26 | int height; |
| 27 | int width; |
| 28 | int margin; |
| 29 | int count; |
| 30 | int indexSelected; |
| 31 | int barHeight; |
| 32 | QColor colorBars; |
| 33 | }; |
| 34 | |
| 35 | StackDisplay::StackDisplay(QWidget* parent) |
| 36 | : QWidget(parent) |
| 37 | { |
| 38 | p = new StackDisplayPrivate; |
| 39 | |
| 40 | p->colorBars.setRgb(200, 200, 200); |
| 41 | |
| 42 | p->count = 6; |
| 43 | p->indexSelected = 1; |
| 44 | p->barHeight = 3; |
| 45 | p->margin = 8; |
| 46 | } |
| 47 | |
| 48 | StackDisplay::~StackDisplay() |
| 49 | { |
| 50 | delete p; |
| 51 | } |
| 52 | |
| 53 | void StackDisplay::resizeEvent(QResizeEvent* e) |
| 54 | { |
| 55 | Q_UNUSED(e); |
| 56 | |
| 57 | p->width = width(); |
| 58 | p->height = height(); |
| 59 | } |
| 60 | |
| 61 | void StackDisplay::setBarColor(QColor color) |
| 62 | { |
| 63 | p->colorBars = color; |
| 64 | update(); |
| 65 | } |
| 66 | |
| 67 | void StackDisplay::setCount(int value) |
| 68 | { |
| 69 | p->count = value; |
| 70 | update(); |
| 71 | } |
| 72 | |
| 73 | void StackDisplay::setSelected(int value) |
| 74 | { |
| 75 | p->indexSelected = value; |
| 76 | update(); |
| 77 | } |
| 78 | |
| 79 | void StackDisplay::setMargin(int value) |
| 80 | { |
| 81 | p->margin = value; |
| 82 | } |
| 83 | |
| 84 | void StackDisplay::paintEvent(QPaintEvent*) |
| 85 | { |
| 86 | drawBars(); |
| 87 | QStyleOption opt; |
| 88 | opt.init(this); |
| 89 | QPainter p(this); |
| 90 | style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); |
| 91 | } |
| 92 | |
| 93 | void StackDisplay::drawBars() |
| 94 | { |
| 95 | |
| 96 | QPainter painter(this); |
| 97 | painter.setPen(p->colorBars); |
| 98 | |
| 99 | if (p->count == 0) |
| 100 | return; |
| 101 | |
| 102 | int space = (int)(p->height / p->count); |
| 103 | int posY = 0; |
| 104 | |
| 105 | for (int i = 0; i < p->count; i++) { |
| 106 | posY = posY + space; |
| 107 | for (int j = 0; j < p->barHeight; j++) { |
| 108 | //draw bars |
| 109 | painter.drawLine(p->margin * 2, |
| 110 | posY + j - p->barHeight, |
| 111 | p->width - p->margin, |
| 112 | posY + j - p->barHeight); |
| 113 | } |
| 114 | if (i == p->indexSelected) { |
| 115 | //draw triancle |
| 116 | for (int j = p->margin; j < p->margin + 5; j++) { |
| 117 | painter.drawLine(j, |
| 118 | posY - (p->margin + 5 - j) + 1 - p->barHeight, |
| 119 | j, |
| 120 | posY + (p->margin + 5 - j) - 1 - p->barHeight); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |