accordion.hpp 6.63 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
#ifndef CVVISUAL_ACCORDION_HPP
#define CVVISUAL_ACCORDION_HPP
// STD
#include <memory>
#include <stdexcept>
#include <map>
#include <limits>
// QT
#include <QWidget>
#include <QString>
#include <QVBoxLayout>
// CVV
#include "collapsable.hpp"
#include "../util/util.hpp"
#include "../util/observer_ptr.hpp"

namespace cvv
{
namespace qtutil
{
/**
 * @brief The Accordion class.
 *
 * Contains multiple widgets and their title. These get stored in collapsables.
 * The collapsables are stored in a collumn.
 */
class Accordion : public QWidget
{
	Q_OBJECT
      public:
	/**
	 * @brief The handle type to access elements
	 */
	using Handle = QWidget *;

	/**
	 * @brief Constructs an empty accordion.
	 * @param parent The parent widget
	 */
	explicit Accordion(QWidget *parent = nullptr);

	~Accordion()
	{
	}

	/**
	 * @brief Returns the element corrsponding to handle
	 * @throw std::out_of_range If there is no element corresponding to
	 * handle
	 * @return The element corrsponding to handle
	 */
	Collapsable &element(Handle handle)
	{
		return *elements_.at(handle);
	}

	const Collapsable &element(Handle handle) const
	{
		return *elements_.at(handle);
	}

	/**
	 * @brief Sets the title above the element.
	 * @param handle The element
	 * @param title The new title.
	 * @throw std::out_of_range If there is no element corresponding to
	 * handle
	 */
	void setTitle(Handle handle, const QString &title)
	{
		element(handle).setTitle(title);
	}

	/**
	 * @brief Returns the current title above the element.
	 * @param handle The element
	 * @throw std::out_of_range If there is no element corresponding to
	 * handle
	 * @return The current title above the element.
	 */
	QString title(Handle handle) const
	{
		return element(handle).title();
	}

	/**
	 * @brief Collapses an element
	 * @param handle The element to collapse
	 * @param b
	 * @parblock
	 * 		true: collapses the widget
	 * 		false: expands the widget
	 * @endparblock
	 * @throw std::out_of_range If there is no element corresponding to
	 * handle
	 */
	void collapse(Handle handle, bool b = true)
	{
		element(handle).collapse(b);
	}

	/**
	 * @brief Expands an element
	 * @param handle Element to expand
	 * @param b
	 * @parblock
	 * 		true: expands the widget
	 * 		false: collapses the widget
	 * @endparblock
	 * @throw std::out_of_range If there is no element corresponding to
	 * handle
	 */
	void expand(Handle handle, bool b = true)
	{
		collapse(handle, !b);
	}

	/**
	 * @brief Collapses all elements
	 * @param b
	 * @parblock
	 * 		true: collapses all elements
	 * 		false: expands all elements
	 * @endparblock
	 */
	void collapseAll(bool b = true);

	/**
	 * @brief Expands all elements
	 * @param b
	 * @parblock
	 * 		true: expands all elements
	 * 		false: collapses all elements
	 * @endparblock
	 */
	void expandAll(bool b = true)
	{
		collapseAll(!b);
	}

	/**
	 * @brief Makes the element invisible
	 * @param handle The element
	 * @param b
	 * @parblock
	 * 		true: makes the element invisible
	 * 		false: makes the element visible
	 * @endparblock
	 * @throw std::out_of_range If there is no element corresponding to
	 * handle
	 */
	void hide(Handle handle, bool b = true)
	{
		element(handle).setVisible(!b);
	}

	/**
	 * @brief Makes the element visible
	 * @param handle The element
	 * @param b
	 * @parblock
	 * 		true: makes the element visible
	 * 		false: makes the element invisible
	 * @endparblock
	 * @throw std::out_of_range If there is no element corresponding to
	 * handle
	 */
	void show(Handle handle, bool b = true)
	{
		hide(handle, !b);
	}

	/**
	 * @brief Sets all elements' visibility to !b
	 * @param b
	 * @parblock
	 * 		true: makes all elements invisible
	 * 		false: makes all elements visible
	 * @endparblock
	 */
	void hideAll(bool b = true);

	/**
	 * @brief Sets all elements' visibility to b
	 * @param b
	 * @parblock
	 * 		true: makes all elements visible
	 * 		false: makes all elements invisible
	 * @endparblock
	 */
	void showAll(bool b = true)
	{
		hideAll(!b);
	}

	/**
	 * @brief Inserts a widget at the given position
	 * @param title The title to display
	 * @param widget The widget to display
	 * @param isCollapsed Whether the widget is collapsed after creation
	 * @param position The position. If it is greater than the number of
	 *elements the widget
	 *	will be added to the end
	 * @return The handle to access the element
	 */
	Handle insert(const QString &title, std::unique_ptr<QWidget> widget,
	              bool isCollapsed = true,
	              std::size_t position =
	                  std::numeric_limits<std::size_t>::max());

	/**
	 * @brief Adds a widget to the end of the Accordion
	 * @param title The title to display
	 * @param widget The widget to display
	 * @param isCollapsed Whether the widget is collapsed after creation
	 * @return The handle to access the element
	 */
	Handle push_back(const QString &title, std::unique_ptr<QWidget> widget,
	                 bool isCollapsed = true)
	{
		return insert(title, std::move(widget), isCollapsed);
	}

	/**
	 * @brief Adds a widget to the front of the Accordion
	 * @param title The title to display
	 * @param widget The widget to display
	 * @param isCollapsed Whether the widget is collapsed after creation
	 * @return The handle to access the element
	 */
	Handle push_front(const QString &title, std::unique_ptr<QWidget> widget,
	                  bool isCollapsed = true)
	{
		return insert(title, std::move(widget), isCollapsed, 0);
	}

	/**
	 * @brief Removes the element and deletes it immediately.
	 * @param handle Handle of the element
	 * @param del
	 * @throw std::out_of_range If there is no element corresponding to
	 * handle
	 */
	void remove(Handle handle);

	/**
	 * @brief Removes all elements and deletes them immediately.
	 * @param del
	 */
	void clear();

	/**
	 * @brief Removes an element and returns its title and Collapsable.
	 * (ownership remains)
	 * @param handle Handle of the element
	 * @throw std::out_of_range If there is no element corresponding to
	 * handle
	 * @return Title and reference
	 */
	std::pair<QString, Collapsable *> pop(Handle handle);

	/**
	 * @brief Removes all elements from the Accordion and returns their
	 *titles
	 *	and Collapsables  (ownership remains)
	 * @return A vector containing all titles and references
	 */
	std::vector<std::pair<QString, Collapsable *>> popAll();

	/**
	 * @brief Returns the number of elements
	 * @return The number of elements
	 */
	std::size_t size() const
	{
		return elements_.size();
	}

      private:
	/**
	 * @brief Storage for all elements
	 */
	std::map<Handle, Collapsable *> elements_;

	/**
	 * @brief Layout for all elements
	 */
	util::ObserverPtr<QVBoxLayout> layout_;
}; // Accordion
}
} // end namespaces qtutil, cvv
#endif // CVVISUAL_ACCORDION_HPP