diff --git a/ground/gcs/src/plugins/config/textbubbleslider.cpp b/ground/gcs/src/plugins/config/textbubbleslider.cpp new file mode 100644 index 000000000..689304260 --- /dev/null +++ b/ground/gcs/src/plugins/config/textbubbleslider.cpp @@ -0,0 +1,195 @@ +/** +****************************************************************************** +* +* @file textbubbleslider.h +* @author Tau Labs, http://taulabs.org Copyright (C) 2013. +* @brief Creates a slider with a text bubble showing the slider value +* @see The GNU Public License (GPL) Version 3 +* @defgroup Config +* @{ +* +*****************************************************************************/ +/* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program; if not, write to the Free Software Foundation, Inc., +* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include +#include +#include +#include + +#include "textbubbleslider.h" + +/** + * @brief TextBubbleSlider::TextBubbleSlider Constructs a regular text-bubble slider + * @param parent + */ +TextBubbleSlider::TextBubbleSlider(QWidget *parent) : + QSlider(parent) +{ + construct(); +} + +/** + * @brief TextBubbleSlider::TextBubbleSlider Constructs a text-bubble slider that copys + * all relevant data from the previous slider + * @param copySlider + * @param parent + */ +TextBubbleSlider::TextBubbleSlider(QSlider *copySlider, QWidget *parent) : + QSlider(parent) +{ + construct(); + + // Copy settings + setSizePolicy(copySlider->sizePolicy()); + setMinimumSize(copySlider->minimumSize()); + setMaximumSize(copySlider->maximumSize()); + setFocusPolicy(copySlider->focusPolicy()); + setOrientation(copySlider->orientation()); + setMaximum(copySlider->maximum()); + setMinimum(copySlider->minimum()); + setToolTip(copySlider->toolTip()); +} + + +/** + * @brief TextBubbleSlider::construct This function needs to be called from all constructors. It + * provides a single point where settings can be changed. + */ +void TextBubbleSlider::construct() +{ + font = QFont("Arial", 13); + slideHandleMargin = 2; // This is a dubious way to set the margin. In reality, it should be read from the style sheet. +} + +TextBubbleSlider::~TextBubbleSlider() +{ +} + + +/** + * @brief numIntegerDigits Counts the number of digits in an integer + * @param number Input integer + * @return Number of digits in input integer + */ +unsigned int numIntegerDigits(int number) +{ + unsigned int digits = 0; + + // If there is a negative sign, be sure to include it in digit count + if (number < 0) + digits = 1; + + while (number) { + number /= 10; + digits++; + } + + return digits; +} + + +/** + * @brief TextBubbleSlider::setMaxPixelWidth Sets maximum pixel width for slider handle + */ +void TextBubbleSlider::setMaxPixelWidth() +{ + // Calculate maximum number of digits possible in string + int maxNumDigits = numIntegerDigits(maximum()) > numIntegerDigits(minimum()) ? numIntegerDigits(maximum()) : numIntegerDigits(minimum()); + + // Generate string with maximum pixel width. Suppose that "0" is + // the widest number in pixels. + QString maximumWidthString; + for (int i=0; i +#include + +class QDESIGNER_WIDGET_EXPORT TextBubbleSlider : public QSlider +{ + Q_OBJECT + +public: + explicit TextBubbleSlider(QWidget *parent = 0); + explicit TextBubbleSlider(QSlider *, QWidget *parent = 0); + void construct(); + ~TextBubbleSlider(); + + void setMinimum(int); + void setMaximum(int); + +protected: + void paintEvent ( QPaintEvent * event ); + +private: + void setMaxPixelWidth(); + + QFont font; + int maximumFontWidth; + int maximumFontHeight; + int slideHandleWidth; + int slideHandleMargin; + +}; + +#endif // TEXTBUBBLESLIDER_H