Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / ground / gcs / src / libs / utils / textbubbleslider.cpp
blobb3ee79bad49833afd315640263e9a20dd9bc17ca
1 /**
2 ******************************************************************************
4 * @file textbubbleslider.h
5 * @author Tau Labs, http://taulabs.org Copyright (C) 2013.
6 * @brief Creates a slider with a text bubble showing the slider value
7 * @see The GNU Public License (GPL) Version 3
8 * @defgroup
9 * @{
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <QDebug>
29 #include <QPainter>
30 #include <QtGui>
31 #include <qmath.h>
33 #include "textbubbleslider.h"
35 /**
36 * @brief TextBubbleSlider::TextBubbleSlider Constructs a regular text-bubble slider
37 * @param parent
39 TextBubbleSlider::TextBubbleSlider(QWidget *parent) :
40 QSlider(parent)
42 QFontDatabase::addApplicationFont(":/utils/fonts/PTS75F.ttf");
44 construct();
47 /**
48 * @brief TextBubbleSlider::TextBubbleSlider Constructs a text-bubble slider that copys
49 * all relevant data from the previous slider
50 * @param copySlider
51 * @param parent
53 TextBubbleSlider::TextBubbleSlider(QSlider *copySlider, QWidget *parent) :
54 QSlider(parent)
56 construct();
58 // Copy settings
59 setSizePolicy(copySlider->sizePolicy());
60 setMinimumSize(copySlider->minimumSize());
61 setMaximumSize(copySlider->maximumSize());
62 setFocusPolicy(copySlider->focusPolicy());
63 setOrientation(copySlider->orientation());
64 setMaximum(copySlider->maximum());
65 setMinimum(copySlider->minimum());
66 setToolTip(copySlider->toolTip());
70 /**
71 * @brief TextBubbleSlider::construct This function needs to be called from all constructors. It
72 * provides a single point where settings can be changed.
74 void TextBubbleSlider::construct()
76 font = QFont("PT Sans", 13, QFont::Bold);
77 slideHandleMargin = 2; // This is a dubious way to set the margin. In reality, it should be read from the style sheet.
80 TextBubbleSlider::~TextBubbleSlider()
84 /**
85 * @brief numIntegerDigits Counts the number of digits in an integer
86 * @param number Input integer
87 * @return Number of digits in input integer
89 unsigned int numIntegerDigits(int number)
91 unsigned int digits = 0;
93 // If there is a negative sign, be sure to include it in digit count
94 if (number < 0) {
95 digits = 1;
98 while (number) {
99 number /= 10;
100 digits++;
103 return digits;
108 * @brief TextBubbleSlider::setMaxPixelWidth Sets maximum pixel width for slider handle
110 void TextBubbleSlider::setMaxPixelWidth()
112 // Calculate maximum number of digits possible in string
113 int maxNumDigits = numIntegerDigits(maximum()) > numIntegerDigits(minimum()) ? numIntegerDigits(maximum()) : numIntegerDigits(minimum());
115 // Generate string with maximum pixel width. Suppose that "0" is
116 // the widest number in pixels.
117 QString maximumWidthString;
119 for (int i = 0; i < maxNumDigits; i++) {
120 maximumWidthString.append("0");
123 // Calculate maximum possible pixel width for string.
124 QFontMetrics fontMetrics(font);
125 maximumFontWidth = fontMetrics.width(QString("%1").arg(maximumWidthString));
126 maximumFontHeight = fontMetrics.height();
128 // Override stylesheet slider handle width
129 slideHandleWidth = maximumFontWidth + 6;
130 setStyleSheet(QString("QSlider::handle:horizontal { width: %1px; margin: -5px 0;}").arg(slideHandleWidth));
135 * @brief TextBubbleSlider::setMinimum Reimplements setMinimum. Ensures that the slider
136 * handle is the correct size for the text field.
137 * @param max maximum
139 void TextBubbleSlider::setMinimum(int max)
141 // Pass value on to QSlider
142 QSlider::setMinimum(max);
144 // Reset handler size
145 setMaxPixelWidth();
150 * @brief TextBubbleSlider::setMaximum Reimplements setMaximum. Ensures that the slider
151 * handle is the correct size for the text field.
152 * @param max maximum
154 void TextBubbleSlider::setMaximum(int max)
156 // Pass value on to QSlider
157 QSlider::setMaximum(max);
159 // Reset handler size
160 setMaxPixelWidth();
165 * @brief TextBubbleSlider::paintEvent Reimplements QSlider::paintEvent.
166 * @param bob
168 void TextBubbleSlider::paintEvent(QPaintEvent *paintEvent)
170 // Pass paint event on to QSlider
171 QSlider::paintEvent(paintEvent);
173 /* Add numbers on top of handler */
175 // Calculate pixel position for text.
176 int sliderWidth = width();
177 int sliderHeight = height();
178 double valuePos;
180 if (!invertedAppearance()) {
181 valuePos = (slideHandleWidth - maximumFontWidth) / 2 + slideHandleMargin + // First part centers text in handle...
182 (value() - minimum()) / (double)(maximum() - minimum()) * (sliderWidth - (slideHandleWidth + slideHandleMargin) - 1); // ... and second part moves text with handle
183 } else {
184 valuePos = (slideHandleWidth - maximumFontWidth) / 2 + slideHandleMargin + // First part centers text in handle...
185 (maximum() - value()) / (double)(maximum() - minimum()) * (sliderWidth - (slideHandleWidth + slideHandleMargin) - 1); // ... and second part moves text with handle
188 // Create painter and set font
189 QPainter painter(this);
190 painter.setFont(font);
191 painter.setPen(QPen(QColor(80, 80, 80)));
193 // Draw neutral value text. Vertically center it in the handle
194 QString neutralStringWidth = QString("%1").arg(value());
195 QFontMetrics fontMetrics(font);
196 int textWidth = fontMetrics.width(neutralStringWidth);
197 painter.drawText(QRectF(valuePos + maximumFontWidth - textWidth, ceil((sliderHeight - maximumFontHeight) / 2.0), textWidth, maximumFontHeight),
198 neutralStringWidth);