collapsable.hpp 2.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
#ifndef CVVISUAL_COLLAPSABLE_H
#define CVVISUAL_COLLAPSABLE_H
// std
#include <cstddef>
// QT
#include <QString>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>
#include <QFrame>

#include "../util/util.hpp"
#include "../util/observer_ptr.hpp"

namespace cvv
{
namespace qtutil
{

/**
 * @brief Contains a widget and a title.
 *
 * The widget can be collapsed and expanded with a button.
 * If the widget is collapsed only button and title are shown.
 */
class Collapsable : public QFrame
{
	Q_OBJECT
      public:
	/**
	 * @brief Constructs a collapsable
	 * @param title The title above the widget.
	 * @param widget The widget to store.
	 * @param isCollapsed If true the contained widget will be collapsed.
	 * (It will be shown
	 * otherwise.)
	 */
	// explicit Collapsable(const QString& title, QWidget& widget, bool
	// isCollapsed = true,
	//		QWidget *parent = 0);
	explicit Collapsable(const QString &title,
	                     std::unique_ptr<QWidget> widget,
	                     bool isCollapsed = true, QWidget *parent = 0);

	~Collapsable()
	{
	}

	/**
	 * @brief Collapses the contained widget.
	 * @param b
	 * @parblock
	 * 		true: collapses the widget
	 * 		false: expands the widget
	 * @endparblock
	 */
	void collapse(bool b = true);

	/**
	 * @brief Expands the contained widget.
	 * @param b
	 * @parblock
	 * 		true: expands the widget
	 * 		false: collapses the widget
	 * @endparblock
	*/
	void expand(bool b = true)
	{
		collapse(!b);
	}

	/**
	* @brief Sets the title above the widget.
	*/
	void setTitle(const QString &title)
	{
		button_->setText(title);
	}

	/**
	 * @brief Returns the current title above the widget.
	 * @return The current title above the widget
	 */
	QString title() const
	{
		return button_->text();
	}

	/**
	 * @brief Returns a reference to the contained widget.
	 * @return A reference to the contained widget.
	 */
	QWidget &widget()
	{
		return *widget_;
	}

	const QWidget &widget() const
	{
		return *widget_;
	}

	/**
	 * @brief Detaches the contained widget. (ownership remains)
	 * @return The contained widget
	 */
	QWidget *detachWidget();

      private
slots:
	/**
	 * @brief Toggles the visibility.
	 */
	void toggleVisibility()
	{
		collapse(widget_->isVisible());
	}

      private:
	/**
	 * @brief The contained widget
	 */
	QWidget *widget_;

	/**
	 * @brief The button to toggle the widget
	 */
	QPushButton *button_;

	/**
	 * @brief The layout containing the header and widget
	 */
	util::ObserverPtr<QVBoxLayout> layout_;
}; // Collapsable
}
} // end namespaces qtutil, cvv

#endif // CVVISUAL_COLLAPSABLE_H